1 of 31

Seeing the World through Lenses

1

Li-yao Xia, PLClub, March 4, 2022

2 of 31

Paleontological Sights

2

?

Lensosaurus

Subtyping

Records

Bidir. Transf.

Interacting systems

Laziness

Machine learning

Common trait: Lenses

3 of 31

Records

3

4 of 31

Record Updates

4

r = { count = 0 ; … }

Record

r’ = { r with count = 1 }

Set a field to a constant

Modify a field

r’ = { r with count = r.count + 1 }

Can we generalize?

r.count = f(r.count)

r.count += 1

In an imperative language

Use references/pointers

loc = &r.count

*loc = f(*loc)

What about pure functional languages?

5 of 31

Record Updates with Lenses

5

r’ = { r with count = f(r.count) }

r’ = r & count %~ f

𝜌’ = { 𝜌 with a = { 𝜌.a with x = f(𝜌.a.x) } }

𝜌’ = 𝜌 & (a . x) %~ f

𝜌 = { a = { x = 0 ; y = 1 }

; … }

_ & _ %~ _ : R → (R ⥂ A) → (A → A) → R

count : { count : ℕ ; … } ⥂ ℕ

_ . _ : (A ⥂ B) →(B ⥂ C) → (A ⥂ C)

a : { a : T ; … } ⥂ T

x : { x : T ; … } ⥂ T

Modify a field

Modify a nested field

6 of 31

Lenses

6

l : A ⥂ B

l.get : A → B

l.put : A × B → A

Lens laws:

(get-put) l.put(x, l.get(x)) = x

(put-get) l.get(l.put(x,y)) = y

x

y

l.get(x)

l.get

l.put(x,y)

l.put

l.get

l.put

Lens l from A to B

Composition:

(A ⥂ B) → (B ⥂ C) → (A ⥂ C)

(A ⥂ B) → (A’ ⥂ B’) → (A × A’ ⥂ B × B’)

Also put-put, omitted

7 of 31

Subtyping

7

8 of 31

Subtyping

8

S is a subtype of T

“S is a subset of T”

“S implements (the fields of) T”

{loc : Coords, name : String}

< {name : String}

ℕ ⊆ ℚ

1 / 3

ℚ → ℚ → ℚ

ℕ

s : {name : String} ⊢ s.name : String

s : {loc : Coords, name : String}

⊢ s.name : String

9 of 31

Typing Record Updates

9

incr = λs. { s with count = s.count + 1 }

incr : { count : ℕ } → { count : ℕ }

incr : { count : ℕ, color : ℂ } → { count : ℕ, color : ℂ }

incr : ∀(T < { count : ℕ }), T → T

What’s the type of incr?

Too specific

With “subtyping as coercion” (T → { count : ℕ }): only inhabited by λx. x

With “subtyping as lens” (T ⥂ { count : ℕ }): can type incr

(Other solution:

row polymorphism)

10 of 31

Covariant Arrays

10

squids : Array(Squid)

octopus : Octopus

squids : Array(Animal)

octopus : Animal

squids[0] ← octopus;

x : Array(T), y : T

x[0] ← y : Unit

Before: 🦑🦑🦑. After: 🐙🦑🦑.

Animal

Octopus

🐙

Squid

🦑

S < T

Array(S) < Array(T)

Broke type safety!

_[_] ← _ : Array(T) → ℕ → T → Unit

contravariant

11 of 31

Covariant Arrays, Electric Boogaloo

11

squids[0] ← octopus;

octopus = 🐙 : Octopus

squids = : Array(Squid)

x[0] ← y

heterogeneous array update

putSquid ⥂ Animal

“squid-ify”

: Animal

getOctopus ⥂ Animal

x[0] ← y

homogeneous array update

: Array(Animal)

getSquid ⥂ Animal

“de-squid-ify”

S ⥂ T

Array(S) ⥂ Array(T)

Subtyping (<) = Lenses (⥂)

12 of 31

Mutable Arrays

12

Abstract type

A : Type

get : ℕ → A → E

set : ℕ → A → E → A

Type of elements E : Type

What is an array?

aka. Array(E) = ∃(A:Type), A × (ℕ → A ⥂ E)

E ⥂ F → A ⥂ E → A ⥂ F

… by lens composition…

E ⥂ F → Array(E) → Array(F)

Array is a functor…

Lens: A ⥂ E

as Lenses

13 of 31

Lenses as Purely Functional References

13

Lenses

to access

Mutable

Functions

data

Immutable

14 of 31

Bidirectional Transformations

14

15 of 31

Lenses as Bidirectional Transformations

15

get

put

put

get

Update Pat

Delete Chris

Add Jo

Update Pat

Delete Chris

Add Jo

Default field

16 of 31

16

Bidirectional Transformations†

(aka. BX)

Optics

Complement Optics

= Profunctor Optics*

Lenses

get : A → B

put : A × B → A

A ← B

A + B ← A

Prisms

This talk

Databases (view-update)

GUI

