Prolog Recursion Pepper Major portions credited to :

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
Higher Order Predicates Higher order predicates in PROLOG. Higher order declarative predicates : – findall – bagof – setof – =.. Higher order non-declarative.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
Prolog: List © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
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 3: Recursion Theory –Introduce recursive definitions in Prolog –Four examples –Show that there.
© 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.
Prolog: Unification & Recursion © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Warm Up: Slope Intercept Form from a Table.
1 Fall 2009ACS-1903 The break And continue Statements a break statement can be used to abnormally terminate a loop. use of the break statement in loops.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
© 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 5: Arithmetic Theory –Introduce Prolog`s built-in abilities for performing arithmetic –Apply.
© 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 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.
1 Fall 2008ACS-1903 for Loop Reading files String conversions Random class.
© 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 Horn clauses A literal is an atomic formula or its negation A clause is a disjunction of literals.
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
LING 388: Language and Computers Sandiway Fong Lecture 5.
ITEC 380 Organization of programming languages Lecture 9 – Prolog.
Discrete Structures Lecture 12: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
SOLVING ONE STEP EQUATIONS. THINK – PAIR - SHARE Think: What does the word inverse mean? Pair Share.
Adding and Subtracting Decimals. Rules for adding or subtracting decimals: (1) Line up the numbers by the decimal point.
Review: computing list results Many programs require list results to be computed, built and returned Many programs require list results to be computed,
Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.
© 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.
CS 103 Discrete Structures Lecture 13 Induction and Recursion (1)
Solve equations using addition and subtraction.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog Lecture 21 – Prolog, Spring CSE3302 Programming Languages, UT-Arlington.
Solving Systems of Equations By Elimination. Warm – up!! *As you walk in, please pick up your calculator!!* Use substitution to solve the following systems.
Year 9 Proof Dr J Frost Last modified: 19 th February 2015 Objectives: Understand what is meant by a proof, and examples.
Artificial Intelligence CS370D
CS 554: Knowledge base systems Part-4: Prolog- 2 By Dr. Syed Noman Hasany.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
1 TP #2: How does Prolog answer questions? n Miscellaneous info; n Last TP exercises solved; n How does Prolog answer questions? n Recursive Prolog programs;
Solving two step Inequalities < < < > < > <
Hex: a Game of Connecting Faces. Player 1 Player 2 Players take turns placing blue chips (player 1) and red chips (player 2). Player 1 plays first. Player.
Data Structure Interview Question and Answers
Unification, Recursion and Lists
Chapter 11 :: Logic Languages
Lists Prolog Pepper Credit to : Blackburn, Patrick, Johan Bos and Kristina Striegnitz. Learn Prolog Now. London: College Publications, (ISBN )
Before: September 6, 2017 Paul has two dozen Halloween cookies. He will eat three cookies on Sunday. On every day that follows, he will eat a number of.
1.7 Represent Graphs as Functions
Warm up Paul has two dozen cookies. He will eat three cookies on Sunday. On every day that follows, he will eat a number of cookies that is one greater.
Add Subtract Positive Negative integers
Prolog: cut & fail © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Prolog Search Strategy
Does graph represent a function? State the domain & range.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Chapter 12 :: Logic Languages
Chapter 12 :: Logic Languages
Introduction to Prolog
Lecture 12 CSE 331 Sep 22, 2014.
Involving One Operation
Lecture 7: Definite Clause Grammars
Objective: Write, Graph, and Solve Inequalities
One-step addition & subtraction equations: fractions & decimals
Chapter 12 :: Logic Languages
Prolog Based on: Chapter 12 of PLP “Seven languages in seven weeks”
Add and Subtract Positive & Negative Integers
Presentation transcript:

Prolog Recursion Pepper Major portions credited to : Blackburn, Patrick, Johan Bos and Kristina Striegnitz. Learn Prolog Now. London: College Publications, 2006. (ISBN 9781904987178) Also available at http://www.learnprolognow.org

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 You are digesting what you just ate You are digesting whatever you ate just ate And so on until you come to an animal that did not just eat something.

Use in a query just_ate(mosquito,blood(john)). just_ate(frog,mosquito). just_ate(stork,frog). just_ate(person,cow). just_ate(cow,frog). is_digesting(person,X)"? Please answer 'y' or 'n'? yes X = cow ; X = frog ; X = mosquito ; X = blood(john) ;

Construction is_digesting(X,Y) :- just_ate(X,Y). is_digesting(X,Y) :- just_ate(X,Z), is_digesting(Z,Y). Base case: Does not use its own predicate. Recursive rule: Handles one case, Recurses over the rest of the cases.

Proof Is_digesting(person,A). What just_ate fact has person on the left side, then the right side can be X. : look for just_ate(person,A). Look for just_ate(X,Z), is_digesting(Z,Y). if just_ate (person,_1) and (_1, A) so (person,cow) and (cow,frog) match so A = frog

Recursion of descendants  facts:  child(anne,bridget).    child(bridget,caroline).    child(caroline,donna).    child(donna,emily). Find descendants: - not far enough    descend(X,Y)  :-  child(X,Y).        descend(X,Y)  :-  child(X,Z),                                      child(Z,Y).

Recursion Analysis Base case: Recursive rule: Does not use its own predicate. If y is a child of x, y descends from x descend(X,Y)  :-  child(X,Y). Recursive rule: Handles one case, Recurses over the rest of the cases. If y is a child of a descendant of x, then y is a descendant of x descend(X,Y) :- child (I,Y), descend(X,I)

Exercise graph Connected points edge(a,b). edge(b, c). edge(b, d). edge(b, e). edge(d, e). edge(y,x). edge(x,z). What points can you reach from a particular point? Assumes no circular connections. connected(a,X). connected (X, z).

Accumulate text Without Accumulation: With Accumulation: Query: descend(X,Y)  :-  child(X,Y). descend(X,Y) :- child (I,Y), descend(X,I) With Accumulation: descend(X,Y, childrel(X,Y)):- child(X,Y). descend(X,Y,childrel(X,I,L)):- child(X,I), descend(I,Y,L). Query: descend2(anne,donna,X). X = parentChild(anne, bridget, parentChild(bridget, caroline, parentChild(caroline, donna))) ;

Exercise Walk the Graph Given a list of edges Show the path that connects from one edge to another connectedPath(a,e,X). Should give: X = go(a, b, go(b, e)) ; X = go(a, b, go(b, d, go(d, e))) ;

Adding Numbers adder(X, Y, Z):- Z is X + Y. 10 ?- adder(1,2,X). X = 3. ERROR: is/2: Arguments are not sufficiently instantiated

Random Numbers random/3 – given a range of numbers as the first 2 arguments, a random number starting from the first number but not including the last number. (for dice 1, 7) makeroll(X) :- random(1,7,X). adder(X,Y,Z):- Z is X + Y. throw2(Z) :- makeroll( X), makeroll( Y), Z is X + Y. You can add writeln(Z).

Count Recursive Attempts Need one variable to initialize to 0 when you start. When you recurse, add 1 to the variable you initialized to 0 When you are done, add 1 to the accumulated list. Example throwUntil_11(Count, FinalCount):- FinalCount is Count + 1, throw2(11). throwUntil_11(Count, FinalCount):- throw2(Y), Z is Count + 1, Y \= 11 throwUntil_11(Z, FinalCount). Example query: throwUntil_11(0,X).

Exercise Count Edge Walk Recursions Count the number of possible steps between two points. countConnections(a,e,0,X). X = 2 ; X = 3 ;

Recursion Summary How to declare a recursive rule How to pair the recursive rule with a base case How prolog proof leads to recursion No base case – endless recursion Random Counting recursive steps