{-# LANGUAGE NondecreasingIndentation #-}
module Agda.TypeChecking.Monad.MetaVars where
import Prelude hiding (null)
import Control.DeepSeq
import Control.Monad ( (<=<), guard )
import Control.Monad.Except
import Control.Monad.State
import Control.Monad.Trans.Identity ( IdentityT )
import Control.Monad.Reader
import Control.Monad.Writer
import Control.Monad.Fail (MonadFail)
import qualified Data.IntMap as IntMap
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
import qualified Data.List as List
import Data.Set (Set)
import qualified Data.Set as Set
import qualified Data.Foldable as Fold
import Agda.Syntax.Common
import Agda.Syntax.Internal
import Agda.Syntax.Internal.MetaVars
import Agda.Syntax.Position
import Agda.Syntax.Scope.Base
import Agda.TypeChecking.Monad.Base
import Agda.TypeChecking.Monad.Builtin (HasBuiltins)
import Agda.TypeChecking.Monad.Trace
import Agda.TypeChecking.Monad.Closure
import Agda.TypeChecking.Monad.Constraints (MonadConstraint)
import Agda.TypeChecking.Monad.Debug (MonadDebug, reportSLn)
import Agda.TypeChecking.Monad.Context
import Agda.TypeChecking.Monad.Signature (HasConstInfo)
import Agda.TypeChecking.Substitute
import {-# SOURCE #-} Agda.TypeChecking.Telescope
import qualified Agda.Utils.BiMap as BiMap
import Agda.Utils.Functor ((<.>))
import Agda.Utils.List (nubOn)
import Agda.Utils.Maybe
import Agda.Utils.Monad
import Agda.Utils.Null
import Agda.Utils.Permutation
import Agda.Utils.Pretty (prettyShow)
import Agda.Utils.Tuple
import qualified Agda.Utils.Maybe.Strict as Strict
import Agda.Utils.Impossible
data MetaKind =
Records
| SingletonRecords
| Levels
deriving (MetaKind -> MetaKind -> Bool
(MetaKind -> MetaKind -> Bool)
-> (MetaKind -> MetaKind -> Bool) -> Eq MetaKind
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: MetaKind -> MetaKind -> Bool
$c/= :: MetaKind -> MetaKind -> Bool
== :: MetaKind -> MetaKind -> Bool
$c== :: MetaKind -> MetaKind -> Bool
Eq, Int -> MetaKind
MetaKind -> Int
MetaKind -> [MetaKind]
MetaKind -> MetaKind
MetaKind -> MetaKind -> [MetaKind]
MetaKind -> MetaKind -> MetaKind -> [MetaKind]
(MetaKind -> MetaKind)
-> (MetaKind -> MetaKind)
-> (Int -> MetaKind)
-> (MetaKind -> Int)
-> (MetaKind -> [MetaKind])
-> (MetaKind -> MetaKind -> [MetaKind])
-> (MetaKind -> MetaKind -> [MetaKind])
-> (MetaKind -> MetaKind -> MetaKind -> [MetaKind])
-> Enum MetaKind
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: MetaKind -> MetaKind -> MetaKind -> [MetaKind]
$cenumFromThenTo :: MetaKind -> MetaKind -> MetaKind -> [MetaKind]
enumFromTo :: MetaKind -> MetaKind -> [MetaKind]
$cenumFromTo :: MetaKind -> MetaKind -> [MetaKind]
enumFromThen :: MetaKind -> MetaKind -> [MetaKind]
$cenumFromThen :: MetaKind -> MetaKind -> [MetaKind]
enumFrom :: MetaKind -> [MetaKind]
$cenumFrom :: MetaKind -> [MetaKind]
fromEnum :: MetaKind -> Int
$cfromEnum :: MetaKind -> Int
toEnum :: Int -> MetaKind
$ctoEnum :: Int -> MetaKind
pred :: MetaKind -> MetaKind
$cpred :: MetaKind -> MetaKind
succ :: MetaKind -> MetaKind
$csucc :: MetaKind -> MetaKind
Enum, MetaKind
MetaKind -> MetaKind -> Bounded MetaKind
forall a. a -> a -> Bounded a
maxBound :: MetaKind
$cmaxBound :: MetaKind
minBound :: MetaKind
$cminBound :: MetaKind
Bounded, Int -> MetaKind -> ShowS
[MetaKind] -> ShowS
MetaKind -> String
(Int -> MetaKind -> ShowS)
-> (MetaKind -> String) -> ([MetaKind] -> ShowS) -> Show MetaKind
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [MetaKind] -> ShowS
$cshowList :: [MetaKind] -> ShowS
show :: MetaKind -> String
$cshow :: MetaKind -> String
showsPrec :: Int -> MetaKind -> ShowS
$cshowsPrec :: Int -> MetaKind -> ShowS
Show)
allMetaKinds :: [MetaKind]
allMetaKinds :: [MetaKind]
allMetaKinds = [MetaKind
forall a. Bounded a => a
minBound .. MetaKind
forall a. Bounded a => a
maxBound]
data KeepMetas = KeepMetas | RollBackMetas
class ( MonadConstraint m
, MonadReduce m
, MonadAddContext m
, MonadTCEnv m
, ReadTCState m
, HasBuiltins m
, HasConstInfo m
, MonadDebug m
) => MonadMetaSolver m where
newMeta' :: MetaInstantiation -> Frozen -> MetaInfo -> MetaPriority -> Permutation ->
Judgement a -> m MetaId
assignV :: CompareDirection -> MetaId -> Args -> Term -> CompareAs -> m ()
assignTerm' :: MonadMetaSolver m => MetaId -> [Arg ArgName] -> Term -> m ()
etaExpandMeta :: [MetaKind] -> MetaId -> m ()
updateMetaVar :: MetaId -> (MetaVariable -> MetaVariable) -> m ()
speculateMetas :: m () -> m KeepMetas -> m ()
dontAssignMetas :: (MonadTCEnv m, HasOptions m, MonadDebug m) => m a -> m a
dontAssignMetas :: m a -> m a
dontAssignMetas m a
cont = do
String -> Int -> String -> m ()
forall (m :: * -> *).
MonadDebug m =>
String -> Int -> String -> m ()
reportSLn String
"tc.meta" Int
45 (String -> m ()) -> String -> m ()
forall a b. (a -> b) -> a -> b
$ String
"don't assign metas"
(TCEnv -> TCEnv) -> m a -> m a
forall (m :: * -> *) a.
MonadTCEnv m =>
(TCEnv -> TCEnv) -> m a -> m a
localTC (\ TCEnv
env -> TCEnv
env { envAssignMetas :: Bool
envAssignMetas = Bool
False }) m a
cont
getMetaStore :: (ReadTCState m) => m MetaStore
getMetaStore :: m MetaStore
getMetaStore = Lens' MetaStore TCState -> m MetaStore
forall (m :: * -> *) a. ReadTCState m => Lens' a TCState -> m a
useR Lens' MetaStore TCState
stMetaStore
modifyMetaStore :: (MetaStore -> MetaStore) -> TCM ()
modifyMetaStore :: (MetaStore -> MetaStore) -> TCM ()
modifyMetaStore MetaStore -> MetaStore
f = Lens' MetaStore TCState
stMetaStore Lens' MetaStore TCState -> (MetaStore -> MetaStore) -> TCM ()
forall (m :: * -> *) a.
MonadTCState m =>
Lens' a TCState -> (a -> a) -> m ()
`modifyTCLens` MetaStore -> MetaStore
f
metasCreatedBy :: ReadTCState m => m a -> m (a, MetaStore)
metasCreatedBy :: m a -> m (a, MetaStore)
metasCreatedBy m a
m = do
!Maybe Int
largestMeta <- Maybe Int -> Maybe Int
forall a. NFData a => a -> a
force (Maybe Int -> Maybe Int)
-> (MetaStore -> Maybe Int) -> MetaStore -> Maybe Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Int, MetaVariable) -> Int)
-> Maybe (Int, MetaVariable) -> Maybe Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int, MetaVariable) -> Int
forall a b. (a, b) -> a
fst (Maybe (Int, MetaVariable) -> Maybe Int)
-> (MetaStore -> Maybe (Int, MetaVariable))
-> MetaStore
-> Maybe Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MetaStore -> Maybe (Int, MetaVariable)
forall a. IntMap a -> Maybe (Int, a)
IntMap.lookupMax (MetaStore -> Maybe Int) -> m MetaStore -> m (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
Lens' MetaStore TCState -> m MetaStore
forall (m :: * -> *) a. ReadTCState m => Lens' a TCState -> m a
useTC Lens' MetaStore TCState
stMetaStore
a
a <- m a
m
MetaStore
ms <- Lens' MetaStore TCState -> m MetaStore
forall (m :: * -> *) a. ReadTCState m => Lens' a TCState -> m a
useTC Lens' MetaStore TCState
stMetaStore
let createdMetas :: MetaStore
createdMetas = case Maybe Int
largestMeta of
Maybe Int
Nothing -> MetaStore
ms
Just Int
largestMeta -> (MetaStore, MetaStore) -> MetaStore
forall a b. (a, b) -> b
snd ((MetaStore, MetaStore) -> MetaStore)
-> (MetaStore, MetaStore) -> MetaStore
forall a b. (a -> b) -> a -> b
$ Int -> MetaStore -> (MetaStore, MetaStore)
forall a. Int -> IntMap a -> (IntMap a, IntMap a)
IntMap.split Int
largestMeta MetaStore
ms
(a, MetaStore) -> m (a, MetaStore)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, MetaStore
createdMetas)
lookupMeta' :: ReadTCState m => MetaId -> m (Maybe MetaVariable)
lookupMeta' :: MetaId -> m (Maybe MetaVariable)
lookupMeta' MetaId
m = Int -> MetaStore -> Maybe MetaVariable
forall a. Int -> IntMap a -> Maybe a
IntMap.lookup (MetaId -> Int
metaId MetaId
m) (MetaStore -> Maybe MetaVariable)
-> m MetaStore -> m (Maybe MetaVariable)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m MetaStore
forall (m :: * -> *). ReadTCState m => m MetaStore
getMetaStore
lookupMeta :: (MonadFail m, ReadTCState m) => MetaId -> m MetaVariable
lookupMeta :: MetaId -> m MetaVariable
lookupMeta MetaId
m = m MetaVariable -> m (Maybe MetaVariable) -> m MetaVariable
forall (m :: * -> *) a. Monad m => m a -> m (Maybe a) -> m a
fromMaybeM m MetaVariable
failure (m (Maybe MetaVariable) -> m MetaVariable)
-> m (Maybe MetaVariable) -> m MetaVariable
forall a b. (a -> b) -> a -> b
$ MetaId -> m (Maybe MetaVariable)
forall (m :: * -> *).
ReadTCState m =>
MetaId -> m (Maybe MetaVariable)
lookupMeta' MetaId
m
where failure :: m MetaVariable
failure = String -> m MetaVariable
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m MetaVariable) -> String -> m MetaVariable
forall a b. (a -> b) -> a -> b
$ String
"no such meta variable " String -> ShowS
forall a. [a] -> [a] -> [a]
++ MetaId -> String
forall a. Pretty a => a -> String
prettyShow MetaId
m
metaType :: (MonadFail m, ReadTCState m) => MetaId -> m Type
metaType :: MetaId -> m Type
metaType MetaId
x = Judgement MetaId -> Type
forall a. Judgement a -> Type
jMetaType (Judgement MetaId -> Type)
-> (MetaVariable -> Judgement MetaId) -> MetaVariable -> Type
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MetaVariable -> Judgement MetaId
mvJudgement (MetaVariable -> Type) -> m MetaVariable -> m Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MetaId -> m MetaVariable
forall (m :: * -> *).
(MonadFail m, ReadTCState m) =>
MetaId -> m MetaVariable
lookupMeta MetaId
x
updateMetaVarTCM :: MetaId -> (MetaVariable -> MetaVariable) -> TCM ()
updateMetaVarTCM :: MetaId -> (MetaVariable -> MetaVariable) -> TCM ()
updateMetaVarTCM MetaId
m MetaVariable -> MetaVariable
f = (MetaStore -> MetaStore) -> TCM ()
modifyMetaStore ((MetaStore -> MetaStore) -> TCM ())
-> (MetaStore -> MetaStore) -> TCM ()
forall a b. (a -> b) -> a -> b
$ (MetaVariable -> MetaVariable) -> Int -> MetaStore -> MetaStore
forall a. (a -> a) -> Int -> IntMap a -> IntMap a
IntMap.adjust MetaVariable -> MetaVariable
f (Int -> MetaStore -> MetaStore) -> Int -> MetaStore -> MetaStore
forall a b. (a -> b) -> a -> b
$ MetaId -> Int
metaId MetaId
m
insertMetaVar :: MetaId -> MetaVariable -> TCM ()
insertMetaVar :: MetaId -> MetaVariable -> TCM ()
insertMetaVar MetaId
m MetaVariable
mv = (MetaStore -> MetaStore) -> TCM ()
modifyMetaStore ((MetaStore -> MetaStore) -> TCM ())
-> (MetaStore -> MetaStore) -> TCM ()
forall a b. (a -> b) -> a -> b
$ Int -> MetaVariable -> MetaStore -> MetaStore
forall a. Int -> a -> IntMap a -> IntMap a
IntMap.insert (MetaId -> Int
metaId MetaId
m) MetaVariable
mv
getMetaPriority :: (MonadFail m, ReadTCState m) => MetaId -> m MetaPriority
getMetaPriority :: MetaId -> m MetaPriority
getMetaPriority = MetaVariable -> MetaPriority
mvPriority (MetaVariable -> MetaPriority)
-> (MetaId -> m MetaVariable) -> MetaId -> m MetaPriority
forall (m :: * -> *) b c a.
Functor m =>
(b -> c) -> (a -> m b) -> a -> m c
<.> MetaId -> m MetaVariable
forall (m :: * -> *).
(MonadFail m, ReadTCState m) =>
MetaId -> m MetaVariable
lookupMeta
isSortMeta :: (MonadFail m, ReadTCState m) => MetaId -> m Bool
isSortMeta :: MetaId -> m Bool
isSortMeta MetaId
m = MetaVariable -> Bool
isSortMeta_ (MetaVariable -> Bool) -> m MetaVariable -> m Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MetaId -> m MetaVariable
forall (m :: * -> *).
(MonadFail m, ReadTCState m) =>
MetaId -> m MetaVariable
lookupMeta MetaId
m
isSortMeta_ :: MetaVariable -> Bool
isSortMeta_ :: MetaVariable -> Bool
isSortMeta_ MetaVariable
mv = case MetaVariable -> Judgement MetaId
mvJudgement MetaVariable
mv of
HasType{} -> Bool
False
IsSort{} -> Bool
True
getMetaType :: (MonadFail m, ReadTCState m) => MetaId -> m Type
getMetaType :: MetaId -> m Type
getMetaType MetaId
m = do
MetaVariable
mv <- MetaId -> m MetaVariable
forall (m :: * -> *).
(MonadFail m, ReadTCState m) =>
MetaId -> m MetaVariable
lookupMeta MetaId
m
Type -> m Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> m Type) -> Type -> m Type
forall a b. (a -> b) -> a -> b
$ case MetaVariable -> Judgement MetaId
mvJudgement MetaVariable
mv of
HasType{ jMetaType :: forall a. Judgement a -> Type
jMetaType = Type
t } -> Type
t
IsSort{} -> Type
forall a. HasCallStack => a
__IMPOSSIBLE__
getMetaContextArgs :: MonadTCEnv m => MetaVariable -> m Args
getMetaContextArgs :: MetaVariable -> m Args
getMetaContextArgs MetaVar{ mvPermutation :: MetaVariable -> Permutation
mvPermutation = Permutation
p } = do
Args
args <- m Args
forall (m :: * -> *). (Applicative m, MonadTCEnv m) => m Args
getContextArgs
Args -> m Args
forall (m :: * -> *) a. Monad m => a -> m a
return (Args -> m Args) -> Args -> m Args
forall a b. (a -> b) -> a -> b
$ Permutation -> Args -> Args
forall a. Permutation -> [a] -> [a]
permute (Int -> Permutation -> Permutation
takeP (Args -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Args
args) Permutation
p) Args
args
getMetaTypeInContext :: (MonadFail m, MonadTCEnv m, ReadTCState m, MonadReduce m, HasBuiltins m)
=> MetaId -> m Type
getMetaTypeInContext :: MetaId -> m Type
getMetaTypeInContext MetaId
m = do
mv :: MetaVariable
mv@MetaVar{ mvJudgement :: MetaVariable -> Judgement MetaId
mvJudgement = Judgement MetaId
j } <- MetaId -> m MetaVariable
forall (m :: * -> *).
(MonadFail m, ReadTCState m) =>
MetaId -> m MetaVariable
lookupMeta MetaId
m
case Judgement MetaId
j of
HasType{ jMetaType :: forall a. Judgement a -> Type
jMetaType = Type
t } -> Type -> Args -> m Type
forall a (m :: * -> *).
(PiApplyM a, MonadReduce m, HasBuiltins m) =>
Type -> a -> m Type
piApplyM Type
t (Args -> m Type) -> m Args -> m Type
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< MetaVariable -> m Args
forall (m :: * -> *). MonadTCEnv m => MetaVariable -> m Args
getMetaContextArgs MetaVariable
mv
IsSort{} -> m Type
forall a. HasCallStack => a
__IMPOSSIBLE__
isGeneralizableMeta :: (ReadTCState m, MonadFail m) => MetaId -> m DoGeneralize
isGeneralizableMeta :: MetaId -> m DoGeneralize
isGeneralizableMeta MetaId
x = Arg DoGeneralize -> DoGeneralize
forall e. Arg e -> e
unArg (Arg DoGeneralize -> DoGeneralize)
-> (MetaVariable -> Arg DoGeneralize)
-> MetaVariable
-> DoGeneralize
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MetaInfo -> Arg DoGeneralize
miGeneralizable (MetaInfo -> Arg DoGeneralize)
-> (MetaVariable -> MetaInfo) -> MetaVariable -> Arg DoGeneralize
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MetaVariable -> MetaInfo
mvInfo (MetaVariable -> DoGeneralize) -> m MetaVariable -> m DoGeneralize
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MetaId -> m MetaVariable
forall (m :: * -> *).
(MonadFail m, ReadTCState m) =>
MetaId -> m MetaVariable
lookupMeta MetaId
x
class IsInstantiatedMeta a where
isInstantiatedMeta :: (MonadFail m, ReadTCState m) => a -> m Bool
instance IsInstantiatedMeta MetaId where
isInstantiatedMeta :: MetaId -> m Bool
isInstantiatedMeta MetaId
m = Maybe Term -> Bool
forall a. Maybe a -> Bool
isJust (Maybe Term -> Bool) -> m (Maybe Term) -> m Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MetaId -> m (Maybe Term)
forall (m :: * -> *).
(MonadFail m, ReadTCState m) =>
MetaId -> m (Maybe Term)
isInstantiatedMeta' MetaId
m
instance IsInstantiatedMeta Term where
isInstantiatedMeta :: Term -> m Bool
isInstantiatedMeta = Term -> m Bool
forall (m :: * -> *).
(MonadFail m, ReadTCState m) =>
Term -> m Bool
loop where
loop :: Term -> m Bool
loop Term
v =
case Term
v of
MetaV MetaId
x Elims
_ -> MetaId -> m Bool
forall a (m :: * -> *).
(IsInstantiatedMeta a, MonadFail m, ReadTCState m) =>
a -> m Bool
isInstantiatedMeta MetaId
x
DontCare Term
v -> Term -> m Bool
loop Term
v
Level Level
l -> Level -> m Bool
forall a (m :: * -> *).
(IsInstantiatedMeta a, MonadFail m, ReadTCState m) =>
a -> m Bool
isInstantiatedMeta Level
l
Lam ArgInfo
_ Abs Term
b -> Abs Term -> m Bool
forall a (m :: * -> *).
(IsInstantiatedMeta a, MonadFail m, ReadTCState m) =>
a -> m Bool
isInstantiatedMeta Abs Term
b
Con ConHead
_ ConInfo
_ Elims
es | Just Args
vs <- Elims -> Maybe Args
forall a. [Elim' a] -> Maybe [Arg a]
allApplyElims Elims
es -> Args -> m Bool
forall a (m :: * -> *).
(IsInstantiatedMeta a, MonadFail m, ReadTCState m) =>
a -> m Bool
isInstantiatedMeta Args
vs
Term
_ -> m Bool
forall a. HasCallStack => a
__IMPOSSIBLE__
instance IsInstantiatedMeta Level where
isInstantiatedMeta :: Level -> m Bool
isInstantiatedMeta (Max Integer
n [PlusLevel' Term]
ls) | Integer
n Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0 = [PlusLevel' Term] -> m Bool
forall a (m :: * -> *).
(IsInstantiatedMeta a, MonadFail m, ReadTCState m) =>
a -> m Bool
isInstantiatedMeta [PlusLevel' Term]
ls
isInstantiatedMeta Level
_ = m Bool
forall a. HasCallStack => a
__IMPOSSIBLE__
instance IsInstantiatedMeta PlusLevel where
isInstantiatedMeta :: PlusLevel' Term -> m Bool
isInstantiatedMeta (Plus Integer
n Term
l) | Integer
n Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0 = Term -> m Bool
forall a (m :: * -> *).
(IsInstantiatedMeta a, MonadFail m, ReadTCState m) =>
a -> m Bool
isInstantiatedMeta Term
l
isInstantiatedMeta PlusLevel' Term
_ = m Bool
forall a. HasCallStack => a
__IMPOSSIBLE__
instance IsInstantiatedMeta a => IsInstantiatedMeta [a] where
isInstantiatedMeta :: [a] -> m Bool
isInstantiatedMeta = [m Bool] -> m Bool
forall (f :: * -> *) (m :: * -> *).
(Foldable f, Monad m) =>
f (m Bool) -> m Bool
andM ([m Bool] -> m Bool) -> ([a] -> [m Bool]) -> [a] -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> m Bool) -> [a] -> [m Bool]
forall a b. (a -> b) -> [a] -> [b]
map a -> m Bool
forall a (m :: * -> *).
(IsInstantiatedMeta a, MonadFail m, ReadTCState m) =>
a -> m Bool
isInstantiatedMeta
instance IsInstantiatedMeta a => IsInstantiatedMeta (Maybe a) where
isInstantiatedMeta :: Maybe a -> m Bool
isInstantiatedMeta = [a] -> m Bool
forall a (m :: * -> *).
(IsInstantiatedMeta a, MonadFail m, ReadTCState m) =>
a -> m Bool
isInstantiatedMeta ([a] -> m Bool) -> (Maybe a -> [a]) -> Maybe a -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe a -> [a]
forall a. Maybe a -> [a]
maybeToList
instance IsInstantiatedMeta a => IsInstantiatedMeta (Arg a) where
isInstantiatedMeta :: Arg a -> m Bool
isInstantiatedMeta = a -> m Bool
forall a (m :: * -> *).
(IsInstantiatedMeta a, MonadFail m, ReadTCState m) =>
a -> m Bool
isInstantiatedMeta (a -> m Bool) -> (Arg a -> a) -> Arg a -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Arg a -> a
forall e. Arg e -> e
unArg
instance IsInstantiatedMeta a => IsInstantiatedMeta (Abs a) where
isInstantiatedMeta :: Abs a -> m Bool
isInstantiatedMeta = a -> m Bool
forall a (m :: * -> *).
(IsInstantiatedMeta a, MonadFail m, ReadTCState m) =>
a -> m Bool
isInstantiatedMeta (a -> m Bool) -> (Abs a -> a) -> Abs a -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Abs a -> a
forall a. Abs a -> a
unAbs
isInstantiatedMeta' :: (MonadFail m, ReadTCState m) => MetaId -> m (Maybe Term)
isInstantiatedMeta' :: MetaId -> m (Maybe Term)
isInstantiatedMeta' MetaId
m = do
MetaVariable
mv <- MetaId -> m MetaVariable
forall (m :: * -> *).
(MonadFail m, ReadTCState m) =>
MetaId -> m MetaVariable
lookupMeta MetaId
m
Maybe Term -> m (Maybe Term)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Term -> m (Maybe Term)) -> Maybe Term -> m (Maybe Term)
forall a b. (a -> b) -> a -> b
$ case MetaVariable -> MetaInstantiation
mvInstantiation MetaVariable
mv of
InstV [Arg String]
tel Term
v -> Term -> Maybe Term
forall a. a -> Maybe a
Just (Term -> Maybe Term) -> Term -> Maybe Term
forall a b. (a -> b) -> a -> b
$ (Arg String -> Term -> Term) -> Term -> [Arg String] -> Term
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Arg String -> Term -> Term
mkLam Term
v [Arg String]
tel
MetaInstantiation
_ -> Maybe Term
forall a. Maybe a
Nothing
constraintMetas :: Constraint -> TCM (Set MetaId)
constraintMetas :: Constraint -> TCM (Set MetaId)
constraintMetas = \case
ValueCmp Comparison
_ CompareAs
_ Term
u Term
v -> Set MetaId -> TCM (Set MetaId)
forall (m :: * -> *) a. Monad m => a -> m a
return (Set MetaId -> TCM (Set MetaId)) -> Set MetaId -> TCM (Set MetaId)
forall a b. (a -> b) -> a -> b
$ (MetaId -> Set MetaId) -> (Term, Term) -> Set MetaId
forall t m. (AllMetas t, Monoid m) => (MetaId -> m) -> t -> m
allMetas MetaId -> Set MetaId
forall a. a -> Set a
Set.singleton (Term
u, Term
v)
ValueCmpOnFace Comparison
_ Term
p Type
_ Term
u Term
v -> Set MetaId -> TCM (Set MetaId)
forall (m :: * -> *) a. Monad m => a -> m a
return (Set MetaId -> TCM (Set MetaId)) -> Set MetaId -> TCM (Set MetaId)
forall a b. (a -> b) -> a -> b
$ (MetaId -> Set MetaId) -> (Term, Term, Term) -> Set MetaId
forall t m. (AllMetas t, Monoid m) => (MetaId -> m) -> t -> m
allMetas MetaId -> Set MetaId
forall a. a -> Set a
Set.singleton (Term
p, Term
u, Term
v)
ElimCmp [Polarity]
_ [IsForced]
_ Type
_ Term
_ Elims
es Elims
es' -> Set MetaId -> TCM (Set MetaId)
forall (m :: * -> *) a. Monad m => a -> m a
return (Set MetaId -> TCM (Set MetaId)) -> Set MetaId -> TCM (Set MetaId)
forall a b. (a -> b) -> a -> b
$ (MetaId -> Set MetaId) -> (Elims, Elims) -> Set MetaId
forall t m. (AllMetas t, Monoid m) => (MetaId -> m) -> t -> m
allMetas MetaId -> Set MetaId
forall a. a -> Set a
Set.singleton (Elims
es, Elims
es')
LevelCmp Comparison
_ Level
l Level
l' -> Set MetaId -> TCM (Set MetaId)
forall (m :: * -> *) a. Monad m => a -> m a
return (Set MetaId -> TCM (Set MetaId)) -> Set MetaId -> TCM (Set MetaId)
forall a b. (a -> b) -> a -> b
$ (MetaId -> Set MetaId) -> (Term, Term) -> Set MetaId
forall t m. (AllMetas t, Monoid m) => (MetaId -> m) -> t -> m
allMetas MetaId -> Set MetaId
forall a. a -> Set a
Set.singleton (Level -> Term
Level Level
l, Level -> Term
Level Level
l')
UnquoteTactic Term
t Term
h Type
g -> Set MetaId -> TCM (Set MetaId)
forall (m :: * -> *) a. Monad m => a -> m a
return (Set MetaId -> TCM (Set MetaId)) -> Set MetaId -> TCM (Set MetaId)
forall a b. (a -> b) -> a -> b
$ (MetaId -> Set MetaId) -> (Term, Term, Type) -> Set MetaId
forall t m. (AllMetas t, Monoid m) => (MetaId -> m) -> t -> m
allMetas MetaId -> Set MetaId
forall a. a -> Set a
Set.singleton (Term
t, Term
h, Type
g)
SortCmp Comparison
_ Sort
s1 Sort
s2 -> Set MetaId -> TCM (Set MetaId)
forall (m :: * -> *) a. Monad m => a -> m a
return (Set MetaId -> TCM (Set MetaId)) -> Set MetaId -> TCM (Set MetaId)
forall a b. (a -> b) -> a -> b
$ (MetaId -> Set MetaId) -> (Term, Term) -> Set MetaId
forall t m. (AllMetas t, Monoid m) => (MetaId -> m) -> t -> m
allMetas MetaId -> Set MetaId
forall a. a -> Set a
Set.singleton (Sort -> Term
Sort Sort
s1, Sort -> Term
Sort Sort
s2)
UnBlock MetaId
x -> MetaId -> Set MetaId -> Set MetaId
forall a. Ord a => a -> Set a -> Set a
Set.insert MetaId
x (Set MetaId -> Set MetaId)
-> ([Set MetaId] -> Set MetaId) -> [Set MetaId] -> Set MetaId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Set MetaId] -> Set MetaId
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions ([Set MetaId] -> Set MetaId)
-> TCMT IO [Set MetaId] -> TCM (Set MetaId)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Listener -> TCM (Set MetaId))
-> [Listener] -> TCMT IO [Set MetaId]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Listener -> TCM (Set MetaId)
listenerMetas ([Listener] -> TCMT IO [Set MetaId])
-> TCMT IO [Listener] -> TCMT IO [Set MetaId]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< MetaId -> TCMT IO [Listener]
forall (m :: * -> *).
(MonadFail m, ReadTCState m) =>
MetaId -> m [Listener]
getMetaListeners MetaId
x)
FindInstance MetaId
x Maybe [Candidate]
_ ->
TCMT IO (Maybe Term)
-> TCM (Set MetaId)
-> (Term -> TCM (Set MetaId))
-> TCM (Set MetaId)
forall (m :: * -> *) a b.
Monad m =>
m (Maybe a) -> m b -> (a -> m b) -> m b
caseMaybeM (MetaId -> TCMT IO (Maybe Term)
forall (m :: * -> *).
(MonadFail m, ReadTCState m) =>
MetaId -> m (Maybe Term)
isInstantiatedMeta' MetaId
x) (Set MetaId -> TCM (Set MetaId)
forall (m :: * -> *) a. Monad m => a -> m a
return (Set MetaId -> TCM (Set MetaId)) -> Set MetaId -> TCM (Set MetaId)
forall a b. (a -> b) -> a -> b
$ MetaId -> Set MetaId
forall a. a -> Set a
Set.singleton MetaId
x) ((Term -> TCM (Set MetaId)) -> TCM (Set MetaId))
-> (Term -> TCM (Set MetaId)) -> TCM (Set MetaId)
forall a b. (a -> b) -> a -> b
$ Set MetaId -> TCM (Set MetaId)
forall (m :: * -> *) a. Monad m => a -> m a
return (Set MetaId -> TCM (Set MetaId))
-> (Term -> Set MetaId) -> Term -> TCM (Set MetaId)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (MetaId -> Set MetaId) -> Term -> Set MetaId
forall t m. (AllMetas t, Monoid m) => (MetaId -> m) -> t -> m
allMetas MetaId -> Set MetaId
forall a. a -> Set a
Set.singleton
IsEmpty{} -> Set MetaId -> TCM (Set MetaId)
forall (m :: * -> *) a. Monad m => a -> m a
return Set MetaId
forall a. Monoid a => a
mempty
CheckFunDef{} -> Set MetaId -> TCM (Set MetaId)
forall (m :: * -> *) a. Monad m => a -> m a
return Set MetaId
forall a. Monoid a => a
mempty
CheckSizeLtSat{} -> Set MetaId -> TCM (Set MetaId)
forall (m :: * -> *) a. Monad m => a -> m a
return Set MetaId
forall a. Monoid a => a
mempty
HasBiggerSort{} -> Set MetaId -> TCM (Set MetaId)
forall (m :: * -> *) a. Monad m => a -> m a
return Set MetaId
forall a. Monoid a => a
mempty
HasPTSRule{} -> Set MetaId -> TCM (Set MetaId)
forall (m :: * -> *) a. Monad m => a -> m a
return Set MetaId
forall a. Monoid a => a
mempty
CheckMetaInst MetaId
x -> Set MetaId -> TCM (Set MetaId)
forall (m :: * -> *) a. Monad m => a -> m a
return Set MetaId
forall a. Monoid a => a
mempty
CheckType Type
t -> Set MetaId -> TCM (Set MetaId)
forall (m :: * -> *) a. Monad m => a -> m a
return (Set MetaId -> TCM (Set MetaId)) -> Set MetaId -> TCM (Set MetaId)
forall a b. (a -> b) -> a -> b
$ (MetaId -> Set MetaId) -> Type -> Set MetaId
forall t m. (AllMetas t, Monoid m) => (MetaId -> m) -> t -> m
allMetas MetaId -> Set MetaId
forall a. a -> Set a
Set.singleton Type
t
CheckLockedVars Term
a Type
b Arg Term
c Type
d -> Set MetaId -> TCM (Set MetaId)
forall (m :: * -> *) a. Monad m => a -> m a
return (Set MetaId -> TCM (Set MetaId)) -> Set MetaId -> TCM (Set MetaId)
forall a b. (a -> b) -> a -> b
$ (MetaId -> Set MetaId)
-> (Term, Type, Arg Term, Type) -> Set MetaId
forall t m. (AllMetas t, Monoid m) => (MetaId -> m) -> t -> m
allMetas MetaId -> Set MetaId
forall a. a -> Set a
Set.singleton (Term
a, Type
b, Arg Term
c, Type
d)
UsableAtModality{} -> Set MetaId -> TCM (Set MetaId)
forall (m :: * -> *) a. Monad m => a -> m a
return Set MetaId
forall a. Monoid a => a
mempty
where
listenerMetas :: Listener -> TCM (Set MetaId)
listenerMetas EtaExpand{} = Set MetaId -> TCM (Set MetaId)
forall (m :: * -> *) a. Monad m => a -> m a
return Set MetaId
forall a. Set a
Set.empty
listenerMetas (CheckConstraint Int
_ ProblemConstraint
c) = Constraint -> TCM (Set MetaId)
constraintMetas (Closure Constraint -> Constraint
forall a. Closure a -> a
clValue (Closure Constraint -> Constraint)
-> Closure Constraint -> Constraint
forall a b. (a -> b) -> a -> b
$ ProblemConstraint -> Closure Constraint
theConstraint ProblemConstraint
c)
createMetaInfo :: (MonadTCEnv m, ReadTCState m) => m MetaInfo
createMetaInfo :: m MetaInfo
createMetaInfo = RunMetaOccursCheck -> m MetaInfo
forall (m :: * -> *).
(MonadTCEnv m, ReadTCState m) =>
RunMetaOccursCheck -> m MetaInfo
createMetaInfo' RunMetaOccursCheck
RunMetaOccursCheck
createMetaInfo'
:: (MonadTCEnv m, ReadTCState m)
=> RunMetaOccursCheck -> m MetaInfo
createMetaInfo' :: RunMetaOccursCheck -> m MetaInfo
createMetaInfo' RunMetaOccursCheck
b = do
Range
r <- m Range
forall (m :: * -> *). MonadTCEnv m => m Range
getCurrentRange
Closure Range
cl <- Range -> m (Closure Range)
forall (m :: * -> *) a.
(MonadTCEnv m, ReadTCState m) =>
a -> m (Closure a)
buildClosure Range
r
DoGeneralize
gen <- Lens' DoGeneralize TCEnv -> m DoGeneralize
forall (m :: * -> *) a. MonadTCEnv m => Lens' a TCEnv -> m a
viewTC Lens' DoGeneralize TCEnv
eGeneralizeMetas
Modality
modality <- Lens' Modality TCEnv -> m Modality
forall (m :: * -> *) a. MonadTCEnv m => Lens' a TCEnv -> m a
viewTC Lens' Modality TCEnv
eModality
MetaInfo -> m MetaInfo
forall (m :: * -> *) a. Monad m => a -> m a
return MetaInfo :: Closure Range
-> Modality
-> RunMetaOccursCheck
-> String
-> Arg DoGeneralize
-> MetaInfo
MetaInfo
{ miClosRange :: Closure Range
miClosRange = Closure Range
cl
, miModality :: Modality
miModality = Modality
modality
, miMetaOccursCheck :: RunMetaOccursCheck
miMetaOccursCheck = RunMetaOccursCheck
b
, miNameSuggestion :: String
miNameSuggestion = String
""
, miGeneralizable :: Arg DoGeneralize
miGeneralizable = DoGeneralize -> Arg DoGeneralize
forall a. a -> Arg a
defaultArg DoGeneralize
gen
}
setValueMetaName :: MonadMetaSolver m => Term -> MetaNameSuggestion -> m ()
setValueMetaName :: Term -> String -> m ()
setValueMetaName Term
v String
s = do
case Term
v of
MetaV MetaId
mi Elims
_ -> MetaId -> String -> m ()
forall (m :: * -> *). MonadMetaSolver m => MetaId -> String -> m ()
setMetaNameSuggestion MetaId
mi String
s
Term
u -> do
String -> Int -> String -> m ()
forall (m :: * -> *).
MonadDebug m =>
String -> Int -> String -> m ()
reportSLn String
"tc.meta.name" Int
70 (String -> m ()) -> String -> m ()
forall a b. (a -> b) -> a -> b
$
String
"cannot set meta name; newMeta returns " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Term -> String
forall a. Show a => a -> String
show Term
u
() -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
getMetaNameSuggestion :: (MonadFail m, ReadTCState m) => MetaId -> m MetaNameSuggestion
getMetaNameSuggestion :: MetaId -> m String
getMetaNameSuggestion MetaId
mi = MetaInfo -> String
miNameSuggestion (MetaInfo -> String)
-> (MetaVariable -> MetaInfo) -> MetaVariable -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MetaVariable -> MetaInfo
mvInfo (MetaVariable -> String) -> m MetaVariable -> m String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MetaId -> m MetaVariable
forall (m :: * -> *).
(MonadFail m, ReadTCState m) =>
MetaId -> m MetaVariable
lookupMeta MetaId
mi
setMetaNameSuggestion :: MonadMetaSolver m => MetaId -> MetaNameSuggestion -> m ()
setMetaNameSuggestion :: MetaId -> String -> m ()
setMetaNameSuggestion MetaId
mi String
s = Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (String -> Bool
forall a. Null a => a -> Bool
null String
s Bool -> Bool -> Bool
|| String -> Bool
forall a. Underscore a => a -> Bool
isUnderscore String
s) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ do
String -> Int -> String -> m ()
forall (m :: * -> *).
MonadDebug m =>
String -> Int -> String -> m ()
reportSLn String
"tc.meta.name" Int
20 (String -> m ()) -> String -> m ()
forall a b. (a -> b) -> a -> b
$
String
"setting name of meta " String -> ShowS
forall a. [a] -> [a] -> [a]
++ MetaId -> String
forall a. Pretty a => a -> String
prettyShow MetaId
mi String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" to " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
s
MetaId -> (MetaVariable -> MetaVariable) -> m ()
forall (m :: * -> *).
MonadMetaSolver m =>
MetaId -> (MetaVariable -> MetaVariable) -> m ()
updateMetaVar MetaId
mi ((MetaVariable -> MetaVariable) -> m ())
-> (MetaVariable -> MetaVariable) -> m ()
forall a b. (a -> b) -> a -> b
$ \ MetaVariable
mvar ->
MetaVariable
mvar { mvInfo :: MetaInfo
mvInfo = (MetaVariable -> MetaInfo
mvInfo MetaVariable
mvar) { miNameSuggestion :: String
miNameSuggestion = String
s }}
setMetaGeneralizableArgInfo :: MonadMetaSolver m => MetaId -> ArgInfo -> m ()
setMetaGeneralizableArgInfo :: MetaId -> ArgInfo -> m ()
setMetaGeneralizableArgInfo MetaId
m ArgInfo
i = MetaId -> (MetaVariable -> MetaVariable) -> m ()
forall (m :: * -> *).
MonadMetaSolver m =>
MetaId -> (MetaVariable -> MetaVariable) -> m ()
updateMetaVar MetaId
m ((MetaVariable -> MetaVariable) -> m ())
-> (MetaVariable -> MetaVariable) -> m ()
forall a b. (a -> b) -> a -> b
$ \ MetaVariable
mv ->
MetaVariable
mv { mvInfo :: MetaInfo
mvInfo = (MetaVariable -> MetaInfo
mvInfo MetaVariable
mv)
{ miGeneralizable :: Arg DoGeneralize
miGeneralizable = ArgInfo -> Arg DoGeneralize -> Arg DoGeneralize
forall a. LensArgInfo a => ArgInfo -> a -> a
setArgInfo ArgInfo
i (MetaInfo -> Arg DoGeneralize
miGeneralizable (MetaVariable -> MetaInfo
mvInfo MetaVariable
mv)) } }
updateMetaVarRange :: MonadMetaSolver m => MetaId -> Range -> m ()
updateMetaVarRange :: MetaId -> Range -> m ()
updateMetaVarRange MetaId
mi Range
r = MetaId -> (MetaVariable -> MetaVariable) -> m ()
forall (m :: * -> *).
MonadMetaSolver m =>
MetaId -> (MetaVariable -> MetaVariable) -> m ()
updateMetaVar MetaId
mi (Range -> MetaVariable -> MetaVariable
forall a. SetRange a => Range -> a -> a
setRange Range
r)
setMetaOccursCheck :: MonadMetaSolver m => MetaId -> RunMetaOccursCheck -> m ()
setMetaOccursCheck :: MetaId -> RunMetaOccursCheck -> m ()
setMetaOccursCheck MetaId
mi RunMetaOccursCheck
b = MetaId -> (MetaVariable -> MetaVariable) -> m ()
forall (m :: * -> *).
MonadMetaSolver m =>
MetaId -> (MetaVariable -> MetaVariable) -> m ()
updateMetaVar MetaId
mi ((MetaVariable -> MetaVariable) -> m ())
-> (MetaVariable -> MetaVariable) -> m ()
forall a b. (a -> b) -> a -> b
$ \ MetaVariable
mvar ->
MetaVariable
mvar { mvInfo :: MetaInfo
mvInfo = (MetaVariable -> MetaInfo
mvInfo MetaVariable
mvar) { miMetaOccursCheck :: RunMetaOccursCheck
miMetaOccursCheck = RunMetaOccursCheck
b } }
class (MonadTCEnv m, ReadTCState m) => MonadInteractionPoints m where
freshInteractionId :: m InteractionId
default freshInteractionId
:: (MonadTrans t, MonadInteractionPoints n, t n ~ m)
=> m InteractionId
freshInteractionId = n InteractionId -> t n InteractionId
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift n InteractionId
forall (m :: * -> *). MonadInteractionPoints m => m InteractionId
freshInteractionId
modifyInteractionPoints :: (InteractionPoints -> InteractionPoints) -> m ()
default modifyInteractionPoints
:: (MonadTrans t, MonadInteractionPoints n, t n ~ m)
=> (InteractionPoints -> InteractionPoints) -> m ()
modifyInteractionPoints = n () -> t n ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (n () -> t n ())
-> ((InteractionPoints -> InteractionPoints) -> n ())
-> (InteractionPoints -> InteractionPoints)
-> t n ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (InteractionPoints -> InteractionPoints) -> n ()
forall (m :: * -> *).
MonadInteractionPoints m =>
(InteractionPoints -> InteractionPoints) -> m ()
modifyInteractionPoints
instance MonadInteractionPoints m => MonadInteractionPoints (IdentityT m)
instance MonadInteractionPoints m => MonadInteractionPoints (ReaderT r m)
instance MonadInteractionPoints m => MonadInteractionPoints (StateT s m)
instance MonadInteractionPoints TCM where
freshInteractionId :: TCM InteractionId
freshInteractionId = TCM InteractionId
forall i (m :: * -> *). MonadFresh i m => m i
fresh
modifyInteractionPoints :: (InteractionPoints -> InteractionPoints) -> TCM ()
modifyInteractionPoints InteractionPoints -> InteractionPoints
f = Lens' InteractionPoints TCState
stInteractionPoints Lens' InteractionPoints TCState
-> (InteractionPoints -> InteractionPoints) -> TCM ()
forall (m :: * -> *) a.
MonadTCState m =>
Lens' a TCState -> (a -> a) -> m ()
`modifyTCLens` InteractionPoints -> InteractionPoints
f
registerInteractionPoint
:: forall m. MonadInteractionPoints m
=> Bool -> Range -> Maybe Nat -> m InteractionId
registerInteractionPoint :: Bool -> Range -> Maybe Int -> m InteractionId
registerInteractionPoint Bool
preciseRange Range
r Maybe Int
maybeId = do
InteractionPoints
m <- Lens' InteractionPoints TCState -> m InteractionPoints
forall (m :: * -> *) a. ReadTCState m => Lens' a TCState -> m a
useR Lens' InteractionPoints TCState
stInteractionPoints
if Bool -> Bool
not Bool
preciseRange Bool -> Bool -> Bool
|| Maybe Int -> Bool
forall a. Maybe a -> Bool
isJust Maybe Int
maybeId then InteractionPoints -> m InteractionId
continue InteractionPoints
m else do
Maybe AbsolutePath
-> m InteractionId
-> (AbsolutePath -> m InteractionId)
-> m InteractionId
forall a b. Maybe a -> b -> (a -> b) -> b
Strict.caseMaybe (Range -> Maybe AbsolutePath
rangeFile Range
r) (InteractionPoints -> m InteractionId
continue InteractionPoints
m) ((AbsolutePath -> m InteractionId) -> m InteractionId)
-> (AbsolutePath -> m InteractionId) -> m InteractionId
forall a b. (a -> b) -> a -> b
$ \ AbsolutePath
_ -> do
Maybe InteractionId
-> m InteractionId
-> (InteractionId -> m InteractionId)
-> m InteractionId
forall a b. Maybe a -> b -> (a -> b) -> b
caseMaybe (Range -> InteractionPoints -> Maybe InteractionId
findInteractionPoint_ Range
r InteractionPoints
m) (InteractionPoints -> m InteractionId
continue InteractionPoints
m) InteractionId -> m InteractionId
forall (m :: * -> *) a. Monad m => a -> m a
return
where
continue :: InteractionPoints -> m InteractionId
continue :: InteractionPoints -> m InteractionId
continue InteractionPoints
m = do
InteractionId
ii <- case Maybe Int
maybeId of
Just Int
i -> InteractionId -> m InteractionId
forall (m :: * -> *) a. Monad m => a -> m a
return (InteractionId -> m InteractionId)
-> InteractionId -> m InteractionId
forall a b. (a -> b) -> a -> b
$ Int -> InteractionId
InteractionId Int
i
Maybe Int
Nothing -> m InteractionId
forall (m :: * -> *). MonadInteractionPoints m => m InteractionId
freshInteractionId
let ip :: InteractionPoint
ip = InteractionPoint :: Range -> Maybe MetaId -> Bool -> IPClause -> InteractionPoint
InteractionPoint { ipRange :: Range
ipRange = Range
r, ipMeta :: Maybe MetaId
ipMeta = Maybe MetaId
forall a. Maybe a
Nothing, ipSolved :: Bool
ipSolved = Bool
False, ipClause :: IPClause
ipClause = IPClause
IPNoClause }
case (InteractionId
-> InteractionPoint -> InteractionPoint -> InteractionPoint)
-> InteractionId
-> InteractionPoint
-> InteractionPoints
-> (Maybe InteractionPoint, InteractionPoints)
forall k v.
(Ord k, Ord (Tag v), HasTag v) =>
(k -> v -> v -> v) -> k -> v -> BiMap k v -> (Maybe v, BiMap k v)
BiMap.insertLookupWithKey (\ InteractionId
key InteractionPoint
new InteractionPoint
old -> InteractionPoint
old) InteractionId
ii InteractionPoint
ip InteractionPoints
m of
(Just InteractionPoint
ip0, InteractionPoints
_)
| InteractionPoint -> Range
ipRange InteractionPoint
ip Range -> Range -> Bool
forall a. Eq a => a -> a -> Bool
/= InteractionPoint -> Range
ipRange InteractionPoint
ip0 -> m InteractionId
forall a. HasCallStack => a
__IMPOSSIBLE__
| Bool
otherwise -> InteractionId -> m InteractionId
forall (m :: * -> *) a. Monad m => a -> m a
return InteractionId
ii
(Maybe InteractionPoint
Nothing, InteractionPoints
m') -> do
(InteractionPoints -> InteractionPoints) -> m ()
forall (m :: * -> *).
MonadInteractionPoints m =>
(InteractionPoints -> InteractionPoints) -> m ()
modifyInteractionPoints (InteractionPoints -> InteractionPoints -> InteractionPoints
forall a b. a -> b -> a
const InteractionPoints
m')
InteractionId -> m InteractionId
forall (m :: * -> *) a. Monad m => a -> m a
return InteractionId
ii
findInteractionPoint_ :: Range -> InteractionPoints -> Maybe InteractionId
findInteractionPoint_ :: Range -> InteractionPoints -> Maybe InteractionId
findInteractionPoint_ Range
r InteractionPoints
m = do
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Maybe ()) -> Bool -> Maybe ()
forall a b. (a -> b) -> a -> b
$ Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Range -> Bool
forall a. Null a => a -> Bool
null Range
r
[InteractionId] -> Maybe InteractionId
forall a. [a] -> Maybe a
listToMaybe ([InteractionId] -> Maybe InteractionId)
-> [InteractionId] -> Maybe InteractionId
forall a b. (a -> b) -> a -> b
$ ((InteractionId, InteractionPoint) -> Maybe InteractionId)
-> [(InteractionId, InteractionPoint)] -> [InteractionId]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (InteractionId, InteractionPoint) -> Maybe InteractionId
sameRange ([(InteractionId, InteractionPoint)] -> [InteractionId])
-> [(InteractionId, InteractionPoint)] -> [InteractionId]
forall a b. (a -> b) -> a -> b
$ InteractionPoints -> [(InteractionId, InteractionPoint)]
forall k v. BiMap k v -> [(k, v)]
BiMap.toList InteractionPoints
m
where
sameRange :: (InteractionId, InteractionPoint) -> Maybe InteractionId
sameRange :: (InteractionId, InteractionPoint) -> Maybe InteractionId
sameRange (InteractionId
ii, InteractionPoint Range
r' Maybe MetaId
_ Bool
False IPClause
_) | Range
r Range -> Range -> Bool
forall a. Eq a => a -> a -> Bool
== Range
r' = InteractionId -> Maybe InteractionId
forall a. a -> Maybe a
Just InteractionId
ii
sameRange (InteractionId, InteractionPoint)
_ = Maybe InteractionId
forall a. Maybe a
Nothing
connectInteractionPoint
:: MonadInteractionPoints m
=> InteractionId -> MetaId -> m ()
connectInteractionPoint :: InteractionId -> MetaId -> m ()
connectInteractionPoint InteractionId
ii MetaId
mi = do
IPClause
ipCl <- (TCEnv -> IPClause) -> m IPClause
forall (m :: * -> *) a. MonadTCEnv m => (TCEnv -> a) -> m a
asksTC TCEnv -> IPClause
envClause
InteractionPoints
m <- Lens' InteractionPoints TCState -> m InteractionPoints
forall (m :: * -> *) a. ReadTCState m => Lens' a TCState -> m a
useR Lens' InteractionPoints TCState
stInteractionPoints
let ip :: InteractionPoint
ip = InteractionPoint :: Range -> Maybe MetaId -> Bool -> IPClause -> InteractionPoint
InteractionPoint { ipRange :: Range
ipRange = Range
forall a. HasCallStack => a
__IMPOSSIBLE__, ipMeta :: Maybe MetaId
ipMeta = MetaId -> Maybe MetaId
forall a. a -> Maybe a
Just MetaId
mi, ipSolved :: Bool
ipSolved = Bool
False, ipClause :: IPClause
ipClause = IPClause
ipCl }
case (InteractionId
-> InteractionPoint -> InteractionPoint -> InteractionPoint)
-> InteractionId
-> InteractionPoint
-> InteractionPoints
-> (Maybe InteractionPoint, InteractionPoints)
forall k v.
(Ord k, Ord (Tag v), HasTag v) =>
(k -> v -> v -> v) -> k -> v -> BiMap k v -> (Maybe v, BiMap k v)
BiMap.insertLookupWithKey (\ InteractionId
key InteractionPoint
new InteractionPoint
old -> InteractionPoint
new { ipRange :: Range
ipRange = InteractionPoint -> Range
ipRange InteractionPoint
old }) InteractionId
ii InteractionPoint
ip InteractionPoints
m of
(Maybe InteractionPoint
Nothing, InteractionPoints
_) -> m ()
forall a. HasCallStack => a
__IMPOSSIBLE__
(Just InteractionPoint
_, InteractionPoints
m') -> (InteractionPoints -> InteractionPoints) -> m ()
forall (m :: * -> *).
MonadInteractionPoints m =>
(InteractionPoints -> InteractionPoints) -> m ()
modifyInteractionPoints ((InteractionPoints -> InteractionPoints) -> m ())
-> (InteractionPoints -> InteractionPoints) -> m ()
forall a b. (a -> b) -> a -> b
$ InteractionPoints -> InteractionPoints -> InteractionPoints
forall a b. a -> b -> a
const InteractionPoints
m'
removeInteractionPoint :: MonadInteractionPoints m => InteractionId -> m ()
removeInteractionPoint :: InteractionId -> m ()
removeInteractionPoint InteractionId
ii =
(InteractionPoints -> InteractionPoints) -> m ()
forall (m :: * -> *).
MonadInteractionPoints m =>
(InteractionPoints -> InteractionPoints) -> m ()
modifyInteractionPoints ((InteractionPoints -> InteractionPoints) -> m ())
-> (InteractionPoints -> InteractionPoints) -> m ()
forall a b. (a -> b) -> a -> b
$ (InteractionPoint -> Maybe InteractionPoint)
-> InteractionId -> InteractionPoints -> InteractionPoints
forall k v.
(Ord k, Ord (Tag v), HasTag v) =>
(v -> Maybe v) -> k -> BiMap k v -> BiMap k v
BiMap.update (\ InteractionPoint
ip -> InteractionPoint -> Maybe InteractionPoint
forall a. a -> Maybe a
Just InteractionPoint
ip{ ipSolved :: Bool
ipSolved = Bool
True }) InteractionId
ii
{-# SPECIALIZE getInteractionPoints :: TCM [InteractionId] #-}
getInteractionPoints :: ReadTCState m => m [InteractionId]
getInteractionPoints :: m [InteractionId]
getInteractionPoints = InteractionPoints -> [InteractionId]
forall k v. BiMap k v -> [k]
BiMap.keys (InteractionPoints -> [InteractionId])
-> m InteractionPoints -> m [InteractionId]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Lens' InteractionPoints TCState -> m InteractionPoints
forall (m :: * -> *) a. ReadTCState m => Lens' a TCState -> m a
useR Lens' InteractionPoints TCState
stInteractionPoints
getInteractionMetas :: ReadTCState m => m [MetaId]
getInteractionMetas :: m [MetaId]
getInteractionMetas =
(InteractionPoint -> Maybe MetaId)
-> [InteractionPoint] -> [MetaId]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe InteractionPoint -> Maybe MetaId
ipMeta ([InteractionPoint] -> [MetaId])
-> (InteractionPoints -> [InteractionPoint])
-> InteractionPoints
-> [MetaId]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (InteractionPoint -> Bool)
-> [InteractionPoint] -> [InteractionPoint]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool)
-> (InteractionPoint -> Bool) -> InteractionPoint -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. InteractionPoint -> Bool
ipSolved) ([InteractionPoint] -> [InteractionPoint])
-> (InteractionPoints -> [InteractionPoint])
-> InteractionPoints
-> [InteractionPoint]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. InteractionPoints -> [InteractionPoint]
forall k v. BiMap k v -> [v]
BiMap.elems (InteractionPoints -> [MetaId])
-> m InteractionPoints -> m [MetaId]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Lens' InteractionPoints TCState -> m InteractionPoints
forall (m :: * -> *) a. ReadTCState m => Lens' a TCState -> m a
useR Lens' InteractionPoints TCState
stInteractionPoints
getUniqueMetasRanges :: (MonadFail m, ReadTCState m) => [MetaId] -> m [Range]
getUniqueMetasRanges :: [MetaId] -> m [Range]
getUniqueMetasRanges = ([Range] -> [Range]) -> m [Range] -> m [Range]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Range -> Range) -> [Range] -> [Range]
forall b a. Ord b => (a -> b) -> [a] -> [a]
nubOn Range -> Range
forall a. a -> a
id) (m [Range] -> m [Range])
-> ([MetaId] -> m [Range]) -> [MetaId] -> m [Range]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (MetaId -> m Range) -> [MetaId] -> m [Range]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM MetaId -> m Range
forall (m :: * -> *).
(MonadFail m, ReadTCState m) =>
MetaId -> m Range
getMetaRange
getUnsolvedMetas :: (MonadFail m, ReadTCState m) => m [Range]
getUnsolvedMetas :: m [Range]
getUnsolvedMetas = do
[MetaId]
openMetas <- m [MetaId]
forall (m :: * -> *). ReadTCState m => m [MetaId]
getOpenMetas
[MetaId]
interactionMetas <- m [MetaId]
forall (m :: * -> *). ReadTCState m => m [MetaId]
getInteractionMetas
[MetaId] -> m [Range]
forall (m :: * -> *).
(MonadFail m, ReadTCState m) =>
[MetaId] -> m [Range]
getUniqueMetasRanges ([MetaId]
openMetas [MetaId] -> [MetaId] -> [MetaId]
forall a. Eq a => [a] -> [a] -> [a]
List.\\ [MetaId]
interactionMetas)
getUnsolvedInteractionMetas :: (MonadFail m, ReadTCState m) => m [Range]
getUnsolvedInteractionMetas :: m [Range]
getUnsolvedInteractionMetas = [MetaId] -> m [Range]
forall (m :: * -> *).
(MonadFail m, ReadTCState m) =>
[MetaId] -> m [Range]
getUniqueMetasRanges ([MetaId] -> m [Range]) -> m [MetaId] -> m [Range]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m [MetaId]
forall (m :: * -> *). ReadTCState m => m [MetaId]
getInteractionMetas
getInteractionIdsAndMetas :: ReadTCState m => m [(InteractionId,MetaId)]
getInteractionIdsAndMetas :: m [(InteractionId, MetaId)]
getInteractionIdsAndMetas =
((InteractionId, InteractionPoint)
-> Maybe (InteractionId, MetaId))
-> [(InteractionId, InteractionPoint)] -> [(InteractionId, MetaId)]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (InteractionId, InteractionPoint) -> Maybe (InteractionId, MetaId)
forall t. (t, InteractionPoint) -> Maybe (t, MetaId)
f ([(InteractionId, InteractionPoint)] -> [(InteractionId, MetaId)])
-> (InteractionPoints -> [(InteractionId, InteractionPoint)])
-> InteractionPoints
-> [(InteractionId, MetaId)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((InteractionId, InteractionPoint) -> Bool)
-> [(InteractionId, InteractionPoint)]
-> [(InteractionId, InteractionPoint)]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool)
-> ((InteractionId, InteractionPoint) -> Bool)
-> (InteractionId, InteractionPoint)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. InteractionPoint -> Bool
ipSolved (InteractionPoint -> Bool)
-> ((InteractionId, InteractionPoint) -> InteractionPoint)
-> (InteractionId, InteractionPoint)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (InteractionId, InteractionPoint) -> InteractionPoint
forall a b. (a, b) -> b
snd) ([(InteractionId, InteractionPoint)]
-> [(InteractionId, InteractionPoint)])
-> (InteractionPoints -> [(InteractionId, InteractionPoint)])
-> InteractionPoints
-> [(InteractionId, InteractionPoint)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. InteractionPoints -> [(InteractionId, InteractionPoint)]
forall k v. BiMap k v -> [(k, v)]
BiMap.toList (InteractionPoints -> [(InteractionId, MetaId)])
-> m InteractionPoints -> m [(InteractionId, MetaId)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Lens' InteractionPoints TCState -> m InteractionPoints
forall (m :: * -> *) a. ReadTCState m => Lens' a TCState -> m a
useR Lens' InteractionPoints TCState
stInteractionPoints
where f :: (t, InteractionPoint) -> Maybe (t, MetaId)
f (t
ii, InteractionPoint
ip) = (t
ii,) (MetaId -> (t, MetaId)) -> Maybe MetaId -> Maybe (t, MetaId)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> InteractionPoint -> Maybe MetaId
ipMeta InteractionPoint
ip
isInteractionMeta :: ReadTCState m => MetaId -> m (Maybe InteractionId)
isInteractionMeta :: MetaId -> m (Maybe InteractionId)
isInteractionMeta MetaId
x = Tag InteractionPoint -> InteractionPoints -> Maybe InteractionId
forall v k. Ord (Tag v) => Tag v -> BiMap k v -> Maybe k
BiMap.invLookup Tag InteractionPoint
MetaId
x (InteractionPoints -> Maybe InteractionId)
-> m InteractionPoints -> m (Maybe InteractionId)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Lens' InteractionPoints TCState -> m InteractionPoints
forall (m :: * -> *) a. ReadTCState m => Lens' a TCState -> m a
useR Lens' InteractionPoints TCState
stInteractionPoints
{-# SPECIALIZE lookupInteractionPoint :: InteractionId -> TCM InteractionPoint #-}
lookupInteractionPoint
:: (MonadFail m, ReadTCState m, MonadError TCErr m)
=> InteractionId -> m InteractionPoint
lookupInteractionPoint :: InteractionId -> m InteractionPoint
lookupInteractionPoint InteractionId
ii =
m InteractionPoint
-> m (Maybe InteractionPoint) -> m InteractionPoint
forall (m :: * -> *) a. Monad m => m a -> m (Maybe a) -> m a
fromMaybeM m InteractionPoint
err (m (Maybe InteractionPoint) -> m InteractionPoint)
-> m (Maybe InteractionPoint) -> m InteractionPoint
forall a b. (a -> b) -> a -> b
$ InteractionId -> InteractionPoints -> Maybe InteractionPoint
forall k v. Ord k => k -> BiMap k v -> Maybe v
BiMap.lookup InteractionId
ii (InteractionPoints -> Maybe InteractionPoint)
-> m InteractionPoints -> m (Maybe InteractionPoint)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Lens' InteractionPoints TCState -> m InteractionPoints
forall (m :: * -> *) a. ReadTCState m => Lens' a TCState -> m a
useR Lens' InteractionPoints TCState
stInteractionPoints
where
err :: m InteractionPoint
err = String -> m InteractionPoint
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m InteractionPoint) -> String -> m InteractionPoint
forall a b. (a -> b) -> a -> b
$ String
"no such interaction point: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ InteractionId -> String
forall a. Show a => a -> String
show InteractionId
ii
lookupInteractionId
:: (MonadFail m, ReadTCState m, MonadError TCErr m, MonadTCEnv m)
=> InteractionId -> m MetaId
lookupInteractionId :: InteractionId -> m MetaId
lookupInteractionId InteractionId
ii = m MetaId -> m (Maybe MetaId) -> m MetaId
forall (m :: * -> *) a. Monad m => m a -> m (Maybe a) -> m a
fromMaybeM m MetaId
err2 (m (Maybe MetaId) -> m MetaId) -> m (Maybe MetaId) -> m MetaId
forall a b. (a -> b) -> a -> b
$ InteractionPoint -> Maybe MetaId
ipMeta (InteractionPoint -> Maybe MetaId)
-> m InteractionPoint -> m (Maybe MetaId)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> InteractionId -> m InteractionPoint
forall (m :: * -> *).
(MonadFail m, ReadTCState m, MonadError TCErr m) =>
InteractionId -> m InteractionPoint
lookupInteractionPoint InteractionId
ii
where
err2 :: m MetaId
err2 = TypeError -> m MetaId
forall (m :: * -> *) a.
(HasCallStack, MonadTCError m) =>
TypeError -> m a
typeError (TypeError -> m MetaId) -> TypeError -> m MetaId
forall a b. (a -> b) -> a -> b
$ String -> TypeError
GenericError (String -> TypeError) -> String -> TypeError
forall a b. (a -> b) -> a -> b
$ String
"No type nor action available for hole " String -> ShowS
forall a. [a] -> [a] -> [a]
++ InteractionId -> String
forall a. Pretty a => a -> String
prettyShow InteractionId
ii String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
". Possible cause: the hole has not been reached during type checking (do you see yellow?)"
lookupInteractionMeta :: ReadTCState m => InteractionId -> m (Maybe MetaId)
lookupInteractionMeta :: InteractionId -> m (Maybe MetaId)
lookupInteractionMeta InteractionId
ii = InteractionId -> InteractionPoints -> Maybe MetaId
lookupInteractionMeta_ InteractionId
ii (InteractionPoints -> Maybe MetaId)
-> m InteractionPoints -> m (Maybe MetaId)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Lens' InteractionPoints TCState -> m InteractionPoints
forall (m :: * -> *) a. ReadTCState m => Lens' a TCState -> m a
useR Lens' InteractionPoints TCState
stInteractionPoints
lookupInteractionMeta_ :: InteractionId -> InteractionPoints -> Maybe MetaId
lookupInteractionMeta_ :: InteractionId -> InteractionPoints -> Maybe MetaId
lookupInteractionMeta_ InteractionId
ii InteractionPoints
m = InteractionPoint -> Maybe MetaId
ipMeta (InteractionPoint -> Maybe MetaId)
-> Maybe InteractionPoint -> Maybe MetaId
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< InteractionId -> InteractionPoints -> Maybe InteractionPoint
forall k v. Ord k => k -> BiMap k v -> Maybe v
BiMap.lookup InteractionId
ii InteractionPoints
m
newMeta :: MonadMetaSolver m => Frozen -> MetaInfo -> MetaPriority -> Permutation -> Judgement a -> m MetaId
newMeta :: Frozen
-> MetaInfo
-> MetaPriority
-> Permutation
-> Judgement a
-> m MetaId
newMeta = MetaInstantiation
-> Frozen
-> MetaInfo
-> MetaPriority
-> Permutation
-> Judgement a
-> m MetaId
forall (m :: * -> *) a.
MonadMetaSolver m =>
MetaInstantiation
-> Frozen
-> MetaInfo
-> MetaPriority
-> Permutation
-> Judgement a
-> m MetaId
newMeta' MetaInstantiation
Open
newMetaTCM' :: MetaInstantiation -> Frozen -> MetaInfo -> MetaPriority -> Permutation ->
Judgement a -> TCM MetaId
newMetaTCM' :: MetaInstantiation
-> Frozen
-> MetaInfo
-> MetaPriority
-> Permutation
-> Judgement a
-> TCM MetaId
newMetaTCM' MetaInstantiation
inst Frozen
frozen MetaInfo
mi MetaPriority
p Permutation
perm Judgement a
j = do
MetaId
x <- TCM MetaId
forall i (m :: * -> *). MonadFresh i m => m i
fresh
let j' :: Judgement MetaId
j' = Judgement a
j { jMetaId :: MetaId
jMetaId = MetaId
x }
mv :: MetaVariable
mv = MetaVar :: MetaInfo
-> MetaPriority
-> Permutation
-> Judgement MetaId
-> MetaInstantiation
-> Set Listener
-> Frozen
-> Maybe MetaId
-> MetaVariable
MetaVar{ mvInfo :: MetaInfo
mvInfo = MetaInfo
mi
, mvPriority :: MetaPriority
mvPriority = MetaPriority
p
, mvPermutation :: Permutation
mvPermutation = Permutation
perm
, mvJudgement :: Judgement MetaId
mvJudgement = Judgement MetaId
j'
, mvInstantiation :: MetaInstantiation
mvInstantiation = MetaInstantiation
inst
, mvListeners :: Set Listener
mvListeners = Set Listener
forall a. Set a
Set.empty
, mvFrozen :: Frozen
mvFrozen = Frozen
frozen
, mvTwin :: Maybe MetaId
mvTwin = Maybe MetaId
forall a. Maybe a
Nothing
}
MetaId -> MetaVariable -> TCM ()
insertMetaVar MetaId
x MetaVariable
mv
MetaId -> TCM MetaId
forall (m :: * -> *) a. Monad m => a -> m a
return MetaId
x
{-# SPECIALIZE getInteractionRange :: InteractionId -> TCM Range #-}
getInteractionRange
:: (MonadInteractionPoints m, MonadFail m, MonadError TCErr m)
=> InteractionId -> m Range
getInteractionRange :: InteractionId -> m Range
getInteractionRange = InteractionPoint -> Range
ipRange (InteractionPoint -> Range)
-> (InteractionId -> m InteractionPoint)
-> InteractionId
-> m Range
forall (m :: * -> *) b c a.
Functor m =>
(b -> c) -> (a -> m b) -> a -> m c
<.> InteractionId -> m InteractionPoint
forall (m :: * -> *).
(MonadFail m, ReadTCState m, MonadError TCErr m) =>
InteractionId -> m InteractionPoint
lookupInteractionPoint
getMetaRange :: (MonadFail m, ReadTCState m) => MetaId -> m Range
getMetaRange :: MetaId -> m Range
getMetaRange = MetaVariable -> Range
forall a. HasRange a => a -> Range
getRange (MetaVariable -> Range)
-> (MetaId -> m MetaVariable) -> MetaId -> m Range
forall (m :: * -> *) b c a.
Functor m =>
(b -> c) -> (a -> m b) -> a -> m c
<.> MetaId -> m MetaVariable
forall (m :: * -> *).
(MonadFail m, ReadTCState m) =>
MetaId -> m MetaVariable
lookupMeta
getInteractionScope
:: (MonadFail m, ReadTCState m, MonadError TCErr m, MonadTCEnv m)
=> InteractionId -> m ScopeInfo
getInteractionScope :: InteractionId -> m ScopeInfo
getInteractionScope = MetaVariable -> ScopeInfo
getMetaScope (MetaVariable -> ScopeInfo)
-> (MetaId -> m MetaVariable) -> MetaId -> m ScopeInfo
forall (m :: * -> *) b c a.
Functor m =>
(b -> c) -> (a -> m b) -> a -> m c
<.> MetaId -> m MetaVariable
forall (m :: * -> *).
(MonadFail m, ReadTCState m) =>
MetaId -> m MetaVariable
lookupMeta (MetaId -> m ScopeInfo)
-> (InteractionId -> m MetaId) -> InteractionId -> m ScopeInfo
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< InteractionId -> m MetaId
forall (m :: * -> *).
(MonadFail m, ReadTCState m, MonadError TCErr m, MonadTCEnv m) =>
InteractionId -> m MetaId
lookupInteractionId
withMetaInfo' :: (MonadTCEnv m, ReadTCState m, MonadTrace m) => MetaVariable -> m a -> m a
withMetaInfo' :: MetaVariable -> m a -> m a
withMetaInfo' MetaVariable
mv = Closure Range -> m a -> m a
forall (m :: * -> *) a.
(MonadTCEnv m, ReadTCState m, MonadTrace m) =>
Closure Range -> m a -> m a
withMetaInfo (MetaInfo -> Closure Range
miClosRange (MetaInfo -> Closure Range) -> MetaInfo -> Closure Range
forall a b. (a -> b) -> a -> b
$ MetaVariable -> MetaInfo
mvInfo MetaVariable
mv)
withMetaInfo :: (MonadTCEnv m, ReadTCState m, MonadTrace m) => Closure Range -> m a -> m a
withMetaInfo :: Closure Range -> m a -> m a
withMetaInfo Closure Range
mI m a
cont = Closure Range -> (Range -> m a) -> m a
forall (m :: * -> *) a c b.
(MonadTCEnv m, ReadTCState m, LensClosure a c) =>
c -> (a -> m b) -> m b
enterClosure Closure Range
mI ((Range -> m a) -> m a) -> (Range -> m a) -> m a
forall a b. (a -> b) -> a -> b
$ \ Range
r ->
Range -> m a -> m a
forall (m :: * -> *) x a.
(MonadTrace m, HasRange x) =>
x -> m a -> m a
setCurrentRange Range
r m a
cont
withInteractionId
:: (MonadFail m, ReadTCState m, MonadError TCErr m, MonadTCEnv m, MonadTrace m)
=> InteractionId -> m a -> m a
withInteractionId :: InteractionId -> m a -> m a
withInteractionId InteractionId
i m a
ret = do
MetaId
m <- InteractionId -> m MetaId
forall (m :: * -> *).
(MonadFail m, ReadTCState m, MonadError TCErr m, MonadTCEnv m) =>
InteractionId -> m MetaId
lookupInteractionId InteractionId
i
MetaId -> m a -> m a
forall (m :: * -> *) a.
(MonadFail m, MonadTCEnv m, ReadTCState m, MonadTrace m) =>
MetaId -> m a -> m a
withMetaId MetaId
m m a
ret
withMetaId
:: (MonadFail m, MonadTCEnv m, ReadTCState m, MonadTrace m)
=> MetaId -> m a -> m a
withMetaId :: MetaId -> m a -> m a
withMetaId MetaId
m m a
ret = do
MetaVariable
mv <- MetaId -> m MetaVariable
forall (m :: * -> *).
(MonadFail m, ReadTCState m) =>
MetaId -> m MetaVariable
lookupMeta MetaId
m
MetaVariable -> m a -> m a
forall (m :: * -> *) a.
(MonadTCEnv m, ReadTCState m, MonadTrace m) =>
MetaVariable -> m a -> m a
withMetaInfo' MetaVariable
mv m a
ret
getMetaVariables :: ReadTCState m => (MetaVariable -> Bool) -> m [MetaId]
getMetaVariables :: (MetaVariable -> Bool) -> m [MetaId]
getMetaVariables MetaVariable -> Bool
p = do
MetaStore
store <- m MetaStore
forall (m :: * -> *). ReadTCState m => m MetaStore
getMetaStore
[MetaId] -> m [MetaId]
forall (m :: * -> *) a. Monad m => a -> m a
return [ Int -> MetaId
MetaId Int
i | (Int
i, MetaVariable
mv) <- MetaStore -> [(Int, MetaVariable)]
forall a. IntMap a -> [(Int, a)]
IntMap.assocs MetaStore
store, MetaVariable -> Bool
p MetaVariable
mv ]
getOpenMetas :: ReadTCState m => m [MetaId]
getOpenMetas :: m [MetaId]
getOpenMetas = (MetaVariable -> Bool) -> m [MetaId]
forall (m :: * -> *).
ReadTCState m =>
(MetaVariable -> Bool) -> m [MetaId]
getMetaVariables (MetaInstantiation -> Bool
isOpenMeta (MetaInstantiation -> Bool)
-> (MetaVariable -> MetaInstantiation) -> MetaVariable -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MetaVariable -> MetaInstantiation
mvInstantiation)
isOpenMeta :: MetaInstantiation -> Bool
isOpenMeta :: MetaInstantiation -> Bool
isOpenMeta MetaInstantiation
Open = Bool
True
isOpenMeta MetaInstantiation
OpenInstance = Bool
True
isOpenMeta BlockedConst{} = Bool
True
isOpenMeta PostponedTypeCheckingProblem{} = Bool
True
isOpenMeta InstV{} = Bool
False
listenToMeta :: MonadMetaSolver m => Listener -> MetaId -> m ()
listenToMeta :: Listener -> MetaId -> m ()
listenToMeta Listener
l MetaId
m =
MetaId -> (MetaVariable -> MetaVariable) -> m ()
forall (m :: * -> *).
MonadMetaSolver m =>
MetaId -> (MetaVariable -> MetaVariable) -> m ()
updateMetaVar MetaId
m ((MetaVariable -> MetaVariable) -> m ())
-> (MetaVariable -> MetaVariable) -> m ()
forall a b. (a -> b) -> a -> b
$ \MetaVariable
mv -> MetaVariable
mv { mvListeners :: Set Listener
mvListeners = Listener -> Set Listener -> Set Listener
forall a. Ord a => a -> Set a -> Set a
Set.insert Listener
l (Set Listener -> Set Listener) -> Set Listener -> Set Listener
forall a b. (a -> b) -> a -> b
$ MetaVariable -> Set Listener
mvListeners MetaVariable
mv }
unlistenToMeta :: MonadMetaSolver m => Listener -> MetaId -> m ()
unlistenToMeta :: Listener -> MetaId -> m ()
unlistenToMeta Listener
l MetaId
m =
MetaId -> (MetaVariable -> MetaVariable) -> m ()
forall (m :: * -> *).
MonadMetaSolver m =>
MetaId -> (MetaVariable -> MetaVariable) -> m ()
updateMetaVar MetaId
m ((MetaVariable -> MetaVariable) -> m ())
-> (MetaVariable -> MetaVariable) -> m ()
forall a b. (a -> b) -> a -> b
$ \MetaVariable
mv -> MetaVariable
mv { mvListeners :: Set Listener
mvListeners = Listener -> Set Listener -> Set Listener
forall a. Ord a => a -> Set a -> Set a
Set.delete Listener
l (Set Listener -> Set Listener) -> Set Listener -> Set Listener
forall a b. (a -> b) -> a -> b
$ MetaVariable -> Set Listener
mvListeners MetaVariable
mv }
getMetaListeners :: (MonadFail m, ReadTCState m) => MetaId -> m [Listener]
getMetaListeners :: MetaId -> m [Listener]
getMetaListeners MetaId
m = Set Listener -> [Listener]
forall a. Set a -> [a]
Set.toList (Set Listener -> [Listener])
-> (MetaVariable -> Set Listener) -> MetaVariable -> [Listener]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MetaVariable -> Set Listener
mvListeners (MetaVariable -> [Listener]) -> m MetaVariable -> m [Listener]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MetaId -> m MetaVariable
forall (m :: * -> *).
(MonadFail m, ReadTCState m) =>
MetaId -> m MetaVariable
lookupMeta MetaId
m
clearMetaListeners :: MonadMetaSolver m => MetaId -> m ()
clearMetaListeners :: MetaId -> m ()
clearMetaListeners MetaId
m =
MetaId -> (MetaVariable -> MetaVariable) -> m ()
forall (m :: * -> *).
MonadMetaSolver m =>
MetaId -> (MetaVariable -> MetaVariable) -> m ()
updateMetaVar MetaId
m ((MetaVariable -> MetaVariable) -> m ())
-> (MetaVariable -> MetaVariable) -> m ()
forall a b. (a -> b) -> a -> b
$ \MetaVariable
mv -> MetaVariable
mv { mvListeners :: Set Listener
mvListeners = Set Listener
forall a. Set a
Set.empty }
freezeMetas :: MetaStore -> TCM IntSet
freezeMetas :: MetaStore -> TCM IntSet
freezeMetas MetaStore
ms =
WriterT IntSet TCM () -> TCM IntSet
forall (m :: * -> *) w a. Monad m => WriterT w m a -> m w
execWriterT (WriterT IntSet TCM () -> TCM IntSet)
-> WriterT IntSet TCM () -> TCM IntSet
forall a b. (a -> b) -> a -> b
$
Lens' MetaStore TCState
-> (MetaStore -> WriterT IntSet TCM MetaStore)
-> WriterT IntSet TCM ()
forall (m :: * -> *) a.
MonadTCState m =>
Lens' a TCState -> (a -> m a) -> m ()
modifyTCLensM Lens' MetaStore TCState
stMetaStore ((MetaStore -> WriterT IntSet TCM MetaStore)
-> WriterT IntSet TCM ())
-> (MetaStore -> WriterT IntSet TCM MetaStore)
-> WriterT IntSet TCM ()
forall a b. (a -> b) -> a -> b
$
StateT MetaStore (WriterT IntSet TCM) ()
-> MetaStore -> WriterT IntSet TCM MetaStore
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m s
execStateT ((Int -> StateT MetaStore (WriterT IntSet TCM) ())
-> [Int] -> StateT MetaStore (WriterT IntSet TCM) ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Int -> StateT MetaStore (WriterT IntSet TCM) ()
forall (m :: * -> *).
Monad m =>
Int -> StateT MetaStore (WriterT IntSet m) ()
freeze ([Int] -> StateT MetaStore (WriterT IntSet TCM) ())
-> [Int] -> StateT MetaStore (WriterT IntSet TCM) ()
forall a b. (a -> b) -> a -> b
$ MetaStore -> [Int]
forall a. IntMap a -> [Int]
IntMap.keys MetaStore
ms)
where
freeze :: Monad m => Int -> StateT MetaStore (WriterT IntSet m) ()
freeze :: Int -> StateT MetaStore (WriterT IntSet m) ()
freeze Int
m = do
MetaStore
store <- StateT MetaStore (WriterT IntSet m) MetaStore
forall s (m :: * -> *). MonadState s m => m s
get
case Int -> MetaStore -> Maybe MetaVariable
forall a. Int -> IntMap a -> Maybe a
IntMap.lookup Int
m MetaStore
store of
Just MetaVariable
mvar
| MetaVariable -> Frozen
mvFrozen MetaVariable
mvar Frozen -> Frozen -> Bool
forall a. Eq a => a -> a -> Bool
/= Frozen
Frozen -> do
WriterT IntSet m () -> StateT MetaStore (WriterT IntSet m) ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (WriterT IntSet m () -> StateT MetaStore (WriterT IntSet m) ())
-> WriterT IntSet m () -> StateT MetaStore (WriterT IntSet m) ()
forall a b. (a -> b) -> a -> b
$ IntSet -> WriterT IntSet m ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell (Int -> IntSet
IntSet.singleton Int
m)
MetaStore -> StateT MetaStore (WriterT IntSet m) ()
forall s (m :: * -> *). MonadState s m => s -> m ()
put (MetaStore -> StateT MetaStore (WriterT IntSet m) ())
-> MetaStore -> StateT MetaStore (WriterT IntSet m) ()
forall a b. (a -> b) -> a -> b
$ Int -> MetaVariable -> MetaStore -> MetaStore
forall a. Int -> a -> IntMap a -> IntMap a
IntMap.insert Int
m (MetaVariable
mvar { mvFrozen :: Frozen
mvFrozen = Frozen
Frozen }) MetaStore
store
| Bool
otherwise -> () -> StateT MetaStore (WriterT IntSet m) ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Maybe MetaVariable
Nothing -> StateT MetaStore (WriterT IntSet m) ()
forall a. HasCallStack => a
__IMPOSSIBLE__
unfreezeMetas :: TCM ()
unfreezeMetas :: TCM ()
unfreezeMetas = (MetaStore -> MetaStore) -> TCM ()
modifyMetaStore ((MetaStore -> MetaStore) -> TCM ())
-> (MetaStore -> MetaStore) -> TCM ()
forall a b. (a -> b) -> a -> b
$ (MetaVariable -> MetaVariable) -> MetaStore -> MetaStore
forall a b. (a -> b) -> IntMap a -> IntMap b
IntMap.map MetaVariable -> MetaVariable
unfreeze
where
unfreeze :: MetaVariable -> MetaVariable
unfreeze :: MetaVariable -> MetaVariable
unfreeze MetaVariable
mvar = MetaVariable
mvar { mvFrozen :: Frozen
mvFrozen = Frozen
Instantiable }
isFrozen :: (MonadFail m, ReadTCState m) => MetaId -> m Bool
isFrozen :: MetaId -> m Bool
isFrozen MetaId
x = do
MetaVariable
mvar <- MetaId -> m MetaVariable
forall (m :: * -> *).
(MonadFail m, ReadTCState m) =>
MetaId -> m MetaVariable
lookupMeta MetaId
x
Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> m Bool) -> Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ MetaVariable -> Frozen
mvFrozen MetaVariable
mvar Frozen -> Frozen -> Bool
forall a. Eq a => a -> a -> Bool
== Frozen
Frozen
class UnFreezeMeta a where
unfreezeMeta :: MonadMetaSolver m => a -> m ()
instance UnFreezeMeta MetaId where
unfreezeMeta :: MetaId -> m ()
unfreezeMeta MetaId
x = do
MetaId -> (MetaVariable -> MetaVariable) -> m ()
forall (m :: * -> *).
MonadMetaSolver m =>
MetaId -> (MetaVariable -> MetaVariable) -> m ()
updateMetaVar MetaId
x ((MetaVariable -> MetaVariable) -> m ())
-> (MetaVariable -> MetaVariable) -> m ()
forall a b. (a -> b) -> a -> b
$ \ MetaVariable
mv -> MetaVariable
mv { mvFrozen :: Frozen
mvFrozen = Frozen
Instantiable }
Type -> m ()
forall a (m :: * -> *).
(UnFreezeMeta a, MonadMetaSolver m) =>
a -> m ()
unfreezeMeta (Type -> m ()) -> m Type -> m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< MetaId -> m Type
forall (m :: * -> *).
(MonadFail m, ReadTCState m) =>
MetaId -> m Type
metaType MetaId
x
instance UnFreezeMeta Type where
unfreezeMeta :: Type -> m ()
unfreezeMeta (El Sort
s Term
t) = Sort -> m ()
forall a (m :: * -> *).
(UnFreezeMeta a, MonadMetaSolver m) =>
a -> m ()
unfreezeMeta Sort
s m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Term -> m ()
forall a (m :: * -> *).
(UnFreezeMeta a, MonadMetaSolver m) =>
a -> m ()
unfreezeMeta Term
t
instance UnFreezeMeta Term where
unfreezeMeta :: Term -> m ()
unfreezeMeta (MetaV MetaId
x Elims
_) = MetaId -> m ()
forall a (m :: * -> *).
(UnFreezeMeta a, MonadMetaSolver m) =>
a -> m ()
unfreezeMeta MetaId
x
unfreezeMeta (Sort Sort
s) = Sort -> m ()
forall a (m :: * -> *).
(UnFreezeMeta a, MonadMetaSolver m) =>
a -> m ()
unfreezeMeta Sort
s
unfreezeMeta (Level Level
l) = Level -> m ()
forall a (m :: * -> *).
(UnFreezeMeta a, MonadMetaSolver m) =>
a -> m ()
unfreezeMeta Level
l
unfreezeMeta (DontCare Term
t) = Term -> m ()
forall a (m :: * -> *).
(UnFreezeMeta a, MonadMetaSolver m) =>
a -> m ()
unfreezeMeta Term
t
unfreezeMeta (Lam ArgInfo
_ Abs Term
v) = Abs Term -> m ()
forall a (m :: * -> *).
(UnFreezeMeta a, MonadMetaSolver m) =>
a -> m ()
unfreezeMeta Abs Term
v
unfreezeMeta Term
_ = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
instance UnFreezeMeta Sort where
unfreezeMeta :: Sort -> m ()
unfreezeMeta (MetaS MetaId
x Elims
_) = MetaId -> m ()
forall a (m :: * -> *).
(UnFreezeMeta a, MonadMetaSolver m) =>
a -> m ()
unfreezeMeta MetaId
x
unfreezeMeta Sort
_ = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
instance UnFreezeMeta Level where
unfreezeMeta :: Level -> m ()
unfreezeMeta (Max Integer
_ [PlusLevel' Term]
ls) = [PlusLevel' Term] -> m ()
forall a (m :: * -> *).
(UnFreezeMeta a, MonadMetaSolver m) =>
a -> m ()
unfreezeMeta [PlusLevel' Term]
ls
instance UnFreezeMeta PlusLevel where
unfreezeMeta :: PlusLevel' Term -> m ()
unfreezeMeta (Plus Integer
_ Term
a) = Term -> m ()
forall a (m :: * -> *).
(UnFreezeMeta a, MonadMetaSolver m) =>
a -> m ()
unfreezeMeta Term
a
instance UnFreezeMeta a => UnFreezeMeta [a] where
unfreezeMeta :: [a] -> m ()
unfreezeMeta = (a -> m ()) -> [a] -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ a -> m ()
forall a (m :: * -> *).
(UnFreezeMeta a, MonadMetaSolver m) =>
a -> m ()
unfreezeMeta
instance UnFreezeMeta a => UnFreezeMeta (Abs a) where
unfreezeMeta :: Abs a -> m ()
unfreezeMeta = (a -> m ()) -> Abs a -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
Fold.mapM_ a -> m ()
forall a (m :: * -> *).
(UnFreezeMeta a, MonadMetaSolver m) =>
a -> m ()
unfreezeMeta