Comp 205: Comparative Programming Languages Declarative Programming Languages Unification Backtracking Lecture notes, exercises, etc., can be found at:

Slides:



Advertisements
Similar presentations
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.
Advertisements

Some Prolog Prolog is a logic programming language
Chapter 11 :: Logic Languages
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
1 Logic Programming School of Informatics, University of Edinburgh Transformations Specification-Program An introduction to moving between Prolog and First.
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).
Comp 307 Lecture 4:1 Prolog I, II Prolog was taught as a procedural language Control structures: if, while, recursion Data structures: structured terms,
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
Chapter 12 - Logic Programming
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/
Comp 205: Comparative Programming Languages Declarative Programming Languages Logic Programming Horn-Clause Logic Prolog Lecture notes, exercises, etc.,
School of Computing and Mathematics, University of Huddersfield Computing Science: WEEK 18 LECTURE: Comparative Programming Languages + Prolog PRACTICALS:
Discussion #20 1/11 Discussion #20 Resolution in Datalog.
CHA2555 Week2: Knowledge Representation, Logic and Prolog Lee McCluskey First term:
CSE (c) S. Tanimoto, 2005 Logic Programming 1 Logic Programming Outline: Motivation Examples: The Grandmother relation Formulation in Prolog Logic,
ISBN Chapter 16 Logic Programming Languages.
Logic Programming Languages
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
Chapter 16 Logic Programming Languages. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 16 Topics Introduction A Brief Introduction to.
1-1 Introduction Logic programming languages, sometimes called declarative programming languages Express programs in a form of symbolic logic Use a logical.
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to.
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.
F28PL1 Programming Languages Lecture 16: Prolog 1.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
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.
CSE S. Tanimoto Horn Clauses and Unification 1 Horn Clauses and Unification Propositional Logic Clauses Resolution Predicate Logic Horn Clauses.
CIS2326 Week2: Logic and Prolog Lee McCluskey First term:
Programming Languages Tucker and Noonan – 2e Chapter 15 – Part 1 Logic Programming “Q: How many legs does a dog have if you call its tail a leg? A: Four.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
Lecture 9b: Prolog Heshaam Faili University of Tehran The language of logic Concepts Examples.
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
CS 363 Comparative Programming Languages Logic Programming Languages.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 25 A First Course in Database Systems.
For Monday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
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.
CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)
© Kenneth C. Louden, Chapter 12 - Logic Programming Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Propositional Logic Predicate Logic
Logic Programming Dr. Yasser Ahmed Nada Fall 2010/2011 Lecture 2 1 Logic Programming.
CHA2555 Week2 Practical: Lee McCluskey First term:
CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog Lecture 21 – Prolog, Spring CSE3302 Programming Languages, UT-Arlington.
1 Lecture 6 Logic Programming rule-based programming with Prolog Ras Bodik with Mangpo, Ali Hack Your Language! CS164: Introduction to Programming Languages.
C. Varela1 Logic Programming (PLP 11, CTM 9.1) Terms, Resolution, Unification, Search, Backtracking (Prolog) Relational Computation Model (Oz) Carlos Varela.
First-Order Logic Reading: C. 8 and C. 9 Pente specifications handed back at end of class.
For Friday No reading Prolog Handout 2. Homework.
For Wednesday No reading Prolog handout 2 Chapter 9, exercise 4.
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.
The portion of a Prolog interpreter that executes queries (goals) is known as the inference engine. An inference engine is a kind of theorem prover, using.
Prolog a declarative language
Carlos Varela Rennselaer Polytechnic Institute November 11, 2016
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Horn Clauses and Unification
Carlos Varela Rensselaer Polytechnic Institute November 14, 2017
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Horn Clauses and Unification
Horn Clauses and Unification
Chapter 12 :: Logic Languages
Programming Paradigms and Languages
Chapter 2: Prolog (Introduction and Basic Concepts)
Presentation transcript:

Comp 205: Comparative Programming Languages Declarative Programming Languages Unification Backtracking Lecture notes, exercises, etc., can be found at:

Substitutions While solving a query, the Prolog interpreter maintains a list of goals and attempts to unify one of its goals with the head of a Horn clause in the database. Unification involves finding substitutions for variables in the goal, and substitutions for variables in the head that make the goal equal to the head.

Example Suppose Lucy only knows fellow members of her yachting club, and Suzy is a member of all exclusive clubs, and that Lucy's yachting club is exclusive. knows(lucy,X) :- member(X,yachtClub). member(suzy,Y) :- exclusive(Y). exclusive(yachtClub).

Example The query "?- knows(lucy,Z)" unifies with the head of knows(lucy,X) :- member(X,yachtClub). by substituting the variable Z for X in the head, giving the new goal member(Z,yachtClub).

Example The goal member(Z,yachtClub) unifies with the head of member(suzy,Y) :- exclusive(Y). by substituting suzy for Z in the goal, and substituting yachtClub for Y in the head, giving the new goal exclusive(yachtClub). which is asserted as a fact in the database, giving the final solution Z=suzy.

More Variables Variables that occur in conditions but not in the head of a Horn clause have an existential force: grandfather(X,Y) :- father(X,Z), parent(Z,Y). This is most naturally read as "X is the grandfather of Y if there is some Z such that X is the father of Z and Z is a parent of Y".

A Little Predicate Calculus In general, variables are universally quantified in Horn clauses. The existential force in the "Grandfather" example comes from the following property of the predicate calculus: (  x,y,z. P(x,z) and F(z,y)  G(x,y)) is equivalent to (  x,y. (  z. P(x,z) and F(z,y))  G(x,y))

More Clauses When a relation is defined by more than one clause, the effect is to include an "or" in the definition: parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). This is most naturally read as "X is the parent of Y if X is the father of Y or X is the mother of Y".

Proof Search Consider the following Prolog database: grandfather(X,Y) :- father(X,Z), parent(Z,Y). parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). father(ted,mary). father(ted,jack). mother(mary,ian). father(jack,lucy). and the query ?-grandfather(ted,Y)

Selecting Clauses The Prolog interpreter attempts to match the first goal in its list (in this case grandfather(ted,Y) ) with the first clause in the database: grandfather(X,Y) :- father(X,Z), parent(Z,Y). Unification succeeds, give the goal-list father(ted,Z), parent(Z,Y)

Selecting Clauses The first clause in the database that matches with father(ted,Z) is: father(ted,mary). giving the goal-list parent(mary,Y). The first clause that matches with this is: parent(X,Y) :- father(X,Y). giving the goal-list father(mary,Y).

Backtracking The goal father(mary,Y) fails (it fails to match with any clause in the database). At this point, the Prolog interpreter backtracks to the last successful match, reinstates the goal-list to its previous state and looks for the next match: parent(X,Y) :- mother(X,Y). giving the goal-list mother(mary,Y).

Backtracking The Prolog interpreter always tries to unify the first goal in its list with the earliest possible Horn clause; when failure occurs, the interpreter backtracks to the last unification, marks that clause as unsuccessful proceeds to attempt unification with the following clauses This process is iterative, and is a form of depth-first search

Proof Search grandfather(X,Y) :- father(X,Z), parent(Z,Y). parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). father(ted,mary). father(ted,jack). mother(mary,ian). father(jack,lucy). Now consider ?- grandfather(X,lucy)...

Proof Search grandfather(X,Y) :- father(X,Z), parent(Z,Y). parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). father(ted,mary). father(ted,jack). mother(mary,ian). father(jack,lucy). goals: father(X,Z), parent(Z,lucy)

Proof Search grandfather(X,Y) :- father(X,Z), parent(Z,Y). parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). father(ted,mary). father(ted,jack). mother(mary,ian). father(jack,lucy). goals: parent(mary,lucy)

Proof Search grandfather(X,Y) :- father(X,Z), parent(Z,Y). parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). father(ted,mary). father(ted,jack). mother(mary,ian). father(jack,lucy). goals: father(mary,lucy)

Proof Search grandfather(X,Y) :- father(X,Z), parent(Z,Y). parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). father(ted,mary). father(ted,jack). mother(mary,ian). father(jack,lucy). goals: father(mary,lucy) FAIL

Proof Search grandfather(X,Y) :- father(X,Z), parent(Z,Y). parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). father(ted,mary). father(ted,jack). mother(mary,ian). father(jack,lucy). goals: mother(mary,lucy)

Proof Search grandfather(X,Y) :- father(X,Z), parent(Z,Y). parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). father(ted,mary). father(ted,jack). mother(mary,ian). father(jack,lucy). goals: mother(mary,lucy) FAIL

Proof Search grandfather(X,Y) :- father(X,Z), parent(Z,Y). parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). father(ted,mary). father(ted,jack). mother(mary,ian). father(jack,lucy). goals were: father(X,Z), parent(Z,lucy)

Proof Search grandfather(X,Y) :- father(X,Z), parent(Z,Y). parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). father(ted,mary). father(ted,jack). mother(mary,ian). father(jack,lucy). goals: parent(jack,lucy)

Proof Search grandfather(X,Y) :- father(X,Z), parent(Z,Y). parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). father(ted,mary). father(ted,jack). mother(mary,ian). father(jack,lucy). goals: father(jack,lucy)

Summary Unification Backtracking and depth-first search Next: Exams….