functional logic semantic bidirectionalization for free
play

Functional Logic Semantic Bidirectionalization for Free! Hugo - PowerPoint PPT Presentation

Functional Logic Semantic Bidirectionalization for Free! Hugo Pacheco HASLab INESC TEC & Universidade do Minho, Braga, Portugal FATBIT/SSaaPP Workshop Braga - September 18th 2012 Towards Functional Logic Semantic Bidirectionalization for


  1. Functional Logic Semantic Bidirectionalization for Free! Hugo Pacheco HASLab INESC TEC & Universidade do Minho, Braga, Portugal FATBIT/SSaaPP Workshop Braga - September 18th 2012

  2. Towards Functional Logic Semantic Bidirectionalization for Free! Hugo Pacheco HASLab INESC TEC & Universidade do Minho, Braga, Portugal FATBIT/SSaaPP Workshop Braga - September 18th 2012

  3. BXs and Lenses lenses are one of the most popular BX frameworks get S V S V put existing lens systems vary on the bidirectionalization approach how is a lens derived from a specification? Functional Logic Semantic Bidirectionalization for Free! 2 / 17 Hugo Pacheco

  4. Functional Semantic Bidirectionalization Voigtl¨ ander proposed the semantic bidirectionalization of Haskell functions [POPL’09] get S ! V ! derive S ! V ! put put via the polymorphic interpretatation of get limited expressiveness - only polymorphic get functions limited updatability - even for the mixed approach of Voigtl¨ ander et al. [ICFP’10] Functional Logic Semantic Bidirectionalization for Free! 3 / 17 Hugo Pacheco

  5. The Lens Laws PutGet law GetPut law put must translate put must preserve empty view updates. view updates exactly. s get s v s' v' put put get get ( put v ′ s ) ⊑ v ′ put ( get s ) s ⊑ s Functional Logic Semantic Bidirectionalization for Free! 4 / 17 Hugo Pacheco

  6. A Better GetPut Law? when the view is modified there are many source updates anything can happen! - no restriction on the permitted translations get s v view update S S ? v' put Functional Logic Semantic Bidirectionalization for Free! 5 / 17 Hugo Pacheco

  7. A Better GetPut Law? when the view is modified there are many source updates only “good” can happen - only minimal source updates are permitted get s v view update s ' v' put ∀ v ′ , s , s ′ . diff ( put v ′ s ) s � diff s ′ s PutDiff the differencing function depends on the source type diff S : S → S → N Functional Logic Semantic Bidirectionalization for Free! 5 / 17 Hugo Pacheco

  8. Functional Logic Semantic Bidirectionalization Idea: use Curry, a functional logic programming language, to compute such minimal updates functional programming: Haskell-like syntax logic programming: logic variables, built-in search (findall, best) get V S derive derive diff S : S → S → N derive S V put derive diff from the source type derive put from get and diff Functional Logic Semantic Bidirectionalization for Free! 6 / 17 Hugo Pacheco

  9. A diff for Algebraic Data Types a generic diff for algebraic data types Eelco Lempsink, Sean Leather and Andres L¨ oh Type-Safe Diff for Families of Datatypes Workshop on Generic Programming 2009 . we implement this diff in Curry diffND List : [ a ] → [ a ] → N diffND List [ ] [ ] = 0 diffND List [ ] ( y : ys ) = 1 + diffND List [ ] ys -- insert diffND List ( x : xs ) [ ] = 1 + diffND List xs [ ] -- delete diffND List ( x : xs ) ( y : ys ) | x =:= y = diffND List xs ys -- copy | x = / = y = 1 + diffND List ( x : xs ) ys -- insert ? 1 + diffND List xs ( y : ys ) -- delete diff List s ′ s = unpack $ head $ best ( λ n → diffND s ′ s =:= n ) ( � ) this diff calculates the sequence of insert, delete and copy operations with the minimal cost Functional Logic Semantic Bidirectionalization for Free! 7 / 17 Hugo Pacheco

  10. Curry Implementation we implement put in Curry as a non-deterministic function put :: V → S → S put v ′ s = putn n v ′ s =:= s ′ S v ′ s where n = diff ′ s ′ free S v ′ s = unpack $ head $ best diff ′ ( λ n → let s ′ free in get s ′ =:= v ′ & diffND S s ′ s =:= n ) putn :: N → V → S → S putn n v ′ s | get s ′ =:= v ′ & diff S s ′ s =:= n = s ′ where s ′ free 1 calculate the minimal difference between any new source (whose view is v ′ ) and the original source s 2 return any new source whose difference to the original source s is n Functional Logic Semantic Bidirectionalization for Free! 8 / 17 Hugo Pacheco

  11. Example 1 ( halve ) - First Attempt calculate the first half of a list get halve [1,2,3,4] [1,2] [5,2,1,6,2,3,4] [5,2,1,6] put halve get = halve halve :: [ a ] → [ a ] halve [ ] = [ ] halve ( x : xs ) = x : halve ′ xs xs where halve ′ xs [ ] = [ ] halve ′ xs [ y ] = [ ] halve ′ ( x : xs ) ( y : z : zs ) = x : halve ′ xs zs is this the best result? Functional Logic Semantic Bidirectionalization for Free! 9 / 17 Hugo Pacheco

  12. Calculating a View Complement view complement = source data not present in the view get ? put put should only recover data from the view complement Functional Logic Semantic Bidirectionalization for Free! 10 / 17 Hugo Pacheco

  13. Calculating a View Complement view complement = source data not present in the view get noncomplement free invert ? free put put should only recover data from the view complement how to calculate the complement in Curry? noncomplement S s = findfirst ( λ x → get x =:= get s & match S x s ) complement S s = invert S s ( noncomplement S s ) Functional Logic Semantic Bidirectionalization for Free! 10 / 17 Hugo Pacheco

  14. Calculating a View Complement we refine diff to take into account the source complement diffND List : [ a ] → [( a , a )] → N diffND List [ ] [ ] = 0 diffND List [ ] (( v , y ) : ys ) = insert diffND List ( x : xs ) [ ] = delete diffND List ( x : xs ) (( v , y ) : ys ) | x =:= y & isVar v =:= False = copy | x =:= y & isVar v =:= True = insert ? delete | x = / = y = insert ? delete diff List s ′ s = unpack $ head $ best ( λ n → diffND s ′ cs =:= n ) ( � ) where cs = zip ( complement s ) s we do not allow source data not in the complement to be copied Functional Logic Semantic Bidirectionalization for Free! 11 / 17 Hugo Pacheco

  15. Example 1 ( halve ) - Second Attempt calculate the complement of the source noncomplement List [ 1 , 2 , 3 , 4 ] = [ 1 , 2 , a , b ] complement List [ 1 , 2 , 3 , 4 ] = [ a , b , 3 , 4 ] calculate the first half of a list (revisited) get halve [1,2,3,4] [1,2] [5,2,1,6,_a,3,4] [5,2,1,6,3,_a,4] [5,2,1,6] [5,2,1,6,3,4,_a] put halve Functional Logic Semantic Bidirectionalization for Free! 12 / 17 Hugo Pacheco

  16. Example 2 ( length ) compute the length of a list get length [1,2,3] 3 [1,2] [1,3] 2 [2,3] put length get = length length :: [ a ] → N length [ ] = 0 length ( x : xs ) = 1 + length xs Functional Logic Semantic Bidirectionalization for Free! 13 / 17 Hugo Pacheco

  17. Example 3 ( append ) append two lists into a single list get append ([1,2],[3,4]) [1,2,3,4] ([0,1],[2,3,4,5]) ([0,1,2],[3,4,5]) [0,1,2,3,4,5] ([0,1,2,3],[4,5]) put append get = append append :: ([ a ] , [ a ]) → [ a ] append ([ ] , ys ) = ys append ( x : xs , ys ) = x : append ( xs , ys ) diff for pairs of lists diff List × List :: ([ a ] , [ a ]) → ([ a ] , [ a ]) → N Functional Logic Semantic Bidirectionalization for Free! 14 / 17 Hugo Pacheco

  18. Example 4 ( zip ) join the elements of two lists pair-wise into a single list get zip ([1,2,3],"abcd") [(1,'a'),(2,'b'),(3,'c')] ([1,2],['x','y',_a,'d']) [(1,'x'),(2,'y')] ([1,2],"xyd") put zip get = zip zip :: ([ a ] , [ b ]) → [( a , b )] zip ([ ] , ys ) = [ ] zip ( xs , [ ]) = [ ] zip ( x : xs , y : ys ) = ( x , y ) : zip ( xs , ys ) Functional Logic Semantic Bidirectionalization for Free! 15 / 17 Hugo Pacheco

  19. Example 5 ( lspine ) calculate the left spine of a binary tree get lspine 1 [1,2] 2 3 0 0 [0,1] 1 1 3 put lspine 3 data Tree a = Empty | Node a ( Tree a ) ( Tree a ) get = lspine lspine :: Tree a → [ a ] lspine Empty = [ ] lspine ( Node x l r ) = x : lspine l Functional Logic Semantic Bidirectionalization for Free! 16 / 17 Hugo Pacheco

  20. Conclusions a semantic bidirectionalization approach using Curry users define any get : S → V function, and we derive: a diff S function a non-deterministic put : V → S → S function that lazily returns the “best” new sources Scoreboard: Future Work: + expressivness automate the tool + updatability improve the reduction strategy for infinite search spaces (e.g. filter ) + properties improve diff – efficiency Functional Logic Semantic Bidirectionalization for Free! 17 / 17 Hugo Pacheco

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend