© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 1 Introduction to Prolog.

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

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
Getting started with Prolog
Computer Science CPSC 322 Lecture 25 Top Down Proof Procedure (Ch 5.2.2)
Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines
Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.
AR for Horn clause logic Introducing: Unification.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
Prolog: List © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Automated Reasoning Systems For first order Predicate Logic.
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).
Methods of Proof Chapter 7, second half.. Proof methods Proof methods divide into (roughly) two kinds: Application of inference rules: Legitimate (sound)
Logic.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Prolog: Unification & Recursion © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
1 Applied Computer Science II Resolution in FOL Luc De Raedt.
Proof methods Proof methods divide into (roughly) two kinds: –Application of inference rules Legitimate (sound) generation of new sentences from old Proof.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
© 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.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
Let remember from the previous lesson what is Knowledge representation
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
PROLOG KAIST Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.
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.
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.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
Logical Agents Logic Propositional Logic Summary
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
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.
CS Introduction to AI Tutorial 8 Resolution Tutorial 8 Resolution.
By: Cory Canter CSC 415 Programming Languages. History  Created by Alain Colmerauer, Phillipe Roussel and Robert Kowalski in 1971  Started as a natural.
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).
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.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
Introduction to Prolog
Introduction to Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
© Copyright 2008 STI INNSBRUCK Intelligent Systems Propositional Logic.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog Lecture 21 – Prolog, Spring CSE3302 Programming Languages, UT-Arlington.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Knowledge Based Information System
CS 554: Knowledge base systems Part-4: Prolog- 2 By Dr. Syed Noman Hasany.
1 Propositional Logic Limits The expressive power of propositional logic is limited. The assumption is that everything can be expressed by simple facts.
Logical Agents Chapter 7. Outline Knowledge-based agents Propositional (Boolean) logic Equivalence, validity, satisfiability Inference rules and theorem.
Proof Methods for Propositional Logic CIS 391 – Intro to Artificial Intelligence.
Done By :- -Nesreen Basem -Sara nabil -Rawan Prolog Program.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Pengenalan Prolog Disampaikan Oleh : Yusuf Nurrachman, ST, MMSI.
Logical Agents. Outline Knowledge-based agents Logic in general - models and entailment Propositional (Boolean) logic Equivalence, validity, satisfiability.
علمتني الثلوج أن أكون كبياض الثلج لا يحمل قلبي غير الحب والعطف والرحمة لا أسيئ الظن بالأخرين ولا أحكم عليهم من مظهرهم فإذا لم تعاشرهم فأنت تجهلهم !
Chapter 11 :: Logic Languages
Prolog Search Strategy
Logic: Top-down proof procedure and Datalog
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Chapter 12 :: Logic Languages
Introduction to Prolog
Representations & Reasoning Systems (RRS) (2.2)
Chapter 12 :: Logic Languages
Presentation transcript:

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 1 Introduction to Prolog

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Learn Prolog Now! Important: be aware that there are different versions of the LPN book: the online html version, the hard copy book, and online pdf. They differ in particular wrt exercises and their numbering. When referring to exercises from the LPN book, I refer to the online html version as can be found on

© Patrick Blackburn, Johan Bos & Kristina Striegnitz SWI Prolog Freely available Prolog interpreter Works with –Linux, –Windows, or –Mac OS There are many more Prolog interpreters Not all are ISO compliant

Aim of this lecture LPN Ch. 1-3 After this lecture, you should be able to explain the meaning of the following key notions and their relation to Prolog: –logic programming –facts, rules and queries (horn clauses) –unification –(most general) unifier (mgu) –backtracking, depth-first search, forward & backward chaining Important: I will not discuss everything from the book in the lectures, but it is to be studied for the exam (to the extent indicated on blackboard)! I strongly advise you to read the relevant parts of the book before the practical sessions.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Introduction

Prolog Colmerauer & Roussel, 1972 Kowalski, Predicate Logic as Programming Language, In Proceedings IFIP Congress, pp

Grace Hopper “Mother of COBOL” (1959) programs should be written in a language that is close to English rather than in machine code

