Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import Control.Applicative
  3. type Symb = String
  4.  
  5. infixl 2 :@
  6.  
  7. data Expr = Var Symb
  8.           | Expr :@ Expr                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
  9.           | Lam Symb Expr
  10.               deriving Eq
  11.  
  12. instance Show Expr where
  13.     showsPrec _ = showExpr
  14.  
  15. showExpr :: Expr -> ShowS
  16. showExpr (Var x)              = \z -> x ++ z
  17. showExpr ((Var x) :@ (Var y)) = showExpr (Var x) . (" " ++) . showExpr (Var y)
  18. showExpr ((Var x) :@ e)       = showExpr (Var x) . (" (" ++) . showExpr (e) . (")" ++)
  19. showExpr (e :@ (Var x))       = ("(" ++) . showExpr (e) . (") " ++) . showExpr (Var x)
  20. showExpr (e1 :@ e2)           = ("(" ++) . showExpr e1 . (")" ++) . (" (" ++) . showExpr e2 . (") "++)
  21. showExpr (Lam x e)            = ("\\" ++) . (x ++) . (" -> " ++) . showExpr e
  22.  
  23. instance Read Expr where
  24.     readsPrec _ s = filter (\(a, b) -> null b) $ delSpaces <$> (apply exprParser s)
  25.  
  26. delSpace s = filter (\x -> x /= ' ') s
  27.  
  28. delSpaces :: (Expr, String) -> (Expr, String)
  29. delSpaces (a, b) = (a, delSpace b)
  30.  
  31. newtype Parser a = Parser { apply :: String -> [(a, String)] }
  32.  
  33. instance Functor Parser where
  34.     fmap f p = Parser (\str -> map (\(val, s) -> (f val, s)) (apply p str))
  35.  
  36. instance Applicative Parser where
  37.     pure x  = Parser $ \s -> [(x, s)]
  38.     p <*> r = Parser (\s -> [(f a, sy) | (f, sx) <- apply p s,
  39.                                          (a, sy) <- apply r sx])
  40.  
  41. instance Alternative Parser where
  42.     empty   = Parser (const [])
  43.     p <|> r = Parser $ \s -> apply p s ++ apply r s
  44.  
  45.  
  46. flex :: String -> (String, String)
  47. flex = head . lex
  48.  
  49. justCond :: Bool -> a -> Maybe a
  50. justCond f x = if f then Just x else Nothing
  51.  
  52. exprParser :: Parser Expr
  53. exprParser = parseBrackets <|> preLamParser <|> ((:@) <$> appParser <*> exprParser) <|> appParser <|> varParser
  54.  
  55. unitParser :: (String -> Maybe String) -> Parser String
  56. unitParser f = Parser (\s -> let flexed = flex s in
  57.     case f (fst flexed) of
  58.         Nothing -> []
  59.         Just x  -> [(x, snd flexed)])
  60.  
  61. symbParser :: Parser Symb
  62. symbParser = unitParser (\x -> justCond (not (elem x bans)) x) where
  63.     bans = ["(", ")", "->", "\\", ":@", ""]
  64.  
  65. varParser :: Parser Expr
  66. varParser = Var <$> symbParser
  67.  
  68. preLamParser :: Parser Expr
  69. preLamParser = (unitParser (\x -> justCond (x == "\\") x)) *> lamParser
  70.  
  71. lamParser :: Parser Expr
  72. lamParser = (Lam <$> symbParser <* (unitParser (\x -> justCond (x == "->") x)) <*> exprParser)
  73.             <|> (Lam <$> symbParser <*> lamParser)
  74.  
  75. parseBrackets :: Parser Expr
  76. parseBrackets = (unitParser (\x -> justCond (x == "(") x)) *> exprParser <* (unitParser (\x -> justCond (x == ")") x))
  77.  
  78. appParser :: Parser Expr
  79. appParser = (:@) <$> (parseBrackets <|> varParser) <*> (parseBrackets <|> varParser)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement