More on Prolog syntax Already seen program statement types: Already seen program statement types: rules: p(X) :- q(X,Y), r(Y,z). rules: p(X) :- q(X,Y),

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
SLD-resolution Introduction Most general unifiers SLD-resolution
Prolog.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
Comp 307 Lecture 4:1 Prolog I, II Prolog was taught as a procedural language Control structures: if, while, recursion Data structures: structured terms,
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog (II) Lecture 22 – Prolog (II), Spring CSE3302 Programming Languages,
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 27: Prolog’s Resolution and Programming Techniques COMP 144 Programming Language.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
CSE (c) S. Tanimoto, 2007 Unification 1 Unification Predicate calculus rules are sometimes so general that we need to create specializations of.
Prolog Ross (Tate). Filling in the Blanks Rather than reverse((a,b)) returns (b,a) Rather than reverse((a,b)) returns (b,a) What X makes reverse((a,b),
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
Prolog Programming Lecture Module 13. Objective ● What is Prolog? ● Prolog program ● Syntax of Prolog ● Prolog Control Strategy ● Execution of Prolog.
Formal Models of Computation Part II The Logic Model
LING 388: Language and Computers Sandiway Fong Lecture 4.
CS 321 Programming Languages and Compilers Prolog part 2.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
Logic Programming Two possible work modes: 1.At the lab: Use SICstus Prolog. To load a prolog file (*.pl or *.pro extension) to the interpreter, use: ?-
1 Lecture 15: Introduction to Logic Programming with Prolog (Section 11.3) A modification of slides developed by Felix Hernandez-Campos at UNC Chapel Hill.
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
Prolog Programming. 2 DATA STRUCTURES IN PROLOG PROGRAMMING TECHNIQUES CONTROL IN PROLOG CUTS.
O A procedure: a set of axioms (rules and facts) with identical signature (predicate symbol and arity). o A logic program: a set of procedures defining.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
For more notes and topics VISIT: IMPLEMENTATION OF STACKS eITnotes.com.
Review: computing list results Many programs require list results to be computed, built and returned Many programs require list results to be computed,
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Automated Reasoning Early AI explored how to automated several reasoning tasks – these were solved by what we might call weak problem solving methods as.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 21.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
© Kenneth C. Louden, Chapter 12 - Logic Programming Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Programming Languages Third Edition Chapter 4 Logic Programming.
CSE (c) S. Tanimoto, 2008 Predicate Calculus II 1 Predicate Calculus 2 Outline: Unification: definitions, algorithm Formal interpretations and satisfiability.
1 Knowledge Based Systems (CM0377) Lecture 6 (last modified 20th February 2002)
LING/C SC/PSYC 438/538 Lecture 15 Sandiway Fong. Did you install SWI Prolog?
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
C. Varela1 Logic Programming (PLP 11, CTM 9.1) Terms, Resolution, Unification, Search, Backtracking (Prolog) Relational Computation Model (Oz) Carlos Varela.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
For Friday No reading Prolog Handout 2. Homework.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
O A procedure: a set of axioms (rules and facts) with identical signature (predicate symbol and arity). o A logic program: a set of procedures (predicates),
Logic Programming Lecture 2: Unification and proof search.
COSC 2P93 Prolog: Debugging
Prolog a declarative language
Carlos Varela Rennselaer Polytechnic Institute November 11, 2016
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
Carlos Varela Rensselaer Polytechnic Institute November 14, 2017
Tests, Backtracking, and Recursion
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Chapter 11 :: Logic Languages
Prolog a declarative language
Prolog syntax + Unification
Prolog a declarative language
Prolog a declarative language
Chapter 12 :: Logic Languages
CSE (c) S. Tanimoto, 2004 Unification
CSE S. Tanimoto Unification
CSE (c) S. Tanimoto, 2002 Unification
CSE 3302 Programming Languages
Chapter 12 :: Logic Languages
PROLOG.
Presentation transcript:

More on Prolog syntax Already seen program statement types: Already seen program statement types: rules: p(X) :- q(X,Y), r(Y,z). rules: p(X) :- q(X,Y), r(Y,z). facts: likes(brian, madonna). facts: likes(brian, madonna). queries: ?- likes(madonna, brian). queries: ?- likes(madonna, brian). Argument forms: Argument forms: 1. logical variables: identifiers starting with upper-case letters 1. logical variables: identifiers starting with upper-case letters eg. Guy, X, List34... eg. Guy, X, List34... also: “_” (underscore): anonymous variable (more later) also: “_” (underscore): anonymous variable (more later) 2. constants: identifiers starting with lower-case, integers, numbers, operators 2. constants: identifiers starting with lower-case, integers, numbers, operators eg. brian, c, f45, long_name, 34, eg. brian, c, f45, long_name, 34, also: single quotes ‘XYZ’ (not commonly done) also: single quotes ‘XYZ’ (not commonly done) 1COSC 2P93 Prolog: Control

More on syntax structures: a constant with arguments structures: a constant with arguments let you create more complex data, possibly with variable components let you create more complex data, possibly with variable components look like goals or predicate names look like goals or predicate names eg. author(X, Book) eg. author(X, Book) a(b(C, D), e(f, G)) a(b(C, D), e(f, G)) tree(Left, Right) tree( tree(Left, Right), Right2) tree( tree(Left, Right), Right2) illegal: X(a, b) illegal: X(a, b) - structures can be nested arbitrarily deep - Note: no space between structure name, “(“ 2COSC 2P93 Prolog: Control

Prolog syntax: operators Prolog lets you define structures in which the structure name looks more like an algebraic operator Prolog lets you define structures in which the structure name looks more like an algebraic operator eg. infix: D + 4 → shorthand for +(D, 4) eg. infix: D + 4 → shorthand for +(D, 4) prefix: \+ G → \+ (D) prefix: \+ G → \+ (D) postfix: G++ → ++ (D) postfix: G++ → ++ (D) Let you make expressions such as: X = 2*(Y+Z-5) / 6. 3COSC 2P93 Prolog: Control

Syntax: lists list: special built-in structure list: special built-in structure [ ] → empty list; shorthand for ‘.’ ( ) [ ] → empty list; shorthand for ‘.’ ( ) [ H | T ] → first element is H, tail is list T - shorthand for ‘.’ (H, T) - shorthand for ‘.’ (H, T) [ a, b, c, d] = [H | T] → H = a, T = [ b, c, d ] - can have nested lists too: [ [a, b], c, [d, e], [ ], f] *** exercise: write this nested list in it’s “full” notation *** 4COSC 2P93 Prolog: Control

Basic Prolog execution We start with a program query(“Q”), that has the form We start with a program query(“Q”), that has the form ?- G1, G2,..., Gk. where k >= 1, Gi = pred(Arg1,..., Arg_n), n > 0 Gi = pred(Arg1,..., Arg_n), n > 0 Prolog interpreter tries to solve Q Prolog interpreter tries to solve Q Basically, interpreter tries to reduce Q to an empty list of goals Basically, interpreter tries to reduce Q to an empty list of goals when empty --> a solution has been found, and it is returned to user when empty --> a solution has been found, and it is returned to user otherwise, either the program fails (no solution) or does not terminate (the query doesn’t shrink) otherwise, either the program fails (no solution) or does not terminate (the query doesn’t shrink) 5COSC 2P93 Prolog: Control

Prolog execution standard Prolog execution strategy: standard Prolog execution strategy: 1. computation rule (or goal selection rule): goals in query are solved from left--to--right goals in query are solved from left--to--right 2. search rule (or clause selection rule): program clauses are matched or unified with query goals in the order they’re found in the program program clauses are matched or unified with query goals in the order they’re found in the program 3. backtracking: when there is no clause that matches, go back to the last place you chose a clause, and try the next one. when there is no clause that matches, go back to the last place you chose a clause, and try the next one. 6COSC 2P93 Prolog: Control

Unification unification: Prolog’s matching technique unification: Prolog’s matching technique type of symbolic pattern matching type of symbolic pattern matching given two terms, finds a set of variable substitutions which, when applied to each term, results in the same term given two terms, finds a set of variable substitutions which, when applied to each term, results in the same term finds most general substitions (least specific same term) finds most general substitions (least specific same term) = is the builtin call to unification algorithm = is the builtin call to unification algorithm Remember: logic variables are placeholders for constants, structures Remember: logic variables are placeholders for constants, structures they therefore unify with anything they therefore unify with anything 7COSC 2P93 Prolog: Control

Unification algorithm (see Section 2.2 Bratko ) (see Section 2.2 Bratko ) 1. an uninstantiated (ie. unbound) variable unifies with anything 2. a constant or integer will unify only with itself 3. a structure will unify with another structure only if: a) it has the same structure name and number of arguments a) it has the same structure name and number of arguments b) all the corresponding arguments unify (ie. recursively call unify on them!) b) all the corresponding arguments unify (ie. recursively call unify on them!) 8COSC 2P93 Prolog: Control