Prolog (2)

© Patrick Blackburn, Johan Bos & Kristina Striegnitz removeThrees in Java static void removeThrees (ArrayList list) { Iterator iterator = list.iterator(); while (iterator.hasNext()) { if (iterator.next() == 3) { iterator.remove(); } } Procedural language: step by step instructions describing what should be computed

© Patrick Blackburn, Johan Bos & Kristina Striegnitz removeThrees in Prolog removeThrees([],[]). removeThrees([3|T],L) :- removeThrees(T,L). removeThrees([H|T],[H|T2]) :- not(H = 3), removeThrees(T,T2). What are the differences with Java?

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Prolog "Programming with Logic" Declarative: separation of knowledge base and inference engine Very different from other (procedural) programming languages Good for knowledge-rich tasks, logical reasoning Initially, small programming tasks to practice specific skills. More elaborate programs as part of GOAL in the 2 nd half of the course, and in Q4.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Why Learn Prolog?

It is the knowledge representation language of GOAL, which will be used to program UT2004 agents in Q4 Different programming paradigm It is a challenge! One of the most well-known AI languages Learn fundamental programming concepts, in particular recursion and recursive data structures WARNING: it seems easy at first

Prolog Development Center advanced scheduling systems and speech based applications for manufacturing, retail and service businesses, larger public institutions, airlines and airports

Visual Prolog support industrial strength programming of complex knowledge emphasized problems. combining the very best features of logical, functional and object-oriented programming paradigms in a consistent and elegant way.

Clarissa at ISS fully voice-operated procedure browser, enabling astronauts to be more efficient with their hands and eyes and to give full attention to the task while they navigate through the procedure using spoken commands

OntoDLV OntoDLV is an intelligent ally for quickly and easily developing powerful knowledge-based applications and decision support systems (e.g. Planning, Customer Profiling or Workforce Scheduling) that otherwise require complex and labourious programming.

Ontological Logic Programming Murat Sensoy, Geeth de Mel, Wamberto W. Vasconcelos and Timothy J. Norman, "Position Paper: Ontological Logic Programming", Proceedings of the 3rd International Workshop on Semantic Sensor Networks (SSN10), 2010.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Facts, Rules and Queries

Alle mensen zijn sterfelijk m(socrates). s(X) :- m(X). M(socrates), ∀ x(M(x) → S(x)) fact rule Variables capitalized, predicates lower case Implication ( :- ) from right to left Each clause is terminated with a full stop. Clauses are implicitly universally quantified. horn clauses

Head and Body of Rule m(socrates). s(X) :- m(X). fact rule Variables capitalized, predicates lower case Implication ( :- ) from right to left Each clause is terminated with a full stop. Clauses are implicitly universally quantified. body head

Is socrates sterfelijk? m(socrates). s(X) :- m(X). M(socrates), ∀ x(M(x) → S(x)) |= S(socrates) “?-” is the SWI-Prolog prompt asking whether a goal formula logically follows from the knowledge base ?- s(socrates). query

Is socrates sterfelijk? m(socrates). s(X) :- m(X).  ∀  →  “?-” is the SWI-Prolog prompt asking whether a goal formula logically follows from the knowledge base ?- s(socrates). yes ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Knowledge Base 4 woman(mia). woman(jody). woman(yolanda).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Prolog Variables woman(mia). woman(jody). woman(yolanda). ?- woman(X).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Variable Instantiation woman(mia). woman(jody). woman(yolanda). ?- woman(X). X=mia returning variable instantiations instead of “only” whether a goal follows from the KB or not, is what gives Prolog its programming power!

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Asking Alternatives woman(mia). woman(jody). woman(yolanda). ?- woman(X). X=mia;

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Asking Alternatives woman(mia). woman(jody). woman(yolanda). ?- woman(X). X=mia; X=jody

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Asking Alternatives woman(mia). woman(jody). woman(yolanda). ?- woman(X). X=mia; X=jody; X=yolanda

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Asking Alternatives woman(mia). woman(jody). woman(yolanda). ?- woman(X). X=mia; X=jody; X=yolanda; no

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Knowledge Base 5 loves(vincent,mia). loves(marsellus,mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). jealous(X,Y):- loves(X,Z), loves(Y,Z). The comma “," expresses conjunction in Prolog

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Knowledge Base 5 loves(vincent,mia). loves(marsellus,mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). jealous(X,Y):- loves(X,Z), loves(Y,Z). ?- jealous(marsellus,W). how many variable instantiations does Prolog return for W?

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Knowledge Base 5 loves(vincent,mia). loves(marsellus,mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). jealous(X,Y):- loves(X,Z), loves(Y,Z). ?- jealous(marsellus,W). W=vincent

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Knowledge Base 5 loves(vincent,mia). loves(marsellus,mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). jealous(X,Y):- loves(X,Z), loves(Y,Z). ?- jealous(marsellus,W). W=vincent; W=marsellus; no

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Syntax

A Few Remarks Be careful: terminology in Learn Prolog Now differs (somewhat) from standard terminology in predicate logic (in particular atom and term)! Prolog atoms (constants) start with lower case, but can also be an arbitrary sequence of characters enclosed in single quotes Examples: 'Vincent', 'Five dollar shake', Variables start with upper case or underscore

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Prolog Syntax What exactly are facts, rules and queries built out of? Terms Simple TermsComplex Terms ConstantsVariables AtomsNumbers Terms Simple TermsComplex Terms ConstantsVariables AtomsNumbers

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Arity is important In Prolog you can define two predicates with the same functor but with different arity Prolog would treat this as two different predicates In Prolog documentation arity of a predicate is usually indicated with the suffix "/" followed by a number to indicate the arity

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Arity This knowledge base defines –happy/1 –listens2music/1 –playsAirGuitar/1 happy(yolanda). listens2music(mia). listens2music(yolanda):- happy(yolanda). playsAirGuitar(mia):- listens2music(mia). playsAirGuitar(yolanda):- listens2music(yolanda).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Unification

From Redeneren en Logica: A subsitution that makes two literals equal is called a unifier. Searching such a substitution is unification. A most general unifier (mgu) is a unifier θ for which it holds that every unifier is an “extension” of θ. Unification is a fundamental computation mechanism in Prolog! The unifier resulting from unification is what gives Prolog its programming power.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Unification 1.If T 1 and T 2 are constants, then T 1 and T 2 unify if they are the same atom, or the same number. 2.If T 1 is a variable and T 2 is any type of term, then T 1 and T 2 unify, and T 1 is instantiated to T 2. (and vice versa) 3.If T 1 and T 2 are complex terms then they unify if: a)They have the same functor and arity, and b)all their corresponding arguments unify, and c)the variable instantiations are compatible.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Exercise: unification Which of the following pairs of terms unify? Which variable instantiations does unification give rise to? 1. ‘Bread’ = bread. 2. ‘bread’ = bread. 3. food(X) = food(bread). 4. meal(food(X),Y) = meal(food(bread),Z).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Exercise: unification ?- ’Bread’ = bread. no ?- ’bread’ = bread. yes ?- food(X) = food(bread). X = bread. ?- meal(food(X),Y) = meal(food(bread),Z). X = bread, Y = Z. <= most general unifier!

© Patrick Blackburn, Johan Bos & Kristina Striegnitz ?- k(s(g),Y) = k(X,t(k)). Prolog unification: =/2

© Patrick Blackburn, Johan Bos & Kristina Striegnitz ?- k(s(g),Y) = k(X,t(k)). X=s(g) Y=t(k) yes ?- Prolog unification: =/2 Important: The equals sign (=) is Prolog’s unification operator. Do not confuse it with operators for comparing integers which look or sound similar, to be introduced later!

int myInt; myInt = 42; Unification is not Assignment The result of assignment is the assignment of a value to a variable. variablevalue myInt(X) = myInt(42). AssignmentUnification term The result of unification is an mgu or failure if no mgu exists.

int myInt; myInt = 42; Unification is not Assignment The result of assignment is the assignment of a value to a variable. variablevalue MyInt = 42. AssignmentUnification term The result of unification is an mgu or failure if no mgu exists.

int myInt; myInt = 3; myInt = 42; Unification is not Assignment The result of assignment is the assignment of a value to a variable. MyInt = 3, MyInt = 42. AssignmentUnification The result of unification is an mgu or failure if no mgu exists.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Programming with Unification

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Programming with Unification vertical( line(point(X,Y), point(X,Z))). horizontal( line(point(X,Y), point(Z,Y))).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Programming with Unification vertical( line(point(X,Y), point(X,Z))). horizontal( line(point(X,Y), point(Z,Y))). ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Programming with Unification vertical( line(point(X,Y), point(X,Z))). horizontal( line(point(X,Y), point(Z,Y))). ?- vertical(line(point(1,1),point(1,3))). yes ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Programming with Unification vertical( line(point(X,Y), point(X,Z))). horizontal( line(point(X,Y), point(Z,Y))). ?- vertical(line(point(1,1),point(1,3))). yes ?- vertical(line(point(1,1),point(3,2))). no ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Programming with Unification vertical( line(point(X,Y), point(X,Z))). horizontal( line(point(X,Y), point(Z,Y))). ?- horizontal(line(point(1,1),point(2,Y))). Y = 1; no ?- Prolog unifies the query with the second clause in the program and returns a unifier as the answer.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Programming with Unification vertical( line(point(X,Y), point(X,Z))). horizontal( line(point(X,Y), point(Z,Y))). ?- horizontal(line(point(2,3),Point)). Point = point(_554,3); no ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Proof Search Intro

