JJ Kam
JJ Kam

Reputation: 91

How to convert user input into a reusable predicate in gnu prolog?

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

Answers (1)

slago
slago

Reputation: 5509

I think you can do something like this:

  • If the formula is an atomic proposition, say p, replace it with a corresponding variable, say A, and collect the pair p = A into a list (only if variable Ais new).
  • Otherwise, if the formula is a compound proposition, recursively process each subproposition (accumulating the used variables).
% 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

Related Questions