From 0a669431c2bef293d92a9257d6a513f16fb65da2 Mon Sep 17 00:00:00 2001 From: Jeffrey Rosenbluth Date: Sun, 1 Jan 2017 12:32:18 -0700 Subject: [PATCH] Fix issue #34 --- chapter7/poly/src/Parser.hs | 16 ++++++++-------- chapter7/poly_constraints/src/Parser.hs | 16 ++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/chapter7/poly/src/Parser.hs b/chapter7/poly/src/Parser.hs index 1749946..f24fe6a 100644 --- a/chapter7/poly/src/Parser.hs +++ b/chapter7/poly/src/Parser.hs @@ -36,7 +36,7 @@ bool = (reserved "True" >> return (Lit (LBool True))) fix :: Parser Expr fix = do reservedOp "fix" - x <- aexp + x <- expr return (Fix x) lambda :: Parser Expr @@ -71,11 +71,11 @@ letrecin = do ifthen :: Parser Expr ifthen = do reserved "if" - cond <- aexp + cond <- expr reservedOp "then" - tr <- aexp + tr <- expr reserved "else" - fl <- aexp + fl <- expr return (If cond tr fl) aexp :: Parser Expr @@ -91,7 +91,9 @@ aexp = <|> variable term :: Parser Expr -term = Ex.buildExpressionParser table aexp +term = aexp >>= \x -> + (many1 aexp >>= \xs -> return (foldl App x xs)) + <|> return x infixOp :: String -> (a -> a -> a) -> Ex.Assoc -> Op a infixOp x f = Ex.Infix (reservedOp x >> return f) @@ -111,9 +113,7 @@ table = [ ] expr :: Parser Expr -expr = do - es <- many1 term - return (foldl1 App es) +expr = Ex.buildExpressionParser table term type Binding = (String, Expr) diff --git a/chapter7/poly_constraints/src/Parser.hs b/chapter7/poly_constraints/src/Parser.hs index 1749946..f24fe6a 100644 --- a/chapter7/poly_constraints/src/Parser.hs +++ b/chapter7/poly_constraints/src/Parser.hs @@ -36,7 +36,7 @@ bool = (reserved "True" >> return (Lit (LBool True))) fix :: Parser Expr fix = do reservedOp "fix" - x <- aexp + x <- expr return (Fix x) lambda :: Parser Expr @@ -71,11 +71,11 @@ letrecin = do ifthen :: Parser Expr ifthen = do reserved "if" - cond <- aexp + cond <- expr reservedOp "then" - tr <- aexp + tr <- expr reserved "else" - fl <- aexp + fl <- expr return (If cond tr fl) aexp :: Parser Expr @@ -91,7 +91,9 @@ aexp = <|> variable term :: Parser Expr -term = Ex.buildExpressionParser table aexp +term = aexp >>= \x -> + (many1 aexp >>= \xs -> return (foldl App x xs)) + <|> return x infixOp :: String -> (a -> a -> a) -> Ex.Assoc -> Op a infixOp x f = Ex.Infix (reservedOp x >> return f) @@ -111,9 +113,7 @@ table = [ ] expr :: Parser Expr -expr = do - es <- many1 term - return (foldl1 App es) +expr = Ex.buildExpressionParser table term type Binding = (String, Expr)