1 Prolog II. 2 The Notion of Unification Unification is when two things “become one” Unification is kind of like assignment Unification is kind of like.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
Prolog Nonmonotonic logic.
Inference Rules Universal Instantiation Existential Generalization
Chapter 11 :: Logic Languages
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Inference and Reasoning. Basic Idea Given a set of statements, does a new statement logically follow from this. For example If an animal has wings and.
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
F22H1 Logic and Proof Week 7 Clausal Form and Resolution.
1 Prolog III. 2 Lists [ ] is the empty list. [x, 2+2, [a, b, c]] is a list of three elements. The first element in the list is its “head”. The list with.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
11-Jun-15 Prolog II Unification and clause order.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
Lesson 6. Refinement of the Operator Model This page describes formally how we refine Figure 2.5 into a more detailed model so that we can connect it.
Formal Models of Computation Part II The Logic Model
1 Prolog I. 2 Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
CS 321 Programming Languages and Compilers Prolog part 2.
CS 2104 – Prog. Lang. Concepts Logic Programming - II Dr. Abhik Roychoudhury School of Computing.
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
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.
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
Derived from csc.villanova.edu/~dmatusze/8310summer2001/index.html/prolog1.ppt1 Prolog.
1 Artificial Intelligence Prolog for. 2 SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux It's licensed under GPL, therefore free.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
CSC 270 – Survey of Programming Languages Prolog Lecture 2 – Unification and Proof Search.
Prolog Programming in Logic. 2 SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux Can be installed on Macintosh with a little more.
Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.
1 CSC 8520 Spring Paula Matuszek Slides taken from David Matuszek,
Programming Languages. A Logic Programming Language: Prolog This lesson describes the basic structures and functions of the logic programming language:
CS4026 Formal Models of Computation Part II The Logic Model Lecture 2 – Prolog: History and Introduction.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
Dr. Muhammed Al-Mulhem ICS An Introduction to Logical Programming.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
CSE (c) S. Tanimoto, 2008 Predicate Calculus II 1 Predicate Calculus 2 Outline: Unification: definitions, algorithm Formal interpretations and satisfiability.
Prolog Unification and clause order. The notion of Unification Unification is when two things “become one” Unification is kind of like assignment Unification.
9/19/01 1 Prolog Tim Finin Tim Finin University of Maryland Baltimore County.
Logic Programming. Formal Logics- Recap Formulas – w/out quantifiers Free Variables Bound Variables Assignments and satisfaction Validity and satisfiability.
Knowledge Based Information System
For Monday Exam 1 is Monday Takehome due Prolog Handout 3 due.
1-1 An Introduction to Logical Programming Sept
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.
1 Knowledge Based Systems (CM0377) Lecture 9 (Last modified 11th April 2002)
23-Feb-16 Prolog II Unification and clause order.
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 2: Unification and proof search.
COSC 2P93 Prolog: Debugging
Prolog Concepts.
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Tests, Backtracking, and Recursion
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Chapter 11 :: Logic Languages
Prolog Lists.
Unification and clause order
Chapter 12 :: Logic Languages
Prolog III.
Prolog Lists.
Programming Techniques
Prolog Concepts.
Chapter 2: Prolog (Introduction and Basic Concepts)
Prolog III Lists 8-Jul-19.
Prolog Concepts.
Presentation transcript:

1 Prolog II

2 The Notion of Unification Unification is when two things “become one” Unification is kind of like assignment Unification is kind of like equality in algebra Unification is mostly like pattern matching Example: –loves(john, X) can unify with loves(john, mary) –and in the process, X gets unified with mary

3 Unification I Any value can be unified with itself. –weather(sunny) = weather(sunny) A variable can be unified with another variable. –X = Y A variable can be unified with (“instantiated to”) any Prolog value. –Topic = weather(sunny)

4 Unification II Two different structures can be unified if their constituents can be unified. –mother(mary, X) = mother(Y, father(Z)) A variable can be unified with a structure containing that same variable. This is usually a Bad Idea. –X = f(X)

