0
votes

I've tried to write a predicate that returns all satisfactory combinations for that predicate. For example... val_form(F,L1,L2) as F being the predicate, L1 the list of its variables, and L2 the output.

For example:

val_form(q & (r -> p),[p,q,r],L2). 

Prolog should return:

L2=[0, 1, 0], L2=[1, 1, 0] e L2=[1,1,1]

typing a " ; " between results.

I've already define the operators " -> " , " & " and their cases of " 0 " and "1" as result. and if it cant find any valid solution it returns False.

2
Would a generate and test approach be satisfactory? - hardmath
Can you include the code for ->/2 and &/2? - Daniel Lyons

2 Answers

0
votes

try to use findall predicate for this

0
votes

I once adapted J.R.Fischer tutorial page about Truth table maker, that explain a very similar task to what you require. I used different operators, apart implication, that fits well with (->)/2.

formula(N,_,_,N) :-
    member(N,[0,1]).
formula(X,Vars,A,Val) :-
    atom(X),
    lookup(X,Vars,A,Val).
formula(X + Y,Vars,A,Val) :-
    formula(X,Vars,A,VX),
    formula(Y,Vars,A,VY),
    and(VX,VY,Val).
formula(X * Y,Vars,A,Val) :-
    formula(X,Vars,A,VX),
    formula(Y,Vars,A,VY),
    or(VX,VY,Val).
formula(X -> Y,Vars,A,Val) :-
    formula(X,Vars,A,VX),
    formula(Y,Vars,A,VY),
    imply(VX,VY,Val).
formula(- X,Vars,A,Val) :-
    formula(X,Vars,A,VX),
    not(VX,Val).

lookup(X,[X|_],[V|_],V).
lookup(X,[_|Vars],[_|A],V) :- lookup(X,Vars,A,V).

and(0,0,0).
and(1,1,1).
and(0,1,0).
and(1,0,0).

or(0,0,0).
or(0,1,1).
or(1,0,1).
or(1,1,1).

not(0,1).
not(1,0).

imply(0,0,1).
imply(0,1,1).
imply(1,0,0).
imply(1,1,1).

This code it's a bit more general than required, to obtain the assignment required we must constrain the call:

?- lenght(A,3),formula(q + (r -> p), [p,q,r], A, 1).
Correct to: "length(A,3)"? yes
A = [0, 1, 0] ;
A = [1, 1, 0] ;
A = [1, 1, 1] ;
false.