Download Notes on Logic Programming Pattern Matching for Java Course Wrap-up | CS 3110 and more Study notes Computer Science in PDF only on Docsity!
CS 3110
Fall 2008
Lecture 27
Andrew Myers
Logic programming
Pattern matching for Java
Course wrap-up
Declarative vs. imperative
- Imperative programming: tell computer how to
change its state to accomplish a result
- Declarative^ programming: tell computer what you
want computed, without specifying state changes
- Avoids side effects, enables analysis and optimization
- functional programming: give an expression equal to the
desired result
- logic programming : give a logical formula describing
what should be true of the result
- a simple version: database queries
Concatenating lists
- Goal: a predicate join(L 1 , L 2 , L 3 ) meaning L 1 @L 2 = L 3.
- L 1 @L 2 = (H 1 ::T 1 )@L 2 = H 1 ::(T 1 @L 2 )
- So L 1 @L 2 = L 3 if^ L 3 = H 1 ::T 3 and T 1 @L 2 =T 3
join([], L2, L2).
join(H1::T1, L2, H1::T3) <= join(T1,L2,T3).
# join([1,2,3], [4,5,6], X)
X = [1,2,3,4,5,6]
# join([1,X,3], 4::Y, [1,2,Z,W,5,6])
X = 2, Y = [5,6], Z = 3, W = 4
# join([1,X,X], [Y,Y], [X,X,Y,Y,Y])
Reversible computation!
Pattern matching
- Pattern matching is (limited) reversible computation
- Forward: let lst = x::y::rest
- Backward: match lst with x::y::rest -> ...
- But we can’t pattern-match unless we know exactly how the
data is represented.
- Can’t pattern-match on an abstract type.
- This is why OO languages don’t have pattern matching.
JMatch logic programming
- A limited form of logic programming! List join(List x, List y) returns(x) returns(y) ( x = List(hx, tx) & tr = join(tx, y) & result = List(hx, tr) ) let List(1, List(2, null)) = join(prefix, List y); ... use y here ...
Rebalancing a red-black tree in JMatch static Node balance(int color, int value, Tree left, Tree right) { if (color == BLACK) { switch (value,left,right) { case int z, Node(RED,int y, Node(RED,int x,Tree a,Tree b),Tree c), Tree d: case z, Node(RED,x,a,Node(RED,y,b,c)),d: case x, c, Node(RED,z,Node(RED,y,a,b),d): case x, a, Node(RED,y,b,Node(RED,z,c,d)): return Node(RED,y, Node(BLACK,x,a,b),Node(BLACK,z,c,d)); } } return Node(color, value, left, right); }
The tree iterator in Java class TreeIterator implements Iterator { Iterator subiterator; boolean hasNext; Object current; int state; // states: // 1. Iterating through left child. // 2. Just yielded current node value // 3. Iterating through right child TreeIterator() { subiterator = Tree.this.left.iterator(); state = 1; preloadNext(); } public boolean hasNext() { return hasNext; } public Object next() { if (!hasNext) throw new NoSuchElementException(); Object ret = current; preloadNext(); return ret; } private void preloadNext() { loop: while (true) { switch (state) { case 1: case 3: hasNext = true; if (subiterator.hasNext()) { current = subiterator.next(); return; } else { if (state == 1) { state = 2; current = Tree.this.value; return; } else { hasNext = false; return; } case 2: subiterator = Tree.right.iterator(); state = 3; continue loop; }
Comparing iterators class Tree { Tree left, right; Elem value; public IEnumerator elements() { foreach (Elem e in left.elements()) { yield return e; } yield return value; foreach (Elem e in right.elements()) { yield return e; } } public Elem elements() iterates (result) { foreach (Elem e = left.elements()) { yield e; } yield value; foreach (Elem e = right.elements()) { yield e; } }
Both languages
have coroutine
iterators
(CLU)
Elem elements() iterates (result) ( result < value && left.contains(result) || result = value || result > value && right.contains(result))
or:
JMatch
C# 2.0+
What was 3110 about? Goal: better software design and implementation
- New programming paradigms
- higher-order functions, pattern matching, polymorphism, concurrency, ...
- Specifying functions and data abstractions
- Reasoning about correctness
- using specifications, logic
- Reasoning about performance
- asymptotic complexity, recurrences, amortized complexity, locality
- Important data structures and algorithms
- balanced binary trees, hash tables, splay trees, B-trees, functional impls
Final exam
- Dec 18, 2-4:30pm, Olin 155
- Open book
- Cumulative
Credits
- Instructor: Andrew Myers
- TAs: K. Vikram^ (PS3), Ed McTigue^ (PS2/6),
Rick Ducott (PS4/6), Tanya Gupta (PS5)
- Consultants: Andrew Owens^ (PS1/5, test harness), Matt
Pokrzywa (PS3/5), Dane Wallinga (PS4/5), David Kupiec
(PS3), Jerzy Hausknecht (PS6), Matt Paff (PS2/6)
- Don’t miss the tournament: Tuesday Dec. 9, Upson B17, 7:30pm.