1{-# LANGUAGE PatternGuards, DeriveFunctor, TypeSynonymInstances #-}
3module Idris.Core.CaseTree(CaseDef(..), SC, SC'(..), CaseAlt, CaseAlt'(..),
5 simpleCase, small, namesUsed, findCalls, findUsedArgs,
6 substSC, substAlt, mkForce) where
10import Control.Monad.State
12import Data.List hiding (partition)
15data CaseDef = CaseDef [Name] !SC [Term]
18-- Note: The case-tree elaborator only produces (Case n alts)-cases;
19-- in other words, it never inspects anything else than variables.
21-- ProjCase is a special powerful case construct that allows inspection
22-- of compound terms. Occurrences of ProjCase arise no earlier than
23-- in the function `prune` as a means of optimisation
24-- of already built case trees.
26-- While the intermediate representation (follows in the pipeline)
27-- allows casing on arbitrary terms, here we choose to maintain the distinction
28-- in order to allow for better optimisation opportunities.
30data SC' t = Case Name [CaseAlt' t] -- ^ invariant: lowest tags first
31 | ProjCase t [CaseAlt' t] -- ^ special case for projections/thunk-forcing before inspection
33 | UnmatchedCase String -- ^ error message
34 | ImpossibleCase -- ^ already checked to be impossible
35 deriving (Eq, Ord, Functor)
37deriving instance Binary SC'
38deriving instance NFData SC'
43data CaseAlt' t = ConCase Name Int [Name] !(SC' t)
44 | FnCase Name [Name] !(SC' t) -- ^ reflection function
45 | ConstCase Const !(SC' t)
46 | SucCase Name !(SC' t)
47 | DefaultCase !(SC' t)
48 deriving (Show, Eq, Ord, Functor)
50deriving instance Binary CaseAlt'
51deriving instance NFData CaseAlt'
54type CaseAlt = CaseAlt' Term
56instance Show t => Show (SC' t) where
59 show' i (Case n alts) = "case " ++ show n ++ " of\n" ++ indent i ++
60 showSep ("\n" ++ indent i) (map (showA i) alts)
61 show' i (ProjCase tm alts) = "case " ++ show tm ++ " of " ++
62 showSep ("\n" ++ indent i) (map (showA i) alts)
63 show' i (STerm tm) = show tm
64 show' i (UnmatchedCase str) = "error " ++ show str
65 show' i ImpossibleCase = "impossible"
67 indent i = concat $ take i (repeat " ")
69 showA i (ConCase n t args sc)
70 = show n ++ "(" ++ showSep (", ") (map show args) ++ ") => "
72 showA i (FnCase n args sc)
73 = "FN " ++ show n ++ "(" ++ showSep (", ") (map show args) ++ ") => "
75 showA i (ConstCase t sc)
76 = show t ++ " => " ++ show' (i+1) sc
77 showA i (SucCase n sc)
78 = show n ++ "+1 => " ++ show' (i+1) sc
79 showA i (DefaultCase sc)
80 = "_ => " ++ show' (i+1) sc
84type Clause = ([Pat], (Term, Term))
85type CS = ([Term], Int, [(Name, Type)])
87instance TermSize SC where
88 termsize n (Case n' as) = termsize n as
89 termsize n (ProjCase n' as) = termsize n as
90 termsize n (STerm t) = termsize n t
93instance TermSize CaseAlt where
94 termsize n (ConCase _ _ _ s) = termsize n s
95 termsize n (FnCase _ _ s) = termsize n s
96 termsize n (ConstCase _ s) = termsize n s
97 termsize n (SucCase _ s) = termsize n s
98 termsize n (DefaultCase s) = termsize n s
100-- simple terms can be inlined trivially - good for primitives in particular
101-- To avoid duplicating work, don't inline something which uses one
102-- of its arguments in more than one place
104small :: Name -> [Name] -> SC -> Bool
105small n args t = let as = findAllUsedArgs t args in
106 length as == length (nub as) &&
109namesUsed :: SC -> [Name]
110namesUsed sc = nub $ nu' [] sc where
111 nu' ps (Case n alts) = nub (concatMap (nua ps) alts) \\ [n]
112 nu' ps (ProjCase t alts) = nub $ nut ps t ++ concatMap (nua ps) alts
113 nu' ps (STerm t) = nub $ nut ps t
116 nua ps (ConCase n i args sc) = nub (nu' (ps ++ args) sc) \\ args
117 nua ps (FnCase n args sc) = nub (nu' (ps ++ args) sc) \\ args
118 nua ps (ConstCase _ sc) = nu' ps sc
119 nua ps (SucCase _ sc) = nu' ps sc
120 nua ps (DefaultCase sc) = nu' ps sc
122 nut ps (P _ n _) | n `elem` ps = []
124 nut ps (App f a) = nut ps f ++ nut ps a
125 nut ps (Proj t _) = nut ps t
126 nut ps (Bind n (Let t v) sc) = nut ps v ++ nut (n:ps) sc
127 nut ps (Bind n b sc) = nut (n:ps) sc
130-- Return all called functions, and which arguments are used in each argument position
131-- for the call, in order to help reduce compilation time, and trace all unused
134findCalls :: SC -> [Name] -> [(Name, [[Name]])]
135findCalls sc topargs = nub $ nu' topargs sc where
136 nu' ps (Case n alts) = nub (concatMap (nua (n : ps)) alts)
137 nu' ps (ProjCase t alts) = nub $ nut ps t ++ concatMap (nua ps) alts
138 nu' ps (STerm t) = nub $ nut ps t
141 nua ps (ConCase n i args sc) = nub (nu' (ps ++ args) sc)
142 nua ps (FnCase n args sc) = nub (nu' (ps ++ args) sc)
143 nua ps (ConstCase _ sc) = nu' ps sc
144 nua ps (SucCase _ sc) = nu' ps sc
145 nua ps (DefaultCase sc) = nu' ps sc
147 nut ps (P Ref n _) | n `elem` ps = []
148 | otherwise = [(n, [])] -- tmp
150 | (P Ref n _, args) <- unApply fn
151 = if n `elem` ps then nut ps f ++ nut ps a
152 else [(n, map argNames args)] ++ concatMap (nut ps) args
153 | (P (TCon _ _) n _, _) <- unApply fn = []
154 | otherwise = nut ps f ++ nut ps a
155 nut ps (Bind n (Let t v) sc) = nut ps v ++ nut (n:ps) sc
156 nut ps (Proj t _) = nut ps t
157 nut ps (Bind n b sc) = nut (n:ps) sc
160 argNames tm = let ns = directUse tm in
161 filter (\x -> x `elem` ns) topargs
163-- Find names which are used directly (i.e. not in a function call) in a term
165directUse :: TT Name -> [Name]
166directUse (P _ n _) = [n]
167directUse (Bind n (Let t v) sc) = nub $ directUse v ++ (directUse sc \\ [n])
169directUse (Bind n b sc) = nub $ directUse (binderTy b) ++ (directUse sc \\ [n])
170directUse fn@(App f a)
171 | (P Ref (UN pfk) _, [App e w]) <- unApply fn,
172 pfk == txt "prim_fork"
173 = directUse e ++ directUse w -- HACK so that fork works
174 | (P Ref (UN fce) _, [_, _, a]) <- unApply fn,
176 = directUse a -- forcing a value counts as a use
177 | (P Ref n _, args) <- unApply fn = [] -- need to know what n does with them
178 | (P (TCon _ _) n _, args) <- unApply fn = [] -- type constructors not used at runtime
179 | otherwise = nub $ directUse f ++ directUse a
180directUse (Proj x i) = nub $ directUse x
183-- Find all directly used arguments (i.e. used but not in function calls)
185findUsedArgs :: SC -> [Name] -> [Name]
186findUsedArgs sc topargs = nub (findAllUsedArgs sc topargs)
188findAllUsedArgs sc topargs = filter (\x -> x `elem` topargs) (nu' sc) where
189 nu' (Case n alts) = n : concatMap nua alts
190 nu' (ProjCase t alts) = directUse t ++ concatMap nua alts
191 nu' (STerm t) = directUse t
194 nua (ConCase n i args sc) = nu' sc
195 nua (FnCase n args sc) = nu' sc
196 nua (ConstCase _ sc) = nu' sc
197 nua (SucCase _ sc) = nu' sc
198 nua (DefaultCase sc) = nu' sc
200-- Return whether name is used anywhere in a case tree
201isUsed :: SC -> Name -> Bool
202isUsed sc n = used sc where
204 used (Case n' alts) = n == n' || or (map usedA alts)
205 used (ProjCase t alts) = n `elem` freeNames t || or (map usedA alts)
206 used (STerm t) = n `elem` freeNames t
209 usedA (ConCase _ _ args sc) = used sc
210 usedA (FnCase _ args sc) = used sc
211 usedA (ConstCase _ sc) = used sc
212 usedA (SucCase _ sc) = used sc
213 usedA (DefaultCase sc) = used sc
216data Phase = CompileTime | RunTime
219-- Generate a simple case tree
222simpleCase :: Bool -> Bool -> Bool ->
223 Phase -> FC -> [Int] -> [Type] ->
224 [([Name], Term, Term)] ->
226simpleCase tc cover reflect phase fc inacc argtys cs
227 = sc' tc cover phase fc (filter (\(_, _, r) ->
232 sc' tc cover phase fc []
233 = return $ CaseDef [] (UnmatchedCase "No pattern clauses") []
234 sc' tc cover phase fc cs
235 = let proj = phase == RunTime
236 vnames = fstT (head cs)
237 pats = map (\ (avs, l, r) ->
238 (avs, toPats reflect tc l, (l, r))) cs
239 chkPats = mapM chkAccessible pats in
242 let numargs = length (fst (head pats))
243 ns = take numargs args
244 (ns', ps') = order [(n, i `elem` inacc) | (i,n) <- zip [0..] ns] pats
245 (tree, st) = runState
246 (match ns' ps' (defaultCase cover))
248 t = CaseDef ns (prune proj (depatt ns' tree)) (fstT st) in
249 if proj then return (stripLambdas t)
251-- FIXME: This check is not quite right in some cases, and is breaking
252-- perfectly valid code!
253-- if checkSameTypes (lstT st) tree
255-- else Error (At fc (Msg "Typecase is not allowed"))
256 Error err -> Error (At fc err)
257 where args = map (\i -> sMN i "e") [0..]
258 defaultCase True = STerm Erased
259 defaultCase False = UnmatchedCase "Error"
263 chkAccessible (avs, l, c)
264 | phase == RunTime || reflect = return (l, c)
265 | otherwise = do mapM_ (acc l) avs
268 acc [] n = Error (Inaccessible n)
269 acc (PV x t : xs) n | x == n = OK ()
270 acc (PCon _ _ ps : xs) n = acc (ps ++ xs) n
271 acc (PSuc p : xs) n = acc (p : xs) n
272 acc (_ : xs) n = acc xs n
274-- For each 'Case', make sure every choice is in the same type family,
275-- as directed by the variable type (i.e. there is no implicit type casing
278checkSameTypes :: [(Name, Type)] -> SC -> Bool
279checkSameTypes tys (Case n alts)
280 = case lookup n tys of
281 Just t -> and (map (checkAlts t) alts)
282 _ -> and (map ((checkSameTypes tys).getSC) alts)
284 checkAlts t (ConCase n _ _ sc) = isType n t && checkSameTypes tys sc
285 checkAlts (Constant t) (ConstCase c sc) = isConstType c t && checkSameTypes tys sc
286 checkAlts _ (ConstCase c sc) = False
289 getSC (ConCase _ _ _ sc) = sc
290 getSC (FnCase _ _ sc) = sc
291 getSC (ConstCase _ sc) = sc
292 getSC (SucCase _ sc) = sc
293 getSC (DefaultCase sc) = sc
294checkSameTypes _ _ = True
296-- FIXME: All we're actually doing here is checking that we haven't arrived
297-- at a specific constructor for a polymorphic argument. I *think* this
298-- is sufficient, but if it turns out not to be, fix it!
299isType n t | (P (TCon _ _) _ _, _) <- unApply t = True
300isType n t | (P Ref _ _, _) <- unApply t = True
303isConstType (I _) (AType (ATInt ITNative)) = True
304isConstType (BI _) (AType (ATInt ITBig)) = True
305isConstType (Fl _) (AType ATFloat) = True
306isConstType (Ch _) (AType (ATInt ITChar)) = True
307isConstType (Str _) StrType = True
308isConstType (B8 _) (AType (ATInt _)) = True
309isConstType (B16 _) (AType (ATInt _)) = True
310isConstType (B32 _) (AType (ATInt _)) = True
311isConstType (B64 _) (AType (ATInt _)) = True
312isConstType (B8V _) (AType (ATInt _)) = True
313isConstType (B16V _) (AType (ATInt _)) = True
314isConstType (B32V _) (AType (ATInt _)) = True
315isConstType (B64V _) (AType (ATInt _)) = True
316isConstType _ _ = False
318data Pat = PCon Name Int [Pat]
321 | PSuc Pat -- special case for n+1 on Integer
322 | PReflected Name [Pat]
324 | PTyPat -- typecase, not allowed, inspect last
327-- If there are repeated variables, take the *last* one (could be name shadowing
328-- in a where clause, so take the most recent).
330toPats :: Bool -> Bool -> Term -> [Pat]
331toPats reflect tc f = reverse (toPat reflect tc (getArgs f)) where
332 getArgs (App f a) = a : getArgs f
335toPat :: Bool -> Bool -> [Term] -> [Pat]
336toPat reflect tc tms = evalState (mapM (\x -> toPat' x []) tms) []
338 toPat' (P (DCon t a) n _) args = do args' <- mapM (\x -> toPat' x []) args
339 return $ PCon n t args'
341 toPat' (P _ (UN pabi) _)
342 [p, Constant (BI 1)] | pabi == txt "prim__addBigInt"
343 = do p' <- toPat' p []
346-- toPat' (P (TCon t a) n _) args | tc
347-- = do args' <- mapM (\x -> toPat' x []) args
348-- return $ PCon n t args'
349-- toPat' (Constant (AType (ATInt ITNative))) []
350-- | tc = return $ PCon (UN "Int") 1 []
351-- toPat' (Constant (AType ATFloat)) [] | tc = return $ PCon (UN "Float") 2 []
352-- toPat' (Constant (AType (ATInt ITChar))) [] | tc = return $ PCon (UN "Char") 3 []
353-- toPat' (Constant StrType) [] | tc = return $ PCon (UN "String") 4 []
354-- toPat' (Constant PtrType) [] | tc = return $ PCon (UN "Ptr") 5 []
355-- toPat' (Constant (AType (ATInt ITBig))) []
356-- | tc = return $ PCon (UN "Integer") 6 []
357-- toPat' (Constant (AType (ATInt (ITFixed n)))) []
358-- | tc = return $ PCon (UN (fixedN n)) (7 + fromEnum n) [] -- 7-10 inclusive
359 toPat' (P Bound n ty) [] = -- trace (show (n, ty)) $
365 toPat' (App f a) args = toPat' f (a : args)
366 toPat' (Constant (AType _)) [] = return PTyPat
367 toPat' (Constant StrType) [] = return PTyPat
368 toPat' (Constant PtrType) [] = return PTyPat
369 toPat' (Constant VoidType) [] = return PTyPat
370 toPat' (Constant x) [] = return $ PConst x
371 toPat' (Bind n (Pi t) sc) [] | reflect && noOccurrence n sc
372 = do t' <- toPat' t []
374 return $ PReflected (sUN "->") (t':sc':[])
375 toPat' (P _ n _) args | reflect
376 = do args' <- mapM (\x -> toPat' x []) args
377 return $ PReflected n args'
378 toPat' t _ = return PAny
381 fixedN IT16 = "Bits16"
382 fixedN IT32 = "Bits32"
383 fixedN IT64 = "Bits64"
386data Partition = Cons [Clause]
390isVarPat (PV _ _ : ps , _) = True
391isVarPat (PAny : ps , _) = True
392isVarPat (PTyPat : ps , _) = True
395isConPat (PCon _ _ _ : ps, _) = True
396isConPat (PReflected _ _ : ps, _) = True
397isConPat (PSuc _ : ps, _) = True
398isConPat (PConst _ : ps, _) = True
401partition :: [Clause] -> [Partition]
404 | isVarPat m = let (vars, rest) = span isVarPat ms in
405 Vars vars : partition rest
406 | isConPat m = let (cons, rest) = span isConPat ms in
407 Cons cons : partition rest
408partition xs = error $ "Partition " ++ show xs
410-- reorder the patterns so that the one with most distinct names
411-- comes next. Take rightmost first, otherwise (i.e. pick value rather
414-- The first argument means [(Name, IsInaccessible)].
416order :: [(Name, Bool)] -> [Clause] -> ([Name], [Clause])
417order [] cs = ([], cs)
418order ns' [] = (map fst ns', [])
419order ns' cs = let patnames = transpose (map (zip ns') (map fst cs))
420 -- note: sortBy . reverse is not nonsense because sortBy is stable
421 pats' = transpose (sortBy moreDistinct (reverse patnames)) in
422 (getNOrder pats', zipWith rebuild pats' cs)
424 getNOrder [] = error $ "Failed order on " ++ show (map fst ns', cs)
425 getNOrder (c : _) = map (fst . fst) c
427 rebuild patnames clause = (map snd patnames, snd clause)
429 -- this compares (+isInaccessible, -numberOfCases)
430 moreDistinct xs ys = compare (snd . fst . head $ xs, numNames [] (map snd ys))
431 (snd . fst . head $ ys, numNames [] (map snd xs))
433 numNames xs (PCon n _ _ : ps)
434 | not (Left n `elem` xs) = numNames (Left n : xs) ps
435 numNames xs (PConst c : ps)
436 | not (Right c `elem` xs) = numNames (Right c : xs) ps
437 numNames xs (_ : ps) = numNames xs ps
438 numNames xs [] = length xs
440match :: [Name] -> [Clause] -> SC -- error case
442match [] (([], ret) : xs) err
443 = do (ts, v, ntys) <- get
444 put (ts ++ (map (fst.snd) xs), v, ntys)
446 Impossible -> return ImpossibleCase
447 tm -> return $ STerm tm -- run out of arguments
448match vs cs err = do let ps = partition cs
451mixture :: [Name] -> [Partition] -> SC -> State CS SC
452mixture vs [] err = return err
453mixture vs (Cons ms : ps) err = do fallthrough <- mixture vs ps err
454 conRule vs ms fallthrough
455mixture vs (Vars ms : ps) err = do fallthrough <- mixture vs ps err
456 varRule vs ms fallthrough
458data ConType = CName Name Int -- named constructor
459 | CFn Name -- reflected function name
461 | CConst Const -- constant, not implemented yet
464data Group = ConGroup ConType -- Constructor
465 [([Pat], Clause)] -- arguments and rest of alternative
468conRule :: [Name] -> [Clause] -> SC -> State CS SC
469conRule (v:vs) cs err = do groups <- groupCons cs
470 caseGroups (v:vs) groups err
472caseGroups :: [Name] -> [Group] -> SC -> State CS SC
473caseGroups (v:vs) gs err = do g <- altGroups gs
474 return $ Case v (sort g)
476 altGroups [] = return [DefaultCase err]
477 altGroups (ConGroup (CName n i) args : cs)
478 = do g <- altGroup n i args
481 altGroups (ConGroup (CFn n) args : cs)
482 = do g <- altFnGroup n args
485 altGroups (ConGroup CSuc args : cs)
486 = do g <- altSucGroup args
489 altGroups (ConGroup (CConst c) args : cs)
490 = do g <- altConstGroup c args
494 altGroup n i gs = do (newArgs, nextCs) <- argsToAlt gs
495 matchCs <- match (newArgs ++ vs) nextCs err
496 return $ ConCase n i newArgs matchCs
497 altFnGroup n gs = do (newArgs, nextCs) <- argsToAlt gs
498 matchCs <- match (newArgs ++ vs) nextCs err
499 return $ FnCase n newArgs matchCs
500 altSucGroup gs = do ([newArg], nextCs) <- argsToAlt gs
501 matchCs <- match (newArg:vs) nextCs err
502 return $ SucCase newArg matchCs
503 altConstGroup n gs = do (_, nextCs) <- argsToAlt gs
504 matchCs <- match vs nextCs err
505 return $ ConstCase n matchCs
507argsToAlt :: [([Pat], Clause)] -> State CS ([Name], [Clause])
508argsToAlt [] = return ([], [])
509argsToAlt rs@((r, m) : rest)
510 = do newArgs <- getNewVars r
511 return (newArgs, addRs rs)
513 getNewVars [] = return []
514 getNewVars ((PV n t) : ns) = do v <- getVar "e"
516 put (cs, i, (v, t) : ntys)
519 getNewVars (PAny : ns) = do v <- getVar "i"
522 getNewVars (PTyPat : ns) = do v <- getVar "t"
525 getNewVars (_ : ns) = do v <- getVar "e"
529 addRs ((r, (ps, res)) : rs) = ((r++ps, res) : addRs rs)
531 uniq i (UN n) = MN i n
534getVar :: String -> State CS Name
535getVar b = do (t, v, ntys) <- get; put (t, v+1, ntys); return (sMN v b)
537groupCons :: [Clause] -> State CS [Group]
538groupCons cs = gc [] cs
540 gc acc [] = return acc
541 gc acc ((p : ps, res) : cs) =
542 do acc' <- addGroup p ps res acc
544 addGroup p ps res acc = case p of
545 PCon con i args -> return $ addg (CName con i) args (ps, res) acc
546 PConst cval -> return $ addConG cval (ps, res) acc
547 PSuc n -> return $ addg CSuc [n] (ps, res) acc
548 PReflected fn args -> return $ addg (CFn fn) args (ps, res) acc
549 pat -> fail $ show pat ++ " is not a constructor or constant (can't happen)"
551 addg c conargs res []
552 = [ConGroup c [(conargs, res)]]
553 addg c conargs res (g@(ConGroup c' cs):gs)
554 | c == c' = ConGroup c (cs ++ [(conargs, res)]) : gs
555 | otherwise = g : addg c conargs res gs
557 addConG con res [] = [ConGroup (CConst con) [([], res)]]
558 addConG con res (g@(ConGroup (CConst n) cs) : gs)
559 | con == n = ConGroup (CConst n) (cs ++ [([], res)]) : gs
560-- | otherwise = g : addConG con res gs
561 addConG con res (g : gs) = g : addConG con res gs
563varRule :: [Name] -> [Clause] -> SC -> State CS SC
564varRule (v : vs) alts err =
565 do alts' <- mapM (repVar v) alts
568 repVar v (PV p ty : ps , (lhs, res))
569 = do (cs, i, ntys) <- get
570 put (cs, i, (v, ty) : ntys)
571 return (ps, (lhs, subst p (P Bound v ty) res))
572 repVar v (PAny : ps , res) = return (ps, res)
573 repVar v (PTyPat : ps , res) = return (ps, res)
575-- fix: case e of S k -> f (S k) ==> case e of S k -> f e
577depatt :: [Name] -> SC -> SC
578depatt ns tm = dp [] tm
580 dp ms (STerm tm) = STerm (applyMaps ms tm)
581 dp ms (Case x alts) = Case x (map (dpa ms x) alts)
584 dpa ms x (ConCase n i args sc)
585 = ConCase n i args (dp ((x, (n, args)) : ms) sc)
586 dpa ms x (FnCase n args sc)
587 = FnCase n args (dp ((x, (n, args)) : ms) sc)
588 dpa ms x (ConstCase c sc) = ConstCase c (dp ms sc)
589 dpa ms x (SucCase n sc) = SucCase n (dp ms sc)
590 dpa ms x (DefaultCase sc) = DefaultCase (dp ms sc)
592 applyMaps ms f@(App _ _)
593 | (P nt cn pty, args) <- unApply f
594 = let args' = map (applyMaps ms) args in
595 applyMap ms nt cn pty args'
597 applyMap [] nt cn pty args' = mkApp (P nt cn pty) args'
598 applyMap ((x, (n, args)) : ms) nt cn pty args'
599 | and ((length args == length args') :
600 (n == cn) : zipWith same args args') = P Ref x Erased
601 | otherwise = applyMap ms nt cn pty args'
602 same n (P _ n' _) = n == n'
605 applyMaps ms (App f a) = App (applyMaps ms f) (applyMaps ms a)
608-- FIXME: Do this for SucCase too
609prune :: Bool -- ^ Convert single branches to projections (only useful at runtime)
611prune proj (Case n alts) = case alts' of
614 -- Projection transformations prevent us from seeing some uses of ctor fields
615 -- because they delete information about which ctor is being used.
617 -- f (X x) = ... x ...
621 -- Hence, we disable this step.
622 -- TODO: re-enable this in toIR
624 -- as@[ConCase cn i args sc]
625 -- | proj -> mkProj n 0 args (prune proj sc)
626 -- mkProj n i xs sc = foldr (\x -> projRep x n i) sc xs
628 -- If none of the args are used in the sc, however, we can just replace it
630 as@[ConCase cn i args sc]
631 | proj -> let sc' = prune proj sc in
632 if any (isUsed sc') args
633 then Case n [ConCase cn i args sc']
638 -> projRep cn n (-1) $ prune proj sc
643 -- Bit of a hack here! The default case will always be 0, make sure
644 -- it gets caught first.
645 [s@(SucCase _ _), DefaultCase dc]
646 -> Case n [ConstCase (BI 0) dc, s]
650 alts' = filter (not . erased) $ map pruneAlt alts
652 pruneAlt (ConCase cn i ns sc) = ConCase cn i ns (prune proj sc)
653 pruneAlt (FnCase cn ns sc) = FnCase cn ns (prune proj sc)
654 pruneAlt (ConstCase c sc) = ConstCase c (prune proj sc)
655 pruneAlt (SucCase n sc) = SucCase n (prune proj sc)
656 pruneAlt (DefaultCase sc) = DefaultCase (prune proj sc)
658 erased (DefaultCase (STerm Erased)) = True
659 erased (DefaultCase ImpossibleCase) = True
662 projRep :: Name -> Name -> Int -> SC -> SC
663 projRep arg n i (Case x alts) | x == arg
664 = ProjCase (Proj (P Bound n Erased) i) $ map (projRepAlt arg n i) alts
665 projRep arg n i (Case x alts)
666 = Case x (map (projRepAlt arg n i) alts)
667 projRep arg n i (ProjCase t alts)
668 = ProjCase (projRepTm arg n i t) $ map (projRepAlt arg n i) alts
669 projRep arg n i (STerm t) = STerm (projRepTm arg n i t)
670 projRep arg n i c = c
672 projRepAlt arg n i (ConCase cn t args rhs)
673 = ConCase cn t args (projRep arg n i rhs)
674 projRepAlt arg n i (FnCase cn args rhs)
675 = FnCase cn args (projRep arg n i rhs)
676 projRepAlt arg n i (ConstCase t rhs)
677 = ConstCase t (projRep arg n i rhs)
678 projRepAlt arg n i (SucCase sn rhs)
679 = SucCase sn (projRep arg n i rhs)
680 projRepAlt arg n i (DefaultCase rhs)
681 = DefaultCase (projRep arg n i rhs)
683 projRepTm arg n i t = subst arg (Proj (P Bound n Erased) i) t
687stripLambdas :: CaseDef -> CaseDef
688stripLambdas (CaseDef ns (STerm (Bind x (Lam _) sc)) tm)
689 = stripLambdas (CaseDef (ns ++ [x]) (STerm (instantiate (P Bound x Erased) sc)) tm)
692substSC :: Name -> Name -> SC -> SC
693substSC n repl (Case n' alts)
694 | n == n' = Case repl (map (substAlt n repl) alts)
695 | otherwise = Case n' (map (substAlt n repl) alts)
696substSC n repl (STerm t) = STerm $ subst n (P Bound repl Erased) t
697substSC n repl sc = error $ "unsupported in substSC: " ++ show sc
699substAlt :: Name -> Name -> CaseAlt -> CaseAlt
700substAlt n repl (ConCase cn a ns sc) = ConCase cn a ns (substSC n repl sc)
701substAlt n repl (FnCase fn ns sc) = FnCase fn ns (substSC n repl sc)
702substAlt n repl (ConstCase c sc) = ConstCase c (substSC n repl sc)
703substAlt n repl (SucCase n' sc)
704 | n == n' = SucCase n (substSC n repl sc)
705 | otherwise = SucCase n' (substSC n repl sc)
706substAlt n repl (DefaultCase sc) = DefaultCase (substSC n repl sc)
708mkForce :: Name -> Name -> SC -> SC
711 mkForceSC n arg (Case x alts) | x == arg
712 = ProjCase (forceArg n) $ map (mkForceAlt n arg) alts
714 mkForceSC n arg (Case x alts)
715 = Case x (map (mkForceAlt n arg) alts)
717 mkForceSC n arg (ProjCase t alts)
718 = ProjCase (forceTm n arg t) $ map (mkForceAlt n arg) alts
720 mkForceSC n arg (STerm t) = STerm (forceTm n arg t)
721 mkForceSC n arg c = c
723 mkForceAlt n arg (ConCase cn t args rhs)
724 = ConCase cn t args (mkForceSC n arg rhs)
725 mkForceAlt n arg (FnCase cn args rhs)
726 = FnCase cn args (mkForceSC n arg rhs)
727 mkForceAlt n arg (ConstCase t rhs)
728 = ConstCase t (mkForceSC n arg rhs)
729 mkForceAlt n arg (SucCase sn rhs)
730 = SucCase sn (mkForceSC n arg rhs)
731 mkForceAlt n arg (DefaultCase rhs)
732 = DefaultCase (mkForceSC n arg rhs)
734 forceTm n arg t = subst arg (forceArg n) t
736 forceArg n = App (App (App (P Ref (sUN "Force") Erased) Erased) Erased)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list