7

I have a very weird problem with PROLOG. I have used it before, but it's been a while and I'm rusty. I have a list of variables and I need to ensure that none of them are the same.

I have tried:

use_module(library(bounds)). all_different(A, B, C, D, 6, 8).

However, when I try this, I get an error saying that all_different/6 is undefined.

How can I go about solving this issue? Is there any library function that I can call directly for this?

I am VERY stuck and would greatly appreciate any help.

Thanks in advance.

solve([
    [A, 6, 1],

    [B, 5, C, 2, D, E, F, G, 6],

    [6, H, I, 5, J, K, 2, L, 3],

    [5, M, 6, N, O, 4, P, Q, 5],

    [4, R, S, T, U, 6, V, 4, W],

    [2, 0, X]
  ]) :-
     all_different([A,6,1,2,D,E]),
     all_different([B,5,C,6,H,I]),
     all_different([C,2,D,I,5,J]),
     all_different([D,E,F,J,K,2]),
     all_different([F,G,6,2,L,3]),
     all_different([H,I,5,M,6,N]),
     all_different([5,J,K,N,O,4]),
     all_different([K,2,L,4,P,Q]),
     all_different([5,M,6,4,R,S]),
     all_different([6,N,O,S,T,U]),
     all_different([O,4,P,U,6,V]),
     all_different([P,Q,5,V,4,W]),
     all_different([T,U,6,2,1,X]),

     A<7, A>0,       B<7, B>0,       C<7, C>0,       D<7, D>0,
     E<7, E>0,       F<7, F>0,       G<7, G>0,       H<7, H>0,
     I<7, I>0,       J<7, J>0,       K<7, K>0,       L<7, L>0,
     M<7, M>0,       N<7, N>0,       O<7, O>0,       P<7, P>0,
     Q<7, Q>0,       R<7, R>0,       S<7, S>0,       T<7, T>0,
     U<7, U>0,       V<7, V>0,       W<7, W>0,       X<7, X>0.
false
  • 10,264
  • 13
  • 101
  • 209
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241

3 Answers3

9
all_different([A,B,C,D,6,8]).

I believe that only a List can be passed into all_different.

Kevin Crowell
  • 10,082
  • 4
  • 35
  • 51
  • I tried that and got this error: Undefined procedure: all_different/1. Any thoughts? – inspectorG4dget Mar 21 '10 at 03:25
  • 1
    I assume you still had this: :- use_module(library(bounds)). – Kevin Crowell Mar 21 '10 at 04:12
  • I do still have use_module(library(bounds)) – inspectorG4dget Mar 21 '10 at 04:34
  • What version of prolog are you using? Type and number. – Kevin Crowell Mar 21 '10 at 04:42
  • 1
    hmmmm, I see now reason why it shouldn't work. Can you post the entire code snippet by editing your OP? – Kevin Crowell Mar 21 '10 at 04:49
  • 1
    Try :- use_module(library(clpfd)). – Kevin Crowell Mar 21 '10 at 04:52
  • No luck. It still says: ERROR: solve/1: Undefined procedure: all_different/1 – inspectorG4dget Mar 21 '10 at 05:01
  • 1
    Lets see if we can narrow this down. Try cutting your procedure down so that the only all_different function reads all_different([A,B,C]). See if that works. Then, try all_different([1,2,3]). See if that works. Does 1 work and not the other? – Kevin Crowell Mar 21 '10 at 05:17
  • I tried what you said by altering the snippet that I've provided. I first removed all the numbers so that my all_different statements had only letters in them. That gave me the same error. So I got rid of that and tried it again with only numbers this time. I still get the same error. I've tried using only use_module(library(bounds)). and only use_module(library(clpfd)). and both. Nothing seems to be working so far. – inspectorG4dget Mar 21 '10 at 05:26
  • 1
    Well, I don't have Prolog on this computer, but I am going to get it and mess around with all_different to see what I can find out. – Kevin Crowell Mar 21 '10 at 05:27
  • Thank you soooo much... Really. This has been driving me nuts for some time now – inspectorG4dget Mar 21 '10 at 05:29
  • 1
    Do you have this file? C:\Program Files\pl\library\clp\bounds.pl Note: Your prolog might be installed somewhere else. – Kevin Crowell Mar 21 '10 at 05:43
  • I am doing this on the school server and I'm sure that it's maintained properly. BTW, it's running linux – inspectorG4dget Mar 21 '10 at 05:51
  • I would see if you can find that file in the prolog library. – Kevin Crowell Mar 21 '10 at 06:16
  • When I access the PROLOG interpreter from /usr/local/packages/pl-5.6.58/lib/pl-5.6.58 by typing "pl" (without quotes) into the UNIX shell, I get a PROLOG prompt. Here, if I try to import clpfd, it compiles about 5 different things and all_different works. However, I am not able to add this to my path nor am I able to bring my code file into this directory (issues with permissions). Any thoughts? – inspectorG4dget Mar 21 '10 at 06:37
  • 1
    I honestly have no idea. I would contact the school network admin and enlist his help. – Kevin Crowell Mar 21 '10 at 06:39
  • @Kevin Crowell. Thanks for your help. I emailed my sysadmin telling her exactly what I just told you (no response yet). But this IS an admin problem, as you said. Thanks again – inspectorG4dget Mar 21 '10 at 16:23
  • @Kevin Crowell: I have one more question about the code that i have posted - since I have A<7, A>0, etc, does PROLOG check for all possible values of A-X that are different and return one configuration of A-x that are all different and between 1-6? – inspectorG4dget Mar 21 '10 at 17:24
  • 1
    If there are multiple solutions, Prolog will grab them. – Kevin Crowell Mar 22 '10 at 15:50
2

I guess that >/2 and </2 may know nothing about attributes that all_different/1 applied to the vars in list. In SWI-Prolog that predicate is provided by library(clpfd) and among other predicates in that library there is #</2 .

        ?- all_different([X,Y]), 0 #< X, X #< 3, 1 #< Y, Y #< 4, indomain(X), indomain(Y). 
        X = 1,
        Y = 2 ;
        X = 1,
        Y = 3 ;
        X = 2,
        Y = 3.

P.S. indomain/1 from the same library yields all feasible values within applied constraints.

ony
  • 12,457
  • 1
  • 33
  • 41
1

The "ic" constraint library of Eclipse Prolog provides the predicate alldifferent/1, which takes a list of variables as its argument, e.g., alldifferent([X,Y]) and computes what you are looking for.

Matthias F
  • 41
  • 3