









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Lecture 25 Spring 2004 Department of Computer Science University of Alabama Joel Jones
PROgramming in LOGic
Algorithm = Logic + Control
Logic = relation R(I,O) between input I and output O
Control = method of searching for O that satisfies R(I,O), given input I
E.g. Find X and Y such that
3X+2Y=
X-Y=
E.g. find array B such that
elements in B are the same as those in A
elements of B are in non-descending order
simplest form of logical statements is an atomic formula. e.g.
man(tom)
woman(mary)
married(tom,mary)
More complex formulas can be built up using logical connectives: Ù,^ Ú,^ ¬,^ fi,^ "X,^ $X Pair Up:
smart(tom) dumb(tom)
smart(tom) tall(tom)
¬ dumb(tom)
X married(tom,X)
X loves(tom,X)
X [married(tom,X) female(X) human(X)]
rich(tom) ¬ smart(tom)
X mother(john,X)
X Y [mother(john,X) mother(john,Y) Y=X] Note: A fi B ” B Ú ¬A
Note that atomic formulas are also Horn rules, often called facts.
A set of Horn rules is called a Logic Program.
Logic programming is based on a simple idea: From rules and facts, derive more facts.
Example 1. Given the facts and rules:
From 1, derive E; from 2, derive F; from 3, derive G.
Example, given (1) X[mortal(son_of(X)) mortal(X)] (2) mortal(plato) derive: mortal(son_of(plato)) (using X=plato) mortal(son_of(son_of(plato))) (using X=son_of(plato)) mortal(son_of(son_of(son_of(plato)))) (using X=son_of(son_of(plato)))
A rule: X [p(X) (q(X) r(X))] is written as p(X) ← q(X), r(X). Prolog conventions: variables begin with upper case (A, B, X,Y, Big, Small, ACE) constants begin with lower case (a, b, x, y, plato, aristotle) Query = list of facts with variables, e.g. mortal(X) sorted([5,3,4,9,2], X) sonOf(martha,S), sonOf(george,S) Prolog program = facts+rules+query
Constructors like student and “.” are called functors in Prolog
Integers
Atoms: user defined, supplied
name starts with lower case: john, student
Variables
begin with upper case: Who, X
‘_’ can be used in place of variable name
Structures
student(ali, freshman, 194).
Lists
[x, y, Z ]
[ Head | Tail ]
syntactic sugar for. ( Head, Tail )
[ ]
/* Prolog the order of the facts and rules is the order it is searched in / / Variation from pure logic model / 2 ?- father( X,Y ). X = mike / italics represents computer output / Y = ann ; / I type ‘;’ to continue searching the data base */ X = mike Y = joe ; no 3 ?- father( X, joe). X = mike ; no
parent( X ,Y ) :– mother( X ,Y ). /* If mother( X ,Y ) then parent( X ,Y ) */
parent( X ,Y ) :– father( X ,Y ).
/* Note: grandparent(sue, ann). redundant */
/* if parent( X ,Y ) and parent(Y,Z ) then grandparent( X ,Z ). */ Pair Up: