Runtime Analysis and Program Transformations for Dynamic Programs John Blatz CS 325/425 April 26, 2006.

Slides:



Advertisements
Similar presentations
Parsing V: Bottom-up Parsing
Advertisements

Boosting Textual Compression in Optimal Linear Time.
1 Turing Machines and Equivalent Models Section 13.2 The Church-Turing Thesis.
Dynamic Programming Nithya Tarek. Dynamic Programming Dynamic programming solves problems by combining the solutions to sub problems. Paradigms: Divide.
0 PROGRAMMING IN HASKELL Chapter 10 - Declaring Types and Classes.
The Big Picture Chapter 3. We want to examine a given computational problem and see how difficult it is. Then we need to compare problems Problems appear.
May 2006CLINT-LN Parsing1 Computational Linguistics Introduction Approaches to Parsing.
GRAMMAR & PARSING (Syntactic Analysis) NLP- WEEK 4.
CKY Parsing Ling 571 Deep Processing Techniques for NLP January 12, 2011.
CPSC 311, Fall 2009: Dynamic Programming 1 CPSC 311 Analysis of Algorithms Dynamic Programming Prof. Jennifer Welch Fall 2009.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Aho-Corasick String Matching An Efficient String Matching.
CPSC 411 Design and Analysis of Algorithms Set 5: Dynamic Programming Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 5 1.
Syntactic Parsing with CFGs CMSC 723: Computational Linguistics I ― Session #7 Jimmy Lin The iSchool University of Maryland Wednesday, October 14, 2009.
Concept of Basic Time Complexity Problem size (Input size) Time complexity analysis.
Improving Code Generation Honors Compilers April 16 th 2002.
Parsing SLP Chapter 13. 7/2/2015 Speech and Language Processing - Jurafsky and Martin 2 Outline  Parsing with CFGs  Bottom-up, top-down  CKY parsing.
Induction and recursion
Query Processing Presented by Aung S. Win.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Intro to NLP - J. Eisner1 Earley’s Algorithm (1970) Nice combo of our parsing ideas so far:  no restrictions on the form of the grammar:  A.
MATH 224 – Discrete Mathematics
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Agenda Review: –Planar Graphs Lecture Content:  Concepts of Trees  Spanning Trees  Binary Trees Exercise.
Sentence Parsing Parsing 3 Dynamic Programming. Jan 2009 Speech and Language Processing - Jurafsky and Martin 2 Acknowledgement  Lecture based on  Jurafsky.
Automated Reasoning Early AI explored how to automated several reasoning tasks – these were solved by what we might call weak problem solving methods as.
Automated Reasoning Early AI explored how to automate several reasoning tasks – these were solved by what we might call weak problem solving methods as.
CS 4705 Lecture 10 The Earley Algorithm. Review Top-Down vs. Bottom-Up Parsers –Both generate too many useless trees –Combine the two to avoid over-generation:
Query Processing – Query Trees. Evaluation of SQL Conceptual order of evaluation – Cartesian product of all tables in from clause – Rows not satisfying.
Program Transformation for Optimization of Parsing Algorithms and other Weighted Logic Programs Jason Eisner and John Blatz 29 July 2006.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Computer Sciences Department1.  Property 1: each node can have up to two successor nodes (children)  The predecessor node of a node is called its.
How To Program An Overview Or A Reframing of the Question of Programming.
System To Generate Test Data: The Analysis Program Syed Nabeel.
PARSING David Kauchak CS159 – Fall Admin Assignment 3 Quiz #1  High: 36  Average: 33 (92%)  Median: 33.5 (93%)
Ch03-Algorithms 1. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Review 1.Structure of the course Lexical Analysis Syntax Analysis Grammar & Language RG & DFA Top-down LL(1) Parsing Bottom-Up LR Layered Automation Semantic.
CSC 594 Topics in AI – Natural Language Processing
User-Written Functions
Statistical NLP Winter 2009
CS 3304 Comparative Languages
CS 326 Programming Languages, Concepts and Implementation
Instructor: Laura Kallmeyer
Algorithmic complexity: Speed of algorithms
Context-Free Grammars: an overview
Programming Languages Translator
GC211Data Structure Lecture2 Sara Alhajjam.
Table-driven parsing Parsing performed by a finite state machine.
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Simplifications of Context-Free Grammars
CSCI 5832 Natural Language Processing
CSCI1600: Embedded and Real Time Software
Unit# 9: Computer Program Development
Dynamic Programming.
Earley’s Algorithm (1970) Nice combo of our parsing ideas so far:
Parsing and More Parsing
Lecture 7: Introduction to Parsing (Syntax Analysis)
Algorithmic complexity: Speed of algorithms
Algorithmic complexity: Speed of algorithms
David Kauchak CS159 – Spring 2019
CSCI1600: Embedded and Real Time Software
Discrete Mathematics CS 2610
Normal Forms for Context-free Grammars
Presentation transcript:

Runtime Analysis and Program Transformations for Dynamic Programs John Blatz CS 325/425 April 26, 2006

/425 Declarative Methods – J. Eisner/J. Blatz Matrix multiplication & computational complexity for I in 1:n  for J in 1:n c(I,J) = 0 for K in 1:n  c(I,J) += a(I,K) * b(K,J) row I column J abc x = c(I, J) O(n 3 )

/425 Declarative Methods – J. Eisner/J. Blatz Matrix multiplication & computational complexity  c(I,J) += a(I,K) * b(K,J) row I column J abc x = c(I, J) O(n 3 ) (declaratively) equivalent Dyna program:

/425 Declarative Methods – J. Eisner/J. Blatz Example: context-free parsing k grammar symbols (X, Y, Z) n words in sentence (I, J, K) O(k 3 n 3 ) Actually just an upper bound! (why?) goal += constit(s,0,N) * end(N). constit(X,I,J) += rewrite(X,W) * word(W,I,J). constit(X,I,K) += rewrite(X,Y,Z) * constit(Y,I,J) * constit(Z,J,K).

/425 Declarative Methods – J. Eisner/J. Blatz Sparsity Runtime of a dynamic rule = total number of ways to instantiate it Sparse computations much faster Example: multiplication of diagonal matrices  Only a and b items that exist are of the form a(I, I) or b(I, I)  Asymptotic runtime = O(n) instead of O(n 3 ) c(I,I) += a(I,I) * b(I,I)c(I,J) += a(I,K) * b(K,J) unification

/425 Declarative Methods – J. Eisner/J. Blatz Building a declarative house Declarative specification Procedural instructions Programmer Solver Output

/425 Declarative Methods – J. Eisner/J. Blatz Building a declarative house Declarative specification Procedural instructions Programmer Solver Output EndTime #= max(EndTimes), minimize(labeling(AllVars), EndTime). ???

/425 Declarative Methods – J. Eisner/J. Blatz Semi-declarative programming How can we get the solver to be more efficient?  Tell it how to solve the problem: minimize(search(AllVars, 0, smallest, indomain_min, complete, []), EndTime).  Explain the problem differently

/425 Declarative Methods – J. Eisner/J. Blatz Building a declarative house Declarative specification Procedural instructions Programmer Solver Output Better declarative specification Transformation

/425 Declarative Methods – J. Eisner/J. Blatz Program transformation examples above(X, Y) :- above(Underling, Y), boss(X, Underling). Prolog will recurse forever on this program “Transform” into equivalent program: above(X, Y) :- boss(X, Underling), above(Underling, Y).

/425 Declarative Methods – J. Eisner/J. Blatz Program transformation examples Fusing constraints makes arc consistency stronger X #\= Y, Y #\= Z, Z #\= X, fuse constraints alldifferent([X,Y,Z])

/425 Declarative Methods – J. Eisner/J. Blatz Program transformation examples Above example computes all possible trees, and so it will run forever Transform it to only consider trees that we are interested in rooted(t(R,[])) max= iq(R). unrooted(t(R,[])) max= zero whenever iq(R). zero := 0. any(T) max= rooted(T).any(T) max= unrooted(T). rooted(t(R,[X|Xs])) max= rooted(t(R,Xs)) + unrooted(X). unrooted(t(R,[X|Xs])) max= unrooted(t(R,Xs)) + any(X)

