Created
September 18, 2024 22:44
-
-
Save solomon-b/a8803588c130d3ec349562e2715729dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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