So suppose this is a parser:
<code>data Parser a = MkParser (String -> Maybe (String, a))
runParser :: Parser a -> String -> Maybe a
runParser (MkParser sf) inp = case sf inp of
Nothing -> Nothing
Just (_, a) -> Just a
unParser :: Parser a -> String -> Maybe (String, a)
unParser (MkParser sf1) = sf1
(>>=) :: Parser a -> (a -> Parser b) -> Parser b
-- takes a parser, and a second parser (second parser takes a char)
-- parses `a`, then feeds `a` to second parser...
MkParser pa >>= k = MkParser sf
where
sf inp = case pa inp of
Nothing -> Nothing
Just (as, a) -> unParser (k a) as
-- Question: If the second parser fails, does the first
-- parser still parse? What string does it return?
-- as or inp?
-- parses any char
anyChar :: Parser Char
anyChar = MkParser sf
where
sf "" = Nothing
sf (c:cs) = Just (cs, c)
-- parses the char we want
char :: Char -> Parser Char
char wanted = MkParser sf
where
sf (c:cs) | c == wanted = Just (cs, c)
sf _ = Nothing
</code>
<code>data Parser a = MkParser (String -> Maybe (String, a))
runParser :: Parser a -> String -> Maybe a
runParser (MkParser sf) inp = case sf inp of
Nothing -> Nothing
Just (_, a) -> Just a
unParser :: Parser a -> String -> Maybe (String, a)
unParser (MkParser sf1) = sf1
(>>=) :: Parser a -> (a -> Parser b) -> Parser b
-- takes a parser, and a second parser (second parser takes a char)
-- parses `a`, then feeds `a` to second parser...
MkParser pa >>= k = MkParser sf
where
sf inp = case pa inp of
Nothing -> Nothing
Just (as, a) -> unParser (k a) as
-- Question: If the second parser fails, does the first
-- parser still parse? What string does it return?
-- as or inp?
-- parses any char
anyChar :: Parser Char
anyChar = MkParser sf
where
sf "" = Nothing
sf (c:cs) = Just (cs, c)
-- parses the char we want
char :: Char -> Parser Char
char wanted = MkParser sf
where
sf (c:cs) | c == wanted = Just (cs, c)
sf _ = Nothing
</code>
data Parser a = MkParser (String -> Maybe (String, a))
runParser :: Parser a -> String -> Maybe a
runParser (MkParser sf) inp = case sf inp of
Nothing -> Nothing
Just (_, a) -> Just a
unParser :: Parser a -> String -> Maybe (String, a)
unParser (MkParser sf1) = sf1
(>>=) :: Parser a -> (a -> Parser b) -> Parser b
-- takes a parser, and a second parser (second parser takes a char)
-- parses `a`, then feeds `a` to second parser...
MkParser pa >>= k = MkParser sf
where
sf inp = case pa inp of
Nothing -> Nothing
Just (as, a) -> unParser (k a) as
-- Question: If the second parser fails, does the first
-- parser still parse? What string does it return?
-- as or inp?
-- parses any char
anyChar :: Parser Char
anyChar = MkParser sf
where
sf "" = Nothing
sf (c:cs) = Just (cs, c)
-- parses the char we want
char :: Char -> Parser Char
char wanted = MkParser sf
where
sf (c:cs) | c == wanted = Just (cs, c)
sf _ = Nothing
Suppose I do
<code>secondFail = anyChar >>= c -> char c
runParser secondFail "test"
</code>
<code>secondFail = anyChar >>= c -> char c
runParser secondFail "test"
</code>
secondFail = anyChar >>= c -> char c
runParser secondFail "test"
This gives Nothing
because the c -> char c
fails (e
is not t
).
My question is, did the first parser run? Did the input string get parsed 1 time? Is it “est” or is it the case that since the second parser failed, the whole thing failed and the string did not get parsed?
I’m not sure how to write code to test the output, since the output is just Nothing
.