/425 Declarative Methods – J. Eisner/J. Blatz Program transformation examples rooted(t(R,[])) max= iq(R). unrooted(t(R,[])) max= zero whenever iq(R). zero := 0. any(T) max= rooted(T).any(T) max= unrooted(T). rooted(t(R,[X|Xs])) max= rooted(t(R,Xs)) + unrooted(X). whenever(interesting(t(R,[X|Xs])). unrooted(t(R,[X|Xs])) max= unrooted(t(R,Xs)) + any(X) whenever(interesting(t(R,[X|Xs])). interesting(X) max= input(X). interesting(X) max= interesting(t(R,[X|_])). interesting(t(R,Xs)) max= interesting(t(R,[_|Xs])). goal max= any(X) whenever input(X).

/425 Declarative Methods – J. Eisner/J. Blatz The folding/unfolding paradigm Small, basic steps which can be composed Has been applied to several declarative languages

/425 Declarative Methods – J. Eisner/J. Blatz Folding goal += constit(s,0,N) * end(N). constit(X,I,J) += word(W,I,J) * rewrite(X,W). constit(X,I,K) += constit(Y,I,J) * constit(Z,J,K) * rewrite(X,Y,Z). constit(X,I,K) word(W,I,K) constit(Z,J,K) rewrite(X,W) constit(Y,I,J) rewrite(X,Y,Z)

/425 Declarative Methods – J. Eisner/J. Blatz Folding goal += constit(s,0,N) * end(N). constit(X,I,J) += word(W,I,J) * rewrite(X,W). constit(X,I,K) += constit(Y,I,J) * constit(Z,J,K) * rewrite(X,Y,Z). constit(X,I,K) word(W,I,K) constit(Z,J,K) rewrite(X,W) constit(Y,I,J) rewrite(X,Y,Z)

/425 Declarative Methods – J. Eisner/J. Blatz Folding goal += constit(s,0,N) * end(N). constit(X,I,J) += word(W,I,J) * rewrite(X,W). constit(X,I,K) += constit(Y,I,J) * constit(Z,J,K) * rewrite(X,Y,Z). temp(X,Y,Z,J,K) = constit(Z,J,K) * rewrite(X,Y,Z). constit(X,I,K) word(W,I,K) constit(Z,J,K) rewrite(X,W) constit(Y,I,J) rewrite(X,Y,Z) temp(X,Y,Z,J,K)

/425 Declarative Methods – J. Eisner/J. Blatz Folding goal += constit(s,0,N) * end(N). constit(X,I,J) += word(W,I,J) * rewrite(X,W). constit(X,I,K) += constit(Y,I,J) * temp(X,Y,Z,J,K) temp(X,Y,Z,J,K) = constit(Z,J,K) * rewrite(X,Y,Z). constit(X,I,K) word(W,I,K) constit(Z,J,K) rewrite(X,W) constit(Y,I,J) rewrite(X,Y,Z) temp(X,Y,Z,I,J)temp(X,Y,Z,J,K)

/425 Declarative Methods – J. Eisner/J. Blatz Fully transformed version goal += constit(s,0,N) * end(N). constit(X,I,J) += word(W,I,J) * rewrite(X,W). constit(X,I,K) += constit(Y,I,J) * temp(X,Y,Z,J,K) temp(X,Y,Z,J,K) = constit(Z,J,K) * rewrite(X,Y,Z). Still O(k 3 n 3 ) in the worst-case But could actually be much faster—why?  Many constit(Z,J,K) items, few rewrite(X,Y,Z)  Avoids repeating work if temp is already built  Fails faster if agenda is poorly ordered  Could be followed by another transformation k grammar symbols (X,Y,Z) n positions in string (I,J,K)

/425 Declarative Methods – J. Eisner/J. Blatz Folding Folding can improve asymptotic runtime! constit(X,I,K) = constit(y1,I,j1)* constit(z1,j1,K) * rewrite(X,y1,z1) + constit(y1,I,j1)* constit(z2,j1,K) * rewrite(X,y1,z2) + constit(y2,I,j1)* constit(z1,j1,K) * rewrite(X,y2,z1) + constit(y2,I,j1)* constit(z2,j1,K) * rewrite(X,y2,z2) + constit(y1,I,j2)* constit(z1,j2,K) * rewrite(X,y1,z1) + constit(y1,I,j2)* constit(z2,j2,K) * rewrite(X,y1,z2) + constit(y2,I,j2)* constit(z1,j2,K) * rewrite(X,y2,z1) + constit(y2,I,j2)* constit(z2,j2,K) * rewrite(X,y2,z2) + … temp(X,y1,z1,j1,K) temp(X,y1,z2,j1,K) temp(X,y2,z1,j1,K) temp(X,y2,z2,j1,K) temp(X,y1,z1,j2,K) temp(X,y1,z2,j2,K) temp(X,y2,z1,j2,K) temp(X,y2,z2,j2,K) temp(X,Y,Z,J,K) = constit(Z,J,K) * rewrite(X,Y,Z).

/425 Declarative Methods – J. Eisner/J. Blatz Folding Sum over values of Z before summing over Y and J constit(X,I,K) = constit(y1,I,j1)* constit(z1,j1,K) * rewrite(X,y1,z1) + constit(y1,I,j1)* constit(z2,j1,K) * rewrite(X,y1,z2) + constit(y2,I,j1)* constit(z1,j1,K) * rewrite(X,y2,z1) + constit(y2,I,j1)* constit(z2,j1,K) * rewrite(X,y2,z2) + constit(y1,I,j2)* constit(z1,j2,K) * rewrite(X,y1,z1) + constit(y1,I,j2)* constit(z2,j2,K) * rewrite(X,y1,z2) + constit(y2,I,j2)* constit(z1,j2,K) * rewrite(X,y2,z1) + constit(y2,I,j2)* constit(z2,j2,K) * rewrite(X,y2,z2) + … temp2(X,Y,J,K) = constit(Z,J,K) * rewrite(X,Y,Z).

/425 Declarative Methods – J. Eisner/J. Blatz Folding Sum over values of Z before summing over Y and J constit(X,I,K) = constit(z1,j1,K) * rewrite(X,y1,z1) + constit(z2,j1,K) * rewrite(X,y1,z2) + constit(y2,I,j1)* constit(z1,j1,K) * rewrite(X,y2,z1) + constit(y2,I,j1)* constit(z2,j1,K) * rewrite(X,y2,z2) + constit(y1,I,j2)* constit(z1,j2,K) * rewrite(X,y1,z1) + constit(y1,I,j2)* constit(z2,j2,K) * rewrite(X,y1,z2) + constit(y2,I,j2)* constit(z1,j2,K) * rewrite(X,y2,z1) + constit(y2,I,j2)* constit(z2,j2,K) * rewrite(X,y2,z2) + … temp2(X,Y,J,K) = constit(Z,J,K) * rewrite(X,Y,Z). temp2(X,y1,j1,K) constit(y1,I,j1) *

/425 Declarative Methods – J. Eisner/J. Blatz Folding Sum over values of Z before summing over Y and J constit(X,I,K) = constit(z1,j1,K) * rewrite(X,y1,z1) + constit(z2,j1,K) * rewrite(X,y1,z2) constit(z1,j1,K) * rewrite(X,y2,z1) + constit(z2,j1,K) * rewrite(X,y2,z2) constit(z1,j2,K) * rewrite(X,y1,z1) + constit(z2,j2,K) * rewrite(X,y1,z2) constit(z1,j2,K) * rewrite(X,y2,z1) + constit(z2,j2,K) * rewrite(X,y2,z2) + … temp2(X,Y,J,K) = constit(Z,J,K) * rewrite(X,Y,Z). temp2(X,y1,j1,K) constit(y1,I,j1) * + constit(y2,I,j2) * + constit(y1,I,j2) * + constit(y2,I,j1) * temp2(X,y2,j1,K)temp2(X,y1,j2,K)temp2(X,y2,j2,K)

/425 Declarative Methods – J. Eisner/J. Blatz Folding – best version goal += constit(s,0,N) * end(N). constit(X,I,J) += word(W,I,J) * rewrite(X,W). constit(X,I,K) += constit(Y,I,J) * temp2(X,Y,J,K) temp2(X,Y,J,K) += constit(Z,J,K) * rewrite(X,Y,Z). Asymptotic complexity has been reduced!  O(k 2 n 3 ) for constit rule (doesn’t mention Z)  + O(k 3 n 2 ) for temp2 rule (doesn’t mention I) k grammar symbols (X,Y,Z) n positions in string (I,J,K)

/425 Declarative Methods – J. Eisner/J. Blatz Other names for folding Substitution Storing intermediate results Common subexpression elimination Moving an invariant out of a loop Building speculatively

/425 Declarative Methods – J. Eisner/J. Blatz Unfolding Unfolding = inverse of folding Inlines computation pathto(X) max= pathto(Y) + edge(Y, X). pathto(X) max= pathto(Z) + edge(Z,Y) + edge(Y, X). pathto(Y) max= pathto(Z) + edge(Z, Y).

/425 Declarative Methods – J. Eisner/J. Blatz Pop quiz A folding transformation can possibly increasedecreasenot affect the asymptotic time complexity. A folding transformation can possibly increasedecreasenot affect the asymptotic space complexity.

/425 Declarative Methods – J. Eisner/J. Blatz Pop quiz An unfolding transformation can possibly increasedecreasenot affect the asymptotic time complexity. An unfolding transformation can possibly increasedecreasenot affect the asymptotic space complexity.

/425 Declarative Methods – J. Eisner/J. Blatz Maximum independent set in a tree any(T) = the size of the maximum independent set in T rooted(T) = the size of the maximum independent set in T that includes T’s root unrooted(T) = the size of the maximum independent set in T that excludes T’s root rooted(t(R,[])) max= iq(R). unrooted(t(_,[])) max= 0. any(T) max= rooted(T). any(T) max= unrooted(T). rooted(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). unrooted(t(R,[X|Xs])) max= any(X) + unrooted(t(R,Xs)). as before

/425 Declarative Methods – J. Eisner/J. Blatz Maximum independent set in a tree (shorter but harder to understand version: find it automatically?) We could actually eliminate “rooted” from the program. Just do everything with “unrooted” and “any.” Slightly more efficient, but harder to convince yourself it’s right. That is, it’s an optimized version of the previous slide! We can prove it’s equivalent by a sequence of folding and unfolding steps—let’s see how! any(t(R,[])) max= iq(R). unrooted(t(_,[])) max= 0. any(T) max= unrooted(T). any(t(R,[X|Xs])) max= any(t(R,Xs)) + unrooted(X). unrooted(t(R,[X|Xs])) max= unrooted(t(R,Xs)) + any(X).

/425 Declarative Methods – J. Eisner/J. Blatz ① any(T) max= rooted(T). ② any(T) max= unrooted(T). ③ rooted(t(R,[])) max= iq(R). ④ rooted(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑤ unrooted(t(_,[])) max= 0. ⑥ unrooted(t(R,[X|Xs])) max= any(X) + unrooted(t(R,Xs)).

/425 Declarative Methods – J. Eisner/J. Blatz ① any(T) max= rooted(T). ② any(T) max= unrooted(T). ③ rooted(t(R,[])) max= iq(R). ④ rooted(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑤ unrooted(t(_,[])) max= 0. ⑥ unrooted(t(R,[X|Xs])) max= any(X) + unrooted(t(R,Xs)). unfold

/425 Declarative Methods – J. Eisner/J. Blatz ① any(T) max= rooted(T). ② any(T) max= unrooted(T). ③ rooted(t(R,[])) max= iq(R). ④ rooted(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑤ unrooted(t(_,[])) max= 0. ⑥ unrooted(t(R,[X|Xs])) max= any(X) + unrooted(t(R,Xs)). ⑦ any(t(R,[])) max= iq(R). ⑧ any(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). replaced by ⑦, ⑧ Gray rules are no longer part of the program

/425 Declarative Methods – J. Eisner/J. Blatz ① any(T) max= rooted(T). ② any(T) max= unrooted(T). ③ rooted(t(R,[])) max= iq(R). ④ rooted(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑤ unrooted(t(_,[])) max= 0. ⑥ unrooted(t(R,[X|Xs])) max= any(X) + unrooted(t(R,Xs)). ⑦ any(t(R,[])) max= iq(R). ⑧ any(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). unfold

/425 Declarative Methods – J. Eisner/J. Blatz ① any(T) max= rooted(T). ② any(T) max= unrooted(T). ③ rooted(t(R,[])) max= iq(R). ④ rooted(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑤ unrooted(t(_,[])) max= 0. ⑥ unrooted(t(R,[X|Xs])) max= any(X) + unrooted(t(R,Xs)). ⑦ any(t(R,[])) max= iq(R). ⑧ any(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑨ any(t(_,[])) max= 0. ⑩ any(t(R,[X|Xs])) max= any(X) + unrooted(t(R, Xs)). replaced by ⑨, ⑩

/425 Declarative Methods – J. Eisner/J. Blatz ① any(T) max= rooted(T). ② any(T) max= unrooted(T). ③ rooted(t(R,[])) max= iq(R). ④ rooted(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑤ unrooted(t(_,[])) max= 0. ⑥ unrooted(t(R,[X|Xs])) max= any(X) + unrooted(t(R,Xs)). ⑦ any(t(R,[])) max= iq(R). ⑧ any(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑨ any(t(_,[])) max= 0. ⑩ any(t(R,[X|Xs])) max= any(X) + unrooted(t(R, Xs)). unfold Rules ① and ② are no longer part of the current program They were the definition of any(T) in a previous valid program, so we can use them for unfolding

/425 Declarative Methods – J. Eisner/J. Blatz ① any(T) max= rooted(T). ② any(T) max= unrooted(T). ③ rooted(t(R,[])) max= iq(R). ④ rooted(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑤ unrooted(t(_,[])) max= 0. ⑥ unrooted(t(R,[X|Xs])) max= any(X) + unrooted(t(R,Xs)). ⑦ any(t(R,[])) max= iq(R). ⑧ any(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑨ any(t(_,[])) max= 0. ⑩ any(t(R,[X|Xs])) max= any(X) + unrooted(t(R, Xs)). ⑪ any(t(R,[X|Xs])) max= rooted(X) + unrooted(t(R, Xs)). ⑫ any(t(R,[X|Xs])) max= unrooted(X) + unrooted(t(R, Xs)). replaced by ⑪, ⑫

/425 Declarative Methods – J. Eisner/J. Blatz ① any(T) max= rooted(T). ② any(T) max= unrooted(T). ③ rooted(t(R,[])) max= iq(R). ④ rooted(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑤ unrooted(t(_,[])) max= 0. ⑥ unrooted(t(R,[X|Xs])) max= any(X) + unrooted(t(R,Xs)). ⑦ any(t(R,[])) max= iq(R). ⑧ any(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑨ any(t(_,[])) max= 0. ⑩ any(t(R,[X|Xs])) max= any(X) + unrooted(t(R, Xs)). ⑪ any(t(R,[X|Xs])) max= rooted(X) + unrooted(t(R, Xs)). ⑫ any(t(R,[X|Xs])) max= unrooted(X) + unrooted(t(R, Xs)). ⒀ any(t(R,[X|Xs])) max= unrooted(X) + unrooted(t(R, Xs)). Duplicating a rule doesn’t affect the value computed by max= Allowed to do this transformation because of this particular property of max= duplicate

/425 Declarative Methods – J. Eisner/J. Blatz ① any(T) max= rooted(T). ② any(T) max= unrooted(T). ③ rooted(t(R,[])) max= iq(R). ④ rooted(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑤ unrooted(t(_,[])) max= 0. ⑥ unrooted(t(R,[X|Xs])) max= any(X) + unrooted(t(R,Xs)). ⑦ any(t(R,[])) max= iq(R). ⑧ any(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑨ any(t(_,[])) max= 0. ⑩ any(t(R,[X|Xs])) max= any(X) + unrooted(t(R, Xs)). ⑪ any(t(R,[X|Xs])) max= rooted(X) + unrooted(t(R, Xs)). ⑫ any(t(R,[X|Xs])) max= unrooted(X) + unrooted(t(R, Xs)). ⒀ any(t(R,[X|Xs])) max= unrooted(X) + unrooted(t(R, Xs)). Fold two rules into two rules fold

/425 Declarative Methods – J. Eisner/J. Blatz ① any(T) max= rooted(T). ② any(T) max= unrooted(T). ③ rooted(t(R,[])) max= iq(R). ④ rooted(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑤ unrooted(t(_,[])) max= 0. ⑥ unrooted(t(R,[X|Xs])) max= any(X) + unrooted(t(R,Xs)). ⑦ any(t(R,[])) max= iq(R). ⑧ any(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑨ any(t(_,[])) max= 0. ⑩ any(t(R,[X|Xs])) max= any(X) + unrooted(t(R, Xs)). ⑪ any(t(R,[X|Xs])) max= rooted(X) + unrooted(t(R, Xs)). ⑫ any(t(R,[X|Xs])) max= unrooted(X) + unrooted(t(R, Xs)). ⒀ any(t(R,[X|Xs])) max= unrooted(X) + unrooted(t(R, Xs)). ⑭ any(t(R,[X|Xs])) max= unrooted(X) + any(t(R, Xs)). replaced by ⑭

/425 Declarative Methods – J. Eisner/J. Blatz ① any(T) max= rooted(T). ② any(T) max= unrooted(T). ③ rooted(t(R,[])) max= iq(R). ④ rooted(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑤ unrooted(t(_,[])) max= 0. ⑥ unrooted(t(R,[X|Xs])) max= any(X) + unrooted(t(R,Xs)). ⑦ any(t(R,[])) max= iq(R). ⑧ any(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑨ any(t(_,[])) max= 0. ⑩ any(t(R,[X|Xs])) max= any(X) + unrooted(t(R, Xs)). ⑪ any(t(R,[X|Xs])) max= rooted(X) + unrooted(t(R, Xs)). ⑫ any(t(R,[X|Xs])) max= unrooted(X) + unrooted(t(R, Xs)). ⒀ any(t(R,[X|Xs])) max= unrooted(X) + unrooted(t(R, Xs)). ⑭ any(t(R,[X|Xs])) max= unrooted(X) + any(t(R, Xs)). fold

/425 Declarative Methods – J. Eisner/J. Blatz ① any(T) max= rooted(T). ② any(T) max= unrooted(T). ③ rooted(t(R,[])) max= iq(R). ④ rooted(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑤ unrooted(t(_,[])) max= 0. ⑥ unrooted(t(R,[X|Xs])) max= any(X) + unrooted(t(R,Xs)). ⑦ any(t(R,[])) max= iq(R). ⑧ any(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑨ any(t(_,[])) max= 0. ⑩ any(t(R,[X|Xs])) max= any(X) + unrooted(t(R, Xs)). ⑪ any(t(R,[X|Xs])) max= rooted(X) + unrooted(t(R, Xs)). ⑫ any(t(R,[X|Xs])) max= unrooted(X) + unrooted(t(R, Xs)). ⒀ any(t(R,[X|Xs])) max= unrooted(X) + unrooted(t(R, Xs)). ⑭ any(t(R,[X|Xs])) max= unrooted(X) + any(t(R, Xs)). ⑮ any(t(R,[X|Xs])) max= any(X) + unrooted(t(R, Xs)). replaced by ⑮

/425 Declarative Methods – J. Eisner/J. Blatz ① any(T) max= rooted(T). ② any(T) max= unrooted(T). ③ rooted(t(R,[])) max= iq(R). ④ rooted(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑤ unrooted(t(_,[])) max= 0. ⑥ unrooted(t(R,[X|Xs])) max= any(X) + unrooted(t(R,Xs)). ⑦ any(t(R,[])) max= iq(R). ⑧ any(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑨ any(t(_,[])) max= 0. ⑩ any(t(R,[X|Xs])) max= any(X) + unrooted(t(R, Xs)). ⑪ any(t(R,[X|Xs])) max= rooted(X) + unrooted(t(R, Xs)). ⑫ any(t(R,[X|Xs])) max= unrooted(X) + unrooted(t(R, Xs)). ⒀ any(t(R,[X|Xs])) max= unrooted(X) + unrooted(t(R, Xs)). ⑭ any(t(R,[X|Xs])) max= unrooted(X) + any(t(R, Xs)). ⑮ any(t(R,[X|Xs])) max= any(X) + unrooted(t(R, Xs)). fold

/425 Declarative Methods – J. Eisner/J. Blatz ① any(T) max= rooted(T). ② any(T) max= unrooted(T). ③ rooted(t(R,[])) max= iq(R). ④ rooted(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑤ unrooted(t(_,[])) max= 0. ⑥ unrooted(t(R,[X|Xs])) max= any(X) + unrooted(t(R,Xs)). ⑦ any(t(R,[])) max= iq(R). ⑧ any(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑨ any(t(_,[])) max= 0. ⑩ any(t(R,[X|Xs])) max= any(X) + unrooted(t(R, Xs)). ⑪ any(t(R,[X|Xs])) max= rooted(X) + unrooted(t(R, Xs)). ⑫ any(t(R,[X|Xs])) max= unrooted(X) + unrooted(t(R, Xs)). ⒀ any(t(R,[X|Xs])) max= unrooted(X) + unrooted(t(R, Xs)). ⑭ any(t(R,[X|Xs])) max= unrooted(X) + any(t(R, Xs)). ⑮ any(t(R,[X|Xs])) max= any(X) + unrooted(t(R, Xs)). ⑯ any(T) max= unrooted(T). fold

/425 Declarative Methods – J. Eisner/J. Blatz ① any(T) max= rooted(T). ② any(T) max= unrooted(T). ③ rooted(t(R,[])) max= iq(R). ④ rooted(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑤ unrooted(t(_,[])) max= 0. ⑥ unrooted(t(R,[X|Xs])) max= any(X) + unrooted(t(R,Xs)). ⑦ any(t(R,[])) max= iq(R). ⑧ any(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑨ any(t(_,[])) max= 0. ⑩ any(t(R,[X|Xs])) max= any(X) + unrooted(t(R, Xs)). ⑪ any(t(R,[X|Xs])) max= rooted(X) + unrooted(t(R, Xs)). ⑫ any(t(R,[X|Xs])) max= unrooted(X) + unrooted(t(R, Xs)). ⒀ any(t(R,[X|Xs])) max= unrooted(X) + unrooted(t(R, Xs)). ⑭ any(t(R,[X|Xs])) max= unrooted(X) + any(t(R, Xs)). ⑮ any(t(R,[X|Xs])) max= any(X) + unrooted(t(R, Xs)). ⑯ any(T) max= unrooted(T). trim Nothing relies on rooted anymore, so we can delete it

/425 Declarative Methods – J. Eisner/J. Blatz ① any(T) max= rooted(T). ② any(T) max= unrooted(T). ③ rooted(t(R,[])) max= iq(R). ④ rooted(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑤ unrooted(t(_,[])) max= 0. ⑥ unrooted(t(R,[X|Xs])) max= any(X) + unrooted(t(R,Xs)). ⑦ any(t(R,[])) max= iq(R). ⑧ any(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑨ any(t(_,[])) max= 0. ⑩ any(t(R,[X|Xs])) max= any(X) + unrooted(t(R, Xs)). ⑪ any(t(R,[X|Xs])) max= rooted(X) + unrooted(t(R, Xs)). ⑫ any(t(R,[X|Xs])) max= unrooted(X) + unrooted(t(R, Xs)). ⒀ any(t(R,[X|Xs])) max= unrooted(X) + unrooted(t(R, Xs)). ⑭ any(t(R,[X|Xs])) max= unrooted(X) + any(t(R, Xs)). ⑮ any(t(R,[X|Xs])) max= any(X) + unrooted(t(R, Xs)). ⑯ any(T) max= unrooted(T).

/425 Declarative Methods – J. Eisner/J. Blatz ① any(T) max= rooted(T). ② any(T) max= unrooted(T). ③ rooted(t(R,[])) max= iq(R). ④ rooted(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑤ unrooted(t(_,[])) max= 0. ⑥ unrooted(t(R,[X|Xs])) max= any(X) + unrooted(t(R,Xs)). ⑦ any(t(R,[])) max= iq(R). ⑧ any(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑨ any(t(_,[])) max= 0. ⑩ any(t(R,[X|Xs])) max= any(X) + unrooted(t(R, Xs)). ⑪ any(t(R,[X|Xs])) max= rooted(X) + unrooted(t(R, Xs)). ⑫ any(t(R,[X|Xs])) max= unrooted(X) + unrooted(t(R, Xs)). ⒀ any(t(R,[X|Xs])) max= unrooted(X) + unrooted(t(R, Xs)). ⑭ any(t(R,[X|Xs])) max= unrooted(X) + any(t(R, Xs)). ⑮ any(t(R,[X|Xs])) max= any(X) + unrooted(t(R, Xs)). ⑯ any(T) max= unrooted(T). Success!

/425 Declarative Methods – J. Eisner/J. Blatz ① any(T) max= rooted(T). ② any(T) max= unrooted(T). ③ rooted(t(R,[])) max= iq(R). ④ rooted(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑤ unrooted(t(_,[])) max= 0. ⑥ unrooted(t(R,[X|Xs])) max= any(X) + unrooted(t(R,Xs)). ⑦ any(t(R,[])) max= iq(R). ⑧ any(t(R,[X|Xs])) max= unrooted(X) + rooted(t(R,Xs)). ⑨ any(t(_,[])) max= 0. ⑩ any(t(R,[X|Xs])) max= any(X) + unrooted(t(R, Xs)). ⑪ any(t(R,[X|Xs])) max= rooted(X) + unrooted(t(R, Xs)). ⑫ any(t(R,[X|Xs])) max= unrooted(X) + unrooted(t(R, Xs)). ⒀ any(t(R,[X|Xs])) max= unrooted(X) + unrooted(t(R, Xs)). ⑭ any(t(R,[X|Xs])) max= unrooted(X) + any(t(R, Xs)). ⑮ any(t(R,[X|Xs])) max= any(X) + unrooted(t(R, Xs)). ⑯ any(T) max= unrooted(T). unfold

/425 Declarative Methods – J. Eisner/J. Blatz ①②③④⑤⑥⑦⑨⑩⑪⑫⑬⑭⑮⑯ ① any(T) max= unrooted(T). ② any(T) max= rooted(T).

/425 Declarative Methods – J. Eisner/J. Blatz Bottom-up evaluation goal subgoal axiom subgoal axiom subgoal axiom subgoal axiom subgoal axiom subgoal Combine all axioms to build all possible subgoals… many irrelevant to goal!

/425 Declarative Methods – J. Eisner/J. Blatz “Magic Templates” Transformation Simulate top-down execution Introduce new “interesting” predicates that keep track of what top-down execution built “interesting(foo(x))” means “top-down execution would have examined “foo(x)” If in order to build “foo” you need to build “bar”, then interesting(bar) :- interesting(foo). Magic predicates will later be used to filter bottom-up construction relate to “interesting” from max indep set example

/425 Declarative Methods – J. Eisner/J. Blatz What would Prolog do? interesting(goal). interesting(constit(s,0)) :- interesting(goal). interesting(rewrite(s)) :- interesting(constit(s,0)). interesting(constit(np,0)) :- interesting(constit(s,0)), rewrite(s,np,vp). interesting(constit(vp,1)) :- interesting(constit(s,0)). rewrite(s,np,vp), constit(np,0,1). goal constit(s,0,N) end(N) goal += constit(s,0,N) * end(N). constit(X,I,J) += rewrite(X,W) * word(W,I,J). constit(X,I,K) += rewrite(X,Y,Z) * constit(Y,I,J) * constit(Z,J,K). constit(np,0,1) constit(vp,1,N) <--- apply this rule <--- now apply this rule Don’t really need this-- assume we’ve built all the grammar rules beforehand

/425 Declarative Methods – J. Eisner/J. Blatz Generalization foo(X, Y) += bar(X, A) * baz(A, Y). interesting(bar(X)) |= interesting(foo(X, Y)). interesting(baz(A,Y)) |= interesting(foo(X,Y)) & bar(X,A).

/425 Declarative Methods – J. Eisner/J. Blatz The transformed program interesting(goal) = TRUE. interesting(constit(s,0)) |= interesting(goal). interesting(constit(Y,I)) |= interesting(constit(X,I)) & rewrite(X,Y,Z). interesting(constit(Z,J)) |= interesting(constit(X,I)) & rewrite(X,Y,Z) & constit(Y,I,J). goal += constit(s,0,N) * end(N) if interesting(goal). constit(X,I,J) += rewrite(X,W) * word(W,I,J) if interesting(constit(X,I)). constit(X,I,K) += rewrite(X,Y,Z) * constit(Y,I,J) * constit(Z,J,K) if interesting(constit(X,I)). Uses forward chaining to simulate backward chaining

/425 Declarative Methods – J. Eisner/J. Blatz Magic Execution goal subgoal axiom subgoal axiom subgoal impossible subgoal axiom impossible subgoal irrelevant subgoal irrelevant subgoal axiom irrelevant subgoal irrelevant axiom irrelevant subgoal Build interesting filters before other predicates