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

Slides:



Advertisements
Similar presentations
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
Advertisements

Prolog Programming (Volume 5) Dr W.F. Clocksin. List cells + Unification = Great Idea Normally we use ‘proper lists’, which are defined according to the.
© 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.
PROBLEM SOLVING AND SEARCH
0 PROGRAMMING IN HASKELL Chapter 10 - Declaring Types and Classes.
String is a synonym for the type [Char].
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
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.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Logic Programming Lecture 3: Recursion, lists, and data structures.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees, Binary Trees, and Binary Search Trees COMP171.
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).
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
CS 321 Programming Languages and Compilers Prolog part 2.
Lecture 17 Non-Linear data structures Richard Gesick.
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.
1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Week 11 - Wednesday.  What did we talk about last time?  Graphs  Euler paths and tours.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
2-3 Tree. Slide 2 Outline  Balanced Search Trees 2-3 Trees Trees.
Trees, Binary Trees, and Binary Search Trees COMP171.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
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.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Computer Sciences Department1.  Property 1: each node can have up to two successor nodes (children)  The predecessor node of a node is called its.
Example: Expressions Python Programming, 2/e 1 [+, [*, 3, 5], [*, 2, [-, 6, 1]]]
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
Lecture 7: Searching a Tree Neil Ghani University of Strathclyde.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Logic Programming Lecture 7: Search Strategies:
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 8: Term manipulation & Meta-programming.
Logic Programming Lecture 2: Unification and proof search.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
String is a synonym for the type [Char].
Lecture 7 Queues Stacks Trees.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Week 6 - Wednesday CS221.
Problems with Linked List (as we’ve seen so far…)
Trees Lecture 12 CS2110 – Fall 2017.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Binary Trees, Binary Search Trees
PROGRAMMING IN HASKELL
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
PROGRAMMING IN HASKELL
Binary Trees, Binary Search Trees
Non-Linear data structures
Binary Trees, Binary Search Trees
8.2 Tree Traversals Chapter 8 - Trees.
Presentation transcript:

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

James CheneyLogic ProgrammingSeptember 25, 2014 Outline for today Recursion proof search behavior practical concerns List processing Programming with terms as data structures

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

James CheneyLogic ProgrammingSeptember 25, 2014 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?

James CheneyLogic ProgrammingSeptember 25, 2014 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.

James CheneyLogic ProgrammingSeptember 25, 2014 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.

James CheneyLogic ProgrammingSeptember 25, 2014 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

James CheneyLogic ProgrammingSeptember 25, 2014 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)...

James CheneyLogic ProgrammingSeptember 25, 2014 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.

James CheneyLogic ProgrammingSeptember 25, 2014 Goal order matters ancestor(a,b)(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)...

James CheneyLogic ProgrammingSeptember 25, 2014 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.

James CheneyLogic ProgrammingSeptember 25, 2014 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)

James CheneyLogic ProgrammingSeptember 25, 2014 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

James CheneyLogic ProgrammingSeptember 25, 2014 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).

James CheneyLogic ProgrammingSeptember 25, 2014 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).

James CheneyLogic ProgrammingSeptember 25, 2014 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).

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

James CheneyLogic ProgrammingSeptember 25, 2014 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 -

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

James CheneyLogic ProgrammingSeptember 25, 2014 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(+,+,-)

James CheneyLogic ProgrammingSeptember 25, 2014 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"

James CheneyLogic ProgrammingSeptember 25, 2014 List flattening Write flatten predicate flatten/2 Given a list of (lists of..) lists Produces a list containing all elements in order

James CheneyLogic ProgrammingSeptember 25, 2014 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)

James CheneyLogic ProgrammingSeptember 25, 2014 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).

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

James CheneyLogic ProgrammingSeptember 25, 2014 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).

James CheneyLogic ProgrammingSeptember 25, 2014 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?

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