Logic Programming

A Courseware

Prolog program is simply based on predicate logic known as Horn clause. In prolog, we compose the program using facts and rules and we pose a query on query prompt about the facts and rules we inserted.

  • Horn Clause : Horn clause consists of head (left hand side) and body (right hand side). Head can have 0 or 1 predicate and body can have list of predicates. That means LHS has only single literal and RHS can have more than one literals.
          Written as :  h :- b     ( where b is p1, p2, p3, .., pn and ' :- ' operator is read as 'if' )
             Read as :  h is true if b is true. In other words h is true if all the predicates on right side are true.                          
               Syntax :  Start the statement (relationship or Object) with lowercase letters and end with '.' period (full stop) - for all facts, rules and queries.

          Examples :
                Headless Horn Clauses :
                    son(john).                                // Read as : john is son
                    son(john,mary).                      // Read as : john is son of mary
                Headed Horn Clauses :
                    boy(john) :- son(john,mary).   // Read as : john is a boy if he is son of mary                      

More About Horn Clause

  • Facts : Facts are those statements that state the objects or describe the relationship between objects. For an instance when we say john likes piano, we are showing the 'like' relationship between two objects 'john and piano' and in prolog this fact can be written as likes(john,piano). Facts are also known as "unconditional horn clauses" and they are always true. 

          Examples: logic_programming.                // Read as : logic programming   
                            music_student(john).               // Read as : john is a music student                     
                            likes('John', car(bmw))            // Read as : john likes bmw car
                            gives(john, chocolate, jane).    // Read as : john gives chocolate to jane

  • Rules :  Rules are the conditional statements about objects and their relationships.  

          Examples: If we want to say that john and jane are friends if john likes jane and jane likes john. Then in prolog this friends rule can be written as,

                           friends(john,jane) :- likes(john,jane), likes(jane,john).

          Examples: Rules with variables. 

                           likes(john, X) :- car(X).       // Read as : john likes X if X is a car. 
                        friends(X, Y) :- likes(X, Y), likes(Y, X).    // Read as : X and Y are friends if X likes Y and Y likes X.  OR  Two people are friends if they like each other.

  • Queries/Goals : Prolog queries are the questions asked by the user to prolog interpreter about facts and rules stored in it's database. For the particular program, upon asking the query, Prolog interpreter trace through the facts and rules it has been given in the top-down manner and try to find a match for a given query. If it finds the matches it will report yes/true and if it doesn't then it will report no/false. Query can be made up of multiple subgoals - for example ?- student(john, music), student(jane,music) - so here in this sentence we have query with two subgoals and subgoals must be separated by commas. But all subgoals must be satisfied otherwise whole query would result in failure. One more thing is - if you use any variables to define rules in prolog program, it's not necessary to use the same variable name when you ask queries.

          Example : For below Prolog Program, we will ask two queries about the facts we have provided.
          Program : 
                          likes(alice,john).         // Read as : alice likes john
                          likes(john,mary).        // Read as : john likes mary

          Queries : ?- likes(alice,john).     // Read as :- Does alice like john?
                          true.
                         ?- likes(alice,mary).    // Read as :- Does alice like mary?
                          false.                          

                          In above example, text in red color is what user types and queries are terminated by the full stop at the end.
                          So, when we enter ?- likes(alice,john). interpreter will answer yes or true since it can find the match for the given query.
                          But when we ask ?- likes(alice,mary). then it would say no or false.

More About Facts, Rules, Queries

  • Variables : In prolog variables always start with an uppercase letter or an underscore and it can be written in head or body or the goals.  

          Examples : Who,  What,  X,  Y,  ABC,  Abc,  A12
                            _who,  _x,  _variable,  _abc,  _Abc,  _12 

But Prolog also has a facility of using an anonymous variable i.e. only _ (an underscore) to which no values are bound. It is just a don't-care variable. Using this anonymous variable we are just defining it's position. For an instance, when we write this fact in Prolog - likes(john,_). we are declaring that john likes something, but that something is not actually of any use for the rest of the program. Another example: when we write this query in Prolog ?- gives(john, chocolate, _). we are only interested in john giving the chocolate, but who - we indicate that as an anonymous variable. But keep in mind this difference - 'John' is an atom and John is a variable. Anything you write between single quotes, becomes an atom whether the text starts with uppercase, underscore or lowercase. Below is the example of prolog variable representation -  
      Prolog Program Fact:
      gives(john, chocolate, jane).


Prolog Query & Answer :
?- gives(john, chocolate,Who).
Who = jane.


Prolog Query & Answer : 
?- gives(john, chocolate, _).
true.
  • Comments in Prolog : Single-line or Multi-line comments begin with /* and end with */  , but cannot be nested.  % is also used only for single line comment, but it should be at the starting of the line.

More About Prolog Programming Basics

       Quiz