Skip to content

Instantly share code, notes, and snippets.

@fumieval
Forked from anonymous/questionasker.hs
Created March 16, 2018 08:15
Show Gist options
  • Save fumieval/0484909edacedce8b677cf0879b0db91 to your computer and use it in GitHub Desktop.
Save fumieval/0484909edacedce8b677cf0879b0db91 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