Presentation is loading. Please wait.

Presentation is loading. Please wait.

Artificial Intelligence programming and Prolog Language Tutorial

Similar presentations


Presentation on theme: "Artificial Intelligence programming and Prolog Language Tutorial"— Presentation transcript:

1 Artificial Intelligence programming and Prolog Language Tutorial

2 Contents What is logic programming? Small Examples The Basics
Another Example: Towers of Hanoi Other Examples

3 What is Logic Programming?

4 What is Logic Programming?
“The use of mathematical logic for computer programming.” (Wikipedia) “A declarative, relational style of programming based on first-order logic.” (Dictionary.com)

5 History of Prolog Developed in 1972 by Alain Colmerauer and Philippe Roussel Name comes from “PROgramming in LOGic” A solution to the debate about which kinds of logic to use

6 History of Logic Programming
Came about in 1960s and 1970s due to debates about using declarative or procedural representations in AI Stanford and Edinburgh – declarative MIT - procedural Developed Planner in 1969 (first language in the proceduralistic paradigm).

7 Systems for Logic Programming
ALF CLP ECLiPSe Elf Fish Flang Gödel KLIC LIFE MONA Oz System RELFUN SAMPLE XSB Just to name a few….

8 Systems for Logic Programming, cont.
Prolog is the most common system Prolog has many variations: &-Prolog, And-parallel Prolog. ACE, And-Or-parallel Prolog. Actor Prolog Andorra-I, an Or- and (deterministic) and-parallel Prolog. Aurora, Or-parallel Prolog. cu-Prolog, a constraint logic programming language lambda Prolog LeanTaP, a small theorem prover written in SICStus Prolog. Logtalk, an extension for Object-Oriented Programming in Prolog. Mixtus, an automatic partial evaluator for full Prolog. Muse, Or-parallel Prolog.

9 Prolog Structure

10 Prolog Structure Prolog facts – a database of predicates and associations. Prolog rules – define new predicates by using Prolog facts. Note: Prolog considers capital letters to denote variables, not predicates.

11 Prolog Structure – Queries
A query searches the database for the first fact that satisfies its goal. If a fact is found then it either unifies the variable with a constant or Prolog returns yes. If a fact is not found that meets that condition then Prolog returns no.

12 Prolog Structure – disjunction and conjunction.
Use a semi-colon to request subsequent answers. In other words, a semi-colon signifies disjunction.  A comma signifies conjunction. 

13 How Prolog Works: Example 1
Example of data base: instructor (perkowski, ee271) instructor (perkowski, ee171) instructor (perkowski, ee478) enrolled (alan-cheng, ee475) enrolled (matthew, ee171) enrolled (alan-cheng,ee171) enrolled (alan-cheng,ee271) enrolled (chris-clark,ee271) enrolled (edison-tsai, ee171) enrolled (chris-clark, ee171) This is the database of Prolog facts.

14 How Prolog Works, cont. Prolog rules: teaches (P,S) :- instructor (P,C), enrolled (S,C) This is to say that an instructor only teaches if he teaches a class and students are enrolled in that class. teaches (Professor, Student) :- instructor (Professor, Class), enrolled (Student,Class) teaches (Professor, Student)  instructor (Professor, Class), enrolled (Student,Class) instructor (Professor, Class), enrolled (Student,Class)  teaches (Professor, Student)

15 How Prolog Works, cont. Prolog answers queries based off of the database that has been given. ?enrolled (chris-clark, ee271) yes ?enrolled (X, ee271) alan-cheng chris-clark ?teaches (X, alan-cheng) perkowski ?teaches (X, chris-clark) Perkowski Jeske greenwood

16 Expanding Database in Prolog
Imagine what happens if we expand the database: instructor (perkowski, ee271) instructor (perkowski, ee171) instructor (perkowski, ee478) enrolled (jeske, ee171) enrolled (greenwood, ee171) enrolled (alan-chen,ee171) enrolled (alan-chen,ee271) enrolled (chris-clark,ee271) enrolled (edison-tsai, ee171) enrolled (chris-clark, ee171) instructor (bebis, cs365) instructor (looney, cs311) instructor (yuksel, cs446) instructor (helfand, cs493) instructor (quint, math486) enrolled (ben, cs365) enrolled (bill, cs365) enrolled (bill, cs446) enrolled (brian, cs311) enrolled (brian, cs365) enrolled (brittney, cs311) enrolled (brittney, cs365) enrolled (brittney, cs446) enrolled (cody, cs311) enrolled (cody, cs365) enrolled (danielle, cs365) enrolled (danielle, cs446) enrolled (danielle, cs493) enrolled (david, cs365) enrolled (javier, cs365) enrolled (jeffrey, cs365) enrolled (jessica, cs311) enrolled (jessica, cs446) enrolled (jessica, math486) enrolled (joel, cs365) enrolled (joseph, cs311) enrolled (joseph, cs365) enrolled (joseph, cs446) enrolled (joseph, cs493) enrolled (joseph, math486) enrolled (kellen, cs365) enrolled (matts, cs311) enrolled (matts, cs365) enrolled (mattw, cs311) enrolled (mattw, cs365) enrolled (mattw, cs446) enrolled (miran, cs365) enrolled (ryan, cs365) enrolled (samuel, cs365) enrolled (shane, cs311) enrolled (shane, cs365) enrolled (shane, cs446) enrolled (tiffany, cs311) enrolled (tiffany, cs365) enrolled (tiffany, cs446)

17 How Prolog Works, asking questions to data base
?enrolled (X, cs365) ben bill brian brittney cody danielle david javier jeffrey joel joseph kellen matts mattw miran ryan samuel shane tiffany This list now gives us the entire roster of students in CS 365.

18 How Prolog Works, more complicated querries
Queries can be more complicated to compare more data: classmates (S1, S2) :- enrolled (S1, C), enrolled (S2, C) ?classmates (joseph, danielle) yes ?classmates (joseph, jessica) ?classmates (jessica, danielle) no class c s2 s1 enrolled c s2 s1 enrolled classmates

19 How Prolog Works, more complicated querries
classmates (S1, S2, C) :- enrolled (S1, C), enrolled (S2, C) ?classmates (joseph, danielle, C) cs365 cs446 cs493 no ?classmates (joseph, jessica, C) math ?classmates (jessica, danielle, C)

20 Family Data base

21 parent(X,Y) :- father(X,Y).
parent(X,Y) :- mother(X,Y). grandparent(X,Z) :- parent(X,Y), parent(Y,Z). ancestor(X,Z) :- parent(X,Z). ancestor(X,Y) :- parent(X,Y), ancestor(Y,Z). sibling(X,Y) :- mother(M,X), mother(M,Y), father(F,X), father(F,Y), X \= Y. cousin(X,Y) :- parent(U,X), parent(V,Y), sibling(U,V). father(albert, jeffrey). mother(alice, jeffrey). father(albert, george). mother(alice, george). father(john, mary). mother(sue, mary). father(george, cindy). mother(mary, cindy). father(george, victor). mother(mary, victor).

22 % kinship compiled 0.00 sec, 3,016 bytes Yes
SWI Prolog ?- [kinship]. % kinship compiled 0.00 sec, 3,016 bytes Yes ?- ancestor(X, cindy), sibling(X, jeffrey). X = george  ?- grandparent(albert, victor). ?- cousin(alice, john). No ?- sibling(A,B). A = jeffrey, B = george ;  A = george, B = jeffrey ;  A = cindy, B = victor ;  A = victor, B = cindy ; 

23 Unification

24 Prolog Structure - Unification
A query resolves by unifying all of its elements. A constant unifies with itself and any variable. Scope is limited to the rule in which a variable occurs. When a variable is unified with a constant in a rule, all instances of that variable in that rule are unified to that constant.

25 PROLOG STRUCTURE - UNIFICATION
Process of making one predicate same as another. A query resolves by unifying all of its elements. A constant unifies with itself and any variable. Scope is limited to the rule in which a variable occurs. When a variable is unified with a constant in a rule, all instances of that variable in that rule are unified to that constant.

26 Example of Unification

27 Prolog Structure - Backtracking
In more complex examples, Prolog uses backtracking to find possible solutions. Prolog will attempt to resolve the first fact of its rule, unifying any variables with the first constant that satisfies that fact It then attempts to resolve the rest of that rules facts. If it is unable to do so under those conditions it “backs up” and tries again with the next available unifying constant.

28 Clauses Programs are constructed from A number of clauses: <head> :- <body> Clauses have three forms: hypotheses (facts) conditions (rules) goals Both <head> and <body> are composed of relationships (also called predications or literals) assertions (database) questions

29 Relationships Represent properties of and relations among the individuals A relationship is application of a predicate to one or more terms Terms: atoms (or constants): john, 25, … variables (begin with uppercase letters): X, … compounds Horn clause form: At most one relationship in <head>

30 World of Toys

31 World of Toys - Example 2 Let us consider the following description of a “system”; Ann likes every toy she plays with. A doll is a toy. A train is a toy. Ann plays with trains. John likes everything Ann likes. To express this in Prolog we must: Identify the entities, or actual things, mentioned in the description Identify the types of properties that things can have, as well as the relations that can hold between these things Figure out which properties/relations hold for which entities There is really no unique way of doing this; we must decide the best way to structure our data (based on what we want to do with it).

32 A Small Example World of Toys - Example 2
We will choose the following: Things: Ann, Sue, doll, train Properties: “... is a toy” Relations: “... likes ...”, “... plays with ...” Constructing our knowledge base then consists of writing down: which properties hold for which things which relationships hold for which things

33 A Small Example World of Toys - Example 2 We write:
likes(ann,X) :- toy(X), plays(ann,X). toy(doll). toy(train). plays(ann,train). likes(john,Y) :- likes(ann,Y).

34 A Small Example – What It Means
World of Toys - Example 2 There are three important logical symbols :- if , and ; or X and Y are variables ann, john, doll and train are constants likes, toy and plays are predicate symbols

35 A Small Example – What It Means
World of Toys - Example 2 A variable represents some unspecified element of the system A constant represents a particular, known, member of the system A predicate represents some relation or property in the system. Note that: Variables always start with an upper-case letter or an underscore Predicates and constants always start with a lower-case letter or digit

36 A Small Example – What It Means
World of Toys - Example 2 Each line in a Prolog program is called a clause There are two types of clauses - facts and rules Rules are clauses which contain the “:-” symbol Facts are clauses which don't Each fact consists of just one predicate Each rule consists of a predicate, followed by a “:-” symbol, followed by a list of predicates separated by “,” or “;” Every clause is terminated by a “.” (full-stop). In a rule, the predicate before the “:-” is called the head of the rule The predicates coming after the ``:-'' are called the body

37 A Small Example – What It Means
World of Toys - Example 2 For example: likes(ann,X) :- toy(X), plays(ann,X). <---Head---> < Body >

38 A Small Example – What It Means
World of Toys - Example 2 A Small Example – What It Means We “define a predicate” by writing down a number of clauses which have that predicate at their head The order in which we write these down is important Any predicates mentioned in the body must either: be defined somewhere else in the program, or be one of Prolog's “built-in” predicates. Defining a predicate in Prolog corresponds roughly to defining a procedure Predicates occurring in the body of a clause correspond roughly to procedure calls Note also that: Constants and variables will never appear “on their own” in a clause. They can only appear as the “arguments” to some predicate. Predicates will (almost) never appear as arguments to another predicate

39 World of Toys - Example 2 What It Says: So, after all that, what does our little program say? Having all the relations expressed as a predicate followed by arguments is not particularly intuitive, so with some suitable swapping-around we get: For any X, (ann likes X) if (X is-a-toy) and (ann plays-with X). (doll is-a-toy). (train is-a-toy). (ann plays-with train). For any Y, (john likes Y) if (ann likes Y).

40 World of Toys - Example 2 Running It So how do we run it?
We run it by giving Prolog a query to prove A query has exactly the same format as a clause-body: one or more predicates, separated by “,” or “;”, terminated by a full-stop Thus, we might enter in the following as a query: likes(john,Z). Logically, this can be interpreted as “is there a Z such that john likes Z?” From a relational point of view, we can read it as: “List all those Z's that john likes”

41 World of Toys - Example 2 In general terms we call the query our “goal”, and say that Prolog is being asked to (find ways to) “satisfy” the goal This process is also known as inferencing: Prolog has to infer the solution to the query from the knowledge base Note that solving a query results in either: failure, in which case “no” is printed out, or success, in which case all sets of values for the variables in the goal (which cause it to be satisfied) are printed out

42 World of Toys - Example 2 How It Works
So how does Prolog get an answer? We have to solve likes(john, Y), so we must examine all the clauses which start with the predicate likes. The first one is of no use at this point, since it only tells us what ann likes. The second rule for likes tells us that in order to find something that john likes, we need only to find something which ann likes. So now we have a new goal to solve - likes(ann,Z). To solve this we again examine all the rules for likes. This time the first rule matches (and the second doesn't), and so we are told that in order to find something which ann likes, we must find something which is a toy, and which ann plays with.

43 World of Toys - Example 2 So first of all we try to find a toy.
To do this we examine the clauses with toy at their head. There are two possibilities here: a toy is either a doll or train. We now take these two toys, and test to see which one ann plays with; that is, we generate two new sub-goals to solve: plays(ann,doll) and plays(ann,train). In general, to solve these, we must look at the clauses for plays. There is only one: since it is for train, we conclude with the answer: Z = train.

44 World of Toys - Example 2 Exercises Example: toys.pl
Does Ann like dolls? Who likes trains? What does John like? Who plays with trains?

45 A Small Example - Exercises
Translate the following sentences into Prolog: John eats all kinds of food. Apples are food. Oysters are food. Anything anyone eats is food. Tom eats snakes. Sue eats everything that Tom eats. Save the program in a file called food.pl. Now read them into Prolog, and formulate queries to find out: What John eats What Sue eats If there is anything which both John and Sue eat. Who eats snakes

46 Lists

47 Prolog Lists: concept Lists are a collection of terms inside [ and ]
[ chevy, ford, dodge] loc_list([apple, broccoli, crackers], kitchen). loc_list([desk, computer], office). loc_list([flashlight, envelope], desk). loc_list([stamp, key], envelope). loc_list(['washing machine'], cellar). loc_list([nani], 'washing machine'). loc_list([], hall)

48 Prolog Lists: unification
Unification works on lists just as it works on other data structures. loc_list(X, kitchen). X = [apple, broccoli, crackers] ?- [_,X,_] = [apples, broccoli, crackers]. X = broccoli The patterns won't unify unless both lists have the same number of elements.

49 Prolog Lists: member and append
List functions [H|T] separate list into head and tail member test if X is a member of a list append append two lists to form a third list

50 Prolog Lists: head and tail
Head and Tail of a List Syntax [H|T] Examples ?- [a|[b,c,d]] = [a,b,c,d]. %We check identity yes ?- [a|b,c,d] = [a,b,c,d]. no

51 Prolog Lists: more on head and tail
More Examples ?- [H|T] = [apple, broccoli, refrigerator]. H = apple T = [broccoli, refrigerator] ?- [H|T] = [a, b, c, d, e]. H = a T = [b, c, d, e] ?- [H|T] = [apples, bananas]. H = apples T = [bananas]

52 Prolog Lists: more on head, tail and list structure
More Examples ?- [One, Two | T] = [apple, sprouts, fridge, milk]. One = apple Two = sprouts T = [fridge, milk] ?- [a|[b|[c|[d|[]]]]] = [a,b,c,d]. yes

53 Prolog Lists: member Testing if an element is in a list. Syntax
member(X, L). Example member(apple, [apple, broccoli, crackers]). member(X, CarList). Full Predicate defined as: member(H,[H|T]). member(X,[H|T]) :- member(X,T).

54 Prolog Lists: append Appending two lists to form a third. Syntax
append(L1, L2, L3). Example append( [a,b,c], [d,e,f], X). X = [a,b,c,d,e,f] Full predicate defined as: append([],X,X). append([H|T1],X,[H|T2]) :- append(T1,X,T2).

55 Prolog is much More Than Just Information: lists and trees
Prolog rules can also be used write programs that do more than find the answers to simple database queries. append([], L, L). append([H|T], L, [H|L1]) :- append(T, L, L1). This will append a list to another list recursively. A binary tree can be defined as follows tree(nil). tree(node(_ , Left, Right) :- tree(left), tree(right).

56 Data Structures in Prolog

57 Comments in Prolog Single line comments use the “%” character
Multi-line comments use /* and */

58 PROLOG DATA STRUCTURES
Prolog's single data type is the term. Terms are either atoms, numbers, variables or compound terms. atoms are: x, blue, 'Some atom', and []. Numbers can be floats or integers Variables are denoted by a string consisting of letters, numbers and underscore characters, and beginning with an upper-case letter or underscore. A compound term has a functor and a number of arguments, which are again terms.

59 Simple Input-Output in Prolog
Simple I/O in Prolog Use the write statement write(‘hello’) write(‘Hello’), write(‘World’) Use a Newline write(‘hello’), nl, write(‘World’)

60 Reading and writing in Prolog
Reading a value from stdin Prolog Syntax: read(X) Example read(X), write(X).

61 Arithmetic in Prolog Using Arithmetic
Different to what you may have seen with other languages. Operators < <= == != => > + - * / Arithmetic is done via evaluation then unification

62 Arithmetic Arithmetic Example X is Y compute Y then unify X and Y
N is N - 1

63 The difference between identity relation and unification
X == Y This is the identity relation. In order for this to be true, X and Y must both be identical variables (i.e. have the same name), or both be identical constants, or both be identical operations applied to identical terms X = Y This is unification It is true if X is unifiable with Y

64 Different “assignment” operators in Prolog
X=:=Y This means “compute X, compute Y, and see if they both have the same value” both X and Y must be arithmetic expressions X is Y This means compute Y and then unify X and Y Y must be an arithmetic expression X can either be an arithmetic expression (of the same form), or a variable

65 Comparison of assignments
Arithmetic Exercises X = 2, Y is X+1 X = 2, Y = X+1 X = 2, Y == X+1 X = 2, Y =:= X+1 X = 2, 3 =:= X+1 Check these on the Prolog system.

66 Recursion in Prolog

67 Example: GCD Arithmetic Examples gcd(X,X,X).
gcd(X,Y,Z) :- X<Y, Y1 is Y-X, gcd(X,Y1,Z). gcd(X,Y,Z) :- X>Y, X1 is X-Y, gcd(X1,Y,Z).

68 Example: Factorial fact(0,1).
fact(X,F) :- X>0, X1 is X-1, fact(X1,F1), F is X*F1.

69 Example: Towers of Hanoi
The Problem A group of over-proud monks in a Hanoi monastery were assigned a task to perform: they had to move 100 discs from one peg to another with the help of a third peg. There are only two rules: Only one disc can be moved at a time The discs are all of different sizes, and no disc can be placed on top of a smaller one We want to write a Prolog program to solve this.

70 Towers of Hanoi The Rules!!!!
In order to work out a recursive solution we must find something to "do" the recursion on, that is, something with: a base case an inductive case that can be expressed in terms of something smaller We will choose to proceed by induction on the number of discs that we want to transfer

71 Towers of Hanoi Moving a disc So how should we define move?
The basic activity will be moving a single disc from one peg to another. Suppose we want to define a predicate for this called move; thus: move(A,B) means move the topmost disc from peg A to peg B. So how should we define move? If we were doing the problem in reality then we would want to formulate some instructions to a robot arm (attached to the computer) to move the pegs.

72 Towers of Hanoi Moving a disk (cont.)
For our purposes, we will assume that what we want is a list of instructions for the monks; thus we define: move(A,B) :- nl, write('Move topmost disc from '), write(A), write(' to '), write(B). Every time we call move, the appropriate instruction will be printed out on screen.

73 Towers of Hanoi Base Case
An initial attempt might select 1 as the base case. To transfer one disc from A to B, simply move it: transfer(1,A,B,I) :- move(A,B). In fact there is an even simpler base case - when N=0! If we have no discs to transfer, then the solution is to simply do nothing. That is, transfer(0,A,B,I) is satisfied by default. We write this as a fact: transfer(0,A,B,I).

74 Towers of Hanoi Inductive Case Example: Towers of Hanoi
To do the inductive case, suppose we are trying to transfer N discs from A to B. By induction, we may assume that we have a program that transfers N-1 discs. The way we proceed is: Transfer the top N-1 discs from A to I Transfer the last disc from A to B Transfer the N-1 discs from I to B Example: Towers of Hanoi

75 Other Examples Example: Making Change Example: Who owns what car
Example: Things in my kitchen

76 Control Structures in Prolog
Looping…Repeat until user enters “end” command_loop:- repeat, write('Enter command (end to exit): '), read(X), write(X), nl, % new line X = end.

77 More on Data Structures

78 Primitives and Constructors
Few primitives and No constructors. Data types and data structures are defined implicitly by their properties.

79 Example (datatype) Natural number arithmetic
sum(succ(X), Y, succ(Z)) :- sum(X,Y,Z). sum(0,X,X). dif(X,Y,Z) :- sum(Z,Y,X). :-sum(succ(succ(0)),succ(succ(succ(0))),A). A = succ(succ(succ(succ(succ(0))))) Very inefficient! (Why such a decision?) Use of ‘is’ operator (unidirectional)

80 Principles of logic programming languages
Simplicity Small number of built-in data types and operations Regularity Uniform treatment of all data types as predicates and terms

81 Data Structures from other languages
Compound terms can represent data structures Example: Lists in LISP (car (cons X L)) = X (cdr (cons X L)) = L (cons (car L) (cdr L)) = L, for nonnull L

82 LISP in Prolog Using compound terms: What about null(L)?
car( cons(X,L), X). cdr( cons(X,L), L). list(nil). list(cons(X,L)) :- list(L). null(nil). What about null(L)? How to accomplish (car (cons ‘(a b) ‘(c d)))?

83 Some Syntactic Sugar Using ‘.’ infix functor (in some systems) instead of cons: Clauses? Most Prolog systems allow the abbreviation: [X1, X2, …, Xn] = X1. X2. … .Xn.nil [ ] = nil ‘.’ is right associative!

84 Component Selection in Prolog
Implicitly done by pattern matching (unification). append( [ ], L, L). append( X.P, L, X.Q) :- append(P,L,Q). Compare with LISP append: (defun append (M L) (if (null M) L (cons (car M) (append (cdr M) L)) )) Taking apart in terms of putting together! What X and P are cons’d to create M? What number do I add to 3 to get 5 (instead of 5-3) Efficient!?

85 Complex Structures in Prolog
A tree using lists (in LISP): (times (plus x y) (plus y 1)) Using compound terms directly (as records): times(plus(x, y), plus(y, 1)) Using predicates directly: sum(x, y, t1). sum(y, 1, t2). prod(t1, t2, t3). Which is better?

86 Why Not Predicates? Symbolic differentiation using predicate structured expressions: d(X,W,Z) :- sum(U,V,W), d(X,Y,DU), d(X,V,DV), sum(DU,DV,Z). d(X,W,Z) :- prod(U,V,W), d(X,U,DU), d(X,V,DV), prod(DU,V,A), prod(U,DV,B), sum(A,B,Z). d(X,X,1). d(X,C,0) :- atomic(C), C \= X.

87 Example: Symbolic Differentiation
It is more convenient to describe individuals without giving them names (expressions or compounds as terms). using functors (tags): d(X, plus(U,V), plus(DU,DV)) :- d(X,U,DU), d(X,V,DV). or using infix functors: d(X, U+V, DU+DV) :- d(X,U,DU), d(X,V,DV). instead of d(X,W,Z) :- sum(U,V,W), d(X,U,DU), d(X,V,DV), sum(DU,DV,Z). with less readability and some other things…

88 Why Not Predicates? (cont.)
Waste use of intermediate (temporary) variables Less readability Unexpected answers! sum(x,1,z). :- d(x,z,D). No Why? What did you expect? How to correct it?

89 Closed World Model All that is true is what can be proved on the basis of the facts and rules in the database. Very reasonable in object-oriented apps (modeling a real or imagined world) All existing objects are defined. No object have a given property which cannot be found in db. Not suitable for mathematical problems (Why?) An object is generally take to exist if its existance doesn’t contradict the axioms. Predicates are better for OO-relationships, Compounds for mathematical ones (Why?) We cannot assume existance of 1+0 whenever needed.

90 An Argument! What’s the answer?
equal(X,X). :- equal(f(Y),Y). ? What’s the logical meaning? (occurs check) Any other meaning? Can it be represented in a finite amount of memory? Should we detect it?

91 Control Structures

92 Algorithm = Logic + Control
N. Wirth: Program = data structure + algorithm R. Kowalski: Algorithm = logic + control In conventional programming: Logic of a program is closely related to its control A change in order of statements alters the meaning of program In (pure) logic programming: Logic (logic phase) is determined by logical interrelationships of the clauses not their order. Control (control phase) affects the order in which actions occur in time and only affects the efficiency of programs. Orthogonality Principle

93 Top-Down vs. Bottom-Up Control
Top-down ≈ Recursion: Try to reach the hypotheses from the goal. Bottom-up ≈ Iteration: Try to reach the goal from the hypotheses. Hybrid: Work from both the goals and the hypotheses and try to meet in the middle. Which one is better? fib(0,1). fib(1,1). fib(N,F) :- N=M+1, M=K+1, fib(M,G), fib(K,H), F=G+H, N>1.

94 Procedural Interpretation
We have seen logical and record (data structure) interpretations. Clauses can also be viewed as procedure invocations: <head>: proc. definition <body>: proc. body (a series of proc. calls) Multiple definitions: branches of a conditional (case) fib() example… Procedure calls can be executed in any order or even concurrently! (pure logic) Input/Output params are not distinguished! fib(3,3) ↔ true. fib(3,F) ↔ F=3. fib(N,3) ↔ N=3. fib(N,F) ↔ ?

95 Unify, Fail, Redo… Heavy use of unification, backtracking and recursion. Unification (Prolog pattern matching – from Wikipedia): One-time assignment (binding) uninst. var with atom/term/another uninst. var (aliasing) (occurs check) atom with the same atom compound with compound if top predicates and arities of the terms are identical and if the parameters can be unified simultaneously We can use ‘=‘ operator to explicitly unify two terms Backtracking: Make another choice if a choice (unif./match) failes or want to find other answers. In logic prog. It is the rule rather than the exception. Very expensive! Example: len([ ], 0). len(X.T, L+1) :- len(T,L).

96 Prolog’s Control Regime
Prolog lang. is defined to use depth-first search: Top to bottom (try the clauses in order of entrance) Left to right In pure logic prog., some complete deductive algorithm such as Robinson’s resolution algorithm must be implemented. DFS other than BFS Needs much fewer memory Doesn’t work for an infinitely deep tree (responsibility of programmer) Some programs may fail if clauses and subgoals are not ordered correctly (pp ) Predictable execution of impure predicates (write, nl, read, retract, asserta, assertz, …)

97 SWI Prolog [trace] ?- ancestor(X, cindy), sibling(X,jeffrey).
Event Depth Subgoal ================================== Call: (1) ancestor(X, cindy) Call: (2) parent(X, cindy) Call: (3) father(X, cindy) Exit: (3) father(george, cindy) Exit: (2) parent(george, cindy) Exit: (1) ancestor(george, cindy) Call: (1) sibling(george, jeffrey) Call: (2) mother(M, george) Exit: (2) mother(alice, george) Call: (2) mother(alice, jeffrey) Exit: (2) mother(alice, jeffrey) Call: (2) father(F, george) Exit: (2) father(albert, george) Call: (2) father(albert, jeffrey) Exit: (2) father(albert, jeffrey) Call: (2) george\=jeffrey Exit: (2) george\=jeffrey Exit: (1) sibling(george, jeffrey) X = george Yes

98 SWI Prolog If we move parent(X,Y) :- father(X,Y) before parent(X,Y) :- mother(X,Y), we have: Event Depth Subgoal ================================== Call: (1) ancestor(X, cindy) Call: (2) parent(X, cindy) Call: (3) mother(X, cindy) Exit: (3) mother(mary, cindy) Exit: (2) parent(mary, cindy) Exit: (1) ancestor(mary, cindy) Call: (1) sibling(mary, jeffrey) Call: (2) mother(M, mary) Exit: (2) mother(sue, mary) Call: (2) mother(sue, jeffrey) Fail: (2) mother(sue, jeffrey) Redo: (2) mother(M, mary) Fail: (2) mother(M, mary) Fail: (1) sibling(mary, jeffrey) Redo: (3) mother(X, cindy) Fail: (3) mother(X, cindy) Redo: (2) parent(X, cindy)

99 Cut!  ‘!’: Discard choice points of parent frame and frames created after the parent frame. Always is satisfied. Used to guarantee termination or control execution order. i.e. in the goal :- p(X,a), ! Only produce the 1st answer to X Probably only one X satisfies p and trying to find another one leads to an infinite search! i.e. in the rule color(X,red) :- red(X), !. Don’t try other choices of red (mentioned above) and color if X satisfies red Similar to then part of a if-then-elseif Fisher, J.R., Prolog Tutorial,

100 Red-Green Cuts (!) A ‘green’ cut A ‘red’ cut Only improves efficiency
e.g. to avoid additional unnecessary computation A ‘red’ cut e.g. block what would be other consequences of the program e.g. control execution order (procedural prog.) Fisher, J.R., Prolog Tutorial,

101 Three Examples part(a). part(b). part(c). p(a). red(a). black(b).
See also MacLennan’s example p.476 p(a). p(X) :- s(X), r(X). p(X) :- u(X). r(a). r(b). s(a). s(b). s(c). u(d). :- p(X), ! :- r(X), !, s(Y). :- r(X), s(Y), ! :- r(X), !, s(X). part(a). part(b). part(c). red(a). black(b). color(P,red) :- red(P),!. color(P,black) :- black(P),!. color(P,unknown). :- color(a, C). :- color(c, C). :- color(a, unknown). max(X,Y,Y) :- Y>X, !. max(X,Y,X). :- max(1,2,D). :- max(1,2,1). Fisher, J.R., Prolog Tutorial,

102 Higher-Order Rules Logic programming is limited to first-order logic: can’t bind variables to predicates themselves. e.g. red (f-reduction) is illegal: (p(x,y,z) ↔ z=f(x,y)) red(P,I,[ ],I). red(P,I,X.L,S) :- red(P,I,L,T), P(X,T,S). But is legal if the latter be defined as: red(P,I,X.L,S):- red(P,I,L,T), Q=..[P,X,T,S], call(Q). What’s the difference?

103 Higher-Order Rules (cont.)
In LISP, both code and data are first-order objects, but in Prolog aren’t. Robinson resolution algorithm is refutation complete for first-order predicate logic. Gödel’s incompleteness theorem: No algorithm is refutation complete for higher-order predicate logic. So, Prolog indirectly supports higher-order rules.

104 Negative Facts How to define nonsibling? Logically…
nonsibling(X,Y) :- X = Y. nonsibling(X,Y) :- mother(M1,X), mother(M2,Y), M1 \= M2. nonsibling(X,Y) :- father(F1,X), father(F2,Y), F1 \= F2. But if parents of X or Y are not in database? What is the answer of nonsibling? Can be solved by… nonsibling(X,Y) :- no_parent(X). nonsibling(X,Y) :- no_parent(Y). How to define no_parent?

105 Negative Facts (cont.) Problem: There is no positive fact expressing the absence of parent. Cause: Horn clauses are limited to C :- P1,P2,…,Pn ≡ C holds if P1^P2^…^Pn hold. No conclusion if P1^P2^…^Pn don’t hold! If, not iff

106 Cut-fail Solutions: Stating all negative facts such as no_parent
Tedious Error-prone Negative facts about sth are usually much more than positive facts about it “Cut-fail” combination nonsibling(X,Y) is satisfiable if sibling(X,Y) is not (i.e. sibling(X,Y) is unsatisfiable) nonsibling(X,Y) :- sibling(X,Y), !, fail. nonsibling(X,Y). how to define ‘fail’ ?!

107 negation :- unsatisfiablility
‘not’ predicate not(P) is satisfiable if P is not (i.e. is unsatisfiable). not(P) :- call(P), !, fail. not(P). nonsibling(X,Y) :- not( sibling(X,Y) ). Is ‘not’ predicate the same as ‘logical negation’? (see p.484)

108 5th-Generation Languages and Philosophy of Prolog

109 5th-Generation Languages
Declarative (nonprocedural) Functional Programming Logic Programming Imperative Object Oriented Programming

110 Nonprocedural Programming
Sorting procedurally: Find the min in the remained numbers. Swap it with the first number. Repeat steps 1,2 until no number remains. Sorting nonprocedurally: B is a sorting of A ↔ B is a permutation of A and B is ordered. B is ordered ↔ for each i<j: B[i] ≤ B[j] Which is higher level?

111 Automated Theorem Proving
A.T.P: Developing programs that can construct formal proofs of propositions stated in a symbolic language. Construct the desired result to prove its existence (most A.T.P.’s). In Logic Programming, programs are expressed in the form of propositions and the theorem prover constructs the result(s). J. A. Robinson: A program is a theory (in some logic) and computation is deduction from the theory.

112 Programming In Logic (Prolog)
Developed in Groupe d’Intelligence Artificielle (GIA) of the University of Marseilles (early 70s) to process a natural language (French). Interpreters: Algol-W (72), FORTRAN (73), Pascal (76), Implemented on many platforms (Now) Application in AI since mid-70s Successor to LISP for AI apps Not standardized (but has ISO standard now)

113 Properties of Prolog Logic programs are self-documenting
Pure logic programs separate logic and control Prolog falls short of logic programming Implementation techniques are improving Prolog is a step toward nonprocedural programming

114 Self-documentation Programming in a higher-level, …
Application orientation and… Transparency programs are described in terms of predicates and individuals of the problem domain. Promotes clear, rapid, accurate programming

115 Separation of Logic and Control
Simplifies programming Correctness only deals with logic Optimization in control cannot affect correctness Obeys Orthogonality Principle

116 Prolog vs. Logic Programming
Definite control strategy Programmers make explicit use of it and the result have little to do with logic Reasoning about the order of events in Prolog is comparable in difficaulty with most imperative of conventional programming languages Cut doesn’t make any sense in logic! not doesn’t correspond to logical negation

117 Improving Efficiency Prolog is far from an efficient language.
So, it’s applications are limited to apps in which: Performance is not important Difficult to implement in a conventional lang. New methods are invented Some compilers produce code comparable to LISP

118 Toward Nonprocedural Programming
Pure logic programs prove the possibility of nonprocedural programming. In Prolog, DFS requires programmers to think in terms of operations and their proper ordering in time (procedurally). And Prolog’s control regime is more unnatural than conventional languages. So, there is still much more important work to be done before nonprocedural programming becomes practical.

119 Presentation References
Colmerauer, Alain, Philippe Roussel, The Birth of Prolog, Nov. 1992, URL: Fisher, J.R., Prolog Tutorial, 2004, URL: MacLennan, Bruce J., Principles of Programming Languages: Design, Evaluation and Implementation, 3rd ed, Oxford University Press, 1999 Merritt, Dennis, “Prolog Under the Hood: An Honest Look”, PC AI magazine, Sep/Oct 1992 “Unification”, Wikipedia, the free encyclopedia, 25 Sep. 2005, URL:

120 Free Prolog Access SWI-Prolog http://www.swi-prolog.org/ YAProlog
Strawberry Prolog

121 Sources http://www.afm.sbu.ac.uk/logic-prog/
Pages of our textbook CS 326 lectures on Prolog (written by Dr. Mircea Nicolescu)

122 Danielle and Joseph Bennett Sadegh Dorri Nogourani
sources Danielle and Joseph Bennett Michael Scherger Giles Oatley MacLennan Sadegh Dorri Nogourani


Download ppt "Artificial Intelligence programming and Prolog Language Tutorial"

Similar presentations


Ads by Google