a) (Either c b) a b ghci> preview _Right (Right 42) Just 42 ghci> preview _Right (Left "nope") Nothing ghci> review _Right 42 Right 42 ghci> preview (_Left . _Right) (Left (Right 42)) Just 42
Prism s t (a, s) (b, t) instance Cons ByteString ByteString Word8 Word8 instance Cons Text Text Char Char instance Cons [a] [b] a b instance Cons (Vector a) (Vector b) a b -- and many more!
Parser s a = Prism s s (a, s) (a, s) char :: (Cons s s a a) => Parser s a char = _Cons runParser :: Parser s a -> s -> Maybe (a, s) runParser = preview runPrinter :: Parser s a -> (a, s) -> s runPrinter = review
Prism s s (a, s) (a, s) element :: (Cons s s a a) => Grammar s a element = _Cons runParser :: Grammar s a -> s -> Maybe (a, s) runParser = preview runPrinter :: Grammar s a -> (a, s) -> s runPrinter = review
-> Grammar s a -> Grammar s b p <<$>> g = g . swapped . aside p . swapped swapped :: Iso (a, b) (c, d) (b, a) (d, c) aside :: Prism s t a b -> Prism (e, s) (e, t) (e, a) (e, b)
s b -> Grammar s (Either a b) g1 <<+>> g2 = prism (\(x, s) -> either (review g1 . (,s)) (review g2 . (,s)) x) (\s -> first Left <$> preview g1 s <|> first Right <$> preview g2 s)
s [a] many g = isoList <<$>> (g <<*>> many g) <<+>> success () isoList :: Iso (Either (a, [a]) ()) [a] isoList = ... -- like pure :: Applicative f => a -> f a success :: a -> Grammar s a success a = prism snd (Just . (a,))
a) => (a -> Bool) -> Grammar s a satisfy f = prism id (\a -> guard (f a) >> pure a) <<$>> element symbol :: (Cons s s a a, Eq a) => a -> Grammar s a symbol a = satisfy (== a) eof :: (Cons s s a a) => Grammar s () eof = prism snd (\s -> maybe (Just ((), s)) (const Nothing) (uncons s))
s () -> Grammar s a (*>>) :: Grammar s () -> Grammar s a -> Grammar s a many1 :: Grammar s a -> Grammar s (NonEmpty a) replicate :: Natural -> Grammar s a -> Grammar s [a]
-> Grammar s b) -> (b -> a) -> Grammar s b bind p f g = prism (\(b, s) -> review p (g b, review (f (g b)) (b, s))) (preview p >=> \(a, s ) -> preview (f a) s )
Grammar s Bool integer :: (Cons s s ASN1 ASN1) => Grammar s Integer octetString :: (Cons s s ASN1 ASN1) => Grammar s B.ByteString oid :: (Cons s s ASN1 ASN1) => Grammar s OID
boomerang, roundtrip, invertible-syntax Christian Marie’s YLJ2015 talk Experiment in combining Applicative and Divisible https://github.com/charleso/portmanteau lens
work is licensed under http://creativecommons.org/licenses/by/4.0/ Slides https://github.com/ frasertweedale/talks/ Email [email protected] Twitter @hackuador