A Logic Your Typechecker Can Count On: Unordered Tree Types in Practice Nate Foster (Penn) Benjamin C. Pierce (Penn) Alan Schmitt (INRIA Rhˆ one-Alpes) µ X . {}| ( hd [ T ]+ tl [ X ] ) � PLAN-X ’07 φ ( x 0 , .., x 4 ) , 2 hd [ T ] , hd [ ¬ T ] , 3 tl [ X ] , tl [ ¬ X ] , 4 5 { hd , tl } [True]
µ X . {}| ( hd [ T ]+ tl [ X ] ) � φ ( x 0 , .., x 4 ) , hd [ T ] , hd [ ¬ T ] , tl [ X ] , tl [ ¬ X ] , { hd , tl } [True]
Types in O A B Sync A’ B’ T Harmony A generic synchronization framework ◮ Architecture takes two replicas + original ⇒ updated replicas. ◮ Data model is “deterministic” trees: unordered, edge-labeled trees.
Types in O A B Sync A’ B’ 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].
Types in O A B Sync A’ B’ 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.].
Deterministic Tree Types Syntax T ::= {} | n [ T ] | T + T | T | T | ~ T | X | ! \{ n 1 , .., n k } [ T ] | * \{ n 1 , .., n k } [ T ]
Deterministic Tree Types Syntax T ::= {} | n [ T ] | T + T | T | T | ~ T | X | ! \{ n 1 , .., n k } [ T ] | * \{ n 1 , .., n k } [ T ] Semantics Singleton denoting the unique tree with no children: ∈ {}
Deterministic Tree Types Syntax T ::= {} | n [ T ] | T + T | T | T | ~ T | X | ! \{ n 1 , .., n k } [ T ] | * \{ n 1 , .., n k } [ T ] Semantics Atoms: trees with single child n and subtree in T : n If ∈ T , then ∈ n [ T ] t t
Deterministic Tree Types Syntax T ::= {} | n [ T ] | T + T | T | T | ~ T | X | ! \{ n 1 , .., n k } [ T ] | * \{ n 1 , .., n k } [ T ] Semantics Commutative concatenation operator: If ∈ T and ∈ T ′ , then ∈ T + T ′ t t’ t t’
Deterministic Tree Types Syntax T ::= {} | n [ T ] | T + T | T | T | ~ T | X | ! \{ n 1 , .., n k } [ T ] | * \{ n 1 , .., n k } [ T ] Semantics Boolean operations and recursion: = X 1 T 1 . . . X n = T n
Deterministic Tree Types Syntax T ::= {} | n [ T ] | T + T | T | T | ~ T | X | ! \{ n 1 , .., n k } [ T ] | * \{ n 1 , .., n k } [ T ] Semantics If m �∈ { n 1 , .., n k } and m ∈ T , then ∈ ! \{ n 1 , .., n k } [ T ] t t
Deterministic Tree Types Syntax T ::= {} | n [ T ] | T + T | T | T | ~ T | X | ! \{ n 1 , .., n k } [ T ] | * \{ n 1 , .., n k } [ T ] Semantics If m 1 , .., m k �∈ { n 1 , .., n k } and m 1 m k .. .. ∈ T , then ∈ * \{ n 1 , .., n k } [ T ] .. t 1 t k t 1 t k
Deterministic Tree Types Syntax T ::= {} | n [ T ] | T + T | T | T | ~ T | X | ! \{ n 1 , .., n k } [ T ] | * \{ n 1 , .., n k } [ T ] Example: hd [ True ]+ tl [ True ] hd tl
Deterministic Tree Types Syntax T ::= {} | n [ T ] | T + T | T | T | ~ T | X | ! \{ n 1 , .., n k } [ T ] | * \{ n 1 , .., n k } [ T ] Example: {}| ( hd [ True ]+ tl [ True ] ) hd tl or
Deterministic Tree Types Syntax T ::= {} | n [ T ] | T + T | T | T | ~ T | X | ! \{ n 1 , .., n k } [ T ] | * \{ n 1 , .., n k } [ T ] Example: X = {}| ( hd [ True ]+ tl [ X ] ) hd tl hd or tl hd tl
Deterministic Tree Types Syntax T ::= {} | n [ T ] | T + T | T | T | ~ T | X | ! \{ n 1 , .., n k } [ T ] | * \{ n 1 , .., n k } [ T ] Example: ! [ True ]+ ! [ True ]
Deterministic Tree Types Syntax T ::= {} | n [ T ] | T + T | T | T | ~ T | X | ! \{ n 1 , .., n k } [ T ] | * \{ n 1 , .., n k } [ T ] Example: ~ (! [ True ]+ ! [ True ] ) or or or ... Can eliminate negations, and use direct algorithms, but types get large...
Sheaves Formulas Formulas S = φ ( x 0 , .., x k ) , where φ is a Presburger formula [ r 0 [ S 0 ] , .., r k [ S k ]] and r i a set of names. [Dal Zilio, Lugiez, Meyssonnier, POPL ’04]
Sheaves Formulas Formulas S = φ ( x 0 , .., x k ) , where φ is a Presburger formula [ r 0 [ S 0 ] , .., r k [ S k ]] and r i a set of names. φ ( x 0 , x 1 ) , 0 0 [ b [True] , { a , c } [True]] a c b
Sheaves Formulas Formulas S = φ ( x 0 , .., x k ) , where φ is a Presburger formula [ r 0 [ S 0 ] , .., r k [ S k ]] and r i a set of names. φ ( x 0 , x 1 ) , 0 1 [ b [True] , { a , c } [True]] a c b
Sheaves Formulas Formulas S = φ ( x 0 , .., x k ) , where φ is a Presburger formula [ r 0 [ S 0 ] , .., r k [ S k ]] and r i a set of names. φ ( x 0 , x 1 ) , 1 1 [ b [True] , { a , c } [True]] a c b
Sheaves Formulas Formulas S = φ ( x 0 , .., x k ) , where φ is a Presburger formula [ r 0 [ S 0 ] , .., r k [ S k ]] and r i a set of names. φ ( x 0 , x 1 ) , 1 2 [ b [True] , { a , c } [True]] a c b
Sheaves Formulas Formulas S = φ ( x 0 , .., x k ) , where φ is a Presburger formula [ r 0 [ S 0 ] , .., r k [ S k ]] and r i a set of names. φ ( x 0 , x 1 ) , 1 2 [ b [True] , { a , c } [True]] ? | = φ (1 , 2)
Sheaves Formulas Formulas S = φ ( x 0 , .., x k ) , where φ is a Presburger formula [ r 0 [ S 0 ] , .., r k [ S k ]] and r i a set of names. φ ( x 0 , x 1 , x 2 ) , � � b [True] , { a , c } [True] , { a , b , c } [True] For coherence: r i [ S i ] must partition set of atoms. Note: does not ensure determinism.
Examples as Sheaves Formulas X = ( {}|hd[ True ]+tl[ X ] ) ( x 0 = x 1 = x 2 = x 3 =0) ∨ ( x 0 = x 1 =1 ∧ x 2 = x 3 =0) , X = � � hd [True] , tl [ X ] , tl [ ¬ X ] , { hd , tl } [True]
Examples as Sheaves Formulas X = ( {}|hd[ True ]+tl[ X ] ) ( x 0 = x 1 = x 2 = x 3 =0) ∨ ( x 0 = x 1 =1 ∧ x 2 = x 3 =0) , X = � � hd [True] , tl [ X ] , tl [ ¬ X ] , { hd , tl } [True] ~ (! [ True ]+ ! [ True ] ) x 0 � = 2 , � � {} [True]
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.
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.
Incremental Algorithm φ ( x 0 , .., x k ) , .. 0 0 0 [ r 0 [ S 0 ] , .. r k [ S k ]] .. n 1 n 2 n k − 1 n k ..
Incremental Algorithm φ ( x 0 , .., x k ) , ( φ ) [ r 0 [ S 0 ] , .. r k [ S k ]] .. n 1 n 2 n k − 1 n k ..
Incremental Algorithm φ ( x 0 , .., x k ) , ( φ ∧ ψ dom ) [ r 0 [ S 0 ] , .. r k [ S k ]] .. n 1 n 2 n k − 1 n k ..
Incremental Algorithm φ ( x 0 , .., x k ) , ( φ ∧ ψ dom ∧ ψ 1 ) [ r 0 [ S 0 ] , .. r k [ S k ]] .. n 1 n 2 n k − 1 n k ..
Incremental Algorithm φ ( x 0 , .., x k ) , ( φ ∧ ψ dom ∧ ψ 1 ∧ ψ 2 ) [ r 0 [ S 0 ] , .. r k [ S k ]] .. n 1 n 2 n k − 1 n k ..
Incremental Algorithm φ ( x 0 , .., x k ) , ( φ ∧ ψ dom ∧ ψ 1 ∧ .. ∧ ψ k − 1 ) [ r 0 [ S 0 ] , .. r k [ S k ]] .. n 1 n 2 n k − 1 n k ..
Incremental Algorithm φ ( x 0 , .., x k ) , ( φ ∧ ψ dom ∧ ψ 1 ∧ .. ∧ ψ k ) [ r 0 [ S 0 ] , .. r k [ S k ]] .. n 1 n 2 n k − 1 n k ..
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.
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.
Experiments: Address Book Validator base base-memo 150 100 Time(seconds) 50 0 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 Input Size (# lines) States Formulas Sat Trees 312 107711 99.8% 25744 99.9% 107711 42.1%
Experiments: Address Book Validator base base-memo incr-all-off 150 incr 100 Time(seconds) 50 0 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 Input Size (# lines) States Formulas Sat Trees 312 107711 99.8% 25744 99.9% 107711 42.1%
Experiments: Address Book Validator base base-memo incr-all-off 150 incr-phi-off incr-member-off incr 100 Time(seconds) 50 0 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 Input Size (# lines) States Formulas Sat Trees 312 107711 99.8% 25744 99.9% 107711 42.1%
Recommend
More recommend