Example unifications 1. apples = apples 2. apples = 4 3. X = cat 4. X = dog(Y) 5. Apples = Oranges 6. mouse(a, X) = mouse(D, 2) 7. cat(a, B, Y) = cat(A, X, c(d)) 8. dog(a, b) = dog (A, B, c) 9. m( A, b, A) = m(1, b, 2) 10. [ 1, 2, 3, 4 ] = [ H | T ] 11. [ 1, 2, 3, 4, [5, 6] ] = [ A, B | C] 12. [ 1, 2 ] = [A, B, C] 13. likes(brian, adriana_lima) = likes(adriana_lima, brian) 9COSC 2P93 Prolog: Control

Applying unifying substitutions the unification algorithm’s output is a set of variable bindings the unification algorithm’s output is a set of variable bindings set might be empty! set might be empty! from previous slide, if we apply a substitution to each term, we get the identical term from previous slide, if we apply a substitution to each term, we get the identical term (eg. 6) mouse(a, X) = mouse(D, 2) → yes { D <- a, X <- 2 } → mouse(a, 2) (eg. 7) cat(a, B, Y) = cat(A, X, c(d)) → yes: { A <- a, B <- X, Y <- c(d) } → cat(a, B, c(d)) 10COSC 2P93 Prolog: Control