Logic & Prolog Kowalski, Predicate Logic as Programming Language, In Proceedings IFIP Congress, pp LogicProlog Sentences in predicate logic (horn clauses) Program Derivation (using resolution and unification) Computation Proof proceduresExecutors of logic programs

Resolution & Proof Search (1) Predicate logic: ¬B ∨ ¬C ∨ A ¬D ∨ B ¬E ∨ C D E ¬A (¬ conclusion) ¬B ∨ ¬C ∨ A & ¬A = ¬B ∨ ¬C ¬B ∨ ¬C & ¬D ∨ B = ¬C ∨ ¬D ¬C ∨ ¬D & D = ¬C ¬C & ¬E ∨ C = ¬E ¬E & E = ☐ Prolog program: a :- b, c. b :- d. c :- e. d. e. ?- a. ➟ [a] (list of query goals) [b,c] (a :- b, c.) [d,c](b :- d.) [c](d.) [e](c :- e.) true.(e.)

Resolution & Proof Search (2) ResolutionProof search in Prolog find two clauses that can be resolved find a rule with head that unifies with current goal calculate resolventupdate goal list by replacing unified goal with body of rule repeat But: Prolog fixes order in which resolvents are computed. This can influence the result of proof search! That is, procedural meaning can differ from declarative (logical) meaning !

Why learn about proof search? declarative vs. procedural meaning of programs writing correct programs debugging Prolog does it for me, so why should I know how it works?

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Proof Search

Key Notions Prolog uses backward chaining, i.e., it only computes if a query is made! linear (top-to-bottom) search for unifying facts or heads of rules depth-first search strategy backtracking if goal fails Important: you should be able to explain using these notions how Prolog searches for a proof of a query!

Backward Chaining Backward chaining: ?- a. true Forward chaining: b. c. a. Prolog uses backward chaining: only computes if a query is made! Alternative is forward chaining, i.e., take the facts and keep on firing rules until no rule can be fired anymore. a :- b, c. b :- d. c :- e. d. e.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Depth-First Search Depth-first traversal: A→B→D→E→C→F→G

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Breadth-First Search Breadth-first traversal: A→B→C→D→E→F→G

Depth-first Search If [q1, …, qn] is the list of query goals, then select the head of the list, i.e., q1 to search for a matching clause. If q1 :- p1, p2, …,pm is the selected unified clause, then update list of query goals to [p1, p2, …, pm, q2, …, qn] and find a matching clause for p1 a :- b, c. b :- d. c :- e. d. e. a.a. b.b.c. d.e.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Linear Search & Backtracking f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). Linear Search & Backtracking

