@@ -4,11 +4,6 @@ Taken straight from Agda source.
44There are two important differences:
55- we also add branches for unreachable defaults (because we need all cases to be exhaustive)
66- we sort the case alts according to the order of constructors for the given inductive
7-
8- Currently, the default branch value is bound in a let binding,
9- I don't know if this will be problematic for some targets that evaluate
10- the binding before the case. Will they fail?
11-
127-}
138
149-- | Eliminates case defaults by adding an alternative for all possible
@@ -22,7 +17,7 @@ import Control.Monad.IO.Class (liftIO)
2217import Agda.Syntax.Treeless
2318import Agda.TypeChecking.Monad
2419import Agda.TypeChecking.Substitute
25- import Agda.Compiler.Treeless.Subst () -- instance only
20+ import Agda.Compiler.Treeless.Subst () -- instance only
2621
2722
2823eliminateCaseDefaults :: TTerm -> TCM TTerm
@@ -35,35 +30,30 @@ eliminateCaseDefaults = tr
3530
3631 let missingCons = dtCons List. \\ map aCon alts
3732 def <- tr def
38- newAlts <- forM missingCons $ \ con -> do
33+
34+ -- we produce a new alternative for every missing constructor
35+ -- whose body is the default body, raised by #args brought in scope
36+ newAlts <- forM missingCons \ con -> do
3937 Constructor {conArity = ar} <- theDef <$> getConstInfo con
40- return $ TACon con ar ( TVar ar)
38+ return $ TACon con ar $ raise ar def
4139
42- alts' <- (++ newAlts) <$> mapM ( trAlt . raise 1 ) alts
40+ alts' <- (++ newAlts) <$> mapM trAlt alts
4341
44- -- sort the alts
45- let alts'' = flip List. sortOn alts' \ alt -> List. elemIndex (aCon alt) dtCons
42+ -- then we sort the alts
43+ let alts'' = flip List. sortOn alts' \ alt -> List. elemIndex (aCon alt) dtCons
4644
47- return $ TLet def $ TCase (sc + 1 ) ct tUnreachable alts''
45+ return $ TCase sc ct tUnreachable alts''
4846
4947 -- case on non-data are always exhaustive
5048 TCase sc ct def alts -> TCase sc ct <$> tr def <*> mapM trAlt alts
5149
52- t@ TVar {} -> return t
53- t@ TDef {} -> return t
54- t@ TCon {} -> return t
55- t@ TPrim {} -> return t
56- t@ TLit {} -> return t
57- t@ TUnit {} -> return t
58- t@ TSort {} -> return t
59- t@ TErased {} -> return t
60- t@ TError {} -> return t
61-
6250 TCoerce a -> TCoerce <$> tr a
6351 TLam b -> TLam <$> tr b
6452 TApp a bs -> TApp <$> tr a <*> mapM tr bs
6553 TLet e b -> TLet <$> tr e <*> tr b
6654
55+ t -> return t
56+
6757 trAlt :: TAlt -> TCM TAlt
6858 trAlt = \ case
6959 TAGuard g b -> TAGuard <$> tr g <*> tr b
0 commit comments