{-|
Module      : Idris.Termination
Description : The termination checker for Idris

License     : BSD3
Maintainer  : The Idris Community.
-}
{-# LANGUAGE PatternGuards #-}
module Idris.Termination (buildSCG, checkAllCovering, checkDeclTotality,
                          checkIfGuarded, checkPositive, checkSizeChange,
                          verifyTotality) where

import Idris.AbsSyntax
import Idris.Core.CaseTree
import Idris.Core.Evaluate
import Idris.Core.TT
import Idris.Delaborate
import Idris.Error
import Idris.Options
import Idris.Output (iWarn)

import Control.Monad.State.Strict
import Data.Either
import Data.List
import Data.Maybe
import Debug.Trace

-- | Check whether function and all descendants cover all cases
-- (partial is okay, as long as it's due to recursion)
checkAllCovering :: FC -> [Name] -> Name -> Name -> Idris ()
checkAllCovering :: FC -> [Name] -> Name -> Name -> Idris ()
checkAllCovering FC
fc [Name]
done Name
top Name
n | Bool -> Bool
not (Name
n Name -> [Name] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Name]
done)
   = do IState
i <- StateT IState (ExceptT Err IO) IState
forall s (m :: * -> *). MonadState s m => m s
get
        case Name -> Context -> [Totality]
lookupTotal Name
n (IState -> Context
tt_ctxt IState
i) of
             [tot :: Totality
tot@(Partial PReason
NotCovering)] ->
                    do let msg :: [Char]
msg = Name -> [Char]
forall a. Show a => a -> [Char]
show Name
top [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" is " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Totality -> [Char]
forall a. Show a => a -> [Char]
show Totality
tot [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" due to " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Name -> [Char]
forall a. Show a => a -> [Char]
show Name
n
                       IState -> Idris ()
putIState IState
i { idris_totcheckfail :: [(FC, [Char])]
idris_totcheckfail = (FC
fc, [Char]
msg) (FC, [Char]) -> [(FC, [Char])] -> [(FC, [Char])]
forall a. a -> [a] -> [a]
: IState -> [(FC, [Char])]
idris_totcheckfail IState
i }
                       IBCWrite -> Idris ()
addIBC (FC -> [Char] -> IBCWrite
IBCTotCheckErr FC
fc [Char]
msg)
             [Partial (Other [Name]
ns)] ->
                     -- Check that none of the partial functions it relies
                     -- on are partial due to missing cases
                     (Name -> Idris ()) -> [Name] -> Idris ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (FC -> [Name] -> Name -> Name -> Idris ()
checkAllCovering FC
fc (Name
n Name -> [Name] -> [Name]
forall a. a -> [a] -> [a]
: [Name]
done) Name
top) [Name]
ns
             [Totality]
x -> () -> Idris ()
forall (m :: * -> *) a. Monad m => a -> m a
return () -- stop if total, or partial due to recursion
checkAllCovering FC
_ [Name]
_ Name
_ Name
_ = () -> Idris ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

-- | Check whether all 'Inf' arguments to the name end up guaranteed to be
-- guarded by constructors (conservatively... maybe this can do better later).
-- Essentially, all it does is check that every branch is a constructor application
-- with no other function applications.
--
-- If so, set the 'AllGuarded' flag which can be used by the productivity check
checkIfGuarded :: Name -> Idris ()
checkIfGuarded :: Name -> Idris ()
checkIfGuarded Name
n
   = do IState
i <- StateT IState (ExceptT Err IO) IState
forall s (m :: * -> *). MonadState s m => m s
get
        let ctxt :: Context
ctxt = IState -> Context
tt_ctxt IState
i
        case Name -> Context -> Maybe Def
lookupDefExact Name
n Context
ctxt of
             Just (CaseOp CaseInfo
_ Type
ty [(Type, Bool)]
_ [Either Type (Type, Type)]
_ [([Name], Type, Type)]
_ CaseDefs
cases) ->
                  let gnames :: [Name]
gnames = ([Name], SC) -> [Name]
forall a b. (a, b) -> a
fst (CaseDefs -> ([Name], SC)
cases_compiletime CaseDefs
cases) in
                      if [Name] -> IState -> SC -> Bool
forall (t :: * -> *). Foldable t => t Name -> IState -> SC -> Bool
allGuarded [Name]
gnames IState
i (([Name], SC) -> SC
forall a b. (a, b) -> b
snd (CaseDefs -> ([Name], SC)
cases_compiletime CaseDefs
cases))
                         then -- trace (show (n, gnames, ty, cases_compiletime cases)) $
                              Name -> FnOpt -> Idris ()
addFnOpt Name
n FnOpt
AllGuarded
                         else () -> Idris ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
             Maybe Def
_ -> () -> Idris ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  where
    guard :: Name -> IState -> Bool
guard Name
n IState
ist = Name -> Context -> Bool
isConName Name
n (IState -> Context
tt_ctxt IState
ist) Bool -> Bool -> Bool
|| Name -> IState -> Bool
guardFlag Name
n IState
ist
    guardFlag :: Name -> IState -> Bool
guardFlag Name
n IState
ist = case Name -> Ctxt [FnOpt] -> Maybe [FnOpt]
forall a. Name -> Ctxt a -> Maybe a
lookupCtxtExact Name
n (IState -> Ctxt [FnOpt]
idris_flags IState
ist) of
                           Maybe [FnOpt]
Nothing -> Bool
False
                           Just [FnOpt]
fs -> FnOpt
AllGuarded FnOpt -> [FnOpt] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [FnOpt]
fs

    -- Top level thing always needs to be a constructor application if
    -- the delayed things are going to be definitely guarded by this definition
    allGuarded :: t Name -> IState -> SC -> Bool
allGuarded t Name
names IState
i (STerm Type
t)
          | (P NameType
_ Name
fn Type
_, [Type]
args) <- Type -> (Type, [Type])
forall n. TT n -> (TT n, [TT n])
unApply Type
t,
            Name -> IState -> Bool
guard Name
fn IState
i
            = [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and ((Type -> Bool) -> [Type] -> [Bool]
forall a b. (a -> b) -> [a] -> [b]
map (t Name -> IState -> Type -> Bool
forall (t :: * -> *).
Foldable t =>
t Name -> IState -> Type -> Bool
guardedTerm t Name
names IState
i) [Type]
args)
          | Bool
otherwise = Bool
False
    allGuarded t Name
names IState
i (ProjCase Type
_ [CaseAlt' Type]
alts) = [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and ((CaseAlt' Type -> Bool) -> [CaseAlt' Type] -> [Bool]
forall a b. (a -> b) -> [a] -> [b]
map (t Name -> IState -> CaseAlt' Type -> Bool
guardedAlt t Name
names IState
i) [CaseAlt' Type]
alts)
    allGuarded t Name
names IState
i (Case CaseType
_ Name
_ [CaseAlt' Type]
alts) = [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and ((CaseAlt' Type -> Bool) -> [CaseAlt' Type] -> [Bool]
forall a b. (a -> b) -> [a] -> [b]
map (t Name -> IState -> CaseAlt' Type -> Bool
guardedAlt t Name
names IState
i) [CaseAlt' Type]
alts)
    allGuarded t Name
names IState
i SC
_ = Bool
True

    guardedTerm :: t Name -> IState -> Type -> Bool
guardedTerm t Name
names IState
i (P NameType
_ Name
v Type
_) = Name
v Name -> t Name -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` t Name
names Bool -> Bool -> Bool
|| Name -> IState -> Bool
guard Name
v IState
i
    guardedTerm t Name
names IState
i (Bind Name
n (Let RigCount
rig Type
t Type
v) Type
sc)
          = t Name -> IState -> Type -> Bool
guardedTerm t Name
names IState
i Type
v Bool -> Bool -> Bool
&& t Name -> IState -> Type -> Bool
guardedTerm t Name
names IState
i Type
sc
    guardedTerm t Name
names IState
i (Bind Name
n Binder Type
b Type
sc) = Bool
False
    guardedTerm t Name
names IState
i ap :: Type
ap@(App AppStatus Name
_ Type
_ Type
_)
          | (P NameType
_ Name
fn Type
_, [Type]
args) <- Type -> (Type, [Type])
forall n. TT n -> (TT n, [TT n])
unApply Type
ap,
            Name -> IState -> Bool
guard Name
fn IState
i Bool -> Bool -> Bool
|| Name
fn Name -> t Name -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` t Name
names
                = [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and ((Type -> Bool) -> [Type] -> [Bool]
forall a b. (a -> b) -> [a] -> [b]
map (t Name -> IState -> Type -> Bool
guardedTerm t Name
names IState
i) [Type]
args)
    guardedTerm t Name
names IState
i (App AppStatus Name
_ Type
f Type
a) = Bool
False
    guardedTerm t Name
names IState
i Type
tm = Bool
True

    guardedAlt :: t Name -> IState -> CaseAlt' Type -> Bool
guardedAlt t Name
names IState
i (ConCase Name
_ Int
_ [Name]
_ SC
t) = t Name -> IState -> SC -> Bool
allGuarded t Name
names IState
i SC
t
    guardedAlt t Name
names IState
i (FnCase Name
_ [Name]
_ SC
t) = t Name -> IState -> SC -> Bool
allGuarded t Name
names IState
i SC
t
    guardedAlt t Name
names IState
i (ConstCase Const
_ SC
t) = t Name -> IState -> SC -> Bool
allGuarded t Name
names IState
i SC
t
    guardedAlt t Name
names IState
i (SucCase Name
_ SC
t) = t Name -> IState -> SC -> Bool
allGuarded t Name
names IState
i SC
t
    guardedAlt t Name
names IState
i (DefaultCase SC
t) = t Name -> IState -> SC -> Bool
allGuarded t Name
names IState
i SC
t

-- | Check if, in a given group of type declarations mut_ns, the
-- constructor cn : ty is strictly positive, and update the context
-- accordingly
checkPositive :: [Name]       -- ^ the group of type declarations
              -> (Name, Type) -- ^ the constructor
              -> Idris Totality
checkPositive :: [Name] -> (Name, Type) -> Idris Totality
checkPositive [Name]
mut_ns (Name
cn, Type
ty')
    = do IState
i <- StateT IState (ExceptT Err IO) IState
getIState
         let ty :: Type
ty = Bool -> Type -> Type
delazy' Bool
True (Context -> Env -> Type -> Type
normalise (IState -> Context
tt_ctxt IState
i) [] Type
ty')
         let p :: Bool
p = IState -> Type -> Bool
cp IState
i Type
ty
         let tot :: Totality
tot = if Bool
p then [Int] -> Totality
Total (Type -> [Int]
forall n. TT n -> [Int]
args Type
ty) else PReason -> Totality
Partial PReason
NotPositive
         let ctxt' :: Context
ctxt' = Name -> Totality -> Context -> Context
setTotal Name
cn Totality
tot (IState -> Context
tt_ctxt IState
i)
         IState -> Idris ()
putIState (IState
i { tt_ctxt :: Context
tt_ctxt = Context
ctxt' })
         Int -> [Char] -> Idris ()
logCoverage Int
5 ([Char] -> Idris ()) -> [Char] -> Idris ()
forall a b. (a -> b) -> a -> b
$ [Char]
"Constructor " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Name -> [Char]
forall a. Show a => a -> [Char]
show Name
cn [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" is " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Totality -> [Char]
forall a. Show a => a -> [Char]
show Totality
tot [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" with " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Name] -> [Char]
forall a. Show a => a -> [Char]
show [Name]
mut_ns
         IBCWrite -> Idris ()
addIBC (Name -> Totality -> IBCWrite
IBCTotal Name
cn Totality
tot)
         Totality -> Idris Totality
forall (m :: * -> *) a. Monad m => a -> m a
return Totality
tot
  where
    args :: TT n -> [Int]
args TT n
t = [Int
0..[(n, TT n)] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (TT n -> [(n, TT n)]
forall n. TT n -> [(n, TT n)]
getArgTys TT n
t)Int -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1]

    cp :: IState -> Type -> Bool
cp IState
i (Bind Name
n (Pi RigCount
_ Maybe ImplicitInfo
_ Type
aty Type
_) Type
sc)
         = IState -> Type -> Bool
posArg IState
i Type
aty Bool -> Bool -> Bool
&& IState -> Type -> Bool
cp IState
i Type
sc
    cp IState
i Type
t | (P NameType
_ Name
n' Type
_ , [Type]
args) <- Type -> (Type, [Type])
forall n. TT n -> (TT n, [TT n])
unApply Type
t,
             Name
n' Name -> [Name] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Name]
mut_ns = (Type -> Bool) -> [Type] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Type -> Bool
noRec [Type]
args
    cp IState
i Type
_ = Bool
True

    posArg :: IState -> Type -> Bool
posArg IState
ist (Bind Name
_ (Pi RigCount
_ Maybe ImplicitInfo
_ Type
nty Type
_) Type
sc) = Type -> Bool
noRec Type
nty Bool -> Bool -> Bool
&& IState -> Type -> Bool
posArg IState
ist Type
sc
    posArg IState
ist Type
t = IState -> Type -> Bool
posParams IState
ist Type
t

    noRec :: Type -> Bool
noRec Type
arg = (Name -> Bool) -> [Name] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (\Name
x -> Name
x Name -> [Name] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Name]
mut_ns) (Type -> [Name]
forall n. Eq n => TT n -> [n]
allTTNames Type
arg)

    -- If the type appears recursively in a parameter argument, that's
    -- fine, otherwise if it appears in an argument it's not fine.
    posParams :: IState -> Type -> Bool
posParams IState
ist Type
t | (P NameType
_ Name
n Type
_, [Type]
args) <- Type -> (Type, [Type])
forall n. TT n -> (TT n, [TT n])
unApply Type
t
       = case Name -> Ctxt TypeInfo -> Maybe TypeInfo
forall a. Name -> Ctxt a -> Maybe a
lookupCtxtExact Name
n (IState -> Ctxt TypeInfo
idris_datatypes IState
ist) of
              Just TypeInfo
ti -> [Int] -> Int -> [Type] -> Bool
forall (t :: * -> *) a.
(Foldable t, Eq a, Num a) =>
t a -> a -> [Type] -> Bool
checkParamsOK (TypeInfo -> [Int]
param_pos TypeInfo
ti) Int
0 [Type]
args
              Maybe TypeInfo
Nothing -> [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and ((Type -> Bool) -> [Type] -> [Bool]
forall a b. (a -> b) -> [a] -> [b]
map (IState -> Type -> Bool
posParams IState
ist) [Type]
args)
    posParams IState
ist Type
t = Type -> Bool
noRec Type
t

    checkParamsOK :: t a -> a -> [Type] -> Bool
checkParamsOK t a
ppos a
i [] = Bool
True
    checkParamsOK t a
ppos a
i (Type
p : [Type]
ps)
          | a
i a -> t a -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` t a
ppos = t a -> a -> [Type] -> Bool
checkParamsOK t a
ppos (a
i a -> a -> a
forall a. Num a => a -> a -> a
+ a
1) [Type]
ps
          | Bool
otherwise = Type -> Bool
noRec Type
p Bool -> Bool -> Bool
&& t a -> a -> [Type] -> Bool
checkParamsOK t a
ppos (a
i a -> a -> a
forall a. Num a => a -> a -> a
+ a
1) [Type]
ps

-- | Calculate the totality of a function from its patterns.  Either
-- follow the size change graph (if inductive) or check for
-- productivity (if coinductive)
calcTotality :: FC -> Name -> [([Name], Term, Term)] -> Idris Totality
calcTotality :: FC -> Name -> [([Name], Type, Type)] -> Idris Totality
calcTotality FC
fc Name
n [([Name], Type, Type)]
pats
    = do IState
i <- StateT IState (ExceptT Err IO) IState
getIState
         case (Type -> Maybe Totality) -> [Type] -> [Totality]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (IState -> Type -> Maybe Totality
checkLHS IState
i) ((([Name], Type, Type) -> Type) -> [([Name], Type, Type)] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map (\ ([Name]
_, Type
l, Type
r) -> Type
l) [([Name], Type, Type)]
pats) of
            (Totality
failure : [Totality]
_) -> Totality -> Idris Totality
forall (m :: * -> *) a. Monad m => a -> m a
return Totality
failure
            [Totality]
_ -> Name -> Idris Totality
checkSizeChange Name
n
  where
    checkLHS :: IState -> Type -> Maybe Totality
checkLHS IState
i (P NameType
_ Name
fn Type
_)
        = case Name -> Context -> Maybe Totality
lookupTotalExact Name
fn (IState -> Context
tt_ctxt IState
i) of
               Just (Partial PReason
_) -> Totality -> Maybe Totality
forall (m :: * -> *) a. Monad m => a -> m a
return (PReason -> Totality
Partial ([Name] -> PReason
Other [Name
fn]))
               Maybe Totality
_ -> Maybe Totality
forall a. Maybe a
Nothing
    checkLHS IState
i (App AppStatus Name
_ Type
f Type
a) = Maybe Totality -> Maybe Totality -> Maybe Totality
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
mplus (IState -> Type -> Maybe Totality
checkLHS IState
i Type
f) (IState -> Type -> Maybe Totality
checkLHS IState
i Type
a)
    checkLHS IState
_ Type
_ = Maybe Totality
forall a. Maybe a
Nothing

checkTotality :: [Name] -> FC -> Name -> Idris Totality
checkTotality :: [Name] -> FC -> Name -> Idris Totality
checkTotality [Name]
path FC
fc Name
n
    | Name
n Name -> [Name] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Name]
path = Totality -> Idris Totality
forall (m :: * -> *) a. Monad m => a -> m a
return (PReason -> Totality
Partial ([Name] -> PReason
Mutual (Name
n Name -> [Name] -> [Name]
forall a. a -> [a] -> [a]
: [Name]
path)))
    | Bool
otherwise = do
        Totality
t <- Name -> Idris Totality
getTotality Name
n
        IState
i <- StateT IState (ExceptT Err IO) IState
getIState
        Context
ctxt' <- do Context
ctxt <- StateT IState (ExceptT Err IO) Context
getContext
                    TC Context -> StateT IState (ExceptT Err IO) Context
forall a. TC a -> Idris a
tclift (TC Context -> StateT IState (ExceptT Err IO) Context)
-> TC Context -> StateT IState (ExceptT Err IO) Context
forall a b. (a -> b) -> a -> b
$ Name -> [Name] -> [[Name]] -> ErasureInfo -> Context -> TC Context
simplifyCasedef Name
n [] [] (IState -> ErasureInfo
getErasureInfo IState
i) Context
ctxt
        Context -> Idris ()
setContext Context
ctxt'
        Context
ctxt <- StateT IState (ExceptT Err IO) Context
getContext
        IState
i <- StateT IState (ExceptT Err IO) IState
getIState
        let opts :: [FnOpt]
opts = case Name -> Ctxt [FnOpt] -> [[FnOpt]]
forall a. Name -> Ctxt a -> [a]
lookupCtxt Name
n (IState -> Ctxt [FnOpt]
idris_flags IState
i) of
                            [[FnOpt]
fs] -> [FnOpt]
fs
                            [[FnOpt]]
_ -> []
        Totality
t' <- case Totality
t of
                Totality
Unchecked ->
                    case Name -> Context -> [Def]
lookupDef Name
n Context
ctxt of
                        [CaseOp CaseInfo
_ Type
_ [(Type, Bool)]
_ [Either Type (Type, Type)]
_ [([Name], Type, Type)]
pats CaseDefs
_] ->
                            do Totality
t' <- if FnOpt
AssertTotal FnOpt -> [FnOpt] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [FnOpt]
opts
                                        then Totality -> Idris Totality
forall (m :: * -> *) a. Monad m => a -> m a
return (Totality -> Idris Totality) -> Totality -> Idris Totality
forall a b. (a -> b) -> a -> b
$ [Int] -> Totality
Total []
                                        else FC -> Name -> [([Name], Type, Type)] -> Idris Totality
calcTotality FC
fc Name
n [([Name], Type, Type)]
pats
                               Int -> [Char] -> Idris ()
logCoverage Int
2 ([Char] -> Idris ()) -> [Char] -> Idris ()
forall a b. (a -> b) -> a -> b
$ [Char]
"Set to " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Totality -> [Char]
forall a. Show a => a -> [Char]
show Totality
t'
                               Name -> Totality -> Idris ()
setTotality Name
n Totality
t'
                               IBCWrite -> Idris ()
addIBC (Name -> Totality -> IBCWrite
IBCTotal Name
n Totality
t')
                               Totality -> Idris Totality
forall (m :: * -> *) a. Monad m => a -> m a
return Totality
t'
                        [TyDecl (DCon Int
_ Int
_ Bool
_) Type
ty] ->
                            case Type -> (Type, [Type])
forall n. TT n -> (TT n, [TT n])
unApply (Type -> Type
forall n. TT n -> TT n
getRetTy Type
ty) of
                              (P NameType
_ Name
tyn Type
_, [Type]
_) -> do
                                 let ms :: [Name]
ms = case Name -> Ctxt TypeInfo -> [TypeInfo]
forall a. Name -> Ctxt a -> [a]
lookupCtxt Name
tyn (IState -> Ctxt TypeInfo
idris_datatypes IState
i) of
                                       [TI [Name]
_ Bool
_ DataOpts
_ [Int]
_ xs :: [Name]
xs@(Name
_:[Name]
_) Bool
_] -> [Name]
xs
                                       [TypeInfo]
ts -> [Name
tyn]
                                 [Name] -> (Name, Type) -> Idris Totality
checkPositive [Name]
ms (Name
n, Type
ty)
                              (Type, [Type])
_-> Totality -> Idris Totality
forall (m :: * -> *) a. Monad m => a -> m a
return (Totality -> Idris Totality) -> Totality -> Idris Totality
forall a b. (a -> b) -> a -> b
$ [Int] -> Totality
Total []
                        [Def]
_ -> Totality -> Idris Totality
forall (m :: * -> *) a. Monad m => a -> m a
return (Totality -> Idris Totality) -> Totality -> Idris Totality
forall a b. (a -> b) -> a -> b
$ [Int] -> Totality
Total []
                Totality
x -> Totality -> Idris Totality
forall (m :: * -> *) a. Monad m => a -> m a
return Totality
x
        case Totality
t' of
            Total [Int]
_ -> Totality -> Idris Totality
forall (m :: * -> *) a. Monad m => a -> m a
return Totality
t'
            Totality
Productive -> Totality -> Idris Totality
forall (m :: * -> *) a. Monad m => a -> m a
return Totality
t'
            Totality
e -> do Bool
w <- Opt -> Idris Bool
cmdOptType Opt
WarnPartial
                    if FnOpt
TotalFn FnOpt -> [FnOpt] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [FnOpt]
opts
                       then do Totality -> Idris ()
forall a. Show a => a -> Idris ()
totalityError Totality
t'; Totality -> Idris Totality
forall (m :: * -> *) a. Monad m => a -> m a
return Totality
t'
                       else do Bool -> Idris () -> Idris ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool
w Bool -> Bool -> Bool
&& Bool -> Bool
not (FnOpt
PartialFn FnOpt -> [FnOpt] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [FnOpt]
opts)) (Idris () -> Idris ()) -> Idris () -> Idris ()
forall a b. (a -> b) -> a -> b
$
                                   Name -> Totality -> Idris ()
forall a. Show a => Name -> a -> Idris ()
warnPartial Name
n Totality
t'
                               Totality -> Idris Totality
forall (m :: * -> *) a. Monad m => a -> m a
return Totality
t'
  where
    totalityError :: a -> Idris ()
totalityError a
t = do IState
i <- StateT IState (ExceptT Err IO) IState
getIState
                         let msg :: [Char]
msg = Name -> [Char]
forall a. Show a => a -> [Char]
show Name
n [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" is " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ a -> [Char]
forall a. Show a => a -> [Char]
show a
t
                         IState -> Idris ()
putIState IState
i { idris_totcheckfail :: [(FC, [Char])]
idris_totcheckfail = (FC
fc, [Char]
msg) (FC, [Char]) -> [(FC, [Char])] -> [(FC, [Char])]
forall a. a -> [a] -> [a]
: IState -> [(FC, [Char])]
idris_totcheckfail IState
i}
                         IBCWrite -> Idris ()
addIBC (FC -> [Char] -> IBCWrite
IBCTotCheckErr FC
fc [Char]
msg)

    warnPartial :: Name -> a -> Idris ()
warnPartial Name
n a
t
       = do IState
i <- StateT IState (ExceptT Err IO) IState
getIState
            case Name -> Context -> [Def]
lookupDef Name
n (IState -> Context
tt_ctxt IState
i) of
               [Def
x] -> do
                  FC -> OutputDoc -> Idris ()
iWarn FC
fc (OutputDoc -> Idris ())
-> ([Char] -> OutputDoc) -> [Char] -> Idris ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IState -> Err -> OutputDoc
pprintErr IState
i (Err -> OutputDoc) -> ([Char] -> Err) -> [Char] -> OutputDoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Err
forall t. [Char] -> Err' t
Msg ([Char] -> Idris ()) -> [Char] -> Idris ()
forall a b. (a -> b) -> a -> b
$ [Char]
"Warning - " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Name -> [Char]
forall a. Show a => a -> [Char]
show Name
n [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" is " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ a -> [Char]
forall a. Show a => a -> [Char]
show a
t
--                                ++ "\n" ++ show x
--                   let cg = lookupCtxtName Nothing n (idris_callgraph i)
--                   iputStrLn (show cg)


checkDeclTotality :: (FC, Name) -> Idris Totality
checkDeclTotality :: (FC, Name) -> Idris Totality
checkDeclTotality (FC
fc, Name
n)
    = do Int -> [Char] -> Idris ()
logCoverage Int
2 ([Char] -> Idris ()) -> [Char] -> Idris ()
forall a b. (a -> b) -> a -> b
$ [Char]
"Checking " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Name -> [Char]
forall a. Show a => a -> [Char]
show Name
n [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" for totality"
         IState
i <- StateT IState (ExceptT Err IO) IState
getIState
         let opts :: [FnOpt]
opts = case Name -> Ctxt [FnOpt] -> Maybe [FnOpt]
forall a. Name -> Ctxt a -> Maybe a
lookupCtxtExact Name
n (IState -> Ctxt [FnOpt]
idris_flags IState
i) of
                              Just [FnOpt]
fs -> [FnOpt]
fs
                              Maybe [FnOpt]
_ -> []
         Bool -> Idris () -> Idris ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (FnOpt
CoveringFn FnOpt -> [FnOpt] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [FnOpt]
opts) (Idris () -> Idris ()) -> Idris () -> Idris ()
forall a b. (a -> b) -> a -> b
$ FC -> [Name] -> Name -> Name -> Idris ()
checkAllCovering FC
fc [] Name
n Name
n
         Totality
t <- [Name] -> FC -> Name -> Idris Totality
checkTotality [] FC
fc Name
n
         Totality -> Idris Totality
forall (m :: * -> *) a. Monad m => a -> m a
return Totality
t

-- If the name calls something which is partial, set it as partial
verifyTotality :: (FC, Name) -> Idris ()
verifyTotality :: (FC, Name) -> Idris ()
verifyTotality (FC
fc, Name
n)
    = do Int -> [Char] -> Idris ()
logCoverage Int
2 ([Char] -> Idris ()) -> [Char] -> Idris ()
forall a b. (a -> b) -> a -> b
$ [Char]
"Checking " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Name -> [Char]
forall a. Show a => a -> [Char]
show Name
n [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"'s descendents are total"
         IState
ist <- StateT IState (ExceptT Err IO) IState
getIState
         case Name -> Context -> Maybe Totality
lookupTotalExact Name
n (IState -> Context
tt_ctxt IState
ist) of
              Just (Total [Int]
_) -> do
                 let ns :: [Name]
ns = Context -> [Name]
getNames (IState -> Context
tt_ctxt IState
ist)

                 case IState -> [Name] -> [Name] -> Maybe [Name]
getPartial IState
ist [] [Name]
ns of
                      Maybe [Name]
Nothing -> () -> Idris ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
                      Just [Name]
bad -> do let t' :: Totality
t' = PReason -> Totality
Partial ([Name] -> PReason
Other [Name]
bad)
                                     Int -> [Char] -> Idris ()
logCoverage Int
2 ([Char] -> Idris ()) -> [Char] -> Idris ()
forall a b. (a -> b) -> a -> b
$ [Char]
"Set in verify to " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Totality -> [Char]
forall a. Show a => a -> [Char]
show Totality
t'
                                     Name -> Totality -> Idris ()
setTotality Name
n Totality
t'
                                     IBCWrite -> Idris ()
addIBC (Name -> Totality -> IBCWrite
IBCTotal Name
n Totality
t')
              Maybe Totality
_ -> () -> Idris ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  where
    getNames :: Context -> [Name]
getNames Context
ctxt = case Name -> Context -> Maybe Def
lookupDefExact Name
n Context
ctxt of
                         Just (CaseOp  CaseInfo
_ Type
_ [(Type, Bool)]
_ [Either Type (Type, Type)]
_ [([Name], Type, Type)]
_ CaseDefs
defs)
                           -> let ([Name]
top, SC
def) = CaseDefs -> ([Name], SC)
cases_compiletime CaseDefs
defs in
                                  ((Name, [[Name]]) -> Name) -> [(Name, [[Name]])] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name, [[Name]]) -> Name
forall a b. (a, b) -> a
fst (Bool -> SC -> [Name] -> [(Name, [[Name]])]
findCalls' Bool
True SC
def [Name]
top)
                         Maybe Def
_ -> []

    getPartial :: IState -> [Name] -> [Name] -> Maybe [Name]
getPartial IState
ist [] [] = Maybe [Name]
forall a. Maybe a
Nothing
    getPartial IState
ist [Name]
bad [] = [Name] -> Maybe [Name]
forall a. a -> Maybe a
Just [Name]
bad
    getPartial IState
ist [Name]
bad (Name
n : [Name]
ns)
        = case Name -> Context -> Maybe Totality
lookupTotalExact Name
n (IState -> Context
tt_ctxt IState
ist) of
               Just (Partial PReason
_) -> IState -> [Name] -> [Name] -> Maybe [Name]
getPartial IState
ist (Name
n Name -> [Name] -> [Name]
forall a. a -> [a] -> [a]
: [Name]
bad) [Name]
ns
               Maybe Totality
_ -> IState -> [Name] -> [Name] -> Maybe [Name]
getPartial IState
ist [Name]
bad [Name]
ns

-- | Calculate the size change graph for this definition
--
-- SCG for a function f consists of a list of:
--    (g, [(a1, sizechange1), (a2, sizechange2), ..., (an, sizechangen)])
--
-- where g is a function called
-- a1 ... an are the arguments of f in positions 1..n of g
-- sizechange1 ... sizechange2 is how their size has changed wrt the input
-- to f
--    Nothing, if the argument is unrelated to the input
buildSCG :: (FC, Name) -> Idris ()
buildSCG :: (FC, Name) -> Idris ()
buildSCG (FC
_, Name
n) = do
   IState
ist <- StateT IState (ExceptT Err IO) IState
getIState
   case Name -> Ctxt CGInfo -> Maybe CGInfo
forall a. Name -> Ctxt a -> Maybe a
lookupCtxtExact Name
n (IState -> Ctxt CGInfo
idris_callgraph IState
ist) of
       Just CGInfo
cg -> case Name -> Context -> Maybe Def
lookupDefExact Name
n (IState -> Context
tt_ctxt IState
ist) of
           Just (CaseOp CaseInfo
_ Type
_ [(Type, Bool)]
_ [Either Type (Type, Type)]
pats [([Name], Type, Type)]
_ CaseDefs
cd) ->
             let ([Name]
args, SC
sc) = CaseDefs -> ([Name], SC)
cases_compiletime CaseDefs
cd in
               do Int -> [Char] -> Idris ()
logCoverage Int
2 ([Char] -> Idris ()) -> [Char] -> Idris ()
forall a b. (a -> b) -> a -> b
$ [Char]
"Building SCG for " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Name -> [Char]
forall a. Show a => a -> [Char]
show Name
n [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" from\n"
                                [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Either Type (Type, Type)] -> [Char]
forall a. Show a => a -> [Char]
show [Either Type (Type, Type)]
pats [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"\n" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ SC -> [Char]
forall a. Show a => a -> [Char]
show SC
sc
                  let newscg :: [SCGEntry]
newscg = IState -> Name -> [(Type, Type)] -> [Name] -> [SCGEntry]
buildSCG' IState
ist Name
n ([Either Type (Type, Type)] -> [(Type, Type)]
forall a b. [Either a b] -> [b]
rights [Either Type (Type, Type)]
pats) [Name]
args
                  Int -> [Char] -> Idris ()
logCoverage Int
5 ([Char] -> Idris ()) -> [Char] -> Idris ()
forall a b. (a -> b) -> a -> b
$ [Char]
"SCG is: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [SCGEntry] -> [Char]
forall a. Show a => a -> [Char]
show [SCGEntry]
newscg
                  Name -> CGInfo -> Idris ()
addToCG Name
n ( CGInfo
cg { scg :: [SCGEntry]
scg = [SCGEntry]
newscg } )
           Maybe Def
_ -> () -> Idris ()
forall (m :: * -> *) a. Monad m => a -> m a
return () -- CG comes from a type declaration only
       Maybe CGInfo
_ -> Int -> [Char] -> Idris ()
logCoverage Int
5 ([Char] -> Idris ()) -> [Char] -> Idris ()
forall a b. (a -> b) -> a -> b
$ [Char]
"Could not build SCG for " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Name -> [Char]
forall a. Show a => a -> [Char]
show Name
n [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"\n"

delazy :: Type -> Type
delazy = Bool -> Type -> Type
delazy' Bool
False -- not lazy codata
delazy' :: Bool -> Type -> Type
delazy' Bool
all t :: Type
t@(App AppStatus Name
_ Type
f Type
a)
     | (P NameType
_ (UN Text
l) Type
_, [P NameType
_ (UN Text
lty) Type
_, Type
_, Type
arg]) <- Type -> (Type, [Type])
forall n. TT n -> (TT n, [TT n])
unApply Type
t,
       Text
l Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== [Char] -> Text
txt [Char]
"Force" Bool -> Bool -> Bool
&& (Bool
all Bool -> Bool -> Bool
|| Text
lty Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= [Char] -> Text
txt [Char]
"Infinite") = Bool -> Type -> Type
delazy' Bool
all Type
arg
     | (P NameType
_ (UN Text
l) Type
_, [P NameType
_ (UN Text
lty) Type
_, Type
_, Type
arg]) <- Type -> (Type, [Type])
forall n. TT n -> (TT n, [TT n])
unApply Type
t,
       Text
l Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== [Char] -> Text
txt [Char]
"Delay" Bool -> Bool -> Bool
&& (Bool
all Bool -> Bool -> Bool
|| Text
lty Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= [Char] -> Text
txt [Char]
"Infinite") = Type -> Type
delazy Type
arg
     | (P NameType
_ (UN Text
l) Type
_, [P NameType
_ (UN Text
lty) Type
_, Type
arg]) <- Type -> (Type, [Type])
forall n. TT n -> (TT n, [TT n])
unApply Type
t,
       Text
l Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== [Char] -> Text
txt [Char]
"Delayed" Bool -> Bool -> Bool
&& (Bool
all Bool -> Bool -> Bool
|| Text
lty Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= [Char] -> Text
txt [Char]
"Infinite") = Bool -> Type -> Type
delazy' Bool
all Type
arg
delazy' Bool
all (App AppStatus Name
s Type
f Type
a) = AppStatus Name -> Type -> Type -> Type
forall n. AppStatus n -> TT n -> TT n -> TT n
App AppStatus Name
s (Bool -> Type -> Type
delazy' Bool
all Type
f) (Bool -> Type -> Type
delazy' Bool
all Type
a)
delazy' Bool
all (Bind Name
n Binder Type
b Type
sc) = Name -> Binder Type -> Type -> Type
forall n. n -> Binder (TT n) -> TT n -> TT n
Bind Name
n ((Type -> Type) -> Binder Type -> Binder Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Bool -> Type -> Type
delazy' Bool
all) Binder Type
b) (Bool -> Type -> Type
delazy' Bool
all Type
sc)
delazy' Bool
all Type
t = Type
t

data Guardedness = Toplevel | Unguarded | Guarded | Delayed
  deriving Int -> Guardedness -> [Char] -> [Char]
[Guardedness] -> [Char] -> [Char]
Guardedness -> [Char]
(Int -> Guardedness -> [Char] -> [Char])
-> (Guardedness -> [Char])
-> ([Guardedness] -> [Char] -> [Char])
-> Show Guardedness
forall a.
(Int -> a -> [Char] -> [Char])
-> (a -> [Char]) -> ([a] -> [Char] -> [Char]) -> Show a
showList :: [Guardedness] -> [Char] -> [Char]
$cshowList :: [Guardedness] -> [Char] -> [Char]
show :: Guardedness -> [Char]
$cshow :: Guardedness -> [Char]
showsPrec :: Int -> Guardedness -> [Char] -> [Char]
$cshowsPrec :: Int -> Guardedness -> [Char] -> [Char]
Show

buildSCG' :: IState -> Name -> [(Term, Term)] -> [Name] -> [SCGEntry]
buildSCG' :: IState -> Name -> [(Type, Type)] -> [Name] -> [SCGEntry]
buildSCG' IState
ist Name
topfn [(Type, Type)]
pats [Name]
args = [SCGEntry] -> [SCGEntry]
forall a. Eq a => [a] -> [a]
nub ([SCGEntry] -> [SCGEntry]) -> [SCGEntry] -> [SCGEntry]
forall a b. (a -> b) -> a -> b
$ ((Type, Type) -> [SCGEntry]) -> [(Type, Type)] -> [SCGEntry]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Type, Type) -> [SCGEntry]
scgPat [(Type, Type)]
pats where
  scgPat :: (Type, Type) -> [SCGEntry]
scgPat (Type
lhs, Type
rhs) = let lhs' :: Type
lhs' = Type -> Type
delazy Type
lhs
                          rhs' :: Type
rhs' = Type -> Type
delazy Type
rhs
                          (Type
_, [Type]
pargs) = Type -> (Type, [Type])
forall n. TT n -> (TT n, [TT n])
unApply (Type -> Type
forall n. TT n -> TT n
dePat Type
lhs') in
                            [Name]
-> Guardedness -> Type -> [Name] -> [(Type, Int)] -> [SCGEntry]
findCalls [] Guardedness
Toplevel (Type -> Type
forall n. TT n -> TT n
dePat Type
rhs') (Type -> [Name]
forall a. TT a -> [a]
patvars Type
lhs')
                                      ([Type] -> [Int] -> [(Type, Int)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Type]
pargs [Int
0..])

  -- Under a delay, calls are excluded from the graph - if it's a call to a
  -- non-total function we'll find that in the final totality check
  findCalls :: [Name]
-> Guardedness -> Type -> [Name] -> [(Type, Int)] -> [SCGEntry]
findCalls [Name]
cases Guardedness
Delayed ap :: Type
ap@(P NameType
_ Name
n Type
_) [Name]
pvs [(Type, Int)]
args = []
  findCalls [Name]
cases Guardedness
guarded ap :: Type
ap@(App AppStatus Name
_ Type
f Type
a) [Name]
pvs [(Type, Int)]
pargs
     -- under a call to "assert_total", don't do any checking, just believe
     -- that it is total.
     | (P NameType
_ (UN Text
at) Type
_, [Type
_, Type
_]) <- Type -> (Type, [Type])
forall n. TT n -> (TT n, [TT n])
unApply Type
ap,
       Text
at Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== [Char] -> Text
txt [Char]
"assert_total" = []
     -- don't go under calls to functions which are asserted total
     | (P NameType
_ Name
n Type
_, [Type]
_) <- Type -> (Type, [Type])
forall n. TT n -> (TT n, [TT n])
unApply Type
ap,
       Just [FnOpt]
opts <- Name -> Ctxt [FnOpt] -> Maybe [FnOpt]
forall a. Name -> Ctxt a -> Maybe a
lookupCtxtExact Name
n (IState -> Ctxt [FnOpt]
idris_flags IState
ist),
       FnOpt
AssertTotal FnOpt -> [FnOpt] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [FnOpt]
opts = []
     -- under a guarded call to "Delay Infinite", we are 'Delayed', so don't
     -- check under guarded constructors.
     | (P NameType
_ (UN Text
del) Type
_, [Type
_,Type
_,Type
arg]) <- Type -> (Type, [Type])
forall n. TT n -> (TT n, [TT n])
unApply Type
ap,
       Guardedness
Guarded <- Guardedness
guarded,
       Text
del Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== [Char] -> Text
txt [Char]
"Delay"
           = [Name]
-> Guardedness -> Type -> [Name] -> [(Type, Int)] -> [SCGEntry]
findCalls [Name]
cases Guardedness
Delayed Type
arg [Name]
pvs [(Type, Int)]
pargs
     | (P NameType
_ Name
n Type
_, [Type]
args) <- Type -> (Type, [Type])
forall n. TT n -> (TT n, [TT n])
unApply Type
ap,
       Guardedness
Delayed <- Guardedness
guarded,
       Name -> Context -> Bool
isConName Name
n (IState -> Context
tt_ctxt IState
ist) Bool -> Bool -> Bool
|| Name -> IState -> Bool
allGuarded Name
n IState
ist
           = -- Still under a 'Delay' and constructor guarded, so check
             -- just the arguments to the constructor, remaining Delayed
             (Type -> [SCGEntry]) -> [Type] -> [SCGEntry]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\Type
x -> [Name]
-> Guardedness -> Type -> [Name] -> [(Type, Int)] -> [SCGEntry]
findCalls [Name]
cases Guardedness
guarded Type
x [Name]
pvs [(Type, Int)]
pargs) [Type]
args
     | (P NameType
_ Name
ifthenelse Type
_, [Type
_, Type
_, Type
t, Type
e]) <- Type -> (Type, [Type])
forall n. TT n -> (TT n, [TT n])
unApply Type
ap,
       Name
ifthenelse Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== Name -> [[Char]] -> Name
sNS ([Char] -> Name
sUN [Char]
"ifThenElse") [[Char]
"Bool", [Char]
"Prelude"]
       -- Continue look inside if...then...else blocks
       -- TODO: Consider whether we should do this for user defined ifThenElse
       -- rather than just the one in the Prelude as a special case
       = [Name]
-> Guardedness -> Type -> [Name] -> [(Type, Int)] -> [SCGEntry]
findCalls [Name]
cases Guardedness
guarded Type
t [Name]
pvs [(Type, Int)]
pargs [SCGEntry] -> [SCGEntry] -> [SCGEntry]
forall a. [a] -> [a] -> [a]
++
         [Name]
-> Guardedness -> Type -> [Name] -> [(Type, Int)] -> [SCGEntry]
findCalls [Name]
cases Guardedness
guarded Type
e [Name]
pvs [(Type, Int)]
pargs
     | (P NameType
_ Name
n Type
_, [Type]
args) <- Type -> (Type, [Type])
forall n. TT n -> (TT n, [TT n])
unApply Type
ap,
       Name -> Bool
caseName Name
n Bool -> Bool -> Bool
&& Name
n Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
/= Name
topfn,
       Maybe Totality -> Bool
notPartial (Name -> Context -> Maybe Totality
lookupTotalExact Name
n (IState -> Context
tt_ctxt IState
ist))
       -- case block - look inside the block, as long as it was covering
       -- (i.e. not currently set at Partial) to get a more accurate size
       -- change result from the top level patterns (also to help pass
       -- information through from guarded corecursion accurately)
       = (Type -> [SCGEntry]) -> [Type] -> [SCGEntry]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\Type
x -> [Name]
-> Guardedness -> Type -> [Name] -> [(Type, Int)] -> [SCGEntry]
findCalls [Name]
cases Guardedness
Unguarded Type
x [Name]
pvs [(Type, Int)]
pargs) [Type]
args [SCGEntry] -> [SCGEntry] -> [SCGEntry]
forall a. [a] -> [a] -> [a]
++
             if Name
n Name -> [Name] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Name]
cases
                then [Name]
-> Guardedness
-> Name
-> [Type]
-> [Name]
-> [(Type, Int)]
-> [SCGEntry]
findCallsCase (Name
n Name -> [Name] -> [Name]
forall a. a -> [a] -> [a]
: [Name]
cases) Guardedness
guarded Name
n [Type]
args [Name]
pvs [(Type, Int)]
pargs
                else []
     | (P NameType
_ Name
n Type
_, [Type]
args) <- Type -> (Type, [Type])
forall n. TT n -> (TT n, [TT n])
unApply Type
ap,
       Guardedness
Delayed <- Guardedness
guarded
       -- Under a delayed recursive call just check the arguments
           = (Type -> [SCGEntry]) -> [Type] -> [SCGEntry]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\Type
x -> [Name]
-> Guardedness -> Type -> [Name] -> [(Type, Int)] -> [SCGEntry]
findCalls [Name]
cases Guardedness
Unguarded Type
x [Name]
pvs [(Type, Int)]
pargs) [Type]
args
     | (P NameType
_ Name
n Type
_, [Type]
args) <- Type -> (Type, [Type])
forall n. TT n -> (TT n, [TT n])
unApply Type
ap,
       Bool -> Bool
not (Name
n Name -> [Name] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Name]
pvs)
        -- Ordinary call, not under a delay.
        -- If n is a constructor, set 'args' as Guarded
        = let nguarded :: Guardedness
nguarded = case Guardedness
guarded of
                              Guardedness
Unguarded -> Guardedness
Unguarded
                              Guardedness
x -> if Name -> Context -> Bool
isConName Name
n (IState -> Context
tt_ctxt IState
ist)
                                       Bool -> Bool -> Bool
|| Name -> IState -> Bool
allGuarded Name
n IState
ist
                                      then Guardedness
Guarded
                                      else Guardedness
Unguarded in
              Name -> [Type] -> [(Type, Int)] -> [SCGEntry]
forall a.
Name -> [Type] -> [(Type, a)] -> [(Name, [Maybe (a, SizeChange)])]
mkChange Name
n [Type]
args [(Type, Int)]
pargs [SCGEntry] -> [SCGEntry] -> [SCGEntry]
forall a. [a] -> [a] -> [a]
++
                 (Type -> [SCGEntry]) -> [Type] -> [SCGEntry]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\Type
x -> [Name]
-> Guardedness -> Type -> [Name] -> [(Type, Int)] -> [SCGEntry]
findCalls [Name]
cases Guardedness
nguarded Type
x [Name]
pvs [(Type, Int)]
pargs) [Type]
args
    where notPartial :: Maybe Totality -> Bool
notPartial (Just (Partial PReason
NotCovering)) = Bool
False
          notPartial Maybe Totality
_ = Bool
True
  findCalls [Name]
cases Guardedness
guarded (App AppStatus Name
_ Type
f Type
a) [Name]
pvs [(Type, Int)]
pargs
        = [Name]
-> Guardedness -> Type -> [Name] -> [(Type, Int)] -> [SCGEntry]
findCalls [Name]
cases Guardedness
Unguarded Type
f [Name]
pvs [(Type, Int)]
pargs [SCGEntry] -> [SCGEntry] -> [SCGEntry]
forall a. [a] -> [a] -> [a]
++ [Name]
-> Guardedness -> Type -> [Name] -> [(Type, Int)] -> [SCGEntry]
findCalls [Name]
cases Guardedness
Unguarded Type
a [Name]
pvs [(Type, Int)]
pargs
  findCalls [Name]
cases Guardedness
guarded (Bind Name
n (Let RigCount
rig Type
t Type
v) Type
e) [Name]
pvs [(Type, Int)]
pargs
        = [Name]
-> Guardedness -> Type -> [Name] -> [(Type, Int)] -> [SCGEntry]
findCalls [Name]
cases Guardedness
Unguarded Type
t [Name]
pvs [(Type, Int)]
pargs [SCGEntry] -> [SCGEntry] -> [SCGEntry]
forall a. [a] -> [a] -> [a]
++
          [Name]
-> Guardedness -> Type -> [Name] -> [(Type, Int)] -> [SCGEntry]
findCalls [Name]
cases Guardedness
Unguarded Type
v [Name]
pvs [(Type, Int)]
pargs [SCGEntry] -> [SCGEntry] -> [SCGEntry]
forall a. [a] -> [a] -> [a]
++
          -- Substitute in the scope since this might reveal some useful
          -- structure
          [Name]
-> Guardedness -> Type -> [Name] -> [(Type, Int)] -> [SCGEntry]
findCalls [Name]
cases Guardedness
guarded (Type -> Type -> Type
forall n. TT n -> TT n -> TT n
substV Type
v Type
e) [Name]
pvs [(Type, Int)]
pargs
  findCalls [Name]
cases Guardedness
guarded (Bind Name
n Binder Type
t Type
e) [Name]
pvs [(Type, Int)]
pargs
        = [Name]
-> Guardedness -> Type -> [Name] -> [(Type, Int)] -> [SCGEntry]
findCalls [Name]
cases Guardedness
Unguarded (Binder Type -> Type
forall b. Binder b -> b
binderTy Binder Type
t) [Name]
pvs [(Type, Int)]
pargs [SCGEntry] -> [SCGEntry] -> [SCGEntry]
forall a. [a] -> [a] -> [a]
++
          [Name]
-> Guardedness -> Type -> [Name] -> [(Type, Int)] -> [SCGEntry]
findCalls [Name]
cases Guardedness
guarded Type
e (Name
n Name -> [Name] -> [Name]
forall a. a -> [a] -> [a]
: [Name]
pvs) [(Type, Int)]
pargs
  findCalls [Name]
cases Guardedness
guarded (P NameType
_ Name
f Type
_ ) [Name]
pvs [(Type, Int)]
pargs
      | Bool -> Bool
not (Name
f Name -> [Name] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Name]
pvs) = [(Name
f, [])]
  findCalls [Name]
_ Guardedness
_ Type
_ [Name]
_ [(Type, Int)]
_ = []

  -- Assumption is that names are preserved in the case block (shadowing
  -- dealt with by the elaborator) so we can just assume the top level names
  -- are okay for building the size change
  findCallsCase :: [Name]
-> Guardedness
-> Name
-> [Type]
-> [Name]
-> [(Type, Int)]
-> [SCGEntry]
findCallsCase [Name]
cases Guardedness
guarded Name
n [Type]
args [Name]
pvs [(Type, Int)]
pargs
      = case Name -> Context -> Maybe Def
lookupDefExact Name
n (IState -> Context
tt_ctxt IState
ist) of
           Just (CaseOp CaseInfo
_ Type
_ [(Type, Bool)]
_ [Either Type (Type, Type)]
pats [([Name], Type, Type)]
_ CaseDefs
cd) ->
                ((Type, Type) -> [SCGEntry]) -> [(Type, Type)] -> [SCGEntry]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap ([Name]
-> [Name]
-> [(Type, Int)]
-> [Type]
-> Guardedness
-> (Type, Type)
-> [SCGEntry]
fccPat [Name]
cases [Name]
pvs [(Type, Int)]
pargs [Type]
args Guardedness
guarded) ([Either Type (Type, Type)] -> [(Type, Type)]
forall a b. [Either a b] -> [b]
rights [Either Type (Type, Type)]
pats)
           Maybe Def
Nothing -> []

  fccPat :: [Name]
-> [Name]
-> [(Type, Int)]
-> [Type]
-> Guardedness
-> (Type, Type)
-> [SCGEntry]
fccPat [Name]
cases [Name]
pvs [(Type, Int)]
pargs [Type]
args Guardedness
g (Type
lhs, Type
rhs)
      = let lhs' :: Type
lhs' = Type -> Type
delazy Type
lhs
            rhs' :: Type
rhs' = Type -> Type
delazy Type
rhs
            (Type
_, [Type]
pargs_case) = Type -> (Type, [Type])
forall n. TT n -> (TT n, [TT n])
unApply (Type -> Type
forall n. TT n -> TT n
dePat Type
lhs')
            -- pargs is a pair of a term, and the argument position that
            -- term appears in. If any of the arguments to the case block
            -- are also on the lhs, we also want those patterns to appear
            -- in the parg list so that we'll spot patterns which are
            -- smaller than then
            newpargs :: [Maybe (Type, Int)]
newpargs = [Type] -> [(Type, Int)] -> [Maybe (Type, Int)]
newPArg [Type]
args [(Type, Int)]
pargs
            -- Now need to update the rhs of the case with the names in the
            -- outer definition: In rhs', wherever we see what's in pargs_case,
            -- replace it with the corresponding thing in pargs
            csubs :: [(Type, Type)]
csubs = [Type] -> [Type] -> [(Type, Type)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Type]
pargs_case [Type]
args
            newrhs :: Type
newrhs = [(Type, Type)] -> Type -> Type
forall n. Eq n => [(TT n, TT n)] -> TT n -> TT n
doCaseSubs [(Type, Type)]
csubs (Type -> Type
forall n. TT n -> TT n
dePat Type
rhs')
            pargs' :: [(Type, Int)]
pargs' = [(Type, Int)]
pargs [(Type, Int)] -> [(Type, Int)] -> [(Type, Int)]
forall a. [a] -> [a] -> [a]
++ [Maybe (Type, Int)] -> [Type] -> [(Type, Int)]
forall a b a. [Maybe (a, b)] -> [a] -> [(a, b)]
addPArg [Maybe (Type, Int)]
newpargs [Type]
pargs_case in
               [Name]
-> Guardedness -> Type -> [Name] -> [(Type, Int)] -> [SCGEntry]
findCalls [Name]
cases Guardedness
g Type
newrhs [Name]
pvs [(Type, Int)]
pargs'
    where
      doCaseSubs :: [(TT n, TT n)] -> TT n -> TT n
doCaseSubs [] TT n
tm = TT n
tm
      doCaseSubs ((TT n
x, TT n
x') : [(TT n, TT n)]
cs) TT n
tm
           = [(TT n, TT n)] -> TT n -> TT n
doCaseSubs (TT n -> TT n -> [(TT n, TT n)] -> [(TT n, TT n)]
forall n. Eq n => TT n -> TT n -> [(TT n, TT n)] -> [(TT n, TT n)]
subIn TT n
x TT n
x' [(TT n, TT n)]
cs) (TT n -> TT n -> TT n -> TT n
forall n. Eq n => TT n -> TT n -> TT n -> TT n
substTerm TT n
x TT n
x' TT n
tm)

      subIn :: TT n -> TT n -> [(TT n, TT n)] -> [(TT n, TT n)]
subIn TT n
x TT n
x' [] = []
      subIn TT n
x TT n
x' ((TT n
l, TT n
r) : [(TT n, TT n)]
cs)
          = (TT n -> TT n -> TT n -> TT n
forall n. Eq n => TT n -> TT n -> TT n -> TT n
substTerm TT n
x TT n
x' TT n
l, TT n -> TT n -> TT n -> TT n
forall n. Eq n => TT n -> TT n -> TT n -> TT n
substTerm TT n
x TT n
x' TT n
r) (TT n, TT n) -> [(TT n, TT n)] -> [(TT n, TT n)]
forall a. a -> [a] -> [a]
: TT n -> TT n -> [(TT n, TT n)] -> [(TT n, TT n)]
subIn TT n
x TT n
x' [(TT n, TT n)]
cs

  addPArg :: [Maybe (a, b)] -> [a] -> [(a, b)]
addPArg (Just (a
t, b
i) : [Maybe (a, b)]
ts) (a
t' : [a]
ts') = (a
t', b
i) (a, b) -> [(a, b)] -> [(a, b)]
forall a. a -> [a] -> [a]
: [Maybe (a, b)] -> [a] -> [(a, b)]
addPArg [Maybe (a, b)]
ts [a]
ts'
  addPArg (Maybe (a, b)
Nothing : [Maybe (a, b)]
ts) (a
t' : [a]
ts') = [Maybe (a, b)] -> [a] -> [(a, b)]
addPArg [Maybe (a, b)]
ts [a]
ts'
  addPArg [Maybe (a, b)]
_ [a]
_ = []

  newPArg :: [Term] -> [(Term, Int)] -> [Maybe (Term, Int)]
  newPArg :: [Type] -> [(Type, Int)] -> [Maybe (Type, Int)]
newPArg (Type
t : [Type]
ts) [(Type, Int)]
pargs = case Type -> [(Type, Int)] -> Maybe Int
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Type
t [(Type, Int)]
pargs of
                                Just Int
i -> (Type, Int) -> Maybe (Type, Int)
forall a. a -> Maybe a
Just (Type
t, Int
i) Maybe (Type, Int) -> [Maybe (Type, Int)] -> [Maybe (Type, Int)]
forall a. a -> [a] -> [a]
: [Type] -> [(Type, Int)] -> [Maybe (Type, Int)]
newPArg [Type]
ts [(Type, Int)]
pargs
                                Maybe Int
Nothing -> Maybe (Type, Int)
forall a. Maybe a
Nothing Maybe (Type, Int) -> [Maybe (Type, Int)] -> [Maybe (Type, Int)]
forall a. a -> [a] -> [a]
: [Type] -> [(Type, Int)] -> [Maybe (Type, Int)]
newPArg [Type]
ts [(Type, Int)]
pargs
  newPArg [] [(Type, Int)]
pargs = []

  expandToArity :: Name -> [Maybe a] -> [Maybe a]
expandToArity Name
n [Maybe a]
args
     = case Name -> Context -> [Type]
lookupTy Name
n (IState -> Context
tt_ctxt IState
ist) of
            [Type
ty] -> Integer -> Type -> [Maybe a] -> [Maybe a]
forall a n a. Num a => a -> TT n -> [Maybe a] -> [Maybe a]
expand Integer
0 (Context -> Env -> Type -> Type
normalise (IState -> Context
tt_ctxt IState
ist) [] Type
ty) [Maybe a]
args
            [Type]
_ -> [Maybe a]
args
     where expand :: a -> TT n -> [Maybe a] -> [Maybe a]
expand a
i (Bind n
n (Pi RigCount
_ Maybe ImplicitInfo
_ TT n
_ TT n
_) TT n
sc) (Maybe a
x : [Maybe a]
xs) = Maybe a
x Maybe a -> [Maybe a] -> [Maybe a]
forall a. a -> [a] -> [a]
: a -> TT n -> [Maybe a] -> [Maybe a]
expand (a
i a -> a -> a
forall a. Num a => a -> a -> a
+ a
1) TT n
sc [Maybe a]
xs
           expand a
i (Bind n
n (Pi RigCount
_ Maybe ImplicitInfo
_ TT n
_ TT n
_) TT n
sc) [] = Maybe a
forall a. Maybe a
Nothing Maybe a -> [Maybe a] -> [Maybe a]
forall a. a -> [a] -> [a]
: a -> TT n -> [Maybe a] -> [Maybe a]
expand (a
i a -> a -> a
forall a. Num a => a -> a -> a
+ a
1) TT n
sc []
           expand a
i TT n
_ [Maybe a]
xs = [Maybe a]
xs

  mkChange :: Name -> [Type] -> [(Type, a)] -> [(Name, [Maybe (a, SizeChange)])]
mkChange Name
n [Type]
args [(Type, a)]
pargs = [(Name
n, Name -> [Maybe (a, SizeChange)] -> [Maybe (a, SizeChange)]
forall a. Name -> [Maybe a] -> [Maybe a]
expandToArity Name
n ([Type] -> [Maybe (a, SizeChange)]
sizes [Type]
args))]
    where
      sizes :: [Type] -> [Maybe (a, SizeChange)]
sizes [] = []
      sizes (Type
a : [Type]
as) = Type -> [(Type, a)] -> Maybe (a, SizeChange)
forall a. Type -> [(Type, a)] -> Maybe (a, SizeChange)
checkSize Type
a [(Type, a)]
pargs Maybe (a, SizeChange)
-> [Maybe (a, SizeChange)] -> [Maybe (a, SizeChange)]
forall a. a -> [a] -> [a]
: [Type] -> [Maybe (a, SizeChange)]
sizes [Type]
as

      -- find which argument in pargs <a> is smaller than, if any
      checkSize :: Type -> [(Type, a)] -> Maybe (a, SizeChange)
checkSize Type
a ((Type
p, a
i) : [(Type, a)]
ps)
          | Type
a Type -> Type -> Bool
forall a. Eq a => a -> a -> Bool
== Type
p = (a, SizeChange) -> Maybe (a, SizeChange)
forall a. a -> Maybe a
Just (a
i, SizeChange
Same)
          | (P NameType
_ (UN Text
as) Type
_, [Type
_,Type
_,Type
arg,Type
_]) <- Type -> (Type, [Type])
forall n. TT n -> (TT n, [TT n])
unApply Type
a,
            Text
as Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== [Char] -> Text
txt [Char]
"assert_smaller" Bool -> Bool -> Bool
&& Type
arg Type -> Type -> Bool
forall a. Eq a => a -> a -> Bool
== Type
p
                  = (a, SizeChange) -> Maybe (a, SizeChange)
forall a. a -> Maybe a
Just (a
i, SizeChange
Smaller)
          | Maybe Type -> Type -> (Type, Maybe Type) -> Bool
smaller Maybe Type
forall a. Maybe a
Nothing Type
a (Type
p, Maybe Type
forall a. Maybe a
Nothing) = (a, SizeChange) -> Maybe (a, SizeChange)
forall a. a -> Maybe a
Just (a
i, SizeChange
Smaller)
          | Bool
otherwise = Type -> [(Type, a)] -> Maybe (a, SizeChange)
checkSize Type
a [(Type, a)]
ps
      checkSize Type
a [] = Maybe (a, SizeChange)
forall a. Maybe a
Nothing

      -- Can't be smaller than an erased thing (need to be careful here
      -- because Erased equals everything)
      smaller :: Maybe Type -> Type -> (Type, Maybe Type) -> Bool
smaller Maybe Type
_ Type
_ (Type
Erased, Maybe Type
_) = Bool
False -- never smaller than an erased thing
      -- If a == t, and we're under a cosntructor, we've found something
      -- smaller
      smaller (Just Type
tyn) Type
a (Type
t, Just Type
tyt) | Type
a Type -> Type -> Bool
forall a. Eq a => a -> a -> Bool
== Type
t = Bool
True
      smaller Maybe Type
ty Type
a (ap :: Type
ap@(App AppStatus Name
_ Type
f Type
s), Maybe Type
_)
          -- Nothing can be smaller than a delayed infinite thing...
          | (P (DCon Int
_ Int
_ Bool
_) (UN Text
d) Type
_, [P NameType
_ (UN Text
reason) Type
_, Type
_, Type
_]) <- Type -> (Type, [Type])
forall n. TT n -> (TT n, [TT n])
unApply Type
ap,
            Text
d Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== [Char] -> Text
txt [Char]
"Delay" Bool -> Bool -> Bool
&& Text
reason Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== [Char] -> Text
txt [Char]
"Infinite"
               = Bool
False
          | (P (DCon Int
_ Int
_ Bool
_) Name
n Type
_, [Type]
args) <- Type -> (Type, [Type])
forall n. TT n -> (TT n, [TT n])
unApply Type
ap
               = let tyn :: Type
tyn = Name -> Type
getType Name
n in
                     ((Type, Maybe Type) -> Bool) -> [(Type, Maybe Type)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Maybe Type -> Type -> (Type, Maybe Type) -> Bool
smaller (Maybe Type
ty Maybe Type -> Maybe Type -> Maybe Type
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` Type -> Maybe Type
forall a. a -> Maybe a
Just Type
tyn) Type
a)
                         ([Type] -> [Maybe Type] -> [(Type, Maybe Type)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Type]
args (((Name, Type) -> Maybe Type) -> [(Name, Type)] -> [Maybe Type]
forall a b. (a -> b) -> [a] -> [b]
map (Name, Type) -> Maybe Type
forall a a. (a, a) -> Maybe a
toJust (Type -> [(Name, Type)]
forall n. TT n -> [(n, TT n)]
getArgTys Type
tyn)))
      -- check higher order recursive arguments
      smaller Maybe Type
ty (App AppStatus Name
_ Type
f Type
s) (Type, Maybe Type)
a = Maybe Type -> Type -> (Type, Maybe Type) -> Bool
smaller Maybe Type
ty Type
f (Type, Maybe Type)
a
      smaller Maybe Type
_ Type
_ (Type, Maybe Type)
_ = Bool
False

      toJust :: (a, a) -> Maybe a
toJust (a
n, a
t) = a -> Maybe a
forall a. a -> Maybe a
Just a
t

      getType :: Name -> Type
getType Name
n = case Name -> Context -> Maybe Type
lookupTyExact Name
n (IState -> Context
tt_ctxt IState
ist) of
                       Just Type
ty -> Type -> Type
delazy (Context -> Env -> Type -> Type
normalise (IState -> Context
tt_ctxt IState
ist) [] Type
ty) -- must exist

  dePat :: TT n -> TT n
dePat (Bind n
x (PVar RigCount
_ TT n
ty) TT n
sc) = TT n -> TT n
dePat (TT n -> TT n -> TT n
forall n. TT n -> TT n -> TT n
instantiate (NameType -> n -> TT n -> TT n
forall n. NameType -> n -> TT n -> TT n
P NameType
Bound n
x TT n
ty) TT n
sc)
  dePat TT n
t = TT n
t

  patvars :: TT a -> [a]
patvars (Bind a
x (PVar RigCount
_ TT a
_) TT a
sc) = a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: TT a -> [a]
patvars TT a
sc
  patvars TT a
_ = []

  allGuarded :: Name -> IState -> Bool
allGuarded Name
n IState
ist = case Name -> Ctxt [FnOpt] -> Maybe [FnOpt]
forall a. Name -> Ctxt a -> Maybe a
lookupCtxtExact Name
n (IState -> Ctxt [FnOpt]
idris_flags IState
ist) of
                          Maybe [FnOpt]
Nothing -> Bool
False
                          Just [FnOpt]
fs -> FnOpt
AllGuarded FnOpt -> [FnOpt] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [FnOpt]
fs

checkSizeChange :: Name -> Idris Totality
checkSizeChange :: Name -> Idris Totality
checkSizeChange Name
n = do
   IState
ist <- StateT IState (ExceptT Err IO) IState
getIState
   case Name -> Ctxt CGInfo -> [CGInfo]
forall a. Name -> Ctxt a -> [a]
lookupCtxt Name
n (IState -> Ctxt CGInfo
idris_callgraph IState
ist) of
       [CGInfo
cg] -> do let ms :: [[SCGEntry]]
ms = IState -> [SCGEntry] -> [SCGEntry] -> [[SCGEntry]]
mkMultiPaths IState
ist [] (CGInfo -> [SCGEntry]
scg CGInfo
cg)
                  Int -> [Char] -> Idris ()
logCoverage Int
5 ([Char]
"Multipath for " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Name -> [Char]
forall a. Show a => a -> [Char]
show Name
n [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
":\n" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
                            [Char]
"from " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [SCGEntry] -> [Char]
forall a. Show a => a -> [Char]
show (CGInfo -> [SCGEntry]
scg CGInfo
cg) [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"\n" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
                            Int -> [Char]
forall a. Show a => a -> [Char]
show ([[SCGEntry]] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [[SCGEntry]]
ms) [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"\n" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
                            [Char] -> [[Char]] -> [Char]
showSep [Char]
"\n" (([SCGEntry] -> [Char]) -> [[SCGEntry]] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map [SCGEntry] -> [Char]
forall a. Show a => a -> [Char]
show [[SCGEntry]]
ms))
                  Int -> [Char] -> Idris ()
logCoverage Int
6 (CGInfo -> [Char]
forall a. Show a => a -> [Char]
show CGInfo
cg)
                  -- every multipath must have an infinitely descending
                  -- thread, then the function terminates
                  -- also need to checks functions called are all total
                  -- (Unchecked is okay as we'll spot problems here)
                  let tot :: [Totality]
tot = ([SCGEntry] -> Totality) -> [[SCGEntry]] -> [Totality]
forall a b. (a -> b) -> [a] -> [b]
map (IState -> Name -> Int -> [SCGEntry] -> Totality
checkMP IState
ist Name
n (IState -> Name -> Int
getArity IState
ist Name
n)) [[SCGEntry]]
ms
                  Int -> [Char] -> Idris ()
logCoverage Int
4 ([Char] -> Idris ()) -> [Char] -> Idris ()
forall a b. (a -> b) -> a -> b
$ [Char]
"Generated " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show ([Totality] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Totality]
tot) [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" paths"
                  Int -> [Char] -> Idris ()
logCoverage Int
5 ([Char] -> Idris ()) -> [Char] -> Idris ()
forall a b. (a -> b) -> a -> b
$ [Char]
"Paths for " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Name -> [Char]
forall a. Show a => a -> [Char]
show Name
n [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" yield " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
                       ([Char] -> [[Char]] -> [Char]
showSep [Char]
"\n" ((([SCGEntry], Totality) -> [Char])
-> [([SCGEntry], Totality)] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map ([SCGEntry], Totality) -> [Char]
forall a. Show a => a -> [Char]
show ([[SCGEntry]] -> [Totality] -> [([SCGEntry], Totality)]
forall a b. [a] -> [b] -> [(a, b)]
zip [[SCGEntry]]
ms [Totality]
tot)))
                  Totality -> Idris Totality
forall (m :: * -> *) a. Monad m => a -> m a
return ([Totality] -> Totality
noPartial [Totality]
tot)
       [] -> do Int -> [Char] -> Idris ()
logCoverage Int
5 ([Char] -> Idris ()) -> [Char] -> Idris ()
forall a b. (a -> b) -> a -> b
$ [Char]
"No paths for " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Name -> [Char]
forall a. Show a => a -> [Char]
show Name
n
                Totality -> Idris Totality
forall (m :: * -> *) a. Monad m => a -> m a
return Totality
Unchecked
  where getArity :: IState -> Name -> Int
getArity IState
ist Name
n
          = case Name -> Context -> [Type]
lookupTy Name
n (IState -> Context
tt_ctxt IState
ist) of
                 [Type
ty] -> Type -> Int
forall n. TT n -> Int
arity (Context -> Env -> Type -> Type
normalise (IState -> Context
tt_ctxt IState
ist) [] Type
ty)
                 [Type]
_ -> [Char] -> Int
forall a. HasCallStack => [Char] -> a
error [Char]
"Can't happen: checkSizeChange.getArity"

type MultiPath = [SCGEntry]

mkMultiPaths :: IState -> MultiPath -> [SCGEntry] -> [MultiPath]
mkMultiPaths :: IState -> [SCGEntry] -> [SCGEntry] -> [[SCGEntry]]
mkMultiPaths IState
ist [SCGEntry]
path [] = [[SCGEntry] -> [SCGEntry]
forall a. [a] -> [a]
reverse [SCGEntry]
path]
mkMultiPaths IState
ist [SCGEntry]
path [SCGEntry]
cg = (SCGEntry -> [[SCGEntry]]) -> [SCGEntry] -> [[SCGEntry]]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap SCGEntry -> [[SCGEntry]]
extend [SCGEntry]
cg
  where extend :: SCGEntry -> [[SCGEntry]]
extend (Name
nextf, [Maybe (Int, SizeChange)]
args)
           | (Name
nextf, [Maybe (Int, SizeChange)]
args) SCGEntry -> [SCGEntry] -> Bool
`inPath` [SCGEntry]
path = [ [SCGEntry] -> [SCGEntry]
forall a. [a] -> [a]
reverse ((Name
nextf, [Maybe (Int, SizeChange)]
args) SCGEntry -> [SCGEntry] -> [SCGEntry]
forall a. a -> [a] -> [a]
: [SCGEntry]
path) ]
           | [Totality
Unchecked] <- Name -> Context -> [Totality]
lookupTotal Name
nextf (IState -> Context
tt_ctxt IState
ist)
               = case Name -> Ctxt CGInfo -> [CGInfo]
forall a. Name -> Ctxt a -> [a]
lookupCtxt Name
nextf (IState -> Ctxt CGInfo
idris_callgraph IState
ist) of
                    [CGInfo
ncg] -> IState -> [SCGEntry] -> [SCGEntry] -> [[SCGEntry]]
mkMultiPaths IState
ist ((Name
nextf, [Maybe (Int, SizeChange)]
args) SCGEntry -> [SCGEntry] -> [SCGEntry]
forall a. a -> [a] -> [a]
: [SCGEntry]
path) (CGInfo -> [SCGEntry]
scg CGInfo
ncg)
                    [CGInfo]
_ -> [ [SCGEntry] -> [SCGEntry]
forall a. [a] -> [a]
reverse ((Name
nextf, [Maybe (Int, SizeChange)]
args) SCGEntry -> [SCGEntry] -> [SCGEntry]
forall a. a -> [a] -> [a]
: [SCGEntry]
path) ]
           | Bool
otherwise = [ [SCGEntry] -> [SCGEntry]
forall a. [a] -> [a]
reverse ((Name
nextf, [Maybe (Int, SizeChange)]
args) SCGEntry -> [SCGEntry] -> [SCGEntry]
forall a. a -> [a] -> [a]
: [SCGEntry]
path) ]

        inPath :: SCGEntry -> [SCGEntry] -> Bool
        inPath :: SCGEntry -> [SCGEntry] -> Bool
inPath SCGEntry
f [] = Bool
False
        inPath SCGEntry
f (SCGEntry
g : [SCGEntry]
gs) = SCGEntry -> SCGEntry -> Bool
smallerEq SCGEntry
f SCGEntry
g Bool -> Bool -> Bool
|| SCGEntry
f SCGEntry -> SCGEntry -> Bool
forall a. Eq a => a -> a -> Bool
== SCGEntry
g Bool -> Bool -> Bool
|| SCGEntry -> [SCGEntry] -> Bool
inPath SCGEntry
f [SCGEntry]
gs

        smallerEq :: SCGEntry -> SCGEntry -> Bool
        smallerEq :: SCGEntry -> SCGEntry -> Bool
smallerEq (Name
f, [Maybe (Int, SizeChange)]
args) (Name
g, [Maybe (Int, SizeChange)]
args')
            = Name
f Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== Name
g Bool -> Bool -> Bool
&& Bool -> Bool
not ([Maybe (Int, SizeChange)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ((Maybe (Int, SizeChange) -> Bool)
-> [Maybe (Int, SizeChange)] -> [Maybe (Int, SizeChange)]
forall a. (a -> Bool) -> [a] -> [a]
filter Maybe (Int, SizeChange) -> Bool
forall a. Maybe (a, SizeChange) -> Bool
smallers [Maybe (Int, SizeChange)]
args))
                     Bool -> Bool -> Bool
&& (Maybe (Int, SizeChange) -> Bool)
-> [Maybe (Int, SizeChange)] -> [Maybe (Int, SizeChange)]
forall a. (a -> Bool) -> [a] -> [a]
filter Maybe (Int, SizeChange) -> Bool
forall a. Maybe (a, SizeChange) -> Bool
smallers [Maybe (Int, SizeChange)]
args [Maybe (Int, SizeChange)] -> [Maybe (Int, SizeChange)] -> Bool
forall a. Eq a => a -> a -> Bool
== (Maybe (Int, SizeChange) -> Bool)
-> [Maybe (Int, SizeChange)] -> [Maybe (Int, SizeChange)]
forall a. (a -> Bool) -> [a] -> [a]
filter Maybe (Int, SizeChange) -> Bool
forall a. Maybe (a, SizeChange) -> Bool
smallers [Maybe (Int, SizeChange)]
args'
        smallers :: Maybe (a, SizeChange) -> Bool
smallers (Just (a
_, SizeChange
Smaller)) = Bool
True
        smallers Maybe (a, SizeChange)
_ = Bool
False

-- If any route along the multipath leads to infinite descent, we're fine.
-- Try a route beginning with every argument.
--   If we reach a point we've been to before, but with a smaller value,
--   that means there is an infinitely descending path from that argument.

checkMP :: IState -> Name -> Int -> MultiPath -> Totality
checkMP :: IState -> Name -> Int -> [SCGEntry] -> Totality
checkMP IState
ist Name
topfn Int
i [SCGEntry]
mp = if Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0
                     then let paths :: [Totality]
paths = ((Int -> Totality) -> [Int] -> [Totality]
forall a b. (a -> b) -> [a] -> [b]
map (Int -> [((SCGEntry, Int), Int)] -> [SCGEntry] -> Int -> Totality
tryPath Int
0 [] [SCGEntry]
mp) [Int
0..Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1]) in
--                               trace ("Paths " ++ show paths) $
                               [Totality] -> Totality
collapse [Totality]
paths
                     else Int -> [((SCGEntry, Int), Int)] -> [SCGEntry] -> Int -> Totality
tryPath Int
0 [] [SCGEntry]
mp Int
0
  where
    mkBig :: (a, b) -> (a, b)
mkBig (a
e, b
d) = (a
e, b
10000)

    tryPath :: Int -> [((SCGEntry, Int), Int)] -> MultiPath -> Int -> Totality
    tryPath :: Int -> [((SCGEntry, Int), Int)] -> [SCGEntry] -> Int -> Totality
tryPath Int
desc [((SCGEntry, Int), Int)]
path [] Int
_ = [Int] -> Totality
Total []
--     tryPath desc path ((UN "believe_me", _) : _) arg
--             = Partial BelieveMe
    -- if we get to a constructor, it's fine as long as it's strictly positive
    tryPath Int
desc [((SCGEntry, Int), Int)]
path ((Name
f, [Maybe (Int, SizeChange)]
_) : [SCGEntry]
es) Int
arg
        | [TyDecl (DCon Int
_ Int
_ Bool
_) Type
_] <- Name -> Context -> [Def]
lookupDef Name
f (IState -> Context
tt_ctxt IState
ist)
            = case Name -> Context -> Maybe Totality
lookupTotalExact Name
f (IState -> Context
tt_ctxt IState
ist) of
                   Just (Total [Int]
_) -> Totality
Unchecked -- okay so far
                   Just (Partial PReason
_) -> PReason -> Totality
Partial ([Name] -> PReason
Other [Name
f])
                   Maybe Totality
x -> Totality
Unchecked -- An error elsewhere, set as Unchecked to make
                                  -- some progress
        | [TyDecl (TCon Int
_ Int
_) Type
_] <- Name -> Context -> [Def]
lookupDef Name
f (IState -> Context
tt_ctxt IState
ist)
            = [Int] -> Totality
Total []
    tryPath Int
desc [((SCGEntry, Int), Int)]
path (e :: SCGEntry
e@(Name
f, [Maybe (Int, SizeChange)]
args) : [SCGEntry]
es) Int
arg
        | [Total [Int]
a] <- Name -> Context -> [Totality]
lookupTotal Name
f (IState -> Context
tt_ctxt IState
ist) = [Int] -> Totality
Total [Int]
a
        | SCGEntry
e SCGEntry -> [SCGEntry] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [SCGEntry]
es Bool -> Bool -> Bool
&& [Maybe (Int, SizeChange)] -> Bool
forall a. [Maybe a] -> Bool
allNothing [Maybe (Int, SizeChange)]
args = PReason -> Totality
Partial ([Name] -> PReason
Mutual [Name
f])
    tryPath Int
desc [((SCGEntry, Int), Int)]
path (e :: SCGEntry
e@(Name
f, [Maybe (Int, SizeChange)]
nextargs) : [SCGEntry]
es) Int
arg
        | [Total [Int]
a] <- Name -> Context -> [Totality]
lookupTotal Name
f (IState -> Context
tt_ctxt IState
ist) = [Int] -> Totality
Total [Int]
a
        | [Partial PReason
_] <- Name -> Context -> [Totality]
lookupTotal Name
f (IState -> Context
tt_ctxt IState
ist) = PReason -> Totality
Partial ([Name] -> PReason
Other [Name
f])
        | Just Int
d <- (SCGEntry, Int) -> [((SCGEntry, Int), Int)] -> Maybe Int
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup (SCGEntry
e, Int
arg) [((SCGEntry, Int), Int)]
path
            = if Int
desc Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 -- Now lower than when we were last here
                   then -- trace ("Descent " ++ show (desc - d) ++ " "
                        --      ++ show (path, e)) $
                        [Int] -> Totality
Total []
                   else PReason -> Totality
Partial ([Name] -> PReason
Mutual ((((SCGEntry, Int), Int) -> Name)
-> [((SCGEntry, Int), Int)] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (SCGEntry -> Name
forall a b. (a, b) -> a
fst (SCGEntry -> Name)
-> (((SCGEntry, Int), Int) -> SCGEntry)
-> ((SCGEntry, Int), Int)
-> Name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SCGEntry, Int) -> SCGEntry
forall a b. (a, b) -> a
fst ((SCGEntry, Int) -> SCGEntry)
-> (((SCGEntry, Int), Int) -> (SCGEntry, Int))
-> ((SCGEntry, Int), Int)
-> SCGEntry
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((SCGEntry, Int), Int) -> (SCGEntry, Int)
forall a b. (a, b) -> a
fst) [((SCGEntry, Int), Int)]
path [Name] -> [Name] -> [Name]
forall a. [a] -> [a] -> [a]
++ [Name
f]))
        | SCGEntry
e SCGEntry -> [SCGEntry] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` (((SCGEntry, Int), Int) -> SCGEntry)
-> [((SCGEntry, Int), Int)] -> [SCGEntry]
forall a b. (a -> b) -> [a] -> [b]
map ((SCGEntry, Int) -> SCGEntry
forall a b. (a, b) -> a
fst ((SCGEntry, Int) -> SCGEntry)
-> (((SCGEntry, Int), Int) -> (SCGEntry, Int))
-> ((SCGEntry, Int), Int)
-> SCGEntry
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((SCGEntry, Int), Int) -> (SCGEntry, Int)
forall a b. (a, b) -> a
fst) [((SCGEntry, Int), Int)]
path
           Bool -> Bool -> Bool
&& Bool -> Bool
not (Name
f Name -> [Name] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` (SCGEntry -> Name) -> [SCGEntry] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map SCGEntry -> Name
forall a b. (a, b) -> a
fst [SCGEntry]
es)
              = PReason -> Totality
Partial ([Name] -> PReason
Mutual ((((SCGEntry, Int), Int) -> Name)
-> [((SCGEntry, Int), Int)] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (SCGEntry -> Name
forall a b. (a, b) -> a
fst (SCGEntry -> Name)
-> (((SCGEntry, Int), Int) -> SCGEntry)
-> ((SCGEntry, Int), Int)
-> Name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SCGEntry, Int) -> SCGEntry
forall a b. (a, b) -> a
fst ((SCGEntry, Int) -> SCGEntry)
-> (((SCGEntry, Int), Int) -> (SCGEntry, Int))
-> ((SCGEntry, Int), Int)
-> SCGEntry
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((SCGEntry, Int), Int) -> (SCGEntry, Int)
forall a b. (a, b) -> a
fst) [((SCGEntry, Int), Int)]
path [Name] -> [Name] -> [Name]
forall a. [a] -> [a] -> [a]
++ [Name
f]))
        | [Totality
Unchecked] <- Name -> Context -> [Totality]
lookupTotal Name
f (IState -> Context
tt_ctxt IState
ist) =
            let argspos :: [(Maybe (Int, SizeChange), Int)]
argspos = [Maybe (Int, SizeChange)]
-> [Int] -> [(Maybe (Int, SizeChange), Int)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Maybe (Int, SizeChange)]
nextargs [Int
0..]
                pathres :: [Totality]
pathres =
                  do (Maybe (Int, SizeChange)
a, Int
pos) <- [(Maybe (Int, SizeChange), Int)]
argspos
                     case Maybe (Int, SizeChange)
a of
                        Maybe (Int, SizeChange)
Nothing -> -- gone up, but if the
                                   -- rest definitely terminates without any
                                   -- cycles (including the path so far, which
                                   -- we take as inaccessible) the path is fine
                            Totality -> [Totality]
forall (m :: * -> *) a. Monad m => a -> m a
return (Totality -> [Totality]) -> Totality -> [Totality]
forall a b. (a -> b) -> a -> b
$ Int -> [((SCGEntry, Int), Int)] -> [SCGEntry] -> Int -> Totality
tryPath Int
0 ((((SCGEntry, Int), Int) -> ((SCGEntry, Int), Int))
-> [((SCGEntry, Int), Int)] -> [((SCGEntry, Int), Int)]
forall a b. (a -> b) -> [a] -> [b]
map ((SCGEntry, Int), Int) -> ((SCGEntry, Int), Int)
forall b a b. Num b => (a, b) -> (a, b)
mkBig (((SCGEntry
e, Int
arg), Int
desc) ((SCGEntry, Int), Int)
-> [((SCGEntry, Int), Int)] -> [((SCGEntry, Int), Int)]
forall a. a -> [a] -> [a]
: [((SCGEntry, Int), Int)]
path)) [SCGEntry]
es Int
pos
                        Just (Int
nextarg, SizeChange
sc) ->
                          if Int
nextarg Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
arg then
                            case SizeChange
sc of
                              SizeChange
Same -> Totality -> [Totality]
forall (m :: * -> *) a. Monad m => a -> m a
return (Totality -> [Totality]) -> Totality -> [Totality]
forall a b. (a -> b) -> a -> b
$ Int -> [((SCGEntry, Int), Int)] -> [SCGEntry] -> Int -> Totality
tryPath Int
desc (((SCGEntry
e, Int
arg), Int
desc) ((SCGEntry, Int), Int)
-> [((SCGEntry, Int), Int)] -> [((SCGEntry, Int), Int)]
forall a. a -> [a] -> [a]
: [((SCGEntry, Int), Int)]
path)
                                                       [SCGEntry]
es Int
pos
                              SizeChange
Smaller -> Totality -> [Totality]
forall (m :: * -> *) a. Monad m => a -> m a
return (Totality -> [Totality]) -> Totality -> [Totality]
forall a b. (a -> b) -> a -> b
$ Int -> [((SCGEntry, Int), Int)] -> [SCGEntry] -> Int -> Totality
tryPath (Int
descInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
                                                          (((SCGEntry
e, Int
arg), Int
desc) ((SCGEntry, Int), Int)
-> [((SCGEntry, Int), Int)] -> [((SCGEntry, Int), Int)]
forall a. a -> [a] -> [a]
: [((SCGEntry, Int), Int)]
path)
                                                          [SCGEntry]
es
                                                          Int
pos
                              SizeChange
_ -> [Char] -> [Totality] -> [Totality]
forall a. [Char] -> a -> a
trace ([Char]
"Shouldn't happen " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ SCGEntry -> [Char]
forall a. Show a => a -> [Char]
show SCGEntry
e) ([Totality] -> [Totality]) -> [Totality] -> [Totality]
forall a b. (a -> b) -> a -> b
$
                                      Totality -> [Totality]
forall (m :: * -> *) a. Monad m => a -> m a
return (PReason -> Totality
Partial PReason
Itself)
                            else Totality -> [Totality]
forall (m :: * -> *) a. Monad m => a -> m a
return Totality
Unchecked in
--                   trace (show (desc, argspos, path, es, pathres)) $
                   [Totality] -> Totality
collapse [Totality]
pathres

        | Bool
otherwise = Totality
Unchecked

allNothing :: [Maybe a] -> Bool
allNothing :: [Maybe a] -> Bool
allNothing [Maybe a]
xs = [(Maybe a, Integer)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([(Maybe a, Integer)] -> [(Maybe a, Integer)]
forall a b. [(Maybe a, b)] -> [(Maybe a, b)]
collapseNothing ([Maybe a] -> [Integer] -> [(Maybe a, Integer)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Maybe a]
xs [Integer
0..]))

collapseNothing :: [(Maybe a, b)] -> [(Maybe a, b)]
collapseNothing :: [(Maybe a, b)] -> [(Maybe a, b)]
collapseNothing ((Maybe a
Nothing, b
t) : [(Maybe a, b)]
xs)
   = (Maybe a
forall a. Maybe a
Nothing, b
t) (Maybe a, b) -> [(Maybe a, b)] -> [(Maybe a, b)]
forall a. a -> [a] -> [a]
: ((Maybe a, b) -> Bool) -> [(Maybe a, b)] -> [(Maybe a, b)]
forall a. (a -> Bool) -> [a] -> [a]
filter (\ (Maybe a
x, b
_) -> case Maybe a
x of
                                             Maybe a
Nothing -> Bool
False
                                             Maybe a
_ -> Bool
True) [(Maybe a, b)]
xs
collapseNothing ((Maybe a, b)
x : [(Maybe a, b)]
xs) = (Maybe a, b)
x (Maybe a, b) -> [(Maybe a, b)] -> [(Maybe a, b)]
forall a. a -> [a] -> [a]
: [(Maybe a, b)] -> [(Maybe a, b)]
forall a b. [(Maybe a, b)] -> [(Maybe a, b)]
collapseNothing [(Maybe a, b)]
xs
collapseNothing [] = []

noPartial :: [Totality] -> Totality
noPartial :: [Totality] -> Totality
noPartial (Partial PReason
p : [Totality]
xs) = PReason -> Totality
Partial PReason
p
noPartial (Totality
_ : [Totality]
xs)         = [Totality] -> Totality
noPartial [Totality]
xs
noPartial []               = [Int] -> Totality
Total []

collapse :: [Totality] -> Totality
collapse :: [Totality] -> Totality
collapse [Totality]
xs = Totality -> [Totality] -> Totality
collapse' Totality
Unchecked [Totality]
xs
collapse' :: Totality -> [Totality] -> Totality
collapse' Totality
def (Total [Int]
r : [Totality]
xs)   = [Int] -> Totality
Total [Int]
r
collapse' Totality
def (Totality
Unchecked : [Totality]
xs) = Totality -> [Totality] -> Totality
collapse' Totality
def [Totality]
xs
collapse' Totality
def (Totality
d : [Totality]
xs)         = Totality -> [Totality] -> Totality
collapse' Totality
d [Totality]
xs
-- collapse' Unchecked []         = Total []
collapse' Totality
def []               = Totality
def

-- totalityCheckBlock :: Idris ()
-- totalityCheckBlock = do
--          ist <- getIState
--          -- Do totality checking after entire mutual block
--          mapM_ (\n -> do logElab 5 $ "Simplifying " ++ show n
--                          ctxt' <- do ctxt <- getContext
--                                      tclift $ simplifyCasedef n (getErasureInfo ist) ctxt
--                          setContext ctxt')
--                  (map snd (idris_totcheck ist))
--          mapM_ buildSCG (idris_totcheck ist)
--          mapM_ checkDeclTotality (idris_totcheck ist)
--          clear_totcheck