Question

In GNU prolog, with constraints: I have a list, which, for N = 5 (for instance), it is like this: [3*(1-_#0(0..1)),2*(1-_#18(0..1)),1*(1-_#36(0..1)),4*(1-_#54(0..1)),2*(1-_#72(0..1))]

I am working on a finite domain constraint environment and I tried this max_list(MaxFilter, M0),

and I got this error (since the variable does not have a value at this moment) uncaught exception: error(type_error(evaluable,_#4195373(0..1)),(=<)/2)

In Eclipse, I would use eval and in SWI, I used this predicate

lmax([H|T], X):-
        foldl(max_, T, H, Exp),
        X #= Exp.
max_(E, X, max(E, X)).

but I did not find any equiivalent of foldl in GNU.

What should I do? Any ideas? I am stuck and I have been searching the whole internet for days...

What I actually want is to find the max of this list, when the labelling of the List of solution occurs (at the very end of my main predicate). When this happens, the variables of the list I am trying to find the max, will have a value (they are the same variables, for the example above, I have Sol=[_#0(0..1), _#18(0..1), ...]). But the code never reaches this point, since it throws an error when I try to find the max.

Was it helpful?

Solution

You could get the definition of foldl/4 from library. Try - on SWI console - to enter

?- edit(foldl/4).

then you can study/copy the definition, that I paste here

foldl(Goal, List, V0, V) :-
    foldl_(List, Goal, V0, V).

foldl_([], _, V, V).
foldl_([H|T], Goal, V0, V) :-
    call(Goal, H, V0, V1),
    foldl_(T, Goal, V1, V).

I then tried in gprolog, and it's working:

| ?- consult('prolog/foldl').  
compiling /home/carlo/prolog/foldl.pl for byte code...
/home/carlo/prolog/foldl.pl compiled, 12 lines read - 1464 bytes written, 22 ms

yes
| ?- lmax([1,4,2],X).

X = 4

yes

Of course a less general version is easier to get working

lmax([A,B|T], X):-
        X #= max(A, Exp),
        lmax([B|T], Exp).
lmax([X], X).

works as well, without foldl.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top