© Patrick Blackburn, Johan Bos & Kristina Striegnitz f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- f( _G34 ), g( _G34 ), h( _G34 ). Y = _G34 Linear Search & Backtracking Variables of rules are renamed (_G34) at each unification with a goal query. most general unifier

© Patrick Blackburn, Johan Bos & Kristina Striegnitz f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- g(a), h(a). _G34 = a ?- f( _G34 ), g( _G34 ), h( _G34 ). Y = _G34 Linear Search & Backtracking Linear search: f(_G34) is first unified with f(a) (the top-most clause that unifies). Resulting unifier is applied to remaining goals.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- g(a), h(a). ?- h(a). ?- f( _G34 ), g( _G34 ), h( _G34 ). Y = _G34 _G34 = a Linear Search & Backtracking

© Patrick Blackburn, Johan Bos & Kristina Striegnitz f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- g(a), h(a). ?- h(a). † ?- f( _G34 ), g( _G34 ), h( _G34 ). Y = _G34 _G34 = a Linear Search & Backtracking h(a) cannot be derived. Prolog backtracks to previous choice point.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- g(a), h(a). ?- h(a). ?- g(b), h(b). _G34 = b † ?- f( _G34 ), g( _G34 ), h( _G34 ). Y = _G34 _G34 = a Linear Search & Backtracking

© Patrick Blackburn, Johan Bos & Kristina Striegnitz f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- g(a), h(a). ?- h(a). ?- g(b), h(b). ?- h(b). † _G34 = b ?- f( _G34 ), g( _G34 ), h( _G34 ). Y = _G34 _G34 = a Linear Search & Backtracking

© Patrick Blackburn, Johan Bos & Kristina Striegnitz f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). Y=b ?- k(Y). ?- g(a), h(a). ?- h(a). ?- g(b), h(b). ?- h(b). † _G34 = b ?- f( _G34 ), g( _G34 ), h( _G34 ). Y = _G34 _G34 = a Linear Search & Backtracking

© Patrick Blackburn, Johan Bos & Kristina Striegnitz f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). Y=b; no ?- ?- k(Y). ?- g(a), h(a). ?- h(a). ?- g(b), h(b). ?- h(b). † _G34 = b ?- f( _G34 ), g( _G34 ), h( _G34 ). Y = _G34 _G34 = a Linear Search & Backtracking using semicolon: ask Prolog to backtrack.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). Trace Demo

© Patrick Blackburn, Johan Bos & Kristina Striegnitz f(1). f(2). f(3). p(3,4). q(4). r(3). r(4). r(5). g(X) :- p(X,Y), q(Y), r(X). h(X) :- f(X). h(X) :- g(X). ?- h(X). X = 1 ; X = 2 ; X = 3 ; no. Exercise 1 Output does not correspond to program. What’s wrong?

© Patrick Blackburn, Johan Bos & Kristina Striegnitz f(1). f(2). f(3). p(3,4). q(4). r(3). r(4). r(5). g(X) :- p(X,Y), q(Y), r(X). h(X) :- f(X). h(X) :- g(X). ?- h(X). X = 1 ; X = 2 ; X = 3 ; X = 3. Exercise 1 Asking Prolog to backtrack gives another substitution X = 3, resulting from unification with the second rule for h(X).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz f(3). p(3,4). q(4). r(3). r(4). r(5). g(X) :- p(X,Y), q(Y), r(X). h(X) :- f(X). h(X) :- g(X). [trace] ?- h(X). Call: (7) h(_G335) ? creep Call: (8) g(_G335) ? creep Call: (9) p(_G335, _L191) ? creep Exit: (9) p(3, 4) ? creep Call: (9) q(4) ? creep Exit: (9) q(4) ? creep Call: (9) r(3) ? creep Exit: (9) r(3) ? creep Exit: (8) g(3) ? creep Exit: (7) h(3) ? creep X = 3. Exercise 2 Output does not correspond to program. What’s wrong?

