{-|
Module      : Idris.Elab.Data
Description : Code to elaborate data structures.

License     : BSD3
Maintainer  : The Idris Community.
-}
{-# LANGUAGE CPP, PatternGuards #-}
module Idris.Elab.Data(elabData) where

import Idris.AbsSyntax
import Idris.ASTUtils
import Idris.Core.Evaluate
import Idris.Core.TT
import Idris.Core.Typecheck
import Idris.Delaborate
import Idris.Docstrings
import Idris.Elab.Rewrite
import Idris.Elab.Type
import Idris.Elab.Utils
import Idris.Elab.Value
import Idris.Error
import Idris.Output (iWarn, sendHighlighting)

import Util.Pretty

#if (MIN_VERSION_base(4,11,0))
import Prelude hiding (id, (.), (<>))
#else
import Prelude hiding (id, (.))
#endif

import Control.Category
import Control.Monad
import Data.List
import qualified Data.Set as S

warnLC :: FC -> Name -> Idris ()
warnLC :: FC -> Name -> Idris ()
warnLC FC
fc Name
n
   = FC -> OutputDoc -> Idris ()
iWarn FC
fc (OutputDoc -> Idris ()) -> OutputDoc -> Idris ()
forall a b. (a -> b) -> a -> b
$ Name -> OutputDoc
annName Name
n OutputDoc -> OutputDoc -> OutputDoc
forall a. Doc a -> Doc a -> Doc a
<+> String -> OutputDoc
forall a. String -> Doc a
text String
"has a name which may be implicitly bound."
           OutputDoc -> OutputDoc -> OutputDoc
forall a. Doc a -> Doc a -> Doc a
<> OutputDoc
forall a. Doc a
line OutputDoc -> OutputDoc -> OutputDoc
forall a. Doc a -> Doc a -> Doc a
<> String -> OutputDoc
forall a. String -> Doc a
text String
"This is likely to lead to problems!"

elabData :: ElabInfo -> SyntaxInfo -> Docstring (Either Err PTerm)-> [(Name, Docstring (Either Err PTerm))] -> FC -> DataOpts -> PData -> Idris ()
elabData :: ElabInfo
-> SyntaxInfo
-> Docstring (Either Err PTerm)
-> [(Name, Docstring (Either Err PTerm))]
-> FC
-> DataOpts
-> PData
-> Idris ()
elabData ElabInfo
info SyntaxInfo
syn Docstring (Either Err PTerm)
doc [(Name, Docstring (Either Err PTerm))]
argDocs FC
fc DataOpts
opts (PLaterdecl Name
n FC
nfc PTerm
t_in)
    = do Int -> String -> Idris ()
logElab Int
1 ((FC, Docstring (Either Err PTerm)) -> String
forall a. Show a => a -> String
show (FC
fc, Docstring (Either Err PTerm)
doc))
         FC -> Name -> Idris ()
checkUndefined FC
fc Name
n
         Bool -> Idris () -> Idris ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Name -> Bool
implicitable (Name -> Name
nsroot Name
n)) (Idris () -> Idris ()) -> Idris () -> Idris ()
forall a b. (a -> b) -> a -> b
$ FC -> Name -> Idris ()
warnLC FC
fc Name
n
         (cty, _, t, inacc) <- ElabInfo
