SLIDE 1 A Logic Your Typechecker Can Count On: Unordered Tree Types in Practice
Nate Foster (Penn) Benjamin C. Pierce (Penn) Alan Schmitt (INRIA Rhˆ
PLAN-X ’07
µX. {}|(hd[T]+tl[X])
2 4 hd[T] , hd[¬T] , tl[X] , tl[¬X] , {hd, tl}[True] 3 5
SLIDE 2 µX. {}|(hd[T]+tl[X])
hd[T] , hd[¬T] , tl[X] , tl[¬X] , {hd, tl}[True]
SLIDE 3 Types in
Sync A B A’ B’ O T
Harmony
A generic synchronization framework
◮ Architecture takes two replicas + original ⇒ updated
replicas.
◮ Data model is “deterministic” trees: unordered,
edge-labeled trees.
SLIDE 4 Types in
Sync A B A’ B’ O T
Harmony: Typed Synchronization [DBPL ’05]
Behavior of synchronizer guided by type.
◮ If inputs well-typed, so are outputs. ◮ Required operations: membership of trees in type [also
sets of names].
SLIDE 5 Types in
Sync A B A’ B’ O T
Harmony: Lenses [POPL ’05]
Pre-/post-process replicas using bi-directional programs.
◮ Facilitates heterogeneous synchronization. ◮ Types in conditionals, run-time asserts, static checkers. ◮ Required operations: membership, inclusion, equivalence,
emptiness, [projection, injection, etc.].
SLIDE 6
Deterministic Tree Types
Syntax
T::= {} | n[T] | T+T | T|T | ~T | X | !\{n1, .., nk}[T] | *\{n1, .., nk}[T]
SLIDE 7
Deterministic Tree Types
Syntax
T::= {} | n[T] | T+T | T|T | ~T | X | !\{n1, .., nk}[T] | *\{n1, .., nk}[T]
Semantics
Singleton denoting the unique tree with no children: ∈ {}
SLIDE 8
Deterministic Tree Types
Syntax
T::= {} | n[T] | T+T | T|T | ~T | X | !\{n1, .., nk}[T] | *\{n1, .., nk}[T]
Semantics
Atoms: trees with single child n and subtree in T: If t ∈ T, then
n
t ∈ n[T]
SLIDE 9
Deterministic Tree Types
Syntax
T::= {} | n[T] | T+T | T|T | ~T | X | !\{n1, .., nk}[T] | *\{n1, .., nk}[T]
Semantics
Commutative concatenation operator: If t ∈ T and t’ ∈ T ′, then t t’ ∈ T+T ′
SLIDE 10
Deterministic Tree Types
Syntax
T::= {} | n[T] | T+T | T|T | ~T | X | !\{n1, .., nk}[T] | *\{n1, .., nk}[T]
Semantics
Boolean operations and recursion: X1 = T1 . . . Xn = Tn
SLIDE 11
Deterministic Tree Types
Syntax
T::= {} | n[T] | T+T | T|T | ~T | X | !\{n1, .., nk}[T] | *\{n1, .., nk}[T]
Semantics
If m ∈ {n1, .., nk} and t ∈ T, then
m
t ∈!\{n1, .., nk}[T]
SLIDE 12
Deterministic Tree Types
Syntax
T::= {} | n[T] | T+T | T|T | ~T | X | !\{n1, .., nk}[T] | *\{n1, .., nk}[T]
Semantics
If m1, .., mk ∈ {n1, .., nk} and t1 .. tk ∈ T, then
m1 mk
.. .. t1 tk ∈ *\{n1, .., nk}[T]
SLIDE 13
Deterministic Tree Types
Syntax
T::= {} | n[T] | T+T | T|T | ~T | X | !\{n1, .., nk}[T] | *\{n1, .., nk}[T]
Example: hd[True]+tl[True]
hd tl
SLIDE 14 Deterministic Tree Types
Syntax
T::= {} | n[T] | T+T | T|T | ~T | X | !\{n1, .., nk}[T] | *\{n1, .., nk}[T]
Example: {}|(hd[True]+tl[True])
hd tl
SLIDE 15 Deterministic Tree Types
Syntax
T::= {} | n[T] | T+T | T|T | ~T | X | !\{n1, .., nk}[T] | *\{n1, .., nk}[T]
Example: X = {}|(hd[True]+tl[X])
hd tl hd tl hd tl
SLIDE 16
Deterministic Tree Types
Syntax
T::= {} | n[T] | T+T | T|T | ~T | X | !\{n1, .., nk}[T] | *\{n1, .., nk}[T]
Example: ![True]+![True]
SLIDE 17 Deterministic Tree Types
Syntax
T::= {} | n[T] | T+T | T|T | ~T | X | !\{n1, .., nk}[T] | *\{n1, .., nk}[T]
Example: ~(![True]+![True])
Can eliminate negations, and use direct algorithms, but types get large...
SLIDE 18
Sheaves Formulas
Formulas
S = φ(x0, .., xk), [r0[S0] , .., rk[Sk]] where φ is a Presburger formula and ri a set of names. [Dal Zilio, Lugiez, Meyssonnier, POPL ’04]
SLIDE 19
Sheaves Formulas
Formulas
S = φ(x0, .., xk), [r0[S0] , .., rk[Sk]] where φ is a Presburger formula and ri a set of names. φ(x0, x1), [b[True], {a, c}[True]]
a b c
SLIDE 20
Sheaves Formulas
Formulas
S = φ(x0, .., xk), [r0[S0] , .., rk[Sk]] where φ is a Presburger formula and ri a set of names. φ(x0, x1), [b[True], {a, c}[True]] 1
a b c
SLIDE 21
Sheaves Formulas
Formulas
S = φ(x0, .., xk), [r0[S0] , .., rk[Sk]] where φ is a Presburger formula and ri a set of names. φ(x0, x1), [b[True], {a, c}[True]] 1 1
a b c
SLIDE 22
Sheaves Formulas
Formulas
S = φ(x0, .., xk), [r0[S0] , .., rk[Sk]] where φ is a Presburger formula and ri a set of names. φ(x0, x1), [b[True], {a, c}[True]] 1 2
a b c
SLIDE 23 Sheaves Formulas
Formulas
S = φ(x0, .., xk), [r0[S0] , .., rk[Sk]] where φ is a Presburger formula and ri a set of names. φ(x0, x1), [b[True], {a, c}[True]] 1 2
?
| = φ(1, 2)
SLIDE 24 Sheaves Formulas
Formulas
S = φ(x0, .., xk), [r0[S0] , .., rk[Sk]] where φ is a Presburger formula and ri a set of names. φ(x0, x1, x2),
- b[True], {a, c}[True], {a, b, c}[True]
- For coherence: ri[Si] must partition set of atoms.
Note: does not ensure determinism.
SLIDE 25 Examples as Sheaves Formulas
X = ({}|hd[True]+tl[X])
X = (x0 =x1 =x2 =x3 =0) ∨ (x0 =x1 =1 ∧ x2 =x3 =0),
- hd[True] , tl[X] , tl[¬X] , {hd, tl}[True]
SLIDE 26 Examples as Sheaves Formulas
X = ({}|hd[True]+tl[X])
X = (x0 =x1 =x2 =x3 =0) ∨ (x0 =x1 =1 ∧ x2 =x3 =0),
- hd[True] , tl[X] , tl[¬X] , {hd, tl}[True]
- ~(![True]+![True])
x0 = 2,
SLIDE 27 Challenges and Strategies
Blowup in naive compilation from types to formulas.
◮ Syntactic optimizations avoid blowup in common cases.
Backtracking in top-down, non-deterministic traversal.
◮ Incremental algorithm avoids useless paths.
Presburger arithmetic requires double-exponential time.
◮ Compile Presburger formulas to MONA representation. ◮ Hash-consing allocation + aggressive memoization.
SLIDE 28 Challenges and Strategies
Blowup in naive compilation from types to formulas.
◮ Syntactic optimizations avoid blowup in common cases.
Backtracking in top-down, non-deterministic traversal.
◮ Incremental algorithm avoids useless paths.
Presburger arithmetic requires double-exponential time.
◮ Compile Presburger formulas to MONA representation. ◮ Hash-consing allocation + aggressive memoization.
Contributions
◮ Strategies and algorithms; ◮ Implementation in Harmony; ◮ Experimental results.
SLIDE 29 Incremental Algorithm
φ(x0, .., xk), [r0[S0] , ..rk[Sk]] ..
n1 n2 nk−1 nk
.. ..
SLIDE 30 Incremental Algorithm
φ(x0, .., xk), [r0[S0] , ..rk[Sk]] (φ)
n1 n2 nk−1 nk
.. ..
SLIDE 31 Incremental Algorithm
φ(x0, .., xk), [r0[S0] , ..rk[Sk]] (φ ∧ ψdom)
n1 n2 nk−1 nk
.. ..
SLIDE 32 Incremental Algorithm
φ(x0, .., xk), [r0[S0] , ..rk[Sk]] (φ ∧ ψdom ∧ ψ1)
n1 n2 nk−1 nk
.. ..
SLIDE 33 Incremental Algorithm
φ(x0, .., xk), [r0[S0] , ..rk[Sk]] (φ ∧ ψdom ∧ ψ1 ∧ ψ2)
n1 n2 nk−1 nk
.. ..
SLIDE 34 Incremental Algorithm
φ(x0, .., xk), [r0[S0] , ..rk[Sk]] (φ ∧ ψdom ∧ ψ1 ∧ .. ∧ ψk−1)
n1 n2 nk−1 nk
.. ..
SLIDE 35 Incremental Algorithm
φ(x0, .., xk), [r0[S0] , ..rk[Sk]] (φ ∧ ψdom ∧ ψ1 ∧ .. ∧ ψk)
n1 n2 nk−1 nk
.. ..
SLIDE 36 Hash-Consing and Memoization
Thousands of formulas and trees, but many repeats. Suggests hash-consed allocation:
◮ Sheaves formulas; ◮ Presburger formulas; ◮ Trees.
Memoization of intermediate results:
◮ MONA representations of Presburger formulas; ◮ Satisfiability of Presburger formulas; ◮ Membership results; ◮ Partially-evaluated member functions.
SLIDE 37 Experiments
Programs:
◮ Structured text parser; ◮ Address book validator; ◮ iCalendar lens.
Experimental setup: structures populated with snippets of Joyce’s Ulysses; 1.4GHz Intel Pentium III, 2GB RAM, SuSE Linux OS kernel 2.6.16; execution times collected from POSIX functions.
SLIDE 38 Experiments: Address Book Validator
Time(seconds) 50 100 150 Input Size (# lines) 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 base base-memo
States Formulas Sat Trees 312 107711 99.8% 25744 99.9% 107711 42.1%
SLIDE 39 Experiments: Address Book Validator
Time(seconds) 50 100 150 Input Size (# lines) 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 base base-memo incr-all-off incr
States Formulas Sat Trees 312 107711 99.8% 25744 99.9% 107711 42.1%
SLIDE 40 Experiments: Address Book Validator
Time(seconds) 50 100 150 Input Size (# lines) 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 base base-memo incr-all-off incr-phi-off incr-member-off incr
States Formulas Sat Trees 312 107711 99.8% 25744 99.9% 107711 42.1%
SLIDE 41 Experiments: Structured Text Parser
Time(seconds) 50 100 150 200 Input Size (# lines) 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 base base-memo incr-all-off incr-phi-off incr-member-off incr
States Formulas Sat Trees 105 12580 99.1% 222 92.8% 3507706 81.4%
SLIDE 42 Experiments: iCalendar Lens
Time(seconds) 50 100 150 200 250 Input Size (# lines) 100 200 300 400 500 600 700 800 900 1000 base base-memo incr-all-off incr-phi-off incr-member-off incr
States Formulas Sat Trees 361 116939 97.4% 17600 87.8% 407652 76.5%
SLIDE 43 Related Work
Types and Automata:
◮ TQL [Cardelli and Ghelli, ESOP ’01] ◮ “A Logic You Can Count On”
[Dal Zilio, Lugiez, Meyssonnier, POPL ’04]
◮ “Counting In Trees For Free”
[Seidl, Schwentick, Muscholl, Habermehl, ICALP ’04]
◮ Survey and Foundations:
[Boneva and Talbot, RTA ’05, LICS ’05] Implementations:
◮ “Static Checkers for Tree Structrures and Heaps”
[Hague ’04]
◮ “Boolean Operations and Inclusion Test for Attribute
Element Constraints” [Hosoya and Murata, ICALP ’03]
SLIDE 44 Conclusions and Future Work
Summary
◮ Strategies and algorithms; ◮ Implemented in Harmony; ◮ Reasonable performance.
Tune algorithm, hash-consing, memoization parameters. Determinize sheaves formulas. Implement Presburger arithmetic directly, optimized for adding constraints incrementally; also restricted fragments. Extend to new structures and types: multitrees, ordered trees, also horizontal recursion, adjoint operators, etc.
SLIDE 45
Acknowledgements
Haruo Hosoya, Christian Kirkegaard, St´ ephane Lescuyer, Thang Nguyen, Val Tannen, Penn PLClub and DB Group.
http://www.seas.upenn.edu/∼harmony/