Fraser writes a simple applicative based parser from scratch showing the concepts. He then extends the applicative parser to be built around Cons to abstract away the stream and piecewise type that the parser is dealing with.
fmap :: (a -> b) -> f a -> f b class Functor f => Applicative (f :: * -> *) where pure :: a -> f a (<*>) :: f (a -> b) -> f a -> f b (*>) :: f a -> f b -> f b -- has default definition (<*) :: f a -> f b -> f a -- has default definition class Applicative f => Alternative (f :: * -> *) where empty :: f a (<|>) :: f a -> f a -> f a
Parser a many :: Parser a -> Parser [a] many1 :: Parser a -> Parser (NonEmpty a) sepBy :: Parser sep -> Parser a -> Parser [a] between :: Parser l -> Parser r -> Parser a -> Parser a
s a a => s -> Maybe (a, s) 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!
the Creative Commons Attribution 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by/4.0/. Slides https://github.com/frasertweedale/talks/ Email [email protected] Twitter @hackuador