Unifying during execution ?- p(a, X, b(Y), 1). p(b, A, B, C). %1 p(a, b, Z, W).%2 p(A, B, A, C).%3 p(a, X, b(25), C) :- q(X, 44), r(C).%4 p(_, d, Y, _).%5 p(1, 2, 3).%6 treat the goal and predicate as structures to unify, and apply unification to them treat the goal and predicate as structures to unify, and apply unification to them note how data passes in two directions during unification note how data passes in two directions during unification eg. case 2:{ X <- b, Z <- b(Y) } eg. case 2:{ X <- b, Z <- b(Y) } this permits very powerful predicates: arguments can supply data to predicate, OR return computed data from predicate this permits very powerful predicates: arguments can supply data to predicate, OR return computed data from predicate 11COSC 2P93 Prolog: Control

More example unifications 1. foo(1, foo(2,3)) = foo(1,Z) 2. ‘.’(1, ‘.’(2, 3)) = ‘.’(1, Z 3. [1 | [2, 3]] = [1 | Z] 4. [1, [2, 3] ] = [1, 2, 3] 5. [a,b,c] = [a| [b,c] ] 6. [a, b, c] = [A] 7. [a, [b, c]] = [X, Y] 8. [a | [b, c] = [X, Y] 9. [a | [b | [ c]]] = [X | Y] 10. [a, b, c] = [X, Y, Z] 11. [a, b, c] = [X, Y, X] 12. [[a], b] = [X|Y] 13. [[a], [b]] = [X|Y] 14. [_] = [X|Y] 12COSC 2P93 Prolog: Control

Back to Prolog execution LOOP until Q is empty OR cannot find a solution: take first goal G_1 in Q; (eg. G_1 = p(A1,..., An)) take first goal G_1 in Q; (eg. G_1 = p(A1,..., An)) unify G_1 with a clause in the predicate p; unify G_1 with a clause in the predicate p; try each clause in the order found in predicate try each clause in the order found in predicate a) if no clauses unify --> BACKTRACK a) if no clauses unify --> BACKTRACK b) otherwise, for the first clause that unifies: b) otherwise, for the first clause that unifies: let the clause be p(B1,...,Bn) :- H1,..., Hm. (m >= 0) let the clause be p(B1,...,Bn) :- H1,..., Hm. (m >= 0) (refresh variable names in it) (refresh variable names in it) let the binding substition set be  let the binding substition set be  replace G1 in query with H1,...,Hm replace G1 in query with H1,...,Hm apply  to the whole query apply  to the whole query note: when a rule is used, the query can grow in size note: when a rule is used, the query can grow in size while facts cause the query to shrink in size while facts cause the query to shrink in size BACKTRACK: this causes interpreter to go back to the last place where a choice of clauses was made, and to try next one BACKTRACK: this causes interpreter to go back to the last place where a choice of clauses was made, and to try next one 13COSC 2P93 Prolog: Control