Structured editing

Exact printing

17 of 31

17

Lenses

Bidirectional

Transformations

Subtyping

Records

Laziness

Interacting

Systems

Machine Learning

Lenses and

the World

?

18 of 31

Theory of Interaction

18

19 of 31

IO Machines

19

output : S → O

input : S × I → S

Lens, with I = O…

Lawless.

output : S → O

input : (S → S) ← I

Let’s generalize: model the dependencies between…

states and transitions

outputs and inputs

S

O

I

Moore machines = no dependencies

20 of 31

Generalized Lenses

20

context

state

view

question

action

transition

update

answer

Interface (C,A):

a space of contexts C;

for each context c : C, a space of actions A(c).

Lens (f,f’) : (C,A) ⥂ (D,B):

f : C → D

f’ : ∀c, A(c) ← B(f(c))

“space” = set with “structure”

e.g., monoid, category, order, vector space, topology

l : S ⥂ T

l.get : S → T

l.put : S × T → S

Classical lenses as special case with

C = S, A(s) = S, D = T, B(t) = T

“→” is a non-dependent “∀”

∀c : C, X → Y = C → X → Y

Laws = “structure-preserving map”

+ pointed structure

for the laws

21 of 31

Abstract Nonsense

21

Bundle (C,A):

a space of points C;

for each point c : C, a space of fibers A(c).

Bundle morphism (f,f’) : (C,A) ⥂ (D,B):

f : C → D

f’ : ∀c, A(c) ← B(f(c))

with a twist

22 of 31

Continuous Dynamical Systems

22

Water levels (L) → Output flows (O)

Change rates (∂tL) ← Input flows (I)

I1

I2

O1

O2

L1

L2

F

F

⊗

;

L1

∂tL1

O1×F

I1

L2

∂tL2

O2

F×I2

L1×L2

∂t(L1×L2)

O1×F×O2

I1×F×I2

F

F

1

1

L1×L2

∂t(L1×L2)

O1×O2

I1×I2

Monoidal category of generalized lenses

with “semi-duals”

23 of 31

A Lens by any other name

23

Generalized Lenses

(f,f’) : (C,A) ⥂ (D,B):

f : C → D

f’ : ∀c, A(c) ← B(f(c))

Geometry

Interaction

“Grothendieck Construction”

The Next 700 Bidirectional PLs

24 of 31

24

Big source

Small image

Local measurement

Remote measurement

Lenses are lenses

25 of 31

Machine Learning

25

through PL-tinted lenses

26 of 31

Gradient Descent

26

x

f(x)

y = f(x)-𝛼·∇f(x)2

⋁f(x,y)

= x-𝛼·∇f(x)

f : Rn → R

⋁f : Rn × R → Rn

Laws

(get-put) ⋁f(x,f(x)) = x

(put-get) f(⋁f(x,y)) ≈ y

(for y ≈ f(x))

f

Minimize f

λξ. f(x)+(ξ-x)·∇f(x)

Gradient repackaged as a put

27 of 31

Learners as Lenses

27

🧑

✔️

❌

Exam

👨‍🎓

Feedback

Make-up exam

put-get

↕

we learn from our mistakes

28 of 31

Laziness

28

29 of 31

Laziness

29

append (1:2:[], 3:[]) = 1:2:3:[]

​

append (1:2:[], 3:⊥) = 1:2:3:⊥

​

append (1:2:[], ⊥) = 1:2:⊥

append (1:2:⊥, ⊥) = 1:2:⊥

append (1:2:⊥, 3:[]) = 1:2:⊥

append’(1:2:[], 3:[]) (1:2:3:[]) = (1:2:[], 3:[])

​

append’(1:2:[], 3:[])(1:2:3:⊥) = (1:2:[], 3:⊥)

​

append’(1:2:[], 3:[])(1:2:⊥) = (1:2:⊥, ⊥)

1:2:⊥

⊑

(1:2:[], 3:[])

1:2:3:[]

append

append

(1:2:⊥, ⊥)

⊑

append’

Tell me how much output you want…

… and I’ll tell you how much input I need

x:y ⊑ x’:y’

x⊑x’

y⊑y’

⊥ ⊑ x

[] ⊑ []

⊑ order

30 of 31

Laziness

30

f : A → B

f’ : A × B ⇀ A

Laws:

(get-put) f’(x, f(x)) ⊑ x

(put-get) y ⊑ f x → y ⊑ f (f’(x,y))

Demand function for f

y

⊑

x

f(x)

f

f’(x,y)

⊑

f’

f

f(f’(x,y))

⊑

Lazy functions are Galois connections (adjunctions)

f’

⊑

f’(x, f(x))

f’(x,y) defined if y ⊑ f(x)

31 of 31

Seeing the World through Lenses

31

“The earliest discovery of lenses (that I know of) was by Kurt Gödel in 1958, as part of his dialectica interpretation.”

Galois Connection

Grothendieck Construction

Geometry

Interaction

The Next 700 Bidirectional PLs