CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)

Preview:

Citation preview

CS 337 Programming

Languages

Logic Programming I (Logic, Intro to Prolog)

week 10 CS 337 Concepts of Programming Languages

2

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

3

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

4

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

5

(First order) Predicate CalculusStarts 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

6

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

7

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) parent(X,Y).For all X and Y, father(X,Y) parent(X,Y).Father(Mohamed,Fatma).Mother(Fatma,Hacen).Father(Ali,Hacen).

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

8

Horn ClausesDrop 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) ancestor(A,C). mother(X,Y) parent(X,Y).father(X,Y) parent(X,Y).father(mohamed,fatma).mother(fatma,hacen).father(ali,hacen).

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

week 10 CS 337 Concepts of Programming Languages

9

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

10

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

11

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

12

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

13

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

14

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

15

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

16

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

17

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

18

Terms

All Prolog programs and data are built from such terms

<term> <constant> | <variable> | <compound-term><constant> <integer> | <real number> | <atom><compound-term> <atom> ( <termlist> )<termlist> <term> | <term> , <termlist>

week 10 CS 337 Concepts of Programming Languages

19

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

20

Example

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

parent(ali,othman).parent(fatma,hacen).parent(mohamed,fatma).parent(mohamed,ibrahim).parent(othman,mohamed).parent(hacen,ahmad).

week 10 CS 337 Concepts of Programming Languages

21

SWI-Prolog

Prompting for a query with ?- Normally interactive: get query, print

result, repeat

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

?-

week 10 CS 337 Concepts of Programming Languages

22

The consult Predicate

Predefined predicate to read a program from a file into the database

File relations (or relations.pl) contains our parent facts

?- consult(relations).% relations compiled 0.00 sec, 0 bytes

Yes?-

week 10 CS 337 Concepts of Programming Languages

23

Simple Queries

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)

?- parent(mohamed,ibrahim).

Yes?- parent(ahmad,mohamed).

No?-

week 10 CS 337 Concepts of Programming Languages

24

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

25

Queries With Variables

Any term can appear as a query, including a term with variables

The Prolog system shows the bindings necessary to prove the query

?- parent(P,hacen).

P = ali

Yes?- parent(P,ali).

No

week 10 CS 337 Concepts of Programming Languages

26

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

27

Conjunctions

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)

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

X = fatma

Yes

week 10 CS 337 Concepts of Programming Languages

28

Multiple Solutions

There might be more than one way to prove the query

By typing ; rather than Enter, you ask the Prolog system to find more

?- parent(mohamed,Child).

Child = ibrahim ;

Child = fatma ;

No

week 10 CS 337 Concepts of Programming Languages

29

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

Parent = fatmaGrandparent = mohamed ;

No?- parent(mohamed,Child),| parent(Child,Grandchild),| parent(Grandchild,GreatGrandchild).

Child = fatmaGrandchild = hacenGreatGrandchild = ahmad

Yes

week 10 CS 337 Concepts of Programming Languages

30

A Rule

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

To prove greatgrandparent(GGP,GGC), find some GP and P for which you can prove parent(GGP,GP), then parent(GP,P) and then finally parent(P,GGC)

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

conditions

head

week 10 CS 337 Concepts of Programming Languages

31

A Program With The Rule

A program consists of a list of clauses A clause is either a fact or a rule, and ends

with a period

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

week 10 CS 337 Concepts of Programming Languages

32

Example

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

?- greatgrandparent(mohamed,GreatGrandchild).

GreatGrandchild = ahmed

Yes

week 10 CS 337 Concepts of Programming Languages

33

Rules Using Other Rules

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

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

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

week 10 CS 337 Concepts of Programming Languages

34

Core Syntax Of Prolog You have seen the complete core syntax:

There is not much more syntax for Prolog than this: it is a very simple language

<clause> <fact> | <rule><fact> <term> .<rule> <term> :- <termlist> .<termlist> <term> | <term> , <termlist>

week 10 CS 337 Concepts of Programming Languages

35

Arithmetic Operators Predicates +, -, * and / are operators too,

with the usual precedence and associativity

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

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

week 10 CS 337 Concepts of Programming Languages

36

Not Evaluated

The term is still +(1,*(2,3)) It is not evaluated To make Prolog evaluate such terms,

use the is built-in predicate.

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

X = 1Y = 2*3

Yes?- 7 = 1+2*3.

No

week 10 CS 337 Concepts of Programming Languages

37

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

Recommended