Logic Programming

What is Logic Programming?

  • In contrast to all other types of programming encountered this semester, Logic Programming is nonprocedural
    • The focus is on what to compute as opposed to how to compute it
  • A common example of this is sorting
    • Instead of coding how to do the sorting, describe what a sorted list looks like

$ sort(old\_list,new\_list) \subset permute(old\_list,new\_list) \cap sorted(new\_list) $

$ sorted(list) \subset \forall j \textrm{ such that } 1 \leq j \le n, list(j) \leq list(j + 1) $

Prolog

  • Prolog is by far the most widely used logic programming language
  • Was invented in the 70's
  • Is based of a system of facts and rules
  • Originally intended for theroem proving and natural language processing

Prolog on GL

  • Like LISP and Scheme, there are many dialects of prolog
    • The one on GL is invoked using the command sicstus
  • The prolog interpreter has two modes, entering facts and querying
    • The default mode is query
    • To enter the fact mode, enter [user]. and hit enter. Type ctrl+D to return to query mode
  • To exit the interpreter, type ctrl+C followed by e

The Syntax of Prolog

  • Prolog has a very limited set of valid statements
  • Atom
    • Like an atom in LISP or Scheme, it is a symbolic value
    • Can contain underscores, letters, or numbers
    • Must start with a lowercase letter or be between single quotes
  • Variable
    • Can also contain underscores, letters, or numbers
    • Must start with an uppercase letter

The Syntax of Prolog

  • The third type of data in prolog is known as a structure
  • The basic form is:
      functor(parameter list)
  • The functor is any atom and is used to identify the structure
  • The parameter list are atoms or variables separated by commas
  • The functor can be though of as a relationship between the parameters in First-Order Logic

Fact Statements

  • Prolog programs are made up of facts and rules to be consulted
  • The facts in a program are a list of structures
  • Every statement ends in a period

Fact Statement Examples

  • The following facts describe the relationships between family members
    female(shelley).
    male(bill).
    female(mary).
    male(jake).
    father(bill, jake).
    father(bill, shelley).
    mother(mary, jake).
    mother(mary, shelley).

Fact Statement Practice

  • Write statements for the following facts:
    • English,German,French,Spanish,Welsh,Russian,Persian,Hindu, and Gujarati are all languages
    • German and English are Germanic Languages
    • Spanish and French are Romance Languages
    • Welsh is a Celtic Language
    • Russian is a Slavic Language
    • Persian is a Iranian Language
    • Hindu and Gujarati are Indic Languges

Logic in Rule Statements

  • When are are multiple structures in the antecedent, thare are connected by and clauses
  • The statement below is read as "if antecedent1 and atencedent2 then consequence"
    consequence :- antecedent1, antecedent2.
    
  • There is no explicit way to say or in prolog
    • Instead two statements are used
      consequence :- antecedentA.
      consequence :- antecedentB.
      

Using Variables in Rules

  • It is perfectly legal, but very boring to have a rule with no variables
    ancestor(mary,shelley) :- mother(mary,shelley).
    
  • When using variables in prolog, it is like adding the universal quantifier $\forall$ to the statement
    parent(X, Y) :- mother(X, Y).
    parent(X, Y) :- father(X, Y).
    grandparent(X, Z) :- parent(X, Y), parent(Y, Z).

Prolog Rule Practice

  • Write rules for the following relationships
    • Iranian Language are a type of Indo-Iranian Language
    • Indic Languages are a type of Indo-Iranian Language
    • Indo-Iranian Languages are a type of Indo-European Language
    • Germanic Languages are a type of Indo-European Language
    • Romance Languages are a type of Indo-European Language
    • Celtic Languages are a type of Indo-European Language
    • Slavic Languages are a type of Indo-European Language

Querying Prolog

  • Once we have defined the facts and rules in our program, we can use prolog to prove hypothesis
  • At the most basic level, a query can be something like male(bill). to which the system will return yes or no
  • More advanced queries are given with variables such as father(X,jake). which will return an answer if one exists.

A more efficent way to query

  • Entering into user mode and having to enter all the facts is quite cumbersome
  • Most prolog implementations have a way to store the database of facts and rules in a file
    • In scistus this is done using the consult call
      consult('file.pl')
  • After this, all facts are loaded and the interpreter can be used to query

How Prolog Solves Queries

  • The process of solving a query is known as unification because a variable and a symbol are unified during the process
    • It is similiar to assignment
    • It is better thought of as pattern matching
  • There are two general algorithms
    • Forward Chaining starts with facts and attempts to derive the query through several steps
    • Backward Chaining starts with the query and matches rules and facts until variables are unified

How Prolog Solves Queries

  • Sometimes, two rules or facts will work at a given stage
    • Here prolog must pick which statement to try first using either breath or depth first searching
  • When the interpreter makes a wrong guess it needs to be able to backtrack to the last decision point and try another rule or fact
  • The process of unification can be viewd by enabling tracing
    • In sicstus this is done by typing ctrl+C followed by t

Cons of Prolog

  • Prolog can be very inefficent
    • If we had implemented the storting algorithm discussed at the beginning of lecture, prolog will just guess list permutations until it finds one that is sorted
  • If a fact in not in the database, prolog assumes it is false
  • Programming anything other than logic is cumbersome
    • Prolog is often more useful when used with another language