{-|
Module      : Idris.Elab.Clause
Description : Code to elaborate clauses.

License     : BSD3
Maintainer  : The Idris Community.
-}
{-# LANGUAGE PatternGuards #-}
module Idris.Elab.Clause where

import Idris.AbsSyntax
import Idris.ASTUtils
import Idris.Core.CaseTree
import Idris.Core.Elaborate hiding (Tactic(..))
import Idris.Core.Evaluate
import Idris.Core.TT
import Idris.Core.Typecheck
import Idris.Core.WHNF
import Idris.Coverage
import Idris.DataOpts
import Idris.DeepSeq ()
import Idris.Delaborate
import Idris.Docstrings hiding (Unchecked)
import Idris.Elab.AsPat
import Idris.Elab.Term
import Idris.Elab.Transform
import Idris.Elab.Type
import Idris.Elab.Utils
import Idris.Error
import Idris.Options
import Idris.Output (iWarn, pshow, sendHighlighting)
import Idris.PartialEval
import Idris.Termination
import Idris.Transforms

import Util.Pretty hiding ((<$>))

import Prelude hiding (id, (.))

import Control.Category
import Control.DeepSeq
import Control.Monad
import qualified Control.Monad.State.Lazy as LState
import Control.Monad.State.Strict as State
import Data.List
import Data.Maybe
import qualified Data.Set as S
import Data.Word
import Debug.Trace
import Numeric

-- | Elaborate a collection of left-hand and right-hand pairs - that is, a
-- top-level definition.
elabClauses :: ElabInfo -> FC -> FnOpts -> Name -> [PClause] -> Idris ()
elabClauses :: ElabInfo -> FC -> FnOpts -> Name -> [PClause] -> Idris ()
elabClauses ElabInfo
info' FC
fc FnOpts
opts Name
n_in [PClause]
cs =
      do let n :: Name
n    = ElabInfo -> Name -> Name
liftname ElabInfo
info Name
n_in
             info :: ElabInfo
info = ElabInfo
info' { elabFC = Just fc }
         Context
ctxt <- Idris Context
getContext
         IState
ist  <- Idris IState
getIState
         [Optimisation]
optimise <- Idris [Optimisation]
getOptimise
         let petrans :: Bool
petrans = Optimisation
PETransform Optimisation -> [Optimisation] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Optimisation]
optimise
         [Int]
inacc <- ((Int, Name) -> Int) -> [(Int, Name)] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map (Int, Name) -> Int
forall a b. (a, b) -> a
fst ([(Int, Name)] -> [Int])
-> StateT IState (ExceptT Err IO) [(Int, Name)]
-> StateT IState (ExceptT Err IO) [Int]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Field IState [(Int, Name)]
-> StateT IState (ExceptT Err IO) [(Int, Name)]
forall s (m :: * -> *) a. MonadState s m => Field s a -> m a
fgetState (Field OptInfo [(Int, Name)]
opt_inaccessible Field OptInfo [(Int, Name)]
-> Field IState OptInfo -> Field IState [(Int, Name)]
forall b c a. Field b c -> Field a b -> Field a c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Name -> Field IState OptInfo
ist_optimisation Name
n)

         -- Check n actually exists, with no definition yet
         let tys :: [Term]
tys = Name -> Context -> [Term]
lookupTy Name
n Context
ctxt
         let reflect :: Bool
reflect = FnOpt
Reflection FnOpt -> FnOpts -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` FnOpts
opts
         Bool -> Idris () -> Idris ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool
reflect Bool -> Bool -> Bool
&& LanguageExt
FCReflection LanguageExt -> [LanguageExt] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` IState -> [LanguageExt]
idris_language_extensions IState
ist) (Idris () -> Idris ()) -> Idris () -> Idris ()
forall a b. (a -> b) -> a -> b
$
           Err -> Idris ()
forall a. Err -> Idris a
ierror (Err -> Idris ()) -> Err -> Idris ()
forall a b. (a -> b) -> a -> b
$ FC -> Err -> Err
forall t. FC -> Err' t -> Err' t
At FC
fc (String -> Err
forall t. String -> Err' t
Msg String
"You must turn on the FirstClassReflection extension to use %reflection")
         Name -> Context -> Idris ()
checkUndefined Name
n Context
ctxt
         Bool -> Idris () -> Idris ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([Term] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Term]
tys Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1) (Idris () -> Idris ()) -> Idris () -> Idris ()
forall a b. (a -> b) -> a -> b
$ do
           Term
fty <- case [Term]
tys of
              [] -> -- TODO: turn into a CAF if there's no arguments
                    -- question: CAFs in where blocks?
                    TC Term -> StateT IState (ExceptT Err IO) Term
forall a. TC a -> Idris a
tclift (TC Term -> StateT IState (ExceptT Err IO) Term)
-> TC Term -> StateT IState (ExceptT Err IO) Term
forall a b. (a -> b) -> a -> b
$ Err -> TC Term
forall a. Err -> TC a
tfail (Err -> TC Term) -> Err -> TC Term
forall a b. (a -> b) -> a -> b
$ FC -> Err -> Err
forall t. FC -> Err' t -> Err' t
At FC
fc (Name -> Err
forall t. Name -> Err' t
NoTypeDecl Name
n)
              [Term
ty] -> Term -> StateT IState (ExceptT Err IO) Term
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return Term
ty
           let atys_in :: [Term]
atys_in = ((Name, Term) -> Term) -> [(Name, Term)] -> [Term]
forall a b. (a -> b) -> [a] -> [b]
map (Name, Term) -> Term
forall a b. (a, b) -> b
snd (Term -> [(Name, Term)]
forall n. TT n -> [(n, TT n)]
getArgTys (Context -> Env -> Term -> Term
normalise Context
ctxt [] Term
fty))
           let atys :: [(Term, Bool)]
atys = (Term -> (Term, Bool)) -> [Term] -> [(Term, Bool)]
forall a b. (a -> b) -> [a] -> [b]
map (\Term
x -> (Term
x, Term -> Context -> Bool
isCanonical Term
x Context
ctxt)) [Term]
atys_in
           [(Either Term (Term, Term), PTerm)]
cs_elab <- ((Int, PClause)
 -> StateT
      IState (ExceptT Err IO) (Either Term (Term, Term), PTerm))
-> [(Int, PClause)]
-> StateT
     IState (ExceptT Err IO) [(Either Term (Term, Term), PTerm)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (ElabInfo
-> FnOpts
-> (Int, PClause)
-> StateT IState (ExceptT Err IO) (Either Term (Term, Term), PTerm)
elabClause ElabInfo
info FnOpts
opts)
                           ([Int] -> [PClause] -> [(Int, PClause)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0..] [PClause]
cs)
           Context
ctxt <- Idris Context
getContext
           -- pats_raw is the basic type checked version, no PE or forcing
           let optinfo :: Ctxt OptInfo
optinfo = IState -> Ctxt OptInfo
idris_optimisation IState
ist
           let ([Either Term (Term, Term)]
pats_in, [PTerm]
cs_full) = [(Either Term (Term, Term), PTerm)]
-> ([Either Term (Term, Term)], [PTerm])
forall a b. [(a, b)] -> ([a], [b])
unzip [(Either Term (Term, Term), PTerm)]
cs_elab
           let pats_raw :: [Either Term (Term, Term)]
pats_raw = (Either Term (Term, Term) -> Either Term (Term, Term))
-> [Either Term (Term, Term)] -> [Either Term (Term, Term)]
forall a b. (a -> b) -> [a] -> [b]
map (Context -> Either Term (Term, Term) -> Either Term (Term, Term)
forall {a} {b}. Context -> Either a (Term, b) -> Either a (Term, b)
simple_lhs Context
ctxt) [Either Term (Term, Term)]
pats_in
           -- We'll apply forcing to the left hand side here, so that we don't
           -- do any unnecessary case splits
           let pats_forced :: [Either Term (Term, Term)]
pats_forced = (Either Term (Term, Term) -> Either Term (Term, Term))
-> [Either Term (Term, Term)] -> [Either Term (Term, Term)]
forall a b. (a -> b) -> [a] -> [b]
map (Ctxt OptInfo
-> Either Term (Term, Term) -> Either Term (Term, Term)
forall {a} {b}.
Ctxt OptInfo -> Either a (Term, b) -> Either a (Term, b)
force_lhs Ctxt OptInfo
optinfo) [Either Term (Term, Term)]
pats_raw

           Int -> String -> Idris ()
logElab Int
3 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"Elaborated patterns:\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ [Either Term (Term, Term)] -> String
forall a. Show a => a -> String
show [Either Term (Term, Term)]
pats_raw
           Int -> String -> Idris ()
logElab Int
5 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"Forced patterns:\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ [Either Term (Term, Term)] -> String
forall a. Show a => a -> String
show [Either Term (Term, Term)]
pats_forced

           FC -> Name -> Idris ()
solveDeferred FC
fc Name
n

           -- just ensure that the structure exists
           Field IState OptInfo -> (OptInfo -> OptInfo) -> Idris ()
forall s (m :: * -> *) a.
MonadState s m =>
Field s a -> (a -> a) -> m ()
fmodifyState (Name -> Field IState OptInfo
ist_optimisation Name
n) OptInfo -> OptInfo
forall a. a -> a
forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
           IBCWrite -> Idris ()
addIBC (Name -> IBCWrite
IBCOpt Name
n)

           IState
ist <- Idris IState
getIState
           Context
ctxt <- Idris Context
getContext
           -- Don't apply rules if this is a partial evaluation definition,
           -- or we'll make something that just runs itself!
           let tpats :: [Either Term (Term, Term)]
tpats = case FnOpts -> Maybe [(Name, Maybe Int)]
specNames FnOpts
opts of
                            Maybe [(Name, Maybe Int)]
Nothing -> IState -> [Either Term (Term, Term)] -> [Either Term (Term, Term)]
transformPats IState
ist [Either Term (Term, Term)]
pats_in
                            Maybe [(Name, Maybe Int)]
_ -> [Either Term (Term, Term)]
pats_in

           -- If the definition is specialisable, this reduces the
           -- RHS
           [Either Term (Term, Term)]
pe_tm <- IState
-> [Either Term (Term, Term)]
-> StateT IState (ExceptT Err IO) [Either Term (Term, Term)]
doPartialEval IState
ist [Either Term (Term, Term)]
tpats

           let pats_pe :: [Either Term (Term, Term)]
pats_pe = if Bool
petrans
                            then (Either Term (Term, Term) -> Either Term (Term, Term))
-> [Either Term (Term, Term)] -> [Either Term (Term, Term)]
forall a b. (a -> b) -> [a] -> [b]
map (Ctxt OptInfo
-> Either Term (Term, Term) -> Either Term (Term, Term)
forall {a} {b}.
Ctxt OptInfo -> Either a (Term, b) -> Either a (Term, b)
force_lhs Ctxt OptInfo
optinfo (Either Term (Term, Term) -> Either Term (Term, Term))
-> (Either Term (Term, Term) -> Either Term (Term, Term))
-> Either Term (Term, Term)
-> Either Term (Term, Term)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Context -> Either Term (Term, Term) -> Either Term (Term, Term)
forall {a} {b}. Context -> Either a (Term, b) -> Either a (Term, b)
simple_lhs Context
ctxt) [Either Term (Term, Term)]
pe_tm
                            else [Either Term (Term, Term)]
pats_forced

           let tcase :: Bool
tcase = IOption -> Bool
opt_typecase (IState -> IOption
idris_options IState
ist)

           -- Look for 'static' names and generate new specialised
           -- definitions for them, as well as generating rewrite rules
           -- for partially evaluated definitions
           [[(Term, Term)]]
newrules <- if Bool
petrans
                          then (Either Term (Term, Term)
 -> StateT IState (ExceptT Err IO) [(Term, Term)])
-> [Either Term (Term, Term)]
-> StateT IState (ExceptT Err IO) [[(Term, Term)]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (\ Either Term (Term, Term)
e -> case Either Term (Term, Term)
e of
                                            Left Term
_ -> [(Term, Term)] -> StateT IState (ExceptT Err IO) [(Term, Term)]
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return []
                                            Right (Term
l, Term
r) -> ElabInfo
-> FC
-> Name
-> Term
-> StateT IState (ExceptT Err IO) [(Term, Term)]
elabPE ElabInfo
info FC
fc Name
n Term
r) [Either Term (Term, Term)]
pats_pe
                          else [[(Term, Term)]] -> StateT IState (ExceptT Err IO) [[(Term, Term)]]
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return []

           -- Redo transforms with the newly generated transformations, so
           -- that the specialised application we've just made gets
           -- used in place of the general one
           IState
ist <- Idris IState
getIState
           let pats_transformed :: [Either Term (Term, Term)]
pats_transformed = if Bool
petrans
                                     then IState -> [Either Term (Term, Term)] -> [Either Term (Term, Term)]
transformPats IState
ist [Either Term (Term, Term)]
pats_pe
                                     else [Either Term (Term, Term)]
pats_pe

           -- Summary of what's about to happen: Definitions go:
           --
           -- pats_in -> pats -> pdef -> pdef'

           -- addCaseDef builds case trees from <pdef> and <pdef'>

           -- pdef is the compile-time pattern definition, after forcing
           -- optimisation applied to LHS
           let pdef :: [([Name], Term, Term)]
pdef = (([(Name, Term)], Term, Term) -> ([Name], Term, Term))
-> [([(Name, Term)], Term, Term)] -> [([Name], Term, Term)]
forall a b. (a -> b) -> [a] -> [b]
map (\([(Name, Term)]
ns, Term
lhs, Term
rhs) -> (((Name, Term) -> Name) -> [(Name, Term)] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name, Term) -> Name
forall a b. (a, b) -> a
fst [(Name, Term)]
ns, Term
lhs, Term
rhs)) ([([(Name, Term)], Term, Term)] -> [([Name], Term, Term)])
-> [([(Name, Term)], Term, Term)] -> [([Name], Term, Term)]
forall a b. (a -> b) -> a -> b
$
                          (Either Term (Term, Term) -> ([(Name, Term)], Term, Term))
-> [Either Term (Term, Term)] -> [([(Name, Term)], Term, Term)]
forall a b. (a -> b) -> [a] -> [b]
map Either Term (Term, Term) -> ([(Name, Term)], Term, Term)
forall {n} {n}.
Either (TT n) (TT n, TT n) -> ([(n, TT n)], TT n, TT n)
debind [Either Term (Term, Term)]
pats_forced
           -- pdef_pe is the one which will get further optimised
           -- for run-time, with no forcing optimisation of the LHS because
           -- the affects erasure. Also, it's partially evaluated
           let pdef_pe :: [([(Name, Term)], Term, Term)]
pdef_pe = (Either Term (Term, Term) -> ([(Name, Term)], Term, Term))
-> [Either Term (Term, Term)] -> [([(Name, Term)], Term, Term)]
forall a b. (a -> b) -> [a] -> [b]
map Either Term (Term, Term) -> ([(Name, Term)], Term, Term)
forall {n} {n}.
Either (TT n) (TT n, TT n) -> ([(n, TT n)], TT n, TT n)
debind [Either Term (Term, Term)]
pats_transformed

           Int -> String -> Idris ()
logElab Int
5 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"Initial typechecked patterns:\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ [Either Term (Term, Term)] -> String
forall a. Show a => a -> String
show [Either Term (Term, Term)]
pats_raw
           Int -> String -> Idris ()
logElab Int
5 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"Initial typechecked pattern def:\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ [([Name], Term, Term)] -> String
forall a. Show a => a -> String
show [([Name], Term, Term)]
pdef

           -- NOTE: Need to store original definition so that proofs which
           -- rely on its structure aren't affected by any changes to the
           -- inliner. Just use the inlined version to generate pdef' and to
           -- help with later inlinings.

           IState
ist <- Idris IState
getIState
           Int
numArgs <- TC Int -> Idris Int
forall a. TC a -> Idris a
tclift (TC Int -> Idris Int) -> TC Int -> Idris Int
forall a b. (a -> b) -> a -> b
$ [([Name], Term, Term)] -> TC Int
forall {a} {n} {c}. [(a, TT n, c)] -> TC Int
sameLength [([Name], Term, Term)]
pdef

           case FnOpts -> Maybe [(Name, Maybe Int)]
specNames FnOpts
opts of
                Just [(Name, Maybe Int)]
_ ->
                   do Int -> String -> Idris ()
logElab Int
3 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"Partially evaluated:\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ [Either Term (Term, Term)] -> String
forall a. Show a => a -> String
show [Either Term (Term, Term)]
pats_pe
                Maybe [(Name, Maybe Int)]
_ -> () -> Idris ()
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
           Int -> String -> Idris ()
logElab Int
3 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"Transformed:\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ [Either Term (Term, Term)] -> String
forall a. Show a => a -> String
show [Either Term (Term, Term)]
pats_transformed

           Name -> [Int]
erInfo <- IState -> Name -> [Int]
getErasureInfo (IState -> Name -> [Int])
-> Idris IState -> StateT IState (ExceptT Err IO) (Name -> [Int])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Idris IState
getIState
           tree :: CaseDef
tree@(CaseDef [Name]
scargs SC
sc [Term]
_) <- TC CaseDef -> Idris CaseDef
forall a. TC a -> Idris a
tclift (TC CaseDef -> Idris CaseDef) -> TC CaseDef -> Idris CaseDef
forall a b. (a -> b) -> a -> b
$
                 Bool
-> SC
-> Bool
-> Phase
-> FC
-> [Int]
-> [(Term, Bool)]
-> [([Name], Term, Term)]
-> (Name -> [Int])
-> TC CaseDef
simpleCase Bool
tcase (String -> SC
forall t. String -> SC' t
UnmatchedCase String
"Error") Bool
reflect Phase
CompileTime FC
fc [Int]
inacc [(Term, Bool)]
atys [([Name], Term, Term)]
pdef Name -> [Int]
erInfo
           Bool
cov <- Idris Bool
coverage
           [PTerm]
pmissing <-
                   if Bool
cov Bool -> Bool -> Bool
&& Bool -> Bool
not ([Either Term (Term, Term)] -> Bool
forall {a} {a} {b}. Eq a => [Either a (TT a, b)] -> Bool
hasDefault [Either Term (Term, Term)]
pats_raw)
                      then do -- Generate clauses from the given possible cases
                              [PTerm]
missing <- FC
-> Name
-> [([Name], Term)]
-> [PTerm]
-> StateT IState (ExceptT Err IO) [PTerm]
genClauses FC
fc Name
n
                                                 ((([Name], Term, Term) -> ([Name], Term))
-> [([Name], Term, Term)] -> [([Name], Term)]
forall a b. (a -> b) -> [a] -> [b]
map (\ ([Name]
ns,Term
tm,Term
_) -> ([Name]
ns, Term
tm)) [([Name], Term, Term)]
pdef)
                                                 [PTerm]
cs_full
                              -- missing <- genMissing n scargs sc
                              [PTerm]
missing' <- ElabInfo
-> FC
-> Bool
-> Name
-> [PTerm]
-> StateT IState (ExceptT Err IO) [PTerm]
checkPossibles ElabInfo
info FC
fc Bool
True Name
n [PTerm]
missing
                              -- Filter out the ones which match one of the
                              -- given cases (including impossible ones)
                              Int -> String -> Idris ()
logElab Int
2 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"Must be unreachable (" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show ([PTerm] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [PTerm]
missing') String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"):\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++
                                          String -> [String] -> String
showSep String
"\n" ((PTerm -> String) -> [PTerm] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map PTerm -> String
showTmImpls [PTerm]
missing') String -> String -> String
forall a. [a] -> [a] -> [a]
++
                                         String
"\nAgainst: " String -> String -> String
forall a. [a] -> [a] -> [a]
++
                                          String -> [String] -> String
showSep String
"\n" ((Term -> String) -> [Term] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (\Term
t -> PTerm -> String
showTmImpls (IState -> Term -> PTerm
delab IState
ist Term
t)) ((([Name], Term, Term) -> Term) -> [([Name], Term, Term)] -> [Term]
forall a b. (a -> b) -> [a] -> [b]
map ([Name], Term, Term) -> Term
forall {a} {b} {c}. (a, b, c) -> b
getLHS [([Name], Term, Term)]
pdef))
                              -- filter out anything in missing' which is
                              -- matched by any of clhs. This might happen since
                              -- unification may force a variable to take a
                              -- particular form, rather than force a case
                              -- to be impossible.
                              [PTerm] -> StateT IState (ExceptT Err IO) [PTerm]
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return [PTerm]
missing' -- (filter (noMatch ist clhs) missing')
                      else [PTerm] -> StateT IState (ExceptT Err IO) [PTerm]
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return []
           let pcover :: Bool
pcover = [PTerm] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [PTerm]
pmissing

           -- pdef' is the version that gets compiled for run-time,
           -- so we start from the partially evaluated version
           [([Name], Term, Term)]
pdef_in' <- [([Name], Term, Term)] -> Idris [([Name], Term, Term)]
forall term. Optimisable term => term -> Idris term
applyOpts ([([Name], Term, Term)] -> Idris [([Name], Term, Term)])
-> [([Name], Term, Term)] -> Idris [([Name], Term, Term)]
forall a b. (a -> b) -> a -> b
$ (([(Name, Term)], Term, Term) -> ([Name], Term, Term))
-> [([(Name, Term)], Term, Term)] -> [([Name], Term, Term)]
forall a b. (a -> b) -> [a] -> [b]
map (\([(Name, Term)]
ns, Term
lhs, Term
rhs) -> (((Name, Term) -> Name) -> [(Name, Term)] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name, Term) -> Name
forall a b. (a, b) -> a
fst [(Name, Term)]
ns, Term
lhs, Term
rhs)) [([(Name, Term)], Term, Term)]
pdef_pe
           Context
ctxt <- Idris Context
getContext
           let pdef' :: [([Name], Term, Term)]
pdef' = (([Name], Term, Term) -> ([Name], Term, Term))
-> [([Name], Term, Term)] -> [([Name], Term, Term)]
forall a b. (a -> b) -> [a] -> [b]
map (Context -> ([Name], Term, Term) -> ([Name], Term, Term)
forall {b}. Context -> ([Name], b, Term) -> ([Name], b, Term)
simple_rt Context
ctxt) [([Name], Term, Term)]
pdef_in'

           Int -> String -> Idris ()
logElab Int
5 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"After data structure transformations:\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ [([Name], Term, Term)] -> String
forall a. Show a => a -> String
show [([Name], Term, Term)]
pdef'

           IState
ist <- Idris IState
getIState
           let tot :: Totality
tot | Bool
pcover = Totality
Unchecked -- finish later
                   | FnOpt
AssertTotal FnOpt -> FnOpts -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` FnOpts
opts = [Int] -> Totality
Total []
                   | FnOpt
PEGenerated FnOpt -> FnOpts -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` FnOpts
opts = Totality
Generated
                   | Bool
otherwise = PReason -> Totality
Partial PReason
NotCovering -- already know it's not total

           case CaseDef
tree of
               CaseDef [Name]
_ SC
_ [] -> () -> Idris ()
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
               CaseDef [Name]
_ SC
_ [Term]
xs -> (Term -> Idris ()) -> [Term] -> Idris ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (\Term
x ->
                   FC -> OutputDoc -> Idris ()
iWarn FC
fc (OutputDoc -> Idris ()) -> OutputDoc -> Idris ()
forall a b. (a -> b) -> a -> b
$ String -> OutputDoc
forall a. String -> Doc a
text String
"Unreachable case:" OutputDoc -> OutputDoc -> OutputDoc
forall a. Doc a -> Doc a -> Doc a
</> String -> OutputDoc
forall a. String -> Doc a
text (PTerm -> String
forall a. Show a => a -> String
show (IState -> Term -> PTerm
delab IState
ist Term
x))
                   ) [Term]
xs
           let knowncovering :: Bool
knowncovering = (Bool
pcover Bool -> Bool -> Bool
&& Bool
cov) Bool -> Bool -> Bool
|| FnOpt
AssertTotal FnOpt -> FnOpts -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` FnOpts
opts
           let defaultcase :: SC' (TT n)
defaultcase = if Bool
knowncovering
                                then TT n -> SC' (TT n)
forall t. t -> SC' t
STerm TT n
forall n. TT n
Erased
                                else String -> SC' (TT n)
forall t. String -> SC' t
UnmatchedCase (String -> SC' (TT n)) -> String -> SC' (TT n)
forall a b. (a -> b) -> a -> b
$ String
"*** " String -> String -> String
forall a. [a] -> [a] -> [a]
++
                                      FC -> String
forall a. Show a => a -> String
show FC
fc String -> String -> String
forall a. [a] -> [a] -> [a]
++
                                       String
":unmatched case in " 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
" ***"

           CaseDef
tree' <- TC CaseDef -> Idris CaseDef
forall a. TC a -> Idris a
tclift (TC CaseDef -> Idris CaseDef) -> TC CaseDef -> Idris CaseDef
forall a b. (a -> b) -> a -> b
$ Bool
-> SC
-> Bool
-> Phase
-> FC
-> [Int]
-> [(Term, Bool)]
-> [([Name], Term, Term)]
-> (Name -> [Int])
-> TC CaseDef
simpleCase Bool
tcase SC
forall {n}. SC' (TT n)
defaultcase Bool
reflect
                                        Phase
RunTime FC
fc [Int]
inacc [(Term, Bool)]
atys [([Name], Term, Term)]
pdef' Name -> [Int]
erInfo
           Int -> String -> Idris ()
logElab Int
3 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"Unoptimised " 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]
++ CaseDef -> String
forall a. Show a => a -> String
show CaseDef
tree
           Int -> String -> Idris ()
logElab Int
3 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"Optimised: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ CaseDef -> String
forall a. Show a => a -> String
show CaseDef
tree'
           Context
ctxt <- Idris Context
getContext
           IState
ist <- Idris IState
getIState
           IState -> Idris ()
putIState (IState
ist { idris_patdefs = addDef n (force pdef_pe, force pmissing)
                                                (idris_patdefs ist) })
           let caseInfo :: CaseInfo
caseInfo = Bool -> Bool -> Bool -> CaseInfo
CaseInfo (FnOpts -> Bool
inlinable FnOpts
opts) (FnOpts -> Bool
inlinable FnOpts
opts) (FnOpts -> Bool
dictionary FnOpts
opts)

           case Name -> Context -> Maybe Term
lookupTyExact Name
n Context
ctxt of
               Just Term
ty ->
                   do Context
ctxt' <- do Context
ctxt <- Idris Context
getContext
                                  TC Context -> Idris Context
forall a. TC a -> Idris a
tclift (TC Context -> Idris Context) -> TC Context -> Idris Context
forall a b. (a -> b) -> a -> b
$
                                    Name
-> (Name -> [Int])
-> CaseInfo
-> Bool
-> SC
-> Bool
-> Bool
-> [(Term, Bool)]
-> [Int]
-> [Either Term (Term, Term)]
-> [([Name], Term, Term)]
-> [([Name], Term, Term)]
-> Term
-> Context
-> TC Context
addCasedef Name
n Name -> [Int]
erInfo CaseInfo
caseInfo
                                                   Bool
tcase SC
forall {n}. SC' (TT n)
defaultcase
                                                   Bool
reflect
                                                   (FnOpt
AssertTotal FnOpt -> FnOpts -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` FnOpts
opts)
                                                   [(Term, Bool)]