Execution trees It is difficult to conceptualize the execution of Prolog when backtracking and recursion is occurring. It is difficult to conceptualize the execution of Prolog when backtracking and recursion is occurring. But Prolog program execution can be more easily “visualized” by using an execution tree (aka search tree) But Prolog program execution can be more easily “visualized” by using an execution tree (aka search tree) (a) root of tree = program query (b) non-leaf nodes of trees =intermediate computed queries (c) branch = the unification of 1st goal of parent node with a clause can label branch with (i) clause selected, and (ii) variable substitutions used in unification can label branch with (i) clause selected, and (ii) variable substitutions used in unification order of branches from left to right reflects order of clauses in program order of branches from left to right reflects order of clauses in program (d) leaf nodes: either success or failure success leafs: can give final substitutions of goal variables success leafs: can give final substitutions of goal variables failure leafs: represent places when interpreter does backtracking failure leafs: represent places when interpreter does backtracking 14COSC 2P93 Prolog: Control

Execution trees 1: plan_date(Guy, Gal, Food) :- likes(Guy, Food), likes(Gal, Food). 2: likes(tom, sushi). 3: likes(tom, pasta). 4: likes(tom, bbq). 5: likes(sue, pasta). 6: likes(sue, bbq). ?- plan_date(tom, sue, Food) ?- likes(tom,Food), likes(sue, Food) ?- likes(sue, sushi) ?- likes(sue, pasta) ?- likes(sue, bbq) 1 {Guy=tom, Gal=sue} 2 {Food=sushi} 3 {Food=pasta} 4{Food=bbq} 56 {Food <- pasta } { Food <- bbq } Food=pastaFood=bbq 15

Backtracking Variables are unique within each clause. always rename variables in clauses so that they don't clash with those in the current goal eg. grandparent(A,B) :- parent(A,X), parent(X,B). parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). ?- grandparent(X, tom). ( grandparent(A,B) :- parent(A, X' ), parent(X', B). ) ?- parent(A, X'), parent(X', tom). ž = { X <- A, B <- tom } 16COSC 2P93 Prolog: Control

Another execution tree p(X,Z) :- a(X,Y), p(Y,Z). %1 p(X,Z) :- a(X,Z).%2 a(a,b).%3 a(b,c).%4 ?- p(a,Z). ?- a(a,Y1), p(Y1, Z). ?- a(a, Z). ?- p(b,Z). ?- a(b,Y2), p(Y2, Z). ?- p(c,Z). ?- a(c,Y3), p(Y3,Z). ?- a(c,Z). ?- a(b,Z) Z <- c Z <- b 17

Some possible program behaviors 1. no solutions: output --> "no" eg. ?- pet(cat). pet(dog). 2. a finite number of solutions eg. ?- pet(X). pet(cat). pet(dog). 3. an infinite number of solutions eg. ?- pet(Y). pet(dog). pet(X) :- pet(X). 4. non-termination, and eventually memory overflow eg. ?- pet(X). pet(Y) :- pet(Y). 5. computation error: bad use of builtin predicates 6. bad solution: eg. pet(television). (erroneous logic) 18COSC 2P93 Prolog: Control

Arithmetic Note that arithmetic expressions in Prolog are simply structures Note that arithmetic expressions in Prolog are simply structures 1+X*Y-4 --> - (+ (1, * (X, Y)), 4) Unification ( = ) uses symbolic structures to unify terms Unification ( = ) uses symbolic structures to unify terms Hence, unification does not see the arithmetic values of expressions Hence, unification does not see the arithmetic values of expressions * 4 = 14 --> fails! * 4 = 14 --> fails! is : builtin arithmetic equality operator is : builtin arithmetic equality operator form: X is Expression form: X is Expression whereX is either a variable (instantiated or not) or a constant whereX is either a variable (instantiated or not) or a constant Expression is an arithmetic expression Expression is an arithmetic expression all variables in Expression MUST be unified, else a run-time error all variables in Expression MUST be unified, else a run-time error ‘is’ (i) evaluates Expression; (ii) unifies value of expression with X ‘is’ (i) evaluates Expression; (ii) unifies value of expression with X (hence X is a variable or constant) (hence X is a variable or constant) 19COSC 2P93 Prolog: Control

Arithmetic ?- X is ?- Y = 4, X is 3 * Y. ?- X is 3 * Y. ?- Y is (6 + 6) / 3. ?- 4.0 is (6 + 6) / 3. ?- 4 is (6 + 6) / 3. ? is 3 * 4. ?- cat is COSC 2P93 Prolog: Control