Skip to content

Instantly share code, notes, and snippets.

Created March 15, 2018 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/5cccd7f3a4221d431c9f398cb1b312cc to your computer and use it in GitHub Desktop.
Save anonymous/5cccd7f3a4221d431c9f398cb1b312cc to your computer and use it in GitHub Desktop.
module Parser where
import Control.Applicative
import Data.Text
-- the shape of the problem
--
-- we want a list of Parsers, all of different types.
-- we will apply each of them, one after another,
-- in the context of asking questions in some monadic
-- context.
type QParser a = (Text, Parser a)
type Parser a = Text -> Maybe a
parse :: Parser a -> Text -> Maybe a
parse = undefined
collectAll2 :: Monad m
=> (Text -> m Text)
-> QParser a -> QParser b
-> m (Maybe (a,b))
collectAll2 ask (q1,p1) (q2,p2) =
(liftA2 (,))
<$> (parse p1 <$> ask q1)
<*> (parse p2 <$> ask q2)
collectAll3 :: Monad m
=> (Text -> m Text)
-> QParser a -> QParser b -> QParser c
-> m (Maybe (a,b,c))
collectAll3 ask (q1,p1) (q2,p2) (q3,p3) =
liftA3 (,,)
<$> (parse p1 <$> ask q1)
<*> (parse p2 <$> ask q2)
<*> (parse p3 <$> ask q3)
-- liftA4 doesn't exist :(
-- collectAll4 :: Monad m
-- => (Text -> m Text)
-- -> QParser a -> QParser b -> QParser c -> QParser d
-- -> m (Maybe (a,b,c,d))
-- collectAll4 ask (q1,p1) (q2,p2) (q3,p3) (q4,p4) =
-- liftA4 (,,,)
-- <*> (parse p1 <$> ask q1)
-- <*> (parse p2 <$> ask q2)
-- <*> (parse p3 <$> ask q3)
-- <*> (parse p4 <$> ask q4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment