Skip to content

Commit

Permalink
fixed the treatment of pattern match fails in list comprehensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
unnohideyuki committed Dec 13, 2020
1 parent 70a73ee commit 5d7ce2f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
6 changes: 1 addition & 5 deletions compiler/lib/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -708,16 +708,12 @@ readDec :: (Integral a) => (String -> [(a, String)])
readDec = readInt 10 isDigit digitToInt

readSigned :: (Real a) => (String -> [(a, String)]) -> (String -> [(a, String)])
readSigned readPos = {- readParen False -} read'
{-
readSigned readPos = readParen False read'
where read' r = read'' r ++
[(-x, t) | ("-", s) <- lex r,
(x, t) <- read'' s]
read'' r = [(n, s) | (str, s) <- lex r,
(n, "") <- readPos str]
-}
where read' r | head r == '-' = [(-n, s) | (n, s) <- readPos (tail r)]
| otherwise = readPos r

instance Read Integer where
readsPrec p = readSigned readDec
Expand Down
9 changes: 6 additions & 3 deletions compiler/src/Rename.hs
Original file line number Diff line number Diff line change
Expand Up @@ -896,19 +896,22 @@ renExp (A.ListCompExp e (A.ExpStmt b : stmts)) =
renExp (A.IfExp b (A.ListCompExp e stmts) nil)
where nil = A.VarExp $ Name "[]" (0,0) True

-- [e | p <- l, Q] = let ok p = [e | Q] in concatMap ok l
-- [e | p <- l, Q] = let {ok p = [e | Q]; ok _ = []} in concatMap ok l
renExp (A.ListCompExp e (A.BindStmt p l : stmts)) = renExp letexp
where
ok = Name "OK" (0,0) False -- "OK" is fresh, will never parsed as a variable.
okp = A.FunAppExp (A.VarExp ok) p
rhs = case stmts of
[] -> A.UnguardedRhs (A.ListExp [e]) []
_ -> A.UnguardedRhs (A.ListCompExp e stmts) []
decl = A.VDecl $ A.ValDecl okp rhs
okp' = A.FunAppExp (A.VarExp ok) A.WildcardPat
rhs' = A.UnguardedRhs (A.VarExp (Name "[]" (0,0) True)) []
decl1 = A.VDecl $ A.ValDecl okp rhs
decl2 = A.VDecl $ A.ValDecl okp' rhs'
body = A.FunAppExp (A.FunAppExp (A.VarExp $ Name "concatMap" (0,0) False)
(A.VarExp ok))
l
letexp = A.LetExp [decl] body
letexp = A.LetExp [decl1, decl2] body

-- [e | let dictdefDecls, Q] = let dictdefDecls in [e | Q]
renExp (A.ListCompExp e (A.LetStmt ddecls : stmts)) = renExp letexp
Expand Down
1 change: 1 addition & 0 deletions compiler/test/expected/sample323.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
44
2 changes: 2 additions & 0 deletions compiler/test/samples/sample323.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
xs = [Just 3, Just 2, Nothing, Just 9, Nothing, Nothing, Just 8]
main = print $ sum $ [y | Just x <- xs, let y = x * 2]

0 comments on commit 5d7ce2f

Please sign in to comment.