Logic Programming Lecture 3: Recursion, lists, and data structures.

Slides:



Advertisements
Similar presentations
Formal Models of Computation Part II The Logic Model
Advertisements

© 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.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
Logic Programming Lecture 3: Recursion, lists, and data structures.
Lecture 12: Revision Lecture Dr John Levine Algorithms and Complexity March 27th 2006.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Logic Programming Lecture 5: Nonlogical features, continued: negation-as-failure, collecting solutions, assert/retract.
Logic Programming Lecture 5: Nonlogical features, continued: negation-as-failure, collecting solutions, assert/retract.
Comp 307 Lecture 4:1 Prolog I, II Prolog was taught as a procedural language Control structures: if, while, recursion Data structures: structured terms,
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
Chapter 12 - Logic Programming
Rules Simple rules. Processing rules. Multiple sub-goals. PROLOG syntax. Recursive rules.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog (II) Lecture 22 – Prolog (II), Spring CSE3302 Programming Languages,
Chapter 8: The Logical Paradigm Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Lecture 8 Recursively enumerable (r.e.) languages
Resolution Refutation Formal Aspects of Computer Science - Week 10 An Automated Theorem Prover Lee McCluskey, room 2/07
LING 388: Language and Computers Sandiway Fong Lecture 6: 9/7.
Data Flow Analysis Compiler Design Nov. 3, 2005.
LING 388: Language and Computers Sandiway Fong Lecture 4: 8/31.
Arithmetic Expression Consider the expression arithmetic expression: (a – b) + ((c + d) + (e * f)) that can be represented as the following tree.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Trees Weiss sec (preview) ch. 18 (sort of).
Logic Programming James Cheney CS 411. Functional Programming Programs as functions Write down function you want to calculate Computer evaluates to a.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Logic Programming Lecture 7: Search Strategies: Problem representations Depth-first, breadth-first, and AND/OR search.
CS 321 Programming Languages and Compilers Prolog part 2.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
A Summary of XISS and Index Fabric Ho Wai Shing. Contents Definition of Terms XISS (Li and Moon, VLDB2001) Numbering Scheme Indices Stored Join Algorithms.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Control Algorithms 1 Chapter 6 Control Algorithms 1 Chapter 6 Pattern Search.
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.
Computer Science CPSC 322 Lecture 9 (Ch , 3.7.6) Slide 1.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
Review: computing list results Many programs require list results to be computed, built and returned Many programs require list results to be computed,
Prolog Program Style (ch. 8) Many style issues are applicable to any program in any language. Many style issues are applicable to any program in any language.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
First-Order Logic and Inductive Logic Programming.
Logic Programming Lecture 5: Nonlogical features, continued: negation-as-failure, assert/retract, collecting solutions.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
COMP 103 Traversing Trees. 2 RECAP-TODAY RECAP  Building and Traversing Trees TODAY  Traversing Trees  Chapter 16, especially 16.2.
© Kenneth C. Louden, Chapter 12 - Logic Programming Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Programming Languages Third Edition Chapter 4 Logic Programming.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Lecture course: Heuristics & Prolog for Artificial Intelligence Applications Notes on Prolog from Professor Bruce Batchelor, DSc.
C. Varela1 Logic Programming (PLP 11, CTM 9.1) Terms, Resolution, Unification, Search, Backtracking (Prolog) Relational Computation Model (Oz) Carlos Varela.
For Monday Exam 1 is Monday Takehome due Prolog Handout 3 due.
Example: Expressions Python Programming, 2/e 1 [+, [*, 3, 5], [*, 2, [-, 6, 1]]]
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
CS CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 13 October 4, 2001 Fall Semester.
1 Artificial Intelligence CS370D Prolog programming List operations.
For Friday No reading Prolog Handout 2. Homework.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
C H A P T E R N I N E Logic Programming Part 2 Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Logic Programming Lecture 7: Search Strategies:
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Best-first search is a search algorithm which explores a graph by expanding the most promising node chosen according to a specified rule.
Logic Programming Lecture 2: Unification and proof search.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
First-Order Logic and Inductive Logic Programming
Chapter 12 :: Logic Languages
Programming Paradigms and Languages
CS 261 – Data Structures Trees.
CSE 3302 Programming Languages
Presentation transcript:

Logic Programming Lecture 3: Recursion, lists, and data structures

Outline for today Recursion proof search behavior practical concerns List processing Programming with terms as data structures

Recursion So far we've (mostly) used nonrecursive rules These are limited: not Turing-complete can't define transitive closure e.g. ancestor

Recursion (1) Nothing to it? ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y). Just use recursively defined predicate as a goal. Easy, right?

Depth first search, revisited Prolog tries rules depth-first in program order no matter what Even if there is an "obvious" solution using later clauses! p :- p. p. will always loop on first rule.

