Logic Programming

Abstract
This is a notebook that is inspired in the computer language  Prolog and shows a possible way to emulate it with Mathematica.

Needs["logic`", "F:\\Renan\\Fiscom\\Book\\logic.m"]

The program begins with a defined statements wich are used to obtain further conclusions.

\(Knowledge = 
      Statements[
Loves[Jhon, Flowers], 
Loves[Jhon, Mary], 

        Loves[Paul, Mary]		
		]; 

    SetAttributes[Statements, {Flat, Orderless}]\)

Then we start with questions about our bank of statements.

Is there anything that  Jhon loves?

Question[Knowledge, Loves[Jhon, X_], X]

Is There anything that  Mary loves?

Question[Knowledge, Loves[X_, Mary] , X]

Is it True that Jhon Loves the flowers?

Question[Knowledge, Loves[Jhon, Flowers] ]

Example 2

Lets work with other statements.

\(Knowledge = 
      Statements[
		Loves[Mary, \ food], 
		Loves[Mary, wine], 
		
        Loves[Jhon, wine], 
		Loves[Jhon, Mary]
		]; 


    SetAttributes[Statements, {Flat, Orderless}]\)

Is there anything that both Jhon and Mary loves ?

\(Question[Knowledge, 

      Statements[	Loves[Mary, X_]\ , \ Loves[Jhon, X_]\ ]\ , X]\)

Question[Knowledge,  Loves[Jhon, wine] ]

Question[Knowledge,  Loves[Jhon, food] ]

Example 3

This example is about a family.

\(\(Knowledge\  = \ 
      Statements[Male[Albert], 
		Male[Edward], 
		Female[Alice], 
		
        Female[Victory], 
		Fathers[Edward, Victory, \ Albert], 
		
        Fathers[Alice, Victory, Albert]
		]; \)\)

SetAttributes[Statements, {Flat, Orderless}]

Now we want to find who can be  Sister of anyone else.

\(Question[Knowledge, 
	
      Statements[\ Female[X_]\ , \ Fathers[X_, M_, F_], Fathers[Y_, M_, F_]], 
      \ Sister[X, Y]\ ]\)

The last answer not only tell us who can be Sister, but also tells a sister of whom.

Example 4

In this example the knowledge is not only given as a set of statements, but also as a Rule that must be taken into account.

\(Knowledge0\  = \ 
      Statements[
		Thief[Jhon], 
		Likes[Mary, food], 
		
        Likes[Mary, wine]		
				]; 

    SetAttributes[Statements, {Flat, Orderless}]
\)Rule1  =  Likes[X_, wine] -> Likes[Jhon, X];

The knowledge must be updated with AddKnowledge.

Knowledge1  =  AddKnowledge[Knowledge0, Rule1];

Now is time to ask what can be stolen by whom.

\(Question[
	Knowledge1, 
	Statements[Thief[X_], Likes[X_, Y_]]	, 

      	CanSteal[X, Y]
	]\)

Example 5

In this example we want to find wheter or not a King was into the power in a certain date.

\(Knowledge\  = \ 
      Statements[
	RulerKing[A, 844, 878], 
	RulerKing[B, 878, 916], 
	
        RulerKing[C, 916, 950], 
	RulerKing[D, 950, 979]
				]; 

    SetAttributes[Statements, {Flat, Orderless}]\)

The function that answers the question is the following:

RulerKingDate[A_, Z_] := 
      Question[Knowledge,   RulerKing[A, X_, Y_] , Z > X  &&  Z < Y ]

And now we can ask for particular values

RulerKingDate[A, 879]

RulerKingDate[A, 845]

References

Clockssin Mellish, Programming in Prolog , Springer Verlag ,1984