© Patrick Blackburn, Johan Bos & Kristina Striegnitz f(3). p(3,4). q(4). r(3). r(4). r(5). g(X) :- p(X,Y), q(Y), r(X). h(X) :- f(X). h(X) :- g(X). [trace] ?- h(X). Call: (7) h(_G335) ? creep Call: (8) f(_G335) ? creep Exit: (8) f(3) ? creep Exit: (7) h(3) ? creep X = 3 ; Redo: (7) h(_G335) ? creep Call: (8) g(_G335) ? creep Call: (9) p(_G335, _L191) ? creep Exit: (9) p(3, 4) ? creep..... Exercise 2 Linear search, so first unify h(X) with the head of the first rule for h(X), resulting in the goal f(_G335).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). ?- loves(_G5,_G6), loves(_G7,_G6). X=_G5Y=_G7

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). ?- loves(_G7,mia). _G5 =vincent _G6 =mia ?- loves(_G5,_G6), loves(_G7,_G6). X=_G5Y=_G7

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). X=vincent Y=vincent ?- jealous(X,Y). ?- loves(_G7,mia). _G5 =vincent _G6 =mia ?- loves(_G5,_G6), loves(_G7,_G6). X=_G5Y=_G7 _G7=vincent

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). X=vincent Y=vincent; X=vincent Y=marsellus ?- jealous(X,Y). ?- loves(_G7,mia). _G5 =vincent _G6 =mia ?- loves(_G5,_G6), loves(_G7,_G6). X=_G5Y=_G7 _G7=vincent _G7=marsellus

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). X=vincent Y=vincent; X=vincent Y=marsellus; ?- loves(_G7,mia). _G5=marsellus _G6=mia _G7=vincent _G7=marsellus ?- jealous(X,Y). ?- loves(_G7,mia). _G5 =vincent _G6 =mia ?- loves(_G5,_G6), loves(_G7,_G6). X=_G5Y=_G7

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). …. X=vincent Y=marsellus; X=marsellus Y=vincent _G7=vincent ?- loves(_G7,mia). _G5=marsellus _G6=mia _G7=vincent _G7=marsellus ?- jealous(X,Y). ?- loves(_G7,mia). _G5 =vincent _G6 =mia ?- loves(_G5,_G6), loves(_G7,_G6). X=_G5Y=_G7

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). …. X=marsellus Y=vincent; X=marsellus Y=marsellus _G7=marsellus _G7=vincent ?- loves(_G7,mia). _G5=marsellus _G6=mia _G7=vincent _G7=marsellus ?- jealous(X,Y). ?- loves(_G7,mia). _G5 =vincent _G6 =mia ?- loves(_G5,_G6), loves(_G7,_G6). X=_G5Y=_G7

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). …. X=marsellus Y=vincent; X=marsellus Y=marsellus; no _G7=marsellus _G7=vincent ?- loves(_G7,mia). _G5=marsellus _G6=mia _G7=vincent _G7=marsellus ?- jealous(X,Y). ?- loves(_G7,mia). _G5 =vincent _G6 =mia ?- loves(_G5,_G6), loves(_G7,_G6). X=_G5Y=_G7

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Summary of this lecture Introduced the following key notions and their relation to Prolog: –logic programming –facts, rules and queries (horn clauses) –unification –(most general) unifier (mgu) –backtracking, depth-first search, forward & backward chaining

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Tips Practical Session add comments to your code!! think before you ask the Prolog interpreter! remember to add a dot (‘.’) after each clause (fact or rule) you need to create and load a knowledge base (Prolog program) before you can query Prolog if you change the program, you need to reload it in Prolog. Use the command ‘make.’ for this. Prolog gives warning if clauses for a single predicate are not grouped in the program. Best practice: group clauses defining the same predicate.

Organization Reading: LPN Ch. 1-2 Tomorrow practical session - work on LPN Sect. 1.4, LPN Sect. 1.3, LPN Sect. 2.3, 2.4 Next lecture –Recursion –Lists –Arithmetic