on master help sign in
Postpone implicit addition in case blocks

They get resolved later, when the lifted function is type checked. This ensures that case blocks have the same pattern scoping rules as top level functions (and saves a small amount of work!) Fixes #1244

commitf15aaef2036c1f01611941107048ddcf049332e6 authorEdwin Brady <ecb10@st-andrews.ac.uk> dateWed 28 May 2014, 15:23 (12 years ago) parent6d6e48b
2 files changed+23−7
src/Idris/AbsSyntax.hs+5−3 src/Idris/ElabTerm.hs+18−4
src/Idris/AbsSyntax.hs+5−3
@@ -1329,9 +1329,11 @@ addImpl' inpat env infns ist ptm
13291329 as' = map (fmap (ai env ds)) as in
13301330 mkPApp fc 1 f' as'
13311331 ai env ds (PCase fc c os)
1332− = let c' = ai env ds c
1333− os' = map (pmap (ai env ds)) os in
1334− PCase fc c' os'
1332+ = let c' = ai env ds c in
1333+ -- leave os alone, because they get lifted into a new pattern match
1334+ -- definition which is passed through addImpl again with more scope
1335+ -- information
1336+ PCase fc c' os
13351337 ai env ds (PLam n ty sc)
13361338 = let ty' = ai env ds ty
13371339 sc' = ai ((n, Just ty):env) ds sc in
src/Idris/ElabTerm.hs+18−4
@@ -265,7 +265,8 @@ elab ist info pattern opts fn tm
265265 ty <- goal
266266 ctxt <- get_context
267267 let (tc, _) = unApply ty
268− let as' = pruneByType tc ctxt as
268+ env <- get_env
269+ let as' = pruneByType (map fst env) tc ctxt as
269270-- trace (show as ++ "\n ==> " ++ showSep ", " (map showTmImpls as')) $
270271 tryAll (zip (map (elab' ina) as') (map showHd as'))
271272 where showHd (PApp _ (PRef _ n) _) = show n
@@ -785,8 +786,21 @@ pruneAlt xs = map prune xs
785786
786787-- Rule out alternatives that don't return the same type as the head of the goal
787788-- (If there are none left as a result, do nothing)
788−pruneByType :: Term -> Context -> [PTerm] -> [PTerm]
789−pruneByType (P _ n _) c as
789+pruneByType :: [Name] -> Term -> Context -> [PTerm] -> [PTerm]
790+-- if an alternative has a locally bound name at the head, take it
791+pruneByType env t c as
792+ | Just a <- locallyBound as = [a]
793+ where
794+ locallyBound [] = Nothing
795+ locallyBound (t:ts)
796+ | Just n <- getName t,
797+ n `elem` env = Just t
798+ | otherwise = locallyBound ts
799+ getName (PRef _ n) = Just n
800+ getName (PApp _ f _) = getName f
801+ getName _ = Nothing
802+
803+pruneByType env (P _ n _) c as
790804-- if the goal type is polymorphic, keep e
791805 | [] <- lookupTy n c = as
792806 | otherwise
@@ -812,7 +826,7 @@ pruneByType (P _ n _) c as
812826 _ -> False
813827 _ -> False
814828
815−pruneByType t _ as = as
829+pruneByType _ t _ as = as
816830
817831findInstances :: IState -> Term -> [Name]
818832findInstances ist t