on 4ddbc65 help sign in
Idris-dev/src/Idris/Coverage.hs
597 lines 25 kB history raw
1{-# LANGUAGE PatternGuards #-}
3module Idris.Coverage where
5import Core.TT
6import Core.Evaluate
7import Core.CaseTree
9import Idris.AbsSyntax
10import Idris.Delaborate
11import Idris.Error
13import Data.List
14import Data.Either
15import Data.Maybe
16import Debug.Trace
18import Control.Monad.State
20-- Generate the LHSes which are missing from a case tree
21-- Eliminate the ones which cannot be well typed
23genMissing :: Name -> [Name] -> SC -> Idris [PTerm]
24genMissing fn args sc
25 = do sc' <- expandTree sc
26 logLvl 5 $ "Checking missing cases for " ++
27 show fn ++ "\n" ++ (show sc')
28 (got, missing) <- gm fn (map (\x -> P Bound x Erased) args) sc'
29 return $ filter (\x -> not (x `elem` got)) missing
31-- Make a term to feed to the pattern matcher from a LHS declared impossible
32-- (we can't type check it, but we need the case analysis to check for
33-- covering...)
35mkPatTm :: PTerm -> Idris Term
36mkPatTm t = do i <- get
37 let timp = addImpl' True [] i t
38 evalStateT (toTT timp) 0
39 where
40 toTT (PRef _ n) = do i <- lift $ get
41 case lookupDef Nothing n (tt_ctxt i) of
42 [TyDecl nt _] -> return $ P nt n Erased
43 _ -> return $ P Ref n Erased
44 toTT (PApp _ t args) = do t' <- toTT t
45 args' <- mapM (toTT . getTm) args
46 return $ mkApp t' args'
47 toTT _ = do v <- get
48 put (v + 1)
49 return (P Bound (MN v "imp") Erased)
51mkPTerm :: Name -> [TT Name] -> Idris PTerm
52mkPTerm f args = do i <- get
53 let fapp = mkApp (P Bound f Erased) (map eraseName args)
54 return $ delab i fapp
55 where eraseName (App f a) = App (eraseName f) (eraseName a)
56 eraseName (P _ (MN _ _) _) = Erased
57 eraseName t = t
59gm :: Name -> [TT Name] -> SC -> Idris ([PTerm], [PTerm])
60gm fn args (Case n alts) = do m <- mapM (gmAlt fn args n) alts
61 let (got, missing) = unzip m
62 return (concat got, concat missing)
63gm fn args (STerm tm) = do logLvl 3 ("Covered: " ++ show args)
64 t <- mkPTerm fn args
65 return ([t], [])
66gm fn args ImpossibleCase = do logLvl 3 ("Impossible: " ++ show args)
67 t <- mkPTerm fn args
68 return ([], [])
69gm fn args (UnmatchedCase _) = do logLvl 3 ("Missing: " ++ show args)
70 t <- mkPTerm fn args
71 return ([], [t])
73gmAlt fn args n (ConCase cn t cargs sc)
74 = do let args' = map (subst n (mkApp (P Bound cn Erased)
75 (map (\x -> P Bound x Erased) cargs)))
76 args
77 gm fn args' sc
78gmAlt fn args n (ConstCase c sc)
79 = do let args' = map (subst n (Constant c)) args
80 gm fn args' sc
81gmAlt fn args n (DefaultCase sc)
82 = do gm fn args sc
84getDefault (DefaultCase sc : _) = sc
85getDefault (_ : cs) = getDefault cs
86getDefault [] = UnmatchedCase ""
88dropDefault (DefaultCase sc : rest) = dropDefault rest
89dropDefault (c : cs) = c : dropDefault cs
90dropDefault [] = []
92expandTree :: SC -> Idris SC
93expandTree (Case n alts) = do i <- get
94 as <- expandAlts i (dropDefault alts)
95 (getDefault alts)
96 alts' <- mapM expandTreeA as
97 return (Case n alts')
98 where expandTreeA (ConCase n i ns sc) = do sc' <- expandTree sc
99 return (ConCase n i ns sc')
100 expandTreeA (ConstCase i sc) = do sc' <- expandTree sc
101 return (ConstCase i sc')
102 expandTreeA (DefaultCase sc) = do sc' <- expandTree sc
103 return (DefaultCase sc')
104expandTree t = return t
106expandAlts :: IState -> [CaseAlt] -> SC -> Idris [CaseAlt]
107expandAlts i all@(ConstCase c _ : alts) def
108 = return $ all ++ [DefaultCase def]
109expandAlts i all@(ConCase n _ _ _ : alts) def
110 | (TyDecl c@(DCon _ arity) ty : _) <- lookupDef Nothing n (tt_ctxt i)
111 = do let tyn = getTy n (tt_ctxt i)
112 case lookupCtxt Nothing tyn (idris_datatypes i) of
113 (TI ns _ : _) -> do let ps = map mkPat ns
114 return $ addAlts ps (altsFor all) all
115 _ -> return all
116 where
117 altsFor [] = []
118 altsFor (ConCase n _ _ _ : alts) = n : altsFor alts
119 altsFor (_ : alts) = altsFor alts
121 addAlts [] got alts = alts
122 addAlts ((n, arity) : ps) got alts
123 | n `elem` got = addAlts ps got alts
124 | otherwise = addAlts ps got (alts ++
125 [ConCase n (-1) (argList arity) def])
127 argList i = take i (map (\x -> (MN x "ign")) [0..])
129 getTy n ctxt
130 = case lookupTy Nothing n ctxt of
131 (t : _) -> case unApply (getRetTy t) of
132 (P _ tyn _, _) -> tyn
133 x -> error $ "Can't happen getTy 1 " ++ show (n, x)
134 _ -> error "Can't happen getTy 2"
136 mkPat x = case lookupCtxt Nothing x (idris_implicits i) of
137 (pargs : _)
138 -> (x, length pargs)
139 _ -> error "Can't happen - genAll"
140expandAlts i alts def = return alts
144-- OLD STUFF: probably broken...
146-- Given a list of LHSs, generate a extra clauses which cover the remaining
147-- cases. The ones which haven't been provided are marked 'absurd' so that the
148-- checker will make sure they can't happen.
150-- This will only work after the given clauses have been typechecked and the
151-- names are fully explicit!
153genClauses :: FC -> Name -> [Term] -> [PClause] -> Idris [PTerm]
154genClauses fc n xs given
155 = do i <- getIState
156 let lhss = map (getLHS i) xs
157 let argss = transpose lhss
158 let all_args = map (genAll i) argss
159 logLvl 7 $ "COVERAGE of " ++ show n
160 logLvl 10 $ show argss ++ "\n" ++ show all_args
161 logLvl 10 $ "Original: \n" ++
162 showSep "\n" (map (\t -> showImp True (delab' i t True)) xs)
163 let parg = case lookupCtxt Nothing n (idris_implicits i) of
164 (p : _) -> p
165 _ -> repeat (pexp Placeholder)
166 let tryclauses = mkClauses parg all_args
167 let new = mnub i $ filter (noMatch i) tryclauses
168 logLvl 7 $ "New clauses: \n" ++ showSep "\n" (map (showImp True) new)
169-- ++ " from:\n" ++ showSep "\n" (map (showImp True) tryclauses)
170 return new
171-- return (map (\t -> PClause n t [] PImpossible []) new)
172 where getLHS i term
173 | (f, args) <- unApply term = map (\t -> delab' i t True) args
174 | otherwise = []
176 lhsApp (PClause _ _ l _ _ _) = l
177 lhsApp (PWith _ _ l _ _ _) = l
179 mnub i [] = []
180 mnub i (x : xs) =
181 if (any (\t -> case matchClause i x t of
182 Right _ -> True
183 Left _ -> False) xs) then mnub i xs
184 else x : mnub i xs
186 noMatch i tm = all (\x -> case matchClause i (delab' i x True) tm of
187 Right _ -> False
188 Left miss -> True) xs
191 mkClauses :: [PArg] -> [[PTerm]] -> [PTerm]
192 mkClauses parg args
193 | all (== [Placeholder]) args = []
194 mkClauses parg args
195 = do args' <- mkArg args
196 let tm = PApp fc (PRef fc n) (zipWith upd args' parg)
197 return tm
198 where
199 mkArg :: [[PTerm]] -> [[PTerm]]
200 mkArg [] = return []
201 mkArg (a : as) = do a' <- a
202 as' <- mkArg as
203 return (a':as')
205-- FIXME: Just look for which one is the deepest, then generate all
206-- possibilities up to that depth.
208genAll :: IState -> [PTerm] -> [PTerm]
209genAll i args = case filter (/=Placeholder) $ concatMap otherPats (nub args) of
210 [] -> [Placeholder]
211 xs -> xs
212 where
213 conForm (PApp _ (PRef fc n) _) = isConName Nothing n (tt_ctxt i)
214 conForm (PRef fc n) = isConName Nothing n (tt_ctxt i)
215 conForm _ = False
217 otherPats :: PTerm -> [PTerm]
218 otherPats o@(PRef fc n) = ops fc n [] o
219 otherPats o@(PApp _ (PRef fc n) xs) = ops fc n xs o
220 otherPats arg = return Placeholder
222 ops fc n xs o
223 | (TyDecl c@(DCon _ arity) ty : _) <- lookupDef Nothing n (tt_ctxt i)
224 = do xs' <- mapM otherPats (map getTm xs)
225 let p = PApp fc (PRef fc n) (zipWith upd xs' xs)
226 let tyn = getTy n (tt_ctxt i)
227 case lookupCtxt Nothing tyn (idris_datatypes i) of
228 (TI ns _ : _) -> p : map (mkPat fc) (ns \\ [n])
229 _ -> [p]
230 ops fc n arg o = return Placeholder
232 getTy n ctxt = case lookupTy Nothing n ctxt of
233 (t : _) -> case unApply (getRetTy t) of
234 (P _ tyn _, _) -> tyn
235 x -> error $ "Can't happen getTy 1 " ++ show (n, x)
236 _ -> error "Can't happen getTy 2"
238 mkPat fc x = case lookupCtxt Nothing x (idris_implicits i) of
239 (pargs : _)
240 -> PApp fc (PRef fc x) (map (upd Placeholder) pargs)
241 _ -> error "Can't happen - genAll"
243upd p' p = p { getTm = p' }
245-- Check if, in a given type n, the constructor cn : ty is strictly positive,
246-- and update the context accordingly
248checkPositive :: Name -> (Name, Type) -> Idris ()
249checkPositive n (cn, ty)
250 = do let p = cp ty
251 i <- getIState
252 let tot = if p then Total (args ty) else Partial NotPositive
253 let ctxt' = setTotal cn tot (tt_ctxt i)
254 putIState (i { tt_ctxt = ctxt' })
255 logLvl 5 $ "Constructor " ++ show cn ++ " is " ++ show tot
256 addIBC (IBCTotal cn tot)
257 where
258 args t = [0..length (getArgTys t)-1]
260 cp (Bind n (Pi aty) sc) = posArg aty && cp sc
261 cp t = True
263 posArg (Bind _ (Pi nty) sc)
264 | (P _ n' _, args) <- unApply nty
265 = n /= n' && posArg sc
266 posArg t = True
268-- Totality checking - check for structural recursion
269-- (no mutual definitions yet)
271data LexOrder = LexXX | LexEQ | LexLT
272 deriving (Show, Eq, Ord)
274calcProd :: IState -> FC -> Name -> [([Name], Term, Term)] -> Idris Totality
275calcProd i fc n pats = do patsprod <- mapM prodRec pats
276 if (and patsprod)
277 then return Productive
278 else return (Partial NotProductive)
279 where
280 -- every application of n must be in an argument of a coinductive
281 -- constructor
283 prodRec :: ([Name], Term, Term) -> Idris Bool
284 prodRec (_, _, tm) = prod False tm
286 prod ok ap@(App _ _)
287 | (P _ (UN "lazy") _, [_, arg]) <- unApply ap = prod ok arg
288 | (P _ f ty, args) <- unApply ap
289 = let co = cotype ty in
290 if f == n
291 then do argsprod <- mapM (prod co) args
292 return (and (ok : argsprod) )
293 else do argsprod <- mapM (prod co) args
294 return (and argsprod)
295 prod ok (App f a) = liftM2 (&&) (prod False f) (prod False a)
296 prod ok (Bind _ (Let t v) sc) = liftM2 (&&) (prod False v) (prod False v)
297 prod ok (Bind _ b sc) = prod ok sc
298 prod ok t = return True
300 cotype ty
301 | (P _ t _, _) <- unApply (getRetTy ty)
302 = case lookupCtxt Nothing t (idris_datatypes i) of
303 [TI _ True] -> True
304 _ -> False
305 | otherwise = False
307calcTotality :: [Name] -> FC -> Name -> [([Name], Term, Term)]
308 -> Idris Totality
309calcTotality path fc n pats
310 = do i <- get
311 let opts = case lookupCtxt Nothing n (idris_flags i) of
312 [fs] -> fs
313 _ -> []
314 case mapMaybe (checkLHS i) (map (\ (_, l, r) -> l) pats) of
315 (failure : _) -> return failure
316 _ -> if (Coinductive `elem` opts)
317 then calcProd i fc n pats
318 else checkSizeChange n
319 where
320 checkLHS i (P _ fn _)
321 = case lookupTotal fn (tt_ctxt i) of
322 [Partial _] -> return (Partial (Other [fn]))
323 _ -> Nothing
324 checkLHS i (App f a) = mplus (checkLHS i f) (checkLHS i a)
325 checkLHS _ _ = Nothing
327checkTotality :: [Name] -> FC -> Name -> Idris Totality
328checkTotality path fc n
329 | n `elem` path = return (Partial (Mutual (n : path)))
330 | otherwise = do
331 t <- getTotality n
332 updateContext (simplifyCasedef n)
333 ctxt <- getContext
334 i <- getIState
335 let opts = case lookupCtxt Nothing n (idris_flags i) of
336 [fs] -> fs
337 _ -> []
338 t' <- case t of
339 Unchecked ->
340 case lookupDef Nothing n ctxt of
341 [CaseOp _ _ _ _ pats _ _ _ _] ->
342 do t' <- if AssertTotal `elem` opts
343 then return $ Total []
344 else calcTotality path fc n pats
345 setTotality n t'
346 addIBC (IBCTotal n t')
347 -- if it's not total, it can't reduce, to keep
348 -- typechecking decidable
349 case t' of
350-- FIXME: Put this back when we can handle mutually recursive things
351-- p@(Partial _) ->
352-- do setAccessibility n Frozen
353-- addIBC (IBCAccess n Frozen)
354-- logLvl 5 $ "HIDDEN: "
355-- ++ show n ++ show p
356 _ -> return ()
357 return t'
358 _ -> return $ Total []
359 x -> return x
360 case t' of
361 Total _ -> return t'
362 Productive -> return t'
363 e -> do w <- cmdOptSet WarnPartial
364 if TotalFn `elem` opts
365 then totalityError t'
366 else do when (w && not (PartialFn `elem` opts)) $
367 warnPartial n t'
368 return t'
369 where
370 totalityError t = tclift $ tfail (At fc (Msg (show n ++ " is " ++ show t)))
372 warnPartial n t
373 = do i <- get
374 case lookupDef Nothing n (tt_ctxt i) of
375 [x] -> do
376 iputStrLn $ show fc ++ ":Warning - " ++ show n ++ " is " ++ show t
377-- ++ "\n" ++ show x
378-- let cg = lookupCtxtName Nothing n (idris_callgraph i)
379-- iputStrLn (show cg)
382checkDeclTotality :: (FC, Name) -> Idris Totality
383checkDeclTotality (fc, n)
384 = do logLvl 2 $ "Checking " ++ show n ++ " for totality"
385 buildSCG (fc, n)
386 checkTotality [] fc n
388-- Calculate the size change graph for this definition
390-- SCG for a function f consists of a list of:
391-- (g, [(a1, sizechange1), (a2, sizechange2), ..., (an, sizechangen)])
393-- where g is a function called
394-- a1 ... an are the arguments of f in positions 1..n of g
395-- sizechange1 ... sizechange2 is how their size has changed wrt the input
396-- to f
397-- Nothing, if the argument is unrelated to the input
399buildSCG :: (FC, Name) -> Idris ()
400buildSCG (_, n) = do
401 ist <- get
402 case lookupCtxt Nothing n (idris_callgraph ist) of
403 [cg] -> case lookupDef Nothing n (tt_ctxt ist) of
404 [CaseOp _ _ _ _ _ args sc _ _] ->
405 do logLvl 5 $ "Building SCG for " ++ show n ++ " from\n"
406 ++ show sc
407 let newscg = buildSCG' ist sc args
408 logLvl 5 $ show newscg
409 addToCG n ( cg { scg = newscg } )
410 [] -> logLvl 5 $ "Could not build SCG for " ++ show n ++ "\n"
411 x -> error $ "buildSCG: " ++ show (n, x)
413buildSCG' :: IState -> SC -> [Name] -> [SCGEntry]
414buildSCG' ist sc args = nub $ scg sc (zip args args)
415 (zip args (zip args (repeat Same)))
416 where
417 scg :: SC -> [(Name, Name)] -> -- local var, originating top level var
418 [(Name, (Name, SizeChange))] -> -- orig to new, and relationship
419 [SCGEntry]
420 scg (Case x alts) vars szs
421 = let x' = findTL x vars in
422 concatMap (scgAlt x' vars szs) alts
423 where
424 findTL x vars
425 | Just x' <- lookup x vars
426 = if x' `elem` args then x'
427 else findTL x' vars
428 | otherwise = x
430 scg (STerm tm) vars szs = scgTerm tm vars szs
431 scg _ _ _ = []
433 -- how the arguments relate - either Smaller or Unknown
434 argRels :: Name -> [(Name, SizeChange)]
435 argRels n = let ctxt = tt_ctxt ist
436 [ty] = lookupTy Nothing n ctxt -- must exist!
437 P _ nty _ = fst (unApply (getRetTy ty))
438 args = map snd (getArgTys ty) in
439 map (getRel nty) (map (fst . unApply . getRetTy) args)
440 where
441 getRel ty (P _ n' _) | n' == ty = (n, Smaller)
442 getRel ty _ = (n, Unknown)
444 scgAlt x vars szs (ConCase n _ args sc)
445 -- all args smaller than top variable of x in sc
446 -- (as long as they are in the same type family)
447 | Just tvar <- lookup x vars
448 = let arel = argRels n
449 szs' = zipWith (\arg (_,t) -> (arg, (x, t))) args arel
450 ++ szs
451 vars' = zip args (repeat tvar) ++ vars in
452 scg sc vars' szs'
453 | otherwise = scg sc vars szs
454 scgAlt x vars szs (ConstCase _ sc) = scg sc vars szs
455 scgAlt x vars szs (DefaultCase sc) = scg sc vars szs
457 scgTerm f@(App _ _) vars szs
458 | (P _ (UN "lazy") _, [_, arg]) <- unApply f
459 = scgTerm arg vars szs
460 | (P _ fn _, args) <- unApply f
461 = let rest = concatMap (\x -> scgTerm x vars szs) args in
462 case lookup fn vars of
463 Just _ -> rest
464 Nothing -> (fn, map (mkChange szs) args) : rest
465 scgTerm (App f a) vars szs
466 = scgTerm f vars szs ++ scgTerm a vars szs
467 scgTerm (Bind n (Let t v) e) vars szs
468 = scgTerm v vars szs ++ scgTerm e vars szs
469 scgTerm (Bind n _ e) vars szs
470 = scgTerm e ((n, n) : vars) szs
471 scgTerm (P _ fn _) vars szs
472 = case lookup fn vars of
473 Just _ -> []
474 Nothing -> [(fn, [])]
475 scgTerm _ _ _ = []
477 mkChange :: [(Name, (Name, SizeChange))] -> Term
478 -> Maybe (Int, SizeChange)
479 mkChange szs tm
480 | (P _ (UN "lazy") _, [_, arg]) <- unApply tm = mkChange szs arg
481 | (P _ n ty, _) <- unApply tm -- get higher order args too
482 = do sc <- lookup n szs
483 case sc of
484 (_, Unknown) -> Nothing
485 (o, sc) -> do i <- getArgPos 0 o args
486 return (i, sc)
487 mkChange _ _ = Nothing
489 getArgPos :: Int -> Name -> [Name] -> Maybe Int
490 getArgPos i n [] = Nothing
491 getArgPos i n (x : xs) | n == x = Just i
492 | otherwise = getArgPos (i + 1) n xs
494checkSizeChange :: Name -> Idris Totality
495checkSizeChange n = do
496 ist <- get
497 case lookupCtxt Nothing n (idris_callgraph ist) of
498 [cg] -> do let ms = mkMultiPaths ist [] (scg cg)
499 logLvl 6 ("Multipath for " ++ show n ++ ":\n" ++
500 "from " ++ show (scg cg) ++ "\n" ++
501 showSep "\n" (map show ms))
502 logLvl 6 (show cg)
503 -- every multipath must have an infinitely descending
504 -- thread, then the function terminates
505 -- also need to checks functions called are all total
506 -- (Unchecked is okay as we'll spot problems here)
507 let tot = map (checkMP ist (length (argsdef cg))) ms
508 logLvl 3 $ "Paths for " ++ show n ++ " yield " ++ (show tot)
509 return (noPartial tot)
510 [] -> do logLvl 5 $ "No paths for " ++ show n
511 return Unchecked
513type MultiPath = [SCGEntry]
515mkMultiPaths :: IState -> MultiPath -> [SCGEntry] -> [MultiPath]
516mkMultiPaths ist path [] = [reverse path]
517mkMultiPaths ist path cg
518 = concat (map extend cg)
519 where extend (nextf, args)
520 | (nextf, args) `elem` path = [ reverse ((nextf, args) : path) ]
521 | otherwise
522 = case lookupCtxt Nothing nextf (idris_callgraph ist) of
523 [ncg] -> mkMultiPaths ist ((nextf, args) : path) (scg ncg)
524 _ -> [ reverse ((nextf, args) : path) ]
526-- do (nextf, args) <- cg
527-- if ((nextf, args) `elem` path)
528-- then return (reverse ((nextf, args) : path))
529-- else case lookupCtxt Nothing nextf (idris_callgraph ist) of
530-- [ncg] -> mkMultiPaths ist ((nextf, args) : path) (scg ncg)
531-- _ -> return (reverse ((nextf, args) : path))
533-- If any route along the multipath leads to infinite descent, we're fine.
534-- Try a route beginning with every argument.
535-- If we reach a point we've been to before, but with a smaller value,
536-- that means there is an infinitely descending path.
538checkMP :: IState -> Int -> MultiPath -> Totality
539checkMP ist i mp = if i > 0
540 then collapse (map (tryPath 0 [] mp) [0..i-1])
541 else tryPath 0 [] mp 0
542 where
543 tryPath :: Int -> [(SCGEntry, Int)] -> MultiPath -> Int -> Totality
544 tryPath desc path [] _ = Total []
545-- tryPath desc path ((UN "believe_me", _) : _) arg
546-- = Partial BelieveMe
547 -- if we get to a constructor, it's fine as long as it's strictly positive
548 tryPath desc path ((f, _) :es) arg
549 | [TyDecl (DCon _ _) _] <- lookupDef Nothing f (tt_ctxt ist)
550 = case lookupTotal f (tt_ctxt ist) of
551 [Total _] -> Total []
552 [Partial _] -> Partial (Other [f])
553 x -> error (show x)
554 | [TyDecl (TCon _ _) _] <- lookupDef Nothing f (tt_ctxt ist)
555 = Total []
556-- tryPath desc path (e@(f, []) : es) arg
557-- | [Unchecked] <- lookupTotal f (tt_ctxt ist) =
558-- tryPath (-10000) ((e, desc) : path) es 0
559 tryPath desc path (e@(f, nextargs) : es) arg
560 | Just d <- lookup e path
561 = if (desc - d) > 0
562 then Total []
563 else Partial (Mutual (map (fst . fst) path ++ [f]))
564 | [Unchecked] <- lookupTotal f (tt_ctxt ist) =
565 let argspos = zip nextargs [0..] in
566 collapse' (Partial (Mutual (map (fst . fst) path ++ [f]))) $
567 do (arg, pos) <- argspos
568 case arg of
569 Nothing -> -- don't know, but it's okay if the
570 -- rest definitely terminates without
571 -- any cycles with route so far
572 map (tryPath (-10000) ((e, desc):path) es)
573 [0..length nextargs - 1]
574 Just (nextarg, sc) ->
575 case sc of
576 Same -> return $ tryPath desc ((e, desc):path)
577 es
578 nextarg
579 Smaller -> return $ tryPath (desc+1)
580 ((e, desc):path)
581 es
582 nextarg
583 _ -> trace ("Shouldn't happen " ++ show e) $
584 return (Partial Itself)
585 | [Total _] <- lookupTotal f (tt_ctxt ist) = Total []
586 | [Partial _] <- lookupTotal f (tt_ctxt ist) = Partial (Other [f])
587 | otherwise = Total []
589noPartial (Partial p : xs) = Partial p
590noPartial (_ : xs) = noPartial xs
591noPartial [] = Total []
593collapse xs = collapse' (Partial Itself) xs
594collapse' def (Total r : xs) = Total r
595collapse' def (d : xs) = collapse' d xs
596collapse' def [] = def