Skip to content

Instantly share code, notes, and snippets.

@mstksg
Last active June 19, 2018 01:20
Show Gist options
  • Save mstksg/804f0b573a4c0080ca1ac35ac6d8dfc7 to your computer and use it in GitHub Desktop.
Save mstksg/804f0b573a4c0080ca1ac35ac6d8dfc7 to your computer and use it in GitHub Desktop.
Type-Level Lenses
ghci> :kind! View FirstFieldSym0 '(1, True)
1
ghci> :kind! Over SecondFieldSym0 NotSym0 '(1, True)
'(1, False)
-- 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