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

shattered lens
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Shattered lens

Oleg Grenrus

MSFP 2020 · online · 2020-09-01

slide-2
SLIDE 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 first name we need to see some trouble.

slide-3
SLIDE 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)

slide-4
SLIDE 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.

slide-5
SLIDE 5

Lenses are not the only optics out there

slide-6
SLIDE 6

Isomorphisms

slide-7
SLIDE 7

Isomorphism

A function f : A → B is an isomorphism, if there exists g : B → A, such that f ◦ g = 1B and g ◦ f = 1A. 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 :=

  • g:B→A

(f ◦ g = 1B) × (g ◦ f = 1A)

is not unique (we don’t have UIP).

slide-8
SLIDE 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 :=

  • b:B

isContr (fiberf b) A ≃ B :=

  • f:A→B

isEquiv f where isContr A :=

  • x:A
  • y:A

x =A y “exists unique” fiberf b :=

  • a:A

f b =A a preimage of a point

slide-9
SLIDE 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.

slide-10
SLIDE 10

Prisms

slide-11
SLIDE 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: MATCHBUILD

∀(v : V), f (g v) = just v

BUILDMATCH

∀(s : S), f s = just v ⇒ g v = s

slide-12
SLIDE 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:

slide-13
SLIDE 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)!.

slide-14
SLIDE 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

  • ver b are mere propositions.

hasPropFibers f :=

  • b:B

isProp (fiberf b) where isProp A :=

  • x:A
  • y:A

x =A y

slide-15
SLIDE 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)

  • r

isDecProp A := isContr A + ¬A we can define isBuilder g :=

  • b:B

isDecProp (fiberg b)

slide-16
SLIDE 16

Corollaries

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

slide-17
SLIDE 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.

slide-18
SLIDE 18

Lenses

slide-19
SLIDE 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 GETPUT

∀(s : S) (v : V), f (g s v) = v

PUTGET

∀(s : S), g s (f s) = s

PUTPUT

∀(s : S) (v : V) (v′ : V), g (g s v′) v = g s v

slide-20
SLIDE 20

Higher lenses

We can have different lens variants:

◮ barely behaving lens (GETPUT) ◮ well behaved lens (GETPUT + PUTGET) ◮ very well behaved lens (GETPUT + PUTGET + PUTPUT) ◮ weak lens (GETPUT and weaker notion of PUTGET and

PUTPUT)

slide-21
SLIDE 21

Counting lenses between finite sets

Let’s take as a getter fst : Fin 2 × Fin 2 → Fin 2

slide-22
SLIDE 22

Counting lenses between finite sets

Let’s take as a getter fst : Fin 2 × Fin 2 → Fin 2

◮ Out of 44×2 = 66536 setter candidates:

slide-23
SLIDE 23

Counting lenses between finite sets

Let’s take as a getter fst : Fin 2 × Fin 2 → Fin 2

◮ Out of 44×2 = 66536 setter candidates: ◮ 256 barely behaving lenses.

isBareLens f :=

  • v1:V
  • v2:V

(fiberf v1 → fiberf v2)

slide-24
SLIDE 24

Counting lenses between finite sets

Let’s take as a getter fst : Fin 2 × Fin 2 → Fin 2

◮ Out of 44×2 = 66536 setter candidates: ◮ 256 barely behaving lenses.

isBareLens f :=

  • v1:V
  • v2:V

(fiberf v1 → fiberf v2) =

  • v1:V
  • v2:V
  • s1:S

(f s1 = v1) →

  • s2:S

(f s2 = v2)

slide-25
SLIDE 25

Counting lenses between finite sets

Let’s take as a getter fst : Fin 2 × Fin 2 → Fin 2

◮ Out of 44×2 = 66536 setter candidates: ◮ 256 barely behaving lenses.

isBareLens f :=

  • v1:V
  • v2:V

(fiberf v1 → fiberf v2) =

  • v1:V
  • v2:V
  • s1:S

(f s1 = v1) →

  • s2:S

(f s2 = v2) ≈ V → S → S

slide-26
SLIDE 26

Counting lenses between finite sets

Let’s take as a getter fst : Fin 2 × Fin 2 → Fin 2

◮ Out of 44×2 = 66536 setter candidates: ◮ 256 barely behaving lenses. ◮ 16 weak lenses.

isWeakLens f :=

  • v1:V
  • v2:V

(fiberf v1 ≃ fiberf v2)

.

slide-27
SLIDE 27

Counting lenses between finite sets

Let’s take as a getter fst : Fin 2 × Fin 2 → Fin 2

◮ Out of 44×2 = 66536 setter candidates: ◮ 256 barely behaving lenses. ◮ 16 weak lenses.

isWeakLens f :=

  • v1:V
  • v2:V

(fiberf v1 ≃ fiberf v2)

◮ Four equivalences: fiberfst 02 ≃ fiberfst 02. . . ◮ Two options: id and not for each ◮ In total 24 = 16.

.

slide-28
SLIDE 28

Counting lenses between finite sets

Let’s take as a getter fst : Fin 2 × Fin 2 → Fin 2

◮ Out of 44×2 = 66536 setter candidates: ◮ 256 barely behaving lenses. ◮ 16 weak lenses.

isWeakLens f :=

  • v1:V
  • v2:V

(fiberf v1 ≃ fiberf v2)

.

Weak PUTGET law

s → g s (f s) is an equivalence

slide-29
SLIDE 29

Counting lenses between finite sets

Let’s take as a getter fst : Fin 2 × Fin 2 → Fin 2

◮ Out of 44×2 = 66536 setter candidates: ◮ 256 barely behaving lenses. ◮ 16 weak lenses. ◮ 16 well behaving lenses. GETPUT and PUTGET

slide-30
SLIDE 30

Counting lenses between finite sets

Let’s take as a getter fst : Fin 2 × Fin 2 → Fin 2

◮ Out of 44×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 :=

  • P:V→Type
  • v:V

(fiberf v ≃ P|v|)

slide-31
SLIDE 31

Counting lenses between finite sets

Let’s take as a getter fst : Fin 2 × Fin 2 → Fin 2

◮ Out of 44×2 = 66536 setter candidates: ◮ 256 barely behaving lenses. ◮ 16 weak lenses. ◮ 16 well behaving lenses. ◮ Two very well-behaved lenses:

isHigherLens f :=

  • P:V→Type
  • v:V

(fiberf v ≃ P|v|)

Another problem: Multiple values for the same setter:

(const Bool, λv . . . • idEquiv) and (const Bool, λv . . . • notEquiv).

slide-32
SLIDE 32

Summary

◮ Prisms are simply decidable embeddings

◮ isDecProp is a useful concept in programming. For example

dec-≤ :

n:N

  • m:N isDecProp (n ≤ m) and

(<=?) :: 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?

slide-33
SLIDE 33

Extra slides: hasGetter

For g : S → V → S: hasGetter g :=

  • s:S

isContr (fiberg s s)