Skip to content

Commit

Permalink
Only evaluate the first matching case.
Browse files Browse the repository at this point in the history
  • Loading branch information
athas committed Dec 20, 2018
1 parent a9cfbdd commit ff94df7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/Language/Futhark/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -771,16 +771,20 @@ eval _ (VConstr0 c _ _) = return $ ValueEnum c

eval env (Match e cs _ _) = do
v <- eval env e
cs' <- mapM (runMaybeT . evalCase v env) cs
case catMaybes cs' of
[] -> fail "Pattern match failure."
(v':_) -> return v'
match v cs
where match v [] =
fail "Pattern match failure."
match v (c:cs) = do
c' <- evalCase v env c
case c' of
Just v' -> return v'
Nothing -> match v cs

eval _ e = error $ "eval not yet: " ++ show e

evalCase :: Value -> Env -> CaseBase Info VName
-> MaybeT EvalM Value
evalCase v env (CasePat p cExp _) = do
-> EvalM (Maybe Value)
evalCase v env (CasePat p cExp _) = runMaybeT $ do
pEnv <- valEnv <$> patternMatch env mempty p v
lift $ eval (pEnv <> env) cExp

Expand Down
7 changes: 7 additions & 0 deletions tests/enums/enum47.fut
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Do not evaluate branches unnecessarily...
-- ==
-- input { 0 } output { 0 }

let main (x: i32) =
match x case 0 -> 0
case _ -> 2/x

0 comments on commit ff94df7

Please sign in to comment.