5 Unification III The explicit unification operator is = Unification is symmetric: Steve = father(isaac) means the same as father(isaac) = Steve Most unification happens implicitly, as a result of parameter transmission.

6 Scope of Names Scope of a variable is the single clause in which it appears Scope of the “anonymous” (“don't care”) variable, _, is itself. –loves(_, _) = loves(john, mary) A variable that only occurs once in a clause is a useless singleton; you should replace it with the anonymous variable

7 Writing Prolog Programs Suppose the database contains loves(chuck, X) :- female(X), rich(X). female(jane). and we ask who Chuck loves, ?- loves(chuck, Woman). female(X) finds a value for X, say, jane rich(X) then tests whether Jane is rich

8 Clauses as Cases A predicate consists of multiple clauses, each of which represents a “case” grandson(X,Y) :- son(X,Z), son(Z,Y). grandson(X,Y) :- son(X,Z), daughter(Z,Y). abs(X, Y) :- X < 0, Y is -X. abs(X, X) :- X >= 0.

9 Ordering Clauses are always tried in order buy(X) :- good(X). buy(X) :- cheap(X). cheap(‘Java 2 Complete’). good(‘Thinking in Java’). What will buy(X) choose first?

10 Ordering II Try to handle more specific cases (those having more variables instantiated) first dislikes(john, bill). dislikes(john, X) :- rich(X). dislikes(X, Y) :- loves(X, Z), loves(Z, Y).

11 Ordering III Some "actions" cannot be undone by backtracking over them: –write, nl, assert, retract, consult Do tests before you do undoable actions: –take(A) :- at(A, in_hand), write('You\'re already holding it!'), nl.

12 Recursion Handle the base cases first Recur only with a simpler case ancestor(X, Y) :- child(Y, X). (X is an ancestor of Y if Y is a child of X.) ancestor(X, Y) :- child(Z, X), ancestor(Z, Y). (X is an ancestor of Y if Z is a child of X and Z is an ancestor of Y).

13 Case Level You can often choose the "level" at which you want cases to be defined. son(isaac, steven). child(X, Y) :- son(X, Y). % make male/1 and child/2 facts male(isaac). child(isaac, steven). % define son in terms of male/1 and child/2 son(X, Y) :- male(X), child(X, Y).

14 Recursive Loops Prolog proofs must be tree structured, that is, they may not contain recursive loops. –child(X,Y) :- son(X,Y). –son(X,Y) :- child(X,Y), male(X). – ?- son(isaac, steven).  May loop! Why? Neither child/2 nor son/2 is atomic

15 Cut and Cut-fail The cut, !, is a commit point. It commits to: –the clause in which it occurs (can't try another) –everything up to that point in the clause Example: – loves(chuck, X) :- female(X), !, rich(X). –Chuck loves the first female in the database, but only if she is rich. Cut-fail, (!, fail), means give up now and don't even try for another solution.

16 What you can't do There are no functions, only predicates Prolog is programming in logic, therefore there are few control structures There are no assignment statements; the state of the program is what's in the database

17 Workarounds I There are few control structures in Prolog, BUT… You don't need IF because you can use multiple clauses with "tests" in them You seldom need loops because you have recursion You can, if necessary, construct a "fail loop"

18 Fail Loops Use fail loops sparingly, if at all. notice_objects_at(Place) :- at(X, Place), write('There is a '), write(X), write(' here.'), nl, fail. notice_objects_at(_).

19 Workarounds II There are no functions, only predicates, BUT… A call to a predicate can instantiate variables: female(X) can either –look for a value for X that satisfies female(X), or –if X already has a value, test whether female(X) can be proved true By convention, output variables are put last

20 Workarounds III There are no assignment statements, BUT… the Prolog database keeps track of program state –assert(at(fly, bedroom)) –bump_count :- retract(count(X)), Y is X + 1, assert(count(Y)). Don't get carried away and misuse this!

21 The End