Skip to content

Instantly share code, notes, and snippets.

@chakpongchung
Forked from dramforever/Parser.hs
Created April 10, 2020 17:31
Show Gist options
  • Save chakpongchung/5aba77535b318ce168e46342c8212462 to your computer and use it in GitHub Desktop.
Save chakpongchung/5aba77535b318ce168e46342c8212462 to your computer and use it in GitHub Desktop.
import sys
gen = 0
counter = 0
for line in sys.stdin:
if line.startswith('='):
print(f'step{counter} {line.rstrip()}')
counter += 1
elif line[0] == '#':
name = line[1:]
print(name + ' = [' + ', '.join(f'step{i}' for i in range(gen, counter)) + ']')
gen = counter
else:
print(line.rstrip())

Expected:

$ python gen.py < three.txt > three.hs
$ runghc three.hs
OK
module Parser where
newtype Parser a = P { parse :: String -> [(a, String)] }
instance Functor Parser where
fmap g p = P (\inp -> case parse p inp of
[] -> []
[(v, out)] -> [(g v, out)])
instance Applicative Parser where
pure v = P (\inp -> [(v, inp)])
pg <*> px = P (\inp -> case parse pg inp of
[] -> []
[(g, out)] -> parse (fmap g px) out)
item :: Parser Char
item = P (\inp -> case inp of
[] -> []
(x:xs) -> [(x, xs)])
g :: a -> p -> b -> (a, b)
g x y z = (x, z)
three :: Parser (Char, Char)
three = pure g <*> item <*> item <*> item
import Parser
= three
{- Inline three -}
= pure g <*> item <*> item <*> item
{- Inline (pure g) -}
= P (\inp -> [(g, inp)]) <*> item <*> item <*> item
{- Inline (P ... <*> item) -}
= P (\inp -> case parse (P (\inp1 -> [(g, inp1)])) inp of
[] -> []
[(g1, out)] -> parse (fmap g1 item) out) <*> item <*> item
{- Inline (parse (P ...))-}
= P (\inp -> case (\inp1 -> [(g, inp1)]) inp of
[] -> []
[(g1, out)] -> parse (fmap g1 item) out) <*> item <*> item
{- Beta ((\inp1 -> ...) inp) -}
= P (\inp -> case [(g, inp)] of
[] -> []
[(g1, out)] -> parse (fmap g1 item) out) <*> item <*> item
{- Evaluate case -}
= P (\inp -> parse (fmap g item) inp) <*> item <*> item
{- Inline (fmap g item) -}
= P (\inp -> parse (P (\inp1 -> case parse item inp1 of
[] -> []
[(v, out)] -> [(g v, out)])) inp) <*> item <*> item
{- Inline (parse (P ...))-}
= P (\inp -> (\inp1 -> case parse item inp1 of
[] -> []
[(v, out)] -> [(g v, out)]) inp) <*> item <*> item
{- Beta ((\inp1 -> ...) inp) -}
= P (\inp -> case parse item inp of
[] -> []
[(v, out)] -> [(g v, out)]) <*> item <*> item
{- Inline item -}
= P (\inp -> case parse (P (\inp1 -> case inp1 of
[] -> []
(x:xs) -> [(x, xs)])) inp of
[] -> []
[(v, out)] -> [(g v, out)]) <*> item <*> item
{- Inline (parse (P ...))-}
{- Beta ((\inp1 -> ...) inp) -}
= P (\inp -> case (case inp of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out)] -> [(g v, out)]) <*> item <*> item
{- Inline (P ... <*> item) -}
= P (\inp -> case parse (P (\inp1 -> case (case inp1 of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out1)] -> [(g v, out1)])) inp of
[] -> []
[(g1, out)] -> parse (fmap g1 item) out) <*> item
{- Inline (parse (P ...))-}
{- Beta ((\inp1 -> ...) inp) -}
= P (\inp -> case (case (case inp of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out1)] -> [(g v, out1)]) of
[] -> []
[(g1, out)] -> parse (fmap g1 item) out) <*> item
{- Inline (fmap g1 item) -}
= P (\inp -> case (case (case inp of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out1)] -> [(g v, out1)]) of
[] -> []
[(g1, out)] -> parse (P (\inp1 -> case parse item inp1 of
[] -> []
[(v, out2)] -> [(g1 v, out2)])) out) <*> item
{- Inline (parse (P ...)) -}
{- Beta ((\inp1 -> ...) out) -}
= P (\inp -> case (case (case inp of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out1)] -> [(g v, out1)]) of
[] -> []
[(g1, out)] -> case parse item out of
[] -> []
[(v, out2)] -> [(g1 v, out2)]) <*> item
{- Inline item -}
= P (\inp -> case (case (case inp of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out1)] -> [(g v, out1)]) of
[] -> []
[(g1, out)] -> case parse (P (\inp1 -> case inp1 of
[] -> []
(x:xs) -> [(x, xs)])) out of
[] -> []
[(v, out2)] -> [(g1 v, out2)]) <*> item
{- Inline (parse (P ...)) -}
{- Beta ((\inp1 -> ...) out) -}
= P (\inp -> case (case (case inp of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out1)] -> [(g v, out1)]) of
[] -> []
[(g1, out)] -> case (case out of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out2)] -> [(g1 v, out2)]) <*> item
{- Inline (P ... <*> item) -}
= P (\inp -> case (case (case inp of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out1)] -> [(g v, out1)]) of
[] -> []
[(g1, out)] -> case (case out of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out2)] -> [(g1 v, out2)]) <*> item
= P (\inp -> case parse (P (\inp1 -> case (case (case inp1 of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out1)] -> [(g v, out1)]) of
[] -> []
[(g1, out)] -> case (case out of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out2)] -> [(g1 v, out2)])) inp of
[] -> []
[(g2, out3)] -> parse (fmap g2 item) out3)
{- Inline (parse (P ...)) -}
{- Beta ((\inp1 -> ...) inp) -}
= P (\inp -> case (case (case (case inp of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out1)] -> [(g v, out1)]) of
[] -> []
[(g1, out)] -> case (case out of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out2)] -> [(g1 v, out2)]) of
[] -> []
[(g2, out3)] -> parse (fmap g2 item) out3)
{- Inline (fmap g2 item) -}
= P (\inp -> case (case (case (case inp of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out1)] -> [(g v, out1)]) of
[] -> []
[(g1, out)] -> case (case out of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out2)] -> [(g1 v, out2)]) of
[] -> []
[(g2, out3)] -> parse (P (\inp1 -> case parse item inp1 of
[] -> []
[(v, out4)] -> [(g2 v, out4)])) out3)
{- Inline (parse (P ...)) -}
{- Beta ((\inp1 -> ...) out3) -}
= P (\inp -> case (case (case (case inp of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out1)] -> [(g v, out1)]) of
[] -> []
[(g1, out)] -> case (case out of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out2)] -> [(g1 v, out2)]) of
[] -> []
[(g2, out3)] -> case parse item out3 of
[] -> []
[(v, out4)] -> [(g2 v, out4)])
{- Inline item -}
= P (\inp -> case (case (case (case inp of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out1)] -> [(g v, out1)]) of
[] -> []
[(g1, out)] -> case (case out of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out2)] -> [(g1 v, out2)]) of
[] -> []
[(g2, out3)] -> case parse (P (\inp1 -> case inp1 of
[] -> []
(x:xs) -> [(x, xs)])) out3 of
[] -> []
[(v, out4)] -> [(g2 v, out4)])
{- Inline (parse (P ...)) -}
{- Beta ((\inp1 -> ...) out3) -}
= P (\inp -> case (case (case (case inp of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out1)] -> [(g v, out1)]) of
[] -> []
[(g1, out)] -> case (case out of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out2)] -> [(g1 v, out2)]) of
[] -> []
[(g2, out3)] -> case (case out3 of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out4)] -> [(g2 v, out4)])
#threeSteps
----------------------------------------------------------------------
= parse three ('a' : 'b' : 'c' : [])
{- By above -}
= parse (P (\inp -> case (case (case (case inp of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out1)] -> [(g v, out1)]) of
[] -> []
[(g1, out)] -> case (case out of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out2)] -> [(g1 v, out2)]) of
[] -> []
[(g2, out3)] -> case (case out3 of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out4)] -> [(g2 v, out4)])) ('a' : 'b' : 'c' : [])
{- Inline (parse (P ...)) -}
{- Beta ((\inp -> ...) ('a' : ...)) -}
= case (case (case (case ('a' : 'b' : 'c' : []) of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out1)] -> [(g v, out1)]) of
[] -> []
[(g1, out)] -> case (case out of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out2)] -> [(g1 v, out2)]) of
[] -> []
[(g2, out3)] -> case (case out3 of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out4)] -> [(g2 v, out4)]
{- Evaluate case -}
= case (case (case [('a', 'b' : 'c' : [])] of
[] -> []
[(v, out1)] -> [(g v, out1)]) of
[] -> []
[(g1, out)] -> case (case out of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out2)] -> [(g1 v, out2)]) of
[] -> []
[(g2, out3)] -> case (case out3 of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out4)] -> [(g2 v, out4)]
{- Evaluate case -}
= case (case [(g 'a', 'b' : 'c' : [])] of
[] -> []
[(g1, out)] -> case (case out of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out2)] -> [(g1 v, out2)]) of
[] -> []
[(g2, out3)] -> case (case out3 of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out4)] -> [(g2 v, out4)]
{- Evaluate case -}
= case (case (case ('b' : 'c' : []) of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out2)] -> [(g 'a' v, out2)]) of
[] -> []
[(g2, out3)] -> case (case out3 of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out4)] -> [(g2 v, out4)]
{- Evaluate case -}
= case (case [('b', 'c' : [])] of
[] -> []
[(v, out2)] -> [(g 'a' v, out2)]) of
[] -> []
[(g2, out3)] -> case (case out3 of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out4)] -> [(g2 v, out4)]
{- Evaluate case -}
= case [(g 'a' 'b', 'c' : [])] of
[] -> []
[(g2, out3)] -> case (case out3 of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out4)] -> [(g2 v, out4)]
{- Evaluate case -}
= case (case ('c' : []) of
[] -> []
(x:xs) -> [(x, xs)]) of
[] -> []
[(v, out4)] -> [(g 'a' 'b' v, out4)]
{- Evaluate case -}
= case [('c', [])] of
[] -> []
[(v, out4)] -> [(g 'a' 'b' v, out4)]
{- Evaluate case -}
= [(g 'a' 'b' 'c', [])]
{- Inline (g 'a' 'b' 'c')-}
= [(('a', 'c'), [])]
#parsedSteps
main = do
let result = [(('a','c'),"")]
check1 = all (\x -> parse x "abc" == result) threeSteps
check2 = all (== result) parsedSteps
if check1 && check2
then putStrLn "OK"
else putStrLn "Not OK!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment