Skip to content

Instantly share code, notes, and snippets.

@solomon-b
Created September 18, 2024 22:44
Show Gist options
  • Save solomon-b/a8803588c130d3ec349562e2715729dd to your computer and use it in GitHub Desktop.
Save solomon-b/a8803588c130d3ec349562e2715729dd to your computer and use it in GitHub Desktop.
module LensStore where
import Control.Comonad
import Control.Comonad.Store
type Coalg s a = s -> Store a s
_1 :: (a, b) -> Store a (a, b)
_1 (a, b) = store set get
where
set a' = (a' , b)
get = a
_2 :: (a, b) -> Store b (a, b)
_2 (a, b) = store set get
where
set b' = (a , b')
get = b
view :: Coalg s a -> s -> a
view l = pos . l
set :: Coalg s a -> a -> s -> s
set l a = peek a . l
comp :: Coalg a b -> Coalg b c -> Coalg a c
comp f g a = store (ba . cb) c
where
(ba, b) = runStore $ f a
(cb, c) = runStore $ g b
ex1 :: Bool
ex1 = view _1 (True, ())
ex2 :: (Bool, ())
ex2 = set _1 False (True, ())
ex3 :: Bool
ex3 = view (_2 `comp` _1) ((), (True, ()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment