Transcript Slide 1

CS 337 Programming Languages

Logic Programming I (Logic, Intro to Prolog)

Outline

   First-order predicate calculus Horn clauses Introduction to Prolog    Terms How to “run” a Prolog program Rules week 10 CS 337 Concepts of Programming Languages 2

Logic Programming

   Uses a set of logical assertions (i.e. statements that are either true or false), as a program (the facts).

Execution is initiated by a

query

or

goal

, which the system attempts to prove true or false, based on the existing set of assertions.

For this reason, logic programming systems are sometimes called

deductive databases

.

week 10 CS 337 Concepts of Programming Languages 3

Examples

  Computing ancestors: A parent is an ancestor.

If A is an ancestor of B, and B is an ancestor of C, then A is an ancestor of C. A mother is a parent.

A father is a parent.

mohamed is the father of fatma.

fatma is the mother of hacen.

ali is the father of hacen.

Computing the factorial function: The factorial of 0 is 1.

If m is the factorial of n - 1, then n * m is the factorial of n.

week 10 CS 337 Concepts of Programming Languages 4

(First order) Predicate Calculus

Starts with a set of axioms ( true assertions), stated using the following elements:    Constants. Usually numbers or names. In the examples, mohamed and 0 are constants.

Predicates. Names for functions that are true or false, like Boolean functions in a program. Predicates can take a number of arguments. In the examples, ancestor and factorial are predicates.

Functions. First-order predicate calculus distinguishes between functions that are true or false—these are the predicates—and all other functions, which represent non Boolean values. * is a function in the examples. week 10 CS 337 Concepts of Programming Languages 5

Predicate Calculus (continued)

   Variables that stand for as yet unspecified quantities. In the examples, A and m are variables.

Connectives. Operations and, or, and not; implication "  " and equivalence "  " .

Quantifiers. These are operations that introduce variables: "for all" - the universal quantifier, and "there exists" - the existential quantifier.Punctuation symbols: left and right parentheses, the comma, and the period.

Note: Arguments to predicates and functions can only be terms: combinations of variables, constants, and functions.

week 10 CS 337 Concepts of Programming Languages 6

Examples written in Pred. Calc.:

  Ancestors: For all X and Y, parent(X,Y)  ancestor(X,Y). For all A, B, and C, ancestor(A,B) and ancestor(B,C)  ancestor(A,C). For all X and Y, mother(X,Y)  For all X and Y, father(X,Y)  Father(Mohamed,Fatma).

Mother(Fatma,Hacen).

Father(Ali,Hacen).

parent(X,Y).

parent(X,Y).

Factorials: Factorial(0,1).

For all n and m, factorial(n-1,m)  factorial(n,n*m).

week 10 CS 337 Concepts of Programming Languages 7

Horn Clauses

Drop the quantifiers (i.e., assume them implicitly). Distinguish variables from constants, predicates, and functions by upper/lower case:  parent(X,Y)  ancestor(X,Y). ancestor(A,B) and ancestor(B,C)  mother(X,Y)  father(X,Y)  parent(X,Y).

parent(X,Y).

father(mohamed,fatma).

mother(fatma,hacen).

father(ali,hacen).

ancestor(A,C).  factorial(0,1).

factorial(N-1,M)  factorial(N,N*M).

week 10 CS 337 Concepts of Programming Languages 8

Resolution

  Resolution is an inference rule for Horn clauses If we have two Horn clauses, we can combine left-hand and right-hand sides of both clauses and then cancel those statements that match on both sides.

week 10 CS 337 Concepts of Programming Languages 9

Examples of resolution

  Given legs(x,2) <- mammal(x), arm(x,2).

legs(x,4) <- mammal(x), arms(x,0).

mammal(horse).

arms(horse,0).

Query: <-legs(horse, 4).

week 10 CS 337 Concepts of Programming Languages 10

Unification

   Unification is the process of matching to make statement identical.

Variables that are set equal to patterns are said to be instantiated.

Example: In the last example, legs(x,4) is unified with legs(horse,4), so x is instantiated with horse.

week 10 CS 337 Concepts of Programming Languages 11

