{-# LANGUAGE NondecreasingIndentation #-}

{-| Coverage checking, case splitting, and splitting for refine tactics.

 -}

module Agda.TypeChecking.Coverage
  ( SplitClause(..), clauseToSplitClause, insertTrailingArgs
  , Covering(..), splitClauses
  , coverageCheck
  , isCovered
  , splitClauseWithAbsurd
  , splitLast
  , splitResult
  , normaliseProjP
  ) where

import Prelude hiding (null, (!!))  -- do not use partial functions like !!

import Control.Monad
import Control.Monad.Except
import Control.Monad.Trans ( lift )

import Data.Foldable (for_)
import qualified Data.List as List
import Data.Map (Map)
import qualified Data.Map as Map
import qualified Data.Set as Set
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet

import Agda.Syntax.Common
import Agda.Syntax.Position
import Agda.Syntax.Internal hiding (DataOrRecord(..))
import Agda.Syntax.Internal.Pattern
import Agda.Syntax.Translation.InternalToAbstract (NamedClause(..))

import Agda.TypeChecking.Names
import Agda.TypeChecking.Primitive hiding (Nat)
import Agda.TypeChecking.Monad

import Agda.TypeChecking.Rules.LHS (DataOrRecord(..), checkSortOfSplitVar)
import Agda.TypeChecking.Rules.LHS.Problem (allFlexVars)
import Agda.TypeChecking.Rules.LHS.Unify
import Agda.TypeChecking.Rules.Term (unquoteTactic)

import Agda.TypeChecking.Coverage.Match
import Agda.TypeChecking.Coverage.SplitTree

import Agda.TypeChecking.Conversion (tryConversion, equalType)
import Agda.TypeChecking.Datatypes (getConForm)
import {-# SOURCE #-} Agda.TypeChecking.Empty ( checkEmptyTel, isEmptyTel, isEmptyType )
import Agda.TypeChecking.Irrelevance
import Agda.TypeChecking.Pretty
import Agda.TypeChecking.Substitute
import Agda.TypeChecking.Reduce
import Agda.TypeChecking.Records
import Agda.TypeChecking.Telescope
import Agda.TypeChecking.Telescope.Path
import Agda.TypeChecking.MetaVars
import Agda.TypeChecking.Warnings

import Agda.Interaction.Options

import Agda.Utils.Either
import Agda.Utils.Functor
import Agda.Utils.List
import Agda.Utils.Maybe
import Agda.Utils.Monad
import Agda.Utils.Null
import Agda.Utils.Permutation
import Agda.Utils.Pretty (prettyShow)
import Agda.Utils.Singleton
import Agda.Utils.Size
import Agda.Utils.WithDefault

import Agda.Utils.Impossible

data SplitClause = SClause
  { SplitClause -> Telescope
scTel    :: Telescope
    -- ^ Type of variables in @scPats@.
  , SplitClause -> [NamedArg SplitPattern]
scPats   :: [NamedArg SplitPattern]
    -- ^ The patterns leading to the currently considered branch of
    --   the split tree.
  , SplitClause -> Substitution' SplitPattern
scSubst  :: Substitution' SplitPattern
    -- ^ Substitution from 'scTel' to old context.
    --   Only needed directly after split on variable:
    --   * To update 'scTarget'
    --   * To rename other split variables when splitting on
    --     multiple variables.
    --   @scSubst@ is not ``transitive'', i.e., does not record
    --   the substitution from the original context to 'scTel'
    --   over a series of splits.  It is freshly computed
    --   after each split by 'computeNeighborhood'; also
    --   'splitResult', which does not split on a variable,
    --   should reset it to the identity 'idS', lest it be
    --   applied to 'scTarget' again, leading to Issue 1294.
  , SplitClause -> Map CheckpointId (Substitution' Term)
scCheckpoints :: Map CheckpointId Substitution
    -- ^ We need to keep track of the module parameter checkpoints for the
    -- clause for the purpose of inferring missing instance clauses.
  , SplitClause -> Maybe (Dom Type)
scTarget :: Maybe (Dom Type)
    -- ^ The type of the rhs, living in context 'scTel'.
    --   'fixTargetType' computes the new 'scTarget' by applying
    --   substitution 'scSubst'.
  }

-- | A @Covering@ is the result of splitting a 'SplitClause'.
data Covering = Covering
  { Covering -> Arg Int
covSplitArg     :: Arg Nat
     -- ^ De Bruijn level (counting dot patterns) of argument we split on.
  , Covering -> [(SplitTag, SplitClause)]
covSplitClauses :: [(SplitTag, SplitClause)]
      -- ^ Covering clauses, indexed by constructor/literal these clauses share.
  }

-- | Project the split clauses out of a covering.
splitClauses :: Covering -> [SplitClause]
splitClauses :: Covering -> [SplitClause]
splitClauses (Covering Arg Int
_ [(SplitTag, SplitClause)]
qcs) = ((SplitTag, SplitClause) -> SplitClause)
-> [(SplitTag, SplitClause)] -> [SplitClause]
forall a b. (a -> b) -> [a] -> [b]
map (SplitTag, SplitClause) -> SplitClause
forall a b. (a, b) -> b
snd [(SplitTag, SplitClause)]
qcs

-- | Create a split clause from a clause in internal syntax. Used by make-case.
clauseToSplitClause :: Clause -> SplitClause
clauseToSplitClause :: Clause -> SplitClause
clauseToSplitClause Clause
cl = SClause
  { scTel :: Telescope
scTel    = Clause -> Telescope
clauseTel Clause
cl
  , scPats :: [NamedArg SplitPattern]
scPats   = NAPs -> [NamedArg SplitPattern]
toSplitPatterns (NAPs -> [NamedArg SplitPattern])
-> NAPs -> [NamedArg SplitPattern]
forall a b. (a -> b) -> a -> b
$ Clause -> NAPs
namedClausePats Clause
cl
  , scSubst :: Substitution' SplitPattern
scSubst  = Substitution' SplitPattern
forall a. Substitution' a
idS  -- Andreas, 2014-07-15  TODO: Is this ok?
  , scCheckpoints :: Map CheckpointId (Substitution' Term)
scCheckpoints = Map CheckpointId (Substitution' Term)
forall k a. Map k a
Map.empty -- #2996: not __IMPOSSIBLE__ for debug printing
  , scTarget :: Maybe (Dom Type)
scTarget = Arg Type -> Dom Type
forall a. Arg a -> Dom a
domFromArg (Arg Type -> Dom Type) -> Maybe (Arg Type) -> Maybe (Dom Type)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Clause -> Maybe (Arg Type)
clauseType Clause
cl
  }

type CoverM = ExceptT SplitError TCM

-- | Top-level function for checking pattern coverage.
--
--   Effects:
--
--   - Marks unreachable clauses as such in the signature.
--
--   - Adds missing instances clauses to the signature.
--
coverageCheck
  :: QName     -- ^ Name @f@ of definition.
  -> Type      -- ^ Absolute type (including the full parameter telescope).
  -> [Clause]  -- ^ Clauses of @f@.  These are the very clauses of @f@ in the signature.
  -> TCM SplitTree
coverageCheck :: QName -> Type -> [Clause] -> TCM SplitTree
coverageCheck QName
f Type
t [Clause]
cs = do
  [Char] -> Int -> [Char] -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> [Char] -> m ()
reportSLn [Char]
"tc.cover.top" Int
30 ([Char] -> TCMT IO ()) -> [Char] -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
"entering coverageCheck for " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ QName -> [Char]
forall a. Pretty a => a -> [Char]
prettyShow QName
f
  [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.top" Int
75 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ TCMT IO Doc
"  of type (raw): " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> ([Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char] -> TCMT IO Doc) -> (Type -> [Char]) -> Type -> TCMT IO Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Type -> [Char]
forall a. Pretty a => a -> [Char]
prettyShow) Type
t
  [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.top" Int
45 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ TCMT IO Doc
"  of type: " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Type -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM Type
t
  TelV Telescope
gamma Type
a <- Int -> Type -> TCMT IO (TelV Type)
forall (m :: * -> *).
(MonadReduce m, MonadAddContext m) =>
Int -> Type -> m (TelV Type)
telViewUpTo (-Int
1) Type
t
  [Char] -> Int -> [Char] -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> [Char] -> m ()
reportSLn [Char]
"tc.cover.top" Int
30 ([Char] -> TCMT IO ()) -> [Char] -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
"coverageCheck: computed telView"

  let -- n             = arity
      -- xs            = variable patterns fitting lgamma
      n :: Int
n            = Telescope -> Int
forall a. Sized a => a -> Int
size Telescope
gamma
      xs :: [NamedArg SplitPattern]
xs           =  (NamedArg SplitPattern -> NamedArg SplitPattern)
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a b. (a -> b) -> [a] -> [b]
map (Origin -> NamedArg SplitPattern -> NamedArg SplitPattern
forall a. LensOrigin a => Origin -> a -> a
setOrigin Origin
Inserted) ([NamedArg SplitPattern] -> [NamedArg SplitPattern])
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a b. (a -> b) -> a -> b
$ Telescope -> [NamedArg SplitPattern]
forall a. DeBruijn a => Telescope -> [NamedArg a]
teleNamedArgs Telescope
gamma

  [Char] -> Int -> [Char] -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> [Char] -> m ()
reportSLn [Char]
"tc.cover.top" Int
30 ([Char] -> TCMT IO ()) -> [Char] -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
"coverageCheck: getDefFreeVars"

      -- The initial module parameter substitutions need to be weakened by the
      -- number of arguments that aren't module parameters.
  Int
fv           <- QName -> TCMT IO Int
forall (m :: * -> *).
(Functor m, Applicative m, ReadTCState m, MonadTCEnv m) =>
QName -> m Int
getDefFreeVars QName
f

  [Char] -> Int -> [Char] -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> [Char] -> m ()
reportSLn [Char]
"tc.cover.top" Int
30 ([Char] -> TCMT IO ()) -> [Char] -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
"coverageCheck: getting checkpoints"

  -- TODO: does this make sense? Why are we weakening by n - fv?
  Map CheckpointId (Substitution' Term)
checkpoints <- Substitution' (SubstArg (Map CheckpointId (Substitution' Term)))
-> Map CheckpointId (Substitution' Term)
-> Map CheckpointId (Substitution' Term)
forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst (Int -> Substitution' Term
forall a. Int -> Substitution' a
raiseS (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
fv)) (Map CheckpointId (Substitution' Term)
 -> Map CheckpointId (Substitution' Term))
-> TCMT IO (Map CheckpointId (Substitution' Term))
-> TCMT IO (Map CheckpointId (Substitution' Term))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Lens' (Map CheckpointId (Substitution' Term)) TCEnv
-> TCMT IO (Map CheckpointId (Substitution' Term))
forall (m :: * -> *) a. MonadTCEnv m => Lens' a TCEnv -> m a
viewTC Lens' (Map CheckpointId (Substitution' Term)) TCEnv
eCheckpoints

      -- construct the initial split clause
  let sc :: SplitClause
sc = Telescope
-> [NamedArg SplitPattern]
-> Substitution' SplitPattern
-> Map CheckpointId (Substitution' Term)
-> Maybe (Dom Type)
-> SplitClause
SClause Telescope
gamma [NamedArg SplitPattern]
xs Substitution' SplitPattern
forall a. Substitution' a
idS Map CheckpointId (Substitution' Term)
checkpoints (Maybe (Dom Type) -> SplitClause)
-> Maybe (Dom Type) -> SplitClause
forall a b. (a -> b) -> a -> b
$ Dom Type -> Maybe (Dom Type)
forall a. a -> Maybe a
Just (Dom Type -> Maybe (Dom Type)) -> Dom Type -> Maybe (Dom Type)
forall a b. (a -> b) -> a -> b
$ Type -> Dom Type
forall a. a -> Dom a
defaultDom Type
a

  [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.top" Int
10 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ do
    let prCl :: Clause -> m Doc
prCl Clause
cl = Telescope -> m Doc -> m Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext (Clause -> Telescope
clauseTel Clause
cl) (m Doc -> m Doc) -> m Doc -> m Doc
forall a b. (a -> b) -> a -> b
$
                  NAPs -> m Doc
forall (m :: * -> *). MonadPretty m => NAPs -> m Doc
prettyTCMPatternList (NAPs -> m Doc) -> NAPs -> m Doc
forall a b. (a -> b) -> a -> b
$ Clause -> NAPs
namedClausePats Clause
cl
    [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
      [ [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char] -> TCMT IO Doc) -> [Char] -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [Char]
"Coverage checking " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ QName -> [Char]
forall a. Pretty a => a -> [Char]
prettyShow QName
f [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" with patterns:"
      , Int -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => Int -> m Doc -> m Doc
nest Int
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat ([TCMT IO Doc] -> TCMT IO Doc) -> [TCMT IO Doc] -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ (Clause -> TCMT IO Doc) -> [Clause] -> [TCMT IO Doc]
forall a b. (a -> b) -> [a] -> [b]
map Clause -> TCMT IO Doc
forall {m :: * -> *}.
(PureTCM m, MonadInteractionPoints m, MonadFresh NameId m,
 MonadStConcreteNames m, IsString (m Doc), Null (m Doc),
 Semigroup (m Doc)) =>
Clause -> m Doc
prCl [Clause]
cs
      ]

  -- used = actually used clauses for cover
  -- pss  = non-covered cases
  CoverResult SplitTree
splitTree IntSet
used [(Telescope, NAPs)]
pss [Clause]
qss IntSet
noex <- QName -> [Clause] -> SplitClause -> TCM CoverResult
cover QName
f [Clause]
cs SplitClause
sc

  -- Andreas, 2018-11-12, issue #378:
  -- some indices in @used@ and @noex@ point outside of @cs@,
  -- since missing hcomp clauses have been added during the course of @cover@.
  -- We simply delete theses indices from @noex@.
  [Int]
noex <- [Int] -> TCMT IO [Int]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Int] -> TCMT IO [Int]) -> [Int] -> TCMT IO [Int]
forall a b. (a -> b) -> a -> b
$ (Int -> Bool) -> [Int] -> [Int]
forall a. (a -> Bool) -> [a] -> [a]
List.filter (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< [Clause] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Clause]
cs) ([Int] -> [Int]) -> [Int] -> [Int]
forall a b. (a -> b) -> a -> b
$ IntSet -> [Int]
IntSet.toList IntSet
noex

  [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.top" Int
10 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
    [ TCMT IO Doc
"cover computed!"
    , [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char] -> TCMT IO Doc) -> [Char] -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [Char]
"used clauses: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ IntSet -> [Char]
forall a. Show a => a -> [Char]
show IntSet
used
    , [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char] -> TCMT IO Doc) -> [Char] -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [Char]
"non-exact clauses: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Int] -> [Char]
forall a. Show a => a -> [Char]
show [Int]
noex
    ]
  [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.splittree" Int
10 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
    [ TCMT IO Doc
"generated split tree for" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> QName -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM QName
f
    , [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char] -> TCMT IO Doc) -> [Char] -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ SplitTree -> [Char]
forall a. Pretty a => a -> [Char]
prettyShow SplitTree
splitTree
    ]
  [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.covering" Int
10 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
    [ [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char] -> TCMT IO Doc) -> [Char] -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [Char]
"covering patterns for " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ QName -> [Char]
forall a. Pretty a => a -> [Char]
prettyShow QName
f
    , Int -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => Int -> m Doc -> m Doc
nest Int
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat ([TCMT IO Doc] -> TCMT IO Doc) -> [TCMT IO Doc] -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ (Clause -> TCMT IO Doc) -> [Clause] -> [TCMT IO Doc]
forall a b. (a -> b) -> [a] -> [b]
map (\ Clause
cl -> Telescope -> TCMT IO Doc -> TCMT IO Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext (Clause -> Telescope
clauseTel Clause
cl) (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ NAPs -> TCMT IO Doc
forall (m :: * -> *). MonadPretty m => NAPs -> m Doc
prettyTCMPatternList (NAPs -> TCMT IO Doc) -> NAPs -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ Clause -> NAPs
namedClausePats Clause
cl) [Clause]
qss
    ]

  -- Storing the covering clauses so that checkIApplyConfluence_ can
  -- find them later.
  -- Andreas, 2019-03-27, only needed when --cubical
  TCMT IO Bool -> TCMT IO () -> TCMT IO ()
forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
whenM (Maybe Cubical -> Bool
forall a. Maybe a -> Bool
isJust (Maybe Cubical -> Bool)
-> (PragmaOptions -> Maybe Cubical) -> PragmaOptions -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PragmaOptions -> Maybe Cubical
optCubical (PragmaOptions -> Bool) -> TCMT IO PragmaOptions -> TCMT IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TCMT IO PragmaOptions
forall (m :: * -> *). HasOptions m => m PragmaOptions
pragmaOptions) (TCMT IO () -> TCMT IO ()) -> TCMT IO () -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ do
    (Signature -> Signature) -> TCMT IO ()
forall (m :: * -> *).
MonadTCState m =>
(Signature -> Signature) -> m ()
modifySignature ((Signature -> Signature) -> TCMT IO ())
-> (Signature -> Signature) -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ QName -> (Definition -> Definition) -> Signature -> Signature
updateDefinition QName
f ((Definition -> Definition) -> Signature -> Signature)
-> (Definition -> Definition) -> Signature -> Signature
forall a b. (a -> b) -> a -> b
$ (Defn -> Defn) -> Definition -> Definition
updateTheDef ((Defn -> Defn) -> Definition -> Definition)
-> (Defn -> Defn) -> Definition -> Definition
forall a b. (a -> b) -> a -> b
$ ([Clause] -> [Clause]) -> Defn -> Defn
updateCovering (([Clause] -> [Clause]) -> Defn -> Defn)
-> ([Clause] -> [Clause]) -> Defn -> Defn
forall a b. (a -> b) -> a -> b
$ [Clause] -> [Clause] -> [Clause]
forall a b. a -> b -> a
const [Clause]
qss


  -- filter out the missing clauses that are absurd.
  [(Telescope, NAPs)]
pss <- (((Telescope, NAPs) -> TCMT IO Bool)
 -> [(Telescope, NAPs)] -> TCMT IO [(Telescope, NAPs)])
-> [(Telescope, NAPs)]
-> ((Telescope, NAPs) -> TCMT IO Bool)
-> TCMT IO [(Telescope, NAPs)]
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((Telescope, NAPs) -> TCMT IO Bool)
-> [(Telescope, NAPs)] -> TCMT IO [(Telescope, NAPs)]
forall (m :: * -> *) a.
Applicative m =>
(a -> m Bool) -> [a] -> m [a]
filterM [(Telescope, NAPs)]
pss (((Telescope, NAPs) -> TCMT IO Bool)
 -> TCMT IO [(Telescope, NAPs)])
-> ((Telescope, NAPs) -> TCMT IO Bool)
-> TCMT IO [(Telescope, NAPs)]
forall a b. (a -> b) -> a -> b
$ \(Telescope
tel,NAPs
ps) ->
    -- Andreas, 2019-04-13, issue #3692: when adding missing absurd
    -- clauses, also put the absurd pattern in.
    TCMT IO (Either ErrorNonEmpty Int)
-> (ErrorNonEmpty -> TCMT IO Bool)
-> (Int -> TCMT IO Bool)
-> TCMT IO Bool
forall (m :: * -> *) a b c.
Monad m =>
m (Either a b) -> (a -> m c) -> (b -> m c) -> m c
caseEitherM (Range -> Telescope -> TCMT IO (Either ErrorNonEmpty Int)
checkEmptyTel Range
forall a. Range' a
noRange Telescope
tel) (\ ErrorNonEmpty
_ -> Bool -> TCMT IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True) ((Int -> TCMT IO Bool) -> TCMT IO Bool)
-> (Int -> TCMT IO Bool) -> TCMT IO Bool
forall a b. (a -> b) -> a -> b
$ \ Int
l -> do
      -- Now, @l@ is the first type in @tel@ (counting from 0=leftmost)
      -- which is empty.  Turn it into a de Bruijn index @i@.
      let i :: Int
i = Telescope -> Int
forall a. Sized a => a -> Int
size Telescope
tel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
l
      -- Build a substitution mapping this pattern variable to the absurd pattern.
      let sub :: PatternSubstitution
sub = Int -> DeBruijnPattern -> PatternSubstitution
forall a. EndoSubst a => Int -> a -> Substitution' a
inplaceS Int
i (DeBruijnPattern -> PatternSubstitution)
-> DeBruijnPattern -> PatternSubstitution
forall a b. (a -> b) -> a -> b
$ Int -> DeBruijnPattern
absurdP Int
i
        -- ifNotM (isEmptyTel tel) (return True) $ do
      -- Jesper, 2018-11-28, Issue #3407: if the clause is absurd,
      -- add the appropriate absurd clause to the definition.
      let cl :: Clause
cl = Clause { clauseLHSRange :: Range
clauseLHSRange    = Range
forall a. Range' a
noRange
                      , clauseFullRange :: Range
clauseFullRange   = Range
forall a. Range' a
noRange
                      , clauseTel :: Telescope
clauseTel         = Telescope
tel
                      , namedClausePats :: NAPs
namedClausePats   = Substitution' (SubstArg NAPs) -> NAPs -> NAPs
forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst PatternSubstitution
Substitution' (SubstArg NAPs)
sub NAPs
ps
                      , clauseBody :: Maybe Term
clauseBody        = Maybe Term
forall a. Maybe a
Nothing
                      , clauseType :: Maybe (Arg Type)
clauseType        = Maybe (Arg Type)
forall a. Maybe a
Nothing
                      , clauseCatchall :: Bool
clauseCatchall    = Bool
True       -- absurd clauses are safe as catch-all
                      , clauseExact :: Maybe Bool
clauseExact       = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False
                      , clauseRecursive :: Maybe Bool
clauseRecursive   = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False
                      , clauseUnreachable :: Maybe Bool
clauseUnreachable = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False
                      , clauseEllipsis :: ExpandedEllipsis
clauseEllipsis    = ExpandedEllipsis
NoEllipsis
                      }
      [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.missing" Int
20 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ TCMT IO Doc -> TCMT IO Doc
forall (tcm :: * -> *) a.
(MonadTCEnv tcm, ReadTCState tcm) =>
tcm a -> tcm a
inTopContext (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ do
        [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
sep [ TCMT IO Doc
"adding missing absurd clause"
            , Int -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => Int -> m Doc -> m Doc
nest Int
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ QNamed Clause -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM (QNamed Clause -> TCMT IO Doc) -> QNamed Clause -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ QName -> Clause -> QNamed Clause
forall a. QName -> a -> QNamed a
QNamed QName
f Clause
cl
            ]
      [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.missing" Int
80 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ TCMT IO Doc -> TCMT IO Doc
forall (tcm :: * -> *) a.
(MonadTCEnv tcm, ReadTCState tcm) =>
tcm a -> tcm a
inTopContext (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
        [ TCMT IO Doc
"l   = " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Int -> TCMT IO Doc
forall (m :: * -> *) a. (Applicative m, Pretty a) => a -> m Doc
pretty Int
l
        , TCMT IO Doc
"i   = " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Int -> TCMT IO Doc
forall (m :: * -> *) a. (Applicative m, Pretty a) => a -> m Doc
pretty Int
i
        , TCMT IO Doc
"cl  = " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> QNamed Clause -> TCMT IO Doc
forall (m :: * -> *) a. (Applicative m, Pretty a) => a -> m Doc
pretty (QName -> Clause -> QNamed Clause
forall a. QName -> a -> QNamed a
QNamed QName
f Clause
cl)
        ]
      QName -> [Clause] -> TCMT IO ()
addClauses QName
f [Clause
cl]
      Bool -> TCMT IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False

  -- report a warning if there are uncovered cases,
  Bool -> TCMT IO () -> TCMT IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([(Telescope, NAPs)] -> Bool
forall a. Null a => a -> Bool
null [(Telescope, NAPs)]
pss) (TCMT IO () -> TCMT IO ()) -> TCMT IO () -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ do
    Lens' (Set QName) TCState
stLocalPartialDefs Lens' (Set QName) TCState -> (Set QName -> Set QName) -> TCMT IO ()
forall (m :: * -> *) a.
MonadTCState m =>
Lens' a TCState -> (a -> a) -> m ()
`modifyTCLens` QName -> Set QName -> Set QName
forall a. Ord a => a -> Set a -> Set a
Set.insert QName
f
    TCMT IO Bool -> TCMT IO () -> TCMT IO ()
forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
whenM ((CoverageCheck
YesCoverageCheck CoverageCheck -> CoverageCheck -> Bool
forall a. Eq a => a -> a -> Bool
==) (CoverageCheck -> Bool) -> TCMT IO CoverageCheck -> TCMT IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Lens' CoverageCheck TCEnv -> TCMT IO CoverageCheck
forall (m :: * -> *) a. MonadTCEnv m => Lens' a TCEnv -> m a
viewTC Lens' CoverageCheck TCEnv
eCoverageCheck) (TCMT IO () -> TCMT IO ()) -> TCMT IO () -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$
      [Clause] -> TCMT IO () -> TCMT IO ()
forall (m :: * -> *) x a.
(MonadTrace m, HasRange x) =>
x -> m a -> m a
setCurrentRange [Clause]
cs (TCMT IO () -> TCMT IO ()) -> TCMT IO () -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ Warning -> TCMT IO ()
forall (m :: * -> *).
(HasCallStack, MonadWarning m) =>
Warning -> m ()
warning (Warning -> TCMT IO ()) -> Warning -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ QName -> [(Telescope, NAPs)] -> Warning
CoverageIssue QName
f [(Telescope, NAPs)]
pss

  -- Andreas, 2017-08-28, issue #2723:
  -- Mark clauses as reachable or unreachable in the signature.
  -- Andreas, 2020-11-19, issue #5065
  -- Remember whether clauses are exact or not.
  let ([Maybe Int]
is0, [Clause]
cs1) = [(Maybe Int, Clause)] -> ([Maybe Int], [Clause])
forall a b. [(a, b)] -> ([a], [b])
unzip ([(Maybe Int, Clause)] -> ([Maybe Int], [Clause]))
-> [(Maybe Int, Clause)] -> ([Maybe Int], [Clause])
forall a b. (a -> b) -> a -> b
$ [(Int, Clause)]
-> ((Int, Clause) -> (Maybe Int, Clause)) -> [(Maybe Int, Clause)]
forall (m :: * -> *) a b. Functor m => m a -> (a -> b) -> m b
for ([Int] -> [Clause] -> [(Int, Clause)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0..] [Clause]
cs) (((Int, Clause) -> (Maybe Int, Clause)) -> [(Maybe Int, Clause)])
-> ((Int, Clause) -> (Maybe Int, Clause)) -> [(Maybe Int, Clause)]
forall a b. (a -> b) -> a -> b
$ \ (Int
i, Clause
cl) -> let
          unreachable :: Bool
unreachable = Int
i Int -> IntSet -> Bool
`IntSet.notMember` IntSet
used
          exact :: Bool
exact       = Int
i Int -> IntSet -> Bool
`IntSet.notMember` ([Int] -> IntSet
IntSet.fromList [Int]
noex)
        in (Bool -> Int -> Maybe Int
forall a. Bool -> a -> Maybe a
boolToMaybe Bool
unreachable Int
i, Clause
cl
             { clauseUnreachable :: Maybe Bool
clauseUnreachable = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
unreachable
             , clauseExact :: Maybe Bool
clauseExact       = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
exact
             })
  -- is = indices of unreachable clauses
  let is :: [Int]
is = [Maybe Int] -> [Int]
forall a. [Maybe a] -> [a]
catMaybes [Maybe Int]
is0
  [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.top" Int
10 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
    [ [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char] -> TCMT IO Doc) -> [Char] -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [Char]
"unreachable clauses: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ if [Int] -> Bool
forall a. Null a => a -> Bool
null [Int]
is then [Char]
"(none)" else [Int] -> [Char]
forall a. Show a => a -> [Char]
show [Int]
is
    ]
  -- Replace the first clauses by @cs1@.  There might be more
  -- added by @inferMissingClause@.
  QName -> ([Clause] -> [Clause]) -> TCMT IO ()
modifyFunClauses QName
f (([Clause] -> [Clause]) -> TCMT IO ())
-> ([Clause] -> [Clause]) -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ \ [Clause]
cs0 -> [Clause]
cs1 [Clause] -> [Clause] -> [Clause]
forall a. [a] -> [a] -> [a]
++ Int -> [Clause] -> [Clause]
forall a. Int -> [a] -> [a]
drop ([Clause] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Clause]
cs1) [Clause]
cs0

  -- Warn if there are unreachable clauses and mark them as unreachable.
  Bool -> TCMT IO () -> TCMT IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([Int] -> Bool
forall a. Null a => a -> Bool
null [Int]
is) (TCMT IO () -> TCMT IO ()) -> TCMT IO () -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ do
    -- Warn about unreachable clauses.
    let unreached :: [Clause]
unreached = (Clause -> Bool) -> [Clause] -> [Clause]
forall a. (a -> Bool) -> [a] -> [a]
filter ((Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True Maybe Bool -> Maybe Bool -> Bool
forall a. Eq a => a -> a -> Bool
==) (Maybe Bool -> Bool) -> (Clause -> Maybe Bool) -> Clause -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Clause -> Maybe Bool
clauseUnreachable) [Clause]
cs1
    let ranges :: [Range]
ranges    = (Clause -> Range) -> [Clause] -> [Range]
forall a b. (a -> b) -> [a] -> [b]
map Clause -> Range
clauseFullRange [Clause]
unreached
    [Range] -> TCMT IO () -> TCMT IO ()
forall (m :: * -> *) x a.
(MonadTrace m, HasRange x) =>
x -> m a -> m a
setCurrentRange [Range]
ranges (TCMT IO () -> TCMT IO ()) -> TCMT IO () -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ Warning -> TCMT IO ()
forall (m :: * -> *).
(HasCallStack, MonadWarning m) =>
Warning -> m ()
warning (Warning -> TCMT IO ()) -> Warning -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ QName -> [Range] -> Warning
UnreachableClauses QName
f [Range]
ranges

  -- report a warning if there are clauses that are not preserved as
  -- definitional equalities and --exact-split is enabled
  Bool -> TCMT IO () -> TCMT IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([Int] -> Bool
forall a. Null a => a -> Bool
null [Int]
noex) (TCMT IO () -> TCMT IO ()) -> TCMT IO () -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ do
      let noexclauses :: [Clause]
noexclauses = (Int -> Clause) -> [Int] -> [Clause]
forall a b. (a -> b) -> [a] -> [b]
map (Clause -> [Clause] -> Int -> Clause
forall a. a -> [a] -> Int -> a
indexWithDefault Clause
forall a. HasCallStack => a
__IMPOSSIBLE__ [Clause]
cs1) [Int]
noex
      [Range] -> TCMT IO () -> TCMT IO ()
forall (m :: * -> *) x a.
(MonadTrace m, HasRange x) =>
x -> m a -> m a
setCurrentRange ((Clause -> Range) -> [Clause] -> [Range]
forall a b. (a -> b) -> [a] -> [b]
map Clause -> Range
clauseLHSRange [Clause]
noexclauses) (TCMT IO () -> TCMT IO ()) -> TCMT IO () -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$
        Warning -> TCMT IO ()
forall (m :: * -> *).
(HasCallStack, MonadWarning m) =>
Warning -> m ()
warning (Warning -> TCMT IO ()) -> Warning -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ QName -> [Clause] -> Warning
CoverageNoExactSplit QName
f ([Clause] -> Warning) -> [Clause] -> Warning
forall a b. (a -> b) -> a -> b
$ [Clause]
noexclauses
  SplitTree -> TCM SplitTree
forall (m :: * -> *) a. Monad m => a -> m a
return SplitTree
splitTree

-- | Top-level function for eliminating redundant clauses in the interactive
--   case splitter
isCovered :: QName -> [Clause] -> SplitClause -> TCM Bool
isCovered :: QName -> [Clause] -> SplitClause -> TCMT IO Bool
isCovered QName
f [Clause]
cs SplitClause
sc = do
  [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.isCovered" Int
20 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
    [ TCMT IO Doc
"isCovered"
    , Int -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => Int -> m Doc -> m Doc
nest Int
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat ([TCMT IO Doc] -> TCMT IO Doc) -> [TCMT IO Doc] -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$
      [ TCMT IO Doc
"f  = " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> QName -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM QName
f
      , TCMT IO Doc
"cs = " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat ((Clause -> TCMT IO Doc) -> [Clause] -> [TCMT IO Doc]
forall a b. (a -> b) -> [a] -> [b]
map (Int -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => Int -> m Doc -> m Doc
nest Int
2 (TCMT IO Doc -> TCMT IO Doc)
-> (Clause -> TCMT IO Doc) -> Clause -> TCMT IO Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NamedClause -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM (NamedClause -> TCMT IO Doc)
-> (Clause -> NamedClause) -> Clause -> TCMT IO Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. QName -> Bool -> Clause -> NamedClause
NamedClause QName
f Bool
True) [Clause]
cs)
      , TCMT IO Doc
"sc = " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> SplitClause -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM SplitClause
sc
      ]
    ]
  -- Jesper, 2019-10: introduce trailing arguments (see #3828)
  (Telescope
_ , SplitClause
sc') <- Bool -> SplitClause -> TCM (Telescope, SplitClause)
insertTrailingArgs Bool
True SplitClause
sc
  CoverResult { coverMissingClauses :: CoverResult -> [(Telescope, NAPs)]
coverMissingClauses = [(Telescope, NAPs)]
missing } <- QName -> [Clause] -> SplitClause -> TCM CoverResult
cover QName
f [Clause]
cs SplitClause
sc'
  Bool -> TCMT IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> TCMT IO Bool) -> Bool -> TCMT IO Bool
forall a b. (a -> b) -> a -> b
$ [(Telescope, NAPs)] -> Bool
forall a. Null a => a -> Bool
null [(Telescope, NAPs)]
missing
 -- Andreas, 2019-08-08 and 2020-02-11
 -- If there is an error (e.g. unification error), don't report it
 -- to the user.  Rather, assume the clause is not already covered.
 TCMT IO Bool -> (TCErr -> TCMT IO Bool) -> TCMT IO Bool
forall e (m :: * -> *) a.
MonadError e m =>
m a -> (e -> m a) -> m a
`catchError` \ TCErr
_ -> Bool -> TCMT IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False

data CoverResult = CoverResult
  { CoverResult -> SplitTree
coverSplitTree       :: SplitTree
  , CoverResult -> IntSet
coverUsedClauses     :: IntSet -- Set Nat
  , CoverResult -> [(Telescope, NAPs)]
coverMissingClauses  :: [(Telescope, [NamedArg DeBruijnPattern])]
  , CoverResult -> [Clause]
coverPatterns        :: [Clause]
  -- ^ The set of patterns used as cover.
  , CoverResult -> IntSet
coverNoExactClauses  :: IntSet -- Set Nat
  }

-- | @cover f cs (SClause _ _ ps _) = return (splitTree, used, pss)@.
--   checks that the list of clauses @cs@ covers the given split clause.
--   Returns the @splitTree@, the @used@ clauses, and missing cases @pss@.
--
--   Effect: adds missing instance clauses for @f@ to signature.
--
cover :: QName -> [Clause] -> SplitClause ->
         TCM CoverResult
cover :: QName -> [Clause] -> SplitClause -> TCM CoverResult
cover QName
f [Clause]
cs sc :: SplitClause
sc@(SClause Telescope
tel [NamedArg SplitPattern]
ps Substitution' SplitPattern
_ Map CheckpointId (Substitution' Term)
_ Maybe (Dom Type)
target) = TCM CoverResult -> TCM CoverResult
forall a. TCM a -> TCM a
updateRelevance (TCM CoverResult -> TCM CoverResult)
-> TCM CoverResult -> TCM CoverResult
forall a b. (a -> b) -> a -> b
$ do
  [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.cover" Int
10 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ TCMT IO Doc -> TCMT IO Doc
forall (tcm :: * -> *) a.
(MonadTCEnv tcm, ReadTCState tcm) =>
tcm a -> tcm a
inTopContext (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
    [ TCMT IO Doc
"checking coverage of pattern:"
    , Int -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => Int -> m Doc -> m Doc
nest Int
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ SplitClause -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM SplitClause
sc
    , Int -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => Int -> m Doc -> m Doc
nest Int
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ TCMT IO Doc
"target sort =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> do Telescope -> TCMT IO Doc -> TCMT IO Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext Telescope
tel (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ TCMT IO Doc
-> (Dom Type -> TCMT IO Doc) -> Maybe (Dom Type) -> TCMT IO Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text [Char]
"<none>") (Sort -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM (Sort -> TCMT IO Doc)
-> (Dom Type -> Sort) -> Dom Type -> TCMT IO Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Type -> Sort
forall a. LensSort a => a -> Sort
getSort (Type -> Sort) -> (Dom Type -> Type) -> Dom Type -> Sort
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dom Type -> Type
forall t e. Dom' t e -> e
unDom) Maybe (Dom Type)
target
    ]
  [Char] -> Int -> [Char] -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> [Char] -> m ()
reportSLn [Char]
"tc.cover.cover" Int
80 ([Char] -> TCMT IO ()) -> [Char] -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
"raw target =\n" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Maybe (Dom Type) -> [Char]
forall a. Show a => a -> [Char]
show Maybe (Dom Type)
target
  [Clause]
-> [NamedArg SplitPattern]
-> TCMT IO (Match (Int, [(Int, SplitPattern)]))
forall (m :: * -> *).
PureTCM m =>
[Clause]
-> [NamedArg SplitPattern]
-> m (Match (Int, [(Int, SplitPattern)]))
match [Clause]
cs [NamedArg SplitPattern]
ps TCMT IO (Match (Int, [(Int, SplitPattern)]))
-> (Match (Int, [(Int, SplitPattern)]) -> TCM CoverResult)
-> TCM CoverResult
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Yes (Int
i,[(Int, SplitPattern)]
mps) -> do
      [Char] -> Int -> [Char] -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> [Char] -> m ()
reportSLn [Char]
"tc.cover.cover" Int
10 ([Char] -> TCMT IO ()) -> [Char] -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
"pattern covered by clause " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
i
      [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.cover" Int
20 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text [Char]
"with mps = " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> do Telescope -> TCMT IO Doc -> TCMT IO Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext Telescope
tel (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [(Int, SplitPattern)] -> TCMT IO Doc
forall (m :: * -> *) a. (Applicative m, Pretty a) => a -> m Doc
pretty [(Int, SplitPattern)]
mps
      Bool
exact <- [(Int, SplitPattern)]
-> ((Int, SplitPattern) -> TCMT IO Bool) -> TCMT IO Bool
forall (f :: * -> *) (m :: * -> *) a.
(Functor f, Foldable f, Monad m) =>
f a -> (a -> m Bool) -> m Bool
allM [(Int, SplitPattern)]
mps (((Int, SplitPattern) -> TCMT IO Bool) -> TCMT IO Bool)
-> ((Int, SplitPattern) -> TCMT IO Bool) -> TCMT IO Bool
forall a b. (a -> b) -> a -> b
$ SplitPattern -> TCMT IO Bool
forall (m :: * -> *) a. HasConstInfo m => Pattern' a -> m Bool
isTrivialPattern (SplitPattern -> TCMT IO Bool)
-> ((Int, SplitPattern) -> SplitPattern)
-> (Int, SplitPattern)
-> TCMT IO Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int, SplitPattern) -> SplitPattern
forall a b. (a, b) -> b
snd
      let cl0 :: Clause
cl0 = Clause -> [Clause] -> Int -> Clause
forall a. a -> [a] -> Int -> a
indexWithDefault Clause
forall a. HasCallStack => a
__IMPOSSIBLE__ [Clause]
cs Int
i
      Clause
cl <- SplitClause -> Clause -> [(Int, SplitPattern)] -> TCM Clause
applyCl SplitClause
sc Clause
cl0 [(Int, SplitPattern)]
mps
      CoverResult -> TCM CoverResult
forall (m :: * -> *) a. Monad m => a -> m a
return (CoverResult -> TCM CoverResult) -> CoverResult -> TCM CoverResult
forall a b. (a -> b) -> a -> b
$ CoverResult
        { coverSplitTree :: SplitTree
coverSplitTree      = Int -> SplitTree
forall a. Int -> SplitTree' a
SplittingDone (Telescope -> Int
forall a. Sized a => a -> Int
size Telescope
tel)
        , coverUsedClauses :: IntSet
coverUsedClauses    = Int -> IntSet
forall el coll. Singleton el coll => el -> coll
singleton Int
i
        , coverMissingClauses :: [(Telescope, NAPs)]
coverMissingClauses = []
        , coverPatterns :: [Clause]
coverPatterns       = [Clause
cl]
        , coverNoExactClauses :: IntSet
coverNoExactClauses = [Int] -> IntSet
IntSet.fromList [ Int
i | Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Bool
exact Bool -> Bool -> Bool
|| Clause -> Bool
clauseCatchall Clause
cl0 ]
        }

    Match (Int, [(Int, SplitPattern)])
No        ->  do
      [Char] -> Int -> [Char] -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> [Char] -> m ()
reportSLn [Char]
"tc.cover" Int
20 ([Char] -> TCMT IO ()) -> [Char] -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
"pattern is not covered"
      let infer :: Dom' a e -> Bool
infer Dom' a e
dom = Dom' a e -> Bool
forall a. LensHiding a => a -> Bool
isInstance Dom' a e
dom Bool -> Bool -> Bool
|| Maybe a -> Bool
forall a. Maybe a -> Bool
isJust (Dom' a e -> Maybe a
forall t e. Dom' t e -> Maybe t
domTactic Dom' a e
dom)
      if Bool -> (Dom Type -> Bool) -> Maybe (Dom Type) -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False Dom Type -> Bool
forall {a} {e}. Dom' a e -> Bool
infer Maybe (Dom Type)
target
        then do
          -- Ulf, 2016-10-31: For now we only infer instance clauses. It would
          -- make sense to do it also for hidden, but since the value of a
          -- hidden clause is expected to be forced by later clauses, it's too
          -- late to add it now. If it was inferrable we would have gotten a
          -- type error before getting to this point.
          -- Ulf, 2019-11-21: Also @tactic clauses.
          Clause
cl <- QName -> SplitClause -> TCM Clause
inferMissingClause QName
f SplitClause
sc
          CoverResult -> TCM CoverResult
forall (m :: * -> *) a. Monad m => a -> m a
return (CoverResult -> TCM CoverResult) -> CoverResult -> TCM CoverResult
forall a b. (a -> b) -> a -> b
$ SplitTree
-> IntSet
-> [(Telescope, NAPs)]
-> [Clause]
-> IntSet
-> CoverResult
CoverResult (Int -> SplitTree
forall a. Int -> SplitTree' a
SplittingDone (Telescope -> Int
forall a. Sized a => a -> Int
size Telescope
tel)) IntSet
forall a. Null a => a
empty [] [Clause
cl] IntSet
forall a. Null a => a
empty
        else do
          let ps' :: NAPs
ps' = [NamedArg SplitPattern] -> NAPs
fromSplitPatterns [NamedArg SplitPattern]
ps
          CoverResult -> TCM CoverResult
forall (m :: * -> *) a. Monad m => a -> m a
return (CoverResult -> TCM CoverResult) -> CoverResult -> TCM CoverResult
forall a b. (a -> b) -> a -> b
$ SplitTree
-> IntSet
-> [(Telescope, NAPs)]
-> [Clause]
-> IntSet
-> CoverResult
CoverResult (Int -> SplitTree
forall a. Int -> SplitTree' a
SplittingDone (Telescope -> Int
forall a. Sized a => a -> Int
size Telescope
tel)) IntSet
forall a. Null a => a
empty [(Telescope
tel, NAPs
ps')] [] IntSet
forall a. Null a => a
empty

    -- We need to split!
    -- If all clauses have an unsplit copattern, we try that first.
    Block BlockedOnResult
res BlockingVars
bs -> BlockedOnResult
-> Bool
-> (SplitError -> TCM CoverResult)
-> TCM CoverResult
-> TCM CoverResult
trySplitRes BlockedOnResult
res (BlockingVars -> Bool
forall a. Null a => a -> Bool
null BlockingVars
bs) SplitError -> TCM CoverResult
forall a. SplitError -> TCM a
splitError (TCM CoverResult -> TCM CoverResult)
-> TCM CoverResult -> TCM CoverResult
forall a b. (a -> b) -> a -> b
$ do
      Bool -> TCMT IO () -> TCMT IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (BlockingVars -> Bool
forall a. Null a => a -> Bool
null BlockingVars
bs) TCMT IO ()
forall a. HasCallStack => a
__IMPOSSIBLE__
      -- Otherwise, if there are variables to split, we try them
      -- in the order determined by a split strategy.
      [Char] -> Int -> [Char] -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> [Char] -> m ()
reportSLn [Char]
"tc.cover.strategy" Int
20 ([Char] -> TCMT IO ()) -> [Char] -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
"blocking vars = " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ BlockingVars -> [Char]
forall a. Pretty a => a -> [Char]
prettyShow BlockingVars
bs
      -- xs is a non-empty lists of blocking variables
      -- try splitting on one of them
      BlockingVars
xs <- BlockingVars -> Telescope -> TCM BlockingVars
splitStrategy BlockingVars
bs Telescope
tel
      -- Andreas, 2017-10-08, issue #2594
      -- First, try to find split order for complete coverage.
      -- If this fails, try to at least carry out the splitting to the end.
      BlockingVars
-> AllowPartialCover
-> (SplitError -> TCM CoverResult)
-> TCM CoverResult
continue BlockingVars
xs AllowPartialCover
NoAllowPartialCover ((SplitError -> TCM CoverResult) -> TCM CoverResult)
-> (SplitError -> TCM CoverResult) -> TCM CoverResult
forall a b. (a -> b) -> a -> b
$ \ SplitError
_err -> do
        BlockingVars
-> AllowPartialCover
-> (SplitError -> TCM CoverResult)
-> TCM CoverResult
continue BlockingVars
xs AllowPartialCover
YesAllowPartialCover ((SplitError -> TCM CoverResult) -> TCM CoverResult)
-> (SplitError -> TCM CoverResult) -> TCM CoverResult
forall a b. (a -> b) -> a -> b
$ \ SplitError
err -> do
          SplitError -> TCM CoverResult
forall a. SplitError -> TCM a
splitError SplitError
err
  where
    -- Andreas, 2019-08-07, issue #3966
    -- When we get a SplitError, tighten the error Range to the clauses
    -- that are still candidates for covering the SplitClause.
    splitError :: SplitError -> TCM a
    splitError :: forall a. SplitError -> TCM a
splitError = TCM a -> TCM a
forall a. TCM a -> TCM a
withRangeOfCandidateClauses (TCM a -> TCM a) -> (SplitError -> TCM a) -> SplitError -> TCM a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TypeError -> TCM a
forall (m :: * -> *) a.
(HasCallStack, MonadTCError m) =>
TypeError -> m a
typeError (TypeError -> TCM a)
-> (SplitError -> TypeError) -> SplitError -> TCM a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SplitError -> TypeError
SplitError

    -- This repeats the matching, but since we are crashing anyway,
    -- the extra work just to compute a better Range does not matter.
    withRangeOfCandidateClauses :: TCM a -> TCM a
    withRangeOfCandidateClauses :: forall a. TCM a -> TCM a
withRangeOfCandidateClauses TCM a
cont = do
      [Clause]
cands <- ((Clause, Match [(Int, SplitPattern)]) -> Maybe Clause)
-> [(Clause, Match [(Int, SplitPattern)])] -> [Clause]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe ((Clause -> Match [(Int, SplitPattern)] -> Maybe Clause)
-> (Clause, Match [(Int, SplitPattern)]) -> Maybe Clause
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Clause -> Match [(Int, SplitPattern)] -> Maybe Clause
forall a. Clause -> Match a -> Maybe Clause
notNo) ([(Clause, Match [(Int, SplitPattern)])] -> [Clause])
-> ([Match [(Int, SplitPattern)]]
    -> [(Clause, Match [(Int, SplitPattern)])])
-> [Match [(Int, SplitPattern)]]
-> [Clause]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Clause]
-> [Match [(Int, SplitPattern)]]
-> [(Clause, Match [(Int, SplitPattern)])]
forall a b. [a] -> [b] -> [(a, b)]
zip [Clause]
cs ([Match [(Int, SplitPattern)]] -> [Clause])
-> TCMT IO [Match [(Int, SplitPattern)]] -> TCMT IO [Clause]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Clause -> TCMT IO (Match [(Int, SplitPattern)]))
-> [Clause] -> TCMT IO [Match [(Int, SplitPattern)]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ([NamedArg SplitPattern]
-> Clause -> TCMT IO (Match [(Int, SplitPattern)])
forall (m :: * -> *).
PureTCM m =>
[NamedArg SplitPattern]
-> Clause -> m (Match [(Int, SplitPattern)])
matchClause [NamedArg SplitPattern]
ps) [Clause]
cs
      [Clause] -> TCM a -> TCM a
forall (m :: * -> *) x a.
(MonadTrace m, HasRange x) =>
x -> m a -> m a
setCurrentRange [Clause]
cands TCM a
cont
      where
        notNo :: Clause -> Match a -> Maybe Clause
        notNo :: forall a. Clause -> Match a -> Maybe Clause
notNo Clause
c = \case
          Yes{}   -> Clause -> Maybe Clause
forall a. a -> Maybe a
Just Clause
c
          Block{} -> Clause -> Maybe Clause
forall a. a -> Maybe a
Just Clause
c
          No{}    -> Maybe Clause
forall a. Maybe a
Nothing

    applyCl :: SplitClause -> Clause -> [(Nat, SplitPattern)] -> TCM Clause
    applyCl :: SplitClause -> Clause -> [(Int, SplitPattern)] -> TCM Clause
applyCl SClause{scTel :: SplitClause -> Telescope
scTel = Telescope
tel, scPats :: SplitClause -> [NamedArg SplitPattern]
scPats = [NamedArg SplitPattern]
sps} Clause
cl [(Int, SplitPattern)]
mps = Telescope -> TCM Clause -> TCM Clause
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext Telescope
tel (TCM Clause -> TCM Clause) -> TCM Clause -> TCM Clause
forall a b. (a -> b) -> a -> b
$ do
        let ps :: NAPs
ps = Clause -> NAPs
namedClausePats Clause
cl
        [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.applyCl" Int
40 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ TCMT IO Doc
"applyCl"
        [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.applyCl" Int
40 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ TCMT IO Doc
"tel    =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Telescope -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM Telescope
tel
        [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.applyCl" Int
40 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ TCMT IO Doc
"ps     =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> NAPs -> TCMT IO Doc
forall (m :: * -> *) a. (Applicative m, Pretty a) => a -> m Doc
pretty NAPs
ps
        [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.applyCl" Int
40 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ TCMT IO Doc
"mps    =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> [(Int, SplitPattern)] -> TCMT IO Doc
forall (m :: * -> *) a. (Applicative m, Pretty a) => a -> m Doc
pretty [(Int, SplitPattern)]
mps
        [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.applyCl" Int
40 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ TCMT IO Doc
"s      =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> PatternSubstitution -> TCMT IO Doc
forall (m :: * -> *) a. (Applicative m, Pretty a) => a -> m Doc
pretty PatternSubstitution
s
        [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.applyCl" Int
40 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ TCMT IO Doc
"ps[s]  =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> NAPs -> TCMT IO Doc
forall (m :: * -> *) a. (Applicative m, Pretty a) => a -> m Doc
pretty (PatternSubstitution
Substitution' (SubstArg NAPs)
s Substitution' (SubstArg NAPs) -> NAPs -> NAPs
forall a. Subst a => Substitution' (SubstArg a) -> a -> a
`applySubst` NAPs
ps)

        -- If a matching clause has fewer patterns than the split
        -- clause we ought to copy over the extra ones.
        -- e.g. if the user wrote:
        --
        --   bar : Bool -> Bool
        --   bar false = false
        --   bar = \ _ -> true
        --
        -- then for the second clause the @extra@ patterns will be @[true]@.

        let extra :: NAPs
extra = Int -> NAPs -> NAPs
forall a. Int -> [a] -> [a]
drop (NAPs -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length NAPs
ps) (NAPs -> NAPs) -> NAPs -> NAPs
forall a b. (a -> b) -> a -> b
$ [NamedArg SplitPattern] -> NAPs
fromSplitPatterns [NamedArg SplitPattern]
sps
            n_extra :: Int
n_extra = NAPs -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length NAPs
extra

        [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.applyCl" Int
40 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ TCMT IO Doc
"extra  =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> NAPs -> TCMT IO Doc
forall (m :: * -> *) a. (Applicative m, Pretty a) => a -> m Doc
pretty NAPs
extra

        -- When we add the extra patterns we also update the type
        -- and the body of the clause.

        Maybe (Arg (TelV Type))
mtv <- ((Arg Type -> TCMT IO (Arg (TelV Type)))
-> Maybe (Arg Type) -> TCMT IO (Maybe (Arg (TelV Type)))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse ((Arg Type -> TCMT IO (Arg (TelV Type)))
 -> Maybe (Arg Type) -> TCMT IO (Maybe (Arg (TelV Type))))
-> ((Type -> TCMT IO (TelV Type))
    -> Arg Type -> TCMT IO (Arg (TelV Type)))
-> (Type -> TCMT IO (TelV Type))
-> Maybe (Arg Type)
-> TCMT IO (Maybe (Arg (TelV Type)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Type -> TCMT IO (TelV Type))
-> Arg Type -> TCMT IO (Arg (TelV Type))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse) (Int -> Type -> TCMT IO (TelV Type)
forall (m :: * -> *). PureTCM m => Int -> Type -> m (TelV Type)
telViewUpToPath Int
n_extra) (Maybe (Arg Type) -> TCMT IO (Maybe (Arg (TelV Type))))
-> Maybe (Arg Type) -> TCMT IO (Maybe (Arg (TelV Type)))
forall a b. (a -> b) -> a -> b
$ Clause -> Maybe (Arg Type)
clauseType Clause
cl
        let ty :: Maybe (Arg Type)
ty = ((Arg (TelV Type) -> Arg Type)
-> Maybe (Arg (TelV Type)) -> Maybe (Arg Type)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Arg (TelV Type) -> Arg Type)
 -> Maybe (Arg (TelV Type)) -> Maybe (Arg Type))
-> ((TelV Type -> Type) -> Arg (TelV Type) -> Arg Type)
-> (TelV Type -> Type)
-> Maybe (Arg (TelV Type))
-> Maybe (Arg Type)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TelV Type -> Type) -> Arg (TelV Type) -> Arg Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) (([DeBruijnPattern] -> PatternSubstitution
forall a. DeBruijn a => [a] -> Substitution' a
parallelS ([DeBruijnPattern] -> [DeBruijnPattern]
forall a. [a] -> [a]
reverse ([DeBruijnPattern] -> [DeBruijnPattern])
-> [DeBruijnPattern] -> [DeBruijnPattern]
forall a b. (a -> b) -> a -> b
$ (Arg (Named_ DeBruijnPattern) -> DeBruijnPattern)
-> NAPs -> [DeBruijnPattern]
forall a b. (a -> b) -> [a] -> [b]
map Arg (Named_ DeBruijnPattern) -> DeBruijnPattern
forall a. NamedArg a -> a
namedArg NAPs
extra) PatternSubstitution -> PatternSubstitution -> PatternSubstitution
forall a.
EndoSubst a =>
Substitution' a -> Substitution' a -> Substitution' a
`composeS` Int -> PatternSubstitution -> PatternSubstitution
forall a. Int -> Substitution' a -> Substitution' a
liftS Int
n_extra PatternSubstitution
s PatternSubstitution -> Type -> Type
forall a. TermSubst a => PatternSubstitution -> a -> a
`applyPatSubst`) (Type -> Type) -> (TelV Type -> Type) -> TelV Type -> Type
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TelV Type -> Type
forall a. TelV a -> a
theCore) Maybe (Arg (TelV Type))
mtv

        [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.applyCl" Int
40 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ TCMT IO Doc
"new ty =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Maybe (Arg Type) -> TCMT IO Doc
forall (m :: * -> *) a. (Applicative m, Pretty a) => a -> m Doc
pretty Maybe (Arg Type)
ty

        Clause -> TCM Clause
forall (m :: * -> *) a. Monad m => a -> m a
return (Clause -> TCM Clause) -> Clause -> TCM Clause
forall a b. (a -> b) -> a -> b
$
             Clause { clauseLHSRange :: Range
clauseLHSRange  = Clause -> Range
clauseLHSRange Clause
cl
                    , clauseFullRange :: Range
clauseFullRange = Clause -> Range
clauseFullRange Clause
cl
                    , clauseTel :: Telescope
clauseTel       = Telescope
tel
                    , namedClausePats :: NAPs
namedClausePats = (PatternSubstitution
Substitution' (SubstArg NAPs)
s Substitution' (SubstArg NAPs) -> NAPs -> NAPs
forall a. Subst a => Substitution' (SubstArg a) -> a -> a
`applySubst` NAPs
ps) NAPs -> NAPs -> NAPs
forall a. [a] -> [a] -> [a]
++ NAPs
extra
                    , clauseBody :: Maybe Term
clauseBody      = (Term -> [Elim] -> Term
forall t. Apply t => t -> [Elim] -> t
`applyE` NAPs -> [Elim]
patternsToElims NAPs
extra) (Term -> Term) -> (Term -> Term) -> Term -> Term
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PatternSubstitution
s PatternSubstitution -> Term -> Term
forall a. TermSubst a => PatternSubstitution -> a -> a
`applyPatSubst`) (Term -> Term) -> Maybe Term -> Maybe Term
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Clause -> Maybe Term
clauseBody Clause
cl
                    , clauseType :: Maybe (Arg Type)
clauseType      = Maybe (Arg Type)
ty
                    , clauseCatchall :: Bool
clauseCatchall  = Clause -> Bool
clauseCatchall Clause
cl
                    , clauseExact :: Maybe Bool
clauseExact     = Clause -> Maybe Bool
clauseExact Clause
cl
                    , clauseRecursive :: Maybe Bool
clauseRecursive = Clause -> Maybe Bool
clauseRecursive Clause
cl
                    , clauseUnreachable :: Maybe Bool
clauseUnreachable = Clause -> Maybe Bool
clauseUnreachable Clause
cl
                    , clauseEllipsis :: ExpandedEllipsis
clauseEllipsis  = Clause -> ExpandedEllipsis
clauseEllipsis Clause
cl
                    }
      where
        ([Int]
vs,[SplitPattern]
qs) = [(Int, SplitPattern)] -> ([Int], [SplitPattern])
forall a b. [(a, b)] -> ([a], [b])
unzip [(Int, SplitPattern)]
mps
        mps' :: [(Int, DeBruijnPattern)]
mps' = [Int] -> [DeBruijnPattern] -> [(Int, DeBruijnPattern)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int]
vs ([DeBruijnPattern] -> [(Int, DeBruijnPattern)])
-> [DeBruijnPattern] -> [(Int, DeBruijnPattern)]
forall a b. (a -> b) -> a -> b
$ (Arg (Named_ DeBruijnPattern) -> DeBruijnPattern)
-> NAPs -> [DeBruijnPattern]
forall a b. (a -> b) -> [a] -> [b]
map Arg (Named_ DeBruijnPattern) -> DeBruijnPattern
forall a. NamedArg a -> a
namedArg (NAPs -> [DeBruijnPattern]) -> NAPs -> [DeBruijnPattern]
forall a b. (a -> b) -> a -> b
$ [NamedArg SplitPattern] -> NAPs
fromSplitPatterns ([NamedArg SplitPattern] -> NAPs)
-> [NamedArg SplitPattern] -> NAPs
forall a b. (a -> b) -> a -> b
$ (SplitPattern -> NamedArg SplitPattern)
-> [SplitPattern] -> [NamedArg SplitPattern]
forall a b. (a -> b) -> [a] -> [b]
map SplitPattern -> NamedArg SplitPattern
forall a. a -> NamedArg a
defaultNamedArg [SplitPattern]
qs
        s :: PatternSubstitution
s = [DeBruijnPattern] -> PatternSubstitution
forall a. DeBruijn a => [a] -> Substitution' a
parallelS ([Int] -> (Int -> DeBruijnPattern) -> [DeBruijnPattern]
forall (m :: * -> *) a b. Functor m => m a -> (a -> b) -> m b
for [Int
0..[Int] -> Int
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum (-Int
1Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
:[Int]
vs)] ((Int -> DeBruijnPattern) -> [DeBruijnPattern])
-> (Int -> DeBruijnPattern) -> [DeBruijnPattern]
forall a b. (a -> b) -> a -> b
$ (\ Int
i -> DeBruijnPattern -> Maybe DeBruijnPattern -> DeBruijnPattern
forall a. a -> Maybe a -> a
fromMaybe (Int -> DeBruijnPattern
forall a. DeBruijn a => Int -> a
deBruijnVar Int
i) (Int -> [(Int, DeBruijnPattern)] -> Maybe DeBruijnPattern
forall a b. Eq a => a -> [(a, b)] -> Maybe b
List.lookup Int
i [(Int, DeBruijnPattern)]
mps')))

    updateRelevance :: TCM a -> TCM a
    updateRelevance :: forall a. TCM a -> TCM a
updateRelevance TCM a
cont =
      -- Don't do anything if there is no target type info.
      Maybe (Dom Type) -> TCM a -> (Dom Type -> TCM a) -> TCM a
forall a b. Maybe a -> b -> (a -> b) -> b
caseMaybe Maybe (Dom Type)
target TCM a
cont ((Dom Type -> TCM a) -> TCM a) -> (Dom Type -> TCM a) -> TCM a
forall a b. (a -> b) -> a -> b
$ \ Dom Type
b -> do
        -- TODO (2018-10-16): if proofs get erased in the compiler, also wake erased vars!
        let m :: Modality
m = Dom Type -> Modality
forall a. LensModality a => a -> Modality
getModality Dom Type
b
        Modality -> TCM a -> TCM a
forall (tcm :: * -> *) m a.
(MonadTCEnv tcm, LensModality m) =>
m -> tcm a -> tcm a
applyModalityToContext Modality
m TCM a
cont

    continue
      :: [BlockingVar]
      -> AllowPartialCover
      -> (SplitError -> TCM CoverResult)
      -> TCM CoverResult
    continue :: BlockingVars
-> AllowPartialCover
-> (SplitError -> TCM CoverResult)
-> TCM CoverResult
continue BlockingVars
xs AllowPartialCover
allowPartialCover SplitError -> TCM CoverResult
handle = do
      Either SplitError (Covering, BlockingVar)
r <- (BlockingVar
 -> TCMT IO (Either SplitError (Covering, BlockingVar)))
-> BlockingVars
-> TCMT IO (Either SplitError (Covering, BlockingVar))
forall (m :: * -> *) a err b.
Monad m =>
(a -> m (Either err b)) -> [a] -> m (Either err b)
altM1 (\ BlockingVar
x -> (Covering -> (Covering, BlockingVar))
-> Either SplitError Covering
-> Either SplitError (Covering, BlockingVar)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (,BlockingVar
x) (Either SplitError Covering
 -> Either SplitError (Covering, BlockingVar))
-> TCMT IO (Either SplitError Covering)
-> TCMT IO (Either SplitError (Covering, BlockingVar))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Induction
-> AllowPartialCover
-> SplitClause
-> BlockingVar
-> TCMT IO (Either SplitError Covering)
split Induction
Inductive AllowPartialCover
allowPartialCover SplitClause
sc BlockingVar
x) BlockingVars
xs
      case Either SplitError (Covering, BlockingVar)
r of
        Left SplitError
err -> SplitError -> TCM CoverResult
handle SplitError
err
        -- If we get the empty covering, we have reached an impossible case
        -- and are done.
        Right (Covering Arg Int
n [], BlockingVar
_) ->
         do
          -- TODO Andrea: I guess an empty pattern is not part of the cover?
          let qs :: [a]
qs = []
          CoverResult -> TCM CoverResult
forall (m :: * -> *) a. Monad m => a -> m a
return (CoverResult -> TCM CoverResult) -> CoverResult -> TCM CoverResult
forall a b. (a -> b) -> a -> b
$ SplitTree
-> IntSet
-> [(Telescope, NAPs)]
-> [Clause]
-> IntSet
-> CoverResult
CoverResult (Int -> SplitTree
forall a. Int -> SplitTree' a
SplittingDone (Telescope -> Int
forall a. Sized a => a -> Int
size Telescope
tel)) IntSet
forall a. Null a => a
empty [] [Clause]
forall a. [a]
qs IntSet
forall a. Null a => a
empty
        Right (Covering Arg Int
n [(SplitTag, SplitClause)]
scs, BlockingVar
x) -> do
          [Clause]
cs <- do
            let fallback :: TCMT IO [Clause]
fallback = [Clause] -> TCMT IO [Clause]
forall (m :: * -> *) a. Monad m => a -> m a
return [Clause]
cs
            TCMT IO (Maybe QName)
-> TCMT IO [Clause]
-> (QName -> TCMT IO [Clause])
-> TCMT IO [Clause]
forall (m :: * -> *) a b.
Monad m =>
m (Maybe a) -> m b -> (a -> m b) -> m b
caseMaybeM ([Char] -> TCMT IO (Maybe QName)
forall (m :: * -> *). HasBuiltins m => [Char] -> m (Maybe QName)
getPrimitiveName' [Char]
builtinHComp) TCMT IO [Clause]
fallback ((QName -> TCMT IO [Clause]) -> TCMT IO [Clause])
-> (QName -> TCMT IO [Clause]) -> TCMT IO [Clause]
forall a b. (a -> b) -> a -> b
$ \ QName
comp -> do
            let isComp :: SplitTag -> Bool
isComp = \case
                  SplitCon QName
c -> QName
comp QName -> QName -> Bool
forall a. Eq a => a -> a -> Bool
== QName
c
                  SplitTag
_ -> Bool
False
            Maybe (SplitTag, SplitClause)
-> TCMT IO [Clause]
-> ((SplitTag, SplitClause) -> TCMT IO [Clause])
-> TCMT IO [Clause]
forall a b. Maybe a -> b -> (a -> b) -> b
caseMaybe (((SplitTag, SplitClause) -> Bool)
-> [(SplitTag, SplitClause)] -> Maybe (SplitTag, SplitClause)
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
List.find (SplitTag -> Bool
isComp (SplitTag -> Bool)
-> ((SplitTag, SplitClause) -> SplitTag)
-> (SplitTag, SplitClause)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SplitTag, SplitClause) -> SplitTag
forall a b. (a, b) -> a
fst) [(SplitTag, SplitClause)]
scs) TCMT IO [Clause]
fallback (((SplitTag, SplitClause) -> TCMT IO [Clause]) -> TCMT IO [Clause])
-> ((SplitTag, SplitClause) -> TCMT IO [Clause])
-> TCMT IO [Clause]
forall a b. (a -> b) -> a -> b
$ \ (SplitTag
_, SplitClause
newSc) -> do
            [Clause] -> Clause -> [Clause]
forall a. [a] -> a -> [a]
snoc [Clause]
cs (Clause -> [Clause]) -> TCM Clause -> TCMT IO [Clause]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> QName
-> Arg Int
-> BlockingVar
-> SplitClause
-> SplitClause
-> TCM Clause
createMissingHCompClause QName
f Arg Int
n BlockingVar
x SplitClause
sc SplitClause
newSc
          [CoverResult]
results <- ((SplitTag, SplitClause) -> TCM CoverResult)
-> [(SplitTag, SplitClause)] -> TCMT IO [CoverResult]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ((QName -> [Clause] -> SplitClause -> TCM CoverResult
cover QName
f [Clause]
cs) (SplitClause -> TCM CoverResult)
-> ((SplitTag, SplitClause) -> SplitClause)
-> (SplitTag, SplitClause)
-> TCM CoverResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SplitTag, SplitClause) -> SplitClause
forall a b. (a, b) -> b
snd) [(SplitTag, SplitClause)]
scs
          let trees :: [SplitTree]
trees = (CoverResult -> SplitTree) -> [CoverResult] -> [SplitTree]
forall a b. (a -> b) -> [a] -> [b]
map CoverResult -> SplitTree
coverSplitTree      [CoverResult]
results
              useds :: [IntSet]
useds = (CoverResult -> IntSet) -> [CoverResult] -> [IntSet]
forall a b. (a -> b) -> [a] -> [b]
map CoverResult -> IntSet
coverUsedClauses    [CoverResult]
results
              psss :: [[(Telescope, NAPs)]]
psss  = (CoverResult -> [(Telescope, NAPs)])
-> [CoverResult] -> [[(Telescope, NAPs)]]
forall a b. (a -> b) -> [a] -> [b]
map CoverResult -> [(Telescope, NAPs)]
coverMissingClauses [CoverResult]
results
              qsss :: [[Clause]]
qsss  = (CoverResult -> [Clause]) -> [CoverResult] -> [[Clause]]
forall a b. (a -> b) -> [a] -> [b]
map CoverResult -> [Clause]
coverPatterns       [CoverResult]
results
              noex :: [IntSet]
noex  = (CoverResult -> IntSet) -> [CoverResult] -> [IntSet]
forall a b. (a -> b) -> [a] -> [b]
map CoverResult -> IntSet
coverNoExactClauses [CoverResult]
results
          -- Jesper, 2016-03-10  We need to remember which variables were
          -- eta-expanded by the unifier in order to generate a correct split
          -- tree (see Issue 1872).
          [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.split.eta" Int
60 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
            [ TCMT IO Doc
"etaRecordSplits"
            , Int -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => Int -> m Doc -> m Doc
nest Int
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
              [ TCMT IO Doc
"n   = " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text (Arg Int -> [Char]
forall a. Show a => a -> [Char]
show Arg Int
n)
              , TCMT IO Doc
"scs = " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> [(SplitTag, SplitClause)] -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM [(SplitTag, SplitClause)]
scs
              , TCMT IO Doc
"ps  = " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> NAPs -> TCMT IO Doc
forall (m :: * -> *). MonadPretty m => NAPs -> m Doc
prettyTCMPatternList ([NamedArg SplitPattern] -> NAPs
fromSplitPatterns [NamedArg SplitPattern]
ps)
              ]
            ]
          -- TODO Andrea: do something with etaRecordSplits and qsss?
          let trees' :: [(SplitTag, SplitTree)]
trees' = ((SplitTag, SplitClause) -> SplitTree -> (SplitTag, SplitTree))
-> [(SplitTag, SplitClause)]
-> [SplitTree]
-> [(SplitTag, SplitTree)]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (Int
-> [NamedArg SplitPattern]
-> (SplitTag, SplitClause)
-> SplitTree
-> (SplitTag, SplitTree)
etaRecordSplits (Arg Int -> Int
forall e. Arg e -> e
unArg Arg Int
n) [NamedArg SplitPattern]
ps) [(SplitTag, SplitClause)]
scs [SplitTree]
trees
              tree :: SplitTree
tree   = Arg Int -> LazySplit -> [(SplitTag, SplitTree)] -> SplitTree
forall a. Arg Int -> LazySplit -> SplitTrees' a -> SplitTree' a
SplitAt Arg Int
n LazySplit
StrictSplit [(SplitTag, SplitTree)]
trees'   -- TODO: Lazy?
          CoverResult -> TCM CoverResult
forall (m :: * -> *) a. Monad m => a -> m a
return (CoverResult -> TCM CoverResult) -> CoverResult -> TCM CoverResult
forall a b. (a -> b) -> a -> b
$ SplitTree
-> IntSet
-> [(Telescope, NAPs)]
-> [Clause]
-> IntSet
-> CoverResult
CoverResult SplitTree
tree ([IntSet] -> IntSet
forall (f :: * -> *). Foldable f => f IntSet -> IntSet
IntSet.unions [IntSet]
useds) ([[(Telescope, NAPs)]] -> [(Telescope, NAPs)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[(Telescope, NAPs)]]
psss) ([[Clause]] -> [Clause]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[Clause]]
qsss) ([IntSet] -> IntSet
forall (f :: * -> *). Foldable f => f IntSet -> IntSet
IntSet.unions [IntSet]
noex)

    -- Try to split result
    trySplitRes
      :: BlockedOnResult                  -- Are we blocked on the result?
      -> Bool                             -- Is this the last thing we try?
      -> (SplitError -> TCM CoverResult)  -- Handler for 'SplitError'
      -> TCM CoverResult                  -- Continuation
      -> TCM CoverResult
    -- not blocked on result: try regular splits
    trySplitRes :: BlockedOnResult
-> Bool
-> (SplitError -> TCM CoverResult)
-> TCM CoverResult
-> TCM CoverResult
trySplitRes BlockedOnResult
NotBlockedOnResult Bool
finalSplit SplitError -> TCM CoverResult
splitError TCM CoverResult
cont
      | Bool
finalSplit = TCM CoverResult
forall a. HasCallStack => a
__IMPOSSIBLE__ -- there must be *some* reason we are blocked
      | Bool
otherwise  = TCM CoverResult
cont
    -- blocked on arguments that are not yet introduced:

    -- we must split on a variable so that the target type becomes a pi type
    trySplitRes (BlockedOnApply ApplyOrIApply
IsApply) Bool
finalSplit SplitError -> TCM CoverResult
splitError TCM CoverResult
cont = do
      -- Andreas, 2021-12-31, issue #5712.
      -- If there is a tactic to solve the clause, we might not have inserted
      -- trailing args (due to #5358).  Now we force it!
      (Telescope
tel, SplitClause
sc') <- Bool -> SplitClause -> TCM (Telescope, SplitClause)
insertTrailingArgs Bool
True SplitClause
sc
      if Telescope -> Bool
forall a. Null a => a -> Bool
null Telescope
tel then
        if Bool
finalSplit then TCM CoverResult
forall a. HasCallStack => a
__IMPOSSIBLE__ -- already ruled out by lhs checker
        else TCM CoverResult
cont
      else QName -> [Clause] -> SplitClause -> TCM CoverResult
cover QName
f [Clause]
cs SplitClause
sc'

    -- ...or it was an IApply pattern, so we might just need to introduce the variable now.
    trySplitRes (BlockedOnApply ApplyOrIApply
IsIApply) Bool
finalSplit SplitError -> TCM CoverResult
splitError TCM CoverResult
cont
       = do
         TCMT IO (Maybe SplitClause)
-> TCM CoverResult
-> (SplitClause -> TCM CoverResult)
-> TCM CoverResult
forall (m :: * -> *) a b.
Monad m =>
m (Maybe a) -> m b -> (a -> m b) -> m b
caseMaybeM (QName -> SplitClause -> TCMT IO (Maybe SplitClause)
splitResultPath QName
f SplitClause
sc) TCM CoverResult
fallback ((SplitClause -> TCM CoverResult) -> TCM CoverResult)
-> (SplitClause -> TCM CoverResult) -> TCM CoverResult
forall a b. (a -> b) -> a -> b
$ (QName -> [Clause] -> SplitClause -> TCM CoverResult
cover QName
f [Clause]
cs (SplitClause -> TCM CoverResult)
-> ((Telescope, SplitClause) -> SplitClause)
-> (Telescope, SplitClause)
-> TCM CoverResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Telescope, SplitClause) -> SplitClause
forall a b. (a, b) -> b
snd) ((Telescope, SplitClause) -> TCM CoverResult)
-> (SplitClause -> TCM (Telescope, SplitClause))
-> SplitClause
-> TCM CoverResult
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< Bool -> SplitClause -> TCM (Telescope, SplitClause)
insertTrailingArgs Bool
False
      where
        fallback :: TCM CoverResult
fallback | Bool
finalSplit = TCM CoverResult
forall a. HasCallStack => a
__IMPOSSIBLE__ -- already ruled out by lhs checker?
                 | Bool
otherwise  = TCM CoverResult
cont

    -- blocked on result but there are catchalls:
    -- try regular splits if there are any, or else throw an error,
    -- this is nicer than continuing and reporting unreachable clauses
    -- (see issue #2833)
    trySplitRes (BlockedOnProj Bool
True) Bool
finalSplit SplitError -> TCM CoverResult
splitError TCM CoverResult
cont
      | Bool
finalSplit = SplitError -> TCM CoverResult
splitError SplitError
CosplitCatchall
      | Bool
otherwise  = TCM CoverResult
cont
    -- all clauses have an unsplit copattern: try to split
    trySplitRes (BlockedOnProj Bool
False) Bool
finalSplit SplitError -> TCM CoverResult
splitError TCM CoverResult
cont = do
      [Char] -> Int -> [Char] -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> [Char] -> m ()
reportSLn [Char]
"tc.cover" Int
20 ([Char] -> TCMT IO ()) -> [Char] -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
"blocked by projection pattern"
      -- forM is a monadic map over a Maybe here
      Either SplitError Covering
mcov <- QName -> SplitClause -> TCMT IO (Either SplitError Covering)
splitResultRecord QName
f SplitClause
sc
      case Either SplitError Covering
mcov of
        Left SplitError
err
          | Bool
finalSplit -> SplitError -> TCM CoverResult
splitError SplitError
err
          | Bool
otherwise  -> TCM CoverResult
cont
        Right (Covering Arg Int
n [(SplitTag, SplitClause)]
scs) -> do
          -- If result splitting was successful, continue coverage checking.
          ([SplitTag]
projs, [CoverResult]
results) <- [(SplitTag, CoverResult)] -> ([SplitTag], [CoverResult])
forall a b. [(a, b)] -> ([a], [b])
unzip ([(SplitTag, CoverResult)] -> ([SplitTag], [CoverResult]))
-> TCMT IO [(SplitTag, CoverResult)]
-> TCMT IO ([SplitTag], [CoverResult])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> do
            ((SplitTag, SplitClause) -> TCMT IO (SplitTag, CoverResult))
-> [(SplitTag, SplitClause)] -> TCMT IO [(SplitTag, CoverResult)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ((SplitClause -> TCM CoverResult)
-> (SplitTag, SplitClause) -> TCMT IO (SplitTag, CoverResult)
forall (t :: * -> *) (m :: * -> *) a b.
(Decoration t, Functor m) =>
(a -> m b) -> t a -> m (t b)
traverseF ((SplitClause -> TCM CoverResult)
 -> (SplitTag, SplitClause) -> TCMT IO (SplitTag, CoverResult))
-> (SplitClause -> TCM CoverResult)
-> (SplitTag, SplitClause)
-> TCMT IO (SplitTag, CoverResult)
forall a b. (a -> b) -> a -> b
$ QName -> [Clause] -> SplitClause -> TCM CoverResult
cover QName
f [Clause]
cs (SplitClause -> TCM CoverResult)
-> (SplitClause -> TCMT IO SplitClause)
-> SplitClause
-> TCM CoverResult
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< ((Telescope, SplitClause) -> SplitClause
forall a b. (a, b) -> b
snd ((Telescope, SplitClause) -> SplitClause)
-> (SplitClause -> TCM (Telescope, SplitClause))
-> SplitClause
-> TCMT IO SplitClause
forall (m :: * -> *) b c a.
Functor m =>
(b -> c) -> (a -> m b) -> a -> m c
<.> Bool -> SplitClause -> TCM (Telescope, SplitClause)
insertTrailingArgs Bool
False)) [(SplitTag, SplitClause)]
scs
            -- OR:
            -- forM scs $ \ (proj, sc') -> (proj,) <$> do
            --   cover f cs =<< do
            --     snd <$> fixTarget sc'
          let trees :: [SplitTree]
trees = (CoverResult -> SplitTree) -> [CoverResult] -> [SplitTree]
forall a b. (a -> b) -> [a] -> [b]
map CoverResult -> SplitTree
coverSplitTree [CoverResult]
results
              useds :: [IntSet]
useds = (CoverResult -> IntSet) -> [CoverResult] -> [IntSet]
forall a b. (a -> b) -> [a] -> [b]
map CoverResult -> IntSet
coverUsedClauses [CoverResult]
results
              psss :: [[(Telescope, NAPs)]]
psss  = (CoverResult -> [(Telescope, NAPs)])
-> [CoverResult] -> [[(Telescope, NAPs)]]
forall a b. (a -> b) -> [a] -> [b]
map CoverResult -> [(Telescope, NAPs)]
coverMissingClauses [CoverResult]
results
              qsss :: [[Clause]]
qsss  = (CoverResult -> [Clause]) -> [CoverResult] -> [[Clause]]
forall a b. (a -> b) -> [a] -> [b]
map CoverResult -> [Clause]
coverPatterns [CoverResult]
results
              noex :: [IntSet]
noex  = (CoverResult -> IntSet) -> [CoverResult] -> [IntSet]
forall a b. (a -> b) -> [a] -> [b]
map CoverResult -> IntSet
coverNoExactClauses [CoverResult]
results
              tree :: SplitTree
tree  = Arg Int -> LazySplit -> [(SplitTag, SplitTree)] -> SplitTree
forall a. Arg Int -> LazySplit -> SplitTrees' a -> SplitTree' a
SplitAt Arg Int
n LazySplit
StrictSplit ([(SplitTag, SplitTree)] -> SplitTree)
-> [(SplitTag, SplitTree)] -> SplitTree
forall a b. (a -> b) -> a -> b
$ [SplitTag] -> [SplitTree] -> [(SplitTag, SplitTree)]
forall a b. [a] -> [b] -> [(a, b)]
zip [SplitTag]
projs [SplitTree]
trees   -- TODO: Lazy?
          CoverResult -> TCM CoverResult
forall (m :: * -> *) a. Monad m => a -> m a
return (CoverResult -> TCM CoverResult) -> CoverResult -> TCM CoverResult
forall a b. (a -> b) -> a -> b
$ SplitTree
-> IntSet
-> [(Telescope, NAPs)]
-> [Clause]
-> IntSet
-> CoverResult
CoverResult SplitTree
tree ([IntSet] -> IntSet
forall (f :: * -> *). Foldable f => f IntSet -> IntSet
IntSet.unions [IntSet]
useds) ([[(Telescope, NAPs)]] -> [(Telescope, NAPs)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[(Telescope, NAPs)]]
psss) ([[Clause]] -> [Clause]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[Clause]]
qsss) ([IntSet] -> IntSet
forall (f :: * -> *). Foldable f => f IntSet -> IntSet
IntSet.unions [IntSet]
noex)

    gatherEtaSplits :: Int -> SplitClause
                    -> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
    gatherEtaSplits :: Int
-> SplitClause
-> [NamedArg SplitPattern]
-> [NamedArg SplitPattern]
gatherEtaSplits Int
n SplitClause
sc []
       | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0    = [NamedArg SplitPattern]
forall a. HasCallStack => a
__IMPOSSIBLE__ -- we should have encountered the main
                                    -- split by now already
       | Bool
otherwise = []
    gatherEtaSplits Int
n SplitClause
sc (NamedArg SplitPattern
p:[NamedArg SplitPattern]
ps) = case NamedArg SplitPattern -> SplitPattern
forall a. NamedArg a -> a
namedArg NamedArg SplitPattern
p of
      VarP PatternInfo
_ SplitPatVar
x
       | Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0    -> case SplitPattern
p' of -- this is the main split
           VarP  PatternInfo
_ SplitPatVar
_    -> NamedArg SplitPattern
p NamedArg SplitPattern
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a. a -> [a] -> [a]
: Int
-> SplitClause
-> [NamedArg SplitPattern]
-> [NamedArg SplitPattern]
gatherEtaSplits (-Int
1) SplitClause
sc [NamedArg SplitPattern]
ps
           DotP  PatternInfo
_ Term
_    -> [NamedArg SplitPattern]
forall a. HasCallStack => a
__IMPOSSIBLE__
           ConP  ConHead
_ ConPatternInfo
_ [NamedArg SplitPattern]
qs -> [NamedArg SplitPattern]
qs [NamedArg SplitPattern]
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a. [a] -> [a] -> [a]
++ Int
-> SplitClause
-> [NamedArg SplitPattern]
-> [NamedArg SplitPattern]
gatherEtaSplits (-Int
1) SplitClause
sc [NamedArg SplitPattern]
ps
           LitP{}       -> Int
-> SplitClause
-> [NamedArg SplitPattern]
-> [NamedArg SplitPattern]
gatherEtaSplits (-Int
1) SplitClause
sc [NamedArg SplitPattern]
ps
           ProjP{}      -> [NamedArg SplitPattern]
forall a. HasCallStack => a
__IMPOSSIBLE__
           IApplyP{}    -> [NamedArg SplitPattern]
forall a. HasCallStack => a
__IMPOSSIBLE__
           DefP  PatternInfo
_ QName
_ [NamedArg SplitPattern]
qs -> [NamedArg SplitPattern]
qs [NamedArg SplitPattern]
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a. [a] -> [a] -> [a]
++ Int
-> SplitClause
-> [NamedArg SplitPattern]
-> [NamedArg SplitPattern]
gatherEtaSplits (-Int
1) SplitClause
sc [NamedArg SplitPattern]
ps -- __IMPOSSIBLE__ -- Andrea: maybe?
       | Bool
otherwise ->
           (SplitPattern -> SplitPattern)
-> NamedArg SplitPattern -> NamedArg SplitPattern
forall a b. (a -> b) -> NamedArg a -> NamedArg b
updateNamedArg (\ SplitPattern
_ -> SplitPattern
p') NamedArg SplitPattern
p NamedArg SplitPattern
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a. a -> [a] -> [a]
: Int
-> SplitClause
-> [NamedArg SplitPattern]
-> [NamedArg SplitPattern]
gatherEtaSplits (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) SplitClause
sc [NamedArg SplitPattern]
ps
        where p' :: SplitPattern
p' = Substitution' SplitPattern -> Int -> SplitPattern
forall a. EndoSubst a => Substitution' a -> Int -> a
lookupS (SplitClause -> Substitution' SplitPattern
scSubst SplitClause
sc) (Int -> SplitPattern) -> Int -> SplitPattern
forall a b. (a -> b) -> a -> b
$ SplitPatVar -> Int
splitPatVarIndex SplitPatVar
x
      IApplyP{}   ->
           (SplitPattern -> SplitPattern)
-> NamedArg SplitPattern -> NamedArg SplitPattern
forall a b. (a -> b) -> NamedArg a -> NamedArg b
updateNamedArg (Substitution' (SubstArg SplitPattern)
-> SplitPattern -> SplitPattern
forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst (SplitClause -> Substitution' SplitPattern
scSubst SplitClause
sc)) NamedArg SplitPattern
p NamedArg SplitPattern
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a. a -> [a] -> [a]
: Int
-> SplitClause
-> [NamedArg SplitPattern]
-> [NamedArg SplitPattern]
gatherEtaSplits (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) SplitClause
sc [NamedArg SplitPattern]
ps
      DotP  PatternInfo
_ Term
_    -> NamedArg SplitPattern
p NamedArg SplitPattern
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a. a -> [a] -> [a]
: Int
-> SplitClause
-> [NamedArg SplitPattern]
-> [NamedArg SplitPattern]
gatherEtaSplits (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) SplitClause
sc [NamedArg SplitPattern]
ps -- count dot patterns
      ConP  ConHead
_ ConPatternInfo
_ [NamedArg SplitPattern]
qs -> Int
-> SplitClause
-> [NamedArg SplitPattern]
-> [NamedArg SplitPattern]
gatherEtaSplits Int
n SplitClause
sc ([NamedArg SplitPattern]
qs [NamedArg SplitPattern]
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a. [a] -> [a] -> [a]
++ [NamedArg SplitPattern]
ps)
      DefP  PatternInfo
_ QName
_ [NamedArg SplitPattern]
qs -> Int
-> SplitClause
-> [NamedArg SplitPattern]
-> [NamedArg SplitPattern]
gatherEtaSplits Int
n SplitClause
sc ([NamedArg SplitPattern]
qs [NamedArg SplitPattern]
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a. [a] -> [a] -> [a]
++ [NamedArg SplitPattern]
ps)
      LitP{}       -> Int
-> SplitClause
-> [NamedArg SplitPattern]
-> [NamedArg SplitPattern]
gatherEtaSplits Int
n SplitClause
sc [NamedArg SplitPattern]
ps
      ProjP{}      -> Int
-> SplitClause
-> [NamedArg SplitPattern]
-> [NamedArg SplitPattern]
gatherEtaSplits Int
n SplitClause
sc [NamedArg SplitPattern]
ps

    addEtaSplits :: Int -> [NamedArg SplitPattern] -> SplitTree -> SplitTree
    addEtaSplits :: Int -> [NamedArg SplitPattern] -> SplitTree -> SplitTree
addEtaSplits Int
k []     SplitTree
t = SplitTree
t
    addEtaSplits Int
k (NamedArg SplitPattern
p:[NamedArg SplitPattern]
ps) SplitTree
t = case NamedArg SplitPattern -> SplitPattern
forall a. NamedArg a -> a
namedArg NamedArg SplitPattern
p of
      VarP  PatternInfo
_ SplitPatVar
_     -> Int -> [NamedArg SplitPattern] -> SplitTree -> SplitTree
addEtaSplits (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) [NamedArg SplitPattern]
ps SplitTree
t
      DotP  PatternInfo
_ Term
_     -> Int -> [NamedArg SplitPattern] -> SplitTree -> SplitTree
addEtaSplits (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) [NamedArg SplitPattern]
ps SplitTree
t
      ConP ConHead
c ConPatternInfo
cpi [NamedArg SplitPattern]
qs -> Arg Int -> LazySplit -> [(SplitTag, SplitTree)] -> SplitTree
forall a. Arg Int -> LazySplit -> SplitTrees' a -> SplitTree' a
SplitAt (NamedArg SplitPattern
p NamedArg SplitPattern -> Int -> Arg Int
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Int
k) LazySplit
LazySplit [(QName -> SplitTag
SplitCon (ConHead -> QName
conName ConHead
c) , Int -> [NamedArg SplitPattern] -> SplitTree -> SplitTree
addEtaSplits Int
k ([NamedArg SplitPattern]
qs [NamedArg SplitPattern]
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a. [a] -> [a] -> [a]
++ [NamedArg SplitPattern]
ps) SplitTree
t)]
      LitP{}        -> SplitTree
forall a. HasCallStack => a
__IMPOSSIBLE__
      ProjP{}       -> SplitTree
forall a. HasCallStack => a
__IMPOSSIBLE__
      DefP{}        -> SplitTree
forall a. HasCallStack => a
__IMPOSSIBLE__ -- Andrea: maybe?
      IApplyP{}     -> Int -> [NamedArg SplitPattern] -> SplitTree -> SplitTree
addEtaSplits (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) [NamedArg SplitPattern]
ps SplitTree
t

    etaRecordSplits :: Int -> [NamedArg SplitPattern] -> (SplitTag,SplitClause)
                    -> SplitTree -> (SplitTag,SplitTree)
    etaRecordSplits :: Int
-> [NamedArg SplitPattern]
-> (SplitTag, SplitClause)
-> SplitTree
-> (SplitTag, SplitTree)
etaRecordSplits Int
n [NamedArg SplitPattern]
ps (SplitTag
q , SplitClause
sc) SplitTree
t =
      (SplitTag
q , Int -> [NamedArg SplitPattern] -> SplitTree -> SplitTree
addEtaSplits Int
0 (Int
-> SplitClause
-> [NamedArg SplitPattern]
-> [NamedArg SplitPattern]
gatherEtaSplits Int
n SplitClause
sc [NamedArg SplitPattern]
ps) SplitTree
t)




-- | Append an hcomp clause to the clauses of a function.
createMissingHCompClause
  :: QName
       -- ^ Function name.
  -> Arg Nat -- ^ index of hcomp pattern
  -> BlockingVar -- ^ Blocking var that lead to hcomp split.
  -> SplitClause -- ^ Clause before the hcomp split
  -> SplitClause
       -- ^ Clause to add.
   -> TCM Clause
createMissingHCompClause :: QName
-> Arg Int
-> BlockingVar
-> SplitClause
-> SplitClause
-> TCM Clause
createMissingHCompClause QName
f Arg Int
n BlockingVar
x SplitClause
old_sc (SClause Telescope
tel [NamedArg SplitPattern]
ps Substitution' SplitPattern
_sigma' Map CheckpointId (Substitution' Term)
cps (Just Dom Type
t)) = QName -> TCM Clause -> TCM Clause
forall (m :: * -> *) x a.
(MonadTrace m, HasRange x) =>
x -> m a -> m a
setCurrentRange QName
f (TCM Clause -> TCM Clause) -> TCM Clause -> TCM Clause
forall a b. (a -> b) -> a -> b
$ do
  [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.hcomp" Int
20 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ Telescope -> TCMT IO Doc -> TCMT IO Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext Telescope
tel (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text [Char]
"Trying to create right-hand side of type" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Dom Type -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM Dom Type
t
  [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.hcomp" Int
30 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ Telescope -> TCMT IO Doc -> TCMT IO Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext Telescope
tel (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text [Char]
"ps = " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> NAPs -> TCMT IO Doc
forall (m :: * -> *). MonadPretty m => NAPs -> m Doc
prettyTCMPatternList ([NamedArg SplitPattern] -> NAPs
fromSplitPatterns [NamedArg SplitPattern]
ps)
  [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.hcomp" Int
30 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text [Char]
"tel = " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Telescope -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM Telescope
tel

  Term
io      <- Term -> Maybe Term -> Term
forall a. a -> Maybe a -> a
fromMaybe Term
forall a. HasCallStack => a
__IMPOSSIBLE__ (Maybe Term -> Term) -> TCMT IO (Maybe Term) -> TCMT IO Term
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> TCMT IO (Maybe Term)
forall (m :: * -> *). HasBuiltins m => [Char] -> m (Maybe Term)
getTerm' [Char]
builtinIOne
  Term
iz      <- Term -> Maybe Term -> Term
forall a. a -> Maybe a -> a
fromMaybe Term
forall a. HasCallStack => a
__IMPOSSIBLE__ (Maybe Term -> Term) -> TCMT IO (Maybe Term) -> TCMT IO Term
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> TCMT IO (Maybe Term)
forall (m :: * -> *). HasBuiltins m => [Char] -> m (Maybe Term)
getTerm' [Char]
builtinIZero
  let
    cannotCreate :: forall m a. (MonadTCEnv m, ReadTCState m, MonadError TCErr m) => Doc -> Closure (Abs Type) -> m a
    cannotCreate :: forall (m :: * -> *) a.
(MonadTCEnv m, ReadTCState m, MonadError TCErr m) =>
Doc -> Closure (Abs Type) -> m a
cannotCreate Doc
doc Closure (Abs Type)
t = do
      TypeError -> m a
forall (m :: * -> *) a.
(HasCallStack, MonadTCError m) =>
TypeError -> m a
typeError (TypeError -> m a)
-> (SplitError -> TypeError) -> SplitError -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SplitError -> TypeError
SplitError (SplitError -> m a) -> SplitError -> m a
forall a b. (a -> b) -> a -> b
$ QName
-> (Telescope, NAPs) -> Doc -> Closure (Abs Type) -> SplitError
CannotCreateMissingClause QName
f (Telescope
tel,[NamedArg SplitPattern] -> NAPs
fromSplitPatterns [NamedArg SplitPattern]
ps) Doc
doc Closure (Abs Type)
t
  let old_ps :: [Elim]
old_ps = NAPs -> [Elim]
patternsToElims (NAPs -> [Elim]) -> NAPs -> [Elim]
forall a b. (a -> b) -> a -> b
$ [NamedArg SplitPattern] -> NAPs
fromSplitPatterns ([NamedArg SplitPattern] -> NAPs)
-> [NamedArg SplitPattern] -> NAPs
forall a b. (a -> b) -> a -> b
$ SplitClause -> [NamedArg SplitPattern]
scPats SplitClause
old_sc
      old_t :: Dom Type
old_t  = Maybe (Dom Type) -> Dom Type
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe (Dom Type) -> Dom Type) -> Maybe (Dom Type) -> Dom Type
forall a b. (a -> b) -> a -> b
$ SplitClause -> Maybe (Dom Type)
scTarget SplitClause
old_sc
      old_tel :: Telescope
old_tel = SplitClause -> Telescope
scTel SplitClause
old_sc
      -- old_tel = Γ(x:H)Δ
      -- Γ(x:H)Δ ⊢ old_t
      -- vs = iApplyVars old_ps
      -- [ α ⇒ b ] = [(i,f old_ps (i=0),f old_ps (i=1)) | i <- vs]

      -- Γ(x:H)(δ : Δ) ⊢ [ α ⇒ b ]
      -- Γ(x:H)Δ ⊢ f old_ps : old_t [ α ⇒ b ]
      -- Γ,φ,u,u0,Δ(x = hcomp φ u u0) ⊢ rhs_we_define : (old_t[ α ⇒ b ])(x = hcomp φ u u0)

      -- Extra assumption:
      -- tel = Γ,φ,u,u0,Δ(x = hcomp φ u u0),Δ'
      -- ps = old_ps[x = hcomp φ u u0],ps'
      -- with Δ' and ps' introduced by fixTarget.
      -- So final clause will be:
      -- tel ⊢ ps ↦ rhs_we_define{wkS ..} ps'
      getLevel :: a -> m Term
getLevel a
t = do
        Sort
s <- Sort -> m Sort
forall a (m :: * -> *). (Reduce a, MonadReduce m) => a -> m a
reduce (Sort -> m Sort) -> Sort -> m Sort
forall a b. (a -> b) -> a -> b
$ a -> Sort
forall a. LensSort a => a -> Sort
getSort a
t
        case Sort
s of
          Type Level' Term
l -> Term -> m Term
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Level' Term -> Term
Level Level' Term
l)
          Sort
s      -> do
            [Char] -> Int -> TCMT IO Doc -> m ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.hcomp" Int
20 (TCMT IO Doc -> m ()) -> TCMT IO Doc -> m ()
forall a b. (a -> b) -> a -> b
$ [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text [Char]
"getLevel, s = " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Sort -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM Sort
s
            TypeError -> m Term
forall (m :: * -> *) a.
(HasCallStack, MonadTCError m) =>
TypeError -> m a
typeError (TypeError -> m Term) -> (Doc -> TypeError) -> Doc -> m Term
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc -> TypeError
GenericDocError (Doc -> m Term) -> m Doc -> m Term
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<
                    ([Char] -> m Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text [Char]
"The sort of" m Doc -> m Doc -> m Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> a -> m Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM a
t m Doc -> m Doc -> m Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> [Char] -> m Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text [Char]
"should be of the form \"Set l\"")

      -- Γ ⊢ hdelta = (x : H)(δ : Δ)
      (Telescope
gamma,hdelta :: Telescope
hdelta@(ExtendTel Dom Type
hdom Abs Telescope
delta)) = Int -> Telescope -> (Telescope, Telescope)
splitTelescopeAt (Telescope -> Int
forall a. Sized a => a -> Int
size Telescope
old_tel Int -> Int -> Int
forall a. Num a => a -> a -> a
- (BlockingVar -> Int
blockingVarNo BlockingVar
x Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)) Telescope
old_tel

      -- Γ,φ,u,u0,Δ(x = hcomp φ u u0) ⊢
      (Telescope
working_tel,Telescope
_deltaEx) = Int -> Telescope -> (Telescope, Telescope)
splitTelescopeAt (Telescope -> Int
forall a. Sized a => a -> Int
size Telescope
gamma Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Abs Telescope -> Int
forall a. Sized a => a -> Int
size Abs Telescope
delta) Telescope
tel

      -- Γ,φ,u,u0,(x:H)(δ : Δ) ⊢ rhoS : Γ(x:H)(δ : Δ)
      {- rhoS = liftS (size hdelta) $ raiseS 3 -}
      vs :: [Int]
vs = [NamedArg SplitPattern] -> [Int]
forall a. DeBruijn a => [NamedArg (Pattern' a)] -> [Int]
iApplyVars (SplitClause -> [NamedArg SplitPattern]
scPats SplitClause
old_sc)

  -- Γ(x:H)(δ : Δ) ⊢ [ α ⇒ b ] = [(i,f old_ps (i=0),f old_ps (i=1)) | i <- vs]
  [(Term, (Term, Term))]
alphab <- [Int]
-> (Int -> TCMT IO (Term, (Term, Term)))
-> TCMT IO [(Term, (Term, Term))]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [Int]
vs ((Int -> TCMT IO (Term, (Term, Term)))
 -> TCMT IO [(Term, (Term, Term))])
-> (Int -> TCMT IO (Term, (Term, Term)))
-> TCMT IO [(Term, (Term, Term))]
forall a b. (a -> b) -> a -> b
$ \ Int
i -> do
               let
                 -- Γ(x:H)(δ : Δ) ⊢
                 tm :: Term
tm = QName -> [Elim] -> Term
Def QName
f [Elim]
old_ps
               -- TODO only reduce IApply _ _ (0/1), as to avoid termination problems
               (Term
l,Term
r) <- (Term, Term) -> TCMT IO (Term, Term)
forall a (m :: * -> *). (Reduce a, MonadReduce m) => a -> m a
reduce (Int -> Term -> Substitution' Term
forall a. EndoSubst a => Int -> a -> Substitution' a
inplaceS Int
i Term
iz Substitution' (SubstArg Term) -> Term -> Term
forall a. Subst a => Substitution' (SubstArg a) -> a -> a
`applySubst` Term
tm, Int -> Term -> Substitution' Term
forall a. EndoSubst a => Int -> a -> Substitution' a
inplaceS Int
i Term
io Substitution' (SubstArg Term) -> Term -> Term
forall a. Subst a => Substitution' (SubstArg a) -> a -> a
`applySubst` Term
tm)
               (Term, (Term, Term)) -> TCMT IO (Term, (Term, Term))
forall (m :: * -> *) a. Monad m => a -> m a
return ((Term, (Term, Term)) -> TCMT IO (Term, (Term, Term)))
-> (Term, (Term, Term)) -> TCMT IO (Term, (Term, Term))
forall a b. (a -> b) -> a -> b
$ (Int -> Term
var Int
i, (Term
l, Term
r))



  Clause
cl <- do
    (Type
ty,Term
rhs) <- Telescope -> TCMT IO (Type, Term) -> TCMT IO (Type, Term)
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext Telescope
working_tel (TCMT IO (Type, Term) -> TCMT IO (Type, Term))
-> TCMT IO (Type, Term) -> TCMT IO (Type, Term)
forall a b. (a -> b) -> a -> b
$ do
      -- Γ(x:H)Δ ⊢ g = f old_ps : old_t [ α ⇒ b ]
      -- Γ(x:H)(δ : Δ) ⊢ [ α ⇒ b ]
      -- Γ,φ,u,u0 ⊢ Δf = i.Δ[x = hfill φ u u0 i]
      -- Γ,φ,u,u0,δ : Δ(x = hcomp φ u u0) ⊢ δ_fill     = i.tFillTel (i. Δf[~i]) δ (~ i) : i.Δf[i]
      -- Γ,φ,u,u0,δ : Δ(x = hcomp φ u u0) ⊢ old_t_fill = i.old_t[x = hfill φ u u0 i, δ_fill[i]]
      -- Γ,φ,u,u0,δ : Δ(x = hcomp φ u u0) ⊢ comp (\ i. old_t_fill[i])
      --                 (\ i. [ φ ↦ g[x = hfill φ u u0 i,δ_fill[i]] = g[u i,δ_fill[i]]
      --                         α ↦ b[x = hfill φ u u0 i,δ_fill[i]]
      --                        ])
      --                 (g[x = u0,δ_fill[0]]) : old_t[x = hcomp φ u u0,δ]

      Names -> NamesT (TCMT IO) (Type, Term) -> TCMT IO (Type, Term)
forall (m :: * -> *) a. Names -> NamesT m a -> m a
runNamesT [] (NamesT (TCMT IO) (Type, Term) -> TCMT IO (Type, Term))
-> NamesT (TCMT IO) (Type, Term) -> TCMT IO (Type, Term)
forall a b. (a -> b) -> a -> b
$ do
          Term
tPOr <- Term -> Maybe Term -> Term
forall a. a -> Maybe a -> a
fromMaybe Term
forall a. HasCallStack => a
__IMPOSSIBLE__ (Maybe Term -> Term)
-> NamesT (TCMT IO) (Maybe Term) -> NamesT (TCMT IO) Term
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> NamesT (TCMT IO) (Maybe Term)
forall (m :: * -> *). HasBuiltins m => [Char] -> m (Maybe Term)
getTerm' [Char]
builtinPOr
          Term
tIMax <- Term -> Maybe Term -> Term
forall a. a -> Maybe a -> a
fromMaybe Term
forall a. HasCallStack => a
__IMPOSSIBLE__ (Maybe Term -> Term)
-> NamesT (TCMT IO) (Maybe Term) -> NamesT (TCMT IO) Term
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> NamesT (TCMT IO) (Maybe Term)
forall (m :: * -> *). HasBuiltins m => [Char] -> m (Maybe Term)
getTerm' [Char]
builtinIMax
          Term
tIMin <- Term -> Maybe Term -> Term
forall a. a -> Maybe a -> a
fromMaybe Term
forall a. HasCallStack => a
__IMPOSSIBLE__ (Maybe Term -> Term)
-> NamesT (TCMT IO) (Maybe Term) -> NamesT (TCMT IO) Term
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> NamesT (TCMT IO) (Maybe Term)
forall (m :: * -> *). HasBuiltins m => [Char] -> m (Maybe Term)
getTerm' [Char]
builtinIMin
          Term
tINeg <- Term -> Maybe Term -> Term
forall a. a -> Maybe a -> a
fromMaybe Term
forall a. HasCallStack => a
__IMPOSSIBLE__ (Maybe Term -> Term)
-> NamesT (TCMT IO) (Maybe Term) -> NamesT (TCMT IO) Term
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> NamesT (TCMT IO) (Maybe Term)
forall (m :: * -> *). HasBuiltins m => [Char] -> m (Maybe Term)
getTerm' [Char]
builtinINeg
          Term
tHComp <- Term -> Maybe Term -> Term
forall a. a -> Maybe a -> a
fromMaybe Term
forall a. HasCallStack => a
__IMPOSSIBLE__ (Maybe Term -> Term)
-> NamesT (TCMT IO) (Maybe Term) -> NamesT (TCMT IO) Term
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> NamesT (TCMT IO) (Maybe Term)
forall (m :: * -> *). HasBuiltins m => [Char] -> m (Maybe Term)
getTerm' [Char]
builtinHComp
          Term
tTrans <- Term -> Maybe Term -> Term
forall a. a -> Maybe a -> a
fromMaybe Term
forall a. HasCallStack => a
__IMPOSSIBLE__ (Maybe Term -> Term)
-> NamesT (TCMT IO) (Maybe Term) -> NamesT (TCMT IO) Term
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> NamesT (TCMT IO) (Maybe Term)
forall (m :: * -> *). HasBuiltins m => [Char] -> m (Maybe Term)
getTerm' [Char]
builtinTrans
          NamesT (TCMT IO) [Elim]
extra_ps <- [Elim] -> NamesT (TCMT IO) (NamesT (TCMT IO) [Elim])
forall (m :: * -> *) a.
(MonadFail m, Subst a) =>
a -> NamesT m (NamesT m a)
open ([Elim] -> NamesT (TCMT IO) (NamesT (TCMT IO) [Elim]))
-> [Elim] -> NamesT (TCMT IO) (NamesT (TCMT IO) [Elim])
forall a b. (a -> b) -> a -> b
$ NAPs -> [Elim]
patternsToElims (NAPs -> [Elim]) -> NAPs -> [Elim]
forall a b. (a -> b) -> a -> b
$ [NamedArg SplitPattern] -> NAPs
fromSplitPatterns ([NamedArg SplitPattern] -> NAPs)
-> [NamedArg SplitPattern] -> NAPs
forall a b. (a -> b) -> a -> b
$ Int -> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a. Int -> [a] -> [a]
drop ([Elim] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Elim]
old_ps) [NamedArg SplitPattern]
ps
          let
            ineg :: NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
ineg NamesT (TCMT IO) Term
j = Term -> NamesT (TCMT IO) Term
forall (f :: * -> *) a. Applicative f => a -> f a
pure Term
tINeg NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> NamesT (TCMT IO) Term
j
            imax :: NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
imax NamesT (TCMT IO) Term
i NamesT (TCMT IO) Term
j = Term -> NamesT (TCMT IO) Term
forall (f :: * -> *) a. Applicative f => a -> f a
pure Term
tIMax NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> NamesT (TCMT IO) Term
i NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> NamesT (TCMT IO) Term
j
            trFillTel' :: t (TCMT IO) (Abs Telescope)
-> t (TCMT IO) Term
-> t (TCMT IO) Args
-> t (TCMT IO) Term
-> t (TCMT IO) Args
trFillTel' t (TCMT IO) (Abs Telescope)
a t (TCMT IO) Term
b t (TCMT IO) Args
c t (TCMT IO) Term
d = do
              ExceptT (Closure (Abs Type)) (TCMT IO) Args
m <- Abs Telescope
-> Term
-> Args
-> Term
-> ExceptT (Closure (Abs Type)) (TCMT IO) Args
trFillTel (Abs Telescope
 -> Term
 -> Args
 -> Term
 -> ExceptT (Closure (Abs Type)) (TCMT IO) Args)
-> t (TCMT IO) (Abs Telescope)
-> t (TCMT IO)
     (Term
      -> Args -> Term -> ExceptT (Closure (Abs Type)) (TCMT IO) Args)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> t (TCMT IO) (Abs Telescope)
a t (TCMT IO)
  (Term
   -> Args -> Term -> ExceptT (Closure (Abs Type)) (TCMT IO) Args)
-> t (TCMT IO) Term
-> t (TCMT IO)
     (Args -> Term -> ExceptT (Closure (Abs Type)) (TCMT IO) Args)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> t (TCMT IO) Term
b t (TCMT IO)
  (Args -> Term -> ExceptT (Closure (Abs Type)) (TCMT IO) Args)
-> t (TCMT IO) Args
-> t (TCMT IO)
     (Term -> ExceptT (Closure (Abs Type)) (TCMT IO) Args)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> t (TCMT IO) Args
c t (TCMT IO) (Term -> ExceptT (Closure (Abs Type)) (TCMT IO) Args)
-> t (TCMT IO) Term
-> t (TCMT IO) (ExceptT (Closure (Abs Type)) (TCMT IO) Args)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> t (TCMT IO) Term
d
              Either (Closure (Abs Type)) Args
x <- TCM (Either (Closure (Abs Type)) Args)
-> t (TCMT IO) (Either (Closure (Abs Type)) Args)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (TCM (Either (Closure (Abs Type)) Args)
 -> t (TCMT IO) (Either (Closure (Abs Type)) Args))
-> TCM (Either (Closure (Abs Type)) Args)
-> t (TCMT IO) (Either (Closure (Abs Type)) Args)
forall a b. (a -> b) -> a -> b
$ ExceptT (Closure (Abs Type)) (TCMT IO) Args
-> TCM (Either (Closure (Abs Type)) Args)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT ExceptT (Closure (Abs Type)) (TCMT IO) Args
m
              case Either (Closure (Abs Type)) Args
x of
                Left Closure (Abs Type)
bad_t -> Doc -> Closure (Abs Type) -> t (TCMT IO) Args
forall (m :: * -> *) a.
(MonadTCEnv m, ReadTCState m, MonadError TCErr m) =>
Doc -> Closure (Abs Type) -> m a
cannotCreate Doc
"Cannot transport with type family:" Closure (Abs Type)
bad_t
                Right Args
args -> Args -> t (TCMT IO) Args
forall (m :: * -> *) a. Monad m => a -> m a
return Args
args
          NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
comp <- do
            let forward :: NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
forward NamesT (TCMT IO) Term
la NamesT (TCMT IO) Term
bA NamesT (TCMT IO) Term
r NamesT (TCMT IO) Term
u = Term -> NamesT (TCMT IO) Term
forall (f :: * -> *) a. Applicative f => a -> f a
pure Term
tTrans NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<#> [Char]
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
forall (m :: * -> *).
MonadFail m =>
[Char] -> (NamesT m Term -> NamesT m Term) -> NamesT m Term
lam [Char]
"i" (\ NamesT (TCMT IO) Term
i -> NamesT (TCMT IO) Term
la NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> (NamesT (TCMT IO) Term
i NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
`imax` NamesT (TCMT IO) Term
r))
                                              NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> [Char]
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
forall (m :: * -> *).
MonadFail m =>
[Char] -> (NamesT m Term -> NamesT m Term) -> NamesT m Term
lam [Char]
"i" (\ NamesT (TCMT IO) Term
i -> NamesT (TCMT IO) Term
bA NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> (NamesT (TCMT IO) Term
i NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
`imax` NamesT (TCMT IO) Term
r))
                                              NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> NamesT (TCMT IO) Term
r
                                              NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> NamesT (TCMT IO) Term
u
            (NamesT (TCMT IO) Term
 -> NamesT (TCMT IO) Term
 -> NamesT (TCMT IO) Term
 -> NamesT (TCMT IO) Term
 -> NamesT (TCMT IO) Term
 -> NamesT (TCMT IO) Term)
-> NamesT
     (TCMT IO)
     (NamesT (TCMT IO) Term
      -> NamesT (TCMT IO) Term
      -> NamesT (TCMT IO) Term
      -> NamesT (TCMT IO) Term
      -> NamesT (TCMT IO) Term
      -> NamesT (TCMT IO) Term)
forall (m :: * -> *) a. Monad m => a -> m a
return ((NamesT (TCMT IO) Term
  -> NamesT (TCMT IO) Term
  -> NamesT (TCMT IO) Term
  -> NamesT (TCMT IO) Term
  -> NamesT (TCMT IO) Term
  -> NamesT (TCMT IO) Term)
 -> NamesT
      (TCMT IO)
      (NamesT (TCMT IO) Term
       -> NamesT (TCMT IO) Term
       -> NamesT (TCMT IO) Term
       -> NamesT (TCMT IO) Term
       -> NamesT (TCMT IO) Term
       -> NamesT (TCMT IO) Term))
-> (NamesT (TCMT IO) Term
    -> NamesT (TCMT IO) Term
    -> NamesT (TCMT IO) Term
    -> NamesT (TCMT IO) Term
    -> NamesT (TCMT IO) Term
    -> NamesT (TCMT IO) Term)
-> NamesT
     (TCMT IO)
     (NamesT (TCMT IO) Term
      -> NamesT (TCMT IO) Term
      -> NamesT (TCMT IO) Term
      -> NamesT (TCMT IO) Term
      -> NamesT (TCMT IO) Term
      -> NamesT (TCMT IO) Term)
forall a b. (a -> b) -> a -> b
$ \ NamesT (TCMT IO) Term
la NamesT (TCMT IO) Term
bA NamesT (TCMT IO) Term
phi NamesT (TCMT IO) Term
u NamesT (TCMT IO) Term
u0 ->
              Term -> NamesT (TCMT IO) Term
forall (f :: * -> *) a. Applicative f => a -> f a
pure Term
tHComp NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<#> (NamesT (TCMT IO) Term
la NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> Term -> NamesT (TCMT IO) Term
forall (f :: * -> *) a. Applicative f => a -> f a
pure Term
io) NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<#> (NamesT (TCMT IO) Term
bA NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> Term -> NamesT (TCMT IO) Term
forall (f :: * -> *) a. Applicative f => a -> f a
pure Term
io) NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<#> NamesT (TCMT IO) Term
phi
                        NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> [Char]
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
forall (m :: * -> *).
MonadFail m =>
[Char] -> (NamesT m Term -> NamesT m Term) -> NamesT m Term
lam [Char]
"i" (\ NamesT (TCMT IO) Term
i -> [Char]
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
forall (m :: * -> *).
MonadFail m =>
[Char] -> (NamesT m Term -> NamesT m Term) -> NamesT m Term
ilam [Char]
"o" ((NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
 -> NamesT (TCMT IO) Term)
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
forall a b. (a -> b) -> a -> b
$ \ NamesT (TCMT IO) Term
o ->
                                NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
forward NamesT (TCMT IO) Term
la NamesT (TCMT IO) Term
bA NamesT (TCMT IO) Term
i (NamesT (TCMT IO) Term
u NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> NamesT (TCMT IO) Term
i NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<..> NamesT (TCMT IO) Term
o))
                        NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
forward NamesT (TCMT IO) Term
la NamesT (TCMT IO) Term
bA (Term -> NamesT (TCMT IO) Term
forall (f :: * -> *) a. Applicative f => a -> f a
pure Term
iz) NamesT (TCMT IO) Term
u0
          let
            hcomp :: NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
hcomp NamesT (TCMT IO) Term
la NamesT (TCMT IO) Term
bA NamesT (TCMT IO) Term
phi NamesT (TCMT IO) Term
u NamesT (TCMT IO) Term
u0 = Term -> NamesT (TCMT IO) Term
forall (f :: * -> *) a. Applicative f => a -> f a
pure Term
tHComp NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<#> NamesT (TCMT IO) Term
la NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<#> NamesT (TCMT IO) Term
bA
                                               NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<#> NamesT (TCMT IO) Term
phi
                                               NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> NamesT (TCMT IO) Term
u
                                               NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> NamesT (TCMT IO) Term
u0

            hfill :: NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
hfill NamesT (TCMT IO) Term
la NamesT (TCMT IO) Term
bA NamesT (TCMT IO) Term
phi NamesT (TCMT IO) Term
u NamesT (TCMT IO) Term
u0 NamesT (TCMT IO) Term
i = NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
hcomp NamesT (TCMT IO) Term
la NamesT (TCMT IO) Term
bA
                                               (Term -> NamesT (TCMT IO) Term
forall (f :: * -> *) a. Applicative f => a -> f a
pure Term
tIMax NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> NamesT (TCMT IO) Term
phi NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> (Term -> NamesT (TCMT IO) Term
forall (f :: * -> *) a. Applicative f => a -> f a
pure Term
tINeg NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> NamesT (TCMT IO) Term
i))
                                               ([Char]
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
forall (m :: * -> *).
MonadFail m =>
[Char] -> (NamesT m Term -> NamesT m Term) -> NamesT m Term
lam [Char]
"j" ((NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
 -> NamesT (TCMT IO) Term)
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
forall a b. (a -> b) -> a -> b
$ \ NamesT (TCMT IO) Term
j -> Term -> NamesT (TCMT IO) Term
forall (f :: * -> *) a. Applicative f => a -> f a
pure Term
tPOr NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<#> NamesT (TCMT IO) Term
la NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> NamesT (TCMT IO) Term
phi NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> (Term -> NamesT (TCMT IO) Term
forall (f :: * -> *) a. Applicative f => a -> f a
pure Term
tINeg NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> NamesT (TCMT IO) Term
i) NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<#> [Char]
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
forall (m :: * -> *).
MonadFail m =>
[Char] -> (NamesT m Term -> NamesT m Term) -> NamesT m Term
ilam [Char]
"o" (\ NamesT (TCMT IO) Term
_ -> NamesT (TCMT IO) Term
bA)
                                                     NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> [Char]
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
forall (m :: * -> *).
MonadFail m =>
[Char] -> (NamesT m Term -> NamesT m Term) -> NamesT m Term
ilam [Char]
"o" (\ NamesT (TCMT IO) Term
o -> NamesT (TCMT IO) Term
u NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> (Term -> NamesT (TCMT IO) Term
forall (f :: * -> *) a. Applicative f => a -> f a
pure Term
tIMin NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> NamesT (TCMT IO) Term
i NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> NamesT (TCMT IO) Term
j) NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<..> NamesT (TCMT IO) Term
o)
                                                     NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> [Char]
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
forall (m :: * -> *).
MonadFail m =>
[Char] -> (NamesT m Term -> NamesT m Term) -> NamesT m Term
ilam [Char]
"o" (\ NamesT (TCMT IO) Term
_ -> NamesT (TCMT IO) Term
u0)
                                                   )
                                               NamesT (TCMT IO) Term
u0
          -- Γ,φ,u,u0,(δ : Δ(x = hcomp φ u u0)) ⊢ hcompS : Γ(x:H)(δ : Δ)
          Substitution' Term
hcompS <- TCMT IO (Substitution' Term)
-> NamesT (TCMT IO) (Substitution' Term)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (TCMT IO (Substitution' Term)
 -> NamesT (TCMT IO) (Substitution' Term))
-> TCMT IO (Substitution' Term)
-> NamesT (TCMT IO) (Substitution' Term)
forall a b. (a -> b) -> a -> b
$ do
            Dom Type
hdom <- Dom Type -> TCMT IO (Dom Type)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Dom Type -> TCMT IO (Dom Type)) -> Dom Type -> TCMT IO (Dom Type)
forall a b. (a -> b) -> a -> b
$ Int -> Dom Type -> Dom Type
forall a. Subst a => Int -> a -> a
raise Int
3 Dom Type
hdom
            let
              [TCMT IO Term
phi,TCMT IO Term
u,TCMT IO Term
u0] = (Int -> TCMT IO Term) -> [Int] -> [TCMT IO Term]
forall a b. (a -> b) -> [a] -> [b]
map (Term -> TCMT IO Term
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Term -> TCMT IO Term) -> (Int -> Term) -> Int -> TCMT IO Term
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Term
var) [Int
2,Int
1,Int
0]
              htype :: TCMT IO Term
htype = Term -> TCMT IO Term
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Term -> TCMT IO Term) -> Term -> TCMT IO Term
forall a b. (a -> b) -> a -> b
$ Type -> Term
forall t a. Type'' t a -> a
unEl (Type -> Term) -> (Dom Type -> Type) -> Dom Type -> Term
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dom Type -> Type
forall t e. Dom' t e -> e
unDom (Dom Type -> Term) -> Dom Type -> Term
forall a b. (a -> b) -> a -> b
$ Dom Type
hdom
              lvl :: TCMT IO Term
lvl = Type -> TCMT IO Term
forall {m :: * -> *} {a}.
(LensSort a, MonadError TCErr m, PrettyTCM a, PureTCM m,
 MonadInteractionPoints m, MonadFresh NameId m,
 MonadStConcreteNames m, IsString (m Doc), Null (m Doc),
 Semigroup (m Doc)) =>
a -> m Term
getLevel (Type -> TCMT IO Term) -> Type -> TCMT IO Term
forall a b. (a -> b) -> a -> b
$ Dom Type -> Type
forall t e. Dom' t e -> e
unDom Dom Type
hdom
            Term
hc <- Term -> TCMT IO Term
forall (f :: * -> *) a. Applicative f => a -> f a
pure Term
tHComp TCMT IO Term -> TCMT IO Term -> TCMT IO Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<#> TCMT IO Term
lvl TCMT IO Term -> TCMT IO Term -> TCMT IO Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<#> TCMT IO Term
htype
                                      TCMT IO Term -> TCMT IO Term -> TCMT IO Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<#> TCMT IO Term
phi
                                      TCMT IO Term -> TCMT IO Term -> TCMT IO Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> TCMT IO Term
u
                                      TCMT IO Term -> TCMT IO Term -> TCMT IO Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> TCMT IO Term
u0
            Substitution' Term -> TCMT IO (Substitution' Term)
forall (m :: * -> *) a. Monad m => a -> m a
return (Substitution' Term -> TCMT IO (Substitution' Term))
-> Substitution' Term -> TCMT IO (Substitution' Term)
forall a b. (a -> b) -> a -> b
$ Int -> Substitution' Term -> Substitution' Term
forall a. Int -> Substitution' a -> Substitution' a
liftS (Abs Telescope -> Int
forall a. Sized a => a -> Int
size Abs Telescope
delta) (Substitution' Term -> Substitution' Term)
-> Substitution' Term -> Substitution' Term
forall a b. (a -> b) -> a -> b
$ Term
hc Term -> Substitution' Term -> Substitution' Term
forall a. DeBruijn a => a -> Substitution' a -> Substitution' a
`consS` Int -> Substitution' Term
forall a. Int -> Substitution' a
raiseS Int
3
          -- Γ,φ,u,u0,Δ(x = hcomp phi u u0) ⊢ raise 3+|Δ| hdom
          Dom Type
hdom <- Dom Type -> NamesT (TCMT IO) (Dom Type)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Dom Type -> NamesT (TCMT IO) (Dom Type))
-> Dom Type -> NamesT (TCMT IO) (Dom Type)
forall a b. (a -> b) -> a -> b
$ Int -> Dom Type -> Dom Type
forall a. Subst a => Int -> a -> a
raise (Int
3Int -> Int -> Int
forall a. Num a => a -> a -> a
+Abs Telescope -> Int
forall a. Sized a => a -> Int
size Abs Telescope
delta) Dom Type
hdom
          NamesT (TCMT IO) Term
htype <- Term -> NamesT (TCMT IO) (NamesT (TCMT IO) Term)
forall (m :: * -> *) a.
(MonadFail m, Subst a) =>
a -> NamesT m (NamesT m a)
open (Term -> NamesT (TCMT IO) (NamesT (TCMT IO) Term))
-> Term -> NamesT (TCMT IO) (NamesT (TCMT IO) Term)
forall a b. (a -> b) -> a -> b
$ Type -> Term
forall t a. Type'' t a -> a
unEl (Type -> Term) -> (Dom Type -> Type) -> Dom Type -> Term
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dom Type -> Type
forall t e. Dom' t e -> e
unDom (Dom Type -> Term) -> Dom Type -> Term
forall a b. (a -> b) -> a -> b
$ Dom Type
hdom
          NamesT (TCMT IO) Term
lvl <- Term -> NamesT (TCMT IO) (NamesT (TCMT IO) Term)
forall (m :: * -> *) a.
(MonadFail m, Subst a) =>
a -> NamesT m (NamesT m a)
open (Term -> NamesT (TCMT IO) (NamesT (TCMT IO) Term))
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) (NamesT (TCMT IO) Term)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (TCMT IO Term -> NamesT (TCMT IO) Term
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (TCMT IO Term -> NamesT (TCMT IO) Term)
-> (Type -> TCMT IO Term) -> Type -> NamesT (TCMT IO) Term
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Type -> TCMT IO Term
forall {m :: * -> *} {a}.
(LensSort a, MonadError TCErr m, PrettyTCM a, PureTCM m,
 MonadInteractionPoints m, MonadFresh NameId m,
 MonadStConcreteNames m, IsString (m Doc), Null (m Doc),
 Semigroup (m Doc)) =>
a -> m Term
getLevel (Type -> NamesT (TCMT IO) Term) -> Type -> NamesT (TCMT IO) Term
forall a b. (a -> b) -> a -> b
$ Dom Type -> Type
forall t e. Dom' t e -> e
unDom Dom Type
hdom)

          -- Γ,φ,u,u0,Δ(x = hcomp phi u u0) ⊢
          [NamesT (TCMT IO) Term
phi,NamesT (TCMT IO) Term
u,NamesT (TCMT IO) Term
u0] <- (Int -> NamesT (TCMT IO) (NamesT (TCMT IO) Term))
-> [Int] -> NamesT (TCMT IO) [NamesT (TCMT IO) Term]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Term -> NamesT (TCMT IO) (NamesT (TCMT IO) Term)
forall (m :: * -> *) a.
(MonadFail m, Subst a) =>
a -> NamesT m (NamesT m a)
open (Term -> NamesT (TCMT IO) (NamesT (TCMT IO) Term))
-> (Int -> Term) -> Int -> NamesT (TCMT IO) (NamesT (TCMT IO) Term)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Term -> Term
forall a. Subst a => Int -> a -> a
raise (Abs Telescope -> Int
forall a. Sized a => a -> Int
size Abs Telescope
delta) (Term -> Term) -> (Int -> Term) -> Int -> Term
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Term
var) [Int
2,Int
1,Int
0]
          -- Γ,x,Δ ⊢ f old_ps
          -- Γ ⊢ abstract hdelta (f old_ps)
          NamesT (TCMT IO) Term
g <- Term -> NamesT (TCMT IO) (NamesT (TCMT IO) Term)
forall (m :: * -> *) a.
(MonadFail m, Subst a) =>
a -> NamesT m (NamesT m a)
open (Term -> NamesT (TCMT IO) (NamesT (TCMT IO) Term))
-> Term -> NamesT (TCMT IO) (NamesT (TCMT IO) Term)
forall a b. (a -> b) -> a -> b
$ Int -> Term -> Term
forall a. Subst a => Int -> a -> a
raise (Int
3Int -> Int -> Int
forall a. Num a => a -> a -> a
+Abs Telescope -> Int
forall a. Sized a => a -> Int
size Abs Telescope
delta) (Term -> Term) -> Term -> Term
forall a b. (a -> b) -> a -> b
$ Telescope -> Term -> Term
forall t. Abstract t => Telescope -> t -> t
abstract Telescope
hdelta (QName -> [Elim] -> Term
Def QName
f [Elim]
old_ps)
          NamesT (TCMT IO) Type
old_t <- Type -> NamesT (TCMT IO) (NamesT (TCMT IO) Type)
forall (m :: * -> *) a.
(MonadFail m, Subst a) =>
a -> NamesT m (NamesT m a)
open (Type -> NamesT (TCMT IO) (NamesT (TCMT IO) Type))
-> Type -> NamesT (TCMT IO) (NamesT (TCMT IO) Type)
forall a b. (a -> b) -> a -> b
$ Int -> Type -> Type
forall a. Subst a => Int -> a -> a
raise (Int
3Int -> Int -> Int
forall a. Num a => a -> a -> a
+Abs Telescope -> Int
forall a. Sized a => a -> Int
size Abs Telescope
delta) (Type -> Type) -> Type -> Type
forall a b. (a -> b) -> a -> b
$ Telescope -> Type -> Type
forall t. Abstract t => Telescope -> t -> t
abstract Telescope
hdelta (Dom Type -> Type
forall t e. Dom' t e -> e
unDom Dom Type
old_t)
          let bapp :: f (Abs b) -> f (SubstArg b) -> f b
bapp f (Abs b)
a f (SubstArg b)
x = Abs b -> SubstArg b -> b
forall a. Subst a => Abs a -> SubstArg a -> a
lazyAbsApp (Abs b -> SubstArg b -> b) -> f (Abs b) -> f (SubstArg b -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (Abs b)
a f (SubstArg b -> b) -> f (SubstArg b) -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> f (SubstArg b)
x
          (NamesT (TCMT IO) (Abs Args)
delta_fill :: NamesT TCM (Abs Args)) <- (Abs Args -> NamesT (TCMT IO) (NamesT (TCMT IO) (Abs Args))
forall (m :: * -> *) a.
(MonadFail m, Subst a) =>
a -> NamesT m (NamesT m a)
open (Abs Args -> NamesT (TCMT IO) (NamesT (TCMT IO) (Abs Args)))
-> NamesT (TCMT IO) (Abs Args)
-> NamesT (TCMT IO) (NamesT (TCMT IO) (Abs Args))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) (NamesT (TCMT IO) (Abs Args)
 -> NamesT (TCMT IO) (NamesT (TCMT IO) (Abs Args)))
-> NamesT (TCMT IO) (Abs Args)
-> NamesT (TCMT IO) (NamesT (TCMT IO) (Abs Args))
forall a b. (a -> b) -> a -> b
$ do
            -- Γ,φ,u,u0,Δ(x = hcomp phi u u0) ⊢ x.Δ
            NamesT (TCMT IO) (Abs Telescope)
delta <- Abs Telescope
-> NamesT (TCMT IO) (NamesT (TCMT IO) (Abs Telescope))
forall (m :: * -> *) a.
(MonadFail m, Subst a) =>
a -> NamesT m (NamesT m a)
open (Abs Telescope
 -> NamesT (TCMT IO) (NamesT (TCMT IO) (Abs Telescope)))
-> Abs Telescope
-> NamesT (TCMT IO) (NamesT (TCMT IO) (Abs Telescope))
forall a b. (a -> b) -> a -> b
$ Int -> Abs Telescope -> Abs Telescope
forall a. Subst a => Int -> a -> a
raise (Int
3Int -> Int -> Int
forall a. Num a => a -> a -> a
+Abs Telescope -> Int
forall a. Sized a => a -> Int
size Abs Telescope
delta) Abs Telescope
delta
            -- Γ,φ,u,u0,Δ(x = hcomp phi u u0) ⊢ i.Δ(x = hfill phi u u0 (~ i))
            NamesT (TCMT IO) (Abs Telescope)
deltaf <- Abs Telescope
-> NamesT (TCMT IO) (NamesT (TCMT IO) (Abs Telescope))
forall (m :: * -> *) a.
(MonadFail m, Subst a) =>
a -> NamesT m (NamesT m a)
open (Abs Telescope
 -> NamesT (TCMT IO) (NamesT (TCMT IO) (Abs Telescope)))
-> NamesT (TCMT IO) (Abs Telescope)
-> NamesT (TCMT IO) (NamesT (TCMT IO) (Abs Telescope))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< [Char]
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Telescope)
-> NamesT (TCMT IO) (Abs Telescope)
forall (m :: * -> *) b a.
(MonadFail m, Subst b, DeBruijn b, Subst a, Free a) =>
[Char] -> (NamesT m b -> NamesT m a) -> NamesT m (Abs a)
bind [Char]
"i" (\ NamesT (TCMT IO) Term
i ->
                           (NamesT (TCMT IO) (Abs Telescope)
delta NamesT (TCMT IO) (Abs Telescope)
-> NamesT (TCMT IO) (SubstArg Telescope)
-> NamesT (TCMT IO) Telescope
forall {f :: * -> *} {b}.
(Applicative f, Subst b) =>
f (Abs b) -> f (SubstArg b) -> f b
`bapp` NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
hfill NamesT (TCMT IO) Term
lvl NamesT (TCMT IO) Term
htype NamesT (TCMT IO) Term
phi NamesT (TCMT IO) Term
u NamesT (TCMT IO) Term
u0 (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
ineg NamesT (TCMT IO) Term
i)))
            -- Γ,φ,u,u0,Δ(x = hcomp phi u u0) ⊢ Δ(x = hcomp phi u u0) = Δf[0]
            NamesT (TCMT IO) Args
args <- (Args -> NamesT (TCMT IO) (NamesT (TCMT IO) Args)
forall (m :: * -> *) a.
(MonadFail m, Subst a) =>
a -> NamesT m (NamesT m a)
open (Args -> NamesT (TCMT IO) (NamesT (TCMT IO) Args))
-> NamesT (TCMT IO) Args
-> NamesT (TCMT IO) (NamesT (TCMT IO) Args)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) (NamesT (TCMT IO) Args -> NamesT (TCMT IO) (NamesT (TCMT IO) Args))
-> NamesT (TCMT IO) Args
-> NamesT (TCMT IO) (NamesT (TCMT IO) Args)
forall a b. (a -> b) -> a -> b
$ Telescope -> Args
forall a t. DeBruijn a => Tele (Dom t) -> [Arg a]
teleArgs (Telescope -> Args)
-> NamesT (TCMT IO) Telescope -> NamesT (TCMT IO) Args
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Abs Telescope -> Term -> Telescope
forall a. Subst a => Abs a -> SubstArg a -> a
lazyAbsApp (Abs Telescope -> Term -> Telescope)
-> NamesT (TCMT IO) (Abs Telescope)
-> NamesT (TCMT IO) (Term -> Telescope)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NamesT (TCMT IO) (Abs Telescope)
deltaf NamesT (TCMT IO) (Term -> Telescope)
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Telescope
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Term -> NamesT (TCMT IO) Term
forall (f :: * -> *) a. Applicative f => a -> f a
pure Term
iz)
            [Char]
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Args)
-> NamesT (TCMT IO) (Abs Args)
forall (m :: * -> *) b a.
(MonadFail m, Subst b, DeBruijn b, Subst a, Free a) =>
[Char] -> (NamesT m b -> NamesT m a) -> NamesT m (Abs a)
bind [Char]
"i" ((NamesT (TCMT IO) Term -> NamesT (TCMT IO) Args)
 -> NamesT (TCMT IO) (Abs Args))
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Args)
-> NamesT (TCMT IO) (Abs Args)
forall a b. (a -> b) -> a -> b
$ \ NamesT (TCMT IO) Term
i -> [Char] -> NamesT (TCMT IO) Args -> NamesT (TCMT IO) Args
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext ([Char]
"i" :: String) (NamesT (TCMT IO) Args -> NamesT (TCMT IO) Args)
-> NamesT (TCMT IO) Args -> NamesT (TCMT IO) Args
forall a b. (a -> b) -> a -> b
$ do -- for error messages.
              -- Γ,φ,u,u0,Δ(x = hcomp phi u u0),(i:I) ⊢ ... : Δ(x = hfill phi u u0 i)
              NamesT (TCMT IO) (Abs Telescope)
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Args
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Args
forall {t :: (* -> *) -> * -> *}.
(MonadTrans t, MonadTCEnv (t (TCMT IO)), ReadTCState (t (TCMT IO)),
 MonadError TCErr (t (TCMT IO))) =>
t (TCMT IO) (Abs Telescope)
-> t (TCMT IO) Term
-> t (TCMT IO) Args
-> t (TCMT IO) Term
-> t (TCMT IO) Args
trFillTel' NamesT (TCMT IO) (Abs Telescope)
deltaf (Term -> NamesT (TCMT IO) Term
forall (f :: * -> *) a. Applicative f => a -> f a
pure Term
iz) NamesT (TCMT IO) Args
args (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
ineg NamesT (TCMT IO) Term
i)
          let
            apply_delta_fill :: NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
apply_delta_fill NamesT (TCMT IO) Term
i NamesT (TCMT IO) Term
f = Term -> Args -> Term
forall t. Apply t => t -> Args -> t
apply (Term -> Args -> Term)
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) (Args -> Term)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NamesT (TCMT IO) Term
f NamesT (TCMT IO) (Args -> Term)
-> NamesT (TCMT IO) Args -> NamesT (TCMT IO) Term
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (NamesT (TCMT IO) (Abs Args)
delta_fill NamesT (TCMT IO) (Abs Args)
-> NamesT (TCMT IO) (SubstArg Args) -> NamesT (TCMT IO) Args
forall {f :: * -> *} {b}.
(Applicative f, Subst b) =>
f (Abs b) -> f (SubstArg b) -> f b
`bapp` NamesT (TCMT IO) Term
NamesT (TCMT IO) (SubstArg Args)
i)
            call :: NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
call NamesT (TCMT IO) Term
v NamesT (TCMT IO) Term
i = NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
apply_delta_fill NamesT (TCMT IO) Term
i (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall a b. (a -> b) -> a -> b
$ NamesT (TCMT IO) Term
g NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> NamesT (TCMT IO) Term
v
          NamesT (TCMT IO) Term -> NamesT (TCMT IO) Type
ty <- do
                (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Type)
-> NamesT
     (TCMT IO) (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Type)
forall (m :: * -> *) a. Monad m => a -> m a
return ((NamesT (TCMT IO) Term -> NamesT (TCMT IO) Type)
 -> NamesT
      (TCMT IO) (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Type))
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Type)
-> NamesT
     (TCMT IO) (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Type)
forall a b. (a -> b) -> a -> b
$ \ NamesT (TCMT IO) Term
i -> do
                    Term
v <- NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
hfill NamesT (TCMT IO) Term
lvl NamesT (TCMT IO) Term
htype NamesT (TCMT IO) Term
phi NamesT (TCMT IO) Term
u NamesT (TCMT IO) Term
u0 NamesT (TCMT IO) Term
i
                    Type
hd <- NamesT (TCMT IO) Type
old_t
                    Args
args <- NamesT (TCMT IO) (Abs Args)
delta_fill NamesT (TCMT IO) (Abs Args)
-> NamesT (TCMT IO) (SubstArg Args) -> NamesT (TCMT IO) Args
forall {f :: * -> *} {b}.
(Applicative f, Subst b) =>
f (Abs b) -> f (SubstArg b) -> f b
`bapp` NamesT (TCMT IO) Term
NamesT (TCMT IO) (SubstArg Args)
i
                    TCMT IO Type -> NamesT (TCMT IO) Type
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (TCMT IO Type -> NamesT (TCMT IO) Type)
-> TCMT IO Type -> NamesT (TCMT IO) Type
forall a b. (a -> b) -> a -> b
$ Type -> Args -> TCMT IO Type
forall a (m :: * -> *).
(PiApplyM a, MonadReduce m, HasBuiltins m) =>
Type -> a -> m Type
piApplyM Type
hd (Args -> TCMT IO Type) -> Args -> TCMT IO Type
forall a b. (a -> b) -> a -> b
$ ArgInfo -> Term -> Arg Term
forall e. ArgInfo -> e -> Arg e
Arg (Dom Type -> ArgInfo
forall t e. Dom' t e -> ArgInfo
domInfo Dom Type
hdom) Term
v Arg Term -> Args -> Args
forall a. a -> [a] -> [a]
: Args
args
          NamesT (TCMT IO) Term
ty_level <- do
            Abs Type
t <- [Char]
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Type)
-> NamesT (TCMT IO) (Abs Type)
forall (m :: * -> *) b a.
(MonadFail m, Subst b, DeBruijn b, Subst a, Free a) =>
[Char] -> (NamesT m b -> NamesT m a) -> NamesT m (Abs a)
bind [Char]
"i" NamesT (TCMT IO) Term -> NamesT (TCMT IO) Type
ty
            Sort
s <- Sort -> NamesT (TCMT IO) Sort
forall a (m :: * -> *). (Reduce a, MonadReduce m) => a -> m a
reduce (Sort -> NamesT (TCMT IO) Sort) -> Sort -> NamesT (TCMT IO) Sort
forall a b. (a -> b) -> a -> b
$ Type -> Sort
forall a. LensSort a => a -> Sort
getSort (Abs Type -> Type
forall a. Subst a => Abs a -> a
absBody Abs Type
t)
            [Char] -> Int -> TCMT IO Doc -> NamesT (TCMT IO) ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.hcomp" Int
20 (TCMT IO Doc -> NamesT (TCMT IO) ())
-> TCMT IO Doc -> NamesT (TCMT IO) ()
forall a b. (a -> b) -> a -> b
$ [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text [Char]
"ty_level, s = " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Sort -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM Sort
s
            case Sort
s of
              Type Level' Term
l -> Term -> NamesT (TCMT IO) (NamesT (TCMT IO) Term)
forall (m :: * -> *) a.
(MonadFail m, Subst a) =>
a -> NamesT m (NamesT m a)
open (Term -> NamesT (TCMT IO) (NamesT (TCMT IO) Term))
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) (NamesT (TCMT IO) Term)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< [Char]
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
forall (m :: * -> *).
MonadFail m =>
[Char] -> (NamesT m Term -> NamesT m Term) -> NamesT m Term
lam [Char]
"i" (\ NamesT (TCMT IO) Term
_ -> Term -> NamesT (TCMT IO) Term
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Term -> NamesT (TCMT IO) Term) -> Term -> NamesT (TCMT IO) Term
forall a b. (a -> b) -> a -> b
$ Level' Term -> Term
Level Level' Term
l)
              Sort
_      -> Doc
-> Closure (Abs Type) -> NamesT (TCMT IO) (NamesT (TCMT IO) Term)
forall (m :: * -> *) a.
(MonadTCEnv m, ReadTCState m, MonadError TCErr m) =>
Doc -> Closure (Abs Type) -> m a
cannotCreate Doc
"Cannot compose with type family:" (Closure (Abs Type) -> NamesT (TCMT IO) (NamesT (TCMT IO) Term))
-> NamesT (TCMT IO) (Closure (Abs Type))
-> NamesT (TCMT IO) (NamesT (TCMT IO) Term)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< TCM (Closure (Abs Type)) -> NamesT (TCMT IO) (Closure (Abs Type))
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (Abs Type -> TCM (Closure (Abs Type))
forall (m :: * -> *) a.
(MonadTCEnv m, ReadTCState m) =>
a -> m (Closure a)
buildClosure Abs Type
t)

          let
            pOr_ty :: NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
pOr_ty NamesT (TCMT IO) Term
i NamesT (TCMT IO) Term
phi NamesT (TCMT IO) Term
psi NamesT (TCMT IO) Term
u0 NamesT (TCMT IO) Term
u1 = Term -> NamesT (TCMT IO) Term
forall (f :: * -> *) a. Applicative f => a -> f a
pure Term
tPOr NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<#> (NamesT (TCMT IO) Term
ty_level NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> NamesT (TCMT IO) Term
i)
                                               NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> NamesT (TCMT IO) Term
phi NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> NamesT (TCMT IO) Term
psi
                                               NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<#> [Char]
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
forall (m :: * -> *).
MonadFail m =>
[Char] -> (NamesT m Term -> NamesT m Term) -> NamesT m Term
ilam [Char]
"o" (\ NamesT (TCMT IO) Term
_ -> Type -> Term
forall t a. Type'' t a -> a
unEl (Type -> Term) -> NamesT (TCMT IO) Type -> NamesT (TCMT IO) Term
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Type
ty NamesT (TCMT IO) Term
i) NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> NamesT (TCMT IO) Term
u0 NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> NamesT (TCMT IO) Term
u1
          NamesT (TCMT IO) Term
alpha <- do
            [NamesT (TCMT IO) Term]
vars <- ((Term, (Term, Term)) -> NamesT (TCMT IO) (NamesT (TCMT IO) Term))
-> [(Term, (Term, Term))]
-> NamesT (TCMT IO) [NamesT (TCMT IO) Term]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Term -> NamesT (TCMT IO) (NamesT (TCMT IO) Term)
forall (m :: * -> *) a.
(MonadFail m, Subst a) =>
a -> NamesT m (NamesT m a)
open (Term -> NamesT (TCMT IO) (NamesT (TCMT IO) Term))
-> ((Term, (Term, Term)) -> Term)
-> (Term, (Term, Term))
-> NamesT (TCMT IO) (NamesT (TCMT IO) Term)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Substitution' (SubstArg Term) -> Term -> Term
forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst Substitution' Term
Substitution' (SubstArg Term)
hcompS (Term -> Term)
-> ((Term, (Term, Term)) -> Term) -> (Term, (Term, Term)) -> Term
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Term, (Term, Term)) -> Term
forall a b. (a, b) -> a
fst) [(Term, (Term, Term))]
alphab
            NamesT (TCMT IO) Term -> NamesT (TCMT IO) (NamesT (TCMT IO) Term)
forall (m :: * -> *) a. Monad m => a -> m a
return (NamesT (TCMT IO) Term -> NamesT (TCMT IO) (NamesT (TCMT IO) Term))
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) (NamesT (TCMT IO) Term)
forall a b. (a -> b) -> a -> b
$ (NamesT (TCMT IO) Term
 -> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
-> [NamesT (TCMT IO) Term]
-> NamesT (TCMT IO) Term
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
imax (NamesT (TCMT IO) Term
 -> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (\ NamesT (TCMT IO) Term
v -> NamesT (TCMT IO) Term
v NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
`imax` NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
ineg NamesT (TCMT IO) Term
v)) (Term -> NamesT (TCMT IO) Term
forall (f :: * -> *) a. Applicative f => a -> f a
pure Term
iz) [NamesT (TCMT IO) Term]
vars

          -- Γ,φ,u,u0,Δ(x = hcomp φ u u0) ⊢ b : (i : I) → [α] -> old_t[x = hfill φ u u0 i,δ_fill[i]]
          NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
b <- do
             [(NamesT (TCMT IO) Term,
  NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)]
sides <- [(Term, (Term, Term))]
-> ((Term, (Term, Term))
    -> NamesT
         (TCMT IO)
         (NamesT (TCMT IO) Term,
          NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term))
-> NamesT
     (TCMT IO)
     [(NamesT (TCMT IO) Term,
       NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [(Term, (Term, Term))]
alphab (((Term, (Term, Term))
  -> NamesT
       (TCMT IO)
       (NamesT (TCMT IO) Term,
        NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term))
 -> NamesT
      (TCMT IO)
      [(NamesT (TCMT IO) Term,
        NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)])
-> ((Term, (Term, Term))
    -> NamesT
         (TCMT IO)
         (NamesT (TCMT IO) Term,
          NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term))
-> NamesT
     (TCMT IO)
     [(NamesT (TCMT IO) Term,
       NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)]
forall a b. (a -> b) -> a -> b
$ \ (Term
psi,(Term
side0,Term
side1)) -> do
                NamesT (TCMT IO) Term
psi <- Term -> NamesT (TCMT IO) (NamesT (TCMT IO) Term)
forall (m :: * -> *) a.
(MonadFail m, Subst a) =>
a -> NamesT m (NamesT m a)
open (Term -> NamesT (TCMT IO) (NamesT (TCMT IO) Term))
-> Term -> NamesT (TCMT IO) (NamesT (TCMT IO) Term)
forall a b. (a -> b) -> a -> b
$ Substitution' Term
Substitution' (SubstArg Term)
hcompS Substitution' (SubstArg Term) -> Term -> Term
forall a. Subst a => Substitution' (SubstArg a) -> a -> a
`applySubst` Term
psi

                [NamesT (TCMT IO) Term
side0,NamesT (TCMT IO) Term
side1] <- (Term -> NamesT (TCMT IO) (NamesT (TCMT IO) Term))
-> [Term] -> NamesT (TCMT IO) [NamesT (TCMT IO) Term]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Term -> NamesT (TCMT IO) (NamesT (TCMT IO) Term)
forall (m :: * -> *) a.
(MonadFail m, Subst a) =>
a -> NamesT m (NamesT m a)
open (Term -> NamesT (TCMT IO) (NamesT (TCMT IO) Term))
-> (Term -> Term)
-> Term
-> NamesT (TCMT IO) (NamesT (TCMT IO) Term)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Term -> Term
forall a. Subst a => Int -> a -> a
raise (Int
3Int -> Int -> Int
forall a. Num a => a -> a -> a
+Abs Telescope -> Int
forall a. Sized a => a -> Int
size Abs Telescope
delta) (Term -> Term) -> (Term -> Term) -> Term -> Term
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Telescope -> Term -> Term
forall t. Abstract t => Telescope -> t -> t
abstract Telescope
hdelta) [Term
side0,Term
side1]
                (NamesT (TCMT IO) Term,
 NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT
     (TCMT IO)
     (NamesT (TCMT IO) Term,
      NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
forall (m :: * -> *) a. Monad m => a -> m a
return ((NamesT (TCMT IO) Term,
  NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
 -> NamesT
      (TCMT IO)
      (NamesT (TCMT IO) Term,
       NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term))
-> (NamesT (TCMT IO) Term,
    NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT
     (TCMT IO)
     (NamesT (TCMT IO) Term,
      NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
forall a b. (a -> b) -> a -> b
$ (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
ineg NamesT (TCMT IO) Term
psi NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
`imax` NamesT (TCMT IO) Term
psi, \ NamesT (TCMT IO) Term
i -> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
pOr_ty NamesT (TCMT IO) Term
i (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
ineg NamesT (TCMT IO) Term
psi) NamesT (TCMT IO) Term
psi ([Char]
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
forall (m :: * -> *).
MonadFail m =>
[Char] -> (NamesT m Term -> NamesT m Term) -> NamesT m Term
ilam [Char]
"o" ((NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
 -> NamesT (TCMT IO) Term)
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
forall a b. (a -> b) -> a -> b
$ \ NamesT (TCMT IO) Term
_ -> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
apply_delta_fill NamesT (TCMT IO) Term
i (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall a b. (a -> b) -> a -> b
$ NamesT (TCMT IO) Term
side0 NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
hfill NamesT (TCMT IO) Term
lvl NamesT (TCMT IO) Term
htype NamesT (TCMT IO) Term
phi NamesT (TCMT IO) Term
u NamesT (TCMT IO) Term
u0 NamesT (TCMT IO) Term
i)
                                                            ([Char]
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
forall (m :: * -> *).
MonadFail m =>
[Char] -> (NamesT m Term -> NamesT m Term) -> NamesT m Term
ilam [Char]
"o" ((NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
 -> NamesT (TCMT IO) Term)
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
forall a b. (a -> b) -> a -> b
$ \ NamesT (TCMT IO) Term
_ -> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
apply_delta_fill NamesT (TCMT IO) Term
i (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall a b. (a -> b) -> a -> b
$ NamesT (TCMT IO) Term
side1 NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
hfill NamesT (TCMT IO) Term
lvl NamesT (TCMT IO) Term
htype NamesT (TCMT IO) Term
phi NamesT (TCMT IO) Term
u NamesT (TCMT IO) Term
u0 NamesT (TCMT IO) Term
i))
             let recurse :: [(NamesT (TCMT IO) Term,
  NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)]
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
recurse []           NamesT (TCMT IO) Term
i = NamesT (TCMT IO) Term
forall a. HasCallStack => a
__IMPOSSIBLE__
                 recurse [(NamesT (TCMT IO) Term
psi,NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
u)]    NamesT (TCMT IO) Term
i = NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
u NamesT (TCMT IO) Term
i
                 recurse ((NamesT (TCMT IO) Term
psi,NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
u):[(NamesT (TCMT IO) Term,
  NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)]
xs) NamesT (TCMT IO) Term
i = NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
pOr_ty NamesT (TCMT IO) Term
i NamesT (TCMT IO) Term
psi (((NamesT (TCMT IO) Term,
  NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
 -> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
-> [(NamesT (TCMT IO) Term,
     NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)]
-> NamesT (TCMT IO) Term
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
imax (NamesT (TCMT IO) Term
 -> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> ((NamesT (TCMT IO) Term,
     NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
    -> NamesT (TCMT IO) Term)
-> (NamesT (TCMT IO) Term,
    NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (NamesT (TCMT IO) Term,
 NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
forall a b. (a, b) -> a
fst) (Term -> NamesT (TCMT IO) Term
forall (f :: * -> *) a. Applicative f => a -> f a
pure Term
iz) [(NamesT (TCMT IO) Term,
  NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)]
xs) (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
u NamesT (TCMT IO) Term
i) ([(NamesT (TCMT IO) Term,
  NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)]
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
recurse [(NamesT (TCMT IO) Term,
  NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)]
xs NamesT (TCMT IO) Term
i)
             (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT
     (TCMT IO) (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
forall (m :: * -> *) a. Monad m => a -> m a
return ((NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
 -> NamesT
      (TCMT IO) (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term))
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT
     (TCMT IO) (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
forall a b. (a -> b) -> a -> b
$ [(NamesT (TCMT IO) Term,
  NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)]
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
recurse [(NamesT (TCMT IO) Term,
  NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)]
sides

          ((,) (Type -> Term -> (Type, Term))
-> NamesT (TCMT IO) Type -> NamesT (TCMT IO) (Term -> (Type, Term))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Type
ty (Term -> NamesT (TCMT IO) Term
forall (f :: * -> *) a. Applicative f => a -> f a
pure Term
io) NamesT (TCMT IO) (Term -> (Type, Term))
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) (Type, Term)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*>) (NamesT (TCMT IO) Term -> NamesT (TCMT IO) (Type, Term))
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) (Type, Term)
forall a b. (a -> b) -> a -> b
$ do
            NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
comp NamesT (TCMT IO) Term
ty_level
               ([Char]
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
forall (m :: * -> *).
MonadFail m =>
[Char] -> (NamesT m Term -> NamesT m Term) -> NamesT m Term
lam [Char]
"i" ((NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
 -> NamesT (TCMT IO) Term)
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
forall a b. (a -> b) -> a -> b
$ (Type -> Term) -> NamesT (TCMT IO) Type -> NamesT (TCMT IO) Term
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Type -> Term
forall t a. Type'' t a -> a
unEl (NamesT (TCMT IO) Type -> NamesT (TCMT IO) Term)
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Type)
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NamesT (TCMT IO) Term -> NamesT (TCMT IO) Type
ty)
                           (NamesT (TCMT IO) Term
phi NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
`imax` NamesT (TCMT IO) Term
alpha)
                           ([Char]
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
forall (m :: * -> *).
MonadFail m =>
[Char] -> (NamesT m Term -> NamesT m Term) -> NamesT m Term
lam [Char]
"i" ((NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
 -> NamesT (TCMT IO) Term)
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
forall a b. (a -> b) -> a -> b
$ \ NamesT (TCMT IO) Term
i ->
                               let rhs :: NamesT (TCMT IO) Term
rhs = ([Char]
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
forall (m :: * -> *).
MonadFail m =>
[Char] -> (NamesT m Term -> NamesT m Term) -> NamesT m Term
ilam [Char]
"o" ((NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
 -> NamesT (TCMT IO) Term)
-> (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term)
-> NamesT (TCMT IO) Term
forall a b. (a -> b) -> a -> b
$ \ NamesT (TCMT IO) Term
o -> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
call (NamesT (TCMT IO) Term
u NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<@> NamesT (TCMT IO) Term
i NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
forall (m :: * -> *). Applicative m => m Term -> m Term -> m Term
<..> NamesT (TCMT IO) Term
o) NamesT (TCMT IO) Term
i)
                               in if [(Term, (Term, Term))] -> Bool
forall a. Null a => a -> Bool
null [(Term, (Term, Term))]
alphab then NamesT (TCMT IO) Term
rhs else
                                   NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term
pOr_ty NamesT (TCMT IO) Term
i NamesT (TCMT IO) Term
phi NamesT (TCMT IO) Term
alpha NamesT (TCMT IO) Term
rhs (NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
b NamesT (TCMT IO) Term
i)
                           )
                           (NamesT (TCMT IO) Term
-> NamesT (TCMT IO) Term -> NamesT (TCMT IO) Term
call NamesT (TCMT IO) Term
u0 (Term -> NamesT (TCMT IO) Term
forall (f :: * -> *) a. Applicative f => a -> f a
pure Term
iz))
    [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.hcomp" Int
20 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text [Char]
"old_tel =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Telescope -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM Telescope
tel
    let n :: Int
n = Telescope -> Int
forall a. Sized a => a -> Int
size Telescope
tel Int -> Int -> Int
forall a. Num a => a -> a -> a
- (Telescope -> Int
forall a. Sized a => a -> Int
size Telescope
gamma Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Abs Telescope -> Int
forall a. Sized a => a -> Int
size Abs Telescope
delta)
    [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.hcomp" Int
20 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text [Char]
"n =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text (Int -> [Char]
forall a. Show a => a -> [Char]
show Int
n)
    (TelV Telescope
deltaEx Type
t,[(Term, (Term, Term))]
bs) <- Int -> Type -> TCMT IO (TelV Type, [(Term, (Term, Term))])
forall (m :: * -> *).
PureTCM m =>
Int -> Type -> m (TelV Type, [(Term, (Term, Term))])
telViewUpToPathBoundary' Int
n Type
ty
    Term
rhs <- Term -> TCMT IO Term
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Term -> TCMT IO Term) -> Term -> TCMT IO Term
forall a b. (a -> b) -> a -> b
$ Int -> Term -> Term
forall a. Subst a => Int -> a -> a
raise Int
n Term
rhs Term -> [Elim] -> Term
forall t. Apply t => t -> [Elim] -> t
`applyE` Telescope -> [(Term, (Term, Term))] -> [Elim]
forall a. DeBruijn a => Telescope -> Boundary' (a, a) -> [Elim' a]
teleElims Telescope
deltaEx [(Term, (Term, Term))]
bs

    Telescope
cxt <- TCMT IO Telescope
forall (m :: * -> *). (Applicative m, MonadTCEnv m) => m Telescope
getContextTelescope
    [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.hcomp" Int
30 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text [Char]
"cxt = " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Telescope -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM Telescope
cxt
    [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.hcomp" Int
30 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text [Char]
"tel = " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Telescope -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM Telescope
tel
    [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.hcomp" Int
20 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ Telescope -> TCMT IO Doc -> TCMT IO Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext Telescope
tel (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text [Char]
"t = " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Type -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM Type
t
    [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.hcomp" Int
20 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ Telescope -> TCMT IO Doc -> TCMT IO Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext Telescope
tel (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text [Char]
"rhs = " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Term -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM Term
rhs

    Clause -> TCM Clause
forall (m :: * -> *) a. Monad m => a -> m a
return (Clause -> TCM Clause) -> Clause -> TCM Clause
forall a b. (a -> b) -> a -> b
$ Clause { clauseLHSRange :: Range
clauseLHSRange  = Range
forall a. Range' a
noRange
                    , clauseFullRange :: Range
clauseFullRange = Range
forall a. Range' a
noRange
                    , clauseTel :: Telescope
clauseTel       = Telescope
tel
                    , namedClausePats :: NAPs
namedClausePats = [NamedArg SplitPattern] -> NAPs
fromSplitPatterns [NamedArg SplitPattern]
ps
                    , clauseBody :: Maybe Term
clauseBody      = Term -> Maybe Term
forall a. a -> Maybe a
Just (Term -> Maybe Term) -> Term -> Maybe Term
forall a b. (a -> b) -> a -> b
$ Term
rhs
                    , clauseType :: Maybe (Arg Type)
clauseType      = Arg Type -> Maybe (Arg Type)
forall a. a -> Maybe a
Just (Arg Type -> Maybe (Arg Type)) -> Arg Type -> Maybe (Arg Type)
forall a b. (a -> b) -> a -> b
$ Type -> Arg Type
forall a. a -> Arg a
defaultArg Type
t
                    , clauseCatchall :: Bool
clauseCatchall  = Bool
False
                    , clauseExact :: Maybe Bool
clauseExact       = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True
                    , clauseRecursive :: Maybe Bool
clauseRecursive   = Maybe Bool
forall a. Maybe a
Nothing     -- TODO: can it be recursive?
                    , clauseUnreachable :: Maybe Bool
clauseUnreachable = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False  -- missing, thus, not unreachable
                    , clauseEllipsis :: ExpandedEllipsis
clauseEllipsis  = ExpandedEllipsis
NoEllipsis
                    }
  QName -> [Clause] -> TCMT IO ()
addClauses QName
f [Clause
cl]  -- Important: add at the end.
  Clause -> TCM Clause
forall (m :: * -> *) a. Monad m => a -> m a
return Clause
cl
createMissingHCompClause QName
_ Arg Int
_ BlockingVar
_ SplitClause
_ (SClause Telescope
_ [NamedArg SplitPattern]
_ Substitution' SplitPattern
_ Map CheckpointId (Substitution' Term)
_ Maybe (Dom Type)
Nothing) = TCM Clause
forall a. HasCallStack => a
__IMPOSSIBLE__

-- | Append a instance clause to the clauses of a function.
inferMissingClause
  :: QName
       -- ^ Function name.
  -> SplitClause
       -- ^ Clause to add.  Clause hiding (in 'clauseType') must be 'Instance'.
   -> TCM Clause
inferMissingClause :: QName -> SplitClause -> TCM Clause
inferMissingClause QName
f (SClause Telescope
tel [NamedArg SplitPattern]
ps Substitution' SplitPattern
_ Map CheckpointId (Substitution' Term)
cps (Just Dom Type
t)) = QName -> TCM Clause -> TCM Clause
forall (m :: * -> *) x a.
(MonadTrace m, HasRange x) =>
x -> m a -> m a
setCurrentRange QName
f (TCM Clause -> TCM Clause) -> TCM Clause -> TCM Clause
forall a b. (a -> b) -> a -> b
$ do
  [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.infer" Int
20 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ Telescope -> TCMT IO Doc -> TCMT IO Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext Telescope
tel (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ TCMT IO Doc
"Trying to infer right-hand side of type" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Dom Type -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM Dom Type
t
  Term
rhs <-
    Telescope -> TCMT IO Term -> TCMT IO Term
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext Telescope
tel
    (TCMT IO Term -> TCMT IO Term) -> TCMT IO Term -> TCMT IO Term
forall a b. (a -> b) -> a -> b
$ Lens' (Map CheckpointId (Substitution' Term)) TCEnv
-> (Map CheckpointId (Substitution' Term)
    -> Map CheckpointId (Substitution' Term))
-> TCMT IO Term
-> TCMT IO Term
forall (m :: * -> *) a b.
MonadTCEnv m =>
Lens' a TCEnv -> (a -> a) -> m b -> m b
locallyTC Lens' (Map CheckpointId (Substitution' Term)) TCEnv
eCheckpoints (Map CheckpointId (Substitution' Term)
-> Map CheckpointId (Substitution' Term)
-> Map CheckpointId (Substitution' Term)
forall a b. a -> b -> a
const Map CheckpointId (Substitution' Term)
cps)
    (TCMT IO Term -> TCMT IO Term) -> TCMT IO Term -> TCMT IO Term
forall a b. (a -> b) -> a -> b
$ Substitution' Term -> TCMT IO Term -> TCMT IO Term
forall (tcm :: * -> *) a.
(MonadDebug tcm, MonadTCM tcm, MonadFresh CheckpointId tcm,
 ReadTCState tcm) =>
Substitution' Term -> tcm a -> tcm a
checkpoint Substitution' Term
forall a. Substitution' a
IdS    -- introduce a fresh checkpoint
    (TCMT IO Term -> TCMT IO Term) -> TCMT IO Term -> TCMT IO Term
forall a b. (a -> b) -> a -> b
$ case Dom Type -> Hiding
forall a. LensHiding a => a -> Hiding
getHiding Dom Type
t of
        Hiding
_ | Just Term
tac <- Dom Type -> Maybe Term
forall t e. Dom' t e -> Maybe t
domTactic Dom Type
t -> do
          [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.infer" Int
40 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
            [ TCMT IO Doc
"@tactic rhs"
            , Int -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => Int -> m Doc -> m Doc
nest Int
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ TCMT IO Doc
"target =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Dom Type -> TCMT IO Doc
forall (m :: * -> *) a. (Applicative m, Pretty a) => a -> m Doc
pretty Dom Type
t ]
          (MetaId
_, Term
v) <- RunMetaOccursCheck -> Comparison -> Type -> TCMT IO (MetaId, Term)
forall (m :: * -> *).
MonadMetaSolver m =>
RunMetaOccursCheck -> Comparison -> Type -> m (MetaId, Term)
newValueMeta RunMetaOccursCheck
DontRunMetaOccursCheck Comparison
CmpLeq (Dom Type -> Type
forall t e. Dom' t e -> e
unDom Dom Type
t)
          Term
v Term -> TCMT IO () -> TCMT IO Term
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Term -> Term -> Type -> TCMT IO ()
unquoteTactic Term
tac Term
v (Dom Type -> Type
forall t e. Dom' t e -> e
unDom Dom Type
t)
        Instance{} -> (MetaId, Term) -> Term
forall a b. (a, b) -> b
snd ((MetaId, Term) -> Term) -> TCMT IO (MetaId, Term) -> TCMT IO Term
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> Type -> TCMT IO (MetaId, Term)
forall (m :: * -> *).
MonadMetaSolver m =>
[Char] -> Type -> m (MetaId, Term)
newInstanceMeta [Char]
"" (Dom Type -> Type
forall t e. Dom' t e -> e
unDom Dom Type
t)
        Hiding
Hidden     -> TCMT IO Term
forall a. HasCallStack => a
__IMPOSSIBLE__
        Hiding
NotHidden  -> TCMT IO Term
forall a. HasCallStack => a
__IMPOSSIBLE__
  let cl :: Clause
cl = Clause { clauseLHSRange :: Range
clauseLHSRange  = Range
forall a. Range' a
noRange
                  , clauseFullRange :: Range
clauseFullRange = Range
forall a. Range' a
noRange
                  , clauseTel :: Telescope
clauseTel       = Telescope
tel
                  , namedClausePats :: NAPs
namedClausePats = [NamedArg SplitPattern] -> NAPs
fromSplitPatterns [NamedArg SplitPattern]
ps
                  , clauseBody :: Maybe Term
clauseBody      = Term -> Maybe Term
forall a. a -> Maybe a
Just Term
rhs
                  , clauseType :: Maybe (Arg Type)
clauseType      = Arg Type -> Maybe (Arg Type)
forall a. a -> Maybe a
Just (Dom Type -> Arg Type
forall t a. Dom' t a -> Arg a
argFromDom Dom Type
t)
                  , clauseCatchall :: Bool
clauseCatchall  = Bool
False
                  , clauseExact :: Maybe Bool
clauseExact       = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True
                  , clauseRecursive :: Maybe Bool
clauseRecursive   = Maybe Bool
forall a. Maybe a
Nothing     -- could be recursive
                  , clauseUnreachable :: Maybe Bool
clauseUnreachable = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False  -- missing, thus, not unreachable
                  , clauseEllipsis :: ExpandedEllipsis
clauseEllipsis  = ExpandedEllipsis
NoEllipsis
                  }
  QName -> [Clause] -> TCMT IO ()
addClauses QName
f [Clause
cl]  -- Important: add at the end.
  Clause -> TCM Clause
forall (m :: * -> *) a. Monad m => a -> m a
return Clause
cl
inferMissingClause QName
_ (SClause Telescope
_ [NamedArg SplitPattern]
_ Substitution' SplitPattern
_ Map CheckpointId (Substitution' Term)
_ Maybe (Dom Type)
Nothing) = TCM Clause
forall a. HasCallStack => a
__IMPOSSIBLE__

splitStrategy :: BlockingVars -> Telescope -> TCM BlockingVars
splitStrategy :: BlockingVars -> Telescope -> TCM BlockingVars
splitStrategy BlockingVars
bs Telescope
tel = BlockingVars -> TCM BlockingVars
forall (m :: * -> *) a. Monad m => a -> m a
return (BlockingVars -> TCM BlockingVars)
-> BlockingVars -> TCM BlockingVars
forall a b. (a -> b) -> a -> b
$ (BlockingVar -> BlockingVar) -> BlockingVars -> BlockingVars
forall a. (a -> a) -> [a] -> [a]
updateLast BlockingVar -> BlockingVar
setBlockingVarOverlap BlockingVars
xs
  -- Make sure we do not insists on precomputed coverage when
  -- we make our last try to split.
  -- Otherwise, we will not get a nice error message.
  where
    xs :: BlockingVars
xs             = BlockingVars
strict BlockingVars -> BlockingVars -> BlockingVars
forall a. [a] -> [a] -> [a]
++ BlockingVars
lazy
    (BlockingVars
lazy, BlockingVars
strict) = (BlockingVar -> Bool)
-> BlockingVars -> (BlockingVars, BlockingVars)
forall a. (a -> Bool) -> [a] -> ([a], [a])
List.partition BlockingVar -> Bool
blockingVarLazy BlockingVars
bs
{- KEEP!
--  Andreas, 2012-10-13
--  The following split strategy which prefers all-constructor columns
--  fails on test/fail/CoverStrategy
    xs       = ys ++ zs
    (ys, zs) = partition allConstructors bs
    allConstructors :: BlockingVar -> Bool
    allConstructors = isJust . snd
-}


-- | Check that a type is a non-irrelevant datatype or a record with
-- named constructor. Unless the 'Induction' argument is 'CoInductive'
-- the data type must be inductive.
isDatatype :: (MonadTCM tcm, MonadError SplitError tcm) =>
              Induction -> Dom Type ->
              tcm (DataOrRecord, QName, [Arg Term], [Arg Term], [QName], Bool)
isDatatype :: forall (tcm :: * -> *).
(MonadTCM tcm, MonadError SplitError tcm) =>
Induction
-> Dom Type -> tcm (DataOrRecord, QName, Args, Args, [QName], Bool)
isDatatype Induction
ind Dom Type
at = do
  let t :: Type
t       = Dom Type -> Type
forall t e. Dom' t e -> e
unDom Dom Type
at
      throw :: (Closure Type -> SplitError)
-> tcm (DataOrRecord, QName, Args, Args, [QName], Bool)
throw Closure Type -> SplitError
f = SplitError -> tcm (DataOrRecord, QName, Args, Args, [QName], Bool)
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (SplitError
 -> tcm (DataOrRecord, QName, Args, Args, [QName], Bool))
-> (Closure Type -> SplitError)
-> Closure Type
-> tcm (DataOrRecord, QName, Args, Args, [QName], Bool)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Closure Type -> SplitError
f (Closure Type
 -> tcm (DataOrRecord, QName, Args, Args, [QName], Bool))
-> tcm (Closure Type)
-> tcm (DataOrRecord, QName, Args, Args, [QName], Bool)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< do TCM (Closure Type) -> tcm (Closure Type)
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCM (Closure Type) -> tcm (Closure Type))
-> TCM (Closure Type) -> tcm (Closure Type)
forall a b. (a -> b) -> a -> b
$ Type -> TCM (Closure Type)
forall (m :: * -> *) a.
(MonadTCEnv m, ReadTCState m) =>
a -> m (Closure a)
buildClosure Type
t
  Type
t' <- TCMT IO Type -> tcm Type
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCMT IO Type -> tcm Type) -> TCMT IO Type -> tcm Type
forall a b. (a -> b) -> a -> b
$ Type -> TCMT IO Type
forall a (m :: * -> *). (Reduce a, MonadReduce m) => a -> m a
reduce Type
t
  Maybe QName
mInterval <- TCMT IO (Maybe QName) -> tcm (Maybe QName)
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCMT IO (Maybe QName) -> tcm (Maybe QName))
-> TCMT IO (Maybe QName) -> tcm (Maybe QName)
forall a b. (a -> b) -> a -> b
$ [Char] -> TCMT IO (Maybe QName)
forall (m :: * -> *). HasBuiltins m => [Char] -> m (Maybe QName)
getBuiltinName' [Char]
builtinInterval
  Maybe QName
mIsOne <- TCMT IO (Maybe QName) -> tcm (Maybe QName)
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCMT IO (Maybe QName) -> tcm (Maybe QName))
-> TCMT IO (Maybe QName) -> tcm (Maybe QName)
forall a b. (a -> b) -> a -> b
$ [Char] -> TCMT IO (Maybe QName)
forall (m :: * -> *). HasBuiltins m => [Char] -> m (Maybe QName)
getBuiltinName' [Char]
builtinIsOne
  case Type -> Term
forall t a. Type'' t a -> a
unEl Type
t' of
    Def QName
d [] | QName -> Maybe QName
forall a. a -> Maybe a
Just QName
d Maybe QName -> Maybe QName -> Bool
forall a. Eq a => a -> a -> Bool
== Maybe QName
mInterval -> (Closure Type -> SplitError)
-> tcm (DataOrRecord, QName, Args, Args, [QName], Bool)
throw Closure Type -> SplitError
NotADatatype
    Def QName
d [Apply Arg Term
phi] | QName -> Maybe QName
forall a. a -> Maybe a
Just QName
d Maybe QName -> Maybe QName -> Bool
forall a. Eq a => a -> a -> Bool
== Maybe QName
mIsOne -> do
                [(Map Int Bool, [Term])]
xs <- TCM [(Map Int Bool, [Term])] -> tcm [(Map Int Bool, [Term])]
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCM [(Map Int Bool, [Term])] -> tcm [(Map Int Bool, [Term])])
-> TCM [(Map Int Bool, [Term])] -> tcm [(Map Int Bool, [Term])]
forall a b. (a -> b) -> a -> b
$ Term -> TCM [(Map Int Bool, [Term])]
forall (m :: * -> *).
HasBuiltins m =>
Term -> m [(Map Int Bool, [Term])]
decomposeInterval (Term -> TCM [(Map Int Bool, [Term])])
-> TCMT IO Term -> TCM [(Map Int Bool, [Term])]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Term -> TCMT IO Term
forall a (m :: * -> *). (Reduce a, MonadReduce m) => a -> m a
reduce (Arg Term -> Term
forall e. Arg e -> e
unArg Arg Term
phi)
                if [(Map Int Bool, [Term])] -> Bool
forall a. Null a => a -> Bool
null [(Map Int Bool, [Term])]
xs
                   then (DataOrRecord, QName, Args, Args, [QName], Bool)
-> tcm (DataOrRecord, QName, Args, Args, [QName], Bool)
forall (m :: * -> *) a. Monad m => a -> m a
return ((DataOrRecord, QName, Args, Args, [QName], Bool)
 -> tcm (DataOrRecord, QName, Args, Args, [QName], Bool))
-> (DataOrRecord, QName, Args, Args, [QName], Bool)
-> tcm (DataOrRecord, QName, Args, Args, [QName], Bool)
forall a b. (a -> b) -> a -> b
$ (DataOrRecord
IsData, QName
d, [Arg Term
phi], [], [], Bool
False)
                   else (Closure Type -> SplitError)
-> tcm (DataOrRecord, QName, Args, Args, [QName], Bool)
throw Closure Type -> SplitError
NotADatatype
    Def QName
d [Elim]
es -> do
      let ~(Just Args
args) = [Elim] -> Maybe Args
forall a. [Elim' a] -> Maybe [Arg a]
allApplyElims [Elim]
es
      Defn
def <- TCM Defn -> tcm Defn
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCM Defn -> tcm Defn) -> TCM Defn -> tcm Defn
forall a b. (a -> b) -> a -> b
$ Definition -> Defn
theDef (Definition -> Defn) -> TCMT IO Definition -> TCM Defn
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> QName -> TCMT IO Definition
forall (m :: * -> *). HasConstInfo m => QName -> m Definition
getConstInfo QName
d
      case Defn
def of
        Datatype{dataPars :: Defn -> Int
dataPars = Int
np, dataCons :: Defn -> [QName]
dataCons = [QName]
cs}
          | Bool
otherwise -> do
              let (Args
ps, Args
is) = Int -> Args -> (Args, Args)
forall a. Int -> [a] -> ([a], [a])
splitAt Int
np Args
args
              (DataOrRecord, QName, Args, Args, [QName], Bool)
-> tcm (DataOrRecord, QName, Args, Args, [QName], Bool)
forall (m :: * -> *) a. Monad m => a -> m a
return (DataOrRecord
IsData, QName
d, Args
ps, Args
is, [QName]
cs, Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ [QName] -> Bool
forall a. Null a => a -> Bool
null (Defn -> [QName]
dataPathCons Defn
def))
        Record{recPars :: Defn -> Int
recPars = Int
np, recConHead :: Defn -> ConHead
recConHead = ConHead
con, recInduction :: Defn -> Maybe Induction
recInduction = Maybe Induction
i, EtaEquality
recEtaEquality' :: Defn -> EtaEquality
recEtaEquality' :: EtaEquality
recEtaEquality'}
          | Maybe Induction
i Maybe Induction -> Maybe Induction -> Bool
forall a. Eq a => a -> a -> Bool
== Induction -> Maybe Induction
forall a. a -> Maybe a
Just Induction
CoInductive Bool -> Bool -> Bool
&& Induction
ind Induction -> Induction -> Bool
forall a. Eq a => a -> a -> Bool
/= Induction
CoInductive ->
              (Closure Type -> SplitError)
-> tcm (DataOrRecord, QName, Args, Args, [QName], Bool)
throw Closure Type -> SplitError
CoinductiveDatatype
          | Bool
otherwise ->
              (DataOrRecord, QName, Args, Args, [QName], Bool)
-> tcm (DataOrRecord, QName, Args, Args, [QName], Bool)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Induction -> EtaEquality -> DataOrRecord
IsRecord Maybe Induction
i EtaEquality
recEtaEquality', QName
d, Args
args, [], [ConHead -> QName
conName ConHead
con], Bool
False)
        Defn
_ -> (Closure Type -> SplitError)
-> tcm (DataOrRecord, QName, Args, Args, [QName], Bool)
throw Closure Type -> SplitError
NotADatatype
    Term
_ -> (Closure Type -> SplitError)
-> tcm (DataOrRecord, QName, Args, Args, [QName], Bool)
throw Closure Type -> SplitError
NotADatatype

-- | Update the target type of the split clause after a case split.
fixTargetType
  :: Quantity  -- ^ The quantity of the thing that is split.
  -> SplitTag -> SplitClause -> Dom Type -> TCM SplitClause
fixTargetType :: Quantity
-> SplitTag -> SplitClause -> Dom Type -> TCMT IO SplitClause
fixTargetType Quantity
q SplitTag
tag sc :: SplitClause
sc@SClause{ scTel :: SplitClause -> Telescope
scTel = Telescope
sctel, scSubst :: SplitClause -> Substitution' SplitPattern
scSubst = Substitution' SplitPattern
sigma } Dom Type
target = do
    [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.target" Int
20 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
sep
      [ TCMT IO Doc
"split clause telescope: " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Telescope -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM Telescope
sctel
      ]
    [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.target" Int
60 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
sep
      [ TCMT IO Doc
"substitution          : " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Substitution' SplitPattern -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM Substitution' SplitPattern
sigma
      ]
    [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.target" Int
60 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
sep
      [ TCMT IO Doc
"target type before substitution:" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Dom Type -> TCMT IO Doc
forall (m :: * -> *) a. (Applicative m, Pretty a) => a -> m Doc
pretty Dom Type
target
      , TCMT IO Doc
"             after substitution:" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Dom Type -> TCMT IO Doc
forall (m :: * -> *) a. (Applicative m, Pretty a) => a -> m Doc
pretty (Substitution' SplitPattern -> Dom Type -> Dom Type
forall a. TermSubst a => Substitution' SplitPattern -> a -> a
applySplitPSubst Substitution' SplitPattern
sigma Dom Type
target)
      ]

    -- We update the target quantity to 0 for erased constructors, but
    -- not if the match is made in an erased position, or if the
    -- original constructor definition is not erased.
    Dom Type -> Dom Type
updQuant <- do
      let erased :: Bool
erased = case Quantity
q of
            Quantity0{} -> Bool
True
            Quantity1{} -> Bool
forall a. HasCallStack => a
__IMPOSSIBLE__
            Quantityω{} -> Bool
False
      if Bool
erased then (Dom Type -> Dom Type) -> TCMT IO (Dom Type -> Dom Type)
forall (m :: * -> *) a. Monad m => a -> m a
return Dom Type -> Dom Type
forall a. a -> a
id else case SplitTag
tag of
        SplitCon QName
c -> do
          Quantity
q <- Definition -> Quantity
forall a. LensQuantity a => a -> Quantity
getQuantity (Definition -> Quantity) -> TCMT IO Definition -> TCMT IO Quantity
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> QName -> TCMT IO Definition
forall (m :: * -> *).
(ReadTCState m, HasConstInfo m) =>
QName -> m Definition
getOriginalConstInfo QName
c
          case Quantity
q of
            Quantity0{} -> (Dom Type -> Dom Type) -> TCMT IO (Dom Type -> Dom Type)
forall (m :: * -> *) a. Monad m => a -> m a
return ((Dom Type -> Dom Type) -> TCMT IO (Dom Type -> Dom Type))
-> (Dom Type -> Dom Type) -> TCMT IO (Dom Type -> Dom Type)
forall a b. (a -> b) -> a -> b
$ (Quantity -> Quantity) -> Dom Type -> Dom Type
forall a. LensQuantity a => (Quantity -> Quantity) -> a -> a
mapQuantity (Quantity -> Quantity -> Quantity
composeQuantity Quantity
q)
            Quantity1{} -> (Dom Type -> Dom Type) -> TCMT IO (Dom Type -> Dom Type)
forall (m :: * -> *) a. Monad m => a -> m a
return Dom Type -> Dom Type
forall a. a -> a
id
            Quantityω{} -> (Dom Type -> Dom Type) -> TCMT IO (Dom Type -> Dom Type)
forall (m :: * -> *) a. Monad m => a -> m a
return Dom Type -> Dom Type
forall a. a -> a
id
        SplitLit{} -> (Dom Type -> Dom Type) -> TCMT IO (Dom Type -> Dom Type)
forall (m :: * -> *) a. Monad m => a -> m a
return Dom Type -> Dom Type
forall a. a -> a
id
        SplitCatchall{} -> (Dom Type -> Dom Type) -> TCMT IO (Dom Type -> Dom Type)
forall (m :: * -> *) a. Monad m => a -> m a
return Dom Type -> Dom Type
forall a. a -> a
id

    SplitClause -> TCMT IO SplitClause
forall (m :: * -> *) a. Monad m => a -> m a
return (SplitClause -> TCMT IO SplitClause)
-> SplitClause -> TCMT IO SplitClause
forall a b. (a -> b) -> a -> b
$ SplitClause
sc { scTarget :: Maybe (Dom Type)
scTarget = Dom Type -> Maybe (Dom Type)
forall a. a -> Maybe a
Just (Dom Type -> Maybe (Dom Type)) -> Dom Type -> Maybe (Dom Type)
forall a b. (a -> b) -> a -> b
$ Dom Type -> Dom Type
updQuant (Dom Type -> Dom Type) -> Dom Type -> Dom Type
forall a b. (a -> b) -> a -> b
$ Substitution' SplitPattern -> Dom Type -> Dom Type
forall a. TermSubst a => Substitution' SplitPattern -> a -> a
applySplitPSubst Substitution' SplitPattern
sigma Dom Type
target }


-- | Add more patterns to split clause if the target type is a function type.
--   Returns the domains of the function type (if any).
insertTrailingArgs
  :: Bool         -- ^ Force insertion even when there is a 'domTactic'?
  -> SplitClause
  -> TCM (Telescope, SplitClause)
insertTrailingArgs :: Bool -> SplitClause -> TCM (Telescope, SplitClause)
insertTrailingArgs Bool
force sc :: SplitClause
sc@SClause{ scTel :: SplitClause -> Telescope
scTel = Telescope
sctel, scPats :: SplitClause -> [NamedArg SplitPattern]
scPats = [NamedArg SplitPattern]
ps, scSubst :: SplitClause -> Substitution' SplitPattern
scSubst = Substitution' SplitPattern
sigma, scCheckpoints :: SplitClause -> Map CheckpointId (Substitution' Term)
scCheckpoints = Map CheckpointId (Substitution' Term)
cps, scTarget :: SplitClause -> Maybe (Dom Type)
scTarget = Maybe (Dom Type)
target } = do
  let fallback :: TCM (Telescope, SplitClause)
fallback = (Telescope, SplitClause) -> TCM (Telescope, SplitClause)
forall (m :: * -> *) a. Monad m => a -> m a
return (Telescope
forall a. Null a => a
empty, SplitClause
sc)
  Maybe (Dom Type)
-> TCM (Telescope, SplitClause)
-> (Dom Type -> TCM (Telescope, SplitClause))
-> TCM (Telescope, SplitClause)
forall a b. Maybe a -> b -> (a -> b) -> b
caseMaybe Maybe (Dom Type)
target TCM (Telescope, SplitClause)
fallback ((Dom Type -> TCM (Telescope, SplitClause))
 -> TCM (Telescope, SplitClause))
-> (Dom Type -> TCM (Telescope, SplitClause))
-> TCM (Telescope, SplitClause)
forall a b. (a -> b) -> a -> b
$ \ Dom Type
a -> do
    if Maybe Term -> Bool
forall a. Maybe a -> Bool
isJust (Dom Type -> Maybe Term
forall t e. Dom' t e -> Maybe t
domTactic Dom Type
a) Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
force then TCM (Telescope, SplitClause)
fallback else do
    (TelV Telescope
tel Type
b) <- Int -> Type -> TCMT IO (TelV Type)
forall (m :: * -> *).
(MonadReduce m, MonadAddContext m) =>
Int -> Type -> m (TelV Type)
telViewUpTo (-Int
1) (Type -> TCMT IO (TelV Type)) -> Type -> TCMT IO (TelV Type)
forall a b. (a -> b) -> a -> b
$ Dom Type -> Type
forall t e. Dom' t e -> e
unDom Dom Type
a
    [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.target" Int
15 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
sep
      [ TCMT IO Doc
"target type telescope: " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> do
          Telescope -> TCMT IO Doc -> TCMT IO Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext Telescope
sctel (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ Telescope -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM Telescope
tel
      , TCMT IO Doc
"target type core     : " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> do
          Telescope -> TCMT IO Doc -> TCMT IO Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext Telescope
sctel (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ Telescope -> TCMT IO Doc -> TCMT IO Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext Telescope
tel (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ Type -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM Type
b
      ]
    let n :: Int
n         = Telescope -> Int
forall a. Sized a => a -> Int
size Telescope
tel
        -- Andreas, 2016-10-04 issue #2236
        -- Need to set origin to "Inserted" to avoid printing of hidden patterns.
        xs :: [NamedArg SplitPattern]
xs        = (NamedArg SplitPattern -> NamedArg SplitPattern)
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a b. (a -> b) -> [a] -> [b]
map ((ArgInfo -> ArgInfo)
-> NamedArg SplitPattern -> NamedArg SplitPattern
forall a. LensArgInfo a => (ArgInfo -> ArgInfo) -> a -> a
mapArgInfo ArgInfo -> ArgInfo
hiddenInserted) ([NamedArg SplitPattern] -> [NamedArg SplitPattern])
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a b. (a -> b) -> a -> b
$ Telescope -> [NamedArg SplitPattern]
forall a. DeBruijn a => Telescope -> [NamedArg a]
teleNamedArgs Telescope
tel
        -- Compute new split clause
        sctel' :: Telescope
sctel'    = ListTel -> Telescope
telFromList (ListTel -> Telescope) -> ListTel -> Telescope
forall a b. (a -> b) -> a -> b
$ Telescope -> ListTel
forall t. Tele (Dom t) -> [Dom ([Char], t)]
telToList (Int -> Telescope -> Telescope
forall a. Subst a => Int -> a -> a
raise Int
n Telescope
sctel) ListTel -> ListTel -> ListTel
forall a. [a] -> [a] -> [a]
++ Telescope -> ListTel
forall t. Tele (Dom t) -> [Dom ([Char], t)]
telToList Telescope
tel
        -- Dot patterns in @ps@ need to be raised!  (Issue 1298)
        ps' :: [NamedArg SplitPattern]
ps'       = Substitution' (SubstArg [NamedArg SplitPattern])
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst (Int -> Substitution' SplitPattern
forall a. Int -> Substitution' a
raiseS Int
n) [NamedArg SplitPattern]
ps [NamedArg SplitPattern]
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a. [a] -> [a] -> [a]
++ [NamedArg SplitPattern]
xs
        newTarget :: Maybe (Dom Type)
newTarget = Dom Type -> Maybe (Dom Type)
forall a. a -> Maybe a
Just (Dom Type -> Maybe (Dom Type)) -> Dom Type -> Maybe (Dom Type)
forall a b. (a -> b) -> a -> b
$ (if Bool -> Bool
not (Telescope -> Bool
forall a. Null a => a -> Bool
null Telescope
tel) then Dom Type
a{ domTactic :: Maybe Term
domTactic = Maybe Term
forall a. Maybe a
Nothing } else Dom Type
a) Dom Type -> Type -> Dom Type
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Type
b
        sc' :: SplitClause
sc'       = SClause
          { scTel :: Telescope
scTel    = Telescope
sctel'
          , scPats :: [NamedArg SplitPattern]
scPats   = [NamedArg SplitPattern]
ps'
          , scSubst :: Substitution' SplitPattern
scSubst  = Int -> Substitution' SplitPattern -> Substitution' SplitPattern
forall a. Int -> Substitution' a -> Substitution' a
wkS Int
n (Substitution' SplitPattern -> Substitution' SplitPattern)
-> Substitution' SplitPattern -> Substitution' SplitPattern
forall a b. (a -> b) -> a -> b
$ Substitution' SplitPattern
sigma -- Should be wkS instead of liftS since
                                     -- variables are only added to new tel.
          , scCheckpoints :: Map CheckpointId (Substitution' Term)
scCheckpoints        = Substitution' (SubstArg (Map CheckpointId (Substitution' Term)))
-> Map CheckpointId (Substitution' Term)
-> Map CheckpointId (Substitution' Term)
forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst (Int -> Substitution' Term
forall a. Int -> Substitution' a
raiseS Int
n) Map CheckpointId (Substitution' Term)
cps
          , scTarget :: Maybe (Dom Type)
scTarget = Maybe (Dom Type)
newTarget
          }
    -- Separate debug printing to find cause of crash (Issue 1374)
    [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.target" Int
30 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
sep
      [ TCMT IO Doc
"new split clause telescope   : " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Telescope -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM Telescope
sctel'
      ]
    [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.target" Int
30 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
sep
      [ TCMT IO Doc
"new split clause patterns    : " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> do
          Telescope -> TCMT IO Doc -> TCMT IO Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext Telescope
sctel' (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ NAPs -> TCMT IO Doc
forall (m :: * -> *). MonadPretty m => NAPs -> m Doc
prettyTCMPatternList (NAPs -> TCMT IO Doc) -> NAPs -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [NamedArg SplitPattern] -> NAPs
fromSplitPatterns [NamedArg SplitPattern]
ps'
      ]
    [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.target" Int
60 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
sep
      [ TCMT IO Doc
"new split clause substitution: " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Substitution' SplitPattern -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM (SplitClause -> Substitution' SplitPattern
scSubst SplitClause
sc')
      ]
    [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.target" Int
30 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
sep
      [ TCMT IO Doc
"new split clause target      : " TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> do
          Telescope -> TCMT IO Doc -> TCMT IO Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext Telescope
sctel' (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ Dom Type -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM (Dom Type -> TCMT IO Doc) -> Dom Type -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ Maybe (Dom Type) -> Dom Type
forall a. HasCallStack => Maybe a -> a
fromJust Maybe (Dom Type)
newTarget
      ]
    [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.target" Int
20 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
sep
      [ TCMT IO Doc
"new split clause"
      , SplitClause -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM SplitClause
sc'
      ]
    (Telescope, SplitClause) -> TCM (Telescope, SplitClause)
forall (m :: * -> *) a. Monad m => a -> m a
return ((Telescope, SplitClause) -> TCM (Telescope, SplitClause))
-> (Telescope, SplitClause) -> TCM (Telescope, SplitClause)
forall a b. (a -> b) -> a -> b
$ if Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 then (Telescope
forall a. Null a => a
empty, SplitClause
sc { scTarget :: Maybe (Dom Type)
scTarget = Maybe (Dom Type)
newTarget }) else (Telescope
tel, SplitClause
sc')

-- Andreas, 2017-01-18, issue #819, set visible arguments to UserWritten.
-- Otherwise, they will be printed as _.
hiddenInserted :: ArgInfo -> ArgInfo
hiddenInserted :: ArgInfo -> ArgInfo
hiddenInserted ArgInfo
ai
  | ArgInfo -> Bool
forall a. LensHiding a => a -> Bool
visible ArgInfo
ai = Origin -> ArgInfo -> ArgInfo
forall a. LensOrigin a => Origin -> a -> a
setOrigin Origin
UserWritten ArgInfo
ai
  | Bool
otherwise  = Origin -> ArgInfo -> ArgInfo
forall a. LensOrigin a => Origin -> a -> a
setOrigin Origin
Inserted ArgInfo
ai

computeHCompSplit  :: Telescope   -- ^ Telescope before split point.
  -> PatVarName                   -- ^ Name of pattern variable at split point.
  -> Telescope                    -- ^ Telescope after split point.
  -> QName                        -- ^ Name of datatype to split at.
  -> Args                         -- ^ Data type parameters.
  -> Args                         -- ^ Data type indices.
  -> Nat                          -- ^ Index of split variable.
  -> Telescope                    -- ^ Telescope for the patterns.
  -> [NamedArg SplitPattern]      -- ^ Patterns before doing the split.
  -> Map CheckpointId Substitution -- ^ Current checkpoints
  -- -> QName                        -- ^ Constructor to fit into hole.
  -> CoverM (Maybe (SplitTag,SplitClause))   -- ^ New split clause if successful.
computeHCompSplit :: Telescope
-> [Char]
-> Telescope
-> QName
-> Args
-> Args
-> Int
-> Telescope
-> [NamedArg SplitPattern]
-> Map CheckpointId (Substitution' Term)
-> CoverM (Maybe (SplitTag, SplitClause))
computeHCompSplit Telescope
delta1 [Char]
n Telescope
delta2 QName
d Args
pars Args
ixs Int
hix Telescope
tel [NamedArg SplitPattern]
ps Map CheckpointId (Substitution' Term)
cps = do
    -- Get the type of the datatype
  -- Δ1 ⊢ dtype
  Sort
dsort <- TCM Sort -> ExceptT SplitError (TCMT IO) Sort
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCM Sort -> ExceptT SplitError (TCMT IO) Sort)
-> TCM Sort -> ExceptT SplitError (TCMT IO) Sort
forall a b. (a -> b) -> a -> b
$ ([Term] -> Substitution' Term
forall a. DeBruijn a => [a] -> Substitution' a
parallelS ([Term] -> [Term]
forall a. [a] -> [a]
reverse ([Term] -> [Term]) -> [Term] -> [Term]
forall a b. (a -> b) -> a -> b
$ (Arg Term -> Term) -> Args -> [Term]
forall a b. (a -> b) -> [a] -> [b]
map Arg Term -> Term
forall e. Arg e -> e
unArg Args
pars) Substitution' (SubstArg Sort) -> Sort -> Sort
forall a. Subst a => Substitution' (SubstArg a) -> a -> a
`applySubst`) (Sort -> Sort) -> (Definition -> Sort) -> Definition -> Sort
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Defn -> Sort
dataSort (Defn -> Sort) -> (Definition -> Defn) -> Definition -> Sort
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Definition -> Defn
theDef (Definition -> Sort) -> TCMT IO Definition -> TCM Sort
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> QName -> TCMT IO Definition
forall (m :: * -> *). HasConstInfo m => QName -> m Definition
getConstInfo QName
d
  QName
hCompName <- QName -> Maybe QName -> QName
forall a. a -> Maybe a -> a
fromMaybe QName
forall a. HasCallStack => a
__IMPOSSIBLE__ (Maybe QName -> QName)
-> ExceptT SplitError (TCMT IO) (Maybe QName)
-> ExceptT SplitError (TCMT IO) QName
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> ExceptT SplitError (TCMT IO) (Maybe QName)
forall (m :: * -> *). HasBuiltins m => [Char] -> m (Maybe QName)
getPrimitiveName' [Char]
builtinHComp
  Type
theHCompT <- Definition -> Type
defType (Definition -> Type)
-> ExceptT SplitError (TCMT IO) Definition
-> ExceptT SplitError (TCMT IO) Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> QName -> ExceptT SplitError (TCMT IO) Definition
forall (m :: * -> *). HasConstInfo m => QName -> m Definition
getConstInfo QName
hCompName
  let
    dlvl :: Term
dlvl = Level' Term -> Term
Level (Level' Term -> Term) -> (Sort -> Level' Term) -> Sort -> Term
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (\ (Type Level' Term
s) -> Level' Term
s) (Sort -> Term) -> Sort -> Term
forall a b. (a -> b) -> a -> b
$ Sort
dsort
    dterm :: Term
dterm = QName -> [Elim] -> Term
Def QName
d [] Term -> Args -> Term
forall t. Apply t => t -> Args -> t
`apply` (Args
pars Args -> Args -> Args
forall a. [a] -> [a] -> [a]
++ Args
ixs)
  -- Δ1 ⊢ gamma
  TelV Telescope
gamma Type
_ <- TCMT IO (TelV Type) -> ExceptT SplitError (TCMT IO) (TelV Type)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (TCMT IO (TelV Type) -> ExceptT SplitError (TCMT IO) (TelV Type))
-> TCMT IO (TelV Type) -> ExceptT SplitError (TCMT IO) (TelV Type)
forall a b. (a -> b) -> a -> b
$ Type -> TCMT IO (TelV Type)
forall (m :: * -> *).
(MonadReduce m, MonadAddContext m) =>
Type -> m (TelV Type)
telView (Type
theHCompT Type -> Args -> Type
`piApply` [Hiding -> Arg Term -> Arg Term
forall a. LensHiding a => Hiding -> a -> a
setHiding Hiding
Hidden (Arg Term -> Arg Term) -> Arg Term -> Arg Term
forall a b. (a -> b) -> a -> b
$ Term -> Arg Term
forall a. a -> Arg a
defaultArg (Term -> Arg Term) -> Term -> Arg Term
forall a b. (a -> b) -> a -> b
$ Term
dlvl , Term -> Arg Term
forall a. a -> Arg a
defaultArg (Term -> Arg Term) -> Term -> Arg Term
forall a b. (a -> b) -> a -> b
$ Term
dterm])
  case (Telescope
delta1 Telescope -> Telescope -> Telescope
forall t. Abstract t => Telescope -> t -> t
`abstract` Telescope
gamma,PatternSubstitution
forall a. Substitution' a
IdS) of
    (Telescope
delta1',PatternSubstitution
rho0) -> do
--      debugSubst "rho0" rho0

      -- We have Δ₁' ⊢ ρ₀ : Δ₁Γ, so split it into the part for Δ₁ and the part for Γ
      let (Substitution' SplitPattern
rho1,Substitution' SplitPattern
rho2) = Int
-> Substitution' SplitPattern
-> (Substitution' SplitPattern, Substitution' SplitPattern)
forall a.
Int -> Substitution' a -> (Substitution' a, Substitution' a)
splitS (Telescope -> Int
forall a. Sized a => a -> Int
size Telescope
gamma) (Substitution' SplitPattern
 -> (Substitution' SplitPattern, Substitution' SplitPattern))
-> Substitution' SplitPattern
-> (Substitution' SplitPattern, Substitution' SplitPattern)
forall a b. (a -> b) -> a -> b
$ PatternSubstitution -> Substitution' SplitPattern
toSplitPSubst PatternSubstitution
rho0

      let defp :: SplitPattern
defp = PatternInfo -> QName -> [NamedArg SplitPattern] -> SplitPattern
forall x.
PatternInfo -> QName -> [NamedArg (Pattern' x)] -> Pattern' x
DefP PatternInfo
defaultPatternInfo QName
hCompName ([NamedArg SplitPattern] -> SplitPattern)
-> ([NamedArg SplitPattern] -> [NamedArg SplitPattern])
-> [NamedArg SplitPattern]
-> SplitPattern
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (NamedArg SplitPattern -> NamedArg SplitPattern)
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a b. (a -> b) -> [a] -> [b]
map (Origin -> NamedArg SplitPattern -> NamedArg SplitPattern
forall a. LensOrigin a => Origin -> a -> a
setOrigin Origin
Inserted) ([NamedArg SplitPattern] -> SplitPattern)
-> [NamedArg SplitPattern] -> SplitPattern
forall a b. (a -> b) -> a -> b
$
                   (Arg SplitPattern -> NamedArg SplitPattern)
-> [Arg SplitPattern] -> [NamedArg SplitPattern]
forall a b. (a -> b) -> [a] -> [b]
map ((SplitPattern -> Named NamedName SplitPattern)
-> Arg SplitPattern -> NamedArg SplitPattern
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SplitPattern -> Named NamedName SplitPattern
forall a name. a -> Named name a
unnamed) [Hiding -> Arg SplitPattern -> Arg SplitPattern
forall a. LensHiding a => Hiding -> a -> a
setHiding Hiding
Hidden (Arg SplitPattern -> Arg SplitPattern)
-> Arg SplitPattern -> Arg SplitPattern
forall a b. (a -> b) -> a -> b
$ SplitPattern -> Arg SplitPattern
forall a. a -> Arg a
defaultArg (SplitPattern -> Arg SplitPattern)
-> SplitPattern -> Arg SplitPattern
forall a b. (a -> b) -> a -> b
$ Substitution' (SubstArg SplitPattern)
-> SplitPattern -> SplitPattern
forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst Substitution' SplitPattern
Substitution' (SubstArg SplitPattern)
rho1 (SplitPattern -> SplitPattern) -> SplitPattern -> SplitPattern
forall a b. (a -> b) -> a -> b
$ PatternInfo -> Term -> SplitPattern
forall x. PatternInfo -> Term -> Pattern' x
DotP PatternInfo
defaultPatternInfo (Term -> SplitPattern) -> Term -> SplitPattern
forall a b. (a -> b) -> a -> b
$ Term
dlvl
                                      ,Hiding -> Arg SplitPattern -> Arg SplitPattern
forall a. LensHiding a => Hiding -> a -> a
setHiding Hiding
Hidden (Arg SplitPattern -> Arg SplitPattern)
-> Arg SplitPattern -> Arg SplitPattern
forall a b. (a -> b) -> a -> b
$ SplitPattern -> Arg SplitPattern
forall a. a -> Arg a
defaultArg (SplitPattern -> Arg SplitPattern)
-> SplitPattern -> Arg SplitPattern
forall a b. (a -> b) -> a -> b
$ Substitution' (SubstArg SplitPattern)
-> SplitPattern -> SplitPattern
forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst Substitution' SplitPattern
Substitution' (SubstArg SplitPattern)
rho1 (SplitPattern -> SplitPattern) -> SplitPattern -> SplitPattern
forall a b. (a -> b) -> a -> b
$ PatternInfo -> Term -> SplitPattern
forall x. PatternInfo -> Term -> Pattern' x
DotP PatternInfo
defaultPatternInfo (Term -> SplitPattern) -> Term -> SplitPattern
forall a b. (a -> b) -> a -> b
$ Term
dterm]
                   [NamedArg SplitPattern]
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a. [a] -> [a] -> [a]
++ Substitution' (SubstArg [NamedArg SplitPattern])
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst Substitution' SplitPattern
Substitution' (SubstArg [NamedArg SplitPattern])
rho2 (Telescope -> [NamedArg SplitPattern]
forall a. DeBruijn a => Telescope -> [NamedArg a]
teleNamedArgs Telescope
gamma) -- rho0?
      -- Compute final context and substitution
      let rho3 :: Substitution' SplitPattern
rho3    = SplitPattern
-> Substitution' SplitPattern -> Substitution' SplitPattern
forall a. DeBruijn a => a -> Substitution' a -> Substitution' a
consS SplitPattern
defp Substitution' SplitPattern
rho1            -- Δ₁' ⊢ ρ₃ : Δ₁(x:D)
          delta2' :: Telescope
delta2' = Substitution' SplitPattern -> Telescope -> Telescope
forall a. TermSubst a => Substitution' SplitPattern -> a -> a
applySplitPSubst Substitution' SplitPattern
rho3 Telescope
delta2  -- Δ₂' = Δ₂ρ₃
          delta' :: Telescope
delta'  = Telescope
delta1' Telescope -> Telescope -> Telescope
forall t. Abstract t => Telescope -> t -> t
`abstract` Telescope
delta2' -- Δ'  = Δ₁'Δ₂'
          rho :: Substitution' SplitPattern
rho     = Int -> Substitution' SplitPattern -> Substitution' SplitPattern
forall a. Int -> Substitution' a -> Substitution' a
liftS (Telescope -> Int
forall a. Sized a => a -> Int
size Telescope
delta2) Substitution' SplitPattern
rho3   -- Δ' ⊢ ρ : Δ₁(x:D)Δ₂

      -- debugTel "delta'" delta'
      -- debugSubst "rho" rho
      -- debugPs tel ps

      -- Apply the substitution
      let ps' :: [NamedArg SplitPattern]
ps' = Substitution' (SubstArg [NamedArg SplitPattern])
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst Substitution' SplitPattern
Substitution' (SubstArg [NamedArg SplitPattern])
rho [NamedArg SplitPattern]
ps
      -- debugPlugged delta' ps'

      let cps' :: Map CheckpointId (Substitution' Term)
cps' = Substitution' SplitPattern
-> Map CheckpointId (Substitution' Term)
-> Map CheckpointId (Substitution' Term)
forall a. TermSubst a => Substitution' SplitPattern -> a -> a
applySplitPSubst Substitution' SplitPattern
rho Map CheckpointId (Substitution' Term)
cps

      Maybe (SplitTag, SplitClause)
-> CoverM (Maybe (SplitTag, SplitClause))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (SplitTag, SplitClause)
 -> CoverM (Maybe (SplitTag, SplitClause)))
-> Maybe (SplitTag, SplitClause)
-> CoverM (Maybe (SplitTag, SplitClause))
forall a b. (a -> b) -> a -> b
$ (SplitTag, SplitClause) -> Maybe (SplitTag, SplitClause)
forall a. a -> Maybe a
Just ((SplitTag, SplitClause) -> Maybe (SplitTag, SplitClause))
-> (SplitClause -> (SplitTag, SplitClause))
-> SplitClause
-> Maybe (SplitTag, SplitClause)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (QName -> SplitTag
SplitCon QName
hCompName,) (SplitClause -> Maybe (SplitTag, SplitClause))
-> SplitClause -> Maybe (SplitTag, SplitClause)
forall a b. (a -> b) -> a -> b
$ Telescope
-> [NamedArg SplitPattern]
-> Substitution' SplitPattern
-> Map CheckpointId (Substitution' Term)
-> Maybe (Dom Type)
-> SplitClause
SClause Telescope
delta' [NamedArg SplitPattern]
ps' Substitution' SplitPattern
rho Map CheckpointId (Substitution' Term)
cps' Maybe (Dom Type)
forall a. Maybe a
Nothing -- target fixed later



-- | @computeNeighbourhood delta1 delta2 d pars ixs hix tel ps con@
--
--   @
--      delta1   Telescope before split point
--      n        Name of pattern variable at split point
--      delta2   Telescope after split point
--      d        Name of datatype to split at
--      pars     Data type parameters
--      ixs      Data type indices
--      hix      Index of split variable
--      tel      Telescope for patterns ps
--      ps       Patterns before doing the split
--      cps      Current module parameter checkpoints
--      con      Constructor to fit into hole
--   @
--   @dtype == d pars ixs@
computeNeighbourhood
  :: Telescope                    -- ^ Telescope before split point.
  -> PatVarName                   -- ^ Name of pattern variable at split point.
  -> Telescope                    -- ^ Telescope after split point.
  -> QName                        -- ^ Name of datatype to split at.
  -> Args                         -- ^ Data type parameters.
  -> Args                         -- ^ Data type indices.
  -> Nat                          -- ^ Index of split variable.
  -> Telescope                    -- ^ Telescope for the patterns.
  -> [NamedArg SplitPattern]      -- ^ Patterns before doing the split.
  -> Map CheckpointId Substitution -- ^ Current checkpoints
  -> QName                        -- ^ Constructor to fit into hole.
  -> CoverM (Maybe SplitClause)   -- ^ New split clause if successful.
computeNeighbourhood :: Telescope
-> [Char]
-> Telescope
-> QName
-> Args
-> Args
-> Int
-> Telescope
-> [NamedArg SplitPattern]
-> Map CheckpointId (Substitution' Term)
-> QName
-> CoverM (Maybe SplitClause)
computeNeighbourhood Telescope
delta1 [Char]
n Telescope
delta2 QName
d Args
pars Args
ixs Int
hix Telescope
tel [NamedArg SplitPattern]
ps Map CheckpointId (Substitution' Term)
cps QName
c = do

  -- Get the type of the datatype
  Type
dtype <- TCMT IO Type -> ExceptT SplitError (TCMT IO) Type
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCMT IO Type -> ExceptT SplitError (TCMT IO) Type)
-> TCMT IO Type -> ExceptT SplitError (TCMT IO) Type
forall a b. (a -> b) -> a -> b
$ (Type -> Args -> Type
`piApply` Args
pars) (Type -> Type) -> (Definition -> Type) -> Definition -> Type
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Definition -> Type
defType (Definition -> Type) -> TCMT IO Definition -> TCMT IO Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> QName -> TCMT IO Definition
forall (m :: * -> *). HasConstInfo m => QName -> m Definition
getConstInfo QName
d

  -- Get the real constructor name
  ConHead
con <- TCM ConHead -> ExceptT SplitError (TCMT IO) ConHead
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCM ConHead -> ExceptT SplitError (TCMT IO) ConHead)
-> TCM ConHead -> ExceptT SplitError (TCMT IO) ConHead
forall a b. (a -> b) -> a -> b
$ (SigError -> ConHead) -> Either SigError ConHead -> ConHead
forall a b. (a -> b) -> Either a b -> b
fromRight SigError -> ConHead
forall a. HasCallStack => a
__IMPOSSIBLE__ (Either SigError ConHead -> ConHead)
-> TCMT IO (Either SigError ConHead) -> TCM ConHead
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> QName -> TCMT IO (Either SigError ConHead)
getConForm QName
c
  ConHead
con <- ConHead -> ExceptT SplitError (TCMT IO) ConHead
forall (m :: * -> *) a. Monad m => a -> m a
return (ConHead -> ExceptT SplitError (TCMT IO) ConHead)
-> ConHead -> ExceptT SplitError (TCMT IO) ConHead
forall a b. (a -> b) -> a -> b
$ ConHead
con { conName :: QName
conName = QName
c }  -- What if we restore the current name?
                                       -- Andreas, 2013-11-29 changes nothing!

  -- Get the type of the constructor
  Type
ctype <- TCMT IO Type -> ExceptT SplitError (TCMT IO) Type
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCMT IO Type -> ExceptT SplitError (TCMT IO) Type)
-> TCMT IO Type -> ExceptT SplitError (TCMT IO) Type
forall a b. (a -> b) -> a -> b
$ Definition -> Type
defType (Definition -> Type) -> TCMT IO Definition -> TCMT IO Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ConHead -> TCMT IO Definition
forall (m :: * -> *). HasConstInfo m => ConHead -> m Definition
getConInfo ConHead
con

  -- Lookup the type of the constructor at the given parameters
  (Telescope
gamma0, Args
cixs, [(Term, (Term, Term))]
boundary) <- do
    (TelV Telescope
gamma0 (El Sort
_ Term
d), [(Term, (Term, Term))]
boundary) <- TCMT IO (TelV Type, [(Term, (Term, Term))])
-> ExceptT SplitError (TCMT IO) (TelV Type, [(Term, (Term, Term))])
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCMT IO (TelV Type, [(Term, (Term, Term))])
 -> ExceptT
      SplitError (TCMT IO) (TelV Type, [(Term, (Term, Term))]))
-> TCMT IO (TelV Type, [(Term, (Term, Term))])
-> ExceptT SplitError (TCMT IO) (TelV Type, [(Term, (Term, Term))])
forall a b. (a -> b) -> a -> b
$ Type -> TCMT IO (TelV Type, [(Term, (Term, Term))])
forall (m :: * -> *).
PureTCM m =>
Type -> m (TelV Type, [(Term, (Term, Term))])
telViewPathBoundaryP (Type
ctype Type -> Args -> Type
`piApply` Args
pars)
    let Def QName
_ [Elim]
es = Term
d
        Just Args
cixs = [Elim] -> Maybe Args
forall a. [Elim' a] -> Maybe [Arg a]
allApplyElims [Elim]
es
    (Telescope, Args, [(Term, (Term, Term))])
-> ExceptT
     SplitError (TCMT IO) (Telescope, Args, [(Term, (Term, Term))])
forall (m :: * -> *) a. Monad m => a -> m a
return (Telescope
gamma0, Args
cixs, [(Term, (Term, Term))]
boundary)

  let (ListTel
_, Dom{domInfo :: forall t e. Dom' t e -> ArgInfo
domInfo = ArgInfo
info} : ListTel
_) = Int -> ListTel -> (ListTel, ListTel)
forall a. Int -> [a] -> ([a], [a])
splitAt (Telescope -> Int
forall a. Sized a => a -> Int
size Telescope
tel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
hix Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (Telescope -> ListTel
forall t. Tele (Dom t) -> [Dom ([Char], t)]
telToList Telescope
tel)

  -- Andreas, 2012-02-25 preserve name suggestion for recursive arguments
  -- of constructor

  let preserve :: ([Char], Type) -> ([Char], Type)
preserve ([Char]
x, t :: Type
t@(El Sort
_ (Def QName
d' [Elim]
_))) | QName
d QName -> QName -> Bool
forall a. Eq a => a -> a -> Bool
== QName
d' = ([Char]
n, Type
t)
      preserve ([Char]
x, Type
t) = ([Char]
x, Type
t)
      gamma :: Telescope
gamma  = ((Dom Type -> Dom Type) -> Telescope -> Telescope
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Dom Type -> Dom Type) -> Telescope -> Telescope)
-> ((Modality -> Modality) -> Dom Type -> Dom Type)
-> (Modality -> Modality)
-> Telescope
-> Telescope
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Modality -> Modality) -> Dom Type -> Dom Type
forall a. LensModality a => (Modality -> Modality) -> a -> a
mapModality) (Modality -> Modality -> Modality
composeModality (ArgInfo -> Modality
forall a. LensModality a => a -> Modality
getModality ArgInfo
info)) (Telescope -> Telescope) -> Telescope -> Telescope
forall a b. (a -> b) -> a -> b
$ ListTel -> Telescope
telFromList (ListTel -> Telescope)
-> (Telescope -> ListTel) -> Telescope -> Telescope
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Dom' Term ([Char], Type) -> Dom' Term ([Char], Type))
-> ListTel -> ListTel
forall a b. (a -> b) -> [a] -> [b]
map ((([Char], Type) -> ([Char], Type))
-> Dom' Term ([Char], Type) -> Dom' Term ([Char], Type)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([Char], Type) -> ([Char], Type)
preserve) (ListTel -> ListTel)
-> (Telescope -> ListTel) -> Telescope -> ListTel
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Telescope -> ListTel
forall t. Tele (Dom t) -> [Dom ([Char], t)]
telToList (Telescope -> Telescope) -> Telescope -> Telescope
forall a b. (a -> b) -> a -> b
$ Telescope
gamma0
      delta1Gamma :: Telescope
delta1Gamma = Telescope
delta1 Telescope -> Telescope -> Telescope
forall t. Abstract t => Telescope -> t -> t
`abstract` Telescope
gamma

  ConHead
-> Type
-> QName
-> Args
-> Args
-> Args
-> Telescope
-> Telescope
-> Telescope
-> Telescope
-> [NamedArg SplitPattern]
-> Int
-> ExceptT SplitError (TCMT IO) ()
forall {tcm :: * -> *} {a} {a} {a} {a} {a} {a} {a} {a} {a} {a} {a}.
(MonadTCM tcm, AddContext a, AddContext a, AddContext a,
 PrettyTCM a, PrettyTCM a, PrettyTCM a, PrettyTCM a, PrettyTCM a,
 PrettyTCM a, PrettyTCM a, PrettyTCM a, PrettyTCM a, PrettyTCM a,
 Show a, Show a, Show a, Show a, Show a, Show a, Show a, Show a,
 Show a, Show a) =>
a
-> a
-> a
-> [a]
-> [a]
-> [a]
-> a
-> a
-> a
-> a
-> [NamedArg SplitPattern]
-> a
-> tcm ()
debugInit ConHead
con Type
ctype QName
d Args
pars Args
ixs Args
cixs Telescope
delta1 Telescope
delta2 Telescope
gamma Telescope
tel [NamedArg SplitPattern]
ps Int
hix

  [IsForced]
cforced <- Definition -> [IsForced]
defForced (Definition -> [IsForced])
-> ExceptT SplitError (TCMT IO) Definition
-> ExceptT SplitError (TCMT IO) [IsForced]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> QName -> ExceptT SplitError (TCMT IO) Definition
forall (m :: * -> *). HasConstInfo m => QName -> m Definition
getConstInfo QName
c
      -- Variables in Δ₁ are not forced, since the unifier takes care to not introduce forced
      -- variables.
  let forced :: [IsForced]
forced = Int -> IsForced -> [IsForced]
forall a. Int -> a -> [a]
replicate (Telescope -> Int
forall a. Sized a => a -> Int
size Telescope
delta1) IsForced
NotForced [IsForced] -> [IsForced] -> [IsForced]
forall a. [a] -> [a] -> [a]
++ [IsForced]
cforced
      flex :: FlexibleVars
flex   = [IsForced] -> Telescope -> FlexibleVars
allFlexVars [IsForced]
forced Telescope
delta1Gamma -- All variables are flexible

  -- Unify constructor target and given type (in Δ₁Γ)
  let conIxs :: Args
conIxs   = Int -> Args -> Args
forall a. Int -> [a] -> [a]
drop (Args -> Int
forall a. Sized a => a -> Int
size Args
pars) Args
cixs
      givenIxs :: Args
givenIxs = Int -> Args -> Args
forall a. Subst a => Int -> a -> a
raise (Telescope -> Int
forall a. Sized a => a -> Int
size Telescope
gamma) Args
ixs

  -- Andrea 2019-07-17 propagate the Cohesion to the equation telescope
  -- TODO: should we propagate the modality in general?
  -- See also LHS checking.
  Type
dtype <- do
         let updCoh :: Cohesion -> Cohesion
updCoh = Cohesion -> Cohesion -> Cohesion
composeCohesion (ArgInfo -> Cohesion
forall a. LensCohesion a => a -> Cohesion
getCohesion ArgInfo
info)
         TelV Telescope
dtel Type
dt <- Type -> ExceptT SplitError (TCMT IO) (TelV Type)
forall (m :: * -> *).
(MonadReduce m, MonadAddContext m) =>
Type -> m (TelV Type)
telView Type
dtype
         Type -> ExceptT SplitError (TCMT IO) Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> ExceptT SplitError (TCMT IO) Type)
-> Type -> ExceptT SplitError (TCMT IO) Type
forall a b. (a -> b) -> a -> b
$ Telescope -> Type -> Type
forall t. Abstract t => Telescope -> t -> t
abstract ((Cohesion -> Cohesion) -> Dom Type -> Dom Type
forall a. LensCohesion a => (Cohesion -> Cohesion) -> a -> a
mapCohesion Cohesion -> Cohesion
updCoh (Dom Type -> Dom Type) -> Telescope -> Telescope
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Telescope
dtel) Type
dt

  ExceptT SplitError (TCMT IO) UnificationResult
-> ExceptT SplitError (TCMT IO) UnificationResult
withKIfStrict <- Telescope
-> ExceptT
     SplitError
     (TCMT IO)
     (ExceptT SplitError (TCMT IO) UnificationResult
      -> ExceptT SplitError (TCMT IO) UnificationResult)
-> ExceptT
     SplitError
     (TCMT IO)
     (ExceptT SplitError (TCMT IO) UnificationResult
      -> ExceptT SplitError (TCMT IO) UnificationResult)
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext Telescope
delta1 (ExceptT
   SplitError
   (TCMT IO)
   (ExceptT SplitError (TCMT IO) UnificationResult
    -> ExceptT SplitError (TCMT IO) UnificationResult)
 -> ExceptT
      SplitError
      (TCMT IO)
      (ExceptT SplitError (TCMT IO) UnificationResult
       -> ExceptT SplitError (TCMT IO) UnificationResult))
-> ExceptT
     SplitError
     (TCMT IO)
     (ExceptT SplitError (TCMT IO) UnificationResult
      -> ExceptT SplitError (TCMT IO) UnificationResult)
-> ExceptT
     SplitError
     (TCMT IO)
     (ExceptT SplitError (TCMT IO) UnificationResult
      -> ExceptT SplitError (TCMT IO) UnificationResult)
forall a b. (a -> b) -> a -> b
$ Sort -> ExceptT SplitError (TCMT IO) Sort
forall a (m :: * -> *). (Reduce a, MonadReduce m) => a -> m a
reduce (Type -> Sort
forall a. LensSort a => a -> Sort
getSort Type
dtype) ExceptT SplitError (TCMT IO) Sort
-> (Sort
    -> ExceptT
         SplitError
         (TCMT IO)
         (ExceptT SplitError (TCMT IO) UnificationResult
          -> ExceptT SplitError (TCMT IO) UnificationResult))
-> ExceptT
     SplitError
     (TCMT IO)
     (ExceptT SplitError (TCMT IO) UnificationResult
      -> ExceptT SplitError (TCMT IO) UnificationResult)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    SSet{} -> (ExceptT SplitError (TCMT IO) UnificationResult
 -> ExceptT SplitError (TCMT IO) UnificationResult)
-> ExceptT
     SplitError
     (TCMT IO)
     (ExceptT SplitError (TCMT IO) UnificationResult
      -> ExceptT SplitError (TCMT IO) UnificationResult)
forall (m :: * -> *) a. Monad m => a -> m a
return ((ExceptT SplitError (TCMT IO) UnificationResult
  -> ExceptT SplitError (TCMT IO) UnificationResult)
 -> ExceptT
      SplitError
      (TCMT IO)
      (ExceptT SplitError (TCMT IO) UnificationResult
       -> ExceptT SplitError (TCMT IO) UnificationResult))
-> (ExceptT SplitError (TCMT IO) UnificationResult
    -> ExceptT SplitError (TCMT IO) UnificationResult)
-> ExceptT
     SplitError
     (TCMT IO)
     (ExceptT SplitError (TCMT IO) UnificationResult
      -> ExceptT SplitError (TCMT IO) UnificationResult)
forall a b. (a -> b) -> a -> b
$ Lens' Bool TCEnv
-> (Bool -> Bool)
-> ExceptT SplitError (TCMT IO) UnificationResult
-> ExceptT SplitError (TCMT IO) UnificationResult
forall (m :: * -> *) a b.
MonadTCEnv m =>
Lens' a TCEnv -> (a -> a) -> m b -> m b
locallyTC Lens' Bool TCEnv
eSplitOnStrict ((Bool -> Bool)
 -> ExceptT SplitError (TCMT IO) UnificationResult
 -> ExceptT SplitError (TCMT IO) UnificationResult)
-> (Bool -> Bool)
-> ExceptT SplitError (TCMT IO) UnificationResult
-> ExceptT SplitError (TCMT IO) UnificationResult
forall a b. (a -> b) -> a -> b
$ Bool -> Bool -> Bool
forall a b. a -> b -> a
const Bool
True
    Sort
_      -> (ExceptT SplitError (TCMT IO) UnificationResult
 -> ExceptT SplitError (TCMT IO) UnificationResult)
-> ExceptT
     SplitError
     (TCMT IO)
     (ExceptT SplitError (TCMT IO) UnificationResult
      -> ExceptT SplitError (TCMT IO) UnificationResult)
forall (m :: * -> *) a. Monad m => a -> m a
return ExceptT SplitError (TCMT IO) UnificationResult
-> ExceptT SplitError (TCMT IO) UnificationResult
forall a. a -> a
id

  UnificationResult
r <- ExceptT SplitError (TCMT IO) UnificationResult
-> ExceptT SplitError (TCMT IO) UnificationResult
withKIfStrict (ExceptT SplitError (TCMT IO) UnificationResult
 -> ExceptT SplitError (TCMT IO) UnificationResult)
-> ExceptT SplitError (TCMT IO) UnificationResult
-> ExceptT SplitError (TCMT IO) UnificationResult
forall a b. (a -> b) -> a -> b
$ Telescope
-> FlexibleVars
-> Type
-> Args
-> Args
-> ExceptT SplitError (TCMT IO) UnificationResult
forall (m :: * -> *).
(PureTCM m, MonadBench m, BenchPhase m ~ Phase) =>
Telescope
-> FlexibleVars -> Type -> Args -> Args -> m UnificationResult
unifyIndices
         Telescope
delta1Gamma
         FlexibleVars
flex
         (Int -> Type -> Type
forall a. Subst a => Int -> a -> a
raise (Telescope -> Int
forall a. Sized a => a -> Int
size Telescope
gamma) Type
dtype)
         Args
conIxs
         Args
givenIxs

  let stuck :: Maybe Blocker -> [UnificationFailure] -> CoverM (Maybe SplitClause)
stuck Maybe Blocker
b [UnificationFailure]
errs = do
        ExceptT SplitError (TCMT IO) ()
debugCantSplit
        SplitError -> CoverM (Maybe SplitClause)
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (SplitError -> CoverM (Maybe SplitClause))
-> SplitError -> CoverM (Maybe SplitClause)
forall a b. (a -> b) -> a -> b
$ Maybe Blocker
-> QName
-> Telescope
-> Args
-> Args
-> [UnificationFailure]
-> SplitError
UnificationStuck Maybe Blocker
b (ConHead -> QName
conName ConHead
con) (Telescope
delta1 Telescope -> Telescope -> Telescope
forall t. Abstract t => Telescope -> t -> t
`abstract` Telescope
gamma) Args
conIxs Args
givenIxs [UnificationFailure]
errs


  case UnificationResult
r of
    NoUnify {} -> ExceptT SplitError (TCMT IO) ()
debugNoUnify ExceptT SplitError (TCMT IO) ()
-> Maybe SplitClause -> CoverM (Maybe SplitClause)
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Maybe SplitClause
forall a. Maybe a
Nothing

    UnifyBlocked Blocker
block -> Maybe Blocker -> [UnificationFailure] -> CoverM (Maybe SplitClause)
stuck (Blocker -> Maybe Blocker
forall a. a -> Maybe a
Just Blocker
block) []

    UnifyStuck [UnificationFailure]
errs -> Maybe Blocker -> [UnificationFailure] -> CoverM (Maybe SplitClause)
stuck Maybe Blocker
forall a. Maybe a
Nothing [UnificationFailure]
errs

    Unifies (Telescope
delta1',PatternSubstitution
rho0,NAPs
_) -> do
      [Char] -> PatternSubstitution -> ExceptT SplitError (TCMT IO) ()
forall {tcm :: * -> *} {a}.
(MonadTCM tcm, PrettyTCM a) =>
[Char] -> a -> tcm ()
debugSubst [Char]
"rho0" PatternSubstitution
rho0

      let rho0' :: Substitution' SplitPattern
rho0' = PatternSubstitution -> Substitution' SplitPattern
toSplitPSubst PatternSubstitution
rho0

      -- We have Δ₁' ⊢ ρ₀ : Δ₁Γ, so split it into the part for Δ₁ and the part for Γ
      let (Substitution' SplitPattern
rho1,Substitution' SplitPattern
rho2) = Int
-> Substitution' SplitPattern
-> (Substitution' SplitPattern, Substitution' SplitPattern)
forall a.
Int -> Substitution' a -> (Substitution' a, Substitution' a)
splitS (Telescope -> Int
forall a. Sized a => a -> Int
size Telescope
gamma) (Substitution' SplitPattern
 -> (Substitution' SplitPattern, Substitution' SplitPattern))
-> Substitution' SplitPattern
-> (Substitution' SplitPattern, Substitution' SplitPattern)
forall a b. (a -> b) -> a -> b
$ Substitution' SplitPattern
rho0'

      -- Andreas, 2015-05-01  I guess it is fine to use no @conPType@
      -- as the result of splitting is never used further down the pipeline.
      -- After splitting, Agda reloads the file.
      -- Andreas, 2017-09-03, issue #2729: remember that pattern was generated by case split.
      let cpi :: ConPatternInfo
cpi  = ConPatternInfo
noConPatternInfo{ conPInfo :: PatternInfo
conPInfo = PatOrigin -> [Name] -> PatternInfo
PatternInfo PatOrigin
PatOSplit [] , conPRecord :: Bool
conPRecord = Bool
True }
          conp :: SplitPattern
conp = ConHead
-> ConPatternInfo -> [NamedArg SplitPattern] -> SplitPattern
forall x.
ConHead -> ConPatternInfo -> [NamedArg (Pattern' x)] -> Pattern' x
ConP ConHead
con ConPatternInfo
cpi ([NamedArg SplitPattern] -> SplitPattern)
-> [NamedArg SplitPattern] -> SplitPattern
forall a b. (a -> b) -> a -> b
$ Substitution' (SubstArg [NamedArg SplitPattern])
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst Substitution' SplitPattern
Substitution' (SubstArg [NamedArg SplitPattern])
rho0' ([NamedArg SplitPattern] -> [NamedArg SplitPattern])
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a b. (a -> b) -> a -> b
$
                   (NamedArg SplitPattern -> NamedArg SplitPattern)
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a b. (a -> b) -> [a] -> [b]
map ((ArgInfo -> ArgInfo)
-> NamedArg SplitPattern -> NamedArg SplitPattern
forall a. LensArgInfo a => (ArgInfo -> ArgInfo) -> a -> a
mapArgInfo ArgInfo -> ArgInfo
hiddenInserted) ([NamedArg SplitPattern] -> [NamedArg SplitPattern])
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a b. (a -> b) -> a -> b
$ (forall a. DeBruijn a => Telescope -> [NamedArg a])
-> Telescope -> [(Term, (Term, Term))] -> [NamedArg SplitPattern]
forall a.
DeBruijn a =>
(forall a. DeBruijn a => Telescope -> [NamedArg a])
-> Telescope -> [(Term, (Term, Term))] -> [NamedArg (Pattern' a)]
telePatterns' (Telescope -> Telescope -> [NamedArg a1]
forall a. DeBruijn a => Telescope -> Telescope -> [NamedArg a]
tele2NamedArgs Telescope
gamma0) Telescope
gamma [(Term, (Term, Term))]
boundary
          -- Andreas, 2016-09-08, issue #2166: use gamma0 for correct argument names

      -- Compute final context and substitution
      let rho3 :: Substitution' SplitPattern
rho3    = SplitPattern
-> Substitution' SplitPattern -> Substitution' SplitPattern
forall a. DeBruijn a => a -> Substitution' a -> Substitution' a
consS SplitPattern
conp Substitution' SplitPattern
rho1            -- Δ₁' ⊢ ρ₃ : Δ₁(x:D)
          delta2' :: Telescope
delta2' = Substitution' SplitPattern -> Telescope -> Telescope
forall a. TermSubst a => Substitution' SplitPattern -> a -> a
applySplitPSubst Substitution' SplitPattern
rho3 Telescope
delta2  -- Δ₂' = Δ₂ρ₃
          delta' :: Telescope
delta'  = Telescope
delta1' Telescope -> Telescope -> Telescope
forall t. Abstract t => Telescope -> t -> t
`abstract` Telescope
delta2' -- Δ'  = Δ₁'Δ₂'
          rho :: Substitution' SplitPattern
rho     = Int -> Substitution' SplitPattern -> Substitution' SplitPattern
forall a. Int -> Substitution' a -> Substitution' a
liftS (Telescope -> Int
forall a. Sized a => a -> Int
size Telescope
delta2) Substitution' SplitPattern
rho3   -- Δ' ⊢ ρ : Δ₁(x:D)Δ₂

      [Char] -> Telescope -> ExceptT SplitError (TCMT IO) ()
forall {tcm :: * -> *} {a}.
(MonadTCM tcm, PrettyTCM a) =>
[Char] -> a -> tcm ()
debugTel [Char]
"delta'" Telescope
delta'
      [Char]
-> Substitution' SplitPattern -> ExceptT SplitError (TCMT IO) ()
forall {tcm :: * -> *} {a}.
(MonadTCM tcm, PrettyTCM a) =>
[Char] -> a -> tcm ()
debugSubst [Char]
"rho" Substitution' SplitPattern
rho
      Telescope
-> [NamedArg SplitPattern] -> ExceptT SplitError (TCMT IO) ()
forall {tcm :: * -> *} {b}.
(MonadTCM tcm, AddContext b) =>
b -> [NamedArg SplitPattern] -> tcm ()
debugPs Telescope
tel [NamedArg SplitPattern]
ps

      -- Apply the substitution
      let ps' :: [NamedArg SplitPattern]
ps' = Substitution' (SubstArg [NamedArg SplitPattern])
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst Substitution' SplitPattern
Substitution' (SubstArg [NamedArg SplitPattern])
rho [NamedArg SplitPattern]
ps
      Telescope
-> [NamedArg SplitPattern] -> ExceptT SplitError (TCMT IO) ()
forall {tcm :: * -> *} {b}.
(MonadTCM tcm, AddContext b) =>
b -> [NamedArg SplitPattern] -> tcm ()
debugPlugged Telescope
delta' [NamedArg SplitPattern]
ps'

      let cps' :: Map CheckpointId (Substitution' Term)
cps'  = Substitution' SplitPattern
-> Map CheckpointId (Substitution' Term)
-> Map CheckpointId (Substitution' Term)
forall a. TermSubst a => Substitution' SplitPattern -> a -> a
applySplitPSubst Substitution' SplitPattern
rho Map CheckpointId (Substitution' Term)
cps

      Maybe SplitClause -> CoverM (Maybe SplitClause)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe SplitClause -> CoverM (Maybe SplitClause))
-> Maybe SplitClause -> CoverM (Maybe SplitClause)
forall a b. (a -> b) -> a -> b
$ SplitClause -> Maybe SplitClause
forall a. a -> Maybe a
Just (SplitClause -> Maybe SplitClause)
-> SplitClause -> Maybe SplitClause
forall a b. (a -> b) -> a -> b
$ Telescope
-> [NamedArg SplitPattern]
-> Substitution' SplitPattern
-> Map CheckpointId (Substitution' Term)
-> Maybe (Dom Type)
-> SplitClause
SClause Telescope
delta' [NamedArg SplitPattern]
ps' Substitution' SplitPattern
rho Map CheckpointId (Substitution' Term)
cps' Maybe (Dom Type)
forall a. Maybe a
Nothing -- target fixed later

  where
    debugInit :: a
-> a
-> a
-> [a]
-> [a]
-> [a]
-> a
-> a
-> a
-> a
-> [NamedArg SplitPattern]
-> a
-> tcm ()
debugInit a
con a
ctype a
d [a]
pars [a]
ixs [a]
cixs a
delta1 a
delta2 a
gamma a
tel [NamedArg SplitPattern]
ps a
hix = TCMT IO () -> tcm ()
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCMT IO () -> tcm ()) -> TCMT IO () -> tcm ()
forall a b. (a -> b) -> a -> b
$ do
      [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.split.con" Int
20 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
        [ TCMT IO Doc
"computeNeighbourhood"
        , Int -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => Int -> m Doc -> m Doc
nest Int
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
          [ TCMT IO Doc
"context=" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> (TCMT IO Doc -> TCMT IO Doc
forall (tcm :: * -> *) a.
(MonadTCEnv tcm, ReadTCState tcm) =>
tcm a -> tcm a
inTopContext (TCMT IO Doc -> TCMT IO Doc)
-> (Telescope -> TCMT IO Doc) -> Telescope -> TCMT IO Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Telescope -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM (Telescope -> TCMT IO Doc) -> TCMT IO Telescope -> TCMT IO Doc
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< TCMT IO Telescope
forall (m :: * -> *). (Applicative m, MonadTCEnv m) => m Telescope
getContextTelescope)
          , TCMT IO Doc
"con    =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> a -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM a
con
          , TCMT IO Doc
"ctype  =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> a -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM a
ctype
          , TCMT IO Doc
"ps     =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> do TCMT IO Doc -> TCMT IO Doc
forall (tcm :: * -> *) a.
(MonadTCEnv tcm, ReadTCState tcm) =>
tcm a -> tcm a
inTopContext (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ a -> TCMT IO Doc -> TCMT IO Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext a
tel (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ NAPs -> TCMT IO Doc
forall (m :: * -> *). MonadPretty m => NAPs -> m Doc
prettyTCMPatternList (NAPs -> TCMT IO Doc) -> NAPs -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [NamedArg SplitPattern] -> NAPs
fromSplitPatterns [NamedArg SplitPattern]
ps
          , TCMT IO Doc
"d      =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> a -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM a
d
          , TCMT IO Doc
"pars   =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> do [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Semigroup (m Doc), Foldable t) =>
t (m Doc) -> m Doc
prettyList ([TCMT IO Doc] -> TCMT IO Doc) -> [TCMT IO Doc] -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ (a -> TCMT IO Doc) -> [a] -> [TCMT IO Doc]
forall a b. (a -> b) -> [a] -> [b]
map a -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM [a]
pars
          , TCMT IO Doc
"ixs    =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> do a -> TCMT IO Doc -> TCMT IO Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext a
delta1 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Semigroup (m Doc), Foldable t) =>
t (m Doc) -> m Doc
prettyList ([TCMT IO Doc] -> TCMT IO Doc) -> [TCMT IO Doc] -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ (a -> TCMT IO Doc) -> [a] -> [TCMT IO Doc]
forall a b. (a -> b) -> [a] -> [b]
map a -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM [a]
ixs
          , TCMT IO Doc
"cixs   =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> do a -> TCMT IO Doc -> TCMT IO Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext a
gamma  (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Semigroup (m Doc), Foldable t) =>
t (m Doc) -> m Doc
prettyList ([TCMT IO Doc] -> TCMT IO Doc) -> [TCMT IO Doc] -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ (a -> TCMT IO Doc) -> [a] -> [TCMT IO Doc]
forall a b. (a -> b) -> [a] -> [b]
map a -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM [a]
cixs
          , TCMT IO Doc
"delta1 =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> do TCMT IO Doc -> TCMT IO Doc
forall (tcm :: * -> *) a.
(MonadTCEnv tcm, ReadTCState tcm) =>
tcm a -> tcm a
inTopContext (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ a -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM a
delta1
          , TCMT IO Doc
"delta2 =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> do TCMT IO Doc -> TCMT IO Doc
forall (tcm :: * -> *) a.
(MonadTCEnv tcm, ReadTCState tcm) =>
tcm a -> tcm a
inTopContext (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ a -> TCMT IO Doc -> TCMT IO Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext a
delta1 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ a -> TCMT IO Doc -> TCMT IO Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext a
gamma (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ a -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM a
delta2
          , TCMT IO Doc
"gamma  =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> do TCMT IO Doc -> TCMT IO Doc
forall (tcm :: * -> *) a.
(MonadTCEnv tcm, ReadTCState tcm) =>
tcm a -> tcm a
inTopContext (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ a -> TCMT IO Doc -> TCMT IO Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext a
delta1 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ a -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM a
gamma
          , TCMT IO Doc
"tel  =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> do TCMT IO Doc -> TCMT IO Doc
forall (tcm :: * -> *) a.
(MonadTCEnv tcm, ReadTCState tcm) =>
tcm a -> tcm a
inTopContext (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ a -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM a
tel
          , TCMT IO Doc
"hix    =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text (a -> [Char]
forall a. Show a => a -> [Char]
show a
hix)
          ]
        ]
      [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.split.con" Int
70 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
        [ TCMT IO Doc
"computeNeighbourhood"
        , Int -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => Int -> m Doc -> m Doc
nest Int
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
          [ TCMT IO Doc
"context=" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> (TCMT IO Doc -> TCMT IO Doc
forall (tcm :: * -> *) a.
(MonadTCEnv tcm, ReadTCState tcm) =>
tcm a -> tcm a
inTopContext (TCMT IO Doc -> TCMT IO Doc)
-> (Telescope -> TCMT IO Doc) -> Telescope -> TCMT IO Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char] -> TCMT IO Doc)
-> (Telescope -> [Char]) -> Telescope -> TCMT IO Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Telescope -> [Char]
forall a. Show a => a -> [Char]
show) (Telescope -> TCMT IO Doc) -> TCMT IO Telescope -> TCMT IO Doc
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< TCMT IO Telescope
forall (m :: * -> *). (Applicative m, MonadTCEnv m) => m Telescope
getContextTelescope)
          , TCMT IO Doc
"con    =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> ([Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char] -> TCMT IO Doc) -> (a -> [Char]) -> a -> TCMT IO Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [Char]
forall a. Show a => a -> [Char]
show) a
con
          , TCMT IO Doc
"ctype  =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> ([Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char] -> TCMT IO Doc) -> (a -> [Char]) -> a -> TCMT IO Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [Char]
forall a. Show a => a -> [Char]
show) a
ctype
          , TCMT IO Doc
"ps     =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> ([Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char] -> TCMT IO Doc)
-> ([NamedArg SplitPattern] -> [Char])
-> [NamedArg SplitPattern]
-> TCMT IO Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [NamedArg SplitPattern] -> [Char]
forall a. Show a => a -> [Char]
show) [NamedArg SplitPattern]
ps
          , TCMT IO Doc
"d      =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> ([Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char] -> TCMT IO Doc) -> (a -> [Char]) -> a -> TCMT IO Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [Char]
forall a. Show a => a -> [Char]
show) a
d
          , TCMT IO Doc
"pars   =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> ([Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char] -> TCMT IO Doc) -> ([a] -> [Char]) -> [a] -> TCMT IO Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> [Char]
forall a. Show a => a -> [Char]
show) [a]
pars
          , TCMT IO Doc
"ixs    =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> ([Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char] -> TCMT IO Doc) -> ([a] -> [Char]) -> [a] -> TCMT IO Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> [Char]
forall a. Show a => a -> [Char]
show) [a]
ixs
          , TCMT IO Doc
"cixs   =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> ([Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char] -> TCMT IO Doc) -> ([a] -> [Char]) -> [a] -> TCMT IO Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> [Char]
forall a. Show a => a -> [Char]
show) [a]
cixs
          , TCMT IO Doc
"delta1 =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> ([Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char] -> TCMT IO Doc) -> (a -> [Char]) -> a -> TCMT IO Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [Char]
forall a. Show a => a -> [Char]
show) a
delta1
          , TCMT IO Doc
"delta2 =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> ([Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char] -> TCMT IO Doc) -> (a -> [Char]) -> a -> TCMT IO Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [Char]
forall a. Show a => a -> [Char]
show) a
delta2
          , TCMT IO Doc
"gamma  =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> ([Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char] -> TCMT IO Doc) -> (a -> [Char]) -> a -> TCMT IO Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [Char]
forall a. Show a => a -> [Char]
show) a
gamma
          , TCMT IO Doc
"hix    =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text (a -> [Char]
forall a. Show a => a -> [Char]
show a
hix)
          ]
        ]

    debugNoUnify :: ExceptT SplitError (TCMT IO) ()
debugNoUnify =
      TCMT IO () -> ExceptT SplitError (TCMT IO) ()
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCMT IO () -> ExceptT SplitError (TCMT IO) ())
-> TCMT IO () -> ExceptT SplitError (TCMT IO) ()
forall a b. (a -> b) -> a -> b
$ [Char] -> Int -> [Char] -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> [Char] -> m ()
reportSLn [Char]
"tc.cover.split.con" Int
20 [Char]
"  Constructor impossible!"

    debugCantSplit :: ExceptT SplitError (TCMT IO) ()
debugCantSplit =
      TCMT IO () -> ExceptT SplitError (TCMT IO) ()
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCMT IO () -> ExceptT SplitError (TCMT IO) ())
-> TCMT IO () -> ExceptT SplitError (TCMT IO) ()
forall a b. (a -> b) -> a -> b
$ [Char] -> Int -> [Char] -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> [Char] -> m ()
reportSLn [Char]
"tc.cover.split.con" Int
20 [Char]
"  Bad split!"

    debugSubst :: [Char] -> a -> tcm ()
debugSubst [Char]
s a
sub =
      TCMT IO () -> tcm ()
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCMT IO () -> tcm ()) -> TCMT IO () -> tcm ()
forall a b. (a -> b) -> a -> b
$ [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.split.con" Int
20 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ Int -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => Int -> m Doc -> m Doc
nest Int
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
        [ [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char]
s [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" =") TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> a -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM a
sub
        ]

    debugTel :: [Char] -> a -> tcm ()
debugTel [Char]
s a
tel =
      TCMT IO () -> tcm ()
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCMT IO () -> tcm ()) -> TCMT IO () -> tcm ()
forall a b. (a -> b) -> a -> b
$ [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.split.con" Int
20 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ Int -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => Int -> m Doc -> m Doc
nest Int
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
        [ [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char]
s [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" =") TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> a -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM a
tel
        ]

    debugPs :: b -> [NamedArg SplitPattern] -> tcm ()
debugPs b
tel [NamedArg SplitPattern]
ps =
      TCMT IO () -> tcm ()
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCMT IO () -> tcm ()) -> TCMT IO () -> tcm ()
forall a b. (a -> b) -> a -> b
$ [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.split.con" Int
20 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$
        TCMT IO Doc -> TCMT IO Doc
forall (tcm :: * -> *) a.
(MonadTCEnv tcm, ReadTCState tcm) =>
tcm a -> tcm a
inTopContext (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ b -> TCMT IO Doc -> TCMT IO Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext b
tel (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ Int -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => Int -> m Doc -> m Doc
nest Int
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
          [ TCMT IO Doc
"ps     =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> NAPs -> TCMT IO Doc
forall (m :: * -> *). MonadPretty m => NAPs -> m Doc
prettyTCMPatternList ([NamedArg SplitPattern] -> NAPs
fromSplitPatterns [NamedArg SplitPattern]
ps)
          ]

    debugPlugged :: b -> [NamedArg SplitPattern] -> tcm ()
debugPlugged b
delta' [NamedArg SplitPattern]
ps' = do
      TCMT IO () -> tcm ()
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCMT IO () -> tcm ()) -> TCMT IO () -> tcm ()
forall a b. (a -> b) -> a -> b
$ [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.split.con" Int
20 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$
        TCMT IO Doc -> TCMT IO Doc
forall (tcm :: * -> *) a.
(MonadTCEnv tcm, ReadTCState tcm) =>
tcm a -> tcm a
inTopContext (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ b -> TCMT IO Doc -> TCMT IO Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext b
delta' (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ Int -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => Int -> m Doc -> m Doc
nest Int
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
          [ TCMT IO Doc
"ps'    =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> do NAPs -> TCMT IO Doc
forall (m :: * -> *). MonadPretty m => NAPs -> m Doc
prettyTCMPatternList (NAPs -> TCMT IO Doc) -> NAPs -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [NamedArg SplitPattern] -> NAPs
fromSplitPatterns [NamedArg SplitPattern]
ps'
          ]

-- | Introduce trailing pattern variables?
data InsertTrailing
  = DoInsertTrailing
  | DontInsertTrailing
  deriving (InsertTrailing -> InsertTrailing -> Bool
(InsertTrailing -> InsertTrailing -> Bool)
-> (InsertTrailing -> InsertTrailing -> Bool) -> Eq InsertTrailing
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: InsertTrailing -> InsertTrailing -> Bool
$c/= :: InsertTrailing -> InsertTrailing -> Bool
== :: InsertTrailing -> InsertTrailing -> Bool
$c== :: InsertTrailing -> InsertTrailing -> Bool
Eq, Int -> InsertTrailing -> [Char] -> [Char]
[InsertTrailing] -> [Char] -> [Char]
InsertTrailing -> [Char]
(Int -> InsertTrailing -> [Char] -> [Char])
-> (InsertTrailing -> [Char])
-> ([InsertTrailing] -> [Char] -> [Char])
-> Show InsertTrailing
forall a.
(Int -> a -> [Char] -> [Char])
-> (a -> [Char]) -> ([a] -> [Char] -> [Char]) -> Show a
showList :: [InsertTrailing] -> [Char] -> [Char]
$cshowList :: [InsertTrailing] -> [Char] -> [Char]
show :: InsertTrailing -> [Char]
$cshow :: InsertTrailing -> [Char]
showsPrec :: Int -> InsertTrailing -> [Char] -> [Char]
$cshowsPrec :: Int -> InsertTrailing -> [Char] -> [Char]
Show)

-- | Allow partial covering for split?
data AllowPartialCover
  = YesAllowPartialCover  -- To try to coverage-check incomplete splits.
  | NoAllowPartialCover   -- Default.
  deriving (AllowPartialCover -> AllowPartialCover -> Bool
(AllowPartialCover -> AllowPartialCover -> Bool)
-> (AllowPartialCover -> AllowPartialCover -> Bool)
-> Eq AllowPartialCover
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AllowPartialCover -> AllowPartialCover -> Bool
$c/= :: AllowPartialCover -> AllowPartialCover -> Bool
== :: AllowPartialCover -> AllowPartialCover -> Bool
$c== :: AllowPartialCover -> AllowPartialCover -> Bool
Eq, Int -> AllowPartialCover -> [Char] -> [Char]
[AllowPartialCover] -> [Char] -> [Char]
AllowPartialCover -> [Char]
(Int -> AllowPartialCover -> [Char] -> [Char])
-> (AllowPartialCover -> [Char])
-> ([AllowPartialCover] -> [Char] -> [Char])
-> Show AllowPartialCover
forall a.
(Int -> a -> [Char] -> [Char])
-> (a -> [Char]) -> ([a] -> [Char] -> [Char]) -> Show a
showList :: [AllowPartialCover] -> [Char] -> [Char]
$cshowList :: [AllowPartialCover] -> [Char] -> [Char]
show :: AllowPartialCover -> [Char]
$cshow :: AllowPartialCover -> [Char]
showsPrec :: Int -> AllowPartialCover -> [Char] -> [Char]
$cshowsPrec :: Int -> AllowPartialCover -> [Char] -> [Char]
Show)

-- | Entry point from @Interaction.MakeCase@.
splitClauseWithAbsurd :: SplitClause -> Nat -> TCM (Either SplitError (Either SplitClause Covering))
splitClauseWithAbsurd :: SplitClause
-> Int -> TCM (Either SplitError (Either SplitClause Covering))
splitClauseWithAbsurd SplitClause
c Int
x =
  CheckEmpty
-> Induction
-> AllowPartialCover
-> InsertTrailing
-> SplitClause
-> BlockingVar
-> TCM (Either SplitError (Either SplitClause Covering))
split' CheckEmpty
CheckEmpty Induction
Inductive AllowPartialCover
NoAllowPartialCover InsertTrailing
DontInsertTrailing SplitClause
c (Int -> [ConHead] -> [Literal] -> Bool -> Bool -> BlockingVar
BlockingVar Int
x [] [] Bool
True Bool
False)
  -- Andreas, 2016-05-03, issue 1950:
  -- Do not introduce trailing pattern vars after split,
  -- because this does not work for with-clauses.

-- | Entry point from @TypeChecking.Empty@ and @Interaction.BasicOps@.
--   @splitLast CoInductive@ is used in the @refine@ tactics.

splitLast :: Induction -> Telescope -> [NamedArg DeBruijnPattern] -> TCM (Either SplitError Covering)
splitLast :: Induction
-> Telescope -> NAPs -> TCMT IO (Either SplitError Covering)
splitLast Induction
ind Telescope
tel NAPs
ps = Induction
-> AllowPartialCover
-> SplitClause
-> BlockingVar
-> TCMT IO (Either SplitError Covering)
split Induction
ind AllowPartialCover
NoAllowPartialCover SplitClause
sc (Int -> [ConHead] -> [Literal] -> Bool -> Bool -> BlockingVar
BlockingVar Int
0 [] [] Bool
True Bool
False)
  where sc :: SplitClause
sc = Telescope
-> [NamedArg SplitPattern]
-> Substitution' SplitPattern
-> Map CheckpointId (Substitution' Term)
-> Maybe (Dom Type)
-> SplitClause
SClause Telescope
tel (NAPs -> [NamedArg SplitPattern]
toSplitPatterns NAPs
ps) Substitution' SplitPattern
forall a. Null a => a
empty Map CheckpointId (Substitution' Term)
forall a. Null a => a
empty Maybe (Dom Type)
forall {t}. Maybe (Dom (Type'' t Term))
target
        -- TODO 2ltt: allows (Empty_fib -> Empty_strict) which is not conservative
        target :: Maybe (Dom (Type'' t Term))
target = (Dom (Type'' t Term) -> Maybe (Dom (Type'' t Term))
forall a. a -> Maybe a
Just (Dom (Type'' t Term) -> Maybe (Dom (Type'' t Term)))
-> Dom (Type'' t Term) -> Maybe (Dom (Type'' t Term))
forall a b. (a -> b) -> a -> b
$ Type'' t Term -> Dom (Type'' t Term)
forall a. a -> Dom a
defaultDom (Type'' t Term -> Dom (Type'' t Term))
-> Type'' t Term -> Dom (Type'' t Term)
forall a b. (a -> b) -> a -> b
$ Sort' t -> Term -> Type'' t Term
forall t a. Sort' t -> a -> Type'' t a
El (Level' t -> Sort' t
forall t. Level' t -> Sort' t
Prop (Integer -> [PlusLevel' t] -> Level' t
forall t. Integer -> [PlusLevel' t] -> Level' t
Max Integer
0 [])) (Term -> Type'' t Term) -> Term -> Type'' t Term
forall a b. (a -> b) -> a -> b
$ [Char] -> [Elim] -> Term
Dummy [Char]
"splitLastTarget" [])

-- | @split ind splitClause x = return res@
--   splits @splitClause@ at pattern var @x@ (de Bruijn index).
--
--   Possible results @res@ are:
--
--   1. @Left err@:
--      Splitting failed.
--
--   2. @Right covering@:
--      A covering set of split clauses, one for each valid constructor.
--      This could be the empty set (denoting an absurd clause).

split :: Induction
         -- ^ Coinductive constructors are allowed if this argument is
         -- 'CoInductive'.
      -> AllowPartialCover
         -- ^ Don't fail if computed 'Covering' does not cover all constructors.
      -> SplitClause
      -> BlockingVar
      -> TCM (Either SplitError Covering)
split :: Induction
-> AllowPartialCover
-> SplitClause
-> BlockingVar
-> TCMT IO (Either SplitError Covering)
split Induction
ind AllowPartialCover
allowPartialCover SplitClause
sc BlockingVar
x =
  (Either SplitClause Covering -> Covering)
-> Either SplitError (Either SplitClause Covering)
-> Either SplitError Covering
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Either SplitClause Covering -> Covering
blendInAbsurdClause (Either SplitError (Either SplitClause Covering)
 -> Either SplitError Covering)
-> TCM (Either SplitError (Either SplitClause Covering))
-> TCMT IO (Either SplitError Covering)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CheckEmpty
-> Induction
-> AllowPartialCover
-> InsertTrailing
-> SplitClause
-> BlockingVar
-> TCM (Either SplitError (Either SplitClause Covering))
split' CheckEmpty
NoCheckEmpty Induction
ind AllowPartialCover
allowPartialCover InsertTrailing
DoInsertTrailing SplitClause
sc BlockingVar
x
  where
    n :: Arg Int
n = SplitClause -> Int -> Arg Int
lookupPatternVar SplitClause
sc (Int -> Arg Int) -> Int -> Arg Int
forall a b. (a -> b) -> a -> b
$ BlockingVar -> Int
blockingVarNo BlockingVar
x
    blendInAbsurdClause :: Either SplitClause Covering -> Covering
    blendInAbsurdClause :: Either SplitClause Covering -> Covering
blendInAbsurdClause = (SplitClause -> Covering)
-> Either SplitClause Covering -> Covering
forall a b. (a -> b) -> Either a b -> b
fromRight (Covering -> SplitClause -> Covering
forall a b. a -> b -> a
const (Covering -> SplitClause -> Covering)
-> Covering -> SplitClause -> Covering
forall a b. (a -> b) -> a -> b
$ Arg Int -> [(SplitTag, SplitClause)] -> Covering
Covering Arg Int
n [])

-- | Convert a de Bruijn index relative to the clause telescope to a de Bruijn
--   level. The result should be the argument position (counted from left,
--   starting with 0) to split at (dot patterns included!).
lookupPatternVar :: SplitClause -> Int -> Arg Nat
lookupPatternVar :: SplitClause -> Int -> Arg Int
lookupPatternVar SClause{ scTel :: SplitClause -> Telescope
scTel = Telescope
tel, scPats :: SplitClause -> [NamedArg SplitPattern]
scPats = [NamedArg SplitPattern]
pats } Int
x = Arg DeBruijnPattern
arg Arg DeBruijnPattern -> Int -> Arg Int
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$>
    if Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 then Int
forall a. HasCallStack => a
__IMPOSSIBLE__ else Int
n
  where n :: Int
n = if Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0
            then Int
forall a. HasCallStack => a
__IMPOSSIBLE__
            else Int -> Maybe Int -> Int
forall a. a -> Maybe a -> a
fromMaybe Int
forall a. HasCallStack => a
__IMPOSSIBLE__ (Maybe Int -> Int) -> Maybe Int -> Int
forall a b. (a -> b) -> a -> b
$ Permutation -> [Int]
permPicks Permutation
perm [Int] -> Int -> Maybe Int
forall a. [a] -> Int -> Maybe a
!!! Int
k
        perm :: Permutation
perm = Permutation -> Maybe Permutation -> Permutation
forall a. a -> Maybe a -> a
fromMaybe Permutation
forall a. HasCallStack => a
__IMPOSSIBLE__ (Maybe Permutation -> Permutation)
-> Maybe Permutation -> Permutation
forall a b. (a -> b) -> a -> b
$ NAPs -> Maybe Permutation
dbPatPerm (NAPs -> Maybe Permutation) -> NAPs -> Maybe Permutation
forall a b. (a -> b) -> a -> b
$ [NamedArg SplitPattern] -> NAPs
fromSplitPatterns [NamedArg SplitPattern]
pats
        k :: Int
k = Telescope -> Int
forall a. Sized a => a -> Int
size Telescope
tel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
        arg :: Arg DeBruijnPattern
arg = Arg DeBruijnPattern
-> [Arg DeBruijnPattern] -> Int -> Arg DeBruijnPattern
forall a. a -> [a] -> Int -> a
indexWithDefault Arg DeBruijnPattern
forall a. HasCallStack => a
__IMPOSSIBLE__ (Int -> Telescope -> [Arg DeBruijnPattern]
telVars (Telescope -> Int
forall a. Sized a => a -> Int
size Telescope
tel) Telescope
tel) Int
k


data CheckEmpty = CheckEmpty | NoCheckEmpty

-- | @split' ind pc ft splitClause x = return res@
--   splits @splitClause@ at pattern var @x@ (de Bruijn index).
--
--   Possible results @res@ are:
--
--   1. @Left err@:
--      Splitting failed.
--
--   2. @Right (Left splitClause')@:
--      Absurd clause (type of @x@ has 0 valid constructors).
--
--   3. @Right (Right covering)@:
--      A covering set of split clauses, one for each valid constructor.

split' :: CheckEmpty
          -- ^ Use isEmptyType to check whether the type of the variable to
          -- split on is empty. This switch is necessary to break the cycle
          -- between split' and isEmptyType.
       -> Induction
          -- ^ Coinductive constructors are allowed if this argument is
          -- 'CoInductive'.
       -> AllowPartialCover
          -- ^ Don't fail if computed 'Covering' does not cover all constructors.
       -> InsertTrailing
          -- ^ If 'DoInsertTrailing', introduce new trailing variable patterns.
       -> SplitClause
       -> BlockingVar
       -> TCM (Either SplitError (Either SplitClause Covering))
split' :: CheckEmpty
-> Induction
-> AllowPartialCover
-> InsertTrailing
-> SplitClause
-> BlockingVar
-> TCM (Either SplitError (Either SplitClause Covering))
split' CheckEmpty
checkEmpty Induction
ind AllowPartialCover
allowPartialCover InsertTrailing
inserttrailing
       sc :: SplitClause
sc@(SClause Telescope
tel [NamedArg SplitPattern]
ps Substitution' SplitPattern
_ Map CheckpointId (Substitution' Term)
cps Maybe (Dom Type)
target) (BlockingVar Int
x [ConHead]
pcons' [Literal]
plits Bool
overlap Bool
lazy) =
 TCM (Either SplitError (Either SplitClause Covering))
-> TCM (Either SplitError (Either SplitClause Covering))
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCM (Either SplitError (Either SplitClause Covering))
 -> TCM (Either SplitError (Either SplitClause Covering)))
-> TCM (Either SplitError (Either SplitClause Covering))
-> TCM (Either SplitError (Either SplitClause Covering))
forall a b. (a -> b) -> a -> b
$ ExceptT SplitError (TCMT IO) (Either SplitClause Covering)
-> TCM (Either SplitError (Either SplitClause Covering))
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT SplitError (TCMT IO) (Either SplitClause Covering)
 -> TCM (Either SplitError (Either SplitClause Covering)))
-> ExceptT SplitError (TCMT IO) (Either SplitClause Covering)
-> TCM (Either SplitError (Either SplitClause Covering))
forall a b. (a -> b) -> a -> b
$ do
  Telescope
-> Int
-> [NamedArg SplitPattern]
-> Map CheckpointId (Substitution' Term)
-> ExceptT SplitError (TCMT IO) ()
forall {tcm :: * -> *} {a} {a} {a}.
(MonadTCM tcm, AddContext a, PrettyTCM a, PrettyTCM a, PrettyTCM a,
 Show a, Show a, Show a) =>
a -> a -> [NamedArg SplitPattern] -> a -> tcm ()
debugInit Telescope
tel Int
x [NamedArg SplitPattern]
ps Map CheckpointId (Substitution' Term)
cps

  -- Split the telescope at the variable
  -- t = type of the variable,  Δ₁ ⊢ t
  ([Char]
n, Dom Type
t, Telescope
delta1, Telescope
delta2) <- do
    let (ListTel
tel1, Dom' Term ([Char], Type)
dom : ListTel
tel2) = Int -> ListTel -> (ListTel, ListTel)
forall a. Int -> [a] -> ([a], [a])
splitAt (Telescope -> Int
forall a. Sized a => a -> Int
size Telescope
tel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (ListTel -> (ListTel, ListTel)) -> ListTel -> (ListTel, ListTel)
forall a b. (a -> b) -> a -> b
$ Telescope -> ListTel
forall t. Tele (Dom t) -> [Dom ([Char], t)]
telToList Telescope
tel
    ([Char], Dom Type, Telescope, Telescope)
-> ExceptT
     SplitError (TCMT IO) ([Char], Dom Type, Telescope, Telescope)
forall (m :: * -> *) a. Monad m => a -> m a
return (([Char], Type) -> [Char]
forall a b. (a, b) -> a
fst (([Char], Type) -> [Char]) -> ([Char], Type) -> [Char]
forall a b. (a -> b) -> a -> b
$ Dom' Term ([Char], Type) -> ([Char], Type)
forall t e. Dom' t e -> e
unDom Dom' Term ([Char], Type)
dom, ([Char], Type) -> Type
forall a b. (a, b) -> b
snd (([Char], Type) -> Type) -> Dom' Term ([Char], Type) -> Dom Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Dom' Term ([Char], Type)
dom, ListTel -> Telescope
telFromList ListTel
tel1, ListTel -> Telescope
telFromList ListTel
tel2)

  -- Compute the neighbourhoods for the constructors
  let computeNeighborhoods :: ExceptT
  SplitError
  (TCMT IO)
  (DataOrRecord, Bool, [(SplitTag, SplitClause)])
computeNeighborhoods = do
        -- Check that t is a datatype or a record
        -- Andreas, 2010-09-21, isDatatype now directly throws an exception if it fails
        -- cons = constructors of this datatype
        (DataOrRecord
dr, QName
d, Args
pars, Args
ixs, [QName]
cons', Bool
isHIT) <- ExceptT
  SplitError
  (TCMT IO)
  (DataOrRecord, QName, Args, Args, [QName], Bool)
-> ExceptT
     SplitError
     (TCMT IO)
     (DataOrRecord, QName, Args, Args, [QName], Bool)
forall (tcm :: * -> *) a.
(MonadTCM tcm, MonadAddContext tcm, MonadDebug tcm) =>
tcm a -> tcm a
inContextOfT (ExceptT
   SplitError
   (TCMT IO)
   (DataOrRecord, QName, Args, Args, [QName], Bool)
 -> ExceptT
      SplitError
      (TCMT IO)
      (DataOrRecord, QName, Args, Args, [QName], Bool))
-> ExceptT
     SplitError
     (TCMT IO)
     (DataOrRecord, QName, Args, Args, [QName], Bool)
-> ExceptT
     SplitError
     (TCMT IO)
     (DataOrRecord, QName, Args, Args, [QName], Bool)
forall a b. (a -> b) -> a -> b
$ Induction
-> Dom Type
-> ExceptT
     SplitError
     (TCMT IO)
     (DataOrRecord, QName, Args, Args, [QName], Bool)
forall (tcm :: * -> *).
(MonadTCM tcm, MonadError SplitError tcm) =>
Induction
-> Dom Type -> tcm (DataOrRecord, QName, Args, Args, [QName], Bool)
isDatatype Induction
ind Dom Type
t
        [QName]
cons <- case CheckEmpty
checkEmpty of
          CheckEmpty
CheckEmpty   -> ExceptT SplitError (TCMT IO) Bool
-> ExceptT SplitError (TCMT IO) [QName]
-> ExceptT SplitError (TCMT IO) [QName]
-> ExceptT SplitError (TCMT IO) [QName]
forall (m :: * -> *) a. Monad m => m Bool -> m a -> m a -> m a
ifM (TCMT IO Bool -> ExceptT SplitError (TCMT IO) Bool
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCMT IO Bool -> ExceptT SplitError (TCMT IO) Bool)
-> TCMT IO Bool -> ExceptT SplitError (TCMT IO) Bool
forall a b. (a -> b) -> a -> b
$ TCMT IO Bool -> TCMT IO Bool
forall (tcm :: * -> *) a.
(MonadTCM tcm, MonadAddContext tcm, MonadDebug tcm) =>
tcm a -> tcm a
inContextOfT (TCMT IO Bool -> TCMT IO Bool) -> TCMT IO Bool -> TCMT IO Bool
forall a b. (a -> b) -> a -> b
$ Type -> TCMT IO Bool
isEmptyType (Type -> TCMT IO Bool) -> Type -> TCMT IO Bool
forall a b. (a -> b) -> a -> b
$ Dom Type -> Type
forall t e. Dom' t e -> e
unDom Dom Type
t) ([QName] -> ExceptT SplitError (TCMT IO) [QName]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []) ([QName] -> ExceptT SplitError (TCMT IO) [QName]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [QName]
cons')
          CheckEmpty
NoCheckEmpty -> [QName] -> ExceptT SplitError (TCMT IO) [QName]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [QName]
cons'
        [Maybe (SplitTag, SplitClause)]
mns  <- [QName]
-> (QName -> CoverM (Maybe (SplitTag, SplitClause)))
-> ExceptT SplitError (TCMT IO) [Maybe (SplitTag, SplitClause)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [QName]
cons ((QName -> CoverM (Maybe (SplitTag, SplitClause)))
 -> ExceptT SplitError (TCMT IO) [Maybe (SplitTag, SplitClause)])
-> (QName -> CoverM (Maybe (SplitTag, SplitClause)))
-> ExceptT SplitError (TCMT IO) [Maybe (SplitTag, SplitClause)]
forall a b. (a -> b) -> a -> b
$ \ QName
con -> (SplitClause -> (SplitTag, SplitClause))
-> Maybe SplitClause -> Maybe (SplitTag, SplitClause)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (QName -> SplitTag
SplitCon QName
con,) (Maybe SplitClause -> Maybe (SplitTag, SplitClause))
-> CoverM (Maybe SplitClause)
-> CoverM (Maybe (SplitTag, SplitClause))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
          Telescope
-> [Char]
-> Telescope
-> QName
-> Args
-> Args
-> Int
-> Telescope
-> [NamedArg SplitPattern]
-> Map CheckpointId (Substitution' Term)
-> QName
-> CoverM (Maybe SplitClause)
computeNeighbourhood Telescope
delta1 [Char]
n Telescope
delta2 QName
d Args
pars Args
ixs Int
x Telescope
tel [NamedArg SplitPattern]
ps Map CheckpointId (Substitution' Term)
cps QName
con
        Maybe (SplitTag, SplitClause)
hcompsc <- if Bool
isHIT Bool -> Bool -> Bool
&& InsertTrailing
inserttrailing InsertTrailing -> InsertTrailing -> Bool
forall a. Eq a => a -> a -> Bool
== InsertTrailing
DoInsertTrailing
                   then Telescope
-> [Char]
-> Telescope
-> QName
-> Args
-> Args
-> Int
-> Telescope
-> [NamedArg SplitPattern]
-> Map CheckpointId (Substitution' Term)
-> CoverM (Maybe (SplitTag, SplitClause))
computeHCompSplit Telescope
delta1 [Char]
n Telescope
delta2 QName
d Args
pars Args
ixs Int
x Telescope
tel [NamedArg SplitPattern]
ps Map CheckpointId (Substitution' Term)
cps
                   else Maybe (SplitTag, SplitClause)
-> CoverM (Maybe (SplitTag, SplitClause))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (SplitTag, SplitClause)
forall a. Maybe a
Nothing
        (DataOrRecord, Bool, [(SplitTag, SplitClause)])
-> ExceptT
     SplitError
     (TCMT IO)
     (DataOrRecord, Bool, [(SplitTag, SplitClause)])
forall (m :: * -> *) a. Monad m => a -> m a
return ( DataOrRecord
dr
               , Bool -> Bool
not (Args -> Bool
forall a. Null a => a -> Bool
null Args
ixs) -- Is "d" indexed?
               , [Maybe (SplitTag, SplitClause)] -> [(SplitTag, SplitClause)]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe (SplitTag, SplitClause)]
mns [Maybe (SplitTag, SplitClause)]
-> [Maybe (SplitTag, SplitClause)]
-> [Maybe (SplitTag, SplitClause)]
forall a. [a] -> [a] -> [a]
++ [Maybe (SplitTag, SplitClause)
hcompsc])
               )

      computeLitNeighborhoods :: ExceptT
  SplitError
  (TCMT IO)
  (DataOrRecord, Bool, [(SplitTag, SplitClause)])
computeLitNeighborhoods = do
        Bool
typeOk <- TCMT IO Bool -> ExceptT SplitError (TCMT IO) Bool
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCMT IO Bool -> ExceptT SplitError (TCMT IO) Bool)
-> TCMT IO Bool -> ExceptT SplitError (TCMT IO) Bool
forall a b. (a -> b) -> a -> b
$ do
          Type
t' <- Literal -> TCMT IO Type
forall (m :: * -> *).
(HasBuiltins m, MonadError TCErr m, MonadTCEnv m, ReadTCState m) =>
Literal -> m Type
litType (Literal -> TCMT IO Type) -> Literal -> TCMT IO Type
forall a b. (a -> b) -> a -> b
$ Literal -> [Literal] -> Literal
forall a. a -> [a] -> a
headWithDefault {-'-} Literal
forall a. HasCallStack => a
__IMPOSSIBLE__ [Literal]
plits
          TCMT IO Bool -> TCMT IO Bool
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCMT IO Bool -> TCMT IO Bool) -> TCMT IO Bool -> TCMT IO Bool
forall a b. (a -> b) -> a -> b
$ TCMT IO Bool -> TCMT IO Bool
forall (m :: * -> *) a.
(MonadTCEnv m, HasOptions m, MonadDebug m) =>
m a -> m a
dontAssignMetas (TCMT IO Bool -> TCMT IO Bool) -> TCMT IO Bool -> TCMT IO Bool
forall a b. (a -> b) -> a -> b
$ TCMT IO () -> TCMT IO Bool
forall (m :: * -> *).
(MonadConstraint m, MonadWarning m, MonadError TCErr m,
 MonadFresh ProblemId m) =>
m () -> m Bool
tryConversion (TCMT IO () -> TCMT IO Bool) -> TCMT IO () -> TCMT IO Bool
forall a b. (a -> b) -> a -> b
$ Type -> Type -> TCMT IO ()
forall (m :: * -> *). MonadConversion m => Type -> Type -> m ()
equalType (Dom Type -> Type
forall t e. Dom' t e -> e
unDom Dom Type
t) Type
t'
        Bool
-> ExceptT SplitError (TCMT IO) ()
-> ExceptT SplitError (TCMT IO) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
typeOk (ExceptT SplitError (TCMT IO) ()
 -> ExceptT SplitError (TCMT IO) ())
-> ExceptT SplitError (TCMT IO) ()
-> ExceptT SplitError (TCMT IO) ()
forall a b. (a -> b) -> a -> b
$ SplitError -> ExceptT SplitError (TCMT IO) ()
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (SplitError -> ExceptT SplitError (TCMT IO) ())
-> (Closure Type -> SplitError)
-> Closure Type
-> ExceptT SplitError (TCMT IO) ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Closure Type -> SplitError
NotADatatype (Closure Type -> ExceptT SplitError (TCMT IO) ())
-> ExceptT SplitError (TCMT IO) (Closure Type)
-> ExceptT SplitError (TCMT IO) ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< do TCM (Closure Type) -> ExceptT SplitError (TCMT IO) (Closure Type)
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCM (Closure Type) -> ExceptT SplitError (TCMT IO) (Closure Type))
-> TCM (Closure Type)
-> ExceptT SplitError (TCMT IO) (Closure Type)
forall a b. (a -> b) -> a -> b
$ Type -> TCM (Closure Type)
forall (m :: * -> *) a.
(MonadTCEnv m, ReadTCState m) =>
a -> m (Closure a)
buildClosure (Dom Type -> Type
forall t e. Dom' t e -> e
unDom Dom Type
t)
        [(SplitTag, SplitClause)]
ns <- [Literal]
-> (Literal
    -> ExceptT SplitError (TCMT IO) (SplitTag, SplitClause))
-> ExceptT SplitError (TCMT IO) [(SplitTag, SplitClause)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [Literal]
plits ((Literal -> ExceptT SplitError (TCMT IO) (SplitTag, SplitClause))
 -> ExceptT SplitError (TCMT IO) [(SplitTag, SplitClause)])
-> (Literal
    -> ExceptT SplitError (TCMT IO) (SplitTag, SplitClause))
-> ExceptT SplitError (TCMT IO) [(SplitTag, SplitClause)]
forall a b. (a -> b) -> a -> b
$ \Literal
lit -> do
          let delta2' :: Telescope
delta2' = Int -> SubstArg Telescope -> Telescope -> Telescope
forall a. Subst a => Int -> SubstArg a -> a -> a
subst Int
0 (Literal -> Term
Lit Literal
lit) Telescope
delta2
              delta' :: Telescope
delta'  = Telescope
delta1 Telescope -> Telescope -> Telescope
forall t. Abstract t => Telescope -> t -> t
`abstract` Telescope
delta2'
              rho :: Substitution' SplitPattern
rho     = Int -> Substitution' SplitPattern -> Substitution' SplitPattern
forall a. Int -> Substitution' a -> Substitution' a
liftS Int
x (Substitution' SplitPattern -> Substitution' SplitPattern)
-> Substitution' SplitPattern -> Substitution' SplitPattern
forall a b. (a -> b) -> a -> b
$ SplitPattern
-> Substitution' SplitPattern -> Substitution' SplitPattern
forall a. DeBruijn a => a -> Substitution' a -> Substitution' a
consS (Literal -> SplitPattern
forall a. Literal -> Pattern' a
litP Literal
lit) Substitution' SplitPattern
forall a. Substitution' a
idS
              ps' :: [NamedArg SplitPattern]
ps'     = Substitution' (SubstArg [NamedArg SplitPattern])
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst Substitution' SplitPattern
Substitution' (SubstArg [NamedArg SplitPattern])
rho [NamedArg SplitPattern]
ps
              cps' :: Map CheckpointId (Substitution' Term)
cps'    = Substitution' SplitPattern
-> Map CheckpointId (Substitution' Term)
-> Map CheckpointId (Substitution' Term)
forall a. TermSubst a => Substitution' SplitPattern -> a -> a
applySplitPSubst Substitution' SplitPattern
rho Map CheckpointId (Substitution' Term)
cps
          (SplitTag, SplitClause)
-> ExceptT SplitError (TCMT IO) (SplitTag, SplitClause)
forall (m :: * -> *) a. Monad m => a -> m a
return (Literal -> SplitTag
SplitLit Literal
lit , Telescope
-> [NamedArg SplitPattern]
-> Substitution' SplitPattern
-> Map CheckpointId (Substitution' Term)
-> Maybe (Dom Type)
-> SplitClause
SClause Telescope
delta' [NamedArg SplitPattern]
ps' Substitution' SplitPattern
rho Map CheckpointId (Substitution' Term)
cps' Maybe (Dom Type)
forall a. Maybe a
Nothing)
        (SplitTag, SplitClause)
ca <- do
          let delta' :: Telescope
delta' = Telescope
tel -- telescope is unchanged for catchall branch
              varp :: SplitPattern
varp   = PatternInfo -> SplitPatVar -> SplitPattern
forall x. PatternInfo -> x -> Pattern' x
VarP (PatOrigin -> [Name] -> PatternInfo
PatternInfo PatOrigin
PatOSplit []) (SplitPatVar -> SplitPattern) -> SplitPatVar -> SplitPattern
forall a b. (a -> b) -> a -> b
$ SplitPatVar
                         { splitPatVarName :: [Char]
splitPatVarName   = [Char]
forall a. Underscore a => a
underscore
                         , splitPatVarIndex :: Int
splitPatVarIndex  = Int
0
                         , splitExcludedLits :: [Literal]
splitExcludedLits = [Literal]
plits
                         }
              rho :: Substitution' SplitPattern
rho    = Int -> Substitution' SplitPattern -> Substitution' SplitPattern
forall a. Int -> Substitution' a -> Substitution' a
liftS Int
x (Substitution' SplitPattern -> Substitution' SplitPattern)
-> Substitution' SplitPattern -> Substitution' SplitPattern
forall a b. (a -> b) -> a -> b
$ SplitPattern
-> Substitution' SplitPattern -> Substitution' SplitPattern
forall a. DeBruijn a => a -> Substitution' a -> Substitution' a
consS SplitPattern
varp (Substitution' SplitPattern -> Substitution' SplitPattern)
-> Substitution' SplitPattern -> Substitution' SplitPattern
forall a b. (a -> b) -> a -> b
$ Int -> Substitution' SplitPattern
forall a. Int -> Substitution' a
raiseS Int
1
              ps' :: [NamedArg SplitPattern]
ps'    = Substitution' (SubstArg [NamedArg SplitPattern])
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst Substitution' SplitPattern
Substitution' (SubstArg [NamedArg SplitPattern])
rho [NamedArg SplitPattern]
ps
          (SplitTag, SplitClause)
-> ExceptT SplitError (TCMT IO) (SplitTag, SplitClause)
forall (m :: * -> *) a. Monad m => a -> m a
return (SplitTag
SplitCatchall , Telescope
-> [NamedArg SplitPattern]
-> Substitution' SplitPattern
-> Map CheckpointId (Substitution' Term)
-> Maybe (Dom Type)
-> SplitClause
SClause Telescope
delta' [NamedArg SplitPattern]
ps' Substitution' SplitPattern
rho Map CheckpointId (Substitution' Term)
cps Maybe (Dom Type)
forall a. Maybe a
Nothing)

        -- If Agda is changed so that the type of a literal can belong
        -- to an inductive family (with at least one index), then the
        -- following code should be changed (the constructor False
        -- stands for "not indexed").
        (DataOrRecord, Bool, [(SplitTag, SplitClause)])
-> ExceptT
     SplitError
     (TCMT IO)
     (DataOrRecord, Bool, [(SplitTag, SplitClause)])
forall (m :: * -> *) a. Monad m => a -> m a
return (DataOrRecord
IsData, Bool
False, [(SplitTag, SplitClause)]
ns [(SplitTag, SplitClause)]
-> [(SplitTag, SplitClause)] -> [(SplitTag, SplitClause)]
forall a. [a] -> [a] -> [a]
++ [ (SplitTag, SplitClause)
ca ])

  (DataOrRecord
dr, Bool
isIndexed, [(SplitTag, SplitClause)]
ns) <- if [ConHead] -> Bool
forall a. Null a => a -> Bool
null [ConHead]
pcons' Bool -> Bool -> Bool
&& Bool -> Bool
not ([Literal] -> Bool
forall a. Null a => a -> Bool
null [Literal]
plits)
        then ExceptT
  SplitError
  (TCMT IO)
  (DataOrRecord, Bool, [(SplitTag, SplitClause)])
computeLitNeighborhoods
        else ExceptT
  SplitError
  (TCMT IO)
  (DataOrRecord, Bool, [(SplitTag, SplitClause)])
computeNeighborhoods

  [(SplitTag, SplitClause)]
ns <- case Maybe (Dom Type)
target of
    Just Dom Type
a  -> [(SplitTag, SplitClause)]
-> ((SplitTag, SplitClause)
    -> ExceptT SplitError (TCMT IO) (SplitTag, SplitClause))
-> ExceptT SplitError (TCMT IO) [(SplitTag, SplitClause)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [(SplitTag, SplitClause)]
ns (((SplitTag, SplitClause)
  -> ExceptT SplitError (TCMT IO) (SplitTag, SplitClause))
 -> ExceptT SplitError (TCMT IO) [(SplitTag, SplitClause)])
-> ((SplitTag, SplitClause)
    -> ExceptT SplitError (TCMT IO) (SplitTag, SplitClause))
-> ExceptT SplitError (TCMT IO) [(SplitTag, SplitClause)]
forall a b. (a -> b) -> a -> b
$ \ (SplitTag
con, SplitClause
sc) -> TCMT IO (SplitTag, SplitClause)
-> ExceptT SplitError (TCMT IO) (SplitTag, SplitClause)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (TCMT IO (SplitTag, SplitClause)
 -> ExceptT SplitError (TCMT IO) (SplitTag, SplitClause))
-> TCMT IO (SplitTag, SplitClause)
-> ExceptT SplitError (TCMT IO) (SplitTag, SplitClause)
forall a b. (a -> b) -> a -> b
$ (SplitTag
con,) (SplitClause -> (SplitTag, SplitClause))
-> TCMT IO SplitClause -> TCMT IO (SplitTag, SplitClause)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
                 Quantity
-> SplitTag -> SplitClause -> Dom Type -> TCMT IO SplitClause
fixTargetType (Dom Type -> Quantity
forall a. LensQuantity a => a -> Quantity
getQuantity Dom Type
t) SplitTag
con SplitClause
sc Dom Type
a
    Maybe (Dom Type)
Nothing -> [(SplitTag, SplitClause)]
-> ExceptT SplitError (TCMT IO) [(SplitTag, SplitClause)]
forall (m :: * -> *) a. Monad m => a -> m a
return [(SplitTag, SplitClause)]
ns

  [(SplitTag, SplitClause)]
ns <- case InsertTrailing
inserttrailing of
    InsertTrailing
DontInsertTrailing -> [(SplitTag, SplitClause)]
-> ExceptT SplitError (TCMT IO) [(SplitTag, SplitClause)]
forall (m :: * -> *) a. Monad m => a -> m a
return [(SplitTag, SplitClause)]
ns
    InsertTrailing
DoInsertTrailing   -> TCMT IO [(SplitTag, SplitClause)]
-> ExceptT SplitError (TCMT IO) [(SplitTag, SplitClause)]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (TCMT IO [(SplitTag, SplitClause)]
 -> ExceptT SplitError (TCMT IO) [(SplitTag, SplitClause)])
-> TCMT IO [(SplitTag, SplitClause)]
-> ExceptT SplitError (TCMT IO) [(SplitTag, SplitClause)]
forall a b. (a -> b) -> a -> b
$ [(SplitTag, SplitClause)]
-> ((SplitTag, SplitClause) -> TCMT IO (SplitTag, SplitClause))
-> TCMT IO [(SplitTag, SplitClause)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [(SplitTag, SplitClause)]
ns (((SplitTag, SplitClause) -> TCMT IO (SplitTag, SplitClause))
 -> TCMT IO [(SplitTag, SplitClause)])
-> ((SplitTag, SplitClause) -> TCMT IO (SplitTag, SplitClause))
-> TCMT IO [(SplitTag, SplitClause)]
forall a b. (a -> b) -> a -> b
$ \(SplitTag
con,SplitClause
sc) ->
      (SplitTag
con,) (SplitClause -> (SplitTag, SplitClause))
-> ((Telescope, SplitClause) -> SplitClause)
-> (Telescope, SplitClause)
-> (SplitTag, SplitClause)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Telescope, SplitClause) -> SplitClause
forall a b. (a, b) -> b
snd ((Telescope, SplitClause) -> (SplitTag, SplitClause))
-> TCM (Telescope, SplitClause) -> TCMT IO (SplitTag, SplitClause)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bool -> SplitClause -> TCM (Telescope, SplitClause)
insertTrailingArgs Bool
False SplitClause
sc

  Maybe QName
mHCompName <- [Char] -> ExceptT SplitError (TCMT IO) (Maybe QName)
forall (m :: * -> *). HasBuiltins m => [Char] -> m (Maybe QName)
getPrimitiveName' [Char]
builtinHComp
  Bool
withoutK   <- WithDefault 'False -> Bool
forall (b :: Bool). KnownBool b => WithDefault b -> Bool
collapseDefault (WithDefault 'False -> Bool)
-> (PragmaOptions -> WithDefault 'False) -> PragmaOptions -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PragmaOptions -> WithDefault 'False
optWithoutK (PragmaOptions -> Bool)
-> ExceptT SplitError (TCMT IO) PragmaOptions
-> ExceptT SplitError (TCMT IO) Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ExceptT SplitError (TCMT IO) PragmaOptions
forall (m :: * -> *). HasOptions m => m PragmaOptions
pragmaOptions

  Bool
erased <- (TCEnv -> Bool) -> ExceptT SplitError (TCMT IO) Bool
forall (m :: * -> *) a. MonadTCEnv m => (TCEnv -> a) -> m a
asksTC TCEnv -> Bool
forall a. LensQuantity a => a -> Bool
hasQuantity0
  [Char] -> Int -> [Char] -> ExceptT SplitError (TCMT IO) ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> [Char] -> m ()
reportSLn [Char]
"tc.cover.split" Int
60 ([Char] -> ExceptT SplitError (TCMT IO) ())
-> [Char] -> ExceptT SplitError (TCMT IO) ()
forall a b. (a -> b) -> a -> b
$ [Char]
"We are in erased context = " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Bool -> [Char]
forall a. Show a => a -> [Char]
show Bool
erased
  let erasedError :: Bool -> ExceptT SplitError (TCMT IO) (Either SplitClause Covering)
erasedError Bool
causedByWithoutK =
        SplitError
-> ExceptT SplitError (TCMT IO) (Either SplitClause Covering)
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (SplitError
 -> ExceptT SplitError (TCMT IO) (Either SplitClause Covering))
-> (Closure Type -> SplitError)
-> Closure Type
-> ExceptT SplitError (TCMT IO) (Either SplitClause Covering)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> Closure Type -> SplitError
ErasedDatatype Bool
causedByWithoutK (Closure Type
 -> ExceptT SplitError (TCMT IO) (Either SplitClause Covering))
-> ExceptT SplitError (TCMT IO) (Closure Type)
-> ExceptT SplitError (TCMT IO) (Either SplitClause Covering)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<
          do TCM (Closure Type) -> ExceptT SplitError (TCMT IO) (Closure Type)
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCM (Closure Type) -> ExceptT SplitError (TCMT IO) (Closure Type))
-> TCM (Closure Type)
-> ExceptT SplitError (TCMT IO) (Closure Type)
forall a b. (a -> b) -> a -> b
$ TCM (Closure Type) -> TCM (Closure Type)
forall (tcm :: * -> *) a.
(MonadTCM tcm, MonadAddContext tcm, MonadDebug tcm) =>
tcm a -> tcm a
inContextOfT (TCM (Closure Type) -> TCM (Closure Type))
-> TCM (Closure Type) -> TCM (Closure Type)
forall a b. (a -> b) -> a -> b
$ Type -> TCM (Closure Type)
forall (m :: * -> *) a.
(MonadTCEnv m, ReadTCState m) =>
a -> m (Closure a)
buildClosure (Dom Type -> Type
forall t e. Dom' t e -> e
unDom Dom Type
t)

  case [(SplitTag, SplitClause)]
ns of
    []  -> do
      let absurdp :: SplitPattern
absurdp = PatternInfo -> SplitPatVar -> SplitPattern
forall x. PatternInfo -> x -> Pattern' x
VarP (PatOrigin -> [Name] -> PatternInfo
PatternInfo PatOrigin
PatOAbsurd []) (SplitPatVar -> SplitPattern) -> SplitPatVar -> SplitPattern
forall a b. (a -> b) -> a -> b
$ [Char] -> Int -> [Literal] -> SplitPatVar
SplitPatVar [Char]
forall a. Underscore a => a
underscore Int
0 []
          rho :: Substitution' SplitPattern
rho = Int -> Substitution' SplitPattern -> Substitution' SplitPattern
forall a. Int -> Substitution' a -> Substitution' a
liftS Int
x (Substitution' SplitPattern -> Substitution' SplitPattern)
-> Substitution' SplitPattern -> Substitution' SplitPattern
forall a b. (a -> b) -> a -> b
$ SplitPattern
-> Substitution' SplitPattern -> Substitution' SplitPattern
forall a. DeBruijn a => a -> Substitution' a -> Substitution' a
consS SplitPattern
absurdp (Substitution' SplitPattern -> Substitution' SplitPattern)
-> Substitution' SplitPattern -> Substitution' SplitPattern
forall a b. (a -> b) -> a -> b
$ Int -> Substitution' SplitPattern
forall a. Int -> Substitution' a
raiseS Int
1
          ps' :: [NamedArg SplitPattern]
ps' = Substitution' (SubstArg [NamedArg SplitPattern])
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst Substitution' SplitPattern
Substitution' (SubstArg [NamedArg SplitPattern])
rho [NamedArg SplitPattern]
ps
      Either SplitClause Covering
-> ExceptT SplitError (TCMT IO) (Either SplitClause Covering)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either SplitClause Covering
 -> ExceptT SplitError (TCMT IO) (Either SplitClause Covering))
-> Either SplitClause Covering
-> ExceptT SplitError (TCMT IO) (Either SplitClause Covering)
forall a b. (a -> b) -> a -> b
$ SplitClause -> Either SplitClause Covering
forall a b. a -> Either a b
Left (SplitClause -> Either SplitClause Covering)
-> SplitClause -> Either SplitClause Covering
forall a b. (a -> b) -> a -> b
$ SClause
               { scTel :: Telescope
scTel  = Telescope
tel
               , scPats :: [NamedArg SplitPattern]
scPats = [NamedArg SplitPattern]
ps'
               , scSubst :: Substitution' SplitPattern
scSubst              = Substitution' SplitPattern
forall a. HasCallStack => a
__IMPOSSIBLE__ -- not used
               , scCheckpoints :: Map CheckpointId (Substitution' Term)
scCheckpoints        = Map CheckpointId (Substitution' Term)
forall a. HasCallStack => a
__IMPOSSIBLE__ -- not used
               , scTarget :: Maybe (Dom Type)
scTarget             = Maybe (Dom Type)
forall a. Maybe a
Nothing
               }

    -- Andreas, 2018-10-17: If more than one constructor matches, we cannot erase.
    (SplitTag, SplitClause)
_ : (SplitTag, SplitClause)
_ : [(SplitTag, SplitClause)]
_ | Bool -> Bool
not Bool
erased Bool -> Bool -> Bool
&& Bool -> Bool
not (Dom Type -> Bool
forall a. LensQuantity a => a -> Bool
usableQuantity Dom Type
t) ->
      Bool -> ExceptT SplitError (TCMT IO) (Either SplitClause Covering)
erasedError Bool
False

    -- If exactly one constructor matches and the K rule is turned
    -- off, then we only allow erasure for non-indexed data types
    -- (#4172).
    [(SplitTag, SplitClause)
_] | Bool -> Bool
not Bool
erased Bool -> Bool -> Bool
&& Bool -> Bool
not (Dom Type -> Bool
forall a. LensQuantity a => a -> Bool
usableQuantity Dom Type
t) Bool -> Bool -> Bool
&&
          Bool
withoutK Bool -> Bool -> Bool
&& Bool
isIndexed ->
      Bool -> ExceptT SplitError (TCMT IO) (Either SplitClause Covering)
erasedError Bool
True

    [(SplitTag, SplitClause)]
_ -> do

      -- Andreas, 2012-10-10 fail if precomputed constructor set does not cover
      -- all the data type constructors
      -- Andreas, 2017-10-08 ... unless partial covering is explicitly allowed.
      let ptags :: [SplitTag]
ptags = (ConHead -> SplitTag) -> [ConHead] -> [SplitTag]
forall a b. (a -> b) -> [a] -> [b]
map (QName -> SplitTag
SplitCon (QName -> SplitTag) -> (ConHead -> QName) -> ConHead -> SplitTag
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConHead -> QName
conName) [ConHead]
pcons' [SplitTag] -> [SplitTag] -> [SplitTag]
forall a. [a] -> [a] -> [a]
++ (Literal -> SplitTag) -> [Literal] -> [SplitTag]
forall a b. (a -> b) -> [a] -> [b]
map Literal -> SplitTag
SplitLit [Literal]
plits
      -- clauses for hcomp will be automatically generated.
      let inferred_tags :: Set SplitTag
inferred_tags = Set SplitTag
-> (QName -> Set SplitTag) -> Maybe QName -> Set SplitTag
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Set SplitTag
forall a. Set a
Set.empty (SplitTag -> Set SplitTag
forall a. a -> Set a
Set.singleton (SplitTag -> Set SplitTag)
-> (QName -> SplitTag) -> QName -> Set SplitTag
forall b c a. (b -> c) -> (a -> b) -> a -> c
. QName -> SplitTag
SplitCon) Maybe QName
mHCompName
      let all_tags :: Set SplitTag
all_tags = [SplitTag] -> Set SplitTag
forall a. Ord a => [a] -> Set a
Set.fromList [SplitTag]
ptags Set SplitTag -> Set SplitTag -> Set SplitTag
forall a. Ord a => Set a -> Set a -> Set a
`Set.union` Set SplitTag
inferred_tags

      Bool
-> ExceptT SplitError (TCMT IO) ()
-> ExceptT SplitError (TCMT IO) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (AllowPartialCover
allowPartialCover AllowPartialCover -> AllowPartialCover -> Bool
forall a. Eq a => a -> a -> Bool
== AllowPartialCover
NoAllowPartialCover Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
overlap) (ExceptT SplitError (TCMT IO) ()
 -> ExceptT SplitError (TCMT IO) ())
-> ExceptT SplitError (TCMT IO) ()
-> ExceptT SplitError (TCMT IO) ()
forall a b. (a -> b) -> a -> b
$
        [(SplitTag, SplitClause)]
-> ((SplitTag, SplitClause) -> ExceptT SplitError (TCMT IO) ())
-> ExceptT SplitError (TCMT IO) ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [(SplitTag, SplitClause)]
ns (((SplitTag, SplitClause) -> ExceptT SplitError (TCMT IO) ())
 -> ExceptT SplitError (TCMT IO) ())
-> ((SplitTag, SplitClause) -> ExceptT SplitError (TCMT IO) ())
-> ExceptT SplitError (TCMT IO) ()
forall a b. (a -> b) -> a -> b
$ \(SplitTag
tag, SplitClause
sc) -> do
          Bool
-> ExceptT SplitError (TCMT IO) ()
-> ExceptT SplitError (TCMT IO) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (SplitTag
tag SplitTag -> Set SplitTag -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set SplitTag
all_tags) (ExceptT SplitError (TCMT IO) ()
 -> ExceptT SplitError (TCMT IO) ())
-> ExceptT SplitError (TCMT IO) ()
-> ExceptT SplitError (TCMT IO) ()
forall a b. (a -> b) -> a -> b
$ do
            Bool
isImpossibleClause <- TCMT IO Bool -> ExceptT SplitError (TCMT IO) Bool
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCMT IO Bool -> ExceptT SplitError (TCMT IO) Bool)
-> TCMT IO Bool -> ExceptT SplitError (TCMT IO) Bool
forall a b. (a -> b) -> a -> b
$ Telescope -> TCMT IO Bool
isEmptyTel (Telescope -> TCMT IO Bool) -> Telescope -> TCMT IO Bool
forall a b. (a -> b) -> a -> b
$ SplitClause -> Telescope
scTel SplitClause
sc
            Bool
-> ExceptT SplitError (TCMT IO) ()
-> ExceptT SplitError (TCMT IO) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
isImpossibleClause (ExceptT SplitError (TCMT IO) ()
 -> ExceptT SplitError (TCMT IO) ())
-> ExceptT SplitError (TCMT IO) ()
-> ExceptT SplitError (TCMT IO) ()
forall a b. (a -> b) -> a -> b
$ do
              TCMT IO () -> ExceptT SplitError (TCMT IO) ()
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCMT IO () -> ExceptT SplitError (TCMT IO) ())
-> TCMT IO () -> ExceptT SplitError (TCMT IO) ()
forall a b. (a -> b) -> a -> b
$ [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover" Int
10 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
                [ [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text [Char]
"Missing case for" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> SplitTag -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM SplitTag
tag
                , Int -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => Int -> m Doc -> m Doc
nest Int
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ SplitClause -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM SplitClause
sc
                ]
              SplitError -> ExceptT SplitError (TCMT IO) ()
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError ([Char] -> SplitError
GenericSplitError [Char]
"precomputed set of constructors does not cover all cases")

      TCMT IO () -> ExceptT SplitError (TCMT IO) ()
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCMT IO () -> ExceptT SplitError (TCMT IO) ())
-> TCMT IO () -> ExceptT SplitError (TCMT IO) ()
forall a b. (a -> b) -> a -> b
$ DataOrRecord -> Type -> Telescope -> Maybe (Dom Type) -> TCMT IO ()
forall (m :: * -> *) a ty.
(MonadTCM m, PureTCM m, MonadError TCErr m, LensSort a,
 PrettyTCM a, LensSort ty, PrettyTCM ty) =>
DataOrRecord -> a -> Telescope -> Maybe ty -> m ()
checkSortOfSplitVar DataOrRecord
dr (Dom Type -> Type
forall t e. Dom' t e -> e
unDom Dom Type
t) Telescope
delta2 Maybe (Dom Type)
target
      Either SplitClause Covering
-> ExceptT SplitError (TCMT IO) (Either SplitClause Covering)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either SplitClause Covering
 -> ExceptT SplitError (TCMT IO) (Either SplitClause Covering))
-> Either SplitClause Covering
-> ExceptT SplitError (TCMT IO) (Either SplitClause Covering)
forall a b. (a -> b) -> a -> b
$ Covering -> Either SplitClause Covering
forall a b. b -> Either a b
Right (Covering -> Either SplitClause Covering)
-> Covering -> Either SplitClause Covering
forall a b. (a -> b) -> a -> b
$ Arg Int -> [(SplitTag, SplitClause)] -> Covering
Covering (SplitClause -> Int -> Arg Int
lookupPatternVar SplitClause
sc Int
x) [(SplitTag, SplitClause)]
ns

  where
    inContextOfT, inContextOfDelta2 :: (MonadTCM tcm, MonadAddContext tcm, MonadDebug tcm) => tcm a -> tcm a
    inContextOfT :: forall (tcm :: * -> *) a.
(MonadTCM tcm, MonadAddContext tcm, MonadDebug tcm) =>
tcm a -> tcm a
inContextOfT      = Telescope -> tcm a -> tcm a
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext Telescope
tel (tcm a -> tcm a) -> (tcm a -> tcm a) -> tcm a -> tcm a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Impossible -> Int -> tcm a -> tcm a
forall (m :: * -> *) a.
MonadAddContext m =>
Impossible -> Int -> m a -> m a
escapeContext Impossible
HasCallStack => Impossible
impossible (Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
    inContextOfDelta2 :: forall (tcm :: * -> *) a.
(MonadTCM tcm, MonadAddContext tcm, MonadDebug tcm) =>
tcm a -> tcm a
inContextOfDelta2 = Telescope -> tcm a -> tcm a
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext Telescope
tel (tcm a -> tcm a) -> (tcm a -> tcm a) -> tcm a -> tcm a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Impossible -> Int -> tcm a -> tcm a
forall (m :: * -> *) a.
MonadAddContext m =>
Impossible -> Int -> m a -> m a
escapeContext Impossible
HasCallStack => Impossible
impossible Int
x

    -- Debug printing
    debugInit :: a -> a -> [NamedArg SplitPattern] -> a -> tcm ()
debugInit a
tel a
x [NamedArg SplitPattern]
ps a
cps = TCMT IO () -> tcm ()
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCMT IO () -> tcm ()) -> TCMT IO () -> tcm ()
forall a b. (a -> b) -> a -> b
$ TCMT IO () -> TCMT IO ()
forall (tcm :: * -> *) a.
(MonadTCEnv tcm, ReadTCState tcm) =>
tcm a -> tcm a
inTopContext (TCMT IO () -> TCMT IO ()) -> TCMT IO () -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ do
      [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.top" Int
10 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
        [ TCMT IO Doc
"TypeChecking.Coverage.split': split"
        , Int -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => Int -> m Doc -> m Doc
nest Int
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
          [ TCMT IO Doc
"tel     =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> a -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM a
tel
          , TCMT IO Doc
"x       =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> a -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM a
x
          , TCMT IO Doc
"ps      =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> do a -> TCMT IO Doc -> TCMT IO Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext a
tel (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ NAPs -> TCMT IO Doc
forall (m :: * -> *). MonadPretty m => NAPs -> m Doc
prettyTCMPatternList (NAPs -> TCMT IO Doc) -> NAPs -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [NamedArg SplitPattern] -> NAPs
fromSplitPatterns [NamedArg SplitPattern]
ps
          , TCMT IO Doc
"cps     =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> a -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM a
cps
          ]
        ]
      [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.top" Int
60 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
        [ TCMT IO Doc
"TypeChecking.Coverage.split': split"
        , Int -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => Int -> m Doc -> m Doc
nest Int
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
          [ TCMT IO Doc
"tel     =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> ([Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char] -> TCMT IO Doc) -> (a -> [Char]) -> a -> TCMT IO Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [Char]
forall a. Show a => a -> [Char]
show) a
tel
          , TCMT IO Doc
"x       =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> ([Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char] -> TCMT IO Doc) -> (a -> [Char]) -> a -> TCMT IO Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [Char]
forall a. Show a => a -> [Char]
show) a
x
          , TCMT IO Doc
"ps      =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> ([Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char] -> TCMT IO Doc)
-> ([NamedArg SplitPattern] -> [Char])
-> [NamedArg SplitPattern]
-> TCMT IO Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [NamedArg SplitPattern] -> [Char]
forall a. Show a => a -> [Char]
show) [NamedArg SplitPattern]
ps
          , TCMT IO Doc
"cps     =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> ([Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char] -> TCMT IO Doc) -> (a -> [Char]) -> a -> TCMT IO Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [Char]
forall a. Show a => a -> [Char]
show) a
cps
          ]
        ]

    debugHoleAndType :: a -> a -> [Char] -> NAPs -> a -> tcm ()
debugHoleAndType a
delta1 a
delta2 [Char]
s NAPs
ps a
t =
      TCMT IO () -> tcm ()
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCMT IO () -> tcm ()) -> TCMT IO () -> tcm ()
forall a b. (a -> b) -> a -> b
$ [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.top" Int
10 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ Int -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => Int -> m Doc -> m Doc
nest Int
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat ([TCMT IO Doc] -> TCMT IO Doc) -> [TCMT IO Doc] -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$
        [ TCMT IO Doc
"p      =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char] -> [Char]
patVarNameToString [Char]
s)
        , TCMT IO Doc
"ps     =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> NAPs -> TCMT IO Doc
forall (m :: * -> *). MonadPretty m => NAPs -> m Doc
prettyTCMPatternList NAPs
ps
        , TCMT IO Doc
"delta1 =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> a -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM a
delta1
        , TCMT IO Doc
"delta2 =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> TCMT IO Doc -> TCMT IO Doc
forall (tcm :: * -> *) a.
(MonadTCM tcm, MonadAddContext tcm, MonadDebug tcm) =>
tcm a -> tcm a
inContextOfDelta2 (a -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM a
delta2)
        , TCMT IO Doc
"t      =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> TCMT IO Doc -> TCMT IO Doc
forall (tcm :: * -> *) a.
(MonadTCM tcm, MonadAddContext tcm, MonadDebug tcm) =>
tcm a -> tcm a
inContextOfT (a -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM a
t)
        ]


-- | splitResult for MakeCase, tries to introduce IApply or ProjP copatterns
splitResult :: QName -> SplitClause -> TCM (Either SplitError [SplitClause])
splitResult :: QName -> SplitClause -> TCM (Either SplitError [SplitClause])
splitResult QName
f SplitClause
sc = do
  TCMT IO (Maybe SplitClause)
-> TCM (Either SplitError [SplitClause])
-> (SplitClause -> TCM (Either SplitError [SplitClause]))
-> TCM (Either SplitError [SplitClause])
forall (m :: * -> *) a b.
Monad m =>
m (Maybe a) -> m b -> (a -> m b) -> m b
caseMaybeM (QName -> SplitClause -> TCMT IO (Maybe SplitClause)
splitResultPath QName
f SplitClause
sc)
             (((Either SplitError Covering -> Either SplitError [SplitClause])
-> TCMT IO (Either SplitError Covering)
-> TCM (Either SplitError [SplitClause])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Either SplitError Covering -> Either SplitError [SplitClause])
 -> TCMT IO (Either SplitError Covering)
 -> TCM (Either SplitError [SplitClause]))
-> ((Covering -> [SplitClause])
    -> Either SplitError Covering -> Either SplitError [SplitClause])
-> (Covering -> [SplitClause])
-> TCMT IO (Either SplitError Covering)
-> TCM (Either SplitError [SplitClause])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Covering -> [SplitClause])
-> Either SplitError Covering -> Either SplitError [SplitClause]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) Covering -> [SplitClause]
splitClauses (TCMT IO (Either SplitError Covering)
 -> TCM (Either SplitError [SplitClause]))
-> TCMT IO (Either SplitError Covering)
-> TCM (Either SplitError [SplitClause])
forall a b. (a -> b) -> a -> b
$ QName -> SplitClause -> TCMT IO (Either SplitError Covering)
splitResultRecord QName
f SplitClause
sc)
             (Either SplitError [SplitClause]
-> TCM (Either SplitError [SplitClause])
forall (m :: * -> *) a. Monad m => a -> m a
return (Either SplitError [SplitClause]
 -> TCM (Either SplitError [SplitClause]))
-> (SplitClause -> Either SplitError [SplitClause])
-> SplitClause
-> TCM (Either SplitError [SplitClause])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [SplitClause] -> Either SplitError [SplitClause]
forall a b. b -> Either a b
Right ([SplitClause] -> Either SplitError [SplitClause])
-> (SplitClause -> [SplitClause])
-> SplitClause
-> Either SplitError [SplitClause]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SplitClause -> [SplitClause] -> [SplitClause]
forall a. a -> [a] -> [a]
:[]))


-- | Tries to split the result to introduce an IApply pattern.
splitResultPath :: QName -> SplitClause -> TCM (Maybe SplitClause)
splitResultPath :: QName -> SplitClause -> TCMT IO (Maybe SplitClause)
splitResultPath QName
f sc :: SplitClause
sc@(SClause Telescope
tel [NamedArg SplitPattern]
ps Substitution' SplitPattern
_ Map CheckpointId (Substitution' Term)
_ Maybe (Dom Type)
target) = do
  Maybe (Dom Type)
-> TCMT IO (Maybe SplitClause)
-> (Dom Type -> TCMT IO (Maybe SplitClause))
-> TCMT IO (Maybe SplitClause)
forall a b. Maybe a -> b -> (a -> b) -> b
caseMaybe Maybe (Dom Type)
target (Maybe SplitClause -> TCMT IO (Maybe SplitClause)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe SplitClause
forall a. Maybe a
Nothing) ((Dom Type -> TCMT IO (Maybe SplitClause))
 -> TCMT IO (Maybe SplitClause))
-> (Dom Type -> TCMT IO (Maybe SplitClause))
-> TCMT IO (Maybe SplitClause)
forall a b. (a -> b) -> a -> b
$ \ Dom Type
t -> do
        TCMT IO (Maybe (Dom Type, Abs Type))
-> TCMT IO (Maybe SplitClause)
-> ((Dom Type, Abs Type) -> TCMT IO (Maybe SplitClause))
-> TCMT IO (Maybe SplitClause)
forall (m :: * -> *) a b.
Monad m =>
m (Maybe a) -> m b -> (a -> m b) -> m b
caseMaybeM (Type -> TCMT IO (Maybe (Dom Type, Abs Type))
forall (m :: * -> *).
PureTCM m =>
Type -> m (Maybe (Dom Type, Abs Type))
isPath (Dom Type -> Type
forall t e. Dom' t e -> e
unDom Dom Type
t)) (Maybe SplitClause -> TCMT IO (Maybe SplitClause)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe SplitClause
forall a. Maybe a
Nothing) (((Dom Type, Abs Type) -> TCMT IO (Maybe SplitClause))
 -> TCMT IO (Maybe SplitClause))
-> ((Dom Type, Abs Type) -> TCMT IO (Maybe SplitClause))
-> TCMT IO (Maybe SplitClause)
forall a b. (a -> b) -> a -> b
$ \ (Dom Type, Abs Type)
_ -> do
               (TelV Telescope
i Type
b, [(Term, (Term, Term))]
boundary) <- Int -> Type -> TCMT IO (TelV Type, [(Term, (Term, Term))])
forall (m :: * -> *).
PureTCM m =>
Int -> Type -> m (TelV Type, [(Term, (Term, Term))])
telViewUpToPathBoundary' Int
1 (Dom Type -> Type
forall t e. Dom' t e -> e
unDom Dom Type
t)
               let tel' :: Telescope
tel' = Telescope -> Telescope -> Telescope
forall t. Abstract t => Telescope -> t -> t
abstract Telescope
tel Telescope
i
                   rho :: Substitution' a
rho  = Int -> Substitution' a
forall a. Int -> Substitution' a
raiseS Int
1
                   ps' :: [NamedArg SplitPattern]
ps' = Substitution' (SubstArg [NamedArg SplitPattern])
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst Substitution' (SubstArg [NamedArg SplitPattern])
forall a. Substitution' a
rho (SplitClause -> [NamedArg SplitPattern]
scPats SplitClause
sc) [NamedArg SplitPattern]
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a. [a] -> [a] -> [a]
++ Telescope -> [(Term, (Term, Term))] -> [NamedArg SplitPattern]
forall a.
DeBruijn a =>
Telescope -> [(Term, (Term, Term))] -> [NamedArg (Pattern' a)]
telePatterns Telescope
i [(Term, (Term, Term))]
boundary
                   cps' :: Map CheckpointId (Substitution' Term)
cps' = Substitution' (SubstArg (Map CheckpointId (Substitution' Term)))
-> Map CheckpointId (Substitution' Term)
-> Map CheckpointId (Substitution' Term)
forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst Substitution' (SubstArg (Map CheckpointId (Substitution' Term)))
forall a. Substitution' a
rho (SplitClause -> Map CheckpointId (Substitution' Term)
scCheckpoints SplitClause
sc)
                   target' :: Maybe (Dom Type)
target' = Dom Type -> Maybe (Dom Type)
forall a. a -> Maybe a
Just (Dom Type -> Maybe (Dom Type)) -> Dom Type -> Maybe (Dom Type)
forall a b. (a -> b) -> a -> b
$ Type
b Type -> Dom Type -> Dom Type
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Dom Type
t
               Maybe SplitClause -> TCMT IO (Maybe SplitClause)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe SplitClause -> TCMT IO (Maybe SplitClause))
-> (SplitClause -> Maybe SplitClause)
-> SplitClause
-> TCMT IO (Maybe SplitClause)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SplitClause -> Maybe SplitClause
forall a. a -> Maybe a
Just (SplitClause -> TCMT IO (Maybe SplitClause))
-> SplitClause -> TCMT IO (Maybe SplitClause)
forall a b. (a -> b) -> a -> b
$ Telescope
-> [NamedArg SplitPattern]
-> Substitution' SplitPattern
-> Map CheckpointId (Substitution' Term)
-> Maybe (Dom Type)
-> SplitClause
SClause Telescope
tel' [NamedArg SplitPattern]
ps' Substitution' SplitPattern
forall a. Substitution' a
idS Map CheckpointId (Substitution' Term)
cps' Maybe (Dom Type)
target'

-- | @splitResultRecord f sc = return res@
--
--   If the target type of @sc@ is a record type, a covering set of
--   split clauses is returned (@sc@ extended by all valid projection patterns),
--   otherwise @res == Left _@.
--   Note that the empty set of split clauses is returned if the record has no fields.
splitResultRecord :: QName -> SplitClause -> TCM (Either SplitError Covering)
splitResultRecord :: QName -> SplitClause -> TCMT IO (Either SplitError Covering)
splitResultRecord QName
f sc :: SplitClause
sc@(SClause Telescope
tel [NamedArg SplitPattern]
ps Substitution' SplitPattern
_ Map CheckpointId (Substitution' Term)
_ Maybe (Dom Type)
target) = do
  [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.split" Int
10 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
    [ TCMT IO Doc
"splitting result:"
    , Int -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => Int -> m Doc -> m Doc
nest Int
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ TCMT IO Doc
"f      =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> QName -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM QName
f
    , Int -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => Int -> m Doc -> m Doc
nest Int
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ TCMT IO Doc
"target =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Telescope -> TCMT IO Doc -> TCMT IO Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext Telescope
tel (TCMT IO Doc
-> (Dom Type -> TCMT IO Doc) -> Maybe (Dom Type) -> TCMT IO Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe TCMT IO Doc
forall a. Null a => a
empty Dom Type -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM Maybe (Dom Type)
target)
    ]
  -- if we want to split projections, but have no target type, we give up
  let failure :: a -> TCMT IO (Either a b)
failure = Either a b -> TCMT IO (Either a b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either a b -> TCMT IO (Either a b))
-> (a -> Either a b) -> a -> TCMT IO (Either a b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Either a b
forall a b. a -> Either a b
Left
  Maybe (Dom Type)
-> TCMT IO (Either SplitError Covering)
-> (Dom Type -> TCMT IO (Either SplitError Covering))
-> TCMT IO (Either SplitError Covering)
forall a b. Maybe a -> b -> (a -> b) -> b
caseMaybe Maybe (Dom Type)
target (SplitError -> TCMT IO (Either SplitError Covering)
forall {a} {b}. a -> TCMT IO (Either a b)
failure SplitError
CosplitNoTarget) ((Dom Type -> TCMT IO (Either SplitError Covering))
 -> TCMT IO (Either SplitError Covering))
-> (Dom Type -> TCMT IO (Either SplitError Covering))
-> TCMT IO (Either SplitError Covering)
forall a b. (a -> b) -> a -> b
$ \ Dom Type
t -> do
    Maybe (QName, Args, Defn)
isR <- Telescope
-> TCMT IO (Maybe (QName, Args, Defn))
-> TCMT IO (Maybe (QName, Args, Defn))
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext Telescope
tel (TCMT IO (Maybe (QName, Args, Defn))
 -> TCMT IO (Maybe (QName, Args, Defn)))
-> TCMT IO (Maybe (QName, Args, Defn))
-> TCMT IO (Maybe (QName, Args, Defn))
forall a b. (a -> b) -> a -> b
$ Type -> TCMT IO (Maybe (QName, Args, Defn))
forall (m :: * -> *).
PureTCM m =>
Type -> m (Maybe (QName, Args, Defn))
isRecordType (Type -> TCMT IO (Maybe (QName, Args, Defn)))
-> Type -> TCMT IO (Maybe (QName, Args, Defn))
forall a b. (a -> b) -> a -> b
$ Dom Type -> Type
forall t e. Dom' t e -> e
unDom Dom Type
t
    case Maybe (QName, Args, Defn)
isR of
      Just (QName
_r, Args
vs, Record{ recFields :: Defn -> [Dom QName]
recFields = [Dom QName]
fs }) -> do
        [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover" Int
20 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
sep
          [ [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char] -> TCMT IO Doc) -> [Char] -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [Char]
"we are of record type _r = " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ QName -> [Char]
forall a. Pretty a => a -> [Char]
prettyShow QName
_r
          , [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text   [Char]
"applied to parameters vs =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Telescope -> TCMT IO Doc -> TCMT IO Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext Telescope
tel (Args -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM Args
vs)
          , [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text ([Char] -> TCMT IO Doc) -> [Char] -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [Char]
"and have fields       fs = " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Dom QName] -> [Char]
forall a. Pretty a => a -> [Char]
prettyShow [Dom QName]
fs
          ]
        -- Andreas, 2018-06-09, issue #2170, we always have irrelevant projections
        -- available on the lhs.
        -- -- Andreas, 2018-03-19, issue #2971, check that we have a "strong" record type,
        -- -- i.e., with all the projections.  Otherwise, we may not split.
        -- ifNotM (strongRecord fs) (failure CosplitIrrelevantProjections) $ {-else-} do
        let es :: [Elim]
es = NAPs -> [Elim]
patternsToElims (NAPs -> [Elim]) -> NAPs -> [Elim]
forall a b. (a -> b) -> a -> b
$ [NamedArg SplitPattern] -> NAPs
fromSplitPatterns [NamedArg SplitPattern]
ps
        -- Note: module parameters are part of ps
        let self :: Arg Term
self  = Term -> Arg Term
forall a. a -> Arg a
defaultArg (Term -> Arg Term) -> Term -> Arg Term
forall a b. (a -> b) -> a -> b
$ QName -> [Elim] -> Term
Def QName
f [] Term -> [Elim] -> Term
forall t. Apply t => t -> [Elim] -> t
`applyE` [Elim]
es
            pargs :: Args
pargs = Args
vs Args -> Args -> Args
forall a. [a] -> [a] -> [a]
++ [Arg Term
self]
            fieldValues :: [Term]
fieldValues = [Dom QName] -> (Dom QName -> Term) -> [Term]
forall (m :: * -> *) a b. Functor m => m a -> (a -> b) -> m b
for [Dom QName]
fs ((Dom QName -> Term) -> [Term]) -> (Dom QName -> Term) -> [Term]
forall a b. (a -> b) -> a -> b
$ \ Dom QName
proj -> Arg Term -> Term
forall e. Arg e -> e
unArg Arg Term
self Term -> [Elim] -> Term
forall t. Apply t => t -> [Elim] -> t
`applyE` [ProjOrigin -> QName -> Elim
forall a. ProjOrigin -> QName -> Elim' a
Proj ProjOrigin
ProjSystem (Dom QName -> QName
forall t e. Dom' t e -> e
unDom Dom QName
proj)]
        [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover" Int
20 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ Telescope -> TCMT IO Doc -> TCMT IO Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext Telescope
tel (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
sep
          [ [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text   [Char]
"we are              self =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Term -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM (Arg Term -> Term
forall e. Arg e -> e
unArg Arg Term
self)
          , [Char] -> TCMT IO Doc
forall (m :: * -> *). Applicative m => [Char] -> m Doc
text   [Char]
"            field values =" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> [Term] -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM [Term]
fieldValues
          ]
        let n :: Arg Int
n = Int -> Arg Int
forall a. a -> Arg a
defaultArg (Int -> Arg Int) -> Int -> Arg Int
forall a b. (a -> b) -> a -> b
$ Permutation -> Int
permRange (Permutation -> Int) -> Permutation -> Int
forall a b. (a -> b) -> a -> b
$ Permutation -> Maybe Permutation -> Permutation
forall a. a -> Maybe a -> a
fromMaybe Permutation
forall a. HasCallStack => a
__IMPOSSIBLE__ (Maybe Permutation -> Permutation)
-> Maybe Permutation -> Permutation
forall a b. (a -> b) -> a -> b
$ NAPs -> Maybe Permutation
dbPatPerm (NAPs -> Maybe Permutation) -> NAPs -> Maybe Permutation
forall a b. (a -> b) -> a -> b
$ [NamedArg SplitPattern] -> NAPs
fromSplitPatterns [NamedArg SplitPattern]
ps
            -- Andreas & James, 2013-11-19 includes the dot patterns!
            -- See test/succeed/CopatternsAndDotPatterns.agda for a case with dot patterns
            -- and copatterns which fails for @n = size tel@ with a broken case tree.

        -- Andreas, 2016-07-22 read the style of projections from the user's lips
        ProjOrigin
projOrigin <- TCMT IO Bool
-> TCMT IO ProjOrigin -> TCMT IO ProjOrigin -> TCMT IO ProjOrigin
forall (m :: * -> *) a. Monad m => m Bool -> m a -> m a -> m a
ifM (PragmaOptions -> Bool
optPostfixProjections (PragmaOptions -> Bool) -> TCMT IO PragmaOptions -> TCMT IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TCMT IO PragmaOptions
forall (m :: * -> *). HasOptions m => m PragmaOptions
pragmaOptions) (ProjOrigin -> TCMT IO ProjOrigin
forall (m :: * -> *) a. Monad m => a -> m a
return ProjOrigin
ProjPostfix) (ProjOrigin -> TCMT IO ProjOrigin
forall (m :: * -> *) a. Monad m => a -> m a
return ProjOrigin
ProjPrefix)
        Covering -> Either SplitError Covering
forall a b. b -> Either a b
Right (Covering -> Either SplitError Covering)
-> ([(SplitTag, SplitClause)] -> Covering)
-> [(SplitTag, SplitClause)]
-> Either SplitError Covering
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Arg Int -> [(SplitTag, SplitClause)] -> Covering
Covering Arg Int
n ([(SplitTag, SplitClause)] -> Either SplitError Covering)
-> TCMT IO [(SplitTag, SplitClause)]
-> TCMT IO (Either SplitError Covering)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> do
          [(Dom QName, [Term])]
-> ((Dom QName, [Term]) -> TCMT IO (SplitTag, SplitClause))
-> TCMT IO [(SplitTag, SplitClause)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM ([Dom QName] -> [[Term]] -> [(Dom QName, [Term])]
forall a b. [a] -> [b] -> [(a, b)]
zip [Dom QName]
fs ([[Term]] -> [(Dom QName, [Term])])
-> [[Term]] -> [(Dom QName, [Term])]
forall a b. (a -> b) -> a -> b
$ [Term] -> [[Term]]
forall a. [a] -> [[a]]
List.inits [Term]
fieldValues) (((Dom QName, [Term]) -> TCMT IO (SplitTag, SplitClause))
 -> TCMT IO [(SplitTag, SplitClause)])
-> ((Dom QName, [Term]) -> TCMT IO (SplitTag, SplitClause))
-> TCMT IO [(SplitTag, SplitClause)]
forall a b. (a -> b) -> a -> b
$ \ (Dom QName
proj, [Term]
prevFields) -> do
            -- compute the new target
            Type
dType <- Definition -> Type
defType (Definition -> Type) -> TCMT IO Definition -> TCMT IO Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> do QName -> TCMT IO Definition
forall (m :: * -> *). HasConstInfo m => QName -> m Definition
getConstInfo (QName -> TCMT IO Definition) -> QName -> TCMT IO Definition
forall a b. (a -> b) -> a -> b
$ Dom QName -> QName
forall t e. Dom' t e -> e
unDom Dom QName
proj -- WRONG: typeOfConst $ unArg proj
            let -- Substitution for parameters and previous fields. Needs to be applied to potential
                -- tactic in proj.
                fieldSub :: Substitution' Term
fieldSub = [Term] -> [Term]
forall a. [a] -> [a]
reverse ((Arg Term -> Term) -> Args -> [Term]
forall a b. (a -> b) -> [a] -> [b]
map Arg Term -> Term
forall e. Arg e -> e
unArg Args
vs [Term] -> [Term] -> [Term]
forall a. [a] -> [a] -> [a]
++ [Term]
prevFields) [Term] -> Substitution' Term -> Substitution' Term
forall a. DeBruijn a => [a] -> Substitution' a -> Substitution' a
++# Impossible -> Substitution' Term
forall a. Impossible -> Substitution' a
EmptyS Impossible
HasCallStack => Impossible
impossible
                proj' :: Dom QName
proj'    = Substitution' (SubstArg (Dom QName)) -> Dom QName -> Dom QName
forall a. Subst a => Substitution' (SubstArg a) -> a -> a
applySubst Substitution' Term
Substitution' (SubstArg (Dom QName))
fieldSub Dom QName
proj
                -- type of projection instantiated at self
                target' :: Maybe (Dom Type)
target' = Dom Type -> Maybe (Dom Type)
forall a. a -> Maybe a
Just (Dom Type -> Maybe (Dom Type)) -> Dom Type -> Maybe (Dom Type)
forall a b. (a -> b) -> a -> b
$ Dom QName
proj' Dom QName -> Type -> Dom Type
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Type
dType Type -> Args -> Type
`piApply` Args
pargs      -- Always visible (#2287)
                projArg :: NamedArg SplitPattern
projArg = (QName -> Named NamedName SplitPattern)
-> Arg QName -> NamedArg SplitPattern
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Maybe NamedName -> SplitPattern -> Named NamedName SplitPattern
forall name a. Maybe name -> a -> Named name a
Named Maybe NamedName
forall a. Maybe a
Nothing (SplitPattern -> Named NamedName SplitPattern)
-> (QName -> SplitPattern) -> QName -> Named NamedName SplitPattern
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ProjOrigin -> QName -> SplitPattern
forall x. ProjOrigin -> QName -> Pattern' x
ProjP ProjOrigin
projOrigin) (Arg QName -> NamedArg SplitPattern)
-> Arg QName -> NamedArg SplitPattern
forall a b. (a -> b) -> a -> b
$ Dom QName -> Arg QName
forall t a. Dom' t a -> Arg a
argFromDom (Dom QName -> Arg QName) -> Dom QName -> Arg QName
forall a b. (a -> b) -> a -> b
$ Hiding -> Dom QName -> Dom QName
forall a. LensHiding a => Hiding -> a -> a
setHiding Hiding
NotHidden Dom QName
proj
                sc' :: SplitClause
sc' = SplitClause
sc { scPats :: [NamedArg SplitPattern]
scPats   = SplitClause -> [NamedArg SplitPattern]
scPats SplitClause
sc [NamedArg SplitPattern]
-> [NamedArg SplitPattern] -> [NamedArg SplitPattern]
forall a. [a] -> [a] -> [a]
++ [NamedArg SplitPattern
projArg]
                         , scSubst :: Substitution' SplitPattern
scSubst  = Substitution' SplitPattern
forall a. Substitution' a
idS
                         , scTarget :: Maybe (Dom Type)
scTarget = Maybe (Dom Type)
target'
                         }
            [Char] -> Int -> TCMT IO Doc -> TCMT IO ()
forall (m :: * -> *).
MonadDebug m =>
[Char] -> Int -> TCMT IO Doc -> m ()
reportSDoc [Char]
"tc.cover.copattern" Int
40 (TCMT IO Doc -> TCMT IO ()) -> TCMT IO Doc -> TCMT IO ()
forall a b. (a -> b) -> a -> b
$ [TCMT IO Doc] -> TCMT IO Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
              [ TCMT IO Doc
"fieldSub for" TCMT IO Doc -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> QName -> TCMT IO Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM (Dom QName -> QName
forall t e. Dom' t e -> e
unDom Dom QName
proj)
              , Int -> TCMT IO Doc -> TCMT IO Doc
forall (m :: * -> *). Functor m => Int -> m Doc -> m Doc
nest Int
2 (TCMT IO Doc -> TCMT IO Doc) -> TCMT IO Doc -> TCMT IO Doc
forall a b. (a -> b) -> a -> b
$ Substitution' Term -> TCMT IO Doc
forall (m :: * -> *) a. (Applicative m, Pretty a) => a -> m Doc
pretty Substitution' Term
fieldSub ]
            (SplitTag, SplitClause) -> TCMT IO (SplitTag, SplitClause)
forall (m :: * -> *) a. Monad m => a -> m a
return (QName -> SplitTag
SplitCon (Dom QName -> QName
forall t e. Dom' t e -> e
unDom Dom QName
proj), SplitClause
sc')
      Maybe (QName, Args, Defn)
_ -> Telescope
-> TCMT IO (Either SplitError Covering)
-> TCMT IO (Either SplitError Covering)
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext Telescope
tel (TCMT IO (Either SplitError Covering)
 -> TCMT IO (Either SplitError Covering))
-> TCMT IO (Either SplitError Covering)
-> TCMT IO (Either SplitError Covering)
forall a b. (a -> b) -> a -> b
$ do
        Type -> TCM (Closure Type)
forall (m :: * -> *) a.
(MonadTCEnv m, ReadTCState m) =>
a -> m (Closure a)
buildClosure (Dom Type -> Type
forall t e. Dom' t e -> e
unDom Dom Type
t) TCM (Closure Type)
-> (Closure Type -> TCMT IO (Either SplitError Covering))
-> TCMT IO (Either SplitError Covering)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= SplitError -> TCMT IO (Either SplitError Covering)
forall {a} {b}. a -> TCMT IO (Either a b)
failure (SplitError -> TCMT IO (Either SplitError Covering))
-> (Closure Type -> SplitError)
-> Closure Type
-> TCMT IO (Either SplitError Covering)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Closure Type -> SplitError
CosplitNoRecordType
  -- Andreas, 2018-06-09, issue #2170: splitting with irrelevant fields is always fine!
  -- where
  -- -- A record type is strong if it has all the projections.
  -- -- This is the case if --irrelevant-projections or no field is irrelevant.
  -- -- TODO: what about shape irrelevance?
  -- strongRecord :: [Arg QName] -> TCM Bool
  -- strongRecord fs = (optIrrelevantProjections <$> pragmaOptions) `or2M`
  --   (return $ not $ any isIrrelevant fs)


-- * Boring instances

-- | For debugging only.
instance PrettyTCM SplitClause where
  prettyTCM :: forall (m :: * -> *). MonadPretty m => SplitClause -> m Doc
prettyTCM (SClause Telescope
tel [NamedArg SplitPattern]
pats Substitution' SplitPattern
sigma Map CheckpointId (Substitution' Term)
cps Maybe (Dom Type)
target) = [m Doc] -> m Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
sep
    [ m Doc
"SplitClause"
    , Int -> m Doc -> m Doc
forall (m :: * -> *). Functor m => Int -> m Doc -> m Doc
nest Int
2 (m Doc -> m Doc) -> m Doc -> m Doc
forall a b. (a -> b) -> a -> b
$ [m Doc] -> m Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
vcat
      [ m Doc
"tel          =" m Doc -> m Doc -> m Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Telescope -> m Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM Telescope
tel
      , m Doc
"pats         =" m Doc -> m Doc -> m Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> [m Doc] -> m Doc
forall (m :: * -> *) (t :: * -> *).
(Applicative m, Foldable t) =>
t (m Doc) -> m Doc
sep ((NamedArg SplitPattern -> m Doc)
-> [NamedArg SplitPattern] -> [m Doc]
forall a b. (a -> b) -> [a] -> [b]
map (SplitPattern -> m Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM (SplitPattern -> m Doc)
-> (NamedArg SplitPattern -> SplitPattern)
-> NamedArg SplitPattern
-> m Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NamedArg SplitPattern -> SplitPattern
forall a. NamedArg a -> a
namedArg) [NamedArg SplitPattern]
pats)
      , m Doc
"subst        =" m Doc -> m Doc -> m Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Substitution' SplitPattern -> m Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM Substitution' SplitPattern
sigma
      , m Doc
"checkpoints  =" m Doc -> m Doc -> m Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Map CheckpointId (Substitution' Term) -> m Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM Map CheckpointId (Substitution' Term)
cps
      , m Doc
"target       =" m Doc -> m Doc -> m Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> do
          Maybe (Dom Type) -> m Doc -> (Dom Type -> m Doc) -> m Doc
forall a b. Maybe a -> b -> (a -> b) -> b
caseMaybe Maybe (Dom Type)
target m Doc
forall a. Null a => a
empty ((Dom Type -> m Doc) -> m Doc) -> (Dom Type -> m Doc) -> m Doc
forall a b. (a -> b) -> a -> b
$ \ Dom Type
t -> do
            Telescope -> m Doc -> m Doc
forall b (m :: * -> *) a.
(AddContext b, MonadAddContext m) =>
b -> m a -> m a
addContext Telescope
tel (m Doc -> m Doc) -> m Doc -> m Doc
forall a b. (a -> b) -> a -> b
$ Dom Type -> m Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM Dom Type
t
      -- Triggers crash (see Issue 1374).
      -- , "subst target = " <+> do
      --     caseMaybe target empty $ \ t -> do
      --       addContext tel $ prettyTCM $ applySubst sigma t
      ]
    ]