Share
Embed
lens: from the ground up (in clojure)
A look at functional lenses in general, and how they would work in an uni-typed language like clojure. This talk walks through why lenses are useful, how to encode lenses, and builds up a small lens library in clojure.
The demonstration code is available at https://github.com/markhibberd/lens-talk-clojure (see lens/script.org src/*.clj)
This talk is derived from a more in-depth presentation into the haskell "lens" library: https://speakerdeck.com/markhibberd/lens-from-the-ground-up
Mark Hibberd
February 27, 2014
Transcript
50 slides
-
(defrecord Address [street city postcode]) ! (defrecord Person [name age
address]) ! (defrecord User [uid username identity password]) records -
(def home (->Address "road" "place" 4000)) ! (def mark (->Person
"mark" 99 home)) ! (def user (->User 3365 "mth" mark "6aefd2842be62cd470709b27aedc7db7")) records (defrecord Address [street city postcode]) ! (defrecord Person [name age address]) ! (defrecord User [uid username identity password]) -
! (def updated (update-in user [:identity] (fn [person] (update-in person
[:address] (fn [address] (update-in address [:postcode] #(+ 4 %))))))) records (defrecord Address [street city postcode]) ! (defrecord Person [name age address]) ! (defrecord User [uid username identity password]) -
! (update-in user [:identity :address :postcode] #(+ 2 %)) records
(defrecord Address [street city postcode]) ! (defrecord Person [name age address]) ! (defrecord User [uid username identity password]) Can we do better?! -
; get :: a -> b ; set :: a
-> b -> a ! (defrecord Lens [getter setter]) ! (defn lget [lens a] ((:getter lens) a)) ! (defn lset [lens a b] ((:setter lens) a b)) intuitions -
; get :: a -> b ; set :: a
-> b -> a ! (defrecord Lens [getter setter]) ! (defn lget [lens a] ((:getter lens) a)) ! (defn lset [lens a b] ((:setter lens) a b)) set-get ==> (get l (set l a b)) == b ! get-set ==> (set l a (get l a)) == a ! set-set ==> (set l (set l b a) c) == (set l a c) intuitions Pierce’s laws! -
but… ! (defn lmodify [lens a func] ((:setter lens) a
(func ((:getter lens) a)))) ! (defn lcompose [lens-a-b lens-b-c] (->Lens (fn [a] (lget lens-b-c (lget lens-a-b a))) (fn [a c] (lset lens-a-b a (lset lens-b-c (lget lens-a-b a) c))))) -
! type Lens’ a b = forall f. Functor f
=> (b -> f b) -> a -> f a back to the drawing board haskell -
! type Lens’ a b = forall f. Functor f
=> (b -> f b) -> a -> f a back to the drawing board haskell ! ! clojure -
! type Lens’ a b = forall f. Functor f
=> (b -> f b) -> a -> f a back to the drawing board ! (deffn set [lens a b] (“can we?”)) ! (deffn get [lens a] (“can we?”)) -
! (defprotocol Functor (fmap [functor f] "fmap :: f a
-> (a -> b) -> f b")) aside: what is a functor -
! (defprotocol Functor (fmap [functor f] "fmap :: f a
-> (a -> b) -> f b")) ! (defrecord List [wrapped] Functor (fmap [functor f] (List. (map f (:wrapped functor))))) aside: what is a functor -
! type Lens’ a b = forall f. Functor f
=> (b -> f b) -> a -> f a back to the drawing board (defrecord Id [runId] Functor (fmap [functor f] (Id. (f (:runId functor))))) ! (deffn -set [lens a] (???)) ! ! ! -
! type Lens’ a b = forall f. Functor f
=> (b -> f b) -> a -> f a back to the drawing board (defrecord Id [runId] Functor (fmap [functor f] (Id. (f (:runId functor))))) ! (deffn -set [lens a b] (let [b-ib (fn [bb] (->Id b)) -- b -> Id b ] (“???”))) -
! type Lens’ a b = forall f. Functor f
=> (b -> f b) -> a -> f a back to the drawing board (defrecord Id [runId] Functor (fmap [functor f] (Id. (f (:runId functor))))) ! (deffn -set [lens a b] (let [b-ib (fn [bb] (->Id b)) -- b -> Id b a-ia (lens b-ib) -- a -> Id a ] (“???”))) -
! type Lens’ a b = forall f. Functor f
=> (b -> f b) -> a -> f a back to the drawing board (defrecord Id [runId] Functor (fmap [functor f] (Id. (f (:runId functor))))) ! (deffn -set [lens a b] (let [b-ib (fn [bb] (->Id b)) -- b -> Id b a-ia (lens b-ib) -- a -> Id a ia (a-ia a)] -- Id a (“???”))) -
! type Lens’ a b = forall f. Functor f
=> (b -> f b) -> a -> f a back to the drawing board (defrecord Id [runId] Functor (fmap [functor f] (Id. (f (:runId functor))))) ! (deffn -set [lens a b] (let [b-ib (fn [bb] (->Id b)) -- b -> Id b a-ia (lens b-ib) -- a -> Id a ia (a-ia a)] -- Id a (:runId ia))) -- a -
! type Lens’ a b = forall f. Functor f
=> (b -> f b) -> a -> f a back to the drawing board (defrecord ??? [???] Functor (fmap [functor f] ???)) ! (deffn -get [lens a] (???)) ! ! ! ! -
! type Lens’ a b = forall f. Functor f
=> (b -> f b) -> a -> f a back to the drawing board (defrecord Const [runConst] Functor (fmap [functor f] (Const. (:runConst functor)))) ! (deffn -get [lens a] (???) ! ! ! -
! type Lens’ a b = forall f. Functor f
=> (b -> f b) -> a -> f a back to the drawing board (defrecord Const [runConst] Functor (fmap [functor f] (Const. (:runConst functor)))) ! (deffn -get [lens a] (let [b-cb (fn [b] (->Const b)) -- b -> Const b ] (???))) -
! type Lens’ a b = forall f. Functor f
=> (b -> f b) -> a -> f a back to the drawing board (defrecord Const [runConst] Functor (fmap [functor f] (Const. (:runConst functor)))) ! (deffn -get [lens a] (let [b-cb (fn [b] (->Const b)) -- b -> Const b a-ca (lens b-cb) -- a -> Const a ] (???))) -
! type Lens’ a b = forall f. Functor f
=> (b -> f b) -> a -> f a back to the drawing board (defrecord Const [runConst] Functor (fmap [functor f] (Const. (:runConst functor)))) ! (deffn -get [lens a] (let [b-cb (fn [b] (->Const b)) -- b -> Const b a-ca (lens b-cb) -- a -> Const a ca (a-ca a)] -- Const a (???))) -
! type Lens’ a b = forall f. Functor f
=> (b -> f b) -> a -> f a back to the drawing board (defrecord Const [runConst] Functor (fmap [functor f] (Const. (:runConst functor)))) ! (deffn -get [lens a] (let [b-cb (fn [b] (->Const b)) -- b -> Const b a-ca (lens b-cb) -- a -> Const a ca (a-ca a)] -- Const a (:runConst ca))) -- a -
! type Lens’ a b = forall f. Functor f
=> (b -> f b) -> a -> f a back to the drawing board (defrecord Id [runId] Functor (fmap [functor f] (Id. (f (:runId functor))))) ! (deffn -modify [lens a f] (let [b-ib (fn [bb] (->Id (f bb))) -- b -> Id b a-ia (lens b-ib) -- a -> Id a ia (a-ia a)] -- Id a (:runId ia))) -- a -
composition ! type Lens’ a b = forall f. Functor
f => (b -> f b) -> a -> f a -
function composition ! type Lens’ a b = forall f.
Functor f => (b -> f b) -> a -> f a x :: Lens a b y :: Lens b c (comp x y) :: Lens a c ! ! ! ! ! -
function composition ! type Lens’ a b = forall f.
Functor f => (b -> f b) -> a -> f a x :: Lens a b :: (b -> f b) -> (a -> f a) y :: Lens b c (comp x y) :: Lens a c ! ! ! ! ! -
function composition ! type Lens’ a b = forall f.
Functor f => (b -> f b) -> a -> f a x :: Lens a b :: (b -> f b) -> (a -> f a) y :: Lens b c :: (c -> f c) -> (b -> f b) (comp x y) :: Lens a c ! ! ! ! ! -
function composition ! type Lens’ a b = forall f.
Functor f => (b -> f b) -> a -> f a x :: Lens a b :: (b -> f b) -> (a -> f a) y :: Lens b c :: (c -> f c) -> (b -> f b) (comp x y) :: Lens a c :: (c -> f c) -> (a -> f a) ! ! ! ! ! -
function composition ! type Lens’ a b = forall f.
Functor f => (b -> f b) -> a -> f a x :: Lens a b :: (b -> f b) -> (a -> f a) y :: Lens b c :: (c -> f c) -> (b -> f b) (comp x y) :: Lens a c :: (c -> f c) -> (a -> f a) ! (comp) :: (i -> j) -> (h -> i) -> h -> j ! ! ! -
function composition ! type Lens’ a b = forall f.
Functor f => (b -> f b) -> a -> f a x :: Lens a b :: (b -> f b) -> (a -> f a) y :: Lens b c :: (c -> f c) -> (b -> f b) (comp x y) :: Lens a c :: (c -> f c) -> (a -> f a) ! (comp) :: (i -> j) -> (h -> i) -> h -> j ! (defn comp [f g] (fn [a] (f (g a)))) -
function composition ! type Lens’ a b = forall f.
Functor f => (b -> f b) -> a -> f a x :: Lens a b :: (b -> f b) -> (a -> f a) y :: Lens b c :: (c -> f c) -> (b -> f b) (comp x y) :: Lens a c :: (c -> f c) -> (a -> f a) ! (comp) :: (i -> j) -> (h -> i) -> h -> j ! x :: i -> j ! -
function composition ! type Lens’ a b = forall f.
Functor f => (b -> f b) -> a -> f a x :: Lens a b :: (b -> f b) -> (a -> f a) y :: Lens b c :: (c -> f c) -> (b -> f b) (comp x y) :: Lens a c :: (c -> f c) -> (a -> f a) ! (comp) :: (i -> j) -> (h -> i) -> h -> j ! x :: i -> j, y :: h -> i ! -
function composition ! type Lens’ a b = forall f.
Functor f => (b -> f b) -> a -> f a x :: Lens a b :: (b -> f b) -> (a -> f a) y :: Lens b c :: (c -> f c) -> (b -> f b) (comp x y) :: Lens a c :: (c -> f c) -> (a -> f a) ! (comp) :: (i -> j) -> (h -> i) -> h -> j ! x :: i -> j, y :: h -> i i :: b -> f b -
function composition ! type Lens’ a b = forall f.
Functor f => (b -> f b) -> a -> f a x :: Lens a b :: (b -> f b) -> (a -> f a) y :: Lens b c :: (c -> f c) -> (b -> f b) (comp x y) :: Lens a c :: (c -> f c) -> (a -> f a) ! (comp) :: (i -> j) -> (h -> i) -> h -> j ! x :: i -> j, y :: h -> i i :: b -> f b, j :: a -> f a -
function composition ! type Lens’ a b = forall f.
Functor f => (b -> f b) -> a -> f a x :: Lens a b :: (b -> f b) -> (a -> f a) y :: Lens b c :: (c -> f c) -> (b -> f b) (comp x y) :: Lens a c :: (c -> f c) -> (a -> f a) ! (comp) :: (i -> j) -> (h -> i) -> h -> j ! x :: i -> j, y :: h -> i i :: b -> f b, j :: a -> f a, h :: c -> f c -
function composition ! type Lens’ a b = forall f.
Functor f => (b -> f b) -> a -> f a x :: Lens a b :: (b -> f b) -> (a -> f a) y :: Lens b c :: (c -> f c) -> (b -> f b) (comp x y) :: Lens a c :: (c -> f c) -> (a -> f a) ! (comp) :: (i -> j) -> (h -> i) -> h -> j ! x :: i -> j, y :: h -> i i :: b -> f b, j :: a -> f a, h :: c -> f c (comp x y) :: h -> j -
function composition ! type Lens’ a b = forall f.
Functor f => (b -> f b) -> a -> f a x :: Lens a b :: (b -> f b) -> (a -> f a) y :: Lens b c :: (c -> f c) -> (b -> f b) (comp x y) :: Lens a c :: (c -> f c) -> (a -> f a) ! (comp) :: (i -> j) -> (h -> i) -> h -> j ! x :: i -> j, y :: h -> i i :: b -> f b, j :: a -> f a, h :: c -> f c (comp x y) :: h -> j :: (c -> f c) -> (a -> f a) -
function composition ! type Lens’ a b = forall f.
Functor f => (b -> f b) -> a -> f a x :: Lens a b :: (b -> f b) -> (a -> f a) y :: Lens b c :: (c -> f c) -> (b -> f b) (comp x y) :: Lens a c :: (c -> f c) -> (a -> f a) ! (comp) :: (i -> j) -> (h -> i) -> h -> j ! x :: i -> j, y :: h -> i i :: b -> f b, j :: a -> f a, h :: c -> f c (comp x y) :: h -> j :: (c -> f c) -> (a -> f a) -
type Getter s a = forall r. (a -> Const
r a) -> s -> Const r s ! type Setter s t a b = (a -> Identity b) -> s -> Identity t mirrored lenses -
multiplate type Traversal a a’ b b’ = forall f.
Applicative f => (b -> f b’) -> a -> f a’ -
type Prism s t a b = forall p f.
(Choice p, Applicative f) => p a (f b) -> p s (f t) prisms / co-lens -
references • Pierce’s original work on lenses / bidirectional programming:
• http://www.cis.upenn.edu/~bcpierce/papers/index.shtml#Lenses • van Laarhoven’s original write-up: • http://www.twanvl.nl/blog/haskell/cps-functional-references • O’Connor’s coining of the term “van Laarhoven lens” and the insight into polymorphic update: • http://r6.ca/blog/20120623T104901Z.html • O’Connor’s multiplate paper (a.k.a. traversals): • http://arxiv.org/pdf/1103.2841v2.pdf • Kmett’s expanding on mirrored lens insights (a.k.a. read-only / write only): • http://comonad.com/reader/2012/mirrored-lenses/ • lens website, lots of links and video: • http://lens.github.io/ • git repo: • https://github.com/ekmett/lens • hackage: • http://hackage.haskell.org/package/lens • this talk in haskell: • https://speakerdeck.com/markhibberd/lens-from-the-ground-up