Recursion (2) Rule order can matter. ancestor2(X,Y) :- parent(X,Z), ancestor2(Z,Y). ancestor2(X,Y) :- parent(X,Y). This may be less efficient (tries to find longest path first) This may also loop unnecessarily (if parent were cyclic). Heuristic: write base case rules first.

Rule order matters ancestor(a,b) parent(a,Z),ancestor(Z,b) Z = b ancestor(b,b) parent(a,b). parent(b,c). parent(a,b) done

Rule order matters ancestor(a,b) parent(a,Z),ancestor(Z,b) Z = b ancestor(b,b) parent(a,b). parent(b,a). parent(b,W),ancestor(W,b) W = a ancestor(a,b)...

Recursion (3) Goal order can matter. ancestor3(X,Y) :- parent(X,Y). ancestor3(X,Y) :- ancestor3(Z,Y), parent(X,Z). This will list all solutions, then loop.

Goal order matters ancestor(a,b) ancestor(Z,b),parent(a,Z) Z = a ancestor(W,b), parent(Z,W), parent(a,Z) parent(a,b). parent(b,c). parent(a,b) done parent(Z,b), parent(a,Z) parent(a,a) parent(a,a)...

Recursion (4) Goal order can matter. ancestor4(X,Y) :- ancestor4(Z,Y), parent(X,Z). ancestor4(X,Y) :- parent(X,Y). This will always loop! Heuristic: try non-recursive goals first.

Goal order matters ancestor(X,Y) ancestor(X,Z), parent(Z,Y) ancestor(X,W), parent(W,Z), parent(Z,Y) ancestor(X,V), parent(V,W), parent(W,Z), parent(Z,Y)... ancestor(X,U), parent(U,V), parent(V,W), parent(W,Z), parent(Z,Y)

Recursion and terms Terms can be arbitrarily nested Example: unary natural numbers nat(z). nat(s(N)) :- nat(N). To do interesting things we need recursion

Addition and subtraction Example: addition add(z,N,N). add(s(N),M,s(P)) :- add(N,M,P). Can run in reverse to find all M,N with M+N=P Can use to define leq leq(M,N) :- add(M,_,N).

Multiplication Can multiply two numbers: multiply(z,N,z). multiply(s(N),M,P) :- multiply(N,M,Q), add(M,Q,P). square(M) :- multiply(N,N,M).

List processing Recall built-in list syntax list([]). list([X|L]) :- list(L). Example: list append append([],L,L). append([X|L],M,[X|N]) :- append(L,M,N).

Append in action Forward direction ?- append([1,2],[3,4],X). Backward direction ?- append(X,Y,[1,2,3,4]).

Mode annotations Notation append(+,+,-) "if you call append with first two arguments ground then it will make the third argument ground" Similarly, append(-,-,+) "if you call append with last argument ground then it will make the first two arguments ground" Not "code", but often used in documentation "?" annotation means either + or -

List processing (2) When is something a member of a list? mem(X,[X|_]). mem(X,[_|L]) :- mem(X,L). Typical modes mem(+,+) mem(-,+)

List processing(3) Removing an element of a list remove(X,[X|L],L). remove(X,[Y|L],[Y|M]) :- remove(X,L,M). Typical mode remove(+,+,-)

List processing (4) Zip, or "pairing" corresponding elements of two lists zip([],[],[]). zip([X|L],[Y|M],[(X,Y)|N]) :- zip(L,M,N). Typical modes: zip(+,+,-). zip(-,-,+). % "unzip"

List flattening Write flatten predicate flatten/2 Given a list of (lists of..) lists Produces a list containing all elements in order

List flattening Write flatten predicate flatten/2 Given a list of (lists of..) lists Produces a list containing all elements in order flatten([],[]). flatten([X|L],M) :- flatten(X,Y1), flatten(L,Y2), append(Y1,Y2,M). flatten(X,[X]) :- ???. We can't fill this in yet! (more next week)

Records/structs We can use terms to define data structures pb([entry(james,' '),...]) and operations on them pb_lookup(pb(B),P,N) :- member(entry(P,N),B). pb_insert(pb(B),P,N,pb([entry(P,N)|B])). pb_remove(pb(B),P,pb(B2)) :- remove(entry(P,_),B,B2).

Trees We can define (binary) trees with data: tree(leaf). tree(node(X,T,U)) :- tree(T), tree(U).

Tree membership Define membership in tree mem_tree(X,node(X,T,U)). mem_tree(X,node(Y,T,U)) :- mem_tree(X,T) ; mem_tree(X,U).

Preorder traversal Define preorder preorder(leaf,[]). preorder(node(X,T,U),[X|N]) :- preorder(T,L), preorder(U,M), append(L,M,N). What happens if we run this in reverse?

Next time Nonlogical features Expression evaluation I/O "Cut" (pruning proof search) Further reading: Learn Prolog Now, ch. 3-4