-> SyntaxInfo
-> FC
-> FnOpts
-> Name
-> PTerm
-> Idris (Type, Type, PTerm, [(Int, Name)])
buildType ElabInfo
info SyntaxInfo
syn FC
fc [] Name
n PTerm
t_in

         addIBC (IBCDef n)
         updateContext (addTyDecl n (TCon 0 0) cty) -- temporary, to check cons
         sendHighlighting $ S.fromList [(FC' nfc, AnnName n Nothing Nothing Nothing)]

elabData ElabInfo
info SyntaxInfo
syn Docstring (Either Err PTerm)
doc [(Name, Docstring (Either Err PTerm))]
argDocs FC
fc DataOpts
opts (PDatadecl Name
n FC
nfc PTerm
t_in [(Docstring (Either Err PTerm),
  [(Name, Docstring (Either Err PTerm))], Name, FC, PTerm, FC,
  [Name])]
dcons)
    = do let codata :: Bool
codata = DataOpt
Codata DataOpt -> DataOpts -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` DataOpts
opts
         Int -> String -> Idris ()
logElab Int
1 (FC -> String
forall a. Show a => a -> String
show FC
fc)
         undef <- FC -> Name -> Idris Bool
isUndefined FC
fc Name
n
         when (implicitable (nsroot n)) $ warnLC fc n
         (cty, ckind, t, inacc) <- buildType info syn fc [] n t_in
         -- if n is defined already, make sure it is just a type declaration
         -- with the same type we've just elaborated, and no constructors
         -- yet
         i <- getIState
         checkDefinedAs fc n cty i
         -- temporary, to check cons
         when undef $ updateContext (addTyDecl n (TCon 0 0) cty)
         let cnameinfo = ElabInfo -> [Name] -> ElabInfo
cinfo ElabInfo
info (((Docstring (Either Err PTerm),
  [(Name, Docstring (Either Err PTerm))], Name, FC, PTerm, FC,
  [Name])
 -> Name)
-> [(Docstring (Either Err PTerm),
     [(Name, Docstring (Either Err PTerm))], Name, FC, PTerm, FC,
     [Name])]
-> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Docstring (Either Err PTerm),
 [(Name, Docstring (Either Err PTerm))], Name, FC, PTerm, FC,
 [Name])
-> Name
forall {a} {b} {c} {d} {e} {f} {g}. (a, b, c, d, e, f, g) -> c
cname [(Docstring (Either Err PTerm),
  [(Name, Docstring (Either Err PTerm))], Name, FC, PTerm, FC,
  [Name])]
dcons)
         unique <- case getRetTy (normalise (tt_ctxt i) [] cty) of
                        UType Universe
UniqueType -> Bool -> Idris Bool
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
                        UType Universe
_ -> Bool -> Idris Bool
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
                        TType UExp
_ -> Bool -> Idris Bool
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
                        Type
rt -> TC Bool -> Idris Bool
forall a. TC a -> Idris a
tclift (TC Bool -> Idris Bool) -> TC Bool -> Idris Bool
forall a b. (a -> b) -> a -> b
$ Err -> TC Bool
forall a. Err -> TC a
tfail (FC -> Err -> Err
forall t. FC -> Err' t -> Err' t
At FC
fc (String -> Name -> Maybe Type -> Err -> Err
forall t. String -> Name -> Maybe t -> Err' t -> Err' t
Elaborating String
"type constructor " Name
n Maybe Type
forall a. Maybe a
Nothing (String -> Err
forall t. String -> Err' t
Msg String
"Not a valid type constructor")))
         cons <- mapM (elabCon cnameinfo syn n codata (getRetTy cty) ckind) dcons
         ttag <- getName

         ctxt <- getContext
         let params = Name -> Type -> [Type] -> [Int]
findParams Name
n (Context -> Env -> Type -> Type
normalise Context
ctxt [] Type
cty) (((Name, Type) -> Type) -> [(Name, Type)] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map (Name, Type) -> Type
forall a b. (a, b) -> b
snd [(Name, Type)]
cons)

         logElab 2 $ "Parameters : " ++ show params
         addParamConstraints fc params cty cons

         i <- getIState
         -- TI contains information about mutually declared types - this will
         -- be updated when the mutual block is complete
         putIState (i { idris_datatypes =
                          addDef n (TI (map fst cons) codata opts params [n]
                                             (any linearArg (map snd cons)))
                                             (idris_datatypes i) })
         addIBC (IBCDef n)
         addIBC (IBCData n)
         checkDocs fc argDocs t
         doc' <- elabDocTerms info doc
         argDocs' <- mapM (\(Name
n, Docstring (Either Err PTerm)
d) -> do d' <- ElabInfo
-> Docstring (Either Err PTerm) -> Idris (Docstring DocTerm)
elabDocTerms ElabInfo
info Docstring (Either Err PTerm)
d
                                         return (n, d')) argDocs
         addDocStr n doc' argDocs'
         addIBC (IBCDoc n)
         let metainf = [Int] -> MetaInformation
DataMI [Int]
params
         addIBC (IBCMetaInformation n metainf)
         -- TMP HACK! Make this a data option
         updateContext (addDatatype (Data n ttag cty unique cons))
         updateContext (setMetaInformation n metainf)

         mapM_ totcheck (zip (repeat fc) (map fst cons))
--          mapM_ (checkPositive n) cons

         -- if there's exactly one constructor,
         -- mark both the type and the constructor as detaggable
         case cons of
            [(Name
cn,Type
ct)] -> Name -> Idris ()
setDetaggable Name
cn Idris () -> Idris () -> Idris ()
forall a b.
StateT IState (ExceptT Err IO) a
-> StateT IState (ExceptT Err IO) b
-> StateT IState (ExceptT Err IO) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Name -> Idris ()
setDetaggable Name
n
                Idris () -> Idris () -> Idris ()
forall a b.
StateT IState (ExceptT Err IO) a
-> StateT IState (ExceptT Err IO) b
-> StateT IState (ExceptT Err IO) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> IBCWrite -> Idris ()
addIBC (Name -> IBCWrite
IBCOpt Name
cn) Idris () -> Idris () -> Idris ()
forall a b.
StateT IState (ExceptT Err IO) a
-> StateT IState (ExceptT Err IO) b
-> StateT IState (ExceptT Err IO) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> IBCWrite -> Idris ()
addIBC (Name -> IBCWrite
IBCOpt Name
n)
            [(Name, Type)]
_ -> () -> Idris ()
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

         -- create a rewriting lemma
         when (n /= sUN "=") $
            elabRewriteLemma info n cty
         -- Emit highlighting info
         sendHighlighting $ S.fromList $ [(FC' nfc, AnnName n Nothing Nothing Nothing)] ++
           map (\(Docstring (Either Err PTerm)
_, [(Name, Docstring (Either Err PTerm))]
_, Name
n, FC
nfc, PTerm
_, FC
_, [Name]
_) ->
                 (FC -> FC'
FC' FC
nfc, Name
-> Maybe NameOutput
-> Maybe String
-> Maybe String
-> OutputAnnotation
AnnName Name
n Maybe NameOutput
forall a. Maybe a
Nothing Maybe String
forall a. Maybe a
Nothing Maybe String
forall a. Maybe a
Nothing))
               dcons
  where
        checkDefinedAs :: FC -> Name -> Type -> IState -> Idris ()
checkDefinedAs FC
fc Name
n Type
t IState
i
            = let defined :: Idris a
defined = TC a -> Idris a
forall a. TC a -> Idris a
tclift (TC a -> Idris a) -> TC a -> Idris a
forall a b. (a -> b) -> a -> b
$ Err -> TC a
forall a. Err -> TC a
tfail (FC -> Err -> Err
forall t. FC -> Err' t -> Err' t
At FC
fc (Name -> Err
forall t. Name -> Err' t
AlreadyDefined Name
n))
                  ctxt :: Context
ctxt = IState -> Context
tt_ctxt IState
i in
                case Name -> Context -> [Def]
lookupDef Name
n Context
ctxt of
                   [] -> () -> Idris ()
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
                   [TyDecl NameType
_ Type
ty] ->
                      case Context -> Env -> Type -> Type -> TC ()
converts Context
ctxt [] Type
t Type
ty of
                           OK () -> case Name -> Ctxt TypeInfo -> Maybe TypeInfo
forall a. Name -> Ctxt a -> Maybe a
lookupCtxtExact Name
n (IState -> Ctxt TypeInfo
idris_datatypes IState
i) of
                                         Maybe TypeInfo
Nothing -> () -> Idris ()
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
                                         Maybe TypeInfo
_ -> Idris ()
forall {a}. Idris a
defined
                           TC ()
_ -> Idris ()
forall {a}. Idris a
defined
                   [Def]
_ -> Idris ()
forall {a}. Idris a
defined


        cname :: (a, b, c, d, e, f, g) -> c
cname (a
_, b
_, c
n, d
_, e
_, f
_, g
_) = c
n

        -- Abuse of ElabInfo.
        -- TODO Contemplate whether the ElabInfo type needs modification.
        cinfo :: ElabInfo -> [Name] -> ElabInfo
        cinfo :: ElabInfo -> [Name] -> ElabInfo
cinfo ElabInfo
info [Name]
ds
          = let newps :: [(Name, PTerm)]
newps = ElabInfo -> [(Name, PTerm)]
params ElabInfo
info
                dsParams :: [(Name, [a])]
dsParams = (Name -> (Name, [a])) -> [Name] -> [(Name, [a])]
forall a b. (a -> b) -> [a] -> [b]
map (\Name
n -> (Name
n, [])) [Name]
ds
                newb :: Ctxt [Name]
newb = [(Name, [Name])] -> Ctxt [Name] -> Ctxt [Name]
forall a. [(Name, a)] -> Ctxt a -> Ctxt a
addAlist [(Name, [Name])]
forall {a}. [(Name, [a])]
dsParams (ElabInfo -> Ctxt [Name]
inblock ElabInfo
info) in
                ElabInfo
info { params = newps,
                       inblock = newb,
                       liftname = id -- Is this appropriate?
                     }

elabCon :: ElabInfo -> SyntaxInfo -> Name -> Bool ->
           Type -> -- for unique kind checking
           Type -> -- data type's kind
           (Docstring (Either Err PTerm), [(Name, Docstring (Either Err PTerm))], Name, FC, PTerm, FC, [Name]) ->
           Idris (Name, Type)
elabCon :: ElabInfo
-> SyntaxInfo
-> Name
-> Bool
-> Type
-> Type
-> (Docstring (Either Err PTerm),
    [(Name, Docstring (Either Err PTerm))], Name, FC, PTerm, FC,
    [Name])
-> StateT IState (ExceptT Err IO) (Name, Type)
elabCon ElabInfo
info SyntaxInfo
syn Name
tn Bool
codata Type
expkind Type
dkind (Docstring (Either Err PTerm)
doc, [(Name, Docstring (Either Err PTerm))]
argDocs, Name
n, FC
nfc, PTerm
t_in, FC
fc, [Name]
forcenames)
    = do FC -> Name -> Idris ()
checkUndefined FC
fc Name
n
         Bool -> Idris () -> Idris ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Name -> Bool
implicitable (Name -> Name
nsroot Name
n)) (Idris () -> Idris ()) -> Idris () -> Idris ()
forall a b. (a -> b) -> a -> b
$ FC -> Name -> Idris ()
warnLC FC
fc Name
n
         Int -> String -> Idris ()
logElab Int
2 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ FC -> String
forall a. Show a => a -> String
show FC
fc String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
":Constructor " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Name -> String
forall a. Show a => a -> String
show Name
n String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" : " String -> String -> String
forall a. [a] -> [a] -> [a]
++ PTerm -> String
forall a. Show a => a -> String
show PTerm
t_in
         (cty, ckind, t, inacc) <- ElabInfo
-> SyntaxInfo
-> FC
-> FnOpts
-> Name
-> PTerm
-> Idris (Type, Type, PTerm, [(Int, Name)])
buildType ElabInfo
info SyntaxInfo
syn FC
fc [FnOpt
Constructor] Name
n (if Bool
codata then PTerm -> PTerm
mkLazy PTerm
t_in else PTerm
t_in)
         ctxt <- getContext
         let cty' = Context -> Env -> Type -> Type
normalise Context
ctxt [] Type
cty
         checkUniqueKind ckind expkind

         -- Check that the constructor type is, in fact, a part of the family being defined
         tyIs n cty'
         -- Need to calculate forceability from the non-normalised type,
         -- because we might not be able to export the definitions which
         -- we're normalising which changes the forceability status!
         let force = if Name
tn Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== String -> Name
sUN String
"Delayed"
                        then [] -- TMP HACK! Totality checker needs this info
                        else Context -> Name -> Type -> [Int]
forceArgs Context
ctxt Name
tn Type
cty

         logElab 5 $ show fc ++ ":Constructor " ++ show n ++ " elaborated : " ++ show t
         logElab 5 $ "Inaccessible args: " ++ show inacc
         logElab 5 $ "Forceable args: " ++ show force
         logElab 2 $ "---> " ++ show n ++ " : " ++ show cty

         -- Add to the context (this is temporary, so that later constructors
         -- can be indexed by it)
         updateContext (addTyDecl n (DCon 0 0 False) cty)

         addIBC (IBCDef n)
         checkDocs fc argDocs t
         doc' <- elabDocTerms info doc
         argDocs' <- mapM (\(Name
n, Docstring (Either Err PTerm)
d) -> do d' <- ElabInfo
-> Docstring (Either Err PTerm) -> Idris (Docstring DocTerm)
elabDocTerms ElabInfo
info Docstring (Either Err PTerm)
d
                                         return (n, d')) argDocs
         addDocStr n doc' argDocs'
         addIBC (IBCDoc n)
         fputState (opt_inaccessible . ist_optimisation n) inacc
         fputState (opt_forceable . ist_optimisation n) force
         addIBC (IBCOpt n)
         return (n, cty)
  where
    tyIs :: Name -> Type -> Idris ()
tyIs Name
con (Bind Name
n Binder Type
b Type
sc) = Name -> Type -> Idris ()
tyIs Name
con (Type -> Type -> Type
forall n. TT n -> TT n -> TT n
substV (NameType -> Name -> Type -> Type
forall n. NameType -> n -> TT n -> TT n
P NameType
Bound Name
n Type
forall n. TT n
Erased) Type
sc)
    tyIs Name
con Type
t | (P NameType
Bound Name
n' Type
_, [Type]
_) <- Type -> (Type, [Type])
forall n. TT n -> (TT n, [TT n])
unApply Type
t
        = if Name
n' Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
/= Name
tn then
               TC () -> Idris ()
forall a. TC a -> Idris a
tclift (TC () -> Idris ()) -> TC () -> Idris ()
forall a b. (a -> b) -> a -> b
$ Err -> TC ()
forall a. Err -> TC a
tfail (FC -> Err -> Err
forall t. FC -> Err' t -> Err' t
At FC
fc (String -> Name -> Maybe Type -> Err -> Err
forall t. String -> Name -> Maybe t -> Err' t -> Err' t
Elaborating String
"constructor " Name
con Maybe Type
forall a. Maybe a
Nothing
                         (String -> Err
forall t. String -> Err' t
Msg (String
"Type level variable " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Name -> String
forall a. Show a => a -> String
show Name
n' String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" is not " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Name -> String
forall a. Show a => a -> String
show Name
tn))))
             else () -> Idris ()
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    tyIs Name
con Type
t | (P NameType
_ Name
n' Type
_, [Type]
_) <- Type -> (Type, [Type])
forall n. TT n -> (TT n, [TT n])
unApply Type
t
        = if Name
n' Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
/= Name
tn then TC () -> Idris ()
forall a. TC a -> Idris a
tclift (TC () -> Idris ()) -> TC () -> Idris ()
forall a b. (a -> b) -> a -> b
$ Err -> TC ()
forall a. Err -> TC a
tfail (FC -> Err -> Err
forall t. FC -> Err' t -> Err' t
At FC
fc (String -> Name -> Maybe Type -> Err -> Err
forall t. String -> Name -> Maybe t -> Err' t -> Err' t
Elaborating String
"constructor " Name
con Maybe Type
forall a. Maybe a
Nothing (String -> Err
forall t. String -> Err' t
Msg (Name -> String
forall a. Show a => a -> String
show Name
n' String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" is not " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Name -> String
forall a. Show a => a -> String
show Name
tn))))
             else () -> Idris ()
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    tyIs Name
con Type
t = TC () -> Idris ()
forall a. TC a -> Idris a
tclift (TC () -> Idris ()) -> TC () -> Idris ()
forall a b. (a -> b) -> a -> b
$ Err -> TC ()
forall a. Err -> TC a
tfail (FC -> Err -> Err
forall t. FC -> Err' t -> Err' t
At FC
fc (String -> Name -> Maybe Type -> Err -> Err
forall t. String -> Name -> Maybe t -> Err' t -> Err' t
Elaborating String
"constructor " Name
con Maybe Type
forall a. Maybe a
Nothing (String -> Err
forall t. String -> Err' t
Msg (Type -> String
forall a. Show a => a -> String
show Type
t String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" is not " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Name -> String
forall a. Show a => a -> String
show Name
tn))))

    mkLazy :: PTerm -> PTerm
mkLazy (PPi Plicity
pl Name
n FC
nfc PTerm
ty PTerm
sc)
        = let ty' :: PTerm
ty' = if PTerm -> Bool
getTyName PTerm
ty
                       then FC -> PTerm -> [PArg] -> PTerm
PApp FC
fc (FC -> [FC] -> Name -> PTerm
PRef FC
fc [] (String -> Name
sUN String
"Delayed"))
                            [PTerm -> PArg
forall {t}. t -> PArg' t
pexp (FC -> [FC] -> Name -> PTerm
PRef FC
fc [] (String -> Name
sUN String
"Infinite")),
                             PTerm -> PArg
forall {t}. t -> PArg' t
pexp PTerm
ty]
                       else PTerm
ty in
              Plicity -> Name -> FC -> PTerm -> PTerm -> PTerm
PPi Plicity
pl Name
n FC
nfc PTerm
ty' (PTerm -> PTerm
mkLazy PTerm
sc)
    mkLazy PTerm
t = PTerm
t

    getTyName :: PTerm -> Bool
getTyName (PApp FC
_ (PRef FC
_ [FC]
_ Name
n) [PArg]
_) = Name
n Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== Name -> Name
nsroot Name
tn
    getTyName (PRef FC
_ [FC]
_ Name
n) = Name
n Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== Name -> Name
nsroot Name
tn
    getTyName PTerm
_ = Bool
False

    -- if the constructor is a UniqueType, the datatype must be too
    -- (AnyType is fine, since that is checked for uniqueness too)
    -- if hte contructor is AnyType, the datatype must be at least AnyType
    checkUniqueKind :: TT n -> TT n -> Idris ()
checkUniqueKind (UType Universe
NullType) (UType Universe
NullType) = () -> Idris ()
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    checkUniqueKind (UType Universe
NullType) TT n
_
        = TC () -> Idris ()
forall a. TC a -> Idris a
tclift (TC () -> Idris ()) -> TC () -> Idris ()
forall a b. (a -> b) -> a -> b
$ Err -> TC ()
forall a. Err -> TC a
tfail (FC -> Err -> Err
forall t. FC -> Err' t -> Err' t
At FC
fc (Universe -> Name -> Err
forall t. Universe -> Name -> Err' t
UniqueKindError Universe
NullType Name
n))
    checkUniqueKind (UType Universe
UniqueType) (UType Universe
UniqueType) = () -> Idris ()
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    checkUniqueKind (UType Universe
UniqueType) (UType Universe
AllTypes) = () -> Idris ()
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    checkUniqueKind (UType Universe
UniqueType) TT n
_
        = TC () -> Idris ()
forall a. TC a -> Idris a
tclift (TC () -> Idris ()) -> TC () -> Idris ()
forall a b. (a -> b) -> a -> b
$ Err -> TC ()
forall a. Err -> TC a
tfail (FC -> Err -> Err
forall t. FC -> Err' t -> Err' t
At FC
fc (Universe -> Name -> Err
forall t. Universe -> Name -> Err' t
UniqueKindError Universe
UniqueType Name
n))
    checkUniqueKind (UType Universe
AllTypes) (UType Universe
AllTypes) = () -> Idris ()
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    checkUniqueKind (UType Universe
AllTypes) (UType Universe
UniqueType) = () -> Idris ()
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    checkUniqueKind (UType Universe
AllTypes) TT n
_
        = TC () -> Idris ()
forall a. TC a -> Idris a
tclift (TC () -> Idris ()) -> TC () -> Idris ()
forall a b. (a -> b) -> a -> b
$ Err -> TC ()
forall a. Err -> TC a
tfail (FC -> Err -> Err
forall t. FC -> Err' t -> Err' t
At FC
fc (Universe -> Name -> Err
forall t. Universe -> Name -> Err' t
UniqueKindError Universe
AllTypes Name
n))
    checkUniqueKind TT n
_ TT n
_ = () -> Idris ()
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

forceArgs :: Context -> Name -> Type -> [Int]
forceArgs :: Context -> Name -> Type -> [Int]
forceArgs Context
ctxt Name
tn Type
ty = Int -> Type -> [Int]
forceFrom Int
0 Type
ty
  where
    -- for each argument, substitute in MN pos "FF"
    -- then when we look at the return type, if we see MN pos name
    -- constructor guarded, then 'pos' is a forceable position
    forceFrom :: Int -> Type -> [Int]
    forceFrom :: Int -> Type -> [Int]
forceFrom Int
i (Bind Name
n (Pi RigCount
_ Maybe ImplicitInfo
_ Type
_ Type
_) Type
sc)
       = Int -> Type -> [Int]
forceFrom (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Type -> Type -> Type
forall n. TT n -> TT n -> TT n
substV (NameType -> Name -> Type -> Type
forall n. NameType -> n -> TT n -> TT n
P NameType
Ref (Int -> String -> Name
sMN Int
i String
"FF") Type
forall n. TT n
Erased) Type
sc)
    forceFrom Int
i Type
sc
        -- Go under the top level type application
        -- We risk affecting erasure of more complex indices, so we'll only
        -- mark something forced if *everything* which appears in an index
        -- is forceable
        -- (FIXME: Actually the real risk is if we erase something a programmer
        -- definitely wants, which is particularly the case with 'views'.
        -- So perhaps we need a way of marking that in the source?)
        | (P NameType
_ Name
ty Type
_, [Type]
args) <- Type -> (Type, [Type])
forall n. TT n -> (TT n, [TT n])
unApply Type
sc,
          Name
ty Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== Name
tn -- Must be the right top level type!
             = if [Int] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ((Type -> [Int]) -> [Type] -> [Int]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Bool -> Type -> [Int]
findNonForcePos Bool
True) [Type]
args)
                  then [Int] -> [Int]
forall a. Eq a => [a] -> [a]
nub ((Type -> [Int]) -> [Type] -> [Int]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Type -> [Int]
findForcePos [Type]
args)
                  else []
    forceFrom Int
i Type
sc = []

    findForcePos :: Type -> [Int]
findForcePos (P NameType
_ (MN Int
i Text
ff) Type
_)
        | Text
ff Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== String -> Text
txt String
"FF" = [Int
i]
    -- Only look under constructors in applications
    findForcePos ap :: Type
ap@(App AppStatus Name
_ Type
f Type
a)
        | (P NameType
_ Name
con Type
_, [Type]
args) <- Type -> (Type, [Type])
forall n. TT n -> (TT n, [TT n])
unApply Type
ap,
          Name -> Context -> Bool
isDConName Name
con Context
ctxt
            = [Int] -> [Int]
forall a. Eq a => [a] -> [a]
nub ([Int] -> [Int]) -> [Int] -> [Int]
forall a b. (a -> b) -> a -> b
$ (Type -> [Int]) -> [Type] -> [Int]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Type -> [Int]
findForcePos [Type]
args
    findForcePos Type
_ = []

    findNonForcePos :: Bool -> Type -> [Int]
findNonForcePos Bool
fok (P NameType
_ (MN Int
i Text
ff) Type
_)
        | Text
ff Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== String -> Text
txt String
"FF" = if Bool
fok then [] else [Int
i]
    -- Look under non-constructors in applications for things which can't
    -- be forced
    findNonForcePos Bool
fok ap :: Type
ap@(App AppStatus Name
_ Type
f Type
a)
        | (P NameType
_ Name
con Type
_, [Type]
args) <- Type -> (Type, [Type])
forall n. TT n -> (TT n, [TT n])
unApply Type
ap
            = [Int] -> [Int]
forall a. Eq a => [a] -> [a]
nub ([Int] -> [Int]) -> [Int] -> [Int]
forall a b. (a -> b) -> a -> b
$ (Type -> [Int]) -> [Type] -> [Int]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Bool -> Type -> [Int]
findNonForcePos (Bool
fok Bool -> Bool -> Bool
&& Name -> Context -> Bool
isConName Name
con Context
ctxt)) [Type]
args
    findNonForcePos Bool
_ Type
_ = []

addParamConstraints :: FC -> [Int] -> Type -> [(Name, Type)] -> Idris ()
addParamConstraints :: FC -> [Int] -> Type -> [(Name, Type)] -> Idris ()
addParamConstraints FC
fc [Int]
ps Type
cty [(Name, Type)]
cons
   = case Type -> Type
forall n. TT n -> TT n
getRetTy Type
cty of
          TType UExp
cvar -> ((Type, [Name]) -> Idris ()) -> [(Type, [Name])] -> Idris ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ ([Int] -> UExp -> (Type, [Name]) -> Idris ()
forall {t :: * -> *} {n} {p}.
(Foldable t, Eq n) =>
p -> UExp -> (TT n, t n) -> Idris ()
addConConstraint [Int]
ps UExp
cvar)
                              (((Name, Type) -> (Type, [Name]))
-> [(Name, Type)] -> [(Type, [Name])]
forall a b. (a -> b) -> [a] -> [b]
map (Name, Type) -> (Type, [Name])
forall {a} {a}. (a, TT a) -> (TT a, [a])
getParamNames [(Name, Type)]
cons)
          Type
_ -> () -> Idris ()
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  where
    getParamNames :: (a, TT a) -> (TT a, [a])
getParamNames (a
n, TT a
ty) = (TT a
ty, TT a -> [a]
forall {a}. TT a -> [a]
getPs TT a
ty)

    getPs :: TT a -> [a]
getPs (Bind a
n (Pi RigCount
_ Maybe ImplicitInfo
_ TT a
_ TT a
_) TT a
sc)
       = TT a -> [a]
getPs (TT a -> TT a -> TT a
forall n. TT n -> TT n -> TT n
substV (NameType -> a -> TT a -> TT a
forall n. NameType -> n -> TT n -> TT n
P NameType
Ref a
n TT a
forall n. TT n
Erased) TT a
sc)
    getPs TT a
t | (TT a
f, [TT a]
args) <- TT a -> (TT a, [TT a])
forall n. TT n -> (TT n, [TT n])
unApply TT a
t
       = Int -> [TT a] -> [a]
forall {a}. Int -> [TT a] -> [a]
paramArgs Int
0 [TT a]
args

    paramArgs :: Int -> [TT a] -> [a]
paramArgs Int
i (P NameType
_ a
n TT a
_ : [TT a]
args) | Int
i Int -> [Int] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Int]
ps = a
n a -> [a] -> [a]
forall a. a -> [a] -> [a]
: Int -> [TT a] -> [a]
paramArgs (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) [TT a]
args
    paramArgs Int
i (TT a
_ : [TT a]
args) = Int -> [TT a] -> [a]
paramArgs (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) [TT a]
args
    paramArgs Int
i [] = []

    addConConstraint :: p -> UExp -> (TT n, t n) -> Idris ()
addConConstraint p
ps UExp
cvar (TT n
ty, t n
pnames) = TT n -> Idris ()
constraintTy TT n
ty
      where
        constraintTy :: TT n -> Idris ()
constraintTy (Bind n
n (Pi RigCount
_ Maybe ImplicitInfo
_ TT n
ty TT n
_) TT n
sc)
           = case TT n -> TT n
forall n. TT n -> TT n
getRetTy TT n
ty of
                  TType UExp
avar -> do tit <- Idris Bool
typeInType
                                   when (not tit) $ do
                                       ctxt <- getContext
                                       let tv = Context -> Int
next_tvar Context
ctxt
                                       let con = if n
n n -> t n -> Bool
forall a. Eq a => a -> t a -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` t n
pnames
                                                    then UExp -> UExp -> UConstraint
ULE UExp
avar UExp
cvar
                                                    else UExp -> UExp -> UConstraint
ULT UExp
avar UExp
cvar
                                       addConstraints fc (tv, [con])
                                       addIBC (IBCConstraint fc con)
                  TT n
_ -> () -> Idris ()
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        constraintTy TT n
t = () -> Idris ()
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()