atys
                                                   [Int]
inacc
                                                   [Either Term (Term, Term)]
pats_forced
                                                   [([Name], Term, Term)]
pdef
                                                   [([Name], Term, Term)]
pdef' Term
ty
                                                   Context
ctxt
                      Context -> Idris ()
setContext Context
ctxt'
                      IBCWrite -> Idris ()
addIBC (Name -> IBCWrite
IBCDef Name
n)
                      Name -> Idris ()
addDefinedName Name
n
                      Name -> Totality -> Idris ()
setTotality Name
n Totality
tot
                      Bool -> Idris () -> Idris ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not Bool
reflect Bool -> Bool -> Bool
&& FnOpt
PEGenerated FnOpt -> FnOpts -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` FnOpts
opts) (Idris () -> Idris ()) -> Idris () -> Idris ()
forall a b. (a -> b) -> a -> b
$
                                           do (FC, Name) -> Idris ()
totcheck (FC
fc, Name
n)
                                              (FC, Name) -> Idris ()
defer_totcheck (FC
fc, Name
n)
                      Bool -> Idris () -> Idris ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Totality
tot Totality -> Totality -> Bool
forall a. Eq a => a -> a -> Bool
/= Totality
Unchecked) (Idris () -> Idris ()) -> Idris () -> Idris ()
forall a b. (a -> b) -> a -> b
$ IBCWrite -> Idris ()
addIBC (Name -> Totality -> IBCWrite
IBCTotal Name
n Totality
tot)
                      IState
i <- Idris IState
getIState
                      Context
ctxt <- Idris Context
getContext
                      case Name -> Context -> [Def]
lookupDef Name
n Context
ctxt of
                          (CaseOp CaseInfo
_ Term
_ [(Term, Bool)]
_ [Either Term (Term, Term)]
_ [([Name], Term, Term)]
_ CaseDefs
cd : [Def]
_) ->
                            let ([Name]
scargs, SC
sc) = CaseDefs -> ([Name], SC)
cases_compiletime CaseDefs
cd in
                              do let calls :: [Name]
calls = ((Name, [[Name]]) -> Name) -> [(Name, [[Name]])] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name, [[Name]]) -> Name
forall a b. (a, b) -> a
fst ([(Name, [[Name]])] -> [Name]) -> [(Name, [[Name]])] -> [Name]
forall a b. (a -> b) -> a -> b
$ SC -> [Name] -> [(Name, [[Name]])]
findCalls SC
sc [Name]
scargs
                                 -- let scg = buildSCG i sc scargs
                                 -- add SCG later, when checking totality
                                 Int -> String -> Idris ()
logElab Int
2 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"Called names: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [Name] -> String
forall a. Show a => a -> String
show [Name]
calls
                                 -- if the definition is public, make sure
                                 -- it only uses public names
                                 Maybe Accessibility
nvis <- Name -> Idris (Maybe Accessibility)
getFromHideList Name
n
                                 case Maybe Accessibility
nvis of
                                      Just Accessibility
Public -> (Name -> Idris ()) -> [Name] -> Idris ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (FC -> Name -> Accessibility -> Accessibility -> Name -> Idris ()
checkVisibility FC
fc Name
n Accessibility
Public Accessibility
Public) [Name]
calls
                                      Maybe Accessibility
_ -> () -> Idris ()
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
                                 Name -> [Name] -> Idris ()
addCalls Name
n [Name]
calls
                                 let rig :: RigCount
rig = if Term -> Bool
linearArg (Context -> Env -> Term -> Term
whnfArgs Context
ctxt [] Term
ty)
                                              then RigCount
Rig1
                                              else RigCount
RigW
                                 (Context -> Context) -> Idris ()
updateContext (Name -> RigCount -> Context -> Context
setRigCount Name
n (Context -> RigCount -> [Name] -> RigCount
minRig Context
ctxt RigCount
rig [Name]
calls))
                                 IBCWrite -> Idris ()
addIBC (Name -> IBCWrite
IBCCG Name
n)
                          [Def]
_ -> () -> Idris ()
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
                      () -> Idris ()
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  --                         addIBC (IBCTotal n tot)
               Maybe Term
_ -> () -> Idris ()
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
           -- Check it's covering, if 'covering' option is used. Chase
           -- all called functions, and fail if any of them are also
           -- 'Partial NotCovering'
           Bool -> Idris () -> Idris ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (FnOpt
CoveringFn FnOpt -> FnOpts -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` FnOpts
opts) (Idris () -> Idris ()) -> Idris () -> Idris ()
forall a b. (a -> b) -> a -> b
$ FC -> [Name] -> Name -> Name -> Idris ()
checkAllCovering FC
fc [] Name
n Name
n
           -- Add the 'AllGuarded' flag if it's guaranteed that every
           -- 'Inf' argument will be guarded by constructors in the result
           -- (allows productivity check to go under this function)
           Name -> Idris ()
checkIfGuarded Name
n
           -- If this has %static arguments, cache the names of functions
           -- it calls for partial evaluation later
           IState
ist <- Idris IState
getIState
           let statics :: [Bool]
statics = case Name -> Ctxt [Bool] -> Maybe [Bool]
forall a. Name -> Ctxt a -> Maybe a
lookupCtxtExact Name
n (IState -> Ctxt [Bool]
idris_statics IState
ist) of
                              Just [Bool]
ns -> [Bool]
ns
                              Maybe [Bool]
Nothing -> []
           Bool -> Idris () -> Idris ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or [Bool]
statics) (Idris () -> Idris ()) -> Idris () -> Idris ()
forall a b. (a -> b) -> a -> b
$ do Name -> Idris [Name]
getAllNames Name
n
                                  () -> Idris ()
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

  where
    checkUndefined :: Name -> Context -> Idris ()
checkUndefined Name
n Context
ctxt = 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
_ Term
_] -> () -> Idris ()
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
                                 [Def]
_ -> 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 (Name -> Err
forall t. Name -> Err' t
AlreadyDefined Name
n))

    debind :: Either (TT n) (TT n, TT n) -> ([(n, TT n)], TT n, TT n)