Outline

   First-order predicate calculus Horn clauses Introduction to Prolog     Terms How to “run” a Prolog program Rules What Prolog is good for week 10 CS 337 Concepts of Programming Languages 12

Terms

 Everything in Prolog is built from

terms .

 Three kinds of terms:    Constants: integers, real numbers, atoms Variables Compound terms week 10 CS 337 Concepts of Programming Languages 13

Constants

   Integer constants:

123

Real constants:

1.23

Atoms:  A lowercase letter followed by any number of additional letters, digits or underscores:

fred

  A sequence of non-alphanumeric characters:

*

,

.

,

=

,

@#$

Plus a few special atoms:

[]

week 10 CS 337 Concepts of Programming Languages 14

Atoms Are Not Variables

 An atom can look like an ML or Java variable: 

i

,

size

,

length

  But an atom is not a variable; it is not bound to anything, never equal to anything else Think of atoms as being more like string constants:

"i"

,

"size"

,

"length"

week 10 CS 337 Concepts of Programming Languages 15

Variables

 Any name beginning with an uppercase letter or an underscore, followed by any number of additional letters, digits or underscores:

X

,

Child

,

Fred

,

_

,

_123

  Most of the variables you write will start with an uppercase letter Those starting with an underscore get special treatment week 10 CS 337 Concepts of Programming Languages 16

Compound Terms

  An atom followed by a parenthesized, comma-separated list of one or more terms:

x(y,z)

,

+(1,2)

,

.(1,[])

,

parent(mohamed,fatma)

,

x(Y,x(Y,Z))

A compound term can look like an ML function call:

f(x,y)

week 10 CS 337 Concepts of Programming Languages 17

Terms

<

term

>  <

constant

> | <

variable

> | <

compound-term

> <

constant

>  <

integer

> | <

real number

> | <

atom

> <

compound-term

>  <

termlist

>  <

term

<

atom

> | < >

term

(

> <

,

termlist

< >

termlist

)

>  All Prolog programs and data are built from such terms week 10 CS 337 Concepts of Programming Languages 18

The Prolog Database

    A Prolog language system maintains a collection of facts and rules of inference It is like an internal database A Prolog program is just a set of data for this database The simplest kind of thing in the database is a fact: a term followed by a period week 10 CS 337 Concepts of Programming Languages 19

Example

parent(ali,othman).

parent(fatma,hacen).

parent(mohamed,fatma).

parent(mohamed,ibrahim).

parent(othman,mohamed).

parent(hacen,ahmad).

   A Prolog program of six facts Defining a predicate

parent

of arity 2 We would naturally interpret these as facts about families: ali is the parent of othman and so on week 10 CS 337 Concepts of Programming Languages 20

SWI-Prolog

Welcome to SWI-Prolog (Version 3.4.2) Copyright (c) 1990-2000 University of Amsterdam.

Copy policy: GPL-2 (see www.gnu.org) For help, use ?- help(Topic). or ?- apropos(Word).

?   Prompting for a query with

?-

Normally interactive: get query, print result, repeat week 10 CS 337 Concepts of Programming Languages 21

The

consult

Predicate

?-

consult(relations).

% relations compiled 0.00 sec, 0 bytes Yes ?   Predefined predicate to read a program from a file into the database File

relations

(or

relations.pl

) contains our

parent

facts week 10 CS 337 Concepts of Programming Languages 22

Simple Queries

?-

parent(mohamed,ibrahim).

Yes ?-

parent(ahmad,mohamed).

   No ? A query asks the language system to prove something The answer will be

Yes

or

No

(Some queries, like

consult

, are executed only for their side-effects) week 10 CS 337 Concepts of Programming Languages 23

Final Period

  Queries can take multiple lines If you forget the final period, Prolog prompts for more input with

|

week 10 CS 337 Concepts of Programming Languages 24

Queries With Variables

?-

parent(P,hacen).

P = ali Yes ?-

parent(P,ali).

  No Any term can appear as a query, including a term with variables The Prolog system shows the bindings necessary to prove the query week 10 CS 337 Concepts of Programming Languages 25

Flexibility

 Normally, variables can appear in any or all positions in a query:  

parent(Parent,mohamed) parent(fatma,Child)

 

