Skip to content

Commit 0a66943

Browse files
Fix issue #34
1 parent 903371f commit 0a66943

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

chapter7/poly/src/Parser.hs

+8-8
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ bool = (reserved "True" >> return (Lit (LBool True)))
3636
fix :: Parser Expr
3737
fix = do
3838
reservedOp "fix"
39-
x <- aexp
39+
x <- expr
4040
return (Fix x)
4141

4242
lambda :: Parser Expr
@@ -71,11 +71,11 @@ letrecin = do
7171
ifthen :: Parser Expr
7272
ifthen = do
7373
reserved "if"
74-
cond <- aexp
74+
cond <- expr
7575
reservedOp "then"
76-
tr <- aexp
76+
tr <- expr
7777
reserved "else"
78-
fl <- aexp
78+
fl <- expr
7979
return (If cond tr fl)
8080

8181
aexp :: Parser Expr
@@ -91,7 +91,9 @@ aexp =
9191
<|> variable
9292

9393
term :: Parser Expr
94-
term = Ex.buildExpressionParser table aexp
94+
term = aexp >>= \x ->
95+
(many1 aexp >>= \xs -> return (foldl App x xs))
96+
<|> return x
9597

9698
infixOp :: String -> (a -> a -> a) -> Ex.Assoc -> Op a
9799
infixOp x f = Ex.Infix (reservedOp x >> return f)
@@ -111,9 +113,7 @@ table = [
111113
]
112114

113115
expr :: Parser Expr
114-
expr = do
115-
es <- many1 term
116-
return (foldl1 App es)
116+
expr = Ex.buildExpressionParser table term
117117

118118
type Binding = (String, Expr)
119119

chapter7/poly_constraints/src/Parser.hs

+8-8
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ bool = (reserved "True" >> return (Lit (LBool True)))
3636
fix :: Parser Expr
3737
fix = do
3838
reservedOp "fix"
39-
x <- aexp
39+
x <- expr
4040
return (Fix x)
4141

4242
lambda :: Parser Expr
@@ -71,11 +71,11 @@ letrecin = do
7171
ifthen :: Parser Expr
7272
ifthen = do
7373
reserved "if"
74-
cond <- aexp
74+
cond <- expr
7575
reservedOp "then"
76-
tr <- aexp
76+
tr <- expr
7777
reserved "else"
78-
fl <- aexp
78+
fl <- expr
7979
return (If cond tr fl)
8080

8181
aexp :: Parser Expr
@@ -91,7 +91,9 @@ aexp =
9191
<|> variable
9292

9393
term :: Parser Expr
94-
term = Ex.buildExpressionParser table aexp
94+
term = aexp >>= \x ->
95+
(many1 aexp >>= \xs -> return (foldl App x xs))
96+
<|> return x
9597

9698
infixOp :: String -> (a -> a -> a) -> Ex.Assoc -> Op a
9799
infixOp x f = Ex.Infix (reservedOp x >> return f)
@@ -111,9 +113,7 @@ table = [
111113
]
112114

113115
expr :: Parser Expr
114-
expr = do
115-
es <- many1 term
116-
return (foldl1 App es)
116+
expr = Ex.buildExpressionParser table term
117117

118118
type Binding = (String, Expr)
119119

0 commit comments

Comments
 (0)