0

I have a predicate that generates sublists like this,

?- f([1,2,3,#,5,6,7,8,#,10,11,12],L).

L = [1,2,3];

L=[5,6,7,8];

L=[10,11,12];

false.

When I call it with variables instead of constants they unify with # ruining the output and stopping them from unifying later. How can I stop he variables from unifying with # like this?

false
  • 10,264
  • 13
  • 101
  • 209

1 Answers1

0

stop the variables from unifying with #

this is general method available only if your Prolog supports dif/2 (SWI-Prolog does).

?- f(..., L), maplist(dif(#), L).

Of course, there could be some better place in your code where to place the dif/2 calls, or you could use the standard ISO operator (\==)/2 to avoid unification in first place. If is such strategy feasible most depends on your code. Generally constraints work better - from the efficiency viewpoint - when we give them a chance to restrict the search space as soon as possible.

CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • Thanks but it still does not work since unification with # happens in the input list and I get a bunch of lists that do not end with a #. And I can't do that on the input list since it needs the # to mark the sublists. – Tomás Rocha May 02 '20 at 12:00
  • 1
    Why not [iso_dif/2](https://stackoverflow.com/q/26720685/772868) for the general case? – false May 02 '20 at 12:56
  • @false: didn't recall iso_dif/2, sorry... I agree could be a better choice than \==, alas seems OP's problem cannot be solved in such a simple way – CapelliC May 02 '20 at 13:04
  • Do you prefer the name `dif_si/2`? si stands for safe inference or [sufficiently instantiated](https://stackoverflow.com/a/30600104/772868) or simply si! – false May 02 '20 at 23:17