© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3: Recursion Theory –Introduce recursive definitions in Prolog –Four examples –Show that there.

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 6 – Arithmetic, fail and the cut.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
Computer Science CPSC 322 Lecture 25 Top Down Proof Procedure (Ch 5.2.2)
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 12: Working with Files This lecture is concerned with various aspects of file handling and.
Declarative Programming Lists in PROLOG Autumn 2014.
© 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.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
PROLOG Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Formal Logic Mathematical Structures for Computer Science Chapter 1 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesFormal Logic.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 11: Database Manipulation and Collecting Solutions Theory –Discuss database manipulation in.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 11: Database Manipulation and Collecting Solutions Exercises –Solutions to exercises LPN chapter.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3: Recursion Theory –Introduce recursive definitions in Prolog –Four examples –Show that there.
Prolog Recursion Pepper. A rule with that calls itself is_digesting(X,Y) :- just_ate(X,Y). is_digesting(X,Y) :- just_ate(X,Z), is_digesting(Z,Y). 2 rules.
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
Prolog: Unification & Recursion © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 8: More DCGs Theory –Examine two important capabilities offered by DCG notation: Extra arguments.
© 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 7: Definite Clause Grammars Theory –Introduce context free grammars and some related concepts.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 5: Arithmetic Theory –Introduce Prolog`s built-in abilities for performing arithmetic –Apply.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 7: Definite Clause Grammars Theory –Introduce context free grammars and some related concepts.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 4: Lists Theory –Introduce lists, an important recursive data structure often used in Prolog.
© 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.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 5: Arithmetic Theory –Introduce Prolog`s built-in abilities for performing arithmetic –Apply.
Formal Aspects of Computer Science – Week 12 RECAP Lee McCluskey, room 2/07
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 4: Lists Theory –Introduce lists, an important recursive data structure often used in Prolog.
LISP and PROLOG AI Programming Language Submitted To: Dr. Hesham El-Zoka Submitted By: Eng. Ismail Fathalla El-Gayar.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Horn clauses A literal is an atomic formula or its negation A clause is a disjunction of literals.
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 5.
LING 388: Language and Computers Sandiway Fong Lecture 4.
LING 388: Language and Computers Sandiway Fong Lecture 3.
ITEC 380 Organization of programming languages Lecture 9 – Prolog.
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3 “Input & Output”, Negation, Search.
LING 388: Language and Computers Sandiway Fong Lecture 6.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
CT214 – Logical Foundations of Computing Lecture 8 Introduction to Prolog.
Chapter Three: Lists, Operators, Arithmetic 1. © Patrick Blackburn, Johan Bos & Kristina Striegnitz Important things about lists  List elements are enclosed.
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.
Operators in Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Introduction to Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog Lecture 21 – Prolog, Spring CSE3302 Programming Languages, UT-Arlington.
LING/C SC/PSYC 438/538 Lecture 15 Sandiway Fong. Did you install SWI Prolog?
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.
For Friday No reading Prolog Handout 2. Homework.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Kansas State University Department of Computing and Information Sciences CIS 730: Introduction to Artificial Intelligence Lecture 14 of 42 Wednesday, 22.
Done By :- -Nesreen Basem -Sara nabil -Rawan Prolog Program.
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.
Unification, Recursion and Lists
Prolog Recursion Pepper Major portions credited to :
Chapter 11 :: Logic Languages
Prolog: cut & fail © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Prolog Search Strategy
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Chapter 12 :: Logic Languages
Introduction to Prolog
Lecture 7: Definite Clause Grammars
Representations & Reasoning Systems (RRS) (2.2)
Chapter 12 :: Logic Languages
Prolog Based on: Chapter 12 of PLP “Seven languages in seven weeks”
Presentation transcript:

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3: Recursion Theory –Introduce recursive definitions in Prolog –Four examples –Show that there can be mismatches between the declarative and procedural meaning of a Prolog program Exercises –Exercises of LPN chapter 3 –Practical work

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Recursive Definitions Prolog predicates can be defined recursively A predicate is recursively defined if one or more rules in its definition refers to itself

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 1: Eating isDigesting(X,Y):- justAte(X,Y). isDigesting(X,Y):- justAte(X,Z), isDigesting(Z,Y). justAte(mosquito,blood(john)). justAte(frog,mosquito). justAte(stork,frog). ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Picture of the situation X Y justAte isDigesting

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Picture of the situation X Y justAte isDigesting X Z justAte isDigesting Y

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 1: Eating isDigesting(X,Y):- justAte(X,Y). isDigesting(X,Y):- justAte(X,Z), isDigesting(Z,Y). justAte(mosquito,blood(john)). justAte(frog,mosquito). justAte(stork,frog). ?- isDigesting(stork,mosquito).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Another recursive definition p:- p. ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Another recursive definition p:- p. ?- p.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Another recursive definition p:- p. ?- p. ERROR: out of memory

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 2: Decendant child(bridget,caroline). child(caroline,donna). descend(X,Y):- child(X,Y). descend(X,Y):- child(X,Z), child(Z,Y).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 2: Decendant child(anna,bridget). child(bridget,caroline). child(caroline,donna). child(donna,emily). descend(X,Y):- child(X,Y). descend(X,Y):- child(X,Z), child(Z,Y).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 2: Decendant child(anna,bridget). child(bridget,caroline). child(caroline,donna). child(donna,emily). descend(X,Y):- child(X,Y). descend(X,Y):- child(X,Z), child(Z,Y). ?- descend(anna,donna). no ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 2: Decendant child(anna,bridget). child(bridget,caroline). child(caroline,donna). child(donna,emily). descend(X,Y):- child(X,Y). descend(X,Y):- child(X,Z), child(Z,Y). descend(X,Y):- child(X,Z), child(Z,U), child(U,Y). ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 2: Decendant child(anna,bridget). child(bridget,caroline). child(caroline,donna). child(donna,emily). descend(X,Y):- child(X,Y). descend(X,Y):- child(X,Z), descend(Z,Y). ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 2: Decendant child(anna,bridget). child(bridget,caroline). child(caroline,donna). child(donna,emily). descend(X,Y):- child(X,Y). descend(X,Y):- child(X,Z), descend(Z,Y). ?- descend(anna,donna).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Search tree Draw search tree for ?- descend(anna,donna).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 3: Successor Suppose we use the following way to write numerals: 1. 0 is a numeral. 2. If X is a numeral, then so is succ(X).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 3: Successor numeral(0). numeral(succ(X)):- numeral(X).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 3: Successor numeral(0). numeral(succ(X)):- numeral(X). ?- numeral(succ(succ(succ(0)))). yes ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 3: Successor numeral(0). numeral(succ(X)):- numeral(X). ?- numeral(X).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 3: Successor numeral(0). numeral(succ(X)):- numeral(X). ?- numeral(X). X=0; X=succ(0); X=succ(succ(0)); X=succ(succ(succ(0))); X=succ(succ(succ(succ(0))))

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 4: Addition ?- add(succ(succ(0)),succ(succ(succ(0))), Result). Result=succ(succ(succ(succ(succ(0))))) yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 4: Addition add(0,X,X). %% base clause ?- add(succ(succ(0)),succ(succ(succ(0))), Result). Result=succ(succ(succ(succ(succ(0))))) yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 4: Addition add(0,X,X). %% base clause add(succ(X),Y,succ(Z)):- %% recursive clause add(X,Y,Z). ?- add(succ(succ(0)),succ(succ(succ(0))), Result). Result=succ(succ(succ(succ(succ(0))))) yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Search tree Draw search tree

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Exercises

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Prolog and Logic Prolog was the first reasonable attempt to create a logic programming language –Programmer gives a declarative specification of the problem, using the language of logic –The programmer should not have to tell the computer what to do –To get information, the programmer simply asks a query

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Prolog and Logic Prolog does some important steps in this direction, but nevertheless, Prolog is not a full logic programming language! Prolog has a specific way of answering queries: –Search knowledge base from top to bottom –Processes clauses from left to right –Backtracking to recover from bad choices

© Patrick Blackburn, Johan Bos & Kristina Striegnitz descend1.pl child(anna,bridget). child(bridget,caroline). child(caroline,donna). child(donna,emily). descend(X,Y):- child(X,Y). descend(X,Y):- child(X,Z), descend(Z,Y). ?- descend(A,B). A=anna B=bridget

© Patrick Blackburn, Johan Bos & Kristina Striegnitz descend2.pl child(anna,bridget). child(bridget,caroline). child(caroline,donna). child(donna,emily). descend(X,Y):- child(X,Z), descend(Z,Y). descend(X,Y):- child(X,Y). ?- descend(A,B). A=anna B=emily

© Patrick Blackburn, Johan Bos & Kristina Striegnitz descend3.pl child(anna,bridget). child(bridget,caroline). child(caroline,donna). child(donna,emily). descend(X,Y):- descend(Z,Y), child(X,Z). descend(X,Y):- child(X,Y). ?- descend(A,B). ERROR: OUT OF LOCAL STACK

© Patrick Blackburn, Johan Bos & Kristina Striegnitz descend4.pl child(anna,bridget). child(bridget,caroline). child(caroline,donna). child(donna,emily). descend(X,Y):- child(X,Y). descend(X,Y):- descend(Z,Y), child(X,Z). ?- descend(A,B).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Summary of this lecture In this lecture we introduced recursive predicates We also looked at the differences between the declarative and the procedural meaning of Prolog programs We have identified some of the shortcomings of Prolog seen as a logical programming language

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Next lecture Introduce lists in Prolog –Important recursive data structure in Prolog programming –Define the member/2 predicate, a fundamental Prolog tool for working with lists –Discuss the idea of recursing down lists