@@ -198,9 +198,10 @@ calcTotality path fc n pats
198198 = do orders <- mapM ctot pats
199199 let order = sortBy cmpOrd $ concat orders
200200 let (errs, valid) = partitionEithers order
201− let lex = stripXX valid
201+ let lex = stripNoLT (stripXX valid)
202202 case errs of
203− [] -> do logLvl 3 $ show n ++ ":\n" ++ showSep "\n" (map show lex)
203+ [] -> do logLvl 3 $ show n ++ ":\n" ++ showSep "\n" (map show lex)
204+ logLvl 10 $ show pats
204205 checkDecreasing lex
205206 (e : _) -> return e -- FIXME: should probably combine them
206207 where
@@ -216,13 +217,16 @@ calcTotality path fc n pats
216217 dec [] = False
217218 dec (LexLT : _) = True
218219 dec (LexEQ : xs) = dec xs
219− dec (LexXX : _) = False
220+ dec (LexXX : xs) = False
220221
221222 stripXX [] = []
222223 stripXX v@(c : cs)
223224 = case span (==LexXX) c of
224225 (ns, rest) -> map (drop (length ns)) v
225226
227+ -- argument positions which are never LT are no use to us
228+ stripNoLT xs = transpose (filter (any (==LexLT)) (transpose xs))
229+
226230 ctot (lhs, rhs)
227231 | (_, args) <- unApply lhs
228232 = do -- check lhs doesn't use any dodgy names
@@ -232,7 +236,7 @@ calcTotality path fc n pats
232236 chkOrd ords args (Bind n (Let t v) sc)
233237 = do ov <- chkOrd ords args v
234238 chkOrd ov args sc
235− chkOrd ords args (Bind n b sc) = chkOrd ords args sc
239+ chkOrd ords args (Bind n b sc) = chkOrd ords (args ++ [P Ref n Erased]) sc
236240 chkOrd ords args ap@(App f a)
237241 | (P _ fn _, args') <- unApply ap
238242 = if fn == n && length args == length args'
@@ -269,17 +273,29 @@ checkTotality path fc n
269273 case t of
270274 Unchecked -> case lookupDef Nothing n ctxt of
271275 [CaseOp _ _ pats _ _ _ _] ->
272− do t' <- calcTotality path fc n pats
273− i <- getIState
274− case lookupCtxt Nothing n (idris_flags i) of
275− [fs] -> if TotalFn `elem` fs then
276− case t' of
277− Total _ -> return ()
278− e -> totalityError t'
279− else return ()
280− _ -> return ()
276+ do i <- getIState
277+ let opts = case lookupCtxt Nothing n (idris_flags i) of
278+ [fs] -> fs
279+ [] -> []
280+ t' <- if AssertTotal `elem` opts
281+ then return $ Total []
282+ else calcTotality path fc n pats
283+ if TotalFn `elem` opts
284+ then case t' of
285+ Total _ -> return ()
286+ e -> totalityError t'
287+ else return ()
281288 setTotality n t'
282289 addIBC (IBCTotal n t')
290+ -- if it's not total, it can't reduce, to keep
291+ -- typechecking decidable
292+ case t' of
293+-- FIXME: Put this back when we can handle mutually recursive things
294+-- p@(Partial _) ->
295+-- do setAccessibility n Frozen
296+-- addIBC (IBCAccess n Frozen)
297+-- iputStrLn $ "HIDDEN: " ++ show n ++ show p
298+ _ -> return ()
283299 return t'
284300 _ -> return $ Total []
285301 x -> return x