debind (Right (TT n
x, TT n
y)) = let ([(n, TT n)]
vs, TT n
x') = [(n, TT n)] -> TT n -> ([(n, TT n)], TT n)
forall {n}. [(n, TT n)] -> TT n -> ([(n, TT n)], TT n)
depat [] TT n
x
                                ([(n, TT n)]
_, TT n
y') = [(n, TT n)] -> TT n -> ([(n, TT n)], TT n)
forall {n}. [(n, TT n)] -> TT n -> ([(n, TT n)], TT n)
depat [] TT n
y in
                                ([(n, TT n)]
vs, TT n
x', TT n
y')
    debind (Left TT n
x)       = let ([(n, TT n)]
vs, TT n
x') = [(n, TT n)] -> TT n -> ([(n, TT n)], TT n)
forall {n}. [(n, TT n)] -> TT n -> ([(n, TT n)], TT n)
depat [] TT n
x in
                                ([(n, TT n)]
vs, TT n
x', TT n
forall n. TT n
Impossible)

    depat :: [(n, TT n)] -> TT n -> ([(n, TT n)], TT n)
depat [(n, TT n)]
acc (Bind n
n (PVar RigCount
rig TT n
t) TT n
sc) = [(n, TT n)] -> TT n -> ([(n, TT n)], TT n)
depat ((n
n, TT n
t) (n, TT n) -> [(n, TT n)] -> [(n, TT n)]
forall a. a -> [a] -> [a]
: [(n, TT n)]
acc) (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
n TT n
t) TT n
sc)
    depat [(n, TT n)]
acc TT n
x = ([(n, TT n)]
acc, TT n
x)


    getPVs :: TT a -> ([a], TT a)
getPVs (Bind a
x (PVar RigCount
rig TT a
_) TT a
tm) = let ([a]
vs, TT a
tm') = TT a -> ([a], TT a)
getPVs TT a
tm
                                          in (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
vs, TT a
tm')
    getPVs TT a
tm = ([], TT a
tm)

    isPatVar :: t a -> TT a -> Bool
isPatVar t a
vs (P NameType
Bound a
n TT a
_) = a
n a -> t a -> Bool
forall a. Eq a => a -> t a -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` t a
vs
    isPatVar t a
_ TT a
_ = Bool
False

    hasDefault :: [Either a (TT a, b)] -> Bool
hasDefault [Either a (TT a, b)]
cs | (Right (TT a
lhs, b
rhs) : [Either a (TT a, b)]
_) <- [Either a (TT a, b)] -> [Either a (TT a, b)]
forall a. [a] -> [a]
reverse [Either a (TT a, b)]
cs
                  , ([a]
pvs, TT a
tm) <- TT a -> ([a], TT a)
forall {a}. TT a -> ([a], TT a)
getPVs (TT a -> TT a
forall n. TT n -> TT n
explicitNames TT a
lhs)
                  , (TT a
f, [TT a]
args) <- TT a -> (TT a, [TT a])
forall n. TT n -> (TT n, [TT n])
unApply TT a
tm = (TT a -> Bool) -> [TT a] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ([a] -> TT a -> Bool
forall {t :: * -> *} {a}. (Foldable t, Eq a) => t a -> TT a -> Bool
isPatVar [a]
pvs) [TT a]
args
    hasDefault [Either a (TT a, b)]
_ = Bool
False

    getLHS :: (a, b, c) -> b
getLHS (a
_, b
l, c
_) = b
l

    -- Simplify the left hand side of a definition, to remove any lets
    -- that may have arisen during elaboration
    simple_lhs :: Context -> Either a (Term, b) -> Either a (Term, b)
simple_lhs Context
ctxt (Right (Term
x, b
y))
        = (Term, b) -> Either a (Term, b)
forall a b. b -> Either a b
Right (Context -> Env -> Term -> Term
Idris.Core.Evaluate.simplify Context
ctxt [] Term
x, b
y)
    simple_lhs Context
ctxt Either a (Term, b)
t = Either a (Term, b)
t

    force_lhs :: Ctxt OptInfo -> Either a (Term, b) -> Either a (Term, b)
force_lhs Ctxt OptInfo
opts (Right (Term
x, b
y)) = (Term, b) -> Either a (Term, b)
forall a b. b -> Either a b
Right (Ctxt OptInfo -> Term -> Term
forceWith Ctxt OptInfo
opts Term
x, b
y)
    force_lhs Ctxt OptInfo
opts Either a (Term, b)
t = Either a (Term, b)
t

    simple_rt :: Context -> ([Name], b, Term) -> ([Name], b, Term)
simple_rt Context
ctxt ([Name]
p, b
x, Term
y) = ([Name]
p, b
x, Term -> Term
forall a. NFData a => a -> a
force ([Name] -> Term -> Term
uniqueBinders [Name]
p
                                                (Context -> Env -> Term -> Term
rt_simplify Context
ctxt [] Term
y)))

    specNames :: FnOpts -> Maybe [(Name, Maybe Int)]
specNames [] = Maybe [(Name, Maybe Int)]
forall a. Maybe a
Nothing
    specNames (Specialise [(Name, Maybe Int)]
ns : FnOpts
_) = [(Name, Maybe Int)] -> Maybe [(Name, Maybe Int)]
forall a. a -> Maybe a
Just [(Name, Maybe Int)]
ns
    specNames (FnOpt
_ : FnOpts
xs) = FnOpts -> Maybe [(Name, Maybe Int)]
specNames FnOpts
xs

    sameLength :: [(a, TT n, c)] -> TC Int
sameLength ((a
_, TT n
x, c
_) : [(a, TT n, c)]
xs)
        = do Int
l <- [(a, TT n, c)] -> TC Int
sameLength [(a, TT n, c)]
xs
             let (TT n
_, [TT n]
as) = TT n -> (TT n, [TT n])
forall n. TT n -> (TT n, [TT n])
unApply TT n
x
             if ([(a, TT n, c)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(a, TT n, c)]
xs Bool -> Bool -> Bool
|| Int
l Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== [TT n] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TT n]
as) then Int -> TC Int
forall a. a -> TC a
forall (m :: * -> *) a. Monad m => a -> m a
return ([TT n] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TT n]
as)
                else Err -> TC Int
forall a. Err -> TC a
tfail (FC -> Err -> Err
forall t. FC -> Err' t -> Err' t
At FC
fc (String -> Err
forall t. String -> Err' t
Msg String
"Clauses have differing numbers of arguments "))
    sameLength [] = Int -> TC Int
forall a. a -> TC a
forall (m :: * -> *) a. Monad m => a -> m a
return Int
0

    -- Partially evaluate, if the definition is marked as specialisable
    doPartialEval :: IState
-> [Either Term (Term, Term)]
-> StateT IState (ExceptT Err IO) [Either Term (Term, Term)]
doPartialEval IState
ist [Either Term (Term, Term)]
pats =
           case FnOpts -> Maybe [(Name, Maybe Int)]
specNames FnOpts
opts of
                Maybe [(Name, Maybe Int)]
Nothing -> [Either Term (Term, Term)]
-> StateT IState (ExceptT Err IO) [Either Term (Term, Term)]
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return [Either Term (Term, Term)]
pats
                Just [(Name, Maybe Int)]
ns -> case Context
-> [(Name, Maybe Int)]
-> [Either Term (Term, Term)]
-> Maybe [Either Term (Term, Term)]
partial_eval (IState -> Context
tt_ctxt IState
ist) [(Name, Maybe Int)]
ns [Either Term (Term, Term)]
pats of
                                Just [Either Term (Term, Term)]
t -> [Either Term (Term, Term)]
-> StateT IState (ExceptT Err IO) [Either Term (Term, Term)]
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return [Either Term (Term, Term)]
t
                                Maybe [Either Term (Term, Term)]
Nothing -> Err -> StateT IState (ExceptT Err IO) [Either Term (Term, Term)]
forall a. Err -> Idris a
ierror (FC -> Err -> Err
forall t. FC -> Err' t -> Err' t
At FC
fc (String -> Err
forall t. String -> Err' t
Msg String
"No specialisation achieved"))

    minRig :: Context -> RigCount -> [Name] -> RigCount
    minRig :: Context -> RigCount -> [Name] -> RigCount
minRig Context
c RigCount
minr [] = RigCount
minr
    minRig Context
c RigCount
minr (Name
r : [Name]
rs) = case Name -> Context -> Maybe RigCount
lookupRigCountExact Name
r Context
c of
                                  Maybe RigCount
Nothing -> Context -> RigCount -> [Name] -> RigCount
minRig Context
c RigCount
minr [Name]
rs
                                  Just RigCount
rc -> Context -> RigCount -> [Name] -> RigCount
minRig Context
c (RigCount -> RigCount -> RigCount
forall a. Ord a => a -> a -> a
min RigCount
minr RigCount
rc) [Name]
rs

forceWith :: Ctxt OptInfo -> Term -> Term
forceWith :: Ctxt OptInfo -> Term -> Term
forceWith Ctxt OptInfo
opts Term
lhs = -- trace (show lhs ++ "\n==>\n" ++ show (force lhs) ++ "\n----") $
                      Term -> Term
force Term
lhs
  where
    -- If there's forced arguments, erase them
    force :: Term -> Term
force ap :: Term
ap@(App AppStatus Name
_ Term
_ Term
_)
       | (fn :: Term
fn@(P NameType
_ Name
c Term
_), [Term]
args) <- Term -> (Term, [Term])
forall n. TT n -> (TT n, [TT n])
unApply Term
ap,
         Just OptInfo
copt <- Name -> Ctxt OptInfo -> Maybe OptInfo
forall a. Name -> Ctxt a -> Maybe a
lookupCtxtExact Name
c Ctxt OptInfo
opts
            = let args' :: [Term]
args' = Int -> [Int] -> [Term] -> [Term]
forall {t :: * -> *} {t} {n}.
(Foldable t, Eq t, Num t) =>
t -> t t -> [TT n] -> [TT n]
eraseArg Int
0 (OptInfo -> [Int]
forceable OptInfo
copt) [Term]
args in
                  Term -> [Term] -> Term
forall n. TT n -> [TT n] -> TT n
mkApp Term
fn ((Term -> Term) -> [Term] -> [Term]
forall a b. (a -> b) -> [a] -> [b]
map Term -> Term
force [Term]
args')
    force (App AppStatus Name
t Term
f Term
a)
       = AppStatus Name -> Term -> Term -> Term
forall n. AppStatus n -> TT n -> TT n -> TT n
App AppStatus Name
t (Term -> Term
force Term
f) (Term -> Term
force Term
a)
    -- We might have pat bindings, so go under them
    force (Bind Name
n Binder Term
b Term
sc) = Name -> Binder Term -> Term -> Term
forall n. n -> Binder (TT n) -> TT n -> TT n
Bind Name
n Binder Term
b (Term -> Term
force Term
sc)
    -- Everything else, leave it alone
    force Term
t = Term
t

    eraseArg :: t -> t t -> [TT n] -> [TT n]
eraseArg t
i t t
fs (TT n
n : [TT n]
ns) | t
i t -> t t -> Bool
forall a. Eq a => a -> t a -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` t t
fs = TT n
forall n. TT n
Erased TT n -> [TT n] -> [TT n]
forall a. a -> [a] -> [a]
: t -> t t -> [TT n] -> [TT n]
eraseArg (t
i t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) t t
fs [TT n]
ns
                           | Bool
otherwise = TT n
n TT n -> [TT n] -> [TT n]
forall a. a -> [a] -> [a]
: t -> t t -> [TT n] -> [TT n]
eraseArg (t
i t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) t t
fs [TT n]
ns
    eraseArg t
i t t
_ [] = []


-- | Find 'static' applications in a term and partially evaluate them.
-- Return any new transformation rules
elabPE :: ElabInfo -> FC -> Name -> Term -> Idris [(Term, Term)]
-- Don't go deeper than 5 nested partially evaluated definitions in one go
-- (make this configurable? It's a good limit for most cases, certainly for
-- interfaces and polymorphic definitions, but maybe not for DSLs and
-- interpreters in complicated cases.
-- Possibly only worry about the limit if we've specialised the same function
-- a number of times in one go.)
elabPE :: ElabInfo
-> FC
-> Name
-> Term
-> StateT IState (ExceptT Err IO) [(Term, Term)]
elabPE ElabInfo
info FC
fc Name
caller Term
r | ElabInfo -> Int
pe_depth ElabInfo
info Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
5 = [(Term, Term)] -> StateT IState (ExceptT Err IO) [(Term, Term)]
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return []
elabPE ElabInfo
info FC
fc Name
caller Term
r =
  do IState
ist <- Idris IState
getIState
     let sa :: [(Name, [(PEArgType, Term)])]
sa = ((Name, [(PEArgType, Term)]) -> Bool)
-> [(Name, [(PEArgType, Term)])] -> [(Name, [(PEArgType, Term)])]
forall a. (a -> Bool) -> [a] -> [a]
filter (\(Name, [(PEArgType, Term)])
ap -> (Name, [(PEArgType, Term)]) -> Name
forall a b. (a, b) -> a
fst (Name, [(PEArgType, Term)])
ap Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
/= Name
caller) ([(Name, [(PEArgType, Term)])] -> [(Name, [(PEArgType, Term)])])
-> [(Name, [(PEArgType, Term)])] -> [(Name, [(PEArgType, Term)])]
forall a b. (a -> b) -> a -> b
$ IState -> [Name] -> Term -> [(Name, [(PEArgType, Term)])]
getSpecApps IState
ist [] Term
r
     [[(Term, Term)]]
rules <- ((Name, [(PEArgType, Term)])
 -> StateT IState (ExceptT Err IO) [(Term, Term)])
-> [(Name, [(PEArgType, Term)])]
-> StateT IState (ExceptT Err IO) [[(Term, Term)]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Name, [(PEArgType, Term)])
-> StateT IState (ExceptT Err IO) [(Term, Term)]
mkSpecialised [(Name, [(PEArgType, Term)])]
sa
     [(Term, Term)] -> StateT IState (ExceptT Err IO) [(Term, Term)]
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ([(Term, Term)] -> StateT IState (ExceptT Err IO) [(Term, Term)])
-> [(Term, Term)] -> StateT IState (ExceptT Err IO) [(Term, Term)]
forall a b. (a -> b) -> a -> b
$ [[(Term, Term)]] -> [(Term, Term)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[(Term, Term)]]
rules
  where
    -- Make a specialised version of the application, and
    -- add a PTerm level transformation rule, which is basically the
    -- new definition in reverse (before specialising it).
    -- RHS => LHS where implicit arguments are left blank in the
    -- transformation.

    -- Transformation rules are applied after every PClause elaboration

    mkSpecialised :: (Name, [(PEArgType, Term)]) -> Idris [(Term, Term)]
    mkSpecialised :: (Name, [(PEArgType, Term)])
-> StateT IState (ExceptT Err IO) [(Term, Term)]
mkSpecialised (Name, [(PEArgType, Term)])
specapp_in = do
        IState
ist <- Idris IState
getIState
        Context
ctxt <- Idris Context
getContext
        (PTerm
specTy, (Name, [(PEArgType, Term)])
specapp) <- IState
-> (Name, [(PEArgType, Term)])
-> StateT
     IState (ExceptT Err IO) (PTerm, (Name, [(PEArgType, Term)]))
getSpecTy IState
ist (Name, [(PEArgType, Term)])
specapp_in
        let (Name
n, Name
newnm, PEDecl
specdecl) = IState
-> (Name, [(PEArgType, Term)]) -> PTerm -> (Name, Name, PEDecl)
getSpecClause IState
ist (Name, [(PEArgType, Term)])
specapp PTerm
specTy
        let lhs :: PTerm
lhs = PEDecl -> PTerm
pe_app PEDecl
specdecl
        let rhs :: PTerm
rhs = PEDecl -> PTerm
pe_def PEDecl
specdecl
        let undef :: Bool
undef = case Name -> Context -> Maybe Def
lookupDefExact Name
newnm Context
ctxt of
                         Maybe Def
Nothing -> Bool
True
                         Maybe Def
_ -> Bool
False
        Int -> String -> Idris ()
logElab Int
5 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ (Name, Bool, [Bool]) -> String
forall a. Show a => a -> String
show (Name
newnm, Bool
undef, ((PEArgType, Term) -> Bool) -> [(PEArgType, Term)] -> [Bool]
forall a b. (a -> b) -> [a] -> [b]
map (IState -> (PEArgType, Term) -> Bool
concreteArg IState
ist) ((Name, [(PEArgType, Term)]) -> [(PEArgType, Term)]
forall a b. (a, b) -> b
snd (Name, [(PEArgType, Term)])
specapp))
        StateT IState (ExceptT Err IO) [(Term, Term)]
-> (Err -> StateT IState (ExceptT Err IO) [(Term, Term)])
-> StateT IState (ExceptT Err IO) [(Term, Term)]
forall a. Idris a -> (Err -> Idris a) -> Idris a
idrisCatch
          (if (Bool
undef Bool -> Bool -> Bool
&& ((PEArgType, Term) -> Bool) -> [(PEArgType, Term)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (IState -> (PEArgType, Term) -> Bool
concreteArg IState
ist) ((Name, [(PEArgType, Term)]) -> [(PEArgType, Term)]
forall a b. (a, b) -> b
snd (Name, [(PEArgType, Term)])
specapp)) then do
                [Name]
cgns <- Name -> Idris [Name]
getAllNames Name
n
                -- on the RHS of the new definition, we should reduce
                -- everything that's not itself static (because we'll
                -- want to be a PE version of those next)
                let cgns' :: [Name]
cgns' = (Name -> Bool) -> [Name] -> [Name]
forall a. (a -> Bool) -> [a] -> [a]
filter (\Name
x -> Name
x Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
/= Name
n Bool -> Bool -> Bool
&&
                                          IState -> Name -> Bool
notStatic IState
ist Name
x) [Name]
cgns
                -- set small reduction limit on partial/productive things
                let maxred :: Int
maxred = case Name -> Context -> [Totality]
lookupTotal Name
n Context
ctxt of
                                  [Total [Int]
_] -> Int
65536
                                  [Totality
Productive] -> Int
16
                                  [Totality]
_ -> Int
1
                let specnames :: [(Name, Maybe Int)]
specnames = ((PEArgType, Term) -> Maybe (Name, Maybe Int))
-> [(PEArgType, Term)] -> [(Name, Maybe Int)]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Bool -> (PEArgType, Term) -> Maybe (Name, Maybe Int)
forall {a} {n}.
Num a =>
Bool -> (PEArgType, TT n) -> Maybe (n, Maybe a)
specName (PEDecl -> Bool
pe_simple PEDecl
specdecl))
                                         ((Name, [(PEArgType, Term)]) -> [(PEArgType, Term)]
forall a b. (a, b) -> b
snd (Name, [(PEArgType, Term)])
specapp)
                [[(Name, Maybe Int)]]
descs <- (Name -> StateT IState (ExceptT Err IO) [(Name, Maybe Int)])
-> [Name] -> StateT IState (ExceptT Err IO) [[(Name, Maybe Int)]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Name -> StateT IState (ExceptT Err IO) [(Name, Maybe Int)]
getStaticsFrom (((Name, Maybe Int) -> Name) -> [(Name, Maybe Int)] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name, Maybe Int) -> Name
forall a b. (a, b) -> a
fst [(Name, Maybe Int)]
specnames)

                let opts :: FnOpts
opts = [[(Name, Maybe Int)] -> FnOpt
Specialise ((if PEDecl -> Bool
pe_simple PEDecl
specdecl
                                            then (Name -> (Name, Maybe Int)) -> [Name] -> [(Name, Maybe Int)]
forall a b. (a -> b) -> [a] -> [b]
map (\Name
x -> (Name
x, Maybe Int
forall a. Maybe a
Nothing)) [Name]
cgns'
                                            else []) [(Name, Maybe Int)] -> [(Name, Maybe Int)] -> [(Name, Maybe Int)]
forall a. [a] -> [a] -> [a]
++
                                         (Name
n, Int -> Maybe Int
forall a. a -> Maybe a
Just Int
maxred) (Name, Maybe Int) -> [(Name, Maybe Int)] -> [(Name, Maybe Int)]
forall a. a -> [a] -> [a]
: [(Name, Maybe Int)]
specnames [(Name, Maybe Int)] -> [(Name, Maybe Int)] -> [(Name, Maybe Int)]
forall a. [a] -> [a] -> [a]
++
                                             [[(Name, Maybe Int)]] -> [(Name, Maybe Int)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[(Name, Maybe Int)]]
descs)]
                Int -> String -> Idris ()
logElab Int
3 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"Specialising application: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ (Name, [(PEArgType, Term)]) -> String
forall a. Show a => a -> String
show (Name, [(PEArgType, Term)])
specapp
                              String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n in \n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Name -> String
forall a. Show a => a -> String
show Name
caller String -> String -> String
forall a. [a] -> [a] -> [a]
++
                              String
"\n with \n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ FnOpts -> String
forall a. Show a => a -> String
show FnOpts
opts
                              String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\nCalling: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [Name] -> String
forall a. Show a => a -> String
show [Name]
cgns
                Int -> String -> Idris ()
logElab Int
3 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"New name: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Name -> String
forall a. Show a => a -> String
show Name
newnm
                Int -> String -> Idris ()
logElab Int
3 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"PE definition type : " String -> String -> String
forall a. [a] -> [a] -> [a]
++ (PTerm -> String
forall a. Show a => a -> String
show PTerm
specTy)
                            String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ FnOpts -> String
forall a. Show a => a -> String
show FnOpts
opts
                Int -> String -> Idris ()
logElab Int
2 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"PE definition " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Name -> String
forall a. Show a => a -> String
show Name
newnm String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
":\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++
                             String -> [String] -> String
showSep String
"\n"
                                (((PTerm, PTerm) -> String) -> [(PTerm, PTerm)] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (\ (PTerm
lhs, PTerm
rhs) ->
                                  (PTerm -> String
showTmImpls PTerm
lhs String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" = " String -> String -> String
forall a. [a] -> [a] -> [a]
++
                                   PTerm -> String
showTmImpls PTerm
rhs)) (PEDecl -> [(PTerm, PTerm)]
pe_clauses PEDecl
specdecl))

                Int -> String -> Idris ()
logElab Int
5 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ Name -> String
forall a. Show a => a -> String
show Name
n String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" transformation rule: " String -> String -> String
forall a. [a] -> [a] -> [a]
++
                           PTerm -> String
showTmImpls PTerm
rhs String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" ==> " String -> String -> String
forall a. [a] -> [a] -> [a]
++ PTerm -> String
showTmImpls PTerm
lhs

                ElabInfo
-> SyntaxInfo
-> Docstring (Either Err PTerm)
-> [(Name, Docstring (Either Err PTerm))]
-> FC
-> FnOpts
-> Name
-> FC
-> PTerm
-> StateT IState (ExceptT Err IO) Term
elabType ElabInfo
info SyntaxInfo
defaultSyntax Docstring (Either Err PTerm)
forall a. Docstring a
emptyDocstring [] FC
fc FnOpts
opts Name
newnm FC
NoFC PTerm
specTy
                let def :: [PClause]
def = ((PTerm, PTerm) -> PClause) -> [(PTerm, PTerm)] -> [PClause]
forall a b. (a -> b) -> [a] -> [b]
map (\(PTerm
lhs, PTerm
rhs) ->
                                 let lhs' :: PTerm
lhs' = (PTerm -> PTerm) -> PTerm -> PTerm
mapPT PTerm -> PTerm
hiddenToPH (PTerm -> PTerm) -> PTerm -> PTerm
forall a b. (a -> b) -> a -> b
$ IState -> PTerm -> PTerm
stripUnmatchable IState
ist PTerm
lhs in
                                  FC
-> Name -> PTerm -> [PTerm] -> PTerm -> [PDecl' PTerm] -> PClause
forall t. FC -> Name -> t -> [t] -> t -> [PDecl' t] -> PClause' t
PClause FC
fc Name
newnm PTerm
lhs' [] PTerm
rhs [])
                              (PEDecl -> [(PTerm, PTerm)]
pe_clauses PEDecl
specdecl)
                (Term, Term)
trans <- ElabInfo -> FC -> Bool -> PTerm -> PTerm -> Idris (Term, Term)
elabTransform ElabInfo
info FC
fc Bool
False PTerm
rhs PTerm
lhs
                ElabInfo -> FC -> FnOpts -> Name -> [PClause] -> Idris ()
elabClauses (ElabInfo
info {pe_depth = pe_depth info + 1}) FC
fc
                            (FnOpt
PEGeneratedFnOpt -> FnOpts -> FnOpts
forall a. a -> [a] -> [a]
:FnOpts
opts) Name
newnm [PClause]
def
                [(Term, Term)] -> StateT IState (ExceptT Err IO) [(Term, Term)]
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return [(Term, Term)
trans]
             else [(Term, Term)] -> StateT IState (ExceptT Err IO) [(Term, Term)]
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return [])
          -- if it doesn't work, just don't specialise. Could happen for lots
          -- of valid reasons (e.g. local variables in scope which can't be
          -- lifted out).
          (\Err
e -> do Int -> String -> Idris ()
logElab Int
5 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"Couldn't specialise: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ (IState -> Err -> String
pshow IState
ist Err
e)
                    [(Term, Term)] -> StateT IState (ExceptT Err IO) [(Term, Term)]
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return [])

    hiddenToPH :: PTerm -> PTerm
hiddenToPH (PHidden PTerm
_) = PTerm
Placeholder
    hiddenToPH PTerm
x = PTerm
x

    specName :: Bool -> (PEArgType, TT n) -> Maybe (n, Maybe a)
specName Bool
simpl (ImplicitS Name
_, TT n
tm)
        | (P NameType
Ref n
n TT n
_, [TT n]
_) <- TT n -> (TT n, [TT n])
forall n. TT n -> (TT n, [TT n])
unApply TT n
tm = (n, Maybe a) -> Maybe (n, Maybe a)
forall a. a -> Maybe a
Just (n
n, a -> Maybe a
forall a. a -> Maybe a
Just (if Bool
simpl then a
1 else a
0))
    specName Bool
simpl (PEArgType
ExplicitS, TT n
tm)
        | (P NameType
Ref n
n TT n
_, [TT n]
_) <- TT n -> (TT n, [TT n])
forall n. TT n -> (TT n, [TT n])
unApply TT n
tm = (n, Maybe a) -> Maybe (n, Maybe a)
forall a. a -> Maybe a
Just (n
n, a -> Maybe a
forall a. a -> Maybe a
Just (if Bool
simpl then a
1 else a
0))
    specName Bool
simpl (PEArgType
ConstraintS, TT n
tm)
        | (P NameType
Ref n
n TT n
_, [TT n]
_) <- TT n -> (TT n, [TT n])
forall n. TT n -> (TT n, [TT n])
unApply TT n
tm = (n, Maybe a) -> Maybe (n, Maybe a)
forall a. a -> Maybe a
Just (n
n, a -> Maybe a
forall a. a -> Maybe a
Just (if Bool
simpl then a
1 else a
0))
    specName Bool
simpl (PEArgType, TT n)
_ = Maybe (n, Maybe a)
forall a. Maybe a
Nothing

    -- get the descendants of the name 'n' which are marked %static
    -- Marking a function %static essentially means it's used to construct
    -- programs, so should be evaluated by the partial evaluator
    getStaticsFrom :: Name -> Idris [(Name, Maybe Int)]
    getStaticsFrom :: Name -> StateT IState (ExceptT Err IO) [(Name, Maybe Int)]
getStaticsFrom Name
n = do [Name]
ns <- Name -> Idris [Name]
getAllNames Name
n
                          IState
i <- Idris IState
getIState
                          let statics :: [Name]
statics = (Name -> Bool) -> [Name] -> [Name]
forall a. (a -> Bool) -> [a] -> [a]
filter (IState -> Name -> Bool
staticFn IState
i) [Name]
ns
                          [(Name, Maybe Int)]
-> StateT IState (ExceptT Err IO) [(Name, Maybe Int)]
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ((Name -> (Name, Maybe Int)) -> [Name] -> [(Name, Maybe Int)]
forall a b. (a -> b) -> [a] -> [b]
map (\Name
n -> (Name
n, Maybe Int
forall a. Maybe a
Nothing)) [Name]
statics)

    staticFn :: IState -> Name -> Bool
    staticFn :: IState -> Name -> Bool
staticFn IState
i Name
n =  case Name -> Ctxt FnOpts -> [FnOpts]
forall a. Name -> Ctxt a -> [a]
lookupCtxt Name
n (IState -> Ctxt FnOpts
idris_flags IState
i) of
                            [FnOpts
opts] -> FnOpt -> FnOpts -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem FnOpt
StaticFn FnOpts
opts
                            [FnOpts]
_ -> Bool
False

    notStatic :: IState -> Name -> Bool
notStatic IState
ist Name
n = case Name -> Ctxt [Bool] -> Maybe [Bool]
forall a. Name -> Ctxt a -> Maybe a
lookupCtxtExact Name
n (IState -> Ctxt [Bool]
idris_statics IState
ist) of
                           Just [Bool]
s -> Bool -> Bool
not ([Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or [Bool]
s)
                           Maybe [Bool]
_ -> Bool
True

    concreteArg :: IState -> (PEArgType, Term) -> Bool
concreteArg IState
ist (ImplicitS Name
_, Term
tm) = IState -> Term -> Bool
concreteTm IState
ist Term
tm
    concreteArg IState
ist (PEArgType
ExplicitS, Term
tm) = IState -> Term -> Bool
concreteTm IState
ist Term
tm
    concreteArg IState
ist (PEArgType, Term)
_ = Bool
True

    concreteTm :: IState -> Term -> Bool
concreteTm IState
ist Term
tm | (P NameType
_ Name
n Term
_, [Term]
_) <- Term -> (Term, [Term])
forall n. TT n -> (TT n, [TT n])
unApply Term
tm =
        case Name -> Context -> [Term]
lookupTy Name
n (IState -> Context
tt_ctxt IState
ist) of
             [] -> Bool
False
             [Term]
_ -> Bool
True
    concreteTm IState
ist (Constant Const
_) = Bool
True
    concreteTm IState
ist (Bind Name
n (Lam RigCount
_ Term
_) Term
sc) = Bool
True
    concreteTm IState
ist (Bind Name
n (Pi RigCount
_ Maybe ImplicitInfo
_ Term
_ Term
_) Term
sc) = Bool
True
    concreteTm IState
ist (Bind Name
n (Let RigCount
_ Term
_ Term
_) Term
sc) = IState -> Term -> Bool
concreteTm IState
ist Term
sc
    concreteTm IState
ist Term
_ = Bool
False

    -- get the type of a specialised application
    getSpecTy :: IState
-> (Name, [(PEArgType, Term)])
-> StateT
     IState (ExceptT Err IO) (PTerm, (Name, [(PEArgType, Term)]))
getSpecTy IState
ist (Name
n, [(PEArgType, Term)]
args)
       = case Name -> Context -> [Term]
lookupTy Name
n (IState -> Context
tt_ctxt IState
ist) of
              [Term
ty] -> let (Term
specty_in, [(PEArgType, Term)]
args') = [(PEArgType, Term)] -> Term -> (Term, [(PEArgType, Term)])
specType [(PEArgType, Term)]
args (Term -> Term
forall n. TT n -> TT n
explicitNames Term
ty)
                          specty :: Term
specty = Context -> Env -> Term -> Term
normalise (IState -> Context
tt_ctxt IState
ist) [] (Term -> Term
forall n. Eq n => TT n -> TT n
finalise Term
specty_in)
                          t :: PTerm
t = IState -> [(PEArgType, Term)] -> Term -> PTerm
mkPE_TyDecl IState
ist [(PEArgType, Term)]
args' (Term -> Term
forall n. TT n -> TT n
explicitNames Term
specty) in
                          (PTerm, (Name, [(PEArgType, Term)]))
-> StateT
     IState (ExceptT Err IO) (PTerm, (Name, [(PEArgType, Term)]))
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return (PTerm
t, (Name
n, [(PEArgType, Term)]
args'))
--                             (normalise (tt_ctxt ist) [] (specType args ty))
              [Term]
_ -> String
-> StateT
     IState (ExceptT Err IO) (PTerm, (Name, [(PEArgType, Term)]))
forall a. String -> Idris a
ifail (String
 -> StateT
      IState (ExceptT Err IO) (PTerm, (Name, [(PEArgType, Term)])))
-> String
-> StateT
     IState (ExceptT Err IO) (PTerm, (Name, [(PEArgType, Term)]))
forall a b. (a -> b) -> a -> b
$ String
"Ambiguous name " 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
" (getSpecTy)"

    -- get the clause of a specialised application
    getSpecClause :: IState
-> (Name, [(PEArgType, Term)]) -> PTerm -> (Name, Name, PEDecl)
getSpecClause IState
ist (Name
n, [(PEArgType, Term)]
args) PTerm
specTy
       = let newnm :: Name
newnm = String -> Name
sUN (String
"PE_" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Name -> String
forall a. Show a => a -> String
show (Name -> Name
nsroot Name
n) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"_" String -> String -> String
forall a. [a] -> [a] -> [a]
++
                               Word64 -> String -> String
qhash Word64
5381 (String -> [String] -> String
showSep String
"_" (((PEArgType, Term) -> String) -> [(PEArgType, Term)] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (PEArgType, Term) -> String
forall {a}. Show a => (PEArgType, TT a) -> String
showArg [(PEArgType, Term)]
args))) in
                               -- UN (show n ++ show (map snd args)) in
             (Name
n, Name
newnm, IState -> Name -> Name -> PTerm -> [(PEArgType, Term)] -> PEDecl
mkPE_TermDecl IState
ist Name
newnm Name
n PTerm
specTy [(PEArgType, Term)]
args)
      where showArg :: (PEArgType, TT a) -> String
showArg (PEArgType
ExplicitS, TT a
n) = TT a -> String
forall {a}. Show a => TT a -> String
qshow TT a
n
            showArg (ImplicitS Name
_, TT a
n) = TT a -> String
forall {a}. Show a => TT a -> String
qshow TT a
n
            showArg (PEArgType, TT a)
_ = String
""

            qshow :: TT a -> String
qshow (Bind a
_ Binder (TT a)
_ TT a
_) = String
"fn"
            qshow (App AppStatus a
_ TT a
f TT a
a) = TT a -> String
qshow TT a
f String -> String -> String
forall a. [a] -> [a] -> [a]
++ TT a -> String
qshow TT a
a
            qshow (P NameType
_ a
n TT a
_) = a -> String
forall a. Show a => a -> String
show a
n
            qshow (Constant Const
c) = Const -> String
forall a. Show a => a -> String
show Const
c
            qshow TT a
_ = String
""

            -- Simple but effective string hashing...
            -- Keep it to 32 bits for readability/debuggability
            qhash :: Word64 -> String -> String
            qhash :: Word64 -> String -> String
qhash Word64
hash [] = Word64 -> String -> String
forall a. Integral a => a -> String -> String
showHex (Word64 -> Word64
forall a. Num a => a -> a
abs Word64
hash Word64 -> Word64 -> Word64
forall a. Integral a => a -> a -> a
`mod` Word64
0xffffffff) String
""
            qhash Word64
hash (Char
x:String
xs) = Word64 -> String -> String
qhash (Word64
hash Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
* Word64
33 Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+ Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral(Char -> Int
forall a. Enum a => a -> Int
fromEnum Char
x)) String
xs

-- | Checks if the clause is a possible left hand side.
-- NOTE: A lot of this is repeated for reflected definitions in Idris.Elab.Term
-- One day, these should be merged, but until then remember that if you edit
-- this you might need to edit the other version...
checkPossible :: ElabInfo -> FC -> Bool -> Name -> PTerm -> Idris (Maybe PTerm)
checkPossible :: ElabInfo -> FC -> Bool -> Name -> PTerm -> Idris (Maybe PTerm)
checkPossible ElabInfo
info FC
fc Bool
tcgen Name
fname PTerm
lhs_in
   = do Context
ctxt <- Idris Context
getContext
        IState
i <- Idris IState
getIState
        let lhs :: PTerm
lhs = IState -> PTerm -> PTerm
addImplPat IState
i PTerm
lhs_in
        Int -> String -> Idris ()
logElab Int
10 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"Trying missing case: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ PTerm -> String
showTmImpls PTerm
lhs
        -- if the LHS type checks, it is possible
        case String
-> Context
-> Ctxt TypeInfo
-> Int
-> Name
-> Term
-> EState
-> Elab' EState ElabResult
-> TC (ElabResult, String)
forall aux a.
String
-> Context
-> Ctxt TypeInfo
-> Int
-> Name
-> Term
-> aux
-> Elab' aux a
-> TC (a, String)
elaborate (ElabInfo -> String
constraintNS ElabInfo
info) Context
ctxt (IState -> Ctxt TypeInfo
idris_datatypes IState
i) (IState -> Int
idris_name IState
i) (Int -> String -> Name
sMN Int
0 String
"patLHS") Term
infP EState
initEState
                            (FC -> Elab' EState ElabResult -> Elab' EState ElabResult
forall aux a. FC -> Elab' aux a -> Elab' aux a
erun FC
fc (IState
-> ElabInfo
-> ElabMode
-> FnOpts
-> Name
-> [Name]
-> PTerm
-> Elab' EState ElabResult
buildTC IState
i ElabInfo
info ElabMode
EImpossible [] Name
fname
                                                (PTerm -> [Name]
allNamesIn PTerm
lhs_in)
                                                (PTerm -> PTerm
infTerm PTerm
lhs))) of
            OK (ElabResult Term
lhs' [(Name, (Int, Maybe Name, Term, [Name]))]
_ [PDecl' PTerm]
_ Context
ctxt' [RDeclInstructions]
newDecls Set (FC', OutputAnnotation)
highlights Int
newGName, String
_) ->
               do Context -> Idris ()
setContext Context
ctxt'
                  ElabInfo -> [RDeclInstructions] -> Idris ()
processTacticDecls ElabInfo
info [RDeclInstructions]
newDecls
                  Set (FC', OutputAnnotation) -> Idris ()
sendHighlighting Set (FC', OutputAnnotation)
highlights
                  (IState -> IState) -> Idris ()
updateIState ((IState -> IState) -> Idris ()) -> (IState -> IState) -> Idris ()
forall a b. (a -> b) -> a -> b
$ \IState
i -> IState
i { idris_name = newGName }
                  let lhs_tm :: Term
lhs_tm = Context -> Env -> Term -> Term
normalise Context
ctxt [] (Term -> Term
orderPats (Term -> Term
getInferTerm Term
lhs'))
                  let emptyPat :: Bool
emptyPat = Context -> Ctxt TypeInfo -> Term -> Bool
hasEmptyPat Context
ctxt (IState -> Ctxt TypeInfo
idris_datatypes IState
i) Term
lhs_tm
                  if Bool
emptyPat then
                     do Int -> String -> Idris ()
logElab Int
10 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"Empty type in pattern "
                        Maybe PTerm -> Idris (Maybe PTerm)
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe PTerm
forall a. Maybe a
Nothing
                    else
                      case String -> Context -> Env -> Raw -> Term -> TC (Term, Term, UCs)
recheck (ElabInfo -> String
constraintNS ElabInfo
info) Context
ctxt' [] (Term -> Raw
forget Term
lhs_tm) Term
lhs_tm of
                           OK (Term
tm, Term
_, UCs
_) ->
                                    do Int -> String -> Idris ()
logElab Int
10 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"Valid " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Term -> String
forall a. Show a => a -> String
show Term
tm String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n"
                                                 String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" from " String -> String -> String
forall a. [a] -> [a] -> [a]
++ PTerm -> String
forall a. Show a => a -> String
show PTerm
lhs
                                       Maybe PTerm -> Idris (Maybe PTerm)
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return (PTerm -> Maybe PTerm
forall a. a -> Maybe a
Just (IState -> Term -> Bool -> Bool -> PTerm
delab' IState
i Term
tm Bool
True Bool
True))
                           TC (Term, Term, UCs)
err -> do Int -> String -> Idris ()
logElab Int
10 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"Conversion failure"
                                     Maybe PTerm -> Idris (Maybe PTerm)
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe PTerm
forall a. Maybe a
Nothing
            -- if it's a recoverable error, the case may become possible
            Error Err
err -> do Int -> String -> Idris ()
logLvl Int
10 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"Impossible case " String -> String -> String
forall a. [a] -> [a] -> [a]
++ (IState -> Err -> String
pshow IState
i Err
err)
                                 String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ (Bool, Bool) -> String
forall a. Show a => a -> String
show (Context -> Err -> Bool
recoverableCoverage Context
ctxt Err
err,
                                                  Context -> Err -> Bool
validCoverageCase Context
ctxt Err
err)
                            -- tcgen means that it was generated by genClauses,
                            -- so only looking for an error. Otherwise, it
                            -- needs to be the right kind of error (a type mismatch
                            -- in the same family).
                            if Bool
tcgen then IState -> Err -> Bool -> Idris (Maybe PTerm)
returnTm IState
i Err
err (Context -> Err -> Bool
recoverableCoverage Context
ctxt Err
err)
                                  else IState -> Err -> Bool -> Idris (Maybe PTerm)
returnTm IState
i Err
err (Context -> Err -> Bool
validCoverageCase Context
ctxt Err
err Bool -> Bool -> Bool
||
                                                 Context -> Err -> Bool
recoverableCoverage Context
ctxt Err
err)
  where returnTm :: IState -> Err -> Bool -> Idris (Maybe PTerm)
returnTm IState
i Err
err Bool
True = do Int -> String -> Idris ()
logLvl Int
10 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"Possibly resolvable error on " String -> String -> String
forall a. [a] -> [a] -> [a]
++
                                    IState -> Err -> String
pshow IState
i ((Term -> Term) -> Err -> Err
forall a b. (a -> b) -> Err' a -> Err' b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Context -> Env -> Term -> Term
normalise (IState -> Context
tt_ctxt IState
i) []) Err
err)
                                              String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" on " String -> String -> String
forall a. [a] -> [a] -> [a]
++ PTerm -> String
showTmImpls PTerm
lhs_in
                                 Maybe PTerm -> Idris (Maybe PTerm)
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe PTerm -> Idris (Maybe PTerm))
-> Maybe PTerm -> Idris (Maybe PTerm)
forall a b. (a -> b) -> a -> b
$ PTerm -> Maybe PTerm
forall a. a -> Maybe a
Just PTerm
lhs_in
        returnTm IState
i Err
err Bool
False = Maybe PTerm -> Idris (Maybe PTerm)
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe PTerm -> Idris (Maybe PTerm))
-> Maybe PTerm -> Idris (Maybe PTerm)
forall a b. (a -> b) -> a -> b
$ Maybe PTerm
forall a. Maybe a
Nothing

-- Filter out the terms which are not well type left hand sides. Whenever we
-- eliminate one, also eliminate later ones which match it without checking,
-- because they're obviously going to have the same result
checkPossibles :: ElabInfo -> FC -> Bool -> Name -> [PTerm] -> Idris [PTerm]
checkPossibles :: ElabInfo
-> FC
-> Bool
-> Name
-> [PTerm]
-> StateT IState (ExceptT Err IO) [PTerm]
checkPossibles ElabInfo
info FC
fc Bool
tcgen Name
fname (PTerm
lhs : [PTerm]
rest)
   = do Maybe PTerm
ok <- ElabInfo -> FC -> Bool -> Name -> PTerm -> Idris (Maybe PTerm)
checkPossible ElabInfo
info FC
fc Bool
tcgen Name
fname PTerm
lhs
        IState
i <- Idris IState
getIState
        -- Hypothesis: any we can remove will be within the next few, because
        -- leftmost patterns tend to change less
        -- Since the match could take a while if there's a lot of cases to
        -- check, just remove from the next batch
        let rest' :: [PTerm]
rest' = (PTerm -> Bool) -> [PTerm] -> [PTerm]
forall a. (a -> Bool) -> [a] -> [a]
filter (\PTerm
x -> Bool -> Bool
not (PTerm -> PTerm -> Bool
qmatch PTerm
x PTerm
lhs)) (Int -> [PTerm] -> [PTerm]
forall a. Int -> [a] -> [a]
take Int
200 [PTerm]
rest) [PTerm] -> [PTerm] -> [PTerm]
forall a. [a] -> [a] -> [a]
++ Int -> [PTerm] -> [PTerm]
forall a. Int -> [a] -> [a]
drop Int
200 [PTerm]
rest
        [PTerm]
restpos <- ElabInfo
-> FC
-> Bool
-> Name
-> [PTerm]
-> StateT IState (ExceptT Err IO) [PTerm]
checkPossibles ElabInfo
info FC
fc Bool
tcgen Name
fname [PTerm]
rest'
        case Maybe PTerm
ok of
             Maybe PTerm
Nothing -> [PTerm] -> StateT IState (ExceptT Err IO) [PTerm]
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return [PTerm]
restpos
             Just PTerm
lhstm -> [PTerm] -> StateT IState (ExceptT Err IO) [PTerm]
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return (PTerm
lhstm PTerm -> [PTerm] -> [PTerm]
forall a. a -> [a] -> [a]
: [PTerm]
restpos)
  where
    qmatch :: PTerm -> PTerm -> Bool
qmatch PTerm
_ PTerm
Placeholder = Bool
True
    qmatch (PApp FC
_ PTerm
f [PArg]
args) (PApp FC
_ PTerm
f' [PArg]
args')
       | [PArg] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [PArg]
args Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== [PArg] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [PArg]
args'
            = PTerm -> PTerm -> Bool
qmatch PTerm
f PTerm
f' Bool -> Bool -> Bool
&& [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and ((PTerm -> PTerm -> Bool) -> [PTerm] -> [PTerm] -> [Bool]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith PTerm -> PTerm -> Bool
qmatch ((PArg -> PTerm) -> [PArg] -> [PTerm]
forall a b. (a -> b) -> [a] -> [b]
map PArg -> PTerm
forall t. PArg' t -> t
getTm [PArg]
args)
                                                 ((PArg -> PTerm) -> [PArg] -> [PTerm]
forall a b. (a -> b) -> [a] -> [b]
map PArg -> PTerm
forall t. PArg' t -> t
getTm [PArg]
args'))
    qmatch (PRef FC
_ [FC]
_ Name
n) (PRef FC
_ [FC]
_ Name
n') = Name
n Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== Name
n'
    qmatch (PPair FC
_ [FC]
_ PunInfo
_ PTerm
l PTerm
r) (PPair FC
_ [FC]
_ PunInfo
_ PTerm
l' PTerm
r') = PTerm -> PTerm -> Bool
qmatch PTerm
l PTerm
l' Bool -> Bool -> Bool
&& PTerm -> PTerm -> Bool
qmatch PTerm
r PTerm
r'
    qmatch (PDPair FC
_ [FC]
_ PunInfo
_ PTerm
l PTerm
t PTerm
r) (PDPair FC
_ [FC]
_ PunInfo
_ PTerm
l' PTerm
t' PTerm
r')
          = PTerm -> PTerm -> Bool
qmatch PTerm
l PTerm
l' Bool -> Bool -> Bool
&& PTerm -> PTerm -> Bool
qmatch PTerm
t PTerm
t' Bool -> Bool -> Bool
&& PTerm -> PTerm -> Bool
qmatch PTerm
r PTerm
r'
    qmatch PTerm
x PTerm
y = PTerm
x PTerm -> PTerm -> Bool
forall a. Eq a => a -> a -> Bool
== PTerm
y
checkPossibles ElabInfo
_ FC
_ Bool
_ Name
_ [] = [PTerm] -> StateT IState (ExceptT Err IO) [PTerm]
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return []

findUnique :: Context -> Env -> Term -> [Name]
findUnique :: Context -> Env -> Term -> [Name]
findUnique Context
ctxt Env
env (Bind Name
n Binder Term
b Term
sc)
   = let rawTy :: Raw
rawTy = [Name] -> Term -> Raw
forgetEnv (((Name, RigCount, Binder Term) -> Name) -> Env -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name, RigCount, Binder Term) -> Name
forall {a} {b} {c}. (a, b, c) -> a
fstEnv Env
env) (Binder Term -> Term
forall b. Binder b -> b
binderTy Binder Term
b)
         uniq :: Bool
uniq = case Context -> Env -> Raw -> TC (Term, Term)
check Context
ctxt Env
env Raw
rawTy of
                     OK (Term
_, UType Universe
UniqueType) -> Bool
True
                     OK (Term
_, UType Universe
NullType) -> Bool
True
                     OK (Term
_, UType Universe
AllTypes) -> Bool
True
                     TC (Term, Term)
_ -> Bool
False in
         if Bool
uniq then Name
n Name -> [Name] -> [Name]
forall a. a -> [a] -> [a]
: Context -> Env -> Term -> [Name]
findUnique Context
ctxt ((Name
n, RigCount
RigW, Binder Term
b) (Name, RigCount, Binder Term) -> Env -> Env
forall a. a -> [a] -> [a]
: Env
env) Term
sc
                 else Context -> Env -> Term -> [Name]
findUnique Context
ctxt ((Name
n, RigCount
RigW, Binder Term
b) (Name, RigCount, Binder Term) -> Env -> Env
forall a. a -> [a] -> [a]
: Env
env) Term
sc
findUnique Context
_ Env
_ Term
_ = []

-- | Return the elaborated LHS/RHS, and the original LHS with implicits added
elabClause :: ElabInfo -> FnOpts -> (Int, PClause) ->
              Idris (Either Term (Term, Term), PTerm)
elabClause :: ElabInfo
-> FnOpts
-> (Int, PClause)
-> StateT IState (ExceptT Err IO) (Either Term (Term, Term), PTerm)
elabClause ElabInfo
info FnOpts
opts (Int
_, PClause FC
fc Name
fname PTerm
lhs_in [] PTerm
PImpossible [])
   = do let tcgen :: Bool
tcgen = FnOpt
Dictionary FnOpt -> FnOpts -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` FnOpts
opts
        IState
i <- Idris IState
forall s (m :: * -> *). MonadState s m => m s
get
        let lhs :: PTerm
lhs = [Name] -> IState -> PTerm -> PTerm
addImpl [] IState
i PTerm
lhs_in
        Maybe PTerm
b <- ElabInfo -> FC -> Bool -> Name -> PTerm -> Idris (Maybe PTerm)
checkPossible ElabInfo
info FC
fc Bool
tcgen Name
fname PTerm
lhs_in
        case Maybe PTerm
b of
            Just PTerm
_ -> TC (Either Term (Term, Term), PTerm)
-> StateT IState (ExceptT Err IO) (Either Term (Term, Term), PTerm)
forall a. TC a -> Idris a
tclift (TC (Either Term (Term, Term), PTerm)
 -> StateT
      IState (ExceptT Err IO) (Either Term (Term, Term), PTerm))
-> TC (Either Term (Term, Term), PTerm)
-> StateT IState (ExceptT Err IO) (Either Term (Term, Term), PTerm)
forall a b. (a -> b) -> a -> b
$ Err -> TC (Either Term (Term, Term), PTerm)
forall a. Err -> TC a
tfail (FC -> Err -> Err
forall t. FC -> Err' t -> Err' t
At FC
fc
                                 (String -> Err
forall t. String -> Err' t
Msg (String -> Err) -> String -> Err
forall a b. (a -> b) -> a -> b
$ PTerm -> String
forall a. Show a => a -> String
show PTerm
lhs_in String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" is a valid case"))
            Maybe PTerm
Nothing -> do Term
ptm <- PTerm -> StateT IState (ExceptT Err IO) Term
mkPatTm PTerm
lhs_in
                          Int -> String -> Idris ()
logElab Int
5 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"Elaborated impossible case " String -> String -> String
forall a. [a] -> [a] -> [a]
++ PTerm -> String
showTmImpls PTerm
lhs String -> String -> String
forall a. [a] -> [a] -> [a]
++
                                      String
"\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Term -> String
forall a. Show a => a -> String
show Term
ptm
                          (Either Term (Term, Term), PTerm)
-> StateT IState (ExceptT Err IO) (Either Term (Term, Term), PTerm)
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Term -> Either Term (Term, Term)
forall a b. a -> Either a b
Left Term
ptm, PTerm
lhs)
elabClause ElabInfo
info FnOpts
opts (Int
cnum, PClause FC
fc Name
fname PTerm
lhs_in_as [PTerm]
withs PTerm
rhs_in_as [PDecl' PTerm]
whereblock_as)
   = do Name -> Bool -> Idris ()
push_estack Name
fname Bool
False
        Context
ctxt <- Idris Context
getContext
        let (PTerm
lhs_in, PTerm
rhs_in, [PDecl' PTerm]
whereblock) = PTerm -> PTerm -> [PDecl' PTerm] -> (PTerm, PTerm, [PDecl' PTerm])
desugarAs PTerm
lhs_in_as PTerm
rhs_in_as [PDecl' PTerm]
whereblock_as

        -- Build the LHS as an "Infer", and pull out its type and
        -- pattern bindings
        IState
i <- Idris IState
getIState
        Bool
inf <- Name -> Idris Bool
isTyInferred Name
fname

        -- Check if we have extra "with" patterns
        Bool -> Idris () -> Idris ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ [PTerm] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [PTerm]
withs) (Idris () -> Idris ()) -> Idris () -> Idris ()
forall a b. (a -> b) -> a -> b
$
            Err -> Idris ()
forall a. Err -> Idris a
ierror (FC -> Err -> Err
forall t. FC -> Err' t -> Err' t
At (FC -> Maybe FC -> FC
forall a. a -> Maybe a -> a
fromMaybe FC
NoFC (Maybe FC -> FC) -> Maybe FC -> FC
forall a b. (a -> b) -> a -> b
$ PTerm -> Maybe FC
highestFC PTerm
lhs_in_as)
                       (String -> Name -> Maybe Term -> Err -> Err
forall t. String -> Name -> Maybe t -> Err' t -> Err' t
Elaborating String
"left hand side of " Name
fname Maybe Term
forall a. Maybe a
Nothing
                        (String -> Err
forall t. String -> Err' t
Msg (String -> Err) -> String -> Err
forall a b. (a -> b) -> a -> b
$ if PTerm -> Bool
isOutsideWith PTerm
lhs_in
                               then String
"unexpected patterns outside of \"with\" block"
                               else String
"unexpected extra \"with\" patterns")))

        -- get the parameters first, to pass through to any where block
        let fn_ty :: Term
fn_ty = case Name -> Context -> [Term]
lookupTy Name
fname Context
ctxt of
                         [Term
t] -> Term
t
                         [Term]
_ -> String -> Term
forall a. HasCallStack => String -> a
error String
"Can't happen (elabClause function type)"
        let fn_is :: [PArg]
fn_is = case Name -> Ctxt [PArg] -> [[PArg]]
forall a. Name -> Ctxt a -> [a]
lookupCtxt Name
fname (IState -> Ctxt [PArg]
idris_implicits IState
i) of
                         [[PArg]
t] -> [PArg]
t
                         [[PArg]]
_ -> []
        let norm_ty :: Term
norm_ty = Context -> Env -> Term -> Term
normalise Context
ctxt [] Term
fn_ty
        let params :: [Name]
params = IState -> [Name] -> [PArg] -> Term -> [Name]
getParamsInType IState
i [] [PArg]
fn_is Term
norm_ty
        let tcparams :: [Name]
tcparams = IState -> [Name] -> [PArg] -> Term -> [Name]
getTCParamsInType IState
i [] [PArg]
fn_is Term
norm_ty

        let lhs :: PTerm
lhs = PTerm -> PTerm
mkLHSapp (PTerm -> PTerm) -> PTerm -> PTerm
forall a b. (a -> b) -> a -> b
$ IState -> PTerm -> PTerm
stripLinear IState
i (PTerm -> PTerm) -> PTerm -> PTerm
forall a b. (a -> b) -> a -> b
$ IState -> PTerm -> PTerm
stripUnmatchable IState
i (PTerm -> PTerm) -> PTerm -> PTerm
forall a b. (a -> b) -> a -> b
$
                    IState -> [Name] -> Term -> [Name] -> PTerm -> PTerm
propagateParams IState
i [Name]
params Term
norm_ty (PTerm -> [Name]
allNamesIn PTerm
lhs_in) (IState -> PTerm -> PTerm
addImplPat IState
i PTerm
lhs_in)

--         let lhs = mkLHSapp $
--                     propagateParams i params fn_ty (addImplPat i lhs_in)
        Int -> String -> Idris ()
logElab Int
10 (([Name], Term) -> String
forall a. Show a => a -> String
show ([Name]
params, Term
fn_ty) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ PTerm -> String
showTmImpls (IState -> PTerm -> PTerm
addImplPat IState
i PTerm
lhs_in))
        Int -> String -> Idris ()
logElab Int
5 (String
"LHS: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ FnOpts -> String
forall a. Show a => a -> String
show FnOpts
opts String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ FC -> String
forall a. Show a => a -> String
show FC
fc String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ PTerm -> String
showTmImpls PTerm
lhs)
        Int -> String -> Idris ()
logElab Int
4 (String
"Fixed parameters: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [Name] -> String
forall a. Show a => a -> String
show [Name]
params String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" from " String -> String -> String
forall a. [a] -> [a] -> [a]
++ PTerm -> String
showTmImpls PTerm
lhs_in String -> String -> String
forall a. [a] -> [a] -> [a]
++
                  String
"\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ (Term, [PArg]) -> String
forall a. Show a => a -> String
show (Term
fn_ty, [PArg]
fn_is))

        ((ElabResult Term
lhs' [(Name, (Int, Maybe Name, Term, [Name]))]
dlhs [] Context
ctxt' [RDeclInstructions]
newDecls Set (FC', OutputAnnotation)
highlights Int
newGName, Fails
probs, [Name]
inj), String
_) <-
           TC ((ElabResult, Fails, [Name]), String)
-> Idris ((ElabResult, Fails, [Name]), String)
forall a. TC a -> Idris a
tclift (TC ((ElabResult, Fails, [Name]), String)
 -> Idris ((ElabResult, Fails, [Name]), String))
-> TC ((ElabResult, Fails, [Name]), String)
-> Idris ((ElabResult, Fails, [Name]), String)
forall a b. (a -> b) -> a -> b
$ String
-> Context
-> Ctxt TypeInfo
-> Int
-> Name
-> Term
-> EState
-> Elab' EState (ElabResult, Fails, [Name])
-> TC ((ElabResult, Fails, [Name]), String)
forall aux a.
String
-> Context
-> Ctxt TypeInfo
-> Int
-> Name
-> Term
-> aux
-> Elab' aux a
-> TC (a, String)
elaborate (ElabInfo -> String
constraintNS ElabInfo
info) Context
ctxt (IState -> Ctxt TypeInfo
idris_datatypes IState
i) (IState -> Int
idris_name IState
i) (Int -> String -> Name
sMN Int
0 String
"patLHS") Term
infP EState
initEState
                    (do ElabResult
res <- String
-> Name
-> Maybe Term
-> Elab' EState ElabResult
-> Elab' EState ElabResult
forall aux a.
String -> Name -> Maybe Term -> Elab' aux a -> Elab' aux a
errAt String
"left hand side of " Name
fname Maybe Term
forall a. Maybe a
Nothing
                                 (FC -> Elab' EState ElabResult -> Elab' EState ElabResult
forall aux a. FC -> Elab' aux a -> Elab' aux a
erun (FC -> Maybe FC -> FC
forall a. a -> Maybe a -> a
fromMaybe FC
NoFC (Maybe FC -> FC) -> Maybe FC -> FC
forall a b. (a -> b) -> a -> b
$ PTerm -> Maybe FC
highestFC PTerm
lhs_in_as)
                                       (IState
-> ElabInfo
-> ElabMode
-> FnOpts
-> Name
-> [Name]
-> PTerm
-> Elab' EState ElabResult
buildTC IState
i ElabInfo
info ElabMode
ELHS FnOpts
opts Name
fname
                                          (PTerm -> [Name]
allNamesIn PTerm
lhs_in)
                                          (PTerm -> PTerm
infTerm PTerm
lhs)))
                        Fails
probs <- Elab' EState Fails
forall aux. Elab' aux Fails
get_probs
                        [Name]
inj <- Elab' EState [Name]
forall aux. Elab' aux [Name]
get_inj
                        (ElabResult, Fails, [Name])
-> Elab' EState (ElabResult, Fails, [Name])
forall a. a -> StateT (ElabState EState) TC a
forall (m :: * -> *) a. Monad m => a -> m a
return (ElabResult
res, Fails
probs, [Name]
inj))
        Context -> Idris ()
setContext Context
ctxt'
        ElabInfo -> [RDeclInstructions] -> Idris ()
processTacticDecls ElabInfo
info [RDeclInstructions]
newDecls
        Set (FC', OutputAnnotation) -> Idris ()
sendHighlighting Set (FC', OutputAnnotation)
highlights
        (IState -> IState) -> Idris ()
updateIState ((IState -> IState) -> Idris ()) -> (IState -> IState) -> Idris ()
forall a b. (a -> b) -> a -> b
$ \IState
i -> IState
i { idris_name = newGName }

        Bool -> Idris () -> Idris ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
inf (Idris () -> Idris ()) -> Idris () -> Idris ()
forall a b. (a -> b) -> a -> b
$ FC -> [(Term, Term)] -> Idris ()
addTyInfConstraints FC
fc (((Term, Term, Bool, Env, Err, [FailContext], FailAt)
 -> (Term, Term))
-> Fails -> [(Term, Term)]
forall a b. (a -> b) -> [a] -> [b]
map (\(Term
x,Term
y,Bool
_,Env
_,Err
_,[FailContext]
_,FailAt
_) -> (Term
x,Term
y)) Fails
probs)

        let lhs_tm :: Term
lhs_tm = Term -> Term
orderPats (Term -> Term
getInferTerm Term
lhs')
        let lhs_ty :: Term
lhs_ty = Term -> Term
getInferType Term
lhs'
        let static_names :: [Name]
static_names = IState -> Term -> [Name]
getStaticNames IState
i Term
lhs_tm

        Int -> String -> Idris ()
logElab Int
3 (String
"Elaborated: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Term -> String
forall a. Show a => a -> String
show Term
lhs_tm)
        Int -> String -> Idris ()
logElab Int
3 (String
"Elaborated type: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Term -> String
forall a. Show a => a -> String
show Term
lhs_ty)
        Int -> String -> Idris ()
logElab Int
5 (String
"Injective: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Name -> String
forall a. Show a => a -> String
show Name
fname String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [Name] -> String
forall a. Show a => a -> String
show [Name]
inj)

        -- If we're inferring metavariables in the type, don't recheck,
        -- because we're only doing this to try to work out those metavariables
        Context
ctxt <- Idris Context
getContext
        (Term
clhs_c, Term
clhsty) <- if Bool -> Bool
not Bool
inf
                               then Bool
-> Bool
-> [Name]
-> String
-> FC
-> (Err -> Err)
-> Env
-> Term
-> Idris (Term, Term)
recheckC_borrowing Bool
False (FnOpt
PEGenerated FnOpt -> FnOpts -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` FnOpts
opts)
                                                       [] (ElabInfo -> String
constraintNS ElabInfo
info) FC
fc Err -> Err
forall a. a -> a
forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id [] Term
lhs_tm
                               else (Term, Term) -> Idris (Term, Term)
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Term
lhs_tm, Term
lhs_ty)
        let clhs :: Term
clhs = Context -> Env -> Term -> Term
normalise Context
ctxt [] Term
clhs_c
        -- These are the names we're not allowed to use on the RHS, because
        -- they're UniqueTypes and borrowed from another function.
        let borrowed :: [Name]
borrowed = [Name] -> Term -> [Name]
borrowedNames [] Term
clhs

        Int -> String -> Idris ()
logElab Int
3 (String
"Normalised LHS: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ PTerm -> String
showTmImpls (IState -> Term -> PTerm
delabMV IState
i Term
clhs))

        Bool
rep <- Idris Bool
useREPL
        Bool -> Idris () -> Idris ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
rep (Idris () -> Idris ()) -> Idris () -> Idris ()
forall a b. (a -> b) -> a -> b
$ do
          String -> Int -> PTerm -> Idris ()
addInternalApp (FC -> String
fc_fname FC
fc) ((Int, Int) -> Int
forall a b. (a, b) -> a
fst ((Int, Int) -> Int) -> (FC -> (Int, Int)) -> FC -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. FC -> (Int, Int)
fc_start (FC -> Int) -> FC -> Int
forall a b. (a -> b) -> a -> b
$ FC
fc) (IState -> Term -> PTerm
delabMV IState
i Term
clhs) -- TODO: Should use span instead of line and filename?
        IBCWrite -> Idris ()
addIBC (String -> Int -> PTerm -> IBCWrite
IBCLineApp (FC -> String
fc_fname FC
fc) ((Int, Int) -> Int
forall a b. (a, b) -> a
fst ((Int, Int) -> Int) -> (FC -> (Int, Int)) -> FC -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. FC -> (Int, Int)
fc_start (FC -> Int) -> FC -> Int
forall a b. (a -> b) -> a -> b
$ FC
fc) (IState -> Term -> PTerm
delabMV IState
i Term
clhs))

        Int -> String -> Idris ()
logElab Int
5 (String
"Checked " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Term -> String
forall a. Show a => a -> String
show Term
clhs String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Term -> String
forall a. Show a => a -> String
show Term
clhsty)
        -- Elaborate where block
        IState
ist <- Idris IState
getIState
        Context
ctxt <- Idris Context
getContext

        Int
windex <- Idris Int
getName
        let decls :: [Name]
decls = [Name] -> [Name]
forall a. Eq a => [a] -> [a]
nub ((PDecl' PTerm -> [Name]) -> [PDecl' PTerm] -> [Name]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap PDecl' PTerm -> [Name]
declared [PDecl' PTerm]
whereblock)
        let defs :: [Name]
defs = [Name] -> [Name]
forall a. Eq a => [a] -> [a]
nub ([Name]
decls [Name] -> [Name] -> [Name]
forall a. [a] -> [a] -> [a]
++ (PDecl' PTerm -> [Name]) -> [PDecl' PTerm] -> [Name]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap PDecl' PTerm -> [Name]
defined [PDecl' PTerm]
whereblock)
        let newargs_all :: [(Name, PTerm)]
newargs_all = IState -> Term -> [(Name, PTerm)]
pvars IState
ist Term
lhs_tm

        -- Unique arguments must be passed to the where block explicitly
        -- (since we can't control "usage" easlily otherwise). Remove them
        -- from newargs here
        let uniqargs :: [Name]
uniqargs = Context -> Env -> Term -> [Name]
findUnique Context
ctxt [] Term
lhs_tm
        let newargs :: [(Name, PTerm)]
newargs = ((Name, PTerm) -> Bool) -> [(Name, PTerm)] -> [(Name, PTerm)]
forall a. (a -> Bool) -> [a] -> [a]
filter (\(Name
n,PTerm
_) -> Name
n Name -> [Name] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Name]
uniqargs) [(Name, PTerm)]
newargs_all

        let winfo :: ElabInfo
winfo = (ElabInfo -> [(Name, PTerm)] -> [Name] -> Int -> ElabInfo
pinfo ElabInfo
info [(Name, PTerm)]
newargs [Name]
defs Int
windex) { elabFC = Just fc }
        let wb :: [PDecl' PTerm]
wb = (PDecl' PTerm -> PDecl' PTerm) -> [PDecl' PTerm] -> [PDecl' PTerm]
forall a b. (a -> b) -> [a] -> [b]
map ([Name] -> PDecl' PTerm -> PDecl' PTerm
mkStatic [Name]
static_names) ([PDecl' PTerm] -> [PDecl' PTerm])
-> [PDecl' PTerm] -> [PDecl' PTerm]
forall a b. (a -> b) -> a -> b
$
                 (PDecl' PTerm -> PDecl' PTerm) -> [PDecl' PTerm] -> [PDecl' PTerm]
forall a b. (a -> b) -> [a] -> [b]
map (IState
-> (Name -> Name)
-> [(Name, PTerm)]
-> [Name]
-> PDecl' PTerm
-> PDecl' PTerm
forall {p1} {p2} {t} {p3}.
p1 -> p2 -> [(Name, t)] -> p3 -> PDecl' t -> PDecl' t
expandImplementationScope IState
ist Name -> Name
decorate [(Name, PTerm)]
newargs [Name]
defs) ([PDecl' PTerm] -> [PDecl' PTerm])
-> [PDecl' PTerm] -> [PDecl' PTerm]
forall a b. (a -> b) -> a -> b
$
                 (PDecl' PTerm -> PDecl' PTerm) -> [PDecl' PTerm] -> [PDecl' PTerm]
forall a b. (a -> b) -> [a] -> [b]
map (Bool
-> IState
-> (Name -> Name)
-> [(Name, PTerm)]
-> [Name]
-> PDecl' PTerm
-> PDecl' PTerm
expandParamsD Bool
False IState
ist Name -> Name
decorate [(Name, PTerm)]
newargs [Name]
defs) [PDecl' PTerm]
whereblock

        -- Split the where block into declarations with a type, and those
        -- without
        -- Elaborate those with a type *before* RHS, those without *after*
        let ([PDecl' PTerm]
wbefore, [PDecl' PTerm]
wafter) = [PDecl' PTerm] -> ([PDecl' PTerm], [PDecl' PTerm])
forall {t}. [PDecl' t] -> ([PDecl' t], [PDecl' t])
sepBlocks [PDecl' PTerm]
wb

        Int -> String -> Idris ()
logElab Int
5 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"Where block:\n " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [PDecl' PTerm] -> String
forall a. Show a => a -> String
show [PDecl' PTerm]
wbefore String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ [PDecl' PTerm] -> String
forall a. Show a => a -> String
show [PDecl' PTerm]
wafter
        (PDecl' PTerm -> Idris ()) -> [PDecl' PTerm] -> Idris ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (ElabInfo -> ElabWhat -> ElabInfo -> PDecl' PTerm -> Idris ()
rec_elabDecl ElabInfo
info ElabWhat
EAll ElabInfo
winfo) [PDecl' PTerm]
wbefore
        -- Now build the RHS, using the type of the LHS as the goal.
        IState
i <- Idris IState
getIState -- new implicits from where block
        Int -> String -> Idris ()
logElab Int
5 (PTerm -> String
showTmImpls ((Name -> Name)
-> [(Name, PTerm)] -> [Name] -> [Name] -> PTerm -> PTerm
expandParams Name -> Name
decorate [(Name, PTerm)]
newargs [Name]
defs ([Name]
defs [Name] -> [Name] -> [Name]
forall a. Eq a => [a] -> [a] -> [a]
\\ [Name]
decls) PTerm
rhs_in))
        let rhs :: PTerm
rhs = ElabInfo -> PTerm -> PTerm
rhs_trans ElabInfo
info (PTerm -> PTerm) -> PTerm -> PTerm
forall a b. (a -> b) -> a -> b
$
                   IState -> [Name] -> [Name] -> PTerm -> PTerm
addImplBoundInf IState
i (((Name, PTerm) -> Name) -> [(Name, PTerm)] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name, PTerm) -> Name
forall a b. (a, b) -> a
fst [(Name, PTerm)]
newargs_all) ([Name]
defs [Name] -> [Name] -> [Name]
forall a. Eq a => [a] -> [a] -> [a]
\\ [Name]
decls)
                                 ((Name -> Name)
-> [(Name, PTerm)] -> [Name] -> [Name] -> PTerm -> PTerm
expandParams Name -> Name
decorate [(Name, PTerm)]
newargs [Name]
defs ([Name]
defs [Name] -> [Name] -> [Name]
forall a. Eq a => [a] -> [a] -> [a]
\\ [Name]
decls) PTerm
rhs_in)
        Int -> String -> Idris ()
logElab Int
2 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"RHS: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [Name] -> String
forall a. Show a => a -> String
show (((Name, PTerm) -> Name) -> [(Name, PTerm)] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name, PTerm) -> Name
forall a b. (a, b) -> a
fst [(Name, PTerm)]
newargs_all) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ PTerm -> String
showTmImpls PTerm
rhs
        Context
ctxt <- Idris Context
getContext -- new context with where block added
        Int -> String -> Idris ()
logElab Int
5 String
"STARTING CHECK"
        ((Term
rhsElab, [(Name, (Int, Maybe Name, Term, [Name]))]
defer, [Name]
holes, [PDecl' PTerm]
is, Fails
probs, Context
ctxt', [RDeclInstructions]
newDecls, Set (FC', OutputAnnotation)
highlights, Int
newGName), String
_) <-
           TC
  ((Term, [(Name, (Int, Maybe Name, Term, [Name]))], [Name],
    [PDecl' PTerm], Fails, Context, [RDeclInstructions],
    Set (FC', OutputAnnotation), Int),
   String)
-> Idris
     ((Term, [(Name, (Int, Maybe Name, Term, [Name]))], [Name],
       [PDecl' PTerm], Fails, Context, [RDeclInstructions],
       Set (FC', OutputAnnotation), Int),
      String)
forall a. TC a -> Idris a
tclift (TC
   ((Term, [(Name, (Int, Maybe Name, Term, [Name]))], [Name],
     [PDecl' PTerm], Fails, Context, [RDeclInstructions],
     Set (FC', OutputAnnotation), Int),
    String)
 -> Idris
      ((Term, [(Name, (Int, Maybe Name, Term, [Name]))], [Name],
        [PDecl' PTerm], Fails, Context, [RDeclInstructions],
        Set (FC', OutputAnnotation), Int),
       String))
-> TC
     ((Term, [(Name, (Int, Maybe Name, Term, [Name]))], [Name],
       [PDecl' PTerm], Fails, Context, [RDeclInstructions],
       Set (FC', OutputAnnotation), Int),
      String)
-> Idris
     ((Term, [(Name, (Int, Maybe Name, Term, [Name]))], [Name],
       [PDecl' PTerm], Fails, Context, [RDeclInstructions],
       Set (FC', OutputAnnotation), Int),
      String)
forall a b. (a -> b) -> a -> b
$ String
-> Context
-> Ctxt TypeInfo
-> Int
-> Name
-> Term
-> EState
-> Elab'
     EState
     (Term, [(Name, (Int, Maybe Name, Term, [Name]))], [Name],
      [PDecl' PTerm], Fails, Context, [RDeclInstructions],
      Set (FC', OutputAnnotation), Int)
-> TC
     ((Term, [(Name, (Int, Maybe Name, Term, [Name]))], [Name],
       [PDecl' PTerm], Fails, Context, [RDeclInstructions],
       Set (FC', OutputAnnotation), Int),
      String)
forall aux a.
String
-> Context
-> Ctxt TypeInfo
-> Int
-> Name
-> Term
-> aux
-> Elab' aux a
-> TC (a, String)
elaborate (ElabInfo -> String
constraintNS ElabInfo
info) Context
ctxt (IState -> Ctxt TypeInfo
idris_datatypes IState
i) (IState -> Int
idris_name IState
i) (Int -> String -> Name
sMN Int
0 String
"patRHS") Term
clhsty EState
initEState
                    (do IState -> Term -> ElabD ()
pbinds IState
ist Term
lhs_tm
                        -- proof search can use explicitly written names
                        (Name -> ElabD ()) -> [Name] -> ElabD ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Name -> ElabD ()
forall aux. Name -> Elab' aux ()
addPSname (PTerm -> [Name]
allNamesIn PTerm
lhs_in)
                        Bool
ulog <- Elab' EState Bool
forall aux. Elab' aux Bool
getUnifyLog
                        Bool -> String -> ElabD () -> ElabD ()
forall {a}. Bool -> String -> a -> a
traceWhen Bool
ulog (String
"Setting injective: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [Name] -> String
forall a. Show a => a -> String
show ([Name] -> [Name]
forall a. Eq a => [a] -> [a]
nub ([Name]
tcparams [Name] -> [Name] -> [Name]
forall a. [a] -> [a] -> [a]
++ [Name]
inj))) (ElabD () -> ElabD ()) -> ElabD () -> ElabD ()
forall a b. (a -> b) -> a -> b
$
                          (Name -> ElabD ()) -> [Name] -> ElabD ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Name -> ElabD ()
forall aux. Name -> Elab' aux ()
setinj ([Name] -> [Name]
forall a. Eq a => [a] -> [a]
nub ([Name]
tcparams [Name] -> [Name] -> [Name]
forall a. [a] -> [a] -> [a]
++ [Name]
inj))
                        ElabD ()
forall aux. Elab' aux ()
setNextName
                        (ElabResult Term
_ [(Name, (Int, Maybe Name, Term, [Name]))]
_ [PDecl' PTerm]
is Context
ctxt' [RDeclInstructions]
newDecls Set (FC', OutputAnnotation)
highlights Int
newGName) <-
                          String
-> Name
-> Maybe Term
-> Elab' EState ElabResult
-> Elab' EState ElabResult
forall aux a.
String -> Name -> Maybe Term -> Elab' aux a -> Elab' aux a
errAt String
"right hand side of " Name
fname (Term -> Maybe Term
forall a. a -> Maybe a
Just Term
clhsty)
                                (FC -> Elab' EState ElabResult -> Elab' EState ElabResult
forall aux a. FC -> Elab' aux a -> Elab' aux a
erun FC
fc (IState
-> ElabInfo
-> ElabMode
-> FnOpts
-> Name
-> PTerm
-> Elab' EState ElabResult
build IState
i ElabInfo
winfo ElabMode
ERHS FnOpts
opts Name
fname PTerm
rhs))
                        String -> Name -> Maybe Term -> ElabD () -> ElabD ()
forall aux a.
String -> Name -> Maybe Term -> Elab' aux a -> Elab' aux a
errAt String
"right hand side of " Name
fname (Term -> Maybe Term
forall a. a -> Maybe a
Just Term
clhsty)
                              (FC -> ElabD () -> ElabD ()
forall aux a. FC -> Elab' aux a -> Elab' aux a
erun FC
fc (ElabD () -> ElabD ()) -> ElabD () -> ElabD ()
forall a b. (a -> b) -> a -> b
$ Term -> ElabD ()
forall {n} {aux}. TT n -> StateT (ElabState aux) TC ()
psolve Term
lhs_tm)
                        Term
tt <- Elab' EState Term
forall aux. Elab' aux Term
get_term
                        EState
aux <- Elab' EState EState
forall aux. Elab' aux aux
getAux
                        let (Term
tm, [(Name, (Int, Maybe Name, Term, [Name]))]
ds) = State [(Name, (Int, Maybe Name, Term, [Name]))] Term
-> [(Name, (Int, Maybe Name, Term, [Name]))]
-> (Term, [(Name, (Int, Maybe Name, Term, [Name]))])
forall s a. State s a -> s -> (a, s)
runState (Maybe Name
-> [Name]
-> Context
-> Term
-> State [(Name, (Int, Maybe Name, Term, [Name]))] Term
collectDeferred (Name -> Maybe Name
forall a. a -> Maybe a
Just Name
fname)
                                                     (((Name, PDecl' PTerm) -> Name) -> [(Name, PDecl' PTerm)] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name, PDecl' PTerm) -> Name
forall a b. (a, b) -> a
fst ([(Name, PDecl' PTerm)] -> [Name])
-> [(Name, PDecl' PTerm)] -> [Name]
forall a b. (a -> b) -> a -> b
$ EState -> [(Name, PDecl' PTerm)]
case_decls EState
aux) Context
ctxt Term
tt) []
                        Fails
probs <- Elab' EState Fails
forall aux. Elab' aux Fails
get_probs
                        [Name]
hs <- Elab' EState [Name]
forall aux. Elab' aux [Name]
get_holes
                        (Term, [(Name, (Int, Maybe Name, Term, [Name]))], [Name],
 [PDecl' PTerm], Fails, Context, [RDeclInstructions],
 Set (FC', OutputAnnotation), Int)
-> Elab'
     EState
     (Term, [(Name, (Int, Maybe Name, Term, [Name]))], [Name],
      [PDecl' PTerm], Fails, Context, [RDeclInstructions],
      Set (FC', OutputAnnotation), Int)
forall a. a -> StateT (ElabState EState) TC a
forall (m :: * -> *) a. Monad m => a -> m a
return (Term
tm, [(Name, (Int, Maybe Name, Term, [Name]))]
ds, [Name]
hs, [PDecl' PTerm]
is, Fails
probs, Context
ctxt', [RDeclInstructions]
newDecls, Set (FC', OutputAnnotation)
highlights, Int
newGName))
        Context -> Idris ()
setContext Context
ctxt'
        ElabInfo -> [RDeclInstructions] -> Idris ()
processTacticDecls ElabInfo
info [RDeclInstructions]
newDecls
        Set (FC', OutputAnnotation) -> Idris ()
sendHighlighting Set (FC', OutputAnnotation)
highlights
        (IState -> IState) -> Idris ()
updateIState ((IState -> IState) -> Idris ()) -> (IState -> IState) -> Idris ()
forall a b. (a -> b) -> a -> b
$ \IState
i -> IState
i { idris_name = newGName }

        Bool -> Idris () -> Idris ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
inf (Idris () -> Idris ()) -> Idris () -> Idris ()
forall a b. (a -> b) -> a -> b
$ FC -> [(Term, Term)] -> Idris ()
addTyInfConstraints FC
fc (((Term, Term, Bool, Env, Err, [FailContext], FailAt)
 -> (Term, Term))
-> Fails -> [(Term, Term)]
forall a b. (a -> b) -> [a] -> [b]
map (\(Term
x,Term
y,Bool
_,Env
_,Err
_,[FailContext]
_,FailAt
_) -> (Term
x,Term
y)) Fails
probs)

        Int -> String -> Idris ()
logElab Int
3 String
"DONE CHECK"
        Int -> String -> Idris ()
logElab Int
3 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"---> " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Term -> String
forall a. Show a => a -> String
show Term
rhsElab
        Context
ctxt <- Idris Context
getContext

        let rhs' :: Term
rhs' = Term
rhsElab

        Bool -> Idris () -> Idris ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not ([(Name, (Int, Maybe Name, Term, [Name]))] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(Name, (Int, Maybe Name, Term, [Name]))]
defer)) (Idris () -> Idris ()) -> Idris () -> Idris ()
forall a b. (a -> b) -> a -> b
$ Int -> String -> Idris ()
logElab Int
2 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"DEFERRED " String -> String -> String
forall a. [a] -> [a] -> [a]
++
                    [(Name, Term)] -> String
forall a. Show a => a -> String
show (((Name, (Int, Maybe Name, Term, [Name])) -> (Name, Term))
-> [(Name, (Int, Maybe Name, Term, [Name]))] -> [(Name, Term)]
forall a b. (a -> b) -> [a] -> [b]
map (\ (Name
n, (Int
_,Maybe Name
_,Term
t,[Name]
_)) -> (Name
n, Term
t)) [(Name, (Int, Maybe Name, Term, [Name]))]
defer)

        -- If there's holes, set the metavariables as undefinable
        [(Name, (Int, Maybe Name, Term, [Name]))]
def' <- ElabInfo
-> FC
-> (Name -> Err -> Err)
-> Bool
-> [(Name, (Int, Maybe Name, Term, [Name]))]
-> Idris [(Name, (Int, Maybe Name, Term, [Name]))]
checkDef ElabInfo
info FC
fc (\Name
n -> String -> Name -> Maybe Term -> Err -> Err
forall t. String -> Name -> Maybe t -> Err' t -> Err' t
Elaborating String
"deferred type of " Name
n Maybe Term
forall a. Maybe a
Nothing) ([Name] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Name]
holes) [(Name, (Int, Maybe Name, Term, [Name]))]
defer
        let def'' :: [(Name, (Int, Maybe Name, Term, [Name], Bool, Bool))]
def'' = ((Name, (Int, Maybe Name, Term, [Name]))
 -> (Name, (Int, Maybe Name, Term, [Name], Bool, Bool)))
-> [(Name, (Int, Maybe Name, Term, [Name]))]
-> [(Name, (Int, Maybe Name, Term, [Name], Bool, Bool))]
forall a b. (a -> b) -> [a] -> [b]
map (\(Name
n, (Int
i, Maybe Name
top, Term
t, [Name]
ns)) -> (Name
n, (Int
i, Maybe Name
top, Term
t, [Name]
ns, Bool
False, [Name] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Name]
holes))) [(Name, (Int, Maybe Name, Term, [Name]))]
def'
        [(Name, (Int, Maybe Name, Term, [Name], Bool, Bool))] -> Idris ()
addDeferred [(Name, (Int, Maybe Name, Term, [Name], Bool, Bool))]
def''

        ((Name, (Int, Maybe Name, Term, [Name], Bool, Bool)) -> Idris ())
-> [(Name, (Int, Maybe Name, Term, [Name], Bool, Bool))]
-> Idris ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (\(Name
n, (Int, Maybe Name, Term, [Name], Bool, Bool)
_) -> IBCWrite -> Idris ()
addIBC (Name -> IBCWrite
IBCDef Name
n)) [(Name, (Int, Maybe Name, Term, [Name], Bool, Bool))]
def''

        Bool -> Idris () -> Idris ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not ([(Name, (Int, Maybe Name, Term, [Name]))] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(Name, (Int, Maybe Name, Term, [Name]))]
def')) (Idris () -> Idris ()) -> Idris () -> Idris ()
forall a b. (a -> b) -> a -> b
$ do
           ((FC, Name) -> Idris ()) -> [(FC, Name)] -> Idris ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (FC, Name) -> Idris ()
defer_totcheck (((Name, (Int, Maybe Name, Term, [Name], Bool, Bool)) -> (FC, Name))
-> [(Name, (Int, Maybe Name, Term, [Name], Bool, Bool))]
-> [(FC, Name)]
forall a b. (a -> b) -> [a] -> [b]
map (\(Name, (Int, Maybe Name, Term, [Name], Bool, Bool))
x -> (FC
fc, (Name, (Int, Maybe Name, Term, [Name], Bool, Bool)) -> Name
forall a b. (a, b) -> a
fst (Name, (Int, Maybe Name, Term, [Name], Bool, Bool))
x)) [(Name, (Int, Maybe Name, Term, [Name], Bool, Bool))]
def'')

        -- Now the remaining deferred (i.e. no type declarations) clauses
        -- from the where block

        (PDecl' PTerm -> Idris ()) -> [PDecl' PTerm] -> Idris ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (ElabInfo -> ElabWhat -> ElabInfo -> PDecl' PTerm -> Idris ()
rec_elabDecl ElabInfo
info ElabWhat
EAll ElabInfo
winfo) [PDecl' PTerm]
wafter
        (PDecl' PTerm -> Idris ()) -> [PDecl' PTerm] -> Idris ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (ElabInfo -> FnOpts -> PDecl' PTerm -> Idris ()
elabCaseBlock ElabInfo
winfo FnOpts
opts) [PDecl' PTerm]
is

        Context
ctxt <- Idris Context
getContext
        Int -> String -> Idris ()
logElab Int
5 String
"Rechecking"
        Int -> String -> Idris ()
logElab Int
6 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
" ==> " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Raw -> String
forall a. Show a => a -> String
show (Term -> Raw
forget Term
rhs')

        (Term
crhs, Term
crhsty) -- if there's holes && deferred things, it's okay
                       -- but we'll need to freeze the definition and not
                       -- allow the deferred things to be definable
                       -- (this is just to allow users to inspect intermediate
                       -- things)
             <- if ([Name] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Name]
holes Bool -> Bool -> Bool
|| [(Name, (Int, Maybe Name, Term, [Name]))] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(Name, (Int, Maybe Name, Term, [Name]))]
def') Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
inf
                   then Bool
-> Bool
-> [Name]
-> String
-> FC
-> (Err -> Err)
-> Env
-> Term
-> Idris (Term, Term)
recheckC_borrowing Bool
True (FnOpt
PEGenerated FnOpt -> FnOpts -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` FnOpts
opts)
                                       [Name]
borrowed (ElabInfo -> String
constraintNS ElabInfo
info) FC
fc Err -> Err
forall a. a -> a
forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id [] Term
rhs'
                   else (Term, Term) -> Idris (Term, Term)
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Term
rhs', Term
clhsty)

        Int -> String -> Idris ()
logElab Int
6 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
" ==> " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Env -> Term -> String
forall {a}.
(Show a, Eq a) =>
[(a, RigCount, Binder (TT a))] -> TT a -> String
showEnvDbg [] Term
crhsty String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"   against   " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Env -> Term -> String
forall {a}.
(Show a, Eq a) =>
[(a, RigCount, Binder (TT a))] -> TT a -> String
showEnvDbg [] Term
clhsty

        -- If there's holes, make sure this definition is frozen
        Bool -> Idris () -> Idris ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not ([Name] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Name]
holes)) (Idris () -> Idris ()) -> Idris () -> Idris ()
forall a b. (a -> b) -> a -> b
$ do
           Int -> String -> Idris ()
logElab Int
5 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"Making " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Name -> String
forall a. Show a => a -> String
show Name
fname String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" frozen due to " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [Name] -> String
forall a. Show a => a -> String
show [Name]
holes
           Name -> Accessibility -> Idris ()
setAccessibility Name
fname Accessibility
Frozen

        Context
ctxt <- Idris Context
getContext
        let constv :: Int
constv = Context -> Int
next_tvar Context
ctxt
        Bool
tit <- Idris Bool
typeInType
        case StateT UCs TC () -> UCs -> TC ((), UCs)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
LState.runStateT (Context -> Env -> Term -> Term -> StateT UCs TC ()
convertsC Context
ctxt [] Term
crhsty Term
clhsty) (Int
constv, []) of
            OK (()
_, UCs
cs) -> Bool -> Idris () -> Idris ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (FnOpt
PEGenerated FnOpt -> FnOpts -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` FnOpts
opts Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
tit) (Idris () -> Idris ()) -> Idris () -> Idris ()
forall a b. (a -> b) -> a -> b
$ do
                             FC -> UCs -> Idris ()
addConstraints FC
fc UCs
cs
                             (UConstraint -> Idris ()) -> [UConstraint] -> Idris ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (\UConstraint
c -> IBCWrite -> Idris ()
addIBC (FC -> UConstraint -> IBCWrite
IBCConstraint FC
fc UConstraint
c)) (UCs -> [UConstraint]
forall a b. (a, b) -> b
snd UCs
cs)
                             Int -> String -> Idris ()
logElab Int
6 (String -> Idris ()) -> String -> Idris ()
forall a b. (a -> b) -> a -> b
$ String
"CONSTRAINTS ADDED: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ UCs -> String
forall a. Show a => a -> String
show UCs
cs String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ (Term, Term) -> String
forall a. Show a => a -> String
show (Term
clhsty, Term
crhsty)
                             () -> Idris ()
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
            Error Err
e -> Err -> Idris ()
forall a. Err -> Idris a
ierror (FC -> Err -> Err
forall t. FC -> Err' t -> Err' t
At FC
fc (Bool
-> (Term, Maybe Provenance)
-> (Term, Maybe Provenance)
-> Err
-> [(Name, Term)]
-> Int
-> Err
forall t.
Bool
-> (t, Maybe Provenance)
-> (t, Maybe Provenance)
-> Err' t
-> [(Name, t)]
-> Int
-> Err' t
CantUnify Bool
False (Term
clhsty, Maybe Provenance
forall a. Maybe a
Nothing) (Term
crhsty, Maybe Provenance
forall a. Maybe a
Nothing) Err
e [] Int
0))
        IState
i <- Idris IState
getIState
        FC -> PTerm -> PTerm -> Idris ()
checkInferred FC
fc (IState -> Term -> Bool -> Bool -> PTerm
delab' IState
i Term
crhs Bool
True Bool
True) PTerm
rhs
        -- if the function is declared '%error_reverse',
        -- then we'll try running it in reverse to improve error messages
        -- Also if the type is '%error_reverse' and the LHS is smaller than
        -- the RHS
        let (Term
ret_fam, [Term]
_) = Term -> (Term, [Term])
forall n. TT n -> (TT n, [TT n])
unApply (Term -> Term
forall n. TT n -> TT n
getRetTy Term
crhsty)
        Bool
rev <- case Term
ret_fam of
                    P NameType
_ Name
rfamn Term
_ ->
                        case Name -> Ctxt TypeInfo -> [TypeInfo]
forall a. Name -> Ctxt a -> [a]
lookupCtxt Name
rfamn (IState -> Ctxt TypeInfo
idris_datatypes IState
i) of
                             [TI [Name]
_ Bool
_ DataOpts
dopts [Int]
_ [Name]
_ Bool
_] ->
                                 Bool -> Idris Bool
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return (DataOpt
DataErrRev DataOpt -> DataOpts -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` DataOpts
dopts Bool -> Bool -> Bool
&&
                                         Term -> Int
forall a. Sized a => a -> Int
size Term
clhs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Term -> Int
forall a. Sized a => a -> Int
size Term
crhs)
                             [TypeInfo]
_ -> Bool -> Idris Bool
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
                    Term
_ -> Bool -> Idris Bool
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False

        Bool -> Idris () -> Idris ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool
rev Bool -> Bool -> Bool
|| FnOpt
ErrorReverse FnOpt -> FnOpts -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` FnOpts
opts) (Idris () -> Idris ()) -> Idris () -> Idris ()
forall a b. (a -> b) -> a -> b
$ do
           IBCWrite -> Idris ()
addIBC ((Term, Term) -> IBCWrite
IBCErrRev (Term
crhs, Term
clhs))
           (Term, Term) -> Idris ()
addErrRev (Term
crhs, Term
clhs)
        Bool -> Idris () -> Idris ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool
rev Bool -> Bool -> Bool
|| FnOpt
ErrorReduce FnOpt -> FnOpts -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` FnOpts
opts) (Idris () -> Idris ()) -> Idris () -> Idris ()
forall a b. (a -> b) -> a -> b
$ do
           IBCWrite -> Idris ()
addIBC (Name -> IBCWrite
IBCErrReduce Name
fname)
           Name -> Idris ()
addErrReduce Name
fname
        Idris ()
pop_estack
        (Either Term (Term, Term), PTerm)
-> StateT IState (ExceptT Err IO) (Either Term (Term, Term), PTerm)
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ((Term, Term) -> Either Term (Term, Term)
forall a b. b -> Either a b
Right (Term
clhs, Term
crhs), PTerm
lhs)
  where
    pinfo :: ElabInfo -> [(Name, PTerm)] -> [Name] -> Int -> ElabInfo
    pinfo :: ElabInfo -> [(Name, PTerm)] -> [Name] -> Int -> ElabInfo
pinfo ElabInfo
info [(Name, PTerm)]
ns [Name]
ds Int
i
          = let newps :: [(Name, PTerm)]
newps = ElabInfo -> [(Name, PTerm)]
params ElabInfo
info [(Name, PTerm)] -> [(Name, PTerm)] -> [(Name, PTerm)]
forall a. [a] -> [a] -> [a]
++ [(Name, PTerm)]
ns
                dsParams :: [(Name, [Name])]
dsParams = (Name -> (Name, [Name])) -> [Name] -> [(Name, [Name])]
forall a b. (a -> b) -> [a] -> [b]
map (\Name
n -> (Name
n, ((Name, PTerm) -> Name) -> [(Name, PTerm)] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name, PTerm) -> Name
forall a b. (a, b) -> a
fst [(Name, PTerm)]
newps)) [Name]
ds
                newb :: Ctxt [Name]
newb = [(Name, [Name])] -> Ctxt [Name] -> Ctxt [Name]
forall a. [(Name, a)] -> Ctxt a -> Ctxt a
addAlist [(Name, [Name])]
dsParams (ElabInfo -> Ctxt [Name]
inblock ElabInfo
info) in
                ElabInfo
info { params = newps,
                       inblock = newb,
                       liftname = id -- (\n -> case lookupCtxt n newb of
                                     --      Nothing -> n
                                     --      _ -> MN i (show n)) . l
                     }

    -- Find the variable names which appear under a 'Ownership.Read' so that
    -- we know they can't be used on the RHS
    borrowedNames :: [Name] -> Term -> [Name]
    borrowedNames :: [Name] -> Term -> [Name]
borrowedNames [Name]
env (App AppStatus Name
_ (App AppStatus Name
_ (P NameType
_ (NS (UN Text
lend) [Text
owner]) Term
_) Term
_) Term
arg)
        | Text
owner Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== String -> Text
txt String
"Ownership" Bool -> Bool -> Bool
&&
          (Text
lend Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== String -> Text
txt String
"lend" Bool -> Bool -> Bool
|| Text
lend Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== String -> Text
txt String
"Read") = Term -> [Name]
forall {n}. TT n -> [Name]
getVs Term
arg
       where
         getVs :: TT n -> [Name]
getVs (V Int
i) = [[Name]
env[Name] -> Int -> Name
forall a. HasCallStack => [a] -> Int -> a
!!Int
i]
         getVs (App AppStatus n
_ TT n
f TT n
a) = [Name] -> [Name]
forall a. Eq a => [a] -> [a]
nub ([Name] -> [Name]) -> [Name] -> [Name]
forall a b. (a -> b) -> a -> b
$ TT n -> [Name]
getVs TT n
f [Name] -> [Name] -> [Name]
forall a. [a] -> [a] -> [a]
++ TT n -> [Name]
getVs TT n
a
         getVs TT n
_ = []
    borrowedNames [Name]
env (App AppStatus Name
_ Term
f Term
a) = [Name] -> [Name]
forall a. Eq a => [a] -> [a]
nub ([Name] -> [Name]) -> [Name] -> [Name]
forall a b. (a -> b) -> a -> b
$ [Name] -> Term -> [Name]
borrowedNames [Name]
env Term
f [Name] -> [Name] -> [Name]
forall a. [a] -> [a] -> [a]
++ [Name] -> Term -> [Name]
borrowedNames [Name]
env Term
a
    borrowedNames [Name]
env (Bind Name
n Binder Term
b Term
sc) = [Name] -> [Name]
forall a. Eq a => [a] -> [a]
nub ([Name] -> [Name]) -> [Name] -> [Name]
forall a b. (a -> b) -> a -> b
$ Binder Term -> [Name]
borrowedB Binder Term
b [Name] -> [Name] -> [Name]
forall a. [a] -> [a] -> [a]
++ [Name] -> Term -> [Name]
borrowedNames (Name
nName -> [Name] -> [Name]
forall a. a -> [a] -> [a]
:[Name]
env) Term
sc
       where borrowedB :: Binder Term -> [Name]
borrowedB (Let RigCount
_ Term
t Term
v) = [Name] -> [Name]
forall a. Eq a => [a] -> [a]
nub ([Name] -> [Name]) -> [Name] -> [Name]
forall a b. (a -> b) -> a -> b
$ [Name] -> Term -> [Name]
borrowedNames [Name]
env Term
t [Name] -> [Name] -> [Name]
forall a. [a] -> [a] -> [a]
++ [Name] -> Term -> [Name]
borrowedNames [Name]
env Term
v
             borrowedB Binder Term
b = [Name] -> Term -> [Name]
borrowedNames [Name]
env (Binder Term -> Term
forall b. Binder b -> b
binderTy Binder Term
b)
    borrowedNames [Name]
_ Term
_ = []

    mkLHSapp :: PTerm -> PTerm
mkLHSapp t :: PTerm
t@(PRef FC
_ [FC]
_ Name
_) = FC -> PTerm -> [PArg] -> PTerm
PApp FC
fc PTerm
t []
    mkLHSapp PTerm
t = PTerm
t

    decorate :: Name -> Name
decorate (NS Name
x [Text]
ns)
       = Name -> [Text] -> Name
NS (SpecialName -> Name
SN (Int -> Name -> Name -> SpecialName
WhereN Int
cnum Name
fname Name
x)) [Text]
ns
    decorate Name
x
       = SpecialName -> Name
SN (Int -> Name -> Name -> SpecialName
WhereN Int
cnum Name
fname Name
x)

    sepBlocks :: [PDecl' t] -> ([PDecl' t], [PDecl' t])
sepBlocks [PDecl' t]
bs = [Name] -> [PDecl' t] -> ([PDecl' t], [PDecl' t])
forall {t}. [Name] -> [PDecl' t] -> ([PDecl' t], [PDecl' t])
sepBlocks' [] [PDecl' t]
bs where
      sepBlocks' :: [Name] -> [PDecl' t] -> ([PDecl' t], [PDecl' t])
sepBlocks' [Name]
ns (d :: PDecl' t
d@(PTy Docstring (Either Err t)
_ [(Name, Docstring (Either Err t))]
_ SyntaxInfo
_ FC
_ FnOpts
_ Name
n FC
_ t
t) : [PDecl' t]
bs)
            = let ([PDecl' t]
bf, [PDecl' t]
af) = [Name] -> [PDecl' t] -> ([PDecl' t], [PDecl' t])
sepBlocks' (Name
n Name -> [Name] -> [Name]
forall a. a -> [a] -> [a]
: [Name]
ns) [PDecl' t]
bs in
                  (PDecl' t
d PDecl' t -> [PDecl' t] -> [PDecl' t]
forall a. a -> [a] -> [a]
: [PDecl' t]
bf, [PDecl' t]
af)
      sepBlocks' [Name]
ns (d :: PDecl' t
d@(PClauses FC
_ FnOpts
_ Name
n [PClause' t]
_) : [PDecl' t]
bs)
         | Bool -> Bool
not (Name
n Name -> [Name] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Name]
ns) = let ([PDecl' t]
bf, [PDecl' t]
af) = [Name] -> [PDecl' t] -> ([PDecl' t], [PDecl' t])
sepBlocks' [Name]
ns [PDecl' t]
bs in
                                   ([PDecl' t]
bf, PDecl' t
d PDecl' t -> [PDecl' t] -> [PDecl' t]
forall a. a -> [a] -> [a]
: [PDecl' t]
af)
      sepBlocks' [Name]
ns (PDecl' t
b : [PDecl' t]
bs) = let ([PDecl' t]
bf, [PDecl' t]
af) = [Name] -> [PDecl' t] -> ([PDecl' t], [PDecl' t])
sepBlocks' [Name]
ns [PDecl' t]
bs in
                                   (PDecl' t
b PDecl' t -> [PDecl' t] -> [PDecl' t]
forall a. a -> [a] -> [a]
: [PDecl' t]
bf, [PDecl' t]
af)
      sepBlocks' [Name]
ns [] = ([], [])

    -- term is not within "with" block
    isOutsideWith :: PTerm -> Bool
    isOutsideWith :: PTerm -> Bool
isOutsideWith (PApp FC
_ (PRef FC
_ [FC]
_ (SN (WithN Int
_ Name
_))) [PArg]
_) = Bool
False
    isOutsideWith PTerm
_ = Bool
True

elabClause ElabInfo
info FnOpts
opts (Int
_, PWith FC
fc Name
fname PTerm
lhs_in [PTerm]
withs PTerm
wval_in Maybe (Name, FC)
pn_in [PDecl' PTerm]
withblock)
   = do Context
ctxt <- Idris Context
getContext
        -- Build the LHS as an "Infer", and pull out its type and
        -- pattern bindings
        IState
i <- Idris IState
getIState
        -- get the parameters first, to pass through to any where block
        let fn_ty :: Term
fn_ty = case Name -> Context -> [Term]
lookupTy Name
fname Context
ctxt of
                         [Term
t] -> Term
t
                         [Term]
_ -> String -> Term
forall a. HasCallStack => String -> a
error String
"Can't happen (elabClause function type)"
        let fn_is :: [PArg]
fn_is = case Name -> Ctxt [PArg] -> [[PArg]]
forall a. Name -> Ctxt a -> [a]
lookupCtxt Name
fname (IState -> Ctxt [PArg]
idris_implicits IState
i) of
                         [[PArg]
t] -> [PArg]
t
                         [[PArg]]
_ -> []
        let params :: [Name]
params = IState -> [Name] -> [PArg] -> Term -> [Name]
getParamsInType IState
i [] [PArg]
fn_is (Context -> Env -> Term -> Term
normalise Context
ctxt [] Term
fn_ty)
        let lhs :: PTerm
lhs = IState -> PTerm -> PTerm
stripLinear IState
i (PTerm -> PTerm) -> PTerm -> PTerm
forall a b. (a -> b) -> a -> b
$ IState -> PTerm -> PTerm
stripUnmatchable IState
i (PTerm -> PTerm) -> PTerm -> PTerm
forall a b. (a -> b) -> a -> b
$
                   IState -> [Name] -> Term -> [Name] -> PTerm -> PTerm
propagateParams IState
i [Name]
params Term
fn_ty (PTerm -> [Name]
allNamesIn PTerm
lhs_in)
                    (IState -> PTerm -> PTerm
addImplPat IState
i PTerm
lhs_in)
        Int -> String -> Idris ()
logElab Int
2 (String
"LHS: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ PTerm -> String
forall a. Show a => a -> String
show PTerm
lhs)
        (ElabResult Term
lhs' [(Name, (Int, Maybe Name, Term, [Name]))]
dlhs [] Context
ctxt' [RDeclInstructions]
newDecls Set (FC', OutputAnnotation)
highlights Int
newGName, String
_) <-
            TC (ElabResult, String) -> Idris (ElabResult, String)
forall a. TC a -> Idris a
tclift (TC (ElabResult, String) -> Idris (ElabResult, String))
-> TC (ElabResult, String) -> Idris (ElabResult, String)
forall a b. (a -> b) -> a -> b
$ String
-> Context
-> Ctxt TypeInfo
-> Int
-> Name
-> Term
-> EState
-> Elab' EState ElabResult
-> TC (ElabResult, String)
forall aux a.
String
-> Context
-> Ctxt TypeInfo
-> Int
-> Name
-> Term
-> aux
-> Elab' aux a
-> TC (a, String)
elaborate (ElabInfo -> String
constraintNS ElabInfo
info) Context
ctxt (IState -> Ctxt TypeInfo
idris_datatypes IState
i) (IState -> Int
idris_name IState
i) (Int -> String -> Name
sMN Int
0 String
"patLHS") Term
infP EState
initEState
              (String
-> Name
-> Maybe Term
-> Elab' EState ElabResult
-> Elab' EState ElabResult
forall aux a.
String -> Name -> Maybe Term -> Elab' aux a -> Elab' aux a
errAt String
"left hand side of with in " Name
fname Maybe Term
forall a. Maybe a
Nothing
                (FC -> Elab' EState ElabResult -> Elab' EState ElabResult
forall aux a. FC -> Elab' aux a -> Elab' aux a
erun (FC -> Maybe FC -> FC
forall a. a -> Maybe a -> a
fromMaybe FC
NoFC (Maybe FC -> FC) -> Maybe FC -> FC
forall a b. (a -> b) -> a -> b
$ PTerm -> Maybe FC
highestFC PTerm
lhs_in)
                      (IState
-> ElabInfo
-> ElabMode
-> FnOpts
-> Name
-> [Name]
-> PTerm
-> Elab' EState ElabResult
buildTC IState
i ElabInfo
info ElabMode
ELHS FnOpts
opts Name
fname
                                  (PTerm -> [Name]
allNamesIn PTerm
lhs_in)
                                  (PTerm -> PTerm
infTerm PTerm
lhs))) )
        Context -> Idris ()
setContext Context
ctxt'
        ElabInfo -> [RDeclInstructions] -> Idris ()
processTacticDecls ElabInfo
info [RDeclInstructions]
newDecls
        Set (FC', OutputAnnotation) -> Idris ()
sendHighlighting Set (FC', OutputAnnotation)
highlights
        (IState -> IState) -> Idris ()
updateIState ((IState -> IState) -> Idris ()) -> (IState -> IState) -> Idris ()
forall a b. (a -> b) -> a -> b
$ \IState
i -> IState
i { idris_name = newGName }

        Context
ctxt <- Idris Context
getContext
        let lhs_tm :: Term
lhs_tm = Term -> Term
orderPats (Term -> Term
getInferTerm Term
lhs')
        let lhs_ty :: Term
lhs_ty = Term -> Term
getInferType Term
lhs'
        let ret_ty :: Term
ret_ty = Term -> Term
forall n. TT n -> TT n
getRetTy (Term -> Term
forall n. TT n -> TT n
explicitNames (Context -> Env -> Term -> Term
normalise Context
ctxt [] Term
lhs_ty))
        let static_names :: [Name]
static_names = IState -> Term -> [Name]
getStaticNames IState
i Term
lhs_tm
        Int -> String -> Idris ()
logElab Int
5 (Term -> String
forall a. Show a => a -> String
show Term
lhs_tm String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ [Name] -> String
forall a. Show a => a -> String
show [Name]
static_names)
        (Term
clhs_c, Term
clhsty) <- String -> FC -> (Err -> Err) -> Env -> Term -> Idris (Term, Term)
recheckC (ElabInfo -> String
constraintNS ElabInfo
info) FC
fc Err -> Err
forall a. a -> a
forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id [] Term
lhs_tm
        let clhs :: Term
clhs = Context -> Env -> Term -> Term
normalise Context
ctxt [] Term
clhs_c

        Int -> String -> Idris ()
logElab Int
5 (String
"Checked " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Term -> String
forall a. Show a => a -> String
show Term
clhs)
        let bargs :: [(Name, Term)]
bargs = Term -> [(Name, Term)]
forall n. TT n -> [(n, TT n)]
getPBtys (Term -> Term
forall n. TT n -> TT n
explicitNames (Context -> Env -> Term -> Term
normalise Context
ctxt [] Term
lhs_tm))
        PTerm
wval <- case PTerm
wval_in of
                     PTerm
Placeholder -> Err -> StateT IState (ExceptT Err IO) PTerm
forall a. Err -> Idris a
ierror (Err -> StateT IState (ExceptT Err IO) PTerm)
-> Err -> StateT IState (ExceptT Err IO) PTerm
forall a b. (a -> b) -> a -> b
$ FC -> Err -> Err
forall t. FC -> Err' t -> Err' t
At FC
fc (Err -> Err) -> Err -> Err
forall a b. (a -> b) -> a -> b
$
                          String -> Err
forall t. String -> Err' t
Msg String
"No expression for the with block to inspect.\nYou need to replace the _ with an expression."
                     PTerm
_ -> PTerm -> StateT IState (ExceptT Err IO) PTerm
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return (PTerm -> StateT IState (ExceptT Err IO) PTerm)
-> PTerm -> StateT IState (ExceptT Err IO) PTerm
forall a b. (a -> b) -> a -> b
$
                            ElabInfo -> PTerm -> PTerm
rhs_trans ElabInfo
info (PTerm -> PTerm) -> PTerm -> PTerm
forall a b. (a -> b) -> a -> b
$
                              IState -> [Name] -> PTerm -> PTerm
addImplBound IState
i (((Name, Term) -> Name) -> [(Name, Term)] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name, Term) -> Name
forall a b. (a, b) -> a
fst [(Name, Term)]
bargs) PTerm
wval_in
        Int -> String -> Idris ()
logElab Int
5 (String
"Checking " String -> String -> String
forall a. [a] -> [a] -> [a]
++ PTerm -> String
showTmImpls PTerm
wval)
        -- Elaborate wval in this context
        ((Term
wvalElab, [(Name, (Int, Maybe Name, Term, [Name]))]
defer, [PDecl' PTerm]
is, Context
ctxt', [RDeclInstructions]
newDecls, Set (FC', OutputAnnotation)
highlights, Int
newGName), String
_) <-
            TC
  ((Term, [(Name, (Int, Maybe Name, Term, [Name]))], [PDecl' PTerm],
    Context, [RDeclInstructions], Set (FC', OutputAnnotation), Int),
   String)
-> Idris
     ((Term, [(Name, (Int, Maybe Name, Term, [Name]))], [PDecl' PTerm],
       Context, [RDeclInstructions], Set (FC', OutputAnnotation), Int),
      String)
forall a. TC a -> Idris a
tclift (TC
   ((Term, [(Name, (Int, Maybe Name, Term, [Name]))], [PDecl' PTerm],
     Context, [RDeclInstructions], Set (FC', OutputAnnotation), Int),
    String)
 -> Idris
      ((Term, [(Name, (Int, Maybe Name, Term, [Name]))], [PDecl' PTerm],
        Context, [RDeclInstructions], Set (FC', OutputAnnotation), Int),
       String))
-> TC
     ((Term, [(Name, (Int, Maybe Name, Term, [Name]))], [PDecl' PTerm],
       Context, [RDeclInstructions], Set (FC', OutputAnnotation), Int),
      String)
-> Idris
     ((Term, [(Name, (Int, Maybe Name, Term, [Name]))], [PDecl' PTerm],
       Context, [RDeclInstructions], Set (FC', OutputAnnotation), Int),
      String)
forall a b. (a -> b) -> a -> b
$ String
-> Context
-> Ctxt TypeInfo
-> Int
-> Name
-> Term
-> EState
-> Elab'
     EState
     (Term, [(Name, (Int, Maybe Name, Term, [Name]))], [PDecl' PTerm],
      Context, [RDeclInstructions], Set (FC', OutputAnnotation), Int)
-> TC
     ((Term, [(Name, (Int, Maybe Name, Term, [Name]))], [PDecl' PTerm],
       Context, [RDeclInstructions], Set (FC', OutputAnnotation), Int),
      String)
forall aux a.
String
-> Context
-> Ctxt TypeInfo
-> Int
-> Name
-> Term
-> aux
-> Elab' aux a
-> TC (a, String)
elaborate (ElabInfo -> String
constraintNS ElabInfo
info) Context
ctxt (IState -> Ctxt TypeInfo
idris_datatypes IState
i) (IState -> Int
idris_name IState
i) (Int -> String -> Name
sMN Int
0 String
"withRHS")
                        ((Term -> Binder Term) -> [(Name, Term)] -> Term -> Term
forall n. (TT n -> Binder (TT n)) -> [(n, TT n)] -> TT n -> TT n
bindTyArgs Term -> Binder Term
forall b. b -> Binder b
PVTy [(Name, Term)]
bargs Term
infP) EState
initEState
                        (do IState -> Term -> ElabD ()
pbinds IState
i Term
lhs_tm
                            -- proof search can use explicitly written names
                            (Name -> ElabD ()) -> [Name] -> ElabD ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Name -> ElabD ()
forall aux. Name -> Elab' aux ()
addPSname (PTerm -> [Name]
allNamesIn PTerm
lhs_in)
                            ElabD ()
forall aux. Elab' aux ()
setNextName
                            -- TODO: may want where here - see winfo abpve
                            (ElabResult Term
_ [(Name, (Int, Maybe Name, Term, [Name]))]
d [PDecl' PTerm]
is Context
ctxt' [RDeclInstructions]
newDecls Set (FC', OutputAnnotation)
highlights Int
newGName) <- String
-> Name
-> Maybe Term
-> Elab' EState ElabResult
-> Elab' EState ElabResult
forall aux a.
String -> Name -> Maybe Term -> Elab' aux a -> Elab' aux a
errAt String
"with value in " Name
fname Maybe Term
forall a. Maybe a
Nothing
                              (FC -> Elab' EState ElabResult -> Elab' EState ElabResult
forall aux a. FC -> Elab' aux a -> Elab' aux a
erun FC
fc (IState
-> ElabInfo
-> ElabMode
-> FnOpts
-> Name
-> PTerm
-> Elab' EState ElabResult
build IState
i ElabInfo
info ElabMode
ERHS FnOpts
opts Name
fname (PTerm -> PTerm
infTerm PTerm
wval)))
                            FC -> ElabD () -> ElabD ()
forall aux a. FC -> Elab' aux a -> Elab' aux a
erun FC
fc (ElabD () -> ElabD ()) -> ElabD () -> ElabD ()
forall a b. (a -> b) -> a -> b
$ Term -> ElabD ()
forall {n} {aux}. TT n -> StateT (ElabState aux) TC ()
psolve Term
lhs_tm
                            Term
tt <- Elab' EState Term
forall aux. Elab' aux Term
get_term
                            (Term, [(Name, (Int, Maybe Name, Term, [Name]))], [PDecl' PTerm],
 Context, [RDeclInstructions], Set (FC', OutputAnnotation), Int)
-> Elab'
     EState
     (Term, [(Name, (Int, Maybe Name, Term, [Name]))], [PDecl' PTerm],
      Context, [RDeclInstructions], Set (FC', OutputAnnotation), Int)
forall a. a -> StateT (ElabState EState) TC a
forall (m :: * -> *) a. Monad m => a -> m a
return (Term
tt, [(Name, (Int, Maybe Name, Term, [Name]))]
d, [PDecl' PTerm]
is, Context
ctxt', [RDeclInstructions]
newDecls, Set (FC', OutputAnnotation)
highlights, Int
newGName))
        Context -> Idris ()
setContext Context
ctxt'
        ElabInfo -> [RDeclInstructions] -> Idris ()
processTacticDecls ElabInfo
info [RDeclInstructions]
newDecls
        Set (FC', OutputAnnotation) -> Idris ()
sendHighlighting Set (FC', OutputAnnotation)
highlights
        (IState -> IState) -> Idris ()
updateIState ((IState -> IState) -> Idris ()) -> (IState -> IState) -> Idris ()
forall a b. (a -> b) -> a -> b
$ \IState
i -> IState
i { idris_name = newGName }

        [(Name, (Int, Maybe Name, Term, [Name]))]
def' <- ElabInfo
-> FC
-> (Name -> Err -> Err)
-> Bool
-> [(Name, (Int, Maybe Name, Term, [Name]))]
-> Idris [(Name, (Int, Maybe Name, Term, [Name]))]
checkDef ElabInfo
info FC
fc Name -> Err -> Err
iderr Bool
True [(Name, (Int, Maybe Name, Term, [Name]))]
defer
        let def'' :: [(Name, (Int, Maybe Name, Term, [Name], Bool, Bool))]
def'' = ((Name, (Int, Maybe Name, Term, [Name]))
 -> (Name, (Int, Maybe Name, Term, [Name], Bool, Bool)))
-> [(Name, (Int, Maybe Name, Term, [Name]))]
-> [(Name, (Int, Maybe Name, Term, [Name], Bool, Bool))]
forall a b. (a -> b) -> [a] -> [b]
map (\(Name
n, (Int
i, Maybe Name
top, Term
t, [Name]
ns)) -> (Name
n, (Int
i, Maybe Name
top, Term
t, [Name]
ns, Bool
False, Bool
True))) [(Name, (Int, Maybe Name, Term, [Name]))]
def'
        [(Name, (Int, Maybe Name, Term, [Name], Bool, Bool))] -> Idris ()
addDeferred [(Name, (Int, Maybe Name, Term, [Name], Bool, Bool))]
def''
        (PDecl' PTerm -> Idris ()) -> [PDecl' PTerm] -> Idris ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (ElabInfo -> FnOpts -> PDecl' PTerm -> Idris ()
elabCaseBlock ElabInfo
info FnOpts
opts) [PDecl' PTerm]
is

        let wval' :: Term
wval' = Term
wvalElab
        Int -> String -> Idris ()
logElab Int
5 (String
"Checked wval " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Term -> String
forall a. Show a => a -> String
show Term
wval')

        Context
ctxt <- Idris Context
getContext
        (Term
cwval, Term
cwvalty) <- String -> FC -> (Err -> Err) -> Env -> Term -> Idris (Term, Term)
recheckC (ElabInfo -> String
constraintNS ElabInfo
info) FC
fc Err -> Err
forall a. a -> a
forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id [] (Term -> Term
getInferTerm Term
wval')
        let cwvaltyN :: Term
cwvaltyN = Term -> Term
forall n. TT n -> TT n
explicitNames (Context -> Env -> Term -> Term
normalise Context
ctxt [] Term
cwvalty)
        let cwvalN :: Term
cwvalN = Term -> Term
forall n. TT n -> TT n
explicitNames (Context -> Env -> Term -> Term
normalise Context
ctxt [] Term
cwval)
        Int -> String -> Idris ()
logElab Int
3 (String
"With type " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Term -> String
forall a. Show a => a -> String
show Term
cwvalty String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\nRet type " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Term -> String
forall a. Show a => a -> String
show Term
ret_ty)
        -- We're going to assume the with type is not a function shortly,
        -- so report an error if it is (you can't match on a function anyway
        -- so this doesn't lose anything)
        case Term -> [(Name, Term)]
forall n. TT n -> [(n, TT n)]
getArgTys Term
cwvaltyN of
             [] -> () -> Idris ()
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
             ((Name, Term)
_:[(Name, Term)]
_) -> Err -> Idris ()
forall a. Err -> Idris a
ierror (Err -> Idris ()) -> Err -> Idris ()
forall a b. (a -> b) -> a -> b
$ FC -> Err -> Err
forall t. FC -> Err' t -> Err' t
At FC
fc (Term -> Err
forall t. t -> Err' t
WithFnType Term
cwvalty)

        let pvars :: [Name]
pvars = ((Name, Term) -> Name) -> [(Name, Term)] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name, Term) -> Name
forall a b. (a, b) -> a
fst (Term -> [(Name, Term)]
forall n. TT n -> [(n, TT n)]
getPBtys Term
cwvalty)
        -- we need the unelaborated term to get the names it depends on
        -- rather than a de Bruijn index.
        let pdeps :: [Name]
pdeps = [Name] -> IState -> PTerm -> [Name]
usedNamesIn [Name]
pvars IState
i (IState -> Term -> PTerm
delab IState
i Term
cwvalty)
        let ([(Name, Term)]
bargs_pre, [(Name, Term)]
bargs_post) = [Name]
-> [(Name, Term)]
-> [(Name, Term)]
-> ([(Name, Term)], [(Name, Term)])
forall {a} {b}.
Eq a =>
[a] -> [(a, b)] -> [(a, b)] -> ([(a, b)], [(a, b)])
split [Name]
pdeps [(Name, Term)]
bargs []

        let mpn :: Maybe Name
mpn = case Maybe (Name, FC)
pn_in of
                       Maybe (Name, FC)
Nothing -> Maybe Name
forall a. Maybe a
Nothing
                       Just (Name
n, FC
nfc) -> Name -> Maybe Name
forall a. a -> Maybe a
Just (Name -> [Name] -> Name
uniqueName Name
n (((Name, Term) -> Name) -> [(Name, Term)] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name, Term) -> Name
forall a b. (a, b) -> a
fst [(Name, Term)]
bargs))

        -- Highlight explicit proofs
        Set (FC', OutputAnnotation) -> Idris ()
sendHighlighting (Set (FC', OutputAnnotation) -> Idris ())
-> Set (FC', OutputAnnotation) -> Idris ()
forall a b. (a -> b) -> a -> b
$ [(FC', OutputAnnotation)] -> Set (FC', OutputAnnotation)
forall a. Ord a => [a] -> Set a
S.fromList [(FC -> FC'
FC' FC
fc, Name -> Bool -> OutputAnnotation
AnnBoundName Name
n Bool
False) | (Name
n, FC
fc) <- Maybe (Name, FC) -> [(Name, FC)]
forall a. Maybe a -> [a]
maybeToList Maybe (Name, FC)
pn_in]

        Int -> String -> Idris ()
logElab Int
10 (String
"With type " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Term -> String
forall a. Show a => a -> String
show (Term -> Term
forall n. TT n -> TT n
getRetTy Term
cwvaltyN) String -> String -> String
forall a. [a] -> [a] -> [a]
++
                  String
" depends on " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [Name] -> String
forall a. Show a => a -> String
show [Name]
pdeps String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" from " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [Name] -> String
forall a. Show a => a -> String
show [Name]
pvars)
        Int -> String -> Idris ()
logElab Int
10 (String
"Pre " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [(Name, Term)] -> String
forall a. Show a => a -> String
show [(Name, Term)]
bargs_pre String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\nPost " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [(Name, Term)] -> String
forall a. Show a => a -> String
show [(Name, Term)]
bargs_post)
        Int
windex <- Idris Int
getName
        -- build a type declaration for the new function:
        -- (ps : Xs) -> (withval : cwvalty) -> (ps' : Xs') -> ret_ty
        let wargval :: Term
wargval = Term -> Term
forall n. TT n -> TT n
getRetTy Term
cwvalN
        let wargtype :: Term
wargtype = Term -> Term
forall n. TT n -> TT n
getRetTy Term
cwvaltyN
        let wargname :: Name
wargname = Int -> String -> Name
sMN Int
windex String
"warg"

        Int -> String -> Idris ()
logElab Int
5 (String
"Abstract over " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Term -> String
forall a. Show a => a -> String
show Term
wargval String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" in " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Term -> String
forall a. Show a => a -> String
show Term
wargtype)
        let wtype :: Term
wtype = (Term -> Binder Term) -> [(Name, Term)] -> Term -> Term
forall n. (TT n -> Binder (TT n)) -> [(n, TT n)] -> TT n -> TT n
bindTyArgs ((Term -> Term -> Binder Term) -> Term -> Term -> Binder Term
forall a b c. (a -> b -> c) -> b -> a -> c
flip (RigCount -> Maybe ImplicitInfo -> Term -> Term -> Binder Term
forall b. RigCount -> Maybe ImplicitInfo -> b -> b -> Binder b
Pi RigCount
RigW Maybe ImplicitInfo
forall a. Maybe a
Nothing) (UExp -> Term
forall n. UExp -> TT n
TType (String -> Int -> UExp
UVar [] Int
0))) ([(Name, Term)]
bargs_pre [(Name, Term)] -> [(Name, Term)] -> [(Name, Term)]
forall a. [a] -> [a] -> [a]
++
                     (Name
wargname, Term
wargtype) (Name, Term) -> [(Name, Term)] -> [(Name, Term)]
forall a. a -> [a] -> [a]
:
                     ((Name, Term) -> (Name, Term)) -> [(Name, Term)] -> [(Name, Term)]
forall a b. (a -> b) -> [a] -> [b]
map (Name -> Term -> Term -> (Name, Term) -> (Name, Term)
forall {n} {a}. Eq n => n -> TT n -> TT n -> (a, TT n) -> (a, TT n)
abstract Name
wargname Term
wargval Term
wargtype) [(Name, Term)]
bargs_post [(Name, Term)] -> [(Name, Term)] -> [(Name, Term)]
forall a. [a] -> [a] -> [a]
++
                     case Maybe Name
mpn of
                          Just Name
pn -> [(Name
pn, Term -> [Term] -> Term
forall n. TT n -> [TT n] -> TT n
mkApp (NameType -> Name -> Term -> Term
forall n. NameType -> n -> TT n -> TT n
P NameType
Ref Name
eqTy Term
forall n. TT n
Erased)
                                       [Term
wargtype, Term
wargtype,
                                         NameType -> Name -> Term -> Term
forall n. NameType -> n -> TT n -> TT n
P NameType
Bound Name
wargname Term
forall n. TT n
Erased, Term
wargval])]
                          Maybe Name
Nothing -> [])
                     (Term -> Term -> Term -> Term
forall n. Eq n => TT n -> TT n -> TT n -> TT n
substTerm Term
wargval (NameType -> Name -> Term -> Term
forall n. NameType -> n -> TT n -> TT n
P NameType
Bound Name
wargname Term
wargtype) Term
ret_ty)
        Int -> String -> Idris ()
logElab Int
3 (String
"New function type " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Term -> String
forall a. Show a => a -> String
show Term
wtype)
        let wname :: Name
wname = SpecialName -> Name
SN (Int -> Name -> SpecialName
WithN Int
windex Name
fname)

        let imps :: [PArg]
imps = Term -> [PArg]
forall {n}. TT n -> [PArg]
getImps Term
wtype -- add to implicits context
        IState -> Idris ()
putIState (IState
i { idris_implicits = addDef wname imps (idris_implicits i) })
        let statics :: [Bool]
statics = [Name] -> Term -> [Bool]
getStatics [Name]
static_names Term
wtype
        Int -> String -> Idris ()
logElab Int
5 (String
"Static positions " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [Bool] -> String
forall a. Show a => a -> String
show [Bool]
statics)
        IState
i <- Idris IState
getIState
        IState -> Idris ()
putIState (IState
i { idris_statics = addDef wname statics (idris_statics i) })

        IBCWrite -> Idris ()
addIBC (Name -> IBCWrite
IBCDef Name
wname)
        IBCWrite -> Idris ()
addIBC (Name -> IBCWrite
IBCImp Name
wname)
        IBCWrite -> Idris ()
addIBC (Name -> IBCWrite
IBCStatic Name
wname)

        [(Name, (Int, Maybe Name, Term, [Name]))]
def' <- ElabInfo
-> FC
-> (Name -> Err -> Err)
-> Bool
-> [(Name, (Int, Maybe Name, Term, [Name]))]
-> Idris [(Name, (Int, Maybe Name, Term, [Name]))]
checkDef ElabInfo
info FC
fc Name -> Err -> Err
iderr Bool
True [(Name
wname, (-Int
1, Maybe Name
forall a. Maybe a
Nothing, Term
wtype, []))]
        let def'' :: [(Name, (Int, Maybe Name, Term, [Name], Bool, Bool))]
def'' = ((Name, (Int, Maybe Name, Term, [Name]))
 -> (Name, (Int, Maybe Name, Term, [Name], Bool, Bool)))
-> [(Name, (Int, Maybe Name, Term, [Name]))]
-> [(Name, (Int, Maybe Name, Term, [Name], Bool, Bool))]
forall a b. (a -> b) -> [a] -> [b]
map (\(Name
n, (Int
i, Maybe Name
top, Term
t, [Name]
ns)) -> (Name
n, (Int
i, Maybe Name
top, Term
t, [Name]
ns, Bool
False, Bool
True))) [(Name, (Int, Maybe Name, Term, [Name]))]
def'
        [(Name, (Int, Maybe Name, Term, [Name], Bool, Bool))] -> Idris ()
addDeferred [(Name, (Int, Maybe Name, Term, [Name], Bool, Bool))]
def''

        -- in the subdecls, lhs becomes:
        --         fname  pats | wpat [rest]
        --    ==>  fname' ps   wpat [rest], match pats against toplevel for ps
        [PDecl' PTerm]
wb <- (PDecl' PTerm -> StateT IState (ExceptT Err IO) (PDecl' PTerm))
-> [PDecl' PTerm] -> StateT IState (ExceptT Err IO) [PDecl' PTerm]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Maybe Name
-> Name
-> PTerm
-> [Name]
-> [Name]
-> PDecl' PTerm
-> StateT IState (ExceptT Err IO) (PDecl' PTerm)
mkAuxC Maybe Name
mpn Name
wname PTerm
lhs (((Name, Term) -> Name) -> [(Name, Term)] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name, Term) -> Name
forall a b. (a, b) -> a
fst [(Name, Term)]
bargs_pre) (((Name, Term) -> Name) -> [(Name, Term)] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name, Term) -> Name
forall a b. (a, b) -> a
fst [(Name, Term)]
bargs_post))
                       [PDecl' PTerm]
withblock
        Int -> String -> Idris ()
logElab Int
3 (String
"with block " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [PDecl' PTerm] -> String
forall a. Show a => a -> String
show [PDecl' PTerm]
wb)
        Name -> FnOpts -> Idris ()
setFlags Name
wname [FnOpt
Inlinable]
        Bool -> Idris () -> Idris ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (FnOpt
AssertTotal FnOpt -> FnOpts -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` FnOpts
opts) (Idris () -> Idris ()) -> Idris () -> Idris ()
forall a b. (a -> b) -> a -> b
$
           Name -> FnOpts -> Idris ()
setFlags Name
wname [FnOpt
Inlinable, FnOpt
AssertTotal]
        IState
i <- Idris IState
getIState
        let rhstrans' :: PTerm -> PTerm
rhstrans' = IState
-> Maybe Name
-> Name
-> PTerm
-> [Name]
-> [Name]
-> PTerm
-> PTerm
updateWithTerm IState
i Maybe Name
mpn Name
wname PTerm
lhs (((Name, Term) -> Name) -> [(Name, Term)] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name, Term) -> Name
forall a b. (a, b) -> a
fst [(Name, Term)]
bargs_pre) (((Name, Term) -> Name) -> [(Name, Term)] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name, Term) -> Name
forall a b. (a, b) -> a
fst ([(Name, Term)]
bargs_post))
                             (PTerm -> PTerm) -> (PTerm -> PTerm) -> PTerm -> PTerm
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ElabInfo -> PTerm -> PTerm
rhs_trans ElabInfo
info
        (PDecl' PTerm -> Idris ()) -> [PDecl' PTerm] -> Idris ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (ElabInfo -> ElabWhat -> ElabInfo -> PDecl' PTerm -> Idris ()
rec_elabDecl ElabInfo
info ElabWhat
EAll (ElabInfo
info { rhs_trans = rhstrans' })) [PDecl' PTerm]
wb

        -- rhs becomes: fname' ps_pre wval ps_post Refl
        let rhs :: PTerm
rhs = FC -> PTerm -> [PArg] -> PTerm
PApp FC
fc (FC -> [FC] -> Name -> PTerm
PRef FC
fc [] Name
wname)
                    (((Name, Term) -> PArg) -> [(Name, Term)] -> [PArg]
forall a b. (a -> b) -> [a] -> [b]
map (PTerm -> PArg
forall {t}. t -> PArg' t
pexp (PTerm -> PArg) -> ((Name, Term) -> PTerm) -> (Name, Term) -> PArg
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (FC -> [FC] -> Name -> PTerm
PRef FC
fc []) (Name -> PTerm) -> ((Name, Term) -> Name) -> (Name, Term) -> PTerm
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Name, Term) -> Name
forall a b. (a, b) -> a
fst) [(Name, Term)]
bargs_pre [PArg] -> [PArg] -> [PArg]
forall a. [a] -> [a] -> [a]
++
                        PTerm -> PArg
forall {t}. t -> PArg' t
pexp PTerm
wval PArg -> [PArg] -> [PArg]
forall a. a -> [a] -> [a]
:
                    (((Name, Term) -> PArg) -> [(Name, Term)] -> [PArg]
forall a b. (a -> b) -> [a] -> [b]
map (PTerm -> PArg
forall {t}. t -> PArg' t
pexp (PTerm -> PArg) -> ((Name, Term) -> PTerm) -> (Name, Term) -> PArg
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (FC -> [FC] -> Name -> PTerm
PRef FC
fc []) (Name -> PTerm) -> ((Name, Term) -> Name) -> (Name, Term) -> PTerm
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Name, Term) -> Name
forall a b. (a, b) -> a
fst) [(Name, Term)]
bargs_post) [PArg] -> [PArg] -> [PArg]
forall a. [a] -> [a] -> [a]
++
                    case Maybe Name
mpn of
                         Maybe Name
Nothing -> []
                         Just Name
_ -> [PTerm -> PArg
forall {t}. t -> PArg' t
pexp (FC -> PTerm -> [PArg] -> PTerm
PApp FC
NoFC (FC -> [FC] -> Name -> PTerm
PRef FC
NoFC [] Name
eqCon)
                                               [ Name -> PTerm -> Bool -> PArg
forall {t}. Name -> t -> Bool -> PArg' t
pimp (String -> Name
sUN String
"A") PTerm
Placeholder Bool
False
                                               , Name -> PTerm -> Bool -> PArg
forall {t}. Name -> t -> Bool -> PArg' t
pimp (String -> Name
sUN String
"x") PTerm
Placeholder Bool
False
                                               ])])
        Int -> String -> Idris ()
logElab Int
5 (String
"New RHS " String -> String -> String
forall a. [a] -> [a] -> [a]
++ PTerm -> String
showTmImpls PTerm
rhs)
        Context
ctxt <- Idris Context
getContext -- New context with block added
        IState
i <- Idris IState
getIState
        ((Term
rhsElab, [(Name, (Int, Maybe Name, Term, [Name]))]
defer, [PDecl' PTerm]
is, Context
ctxt', [RDeclInstructions]
newDecls, Set (FC', OutputAnnotation)
highlights, Int
newGName), String
_) <-
           TC
  ((Term, [(Name, (Int, Maybe Name, Term, [Name]))], [PDecl' PTerm],
    Context, [RDeclInstructions], Set (FC', OutputAnnotation), Int),
   String)
-> Idris
     ((Term, [(Name, (Int, Maybe Name, Term, [Name]))], [PDecl' PTerm],
       Context, [RDeclInstructions], Set (FC', OutputAnnotation), Int),
      String)
forall a. TC a -> Idris a
tclift (TC
   ((Term, [(Name, (Int, Maybe Name, Term, [Name]))], [PDecl' PTerm],
     Context, [RDeclInstructions], Set (FC', OutputAnnotation), Int),
    String)
 -> Idris
      ((Term, [(Name, (Int, Maybe Name, Term, [Name]))], [PDecl' PTerm],
        Context, [RDeclInstructions], Set (FC', OutputAnnotation), Int),
       String))
-> TC
     ((Term, [(Name, (Int, Maybe Name, Term, [Name]))], [PDecl' PTerm],
       Context, [RDeclInstructions], Set (FC', OutputAnnotation), Int),
      String)
-> Idris
     ((Term, [(Name, (Int, Maybe Name, Term, [Name]))], [PDecl' PTerm],
       Context, [RDeclInstructions], Set (FC', OutputAnnotation), Int),
      String)
forall a b. (a -> b) -> a -> b
$ String
-> Context
-> Ctxt TypeInfo
-> Int
-> Name
-> Term
-> EState
-> Elab'
     EState
     (Term, [(Name, (Int, Maybe Name, Term, [Name]))], [PDecl' PTerm],
      Context, [RDeclInstructions], Set (FC', OutputAnnotation), Int)
-> TC
     ((Term, [(Name, (Int, Maybe Name, Term, [Name]))], [PDecl' PTerm],
       Context, [RDeclInstructions], Set (FC', OutputAnnotation), Int),
      String)
forall aux a.
String
-> Context
-> Ctxt TypeInfo
-> Int
-> Name
-> Term
-> aux
-> Elab' aux a
-> TC (a, String)
elaborate (ElabInfo -> String
constraintNS ElabInfo
info) Context
ctxt (IState -> Ctxt TypeInfo
idris_datatypes IState
i) (IState -> Int
idris_name IState
i) (Int -> String -> Name
sMN Int
0 String
"wpatRHS") Term
clhsty EState
initEState
                    (do IState -> Term -> ElabD ()
pbinds IState
i Term
lhs_tm
                        ElabD ()
forall aux. Elab' aux ()
setNextName
                        (ElabResult Term
_ [(Name, (Int, Maybe Name, Term, [Name]))]
d [PDecl' PTerm]
is Context
ctxt' [RDeclInstructions]
newDecls Set (FC', OutputAnnotation)
highlights Int
newGName) <-
                           FC -> Elab' EState ElabResult -> Elab' EState ElabResult
forall aux a. FC -> Elab' aux a -> Elab' aux a
erun FC
fc (IState
-> ElabInfo
-> ElabMode
-> FnOpts
-> Name
-> PTerm
-> Elab' EState ElabResult
build IState
i ElabInfo
info ElabMode
ERHS FnOpts
opts Name
fname PTerm
rhs)
                        Term -> ElabD ()
forall {n} {aux}. TT n -> StateT (ElabState aux) TC ()
psolve Term
lhs_tm
                        Term
tt <- Elab' EState Term
forall aux. Elab' aux Term
get_term
                        (Term, [(Name, (Int, Maybe Name, Term, [Name]))], [PDecl' PTerm],
 Context, [RDeclInstructions], Set (FC', OutputAnnotation), Int)
-> Elab'
     EState
     (Term, [(Name, (Int, Maybe Name, Term, [Name]))], [PDecl' PTerm],
      Context, [RDeclInstructions], Set (FC', OutputAnnotation), Int)
forall a. a -> StateT (ElabState EState) TC a
forall (m :: * -> *) a. Monad m => a -> m a
return (Term
tt, [(Name, (Int, Maybe Name, Term, [Name]))]
d, [PDecl' PTerm]
is, Context
ctxt', [RDeclInstructions]
newDecls, Set (FC', OutputAnnotation)
highlights, Int
newGName))
        Context -> Idris ()
setContext Context
ctxt'

        ElabInfo -> [RDeclInstructions] -> Idris ()
processTacticDecls ElabInfo
info [RDeclInstructions]
newDecls
        Set (FC', OutputAnnotation) -> Idris ()
sendHighlighting Set (FC', OutputAnnotation)
highlights
        (IState -> IState) -> Idris ()
updateIState ((IState -> IState) -> Idris ()) -> (IState -> IState) -> Idris ()
forall a b. (a -> b) -> a -> b
$ \IState
i -> IState
i { idris_name = newGName }

        Context
ctxt <- Idris Context
getContext
        let rhs' :: Term
rhs' = Term
rhsElab

        [(Name, (Int, Maybe Name, Term, [Name]))]
def' <- ElabInfo
-> FC
-> (Name -> Err -> Err)
-> Bool
-> [(Name, (Int, Maybe Name, Term, [Name]))]
-> Idris [(Name, (Int, Maybe Name, Term, [Name]))]
checkDef ElabInfo
info FC
fc Name -> Err -> Err
iderr Bool
True [(Name, (Int, Maybe Name, Term, [Name]))]
defer
        let def'' :: [(Name, (Int, Maybe Name, Term, [Name], Bool, Bool))]
def'' = ((Name, (Int, Maybe Name, Term, [Name]))
 -> (Name, (Int, Maybe Name, Term, [Name], Bool, Bool)))
-> [(Name, (Int, Maybe Name, Term, [Name]))]
-> [(Name, (Int, Maybe Name, Term, [Name], Bool, Bool))]
forall a b. (a -> b) -> [a] -> [b]
map (\(Name
n, (Int
i, Maybe Name
top, Term
t, [Name]
ns)) -> (Name
n, (Int
i, Maybe Name
top, Term
t, [Name]
ns, Bool
False, Bool
True))) [(Name, (Int, Maybe Name, Term, [Name]))]
def'
        [(Name, (Int, Maybe Name, Term, [Name], Bool, Bool))] -> Idris ()
addDeferred [(Name, (Int, Maybe Name, Term, [Name], Bool, Bool))]
def''
        (PDecl' PTerm -> Idris ()) -> [PDecl' PTerm] -> Idris ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (ElabInfo -> FnOpts -> PDecl' PTerm -> Idris ()
elabCaseBlock ElabInfo
info FnOpts
opts) [PDecl' PTerm]
is
        Int -> String -> Idris ()
logElab Int
5 (String
"Checked RHS " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Term -> String
forall a. Show a => a -> String
show Term
rhs')
        (Term
crhs, Term
crhsty) <- String -> FC -> (Err -> Err) -> Env -> Term -> Idris (Term, Term)
recheckC (ElabInfo -> String
constraintNS ElabInfo
info) FC
fc Err -> Err
forall a. a -> a
forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id [] Term
rhs'
        (Either Term (Term, Term), PTerm)
-> StateT IState (ExceptT Err IO) (Either Term (Term, Term), PTerm)
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return ((Term, Term) -> Either Term (Term, Term)
forall a b. b -> Either a b
Right (Term
clhs, Term
crhs), PTerm
lhs)
  where
    getImps :: TT n -> [PArg]
getImps (Bind n
n (Pi RigCount
_ Maybe ImplicitInfo
_ TT n
_ TT n
_) TT n
t) = PTerm -> PArg
forall {t}. t -> PArg' t
pexp PTerm
Placeholder PArg -> [PArg] -> [PArg]
forall a. a -> [a] -> [a]
: TT n -> [PArg]
getImps TT n
t
    getImps TT n
_ = []

    mkAuxC :: Maybe Name
-> Name
-> PTerm
-> [Name]
-> [Name]
-> PDecl' PTerm
-> StateT IState (ExceptT Err IO) (PDecl' PTerm)
mkAuxC Maybe Name
pn Name
wname PTerm
lhs [Name]
ns [Name]
ns' (PClauses FC
fc FnOpts
o Name
n [PClause]
cs)
                = do [PClause]
cs' <- (PClause -> StateT IState (ExceptT Err IO) PClause)
-> [PClause] -> StateT IState (ExceptT Err IO) [PClause]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Maybe Name
-> Name
-> PTerm
-> [Name]
-> [Name]
-> PClause
-> StateT IState (ExceptT Err IO) PClause
mkAux Maybe Name
pn Name
wname PTerm
lhs [Name]
ns [Name]
ns') [PClause]
cs
                     PDecl' PTerm -> StateT IState (ExceptT Err IO) (PDecl' PTerm)
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return (PDecl' PTerm -> StateT IState (ExceptT Err IO) (PDecl' PTerm))
-> PDecl' PTerm -> StateT IState (ExceptT Err IO) (PDecl' PTerm)
forall a b. (a -> b) -> a -> b
$ FC -> FnOpts -> Name -> [PClause] -> PDecl' PTerm
forall t. FC -> FnOpts -> Name -> [PClause' t] -> PDecl' t
PClauses FC
fc FnOpts
o Name
wname [PClause]
cs'
    mkAuxC Maybe Name
pn Name
wname PTerm
lhs [Name]
ns [Name]
ns' PDecl' PTerm
d = PDecl' PTerm -> StateT IState (ExceptT Err IO) (PDecl' PTerm)
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return PDecl' PTerm
d

    mkAux :: Maybe Name
-> Name
-> PTerm
-> [Name]
-> [Name]
-> PClause
-> StateT IState (ExceptT Err IO) PClause
mkAux Maybe Name
pn Name
wname PTerm
toplhs [Name]
ns [Name]
ns' (PClause FC
fc Name
n PTerm
tm_in (PTerm
w:[PTerm]
ws) PTerm
rhs [PDecl' PTerm]
wheres)
        = do IState
i <- Idris IState
getIState
             let tm :: PTerm
tm = IState -> PTerm -> PTerm
addImplPat IState
i PTerm
tm_in
             Int -> String -> Idris ()
logElab Int
2 (String
"Matching " String -> String -> String
forall a. [a] -> [a] -> [a]
++ PTerm -> String
showTmImpls PTerm
tm String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" against " String -> String -> String
forall a. [a] -> [a] -> [a]
++
                                      PTerm -> String
showTmImpls PTerm
toplhs)
             case IState -> PTerm -> PTerm -> Either (PTerm, PTerm) [(Name, PTerm)]
matchClause IState
i PTerm
toplhs PTerm
tm of
                Left (PTerm
a,PTerm
b) -> String -> StateT IState (ExceptT Err IO) PClause
forall a. String -> Idris a
ifail (String -> StateT IState (ExceptT Err IO) PClause)
-> String -> StateT IState (ExceptT Err IO) PClause
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
":with clause does not match top level"
                Right [(Name, PTerm)]
mvars ->
                    do Int -> String -> Idris ()
logElab Int
3 (String
"Match vars : " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [(Name, PTerm)] -> String
forall a. Show a => a -> String
show [(Name, PTerm)]
mvars)
                       PTerm
lhs <- Name
-> Maybe Name
-> Name
-> [(Name, PTerm)]
-> [Name]
-> [Name]
-> PTerm
-> PTerm
-> StateT IState (ExceptT Err IO) PTerm
forall {m :: * -> *} {t}.
Monad m =>
t
-> Maybe Name
-> Name
-> [(Name, PTerm)]
-> [Name]
-> [Name]
-> PTerm
-> PTerm
-> m PTerm
updateLHS Name
n Maybe Name
pn Name
wname [(Name, PTerm)]
mvars [Name]
ns [Name]
ns' (PTerm -> PTerm
fullApp PTerm
tm) PTerm
w
                       PClause -> StateT IState (ExceptT Err IO) PClause
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return (PClause -> StateT IState (ExceptT Err IO) PClause)
-> PClause -> StateT IState (ExceptT Err IO) PClause
forall a b. (a -> b) -> a -> b
$ FC
-> Name -> PTerm -> [PTerm] -> PTerm -> [PDecl' PTerm] -> PClause
forall t. FC -> Name -> t -> [t] -> t -> [PDecl' t] -> PClause' t
PClause FC
fc Name
wname PTerm
lhs [PTerm]
ws PTerm
rhs [PDecl' PTerm]
wheres
    mkAux Maybe Name
pn Name
wname PTerm
toplhs [Name]
ns [Name]
ns' (PWith FC
fc Name
n PTerm
tm_in (PTerm
w:[PTerm]
ws) PTerm
wval Maybe (Name, FC)
pn' [PDecl' PTerm]
withs)
        = do IState
i <- Idris IState
getIState
             let tm :: PTerm
tm = IState -> PTerm -> PTerm
addImplPat IState
i PTerm
tm_in
             Int -> String -> Idris ()
logElab Int
2 (String
"Matching " String -> String -> String
forall a. [a] -> [a] -> [a]
++ PTerm -> String
showTmImpls PTerm
tm String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" against " String -> String -> String
forall a. [a] -> [a] -> [a]
++
                                      PTerm -> String
showTmImpls PTerm
toplhs)
             [PDecl' PTerm]
withs' <- (PDecl' PTerm -> StateT IState (ExceptT Err IO) (PDecl' PTerm))
-> [PDecl' PTerm] -> StateT IState (ExceptT Err IO) [PDecl' PTerm]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Maybe Name
-> Name
-> PTerm
-> [Name]
-> [Name]
-> PDecl' PTerm
-> StateT IState (ExceptT Err IO) (PDecl' PTerm)
mkAuxC Maybe Name
pn Name
wname PTerm
toplhs [Name]
ns [Name]
ns') [PDecl' PTerm]
withs
             case IState -> PTerm -> PTerm -> Either (PTerm, PTerm) [(Name, PTerm)]
matchClause IState
i PTerm
toplhs PTerm
tm of
                Left (PTerm
a,PTerm
b) -> String
-> StateT IState (ExceptT Err IO) PClause
-> StateT IState (ExceptT Err IO) PClause
forall a. String -> a -> a
trace (String
"matchClause: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ PTerm -> String
forall a. Show a => a -> String
show PTerm
a 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
b) (String -> StateT IState (ExceptT Err IO) PClause
forall a. String -> Idris a
ifail (String -> StateT IState (ExceptT Err IO) PClause)
-> String -> StateT IState (ExceptT Err IO) PClause
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
"with clause does not match top level")
                Right [(Name, PTerm)]
mvars ->
                    do PTerm
lhs <- Name
-> Maybe Name
-> Name
-> [(Name, PTerm)]
-> [Name]
-> [Name]
-> PTerm
-> PTerm
-> StateT IState (ExceptT Err IO) PTerm
forall {m :: * -> *} {t}.
Monad m =>
t
-> Maybe Name
-> Name
-> [(Name, PTerm)]
-> [Name]
-> [Name]
-> PTerm
-> PTerm
-> m PTerm
updateLHS Name
n Maybe Name
pn Name
wname [(Name, PTerm)]
mvars [Name]
ns [Name]
ns' (PTerm -> PTerm
fullApp PTerm
tm) PTerm
w
                       PClause -> StateT IState (ExceptT Err IO) PClause
forall a. a -> StateT IState (ExceptT Err IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return (PClause -> StateT IState (ExceptT Err IO) PClause)
-> PClause -> StateT IState (ExceptT Err IO) PClause
forall a b. (a -> b) -> a -> b
$ FC
-> Name
-> PTerm
-> [PTerm]
-> PTerm
-> Maybe (Name, FC)
-> [PDecl' PTerm]
-> PClause
forall t.
FC
-> Name
-> t
-> [t]
-> t
-> Maybe (Name, FC)
-> [PDecl' t]
-> PClause' t
PWith FC
fc Name
wname PTerm
lhs [PTerm]
ws PTerm
wval Maybe (Name, FC)
pn' [PDecl' PTerm]
withs'
    mkAux Maybe Name
pn Name
wname PTerm
toplhs [Name]
ns [Name]
ns' PClause
c
        = String -> StateT IState (ExceptT Err IO) PClause
forall a. String -> Idris a
ifail (String -> StateT IState (ExceptT Err IO) PClause)
-> String -> StateT IState (ExceptT Err IO) PClause
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
":badly formed with clause"

    -- ns, arguments which don't depend on the with argument
    -- ns', arguments which do
    updateLHS :: t
-> Maybe Name
-> Name
-> [(Name, PTerm)]
-> [Name]
-> [Name]
-> PTerm
-> PTerm
-> m PTerm
updateLHS t
n Maybe Name
pn Name
wname [(Name, PTerm)]
mvars [Name]
ns_in [Name]
ns_in' (PApp FC
fc (PRef FC
fc' [FC]
hls' Name
n') [PArg]
args) PTerm
w
        = let ns :: [PTerm]
ns = (Name -> PTerm) -> [Name] -> [PTerm]
forall a b. (a -> b) -> [a] -> [b]
map ([Name] -> FC -> Name -> PTerm
forall {t :: * -> *}. Foldable t => t Name -> FC -> Name -> PTerm
keepMvar (((Name, PTerm) -> Name) -> [(Name, PTerm)] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name, PTerm) -> Name
forall a b. (a, b) -> a
fst [(Name, PTerm)]
mvars) FC
fc') [Name]
ns_in
              ns' :: [PTerm]
ns' = (Name -> PTerm) -> [Name] -> [PTerm]
forall a b. (a -> b) -> [a] -> [b]
map ([Name] -> FC -> Name -> PTerm
forall {t :: * -> *}. Foldable t => t Name -> FC -> Name -> PTerm
keepMvar (((Name, PTerm) -> Name) -> [(Name, PTerm)] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name, PTerm) -> Name
forall a b. (a, b) -> a
fst [(Name, PTerm)]
mvars) FC
fc') [Name]
ns_in' in
              PTerm -> m PTerm
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (PTerm -> m PTerm) -> PTerm -> m PTerm
forall a b. (a -> b) -> a -> b
$ [(Name, PTerm)] -> PTerm -> PTerm
substMatches [(Name, PTerm)]
mvars (PTerm -> PTerm) -> PTerm -> PTerm
forall a b. (a -> b) -> a -> b
$
                  FC -> PTerm -> [PArg] -> PTerm
PApp FC
fc (FC -> [FC] -> Name -> PTerm
PRef FC
fc' [] Name
wname)
                      ((PTerm -> PArg) -> [PTerm] -> [PArg]
forall a b. (a -> b) -> [a] -> [b]
map PTerm -> PArg
forall {t}. t -> PArg' t
pexp [PTerm]
ns [PArg] -> [PArg] -> [PArg]
forall a. [a] -> [a] -> [a]
++ PTerm -> PArg
forall {t}. t -> PArg' t
pexp PTerm
w PArg -> [PArg] -> [PArg]
forall a. a -> [a] -> [a]
: ((PTerm -> PArg) -> [PTerm] -> [PArg]
forall a b. (a -> b) -> [a] -> [b]
map PTerm -> PArg
forall {t}. t -> PArg' t
pexp [PTerm]
ns') [PArg] -> [PArg] -> [PArg]
forall a. [a] -> [a] -> [a]
++
                        case Maybe Name
pn of
                             Maybe Name
Nothing -> []
                             Just Name
pnm -> [PTerm -> PArg
forall {t}. t -> PArg' t
pexp (FC -> [FC] -> Name -> PTerm
PRef FC
fc [] Name
pnm)])
    updateLHS t
n Maybe Name
pn Name
wname [(Name, PTerm)]
mvars [Name]
ns_in [Name]
ns_in' PTerm
tm PTerm
w
        = t
-> Maybe Name
-> Name
-> [(Name, PTerm)]
-> [Name]
-> [Name]
-> PTerm
-> PTerm
-> m PTerm
updateLHS t
n Maybe Name
pn Name
wname [(Name, PTerm)]
mvars [Name]
ns_in [Name]
ns_in' (FC -> PTerm -> [PArg] -> PTerm
PApp FC
fc PTerm
tm []) PTerm
w

    -- Only keep a var as a pattern variable in the with block if it's
    -- matched in the top level pattern
    keepMvar :: t Name -> FC -> Name -> PTerm
keepMvar t Name
mvs FC
fc Name
v | Name
v Name -> t Name -> Bool
forall a. Eq a => a -> t a -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` t Name
mvs = FC -> [FC] -> Name -> PTerm
PRef FC
fc [] Name
v
                      | Bool
otherwise = PTerm
Placeholder

    updateWithTerm :: IState -> Maybe Name -> Name -> PTerm -> [Name] -> [Name] -> PTerm -> PTerm
    updateWithTerm :: IState
-> Maybe Name
-> Name
-> PTerm
-> [Name]
-> [Name]
-> PTerm
-> PTerm
updateWithTerm IState
ist Maybe Name
pn Name
wname PTerm
toplhs [Name]
ns_in [Name]
ns_in' PTerm
tm
          = (PTerm -> PTerm) -> PTerm -> PTerm
mapPT PTerm -> PTerm
updateApp PTerm
tm
       where
         currentFn :: Name -> PTerm -> PTerm
currentFn Name
fname (PAlternative [(Name, Name)]
_ PAltType
_ [PTerm]
as)
              | Just PTerm
tm <- [PTerm] -> Maybe PTerm
getApp [PTerm]
as = PTerm
tm
            where getApp :: [PTerm] -> Maybe PTerm
getApp (tm :: PTerm
tm@(PApp FC
_ (PRef FC
_ [FC]
_ Name
f) [PArg]
_) : [PTerm]
as)
                      | Name
f Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== Name
fname = PTerm -> Maybe PTerm
forall a. a -> Maybe a
Just PTerm
tm
                  getApp (PTerm
_ : [PTerm]
as) = [PTerm] -> Maybe PTerm
getApp [PTerm]
as
                  getApp [] = Maybe PTerm
forall a. Maybe a
Nothing
         currentFn Name
_ PTerm
tm = PTerm
tm

         updateApp :: PTerm -> PTerm
updateApp wtm :: PTerm
wtm@(PWithApp FC
fcw PTerm
tm_in PTerm
warg) =
           let tm :: PTerm
tm = Name -> PTerm -> PTerm
currentFn Name
fname PTerm
tm_in in
              case IState -> PTerm -> PTerm -> Either (PTerm, PTerm) [(Name, PTerm)]
matchClause IState
ist PTerm
toplhs PTerm
tm of
                Left (PTerm, PTerm)
_ -> Err -> PTerm
PElabError (String -> Err
forall t. String -> Err' t
Msg (FC -> String
forall a. Show a => a -> String
show FC
fc String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
":with application does not match top level "))
                Right [(Name, PTerm)]
mvars ->
                   let ns :: [PTerm]
ns = (Name -> PTerm) -> [Name] -> [PTerm]
forall a b. (a -> b) -> [a] -> [b]
map ([Name] -> FC -> Name -> PTerm
forall {t :: * -> *}. Foldable t => t Name -> FC -> Name -> PTerm
keepMvar (((Name, PTerm) -> Name) -> [(Name, PTerm)] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name, PTerm) -> Name
forall a b. (a, b) -> a
fst [(Name, PTerm)]
mvars) FC
fcw) [Name]
ns_in
                       ns' :: [PTerm]
ns' = (Name -> PTerm) -> [Name] -> [PTerm]
forall a b. (a -> b) -> [a] -> [b]
map ([Name] -> FC -> Name -> PTerm
forall {t :: * -> *}. Foldable t => t Name -> FC -> Name -> PTerm
keepMvar (((Name, PTerm) -> Name) -> [(Name, PTerm)] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name, PTerm) -> Name
forall a b. (a, b) -> a
fst [(Name, PTerm)]
mvars) FC
fcw) [Name]
ns_in'
                       wty :: Maybe Term
wty = Name -> Context -> Maybe Term
lookupTyExact Name
wname (IState -> Context
tt_ctxt IState
ist)
                       res :: PTerm
res = [(Name, PTerm)] -> PTerm -> PTerm
substMatches [(Name, PTerm)]
mvars (PTerm -> PTerm) -> PTerm -> PTerm
forall a b. (a -> b) -> a -> b
$
                          FC -> PTerm -> [PArg] -> PTerm
PApp FC
fcw (FC -> [FC] -> Name -> PTerm
PRef FC
fcw [] Name
wname)
                            ((PTerm -> PArg) -> [PTerm] -> [PArg]
forall a b. (a -> b) -> [a] -> [b]
map PTerm -> PArg
forall {t}. t -> PArg' t
pexp [PTerm]
ns [PArg] -> [PArg] -> [PArg]
forall a. [a] -> [a] -> [a]
++ PTerm -> PArg
forall {t}. t -> PArg' t
pexp PTerm
warg PArg -> [PArg] -> [PArg]
forall a. a -> [a] -> [a]
: ((PTerm -> PArg) -> [PTerm] -> [PArg]
forall a b. (a -> b) -> [a] -> [b]
map PTerm -> PArg
forall {t}. t -> PArg' t
pexp [PTerm]
ns') [PArg] -> [PArg] -> [PArg]
forall a. [a] -> [a] -> [a]
++
                                case Maybe Name
pn of
                                     Maybe Name
Nothing -> []
                                     Just Name
pnm -> [PTerm -> PArg
forall {t}. t -> PArg' t
pexp (FC -> [FC] -> Name -> PTerm
PRef FC
fc [] Name
pnm)]) in
                          case Maybe Term
wty of
                               Maybe Term
Nothing -> PTerm
res -- can't happen!
                               Just Term
ty -> Term -> PTerm -> PTerm
addResolves Term
ty PTerm
res
         updateApp PTerm
tm = PTerm
tm

         addResolves :: Term -> PTerm -> PTerm
addResolves Term
ty (PApp FC
fc PTerm
f [PArg]
args) = FC -> PTerm -> [PArg] -> PTerm
PApp FC
fc PTerm
f (FC -> Term -> [PArg] -> [PArg]
addResolvesArgs FC
fc Term
ty [PArg]
args)
         addResolves Term
ty PTerm
tm = PTerm
tm

         -- if an argument's type is an interface, and is otherwise to
         -- be inferred, then resolve it with implementation search
         -- This is something of a hack, because matching on the top level
         -- application won't find this information for us
         addResolvesArgs :: FC -> Term -> [PArg] -> [PArg]
         addResolvesArgs :: FC -> Term -> [PArg] -> [PArg]
addResolvesArgs FC
fc (Bind Name
n (Pi RigCount
_ Maybe ImplicitInfo
_ Term
ty Term
_) Term
sc) (PArg
a : [PArg]
args)
             | (P NameType
_ Name
cn Term
_, [Term]
_) <- Term -> (Term, [Term])
forall n. TT n -> (TT n, [TT n])
unApply Term
ty,
               PArg -> PTerm
forall t. PArg' t -> t
getTm PArg
a PTerm -> PTerm -> Bool
forall a. Eq a => a -> a -> Bool
== PTerm
Placeholder
                 = case Name -> Ctxt InterfaceInfo -> Maybe InterfaceInfo
forall a. Name -> Ctxt a -> Maybe a
lookupCtxtExact Name
cn (IState -> Ctxt InterfaceInfo
idris_interfaces IState
ist) of
                        Just InterfaceInfo
_ -> PArg
a { getTm = PResolveTC fc } PArg -> [PArg] -> [PArg]
forall a. a -> [a] -> [a]
: FC -> Term -> [PArg] -> [PArg]
addResolvesArgs FC
fc Term
sc [PArg]
args
                        Maybe InterfaceInfo
Nothing -> PArg
a PArg -> [PArg] -> [PArg]
forall a. a -> [a] -> [a]
: FC -> Term -> [PArg] -> [PArg]
addResolvesArgs FC
fc Term
sc [PArg]
args
         addResolvesArgs FC
fc (Bind Name
n (Pi RigCount
_ Maybe ImplicitInfo
_ Term
ty Term
_) Term
sc) (PArg
a : [PArg]
args)
                 = PArg
a PArg -> [PArg] -> [PArg]
forall a. a -> [a] -> [a]
: FC -> Term -> [PArg] -> [PArg]
addResolvesArgs FC
fc Term
sc [PArg]
args
         addResolvesArgs FC
fc Term
_ [PArg]
args = [PArg]
args

    fullApp :: PTerm -> PTerm
fullApp (PApp FC
_ (PApp FC
fc PTerm
f [PArg]
args) [PArg]
xs) = PTerm -> PTerm
fullApp (FC -> PTerm -> [PArg] -> PTerm
PApp FC
fc PTerm
f ([PArg]
args [PArg] -> [PArg] -> [PArg]
forall a. [a] -> [a] -> [a]
++ [PArg]
xs))
    fullApp PTerm
x = PTerm
x

    split :: [a] -> [(a, b)] -> [(a, b)] -> ([(a, b)], [(a, b)])
split [] [(a, b)]
rest [(a, b)]
pre = ([(a, b)] -> [(a, b)]
forall a. [a] -> [a]
reverse [(a, b)]
pre, [(a, b)]
rest)
    split [a]
deps ((a
n, b
ty) : [(a, b)]
rest) [(a, b)]
pre
          | a
n a -> [a] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [a]
deps = [a] -> [(a, b)] -> [(a, b)] -> ([(a, b)], [(a, b)])
split ([a]
deps [a] -> [a] -> [a]
forall a. Eq a => [a] -> [a] -> [a]
\\ [a
n]) [(a, b)]
rest ((a
n, b
ty) (a, b) -> [(a, b)] -> [(a, b)]
forall a. a -> [a] -> [a]
: [(a, b)]
pre)
          | Bool
otherwise = [a] -> [(a, b)] -> [(a, b)] -> ([(a, b)], [(a, b)])
split [a]
deps [(a, b)]
rest ((a
n, b
ty) (a, b) -> [(a, b)] -> [(a, b)]
forall a. a -> [a] -> [a]
: [(a, b)]
pre)
    split [a]
deps [] [(a, b)]
pre = ([(a, b)] -> [(a, b)]
forall a. [a] -> [a]
reverse [(a, b)]
pre, [])

    abstract :: n -> TT n -> TT n -> (a, TT n) -> (a, TT n)
abstract n
wn TT n
wv TT n
wty (a
n, TT n
argty) = (a
n, TT n -> TT n -> TT n -> TT n
forall n. Eq n => TT n -> TT n -> TT n -> TT n
substTerm TT n
wv (NameType -> n -> TT n -> TT n
forall n. NameType -> n -> TT n -> TT n
P NameType
Bound n
wn TT n
wty) TT n
argty)

-- | Apply a transformation to all RHSes and nested RHSs
mapRHS :: (PTerm -> PTerm) -> PClause -> PClause
mapRHS :: (PTerm -> PTerm) -> PClause -> PClause
mapRHS PTerm -> PTerm
f (PClause FC
fc Name
n PTerm
lhs [PTerm]
args PTerm
rhs [PDecl' PTerm]
ws)
    = FC
-> Name -> PTerm -> [PTerm] -> PTerm -> [PDecl' PTerm] -> PClause
forall t. FC -> Name -> t -> [t] -> t -> [PDecl' t] -> PClause' t
PClause FC
fc Name
n PTerm
lhs [PTerm]
args (PTerm -> PTerm
f PTerm
rhs) ((PDecl' PTerm -> PDecl' PTerm) -> [PDecl' PTerm] -> [PDecl' PTerm]
forall a b. (a -> b) -> [a] -> [b]
map ((PTerm -> PTerm) -> PDecl' PTerm -> PDecl' PTerm
mapRHSdecl PTerm -> PTerm
f) [PDecl' PTerm]
ws)
mapRHS PTerm -> PTerm
f (PWith FC
fc Name
n PTerm
lhs [PTerm]
args PTerm
warg Maybe (Name, FC)
prf [PDecl' PTerm]
ws)
    = FC
-> Name
-> PTerm
-> [PTerm]
-> PTerm
-> Maybe (Name, FC)
-> [PDecl' PTerm]
-> PClause
forall t.
FC
-> Name
-> t
-> [t]
-> t
-> Maybe (Name, FC)
-> [PDecl' t]
-> PClause' t
PWith FC
fc Name
n PTerm
lhs [PTerm]
args (PTerm -> PTerm
f PTerm
warg) Maybe (Name, FC)
prf ((PDecl' PTerm -> PDecl' PTerm) -> [PDecl' PTerm] -> [PDecl' PTerm]
forall a b. (a -> b) -> [a] -> [b]
map ((PTerm -> PTerm) -> PDecl' PTerm -> PDecl' PTerm
mapRHSdecl PTerm -> PTerm
f) [PDecl' PTerm]
ws)
mapRHS PTerm -> PTerm
f (PClauseR FC
fc [PTerm]
args PTerm
rhs [PDecl' PTerm]
ws)
    = FC -> [PTerm] -> PTerm -> [PDecl' PTerm] -> PClause
forall t. FC -> [t] -> t -> [PDecl' t] -> PClause' t
PClauseR FC
fc [PTerm]
args (PTerm -> PTerm
f PTerm
rhs) ((PDecl' PTerm -> PDecl' PTerm) -> [PDecl' PTerm] -> [PDecl' PTerm]
forall a b. (a -> b) -> [a] -> [b]
map ((PTerm -> PTerm) -> PDecl' PTerm -> PDecl' PTerm
mapRHSdecl PTerm -> PTerm
f) [PDecl' PTerm]
ws)
mapRHS PTerm -> PTerm
f (PWithR FC
fc [PTerm]
args PTerm
warg Maybe (Name, FC)
prf [PDecl' PTerm]
ws)
    = FC
-> [PTerm]
-> PTerm
-> Maybe (Name, FC)
-> [PDecl' PTerm]
-> PClause
forall t.
FC -> [t] -> t -> Maybe (Name, FC) -> [PDecl' t] -> PClause' t
PWithR FC
fc [PTerm]
args (PTerm -> PTerm
f PTerm
warg) Maybe (Name, FC)
prf ((PDecl' PTerm -> PDecl' PTerm) -> [PDecl' PTerm] -> [PDecl' PTerm]
forall a b. (a -> b) -> [a] -> [b]
map ((PTerm -> PTerm) -> PDecl' PTerm -> PDecl' PTerm
mapRHSdecl PTerm -> PTerm
f) [PDecl' PTerm]
ws)

mapRHSdecl :: (PTerm -> PTerm) -> PDecl -> PDecl
mapRHSdecl :: (PTerm -> PTerm) -> PDecl' PTerm -> PDecl' PTerm
mapRHSdecl PTerm -> PTerm
f (PClauses FC
fc FnOpts
opt Name
n [PClause]
cs)
    = FC -> FnOpts -> Name -> [PClause] -> PDecl' PTerm
forall t. FC -> FnOpts -> Name -> [PClause' t] -> PDecl' t
PClauses FC
fc FnOpts
opt Name
n ((PClause -> PClause) -> [PClause] -> [PClause]
forall a b. (a -> b) -> [a] -> [b]
map ((PTerm -> PTerm) -> PClause -> PClause
mapRHS PTerm -> PTerm
f) [PClause]
cs)
mapRHSdecl PTerm -> PTerm
f PDecl' PTerm
t = PDecl' PTerm
t