Docsity

Prolog: A Logic-Based Programming Language, Study notes of Computer Science

Partial preview of the text

Download Prolog: A Logic-Based Programming Language and more Study notes Computer Science in PDF only on Docsity!

CS 603: Programming

Languages

Lecture 25 Spring 2004 Department of Computer Science University of Alabama Joel Jones

Prolog

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

Classical First-Order

Logic

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:

  • Define each of these symbols

Examples of First Order Logic

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

Horn Rules (cont.)

Note that atomic formulas are also Horn rules, often called facts.

A set of Horn rules is called a Logic Program.

Logical Inference with

Horn Rules

Logic programming is based on a simple idea: From rules and facts, derive more facts.

Example 1. Given the facts and rules:

  1. A
  2. B
  3. C
  4. E ← A B
  5. F ← C E
  6. G ← E F

From 1, derive E; from 2, derive F; from 3, derive G.

Recursive Inference

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)))

Prolog Notation

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

Syntax

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 Introduction

/* list of facts in prolog, stored in an

ascii file, ‘family.pl’*/

mother(mary, ann).

mother(mary, joe).

mother(sue, mary ).

father(mike, ann).

father(mike, joe).

grandparent(sue, ann).

/* 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

Rules

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:

  • Define grandparent grandparent( X , Z ) :– parent( X ,Y ),parent(Y, Z ).