shattered lens
play

Shattered lens Oleg Grenrus MSFP 2020 online 2020-09-01 The - PowerPoint PPT Presentation

Shattered lens Oleg Grenrus MSFP 2020 online 2020-09-01 The record update problem Lenses are very powerful and useful things in FP. They represent first class fields for a data structure. If we have a User , which has a Name , which


  1. Shattered lens Oleg Grenrus MSFP 2020 · online · 2020-09-01

  2. The record update problem Lenses are very powerful and useful things in FP. They represent “first class” fields for a data structure. If we have a User , which has a Name , which consists of first name (a String ) and some other data: data User = MkUser { userName :: Name , ... } data Name = MkName { firstName :: String , ... } then to update user ’ s fi rst name we need to see some trouble.

  3. Lenses offer a solution If we use lenses userNameL :: Lens User Name firstNameL :: Lens Name String which compose userNameL % firstNameL :: Lens User String Then using set operation set :: Lens a b -> b -> a -> a we can set new first name concisely: setFirstName :: String -> User -> User setFirstName = set ( userNameL % firstNameL )

  4. Proving facts about lenses is not simple. We usually need functional extensionality and some proof irrelevance . Homotopy Type Theory (HoTT) gives a new perspective to look at things. And Cubical Agda allows to play with the ideas.

  5. Lenses are not the only optics out there

  6. Isomorphisms

  7. Isomorphism A function f : A → B is an isomorphism , if there exists g : B → A , such that f ◦ g = 1 B and g ◦ f = 1 A . It is an easy exercise to show that inverse g is unique if it exists, so we don’t need to require that explicitly. However, together with equality proofs, a quasi-inverse of f � qinv f := ( f ◦ g = 1 B ) × ( g ◦ f = 1 A ) g : B → A is not unique (we don ’ t have U I P).

  8. Equivalence A function f : A → B is an equivalence if for all b : B the fibers of f over b are contractible. � isEquiv f := isContr ( fiber f b ) b : B � A ≃ B := isEquiv f f : A → B where � � isContr A := x = A y “exists unique” x : A y : A � fiber f b := f b = A a preimage of a point a : A

  9. Mere proposition For all A , isEquiv A is a mere proposition , which means that all values of isEquiv A are equal. For example proving that composition of equivalences is associative reduces to proving that function composition is associative. compEquiv-assoc : { ab : A ≃ B } → { bc : B ≃ C } → { cd : C ≃ D } → compEquiv ( compEquiv ab bc ) cd ≡ compEquiv ab ( compEquiv bc cd ) compEquiv-assoc = Σ Prop ≡ isPropIsEquiv refl Note: mere propositions are normal Types, they are not in a proof-irrelevant universe.

  10. Prisms

  11. Prisms A prism from S to V consists of ◮ a matcher f : S → Maybe V and ◮ a builder g : V → S satisfying following laws: ∀ ( v : V ) , f ( g v ) = just v M ATCH B UILD ∀ ( s : S ) , f s = just v ⇒ g v = s B UILD M ATCH

  12. Counting prisms between finite sets [ ( f , g ) | f <- gen , g <- gen , and [ f ( g v ) == Just v | v <- gen ] , and [ g v == s | s <- gen , Just v <- [f s ] ] ] Prisms between Fin 4 and Fin 2:

  13. Counting prisms between finite sets [ ( f , g ) | f <- gen , g <- gen , and [ f ( g v ) == Just v | v <- gen ] , and [ g v == s | s <- gen , Just v <- [f s ] ] ] Prisms between Fin 4 and Fin 2: There are 12 = 4 ! / ( 4 − 2 )! .

  14. Embedding The HoTT version of injection is an embedding . A function f : A → B is an embedding if for all b : B the fibres of f over b are mere propositions. � hasPropFibers f := isProp ( fiber f b ) b : B where � � isProp A := x = A y x : A y : A

  15. Decidable Embedding The isProp value tells us only that it the value is unique if it exists, but it doesn’t give any means to construct it! We need something stronger. Using isDecProp A := isProp A × ( A + ¬ A ) isDecProp A := isContr A + ¬ A or we can define � isBuilder g := isDecProp ( fiber g b ) b : B

  16. Corollaries ◮ Every equivalence is a builder ◮ Builder composition is associative, . . . ◮ Builder uniquely determines the matcher part of a prism.

  17. Corollaries ◮ Every equivalence is a builder ◮ Builder composition is associative, . . . ◮ Builder uniquely determines the matcher part of a prism. A Prism is just a decidable embedding .

  18. Lenses

  19. Lenses A lens from S to V consists of ◮ a getter f : S → V and ◮ a setter g : S → V → S satisfying following laws ∀ ( s : S ) ( v : V ) , f ( g s v ) = v G ET P UT ∀ ( s : S ) , g s ( f s ) = s P UT G ET ∀ ( s : S ) ( v : V ) ( v ′ : V ) , g ( g s v ′ ) v = g s v P UT P UT

  20. Higher lenses We can have different lens variants: ◮ barely behaving lens (G ET P UT ) ◮ well behaved lens (G ET P UT + P UT G ET ) ◮ very well behaved lens (G ET P UT + P UT G ET + P UT P UT ) ◮ weak lens (G ET P UT and weaker notion of P UT G ET and P UT P UT )

  21. Counting lenses between finite sets Let’s take as a getter fst : Fin 2 × Fin 2 → Fin 2

  22. Counting lenses between finite sets Let’s take as a getter fst : Fin 2 × Fin 2 → Fin 2 ◮ Out of 4 4 × 2 = 66536 setter candidates:

  23. Counting lenses between finite sets Let’s take as a getter fst : Fin 2 × Fin 2 → Fin 2 ◮ Out of 4 4 × 2 = 66536 setter candidates: ◮ 256 barely behaving lenses. � � isBareLens f := ( fiber f v 1 → fiber f v 2 ) v 1 : V v 2 : V

  24. Counting lenses between finite sets Let’s take as a getter fst : Fin 2 × Fin 2 → Fin 2 ◮ Out of 4 4 × 2 = 66536 setter candidates: ◮ 256 barely behaving lenses. � � isBareLens f := ( fiber f v 1 → fiber f v 2 ) v 1 : V v 2 : V � � � � = ( f s 1 = v 1 ) → ( f s 2 = v 2 ) v 1 : V v 2 : V s 1 : S s 2 : S

  25. Counting lenses between finite sets Let’s take as a getter fst : Fin 2 × Fin 2 → Fin 2 ◮ Out of 4 4 × 2 = 66536 setter candidates: ◮ 256 barely behaving lenses. � � isBareLens f := ( fiber f v 1 → fiber f v 2 ) v 1 : V v 2 : V � � � � = ( f s 1 = v 1 ) → ( f s 2 = v 2 ) v 1 : V v 2 : V s 1 : S s 2 : S ≈ V → S → S

  26. Counting lenses between finite sets Let’s take as a getter fst : Fin 2 × Fin 2 → Fin 2 ◮ Out of 4 4 × 2 = 66536 setter candidates: ◮ 256 barely behaving lenses. ◮ 16 weak lenses. � � isWeakLens f := ( fiber f v 1 ≃ fiber f v 2 ) v 1 : V v 2 : V .

  27. Counting lenses between finite sets Let’s take as a getter fst : Fin 2 × Fin 2 → Fin 2 ◮ Out of 4 4 × 2 = 66536 setter candidates: ◮ 256 barely behaving lenses. ◮ 16 weak lenses. � � isWeakLens f := ( fiber f v 1 ≃ fiber f v 2 ) v 1 : V v 2 : V ◮ Four equivalences: fiber fst 0 2 ≃ fiber fst 0 2 . . . ◮ Two options: id and not for each ◮ In total 2 4 = 16. .

  28. Counting lenses between finite sets Let’s take as a getter fst : Fin 2 × Fin 2 → Fin 2 ◮ Out of 4 4 × 2 = 66536 setter candidates: ◮ 256 barely behaving lenses. ◮ 16 weak lenses. � � isWeakLens f := ( fiber f v 1 ≃ fiber f v 2 ) v 1 : V v 2 : V . Weak P UT G ET law s �→ g s ( f s ) is an equivalence

  29. Counting lenses between finite sets Let’s take as a getter fst : Fin 2 × Fin 2 → Fin 2 ◮ Out of 4 4 × 2 = 66536 setter candidates: ◮ 256 barely behaving lenses. ◮ 16 weak lenses. ◮ 16 well behaving lenses. G ET P UT and P UT G ET

  30. Counting lenses between finite sets Let’s take as a getter fst : Fin 2 × Fin 2 → Fin 2 ◮ Out of 4 4 × 2 = 66536 setter candidates: ◮ 256 barely behaving lenses. ◮ 16 weak lenses. ◮ 16 well behaving lenses. ◮ Two very well-behaved lenses: g ( x , y ) v = ( v , y ) and g ( x , y ) v = ( v , y ‘ xor ‘ x ‘ xor ‘ v ) � � isHigherLens f := ( fiber f v ≃ P | v | ) P : � V �→ Type v : V

  31. Counting lenses between finite sets Let’s take as a getter fst : Fin 2 × Fin 2 → Fin 2 ◮ Out of 4 4 × 2 = 66536 setter candidates: ◮ 256 barely behaving lenses. ◮ 16 weak lenses. ◮ 16 well behaving lenses. ◮ Two very well-behaved lenses: � � isHigherLens f := ( fiber f v ≃ P | v | ) P : � V �→ Type v : V Another problem: Multiple values for the same setter: ( const Bool , λ v . . . • idEquiv ) and ( const Bool , λ v . . . • notEquiv ) .

  32. Summary ◮ Prisms are simply decidable embeddings ◮ isDecProp is a useful concept in programming. For example dec- ≤ : � � m : N isDecProp ( n ≤ m ) and n : N ( <=? ) :: Natural -> Natural -> Maybe Natural ◮ No satisfying results for lenses ◮ Getter doesn’t determine whole lens. ◮ isHigherLens has a "degree of freedom" ◮ Are weak lenses useful?

  33. Extra slides: hasGetter For g : S → V → S : � hasGetter g := isContr ( fiber g s s ) s : S

Recommend


More recommend