Last active
March 6, 2016 01:43
-
-
Save robrix/902e8008ee4ad02763b4 to your computer and use it in GitHub Desktop.
Parsing with Derivatives, via GADTs
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
| data Parser a where | |
| Cat :: Parser a -> Parser b -> Parser (a, b) | |
| Alt :: Parser a -> Parser b -> Parser (Either a b) | |
| Rep :: Parser a -> Parser [a] | |
| Map :: Parser a -> (a -> b) -> Parser b | |
| Bnd :: Parser a -> (a -> Parser b) -> Parser b | |
| Lit :: Char -> Parser Char | |
| Ret :: [a] -> Parser a | |
| Nul :: Parser a | |
| Eps :: Parser a | |
| instance Functor Parser where | |
| fmap = flip Map | |
| instance Applicative Parser where | |
| pure = return | |
| (<*>) = ap | |
| instance Alternative Parser where | |
| empty = Eps | |
| (<|>) = (fmap (either id id) .) . Alt | |
| many = Rep | |
| instance Monad Parser where | |
| return = Ret . pure | |
| (>>=) = Bnd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment