Mese: Marzo 2022

Vanilla meta-interpreter prolog

Vanilla meta-interpreter in prolog is structured in this way.

solve(true) :- !.
solve( (A,B) ) :- !, solve(A), solve(B).
solve(A) :- clause(A,B), solve(B).

This is the implementation of the left-most goal selection.
For the right-most we need to apply solve(B) first and then solve(A).

solve(true) :- !.
solve( (A,B) ) :- !, solve(B), solve(A).
solve(A) :- clause(A,B), solve(B).

Prolog is able to prove something, but also we can compute the profundity of the path.
It is important because we can find the optimal path to reach the solution.

solve(true, 0) :- !.
solve( (A,B), S ) :- !, solve(A,SA), solve(B,SB), S is SA + SB.
solve(A,S) :- clause(A,B), solve(B,SB), S is SB.

If we wanted to define meta-interpreter solve(Goal,CF) that is true if Goal can be proved with certainty CF.

Starting from a set of rules:

rule(x, (y,z), 10).
rule(y, true, 100).
rule(z, true, 50).

solve(true, 100) :- !.
solve( (A,B), CF ) :- !, solve(A,CFA), solve(B, CFB), min(CFA,CFB,CF).
solve(A,CF) :- rule(A, B, CFR), solve(B,CFB), CF is ((CFR*CFB)/100).

min(A,B,B) :- A>=B, !.
min(A,B,A).

 

Some prolog exercises

  • Everybody who has a child is happy.
    • We need the predicates happy/1, parent/2, child/1.
      • happy(X) :- parent(X, Y), child(Y).
  • For all X, if X has a child who has a sister then X has two children.
    • We need the predicates child/1, sisterOf/2 (symmetric predicate), hastwochildren/1.
      • hastwochildren(X) :- parent(X,Y), sisterOf(Y,Z).
    • As an alternative:
      • hastwochildren(X) :- parent(X,Y), parent(X,Z), female(Z).
  • Define the relation grandchild using the parent relation. 
    • We need the predicates grandchild/2, parent/2.
      • grandchild(X,Y) :- parent(Z,X), parent(Y,Z).
  • Define the relation aunt(X, Y)
    • We need the predicates: aunt/2, parent/2, sisterOf/2.
      • aunt(X,Y) :- parent(Z,Y), sisterOf(X,Z).
  • Define the  predecessor relation
    • We need the predicates: predecessor/2, parent/2:
      • predecessor(X,Y) :- parent(X,Y).
        predecessor(X,Y) :- parent(Z,Y), predecessor(X,Y).
    • As an alternative:
    • predecessor(X,Y) :- parent(X,Y).
      predecessor(X,Y) :- parent(X,Z), predecessor(Z,Y).

Some remarks:

The predecessor relation is defined by two clauses. This set of clauses is called procedure.