parent(Parent,Child) parent(Person,Person)

week 10 CS 337 Concepts of Programming Languages 26

Conjunctions

?-

parent(mohamed,X), parent(X,hacen).

X = fatma Yes   A conjunctive query has a list of query terms separated by commas The Prolog system tries to prove them all (using a single set of bindings) week 10 CS 337 Concepts of Programming Languages 27

Multiple Solutions

?-

parent(mohamed,Child).

Child = ibrahim

;

Child = fatma

;

  No There might be more than one way to prove the query By typing

;

rather than Enter, you ask the Prolog system to find more week 10 CS 337 Concepts of Programming Languages 28

?-

parent(Parent,hacen), parent(Grandparent,Parent).

Parent = fatma Grandparent = mohamed ; No ?-

parent(mohamed,Child),

|

parent(Child,Grandchild),

|

parent(Grandchild,GreatGrandchild).

Child = fatma Grandchild = hacen GreatGrandchild = ahmad Yes week 10 CS 337 Concepts of Programming Languages 29

A Rule

head

greatgrandparent(GGP,GGC) : parent(GGP,GP), parent(GP,P), parent(P,GGC).

conditions

  A rule says how to prove something: to prove the head, prove the conditions To prove

greatgrandparent(GGP,GGC)

, find some

GP

and

P parent(GGP,GP)

for which you can prove , then

parent(GP,P)

finally

parent(P,GGC)

and then week 10 CS 337 Concepts of Programming Languages 30

A Program With The Rule

parent(ali,othman).

parent(fatma,hacen).

parent(mohamed,fatma).

parent(mohamed,ibrahim).

parent(othman,mohamed).

parent(hacen,ahmad).

greatgrandparent(GGP,GGC) : parent(GGP,GP), parent(GP,P), parent(P,GGC).

  A program consists of a list of clauses A clause is either a fact or a rule, and ends with a period week 10 CS 337 Concepts of Programming Languages 31

Example

?-

greatgrandparent(mohamed,GreatGrandchild).

GreatGrandchild = ahmed Yes   This shows the initial query and final result Internally, there are intermediate goals:  The first goal is the initial query   The next is what remains to be proved after transforming the first goal using one of the clauses (in this case, the greatgrandparent rule) And so on, until nothing remains to be proved week 10 CS 337 Concepts of Programming Languages 32

Rules Using Other Rules

grandparent(GP,GC) : parent(GP,P), parent(P,GC).

greatgrandparent(GGP,GGC) : grandparent(GGP,P), parent(P,GGC).

  Same relation, defined indirectly Note that both clauses use a variable

P

 The scope of the definition of a variable is the clause that contains it week 10 CS 337 Concepts of Programming Languages 33

Core Syntax Of Prolog

 You have seen the complete core syntax:  <

clause

>  <

fact

>  <

fact

> | < <

term

>

.

rule

> <

rule

>  <

termlist

>  <

term

>

:-

<

term

<

termlist

> | <

term

> >

, .

<

termlist

> There is not much more syntax for Prolog than this: it is a very simple language week 10 CS 337 Concepts of Programming Languages 34

Arithmetic Operators

 Predicates associativity ?-

+

,

-

,

*

and

X = +(1,*(2,3)).

/

are operators too, with the usual precedence and week 10 X = 1+2*3 Yes ?-

X = 1+2*3.

X = 1+2*3 Yes Prolog lets you use operator notation, and prints it out that way, but the underlying term is still

+(1,*(2,3))

CS 337 Concepts of Programming Languages 35

Not Evaluated

?-

+(X,Y) = 1+2*3.

X = 1 Y = 2*3 Yes ?-

7 = 1+2*3.

No    The term is still

+(1,*(2,3))

It is not evaluated To make Prolog evaluate such terms, use the

is

built-in predicate.

week 10 CS 337 Concepts of Programming Languages 36

Declarative Languages

  Each piece of the program corresponds to a simple mathematical abstraction   Prolog clauses – formulas in first-order logic ML fun definitions – functions Many people use declarative as the opposite of imperative, including both logic languages and functional languages week 10 CS 337 Concepts of Programming Languages 37