fun with string lenses
play

Fun With String Lenses Benjamin C. Pierce University of - PowerPoint PPT Presentation

Fun With String Lenses Benjamin C. Pierce University of Pennsylvania WG 2.8, July 2007 My usual obsession... The View Update Problem We transform source structure C to target structure A C A The View Update Problem We transform


  1. Fun With String Lenses Benjamin C. Pierce University of Pennsylvania WG 2.8, July 2007

  2. My usual obsession...

  3. The View Update Problem ◮ We transform source structure C to target structure A C A

  4. The View Update Problem ◮ We transform source structure C to target structure A ◮ Someone updates A C A update Updated A

  5. The View Update Problem ◮ We transform source structure C to target structure A ◮ Someone updates A ◮ We must now translate this update to obtain an appropriately updated C C A Updated Updated C A

  6. A Bad “Solution” We could just write such pairs of functions in any old programming language. ◮ But this would be ugly and unmaintainable!

  7. A Good Solution Better: take a linguistic approach. ◮ Design a bi-directional programming language, in which every expression can be read... ◮ from left to right as a get function ◮ from right to left as the corresponding put function Pieces of the puzzle: ◮ A semantic space of pairs of functions that “behave well together” (dubbed lenses) ◮ Natural, convenient syntax with a compositional semantics ◮ Static type system guaranteeing well-behavedness and totality

  8. Lenses For Trees [POPL 2005, PLANX 2007] Data model: Trees (XML, etc.) Computation model: Local tree manipulation combinators, plus mapping, conditionals, recursion. Type system: Based on regular tree automata ◮ with some interesting side-conditions

  9. Lenses For Relations [PODS 2006] Data model: Relational databases (named collections of tables) Computation model: Operators from relational algebra, each augmented with enough parameters to determine put behavior. Type system: Built using standard tools from databases ◮ predicates on rows of tables ◮ functional dependencies between columns

  10. Lenses for Strings [in progress] Data model: Strings over a finite alphabet Computation model: Finite-state string transducers, described using regular-expression-like operators Type system: Regular expressions ◮ with some interesting side conditions

  11. What We’re Up To 1. string lens combinators ◮ intuitive semantics and typing rules ◮ based on familiar regular operators (union, concatenation, Kleene-star). 2. dictionary lenses (and two more combinators) for dealing with ordered data 3. Boomerang : a full-blown bidirectional programming language 4. Pretty big examples ◮ e.g., SwissProt ascii ← → XML (2Kloc) Bottom line: Finally, a bi-directional language that is (pretty) easy to learn and (a lot of) fun to use.

  12. String Lenses

  13. Semantics of Basic Lenses A basic lens l from C to A is a triple of functions l . get ∈ C − → A ∈ A − → C − → C l . put l . create ∈ A − → C obeying three “round-tripping” laws: l . put ( l . get c ) c = c ( GetPut ) l . get ( l . put a c ) = a ( PutGet ) l . get ( l . create a ) = a ( CreateGet )

  14. [Switch to demo]

  15. String Lens Primitives

  16. Copy E ∈ R cp E ∈ [ [ E ] ] ⇐ ⇒ [ [ E ] ] = get c c put a c = a = create a a

  17. Const u ∈ Σ ∗ E ∈ R v ∈ [ [ E ] ] const E u v ∈ [ ] ⇐ ⇒ { u } [ E ] = get c u put a c = c create a = v

  18. Derived Forms E ↔ u ∈ ] ⇐ ⇒ { u } [ [ E ] E ↔ u = const E u ( choose ( E )) del E ∈ [ [ E ] ] ⇐ ⇒ { ǫ } = E ↔ ǫ del E ins u ∈ { ǫ } ⇐ ⇒ { u } = ǫ ↔ u ins u

  19. Concatenation C 1 · ! C 2 A 1 · ! A 2 l 1 ∈ C 1 ⇐ ⇒ A 1 l 2 ∈ C 2 ⇐ ⇒ A 2 l 1 · l 2 ∈ C 1 · C 2 ⇐ ⇒ A 1 · A 2 get ( c 1 · c 2 ) = ( l 1 . get c 1 ) · ( l 2 . get c 2 ) put ( a 1 · a 2 ) ( c 1 · c 2 ) = ( l 1 . put a 1 c 1 ) · ( l 2 . put a 2 c 2 ) create ( a 1 · a 2 ) = ( l 1 . create a 1 ) · ( l 2 . create a 2 )

  20. Iteration C ! ∗ A ! ∗ ∈ C ⇐ ⇒ A l l ∗ C ∗ ⇐ ⇒ A ∗ ∈ get ( c 1 ··· c n ) ( l . get c 1 ) ··· ( l . get c n ) = put ( a 1 ··· a n ) ( c 1 ··· c m ) = c ′ 1 ··· c ′ n � l . put a i c i i ∈ { 1 , ..., min( m , n ) } where c ′ i = l . create a i i ∈ { m + 1 , ..., n } create ( a 1 ··· a n ) = ( l . create a 1 ) ··· ( l . create a n )

  21. Union C 1 ∩ C 2 = ∅ ∈ C 1 ⇐ ⇒ A 1 ∈ C 2 ⇐ ⇒ A 2 l 1 l 2 l 1 | l 2 ∈ C 1 ∪ C 2 ⇐ ⇒ A 1 ∪ A 2 � if c ∈ C 1 l 1 . get c get c = l 2 . get c if c ∈ C 2  if c ∈ C 1 ∧ a ∈ A 1 l 1 . put a c    if c ∈ C 2 ∧ a ∈ A 2 l 2 . put a c  put a c = if c ∈ C 2 ∧ a ∈ A 1 \ A 2 l 1 . create a    if c ∈ C 1 ∧ a ∈ A 2 \ A 1  l 2 . create a � l 1 . create a if a ∈ A 1 create a = if a ∈ A 2 \ A 1 l 2 . create a

  22. [back to demo]

  23. Dictionary Lenses

  24. Semantics of Dictionary Lenses S , D l ∈ C ⇐ ⇒ A if... l . get ∈ C − → A l . parse ∈ C − → S × D l . key ∈ A − → K l . create ∈ A − → D − → C × D l . put ∈ A − → S × D − → C × D ...obeying... s , d ′ = l . parse c d ∈ D ( GetPut ) l . put ( l . get c ) ( s , ( d ′ + + d )) = c , d c , d ′ = l . put a ( s , d ) ( PutGet ) l . get c = a c , d ′ = l . create a d ( CreateGet ) l . get c = a

  25. Boomerang

  26. A full-blown language based on dictionary lenses ◮ A simply typed functional language with base types: ◮ string ◮ regexp ◮ dlens ◮ ... and primitives: get : dlens -> string -> string put : dlens -> string -> string -> string create : dlens -> string -> string union : dlens -> dlens -> dlens concat : dlens -> dlens -> dlens ...

  27. Two-stage typechecking Problem: ◮ Our lens combinators have types involving regular expressions ◮ The functional component of Boomerang involves arrow types ◮ Not clear how to mix them!

  28. Two-stage typechecking A pretty reasonable solution: ◮ Typecheck functional program (using simple types) ◮ Executing it involves applying operators like concat to dlens values ◮ dlens values include (functional) components get , put , etc., and (regular expression) components domain , codomain , etc. ◮ evaluating concat dynamically applies the static typing rule for lens concatenation (using ◮ if this succeeds, then the resulting dlens can be further composed, or applied to a string using get , etc.

  29. Thank You! Collaborators on this work: Aaron Bohannon, Nate Foster, Alexandre Pilkiewicz, Alan Schmitt Other Harmony contributors: Ravi Chugh, Malo Denielou, Michael Greenwald, Owen Gunden, Martin Hofmann, Sanjeev Khanna, Keshav Kunal, St´ ephane Lescuyer, Jon Moore, Jeff Vaughan, Zhe Yang Resources: Papers, slides, (open) source code, and online demos: http://www.seas.upenn.edu/ ∼ harmony/

  30. The Real Semantics of Dictionary Lenses A dictionary lens from C to A with skeleton type S and dictionary type D has components... l . get ∈ C − → A l . parse ∈ C − → S × D ( L ) l . key ∈ A − → K l . create ∈ A − → D ( L ) − → C × D ( L ) l . put ∈ A − → S × D ( L ) − → C × D ( L ) ... where... s , d ′ = l . parse c d ∈ D ( L ) ( GetPut ) l . put ( l . get c ) ( s , ( d ′ + + d )) = c , d c , d ′ = l . put a ( s , d ) ( PutGet ) l . get c = a c , d ′ = l . create a d ( CreateGet ) l . get c = a

Recommend


More recommend