CSC 270 – Survey of Programming Languages Prolog Lecture 2 – Unification and Proof Search.

Slides:



Advertisements
Similar presentations
© Johan Bos Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical work [lab] Teaching material –Learn Prolog.
Advertisements

CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic.
First Order Logic Logic is a mathematical attempt to formalize the way we think. First-order predicate calculus was created in an attempt to mechanize.
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: ?-
Chapter 11 :: Logic Languages
Prolog.
1 A formula in predicate logic An atom is a formula. If F is a formula then (~F) is a formula. If F and G are Formulae then (F /\ G), (F \/ G), (F → G),
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
Declarative Programming Autumn 2014 Basic syntax and sample programs.
Prolog: Unification & Recursion © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Spin Tutorial (some verification options). Assertion is always executable and has no other effect on the state of the system than to change the local.
1 Introduction to Computability Theory Lecture12: Reductions Prof. Amos Israeli.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 27: Prolog’s Resolution and Programming Techniques COMP 144 Programming Language.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 2 Theory –Unification –Unification in Prolog –Proof search Exercises –Correction exercises.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
Logic Programming Part 2: Semantics James Cheney CS 411.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
CS1502 Formal Methods in Computer Science Lecture Notes 10 Resolution and Horn Sentences.
PROLOG KAIST Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.
Formal Models of Computation Part II The Logic Model
CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
LING 388: Language and Computers Sandiway Fong Lecture 4.
CS 321 Programming Languages and Compilers Prolog part 2.
Agile Software Development Lab 2008 Dr. Günter Kniesel, Daniel Speicher, Tobias Rho, Pascal Bihler Spring 2008 R O O T S Prolog - Part 1 Alexis Raptarchis.
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.
1 Knowledge Based Systems (CM0377) Lecture 4 (Last modified 5th February 2001)
14/10/04 AIPP Lecture 7: The Cut1 Controlling Backtracking: The Cut Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 7 14/10/04.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
F28PL1 Programming Languages Lecture 16: Prolog 1.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Programming Languages. A Logic Programming Language: Prolog This lesson describes the basic structures and functions of the logic programming language:
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 2 – Prolog: History and Introduction.
Prolog Kyle Marcotte. Outline What is Prolog? Origins of Prolog (History) Basic Tutorial TEST!!! (sort of…actually not really at all) My example Why Prolog?
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
Rules Statements about objects and their relationships Expess ◦ If-then conditions  I use an umbrella if there is a rain  use(i, umbrella) :- occur(rain).
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.
PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler.
Lab Lecture#4 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
Introduction to Prolog
Introduction to Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
1 Knowledge Based Systems (CM0377) Lecture 6 (last modified 20th February 2002)
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
Knowledge Based Information System
CS 554: Knowledge base systems Part-4: Prolog- 2 By Dr. Syed Noman Hasany.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
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:... :-....
Logic Programming Lecture 8: Term manipulation & Meta-programming.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
Instructor :Bayan Ghozlan الاستاذه : بيان غزلان.  البرولوغ Prolog هي لغة برمجة منطقية. وتعني برمجه المنطق programing language  تم اختراع اللغة بواسطة.
Logic Programming Lecture 2: Unification and proof search.
Pengenalan Prolog Disampaikan Oleh : Yusuf Nurrachman, ST, MMSI.
علمتني الثلوج أن أكون كبياض الثلج لا يحمل قلبي غير الحب والعطف والرحمة لا أسيئ الظن بالأخرين ولا أحكم عليهم من مظهرهم فإذا لم تعاشرهم فأنت تجهلهم !
The function of knowledge representation scheme is
Tests, Backtracking, and Recursion
Prolog syntax + Unification
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Presentation transcript:

CSC 270 – Survey of Programming Languages Prolog Lecture 2 – Unification and Proof Search

Unification From Knowledge Base 4: woman(mia). woman(jody). woman(yolanda). … Querying Knowledge Base 4: 1 ?- woman(X). X = mia. 2 ?- Prolog unifies woman(X) with woman(mia), thereby instantiating the variable X to mia

Types in Prolog – A Quick Review Recall that there are three types of term: – Constants. These can either be atoms (such as vincent ) or numbers (such as 24 ). – Variables. (Such as X, Z3, and List.) – Complex terms. These have the form: – functor(term_1,...,term_n).

Unification – A Definition Two terms unify: – they are the same term or – if they contain variables that can be uniformly instantiated with terms in such a way that the resulting terms are equal.

Unification – An Example This means that – mia and mia unify, because they are the same atom. – 42 and 42 unify, because they are the same number. – X and X unify, because they are the same variable. – woman(mia) and woman(mia) unify, because they are the same complex term.

Unification – An Example (continued) This means that – woman(mia) and woman(vincent) do not unify, because they are not the same (and neither of them contains a variable that could be instantiated to make them the same).

Unification and Instantiation mia and X are not the same, but X can be instantiated to mia which makes them equal. Therefore, mia and X unify. woman(X) and woman(mia) unify, because they can be made equal by instantiating X to mia. loves(vincent,X) and loves(X,mia) do not unify because it is impossible to find an instantiation of X that makes the two terms equal.

A Formal Definition of Unification If term1 and term2 are constants, then term1 and term2 unify if and only if they are the same atom, or the same number.

A Formal Definition of Unification (continued) If term1 is a variable and term2 is any type of term, then term1 and term2 unify, and term1 is instantiated to term2. If term2 is a variable and term1 is any type of term, then term1 and term2 unify, and term2 is instantiated to term1. If they are both variables, they’re both instantiated to each other, and we say that they share values.

A Formal Definition of Unification (continued) If term1 and term2 are complex terms, then they unify if and only if: – They have the same functor and arity, and – all their corresponding arguments unify, and – the variable instantiations are compatible.

A Formal Definition of Unification (continued) E. g., it’s impossible to instantiate variable X to mia when unifying one pair of arguments, and to instantiate X to vincent when unifying another pair of arguments.) Two terms unify if and only if it follows from the previous three clauses that they unify.

Unification – Some Examples 1 ?- =(mia, mia). true. 2 ?- =(2, 2). true. 3 ?- mia = mia. true. 4 ?- 2 = 2. true. 5 ?- mia = vincent. false. 6 ?- 'mia' = mia. true. 7 ?- '2' = 2. false. 8 ?-

8 ?- mia = X. X = mia. 9 ?- mia = X. X = mia. 10 ?- X = Y. X = Y. 11 ?- X = _5067. X = _ ?- Y = _5067. Y = _ ?- X = mia, X = vincent. false.

Unification – Another Example 2 ?- k(s(g), Y) = k(X, t(k)). Y = t(k), X = s(g). 3 ?- We use clause 3 because we are trying to unify two complex terms. Do both complex terms have the same functor and arity? Yes. So do the first arguments, s(g) and X, unify? By clause 2, yes. So do the second arguments, Y and t(k), unify? By clause 2, yes.

Unification – Other Examples 3 ?- k(s(g), t(k)) = k(X, t(Y)). X = s(g), Y = k. 4 ?- loves(X, X) = loves(marcellus, mia). false. 5 ?- These terms do not unify because while they are both complex terms and have the same functor and arity, all corresponding arguments do not unify in this example

Prolog and Unification Algorithm Prolog does not use a standard unification algorithm when it performs its version of unification; it takes a shortcut. You need to know about this shortcut. Consider the following query: ?- father(X) = X.

Prolog and Unification Algorithm (continued) Do these terms unify or not? No Why is that? Pick any term and instantiate X to the term you picked. father(father(father(butch))) = father(father(butch)) and so on.

Prolog and Unification Algorithm (continued) Prolog unification won’t spot the problem and halt. Having instantiated X to father(X), Prolog is committed to carrying out an unending sequence of expansions and will run until it runs out of memory.

Prolog and Unification Algorithm (continued) There are actually 3 responses to “does father(X) unify with X ”. – Standard unification algorithm (no) – Older Prolog implementations (run amok until they use up the available memory) – Sophisticated Prolog implementations (yes) In short, there is no ‘right’ answer to this question.

occurs check A standard algorithm given two terms to unify carries out what is known as the occurs check. This means that if it is asked to unify a variable with a term, it first checks whether the variable occurs in the term. If it does, the standard algorithm declares that unification is impossible, for clearly it is the presence of the variable X in father(X) which leads to problems. Only if the variable does not occur in the term do standard algorithms attempt to carry out the unification.

Prolog and Unification Algorithm (continued) Prolog comes with a built-in predicate that carries out standard unification (that is, unification with the occurs check). The predicate is unify_with_occurs_check/2. So if we posed the query ?- unify_with_occurs_check (father(X),X). we would get the response no.

Unification Proof Search f(a). f(b). g(a). g(b). h(b). k(X):- f(X),g(X),h(X). try to match f (_1), g(_1), h(_1) When f(a), then _1 = a. Does it find f(a), g(a),h(a) – nope When f(b) then _1 = b. Does it find f(b), g(b), h(b) – yes so it stops and says true. See with trace Stop trace with notrace

Crossword puzzle match word(astante,a,s,t,a,n,t,e). word(astoria,a,s,t,o,r,i,a). word(baratto,b,a,r,a,t,t,o). word(cobalto,c,o,b,a,l,t,o). word(pistola,p,i,s,t,o,l,a). word(statale,s,t,a,t,a,l,e). crossword(X,Y, Z, A, B, C) :- word(X,_,X1,_,X2,_,X3,_), word(Y,_,Y1,_,Y2,_,Y3,_), word(Z,_,Z1,_,Z2,_,Z3,_), word(C,_,X3,_,Y3,_,Z3,_), word(B,_,X2,_,Y2,_,Z2,_), word(A,_,X1,_,Y1,_,Z1,_).

Summary Prolog proves by instantiating variables and then checking truth Stops at first complete matching truth Will continue finding match when you press ; Trace using trace / notrace Can go into infinite checking unless you call with unify_with_occurs_check