Last active
June 19, 2018 01:20
-
-
Save mstksg/804f0b573a4c0080ca1ac35ac6d8dfc7 to your computer and use it in GitHub Desktop.
Type-Level Lenses
This file contains 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
ghci> :kind! View FirstFieldSym0 '(1, True) | |
1 | |
ghci> :kind! Over SecondFieldSym0 NotSym0 '(1, True) | |
'(1, False) |
This file contains 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
-- singletons can promote these to the type level using template haskell | |
type Setter s t a b = (a -> Identity b) -> (s -> Identity t) | |
over :: Setter s t a b -> (a -> b) -> (s -> t) | |
over l f = (\(Identity x) -> x) . l (Identity . f) | |
type Getter s a = (a -> Const a a) -> (s -> Const a s) | |
view :: Getter s a -> s -> a | |
view l = (\(Const x) -> x) . l Const | |
firstField :: Functor f => (a -> f b) -> (a, c) -> f (b, c) | |
firstField f (x, y) = (\x' -> (x', y)) <$> f x | |
secondField :: Functor f => (b -> f c) -> (a, b) -> f (a, c) | |
secondField f (x, y) = (\y' -> (x, y')) <$> f y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment