Reputation: 91
Given a clause
functionClause(Function):-...
and user input
?functionClause(or(and(r,q), not(p)))
,is it possible to write other clauses inside the program to obtain access to p,r, and q in order use it, e.g to place truth values inside p,r and q and obtain results?
Upvotes: 0
Views: 108
Reputation: 5509
I think you can do something like this:
p
, replace it with a corresponding variable, say A
, and collect the pair p = A
into a list (only if variable A
is new).% lifted(+Formula, -Lifted, -Variables)
lifted(Formula, Lifted, Variables) :-
lifted(Formula, Lifted, [], Variables0),
reverse(Variables0, Variables).
lifted(F, V, Vs0, Vs) :-
atom(F),
!, % transform atom into a corresponding variable
( memberchk((F=V), Vs0)
-> Vs = Vs0 % use existing variable
; Vs = [(F=V)|Vs0] % use new variable
).
lifted(not(F), not(L), Vs0, Vs) :-
lifted(F, L, Vs0, Vs).
lifted(and(F1, F2), and(L1, L2), Vs0, Vs) :-
lifted(F1, L1, Vs0, Vs1),
lifted(F2, L2, Vs1, Vs).
lifted(or(F1, F2), or(L1, L2), Vs0, Vs) :-
lifted(F1, L1, Vs0, Vs1),
lifted(F2, L2, Vs1, Vs).
bool(_ = false).
bool(_ = true ).
interpretations(Formula) :-
lifted(Formula, Lifted, Variables),
forall( maplist(bool, Variables),
format('~w\n', [Lifted]) ).
Some examples:
?- lifted( and(p, or(not(p),q)), Lifted, Variables).
Lifted = and(_A, or(not(_A), _B)),
Variables = [p=_A, q=_B].
?- interpretations( and(p, or(not(p),q)) ).
and(false,or(not(false),false))
and(false,or(not(false),true))
and(true,or(not(true),false))
and(true,or(not(true),true))
true.
Upvotes: 1