{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Rzk.TypeCheck.Monad where
import Control.Monad (unless)
import Control.Monad.Except (ExceptT,
MonadError (catchError, throwError),
runExceptT)
import Control.Monad.Reader (ReaderT (..), ask, asks, local)
import Control.Monad.Trans (lift)
import Control.Monad.Trans.State.Strict (State, get, modify', put,
runState)
import Debug.Trace (trace)
import Control.Monad.Foil (Distinct)
import qualified Control.Monad.Foil as Foil
import Language.Rzk.Foil.Names (VarIdent)
import Language.Rzk.Foil.Syntax (positionOfTerm)
import Rzk.TypeCheck.Context
import Rzk.TypeCheck.Display
import Rzk.TypeCheck.Error
data HoleEntry = HoleEntry
{ HoleEntry -> VarIdent
holeEntryName :: VarIdent
, HoleEntry -> Rendered
holeEntryType :: Rendered
} deriving (HoleEntry -> HoleEntry -> Bool
(HoleEntry -> HoleEntry -> Bool)
-> (HoleEntry -> HoleEntry -> Bool) -> Eq HoleEntry
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HoleEntry -> HoleEntry -> Bool
== :: HoleEntry -> HoleEntry -> Bool
$c/= :: HoleEntry -> HoleEntry -> Bool
/= :: HoleEntry -> HoleEntry -> Bool
Eq, Int -> HoleEntry -> ShowS
[HoleEntry] -> ShowS
HoleEntry -> String
(Int -> HoleEntry -> ShowS)
-> (HoleEntry -> String)
-> ([HoleEntry] -> ShowS)
-> Show HoleEntry
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HoleEntry -> ShowS
showsPrec :: Int -> HoleEntry -> ShowS
$cshow :: HoleEntry -> String
show :: HoleEntry -> String
$cshowList :: [HoleEntry] -> ShowS
showList :: [HoleEntry] -> ShowS
Show)
data HoleInfo = HoleInfo
{ HoleInfo -> Maybe VarIdent
holeName :: Maybe VarIdent
, HoleInfo -> Rendered
holeGoal :: Rendered
, HoleInfo -> Maybe (VarIdent, Rendered)
holeGoalShape :: Maybe (VarIdent, Rendered)
, HoleInfo -> [HoleEntry]
holeTermVars :: [HoleEntry]
, HoleInfo -> [HoleEntry]
holeCubeVars :: [HoleEntry]
, HoleInfo -> [Rendered]
holeTopes :: [Rendered]
, HoleInfo -> [Rendered]
holeCandidates :: [Rendered]
, HoleInfo -> [Rendered]
holeIntroductions :: [Rendered]
, HoleInfo -> Maybe String
holeDiagram :: Maybe String
, HoleInfo -> Maybe LocationInfo
holeLocation :: Maybe LocationInfo
} deriving (HoleInfo -> HoleInfo -> Bool
(HoleInfo -> HoleInfo -> Bool)
-> (HoleInfo -> HoleInfo -> Bool) -> Eq HoleInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HoleInfo -> HoleInfo -> Bool
== :: HoleInfo -> HoleInfo -> Bool
$c/= :: HoleInfo -> HoleInfo -> Bool
/= :: HoleInfo -> HoleInfo -> Bool
Eq, Int -> HoleInfo -> ShowS
[HoleInfo] -> ShowS
HoleInfo -> String
(Int -> HoleInfo -> ShowS)
-> (HoleInfo -> String) -> ([HoleInfo] -> ShowS) -> Show HoleInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HoleInfo -> ShowS
showsPrec :: Int -> HoleInfo -> ShowS
$cshow :: HoleInfo -> String
show :: HoleInfo -> String
$cshowList :: [HoleInfo] -> ShowS
showList :: [HoleInfo] -> ShowS
Show)
data CheckWarning
= LargeInductiveTypeWarning
VarIdent
VarIdent
(Maybe LocationInfo)
| MetaPrefixWarning
VarIdent
VarIdent
Int
Int
MetaPrefixRule
(Maybe LocationInfo)
deriving (CheckWarning -> CheckWarning -> Bool
(CheckWarning -> CheckWarning -> Bool)
-> (CheckWarning -> CheckWarning -> Bool) -> Eq CheckWarning
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CheckWarning -> CheckWarning -> Bool
== :: CheckWarning -> CheckWarning -> Bool
$c/= :: CheckWarning -> CheckWarning -> Bool
/= :: CheckWarning -> CheckWarning -> Bool
Eq, Int -> CheckWarning -> ShowS
[CheckWarning] -> ShowS
CheckWarning -> String
(Int -> CheckWarning -> ShowS)
-> (CheckWarning -> String)
-> ([CheckWarning] -> ShowS)
-> Show CheckWarning
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CheckWarning -> ShowS
showsPrec :: Int -> CheckWarning -> ShowS
$cshow :: CheckWarning -> String
show :: CheckWarning -> String
$cshowList :: [CheckWarning] -> ShowS
showList :: [CheckWarning] -> ShowS
Show)
warningLocation :: CheckWarning -> Maybe LocationInfo
warningLocation :: CheckWarning -> Maybe LocationInfo
warningLocation (LargeInductiveTypeWarning VarIdent
_ VarIdent
_ Maybe LocationInfo
loc) = Maybe LocationInfo
loc
warningLocation (MetaPrefixWarning VarIdent
_ VarIdent
_ Int
_ Int
_ MetaPrefixRule
_ Maybe LocationInfo
loc) = Maybe LocationInfo
loc
data MetaPrefixRule
= MetaPrefixBoth
| MetaPrefixStrictOnly
deriving (MetaPrefixRule -> MetaPrefixRule -> Bool
(MetaPrefixRule -> MetaPrefixRule -> Bool)
-> (MetaPrefixRule -> MetaPrefixRule -> Bool) -> Eq MetaPrefixRule
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MetaPrefixRule -> MetaPrefixRule -> Bool
== :: MetaPrefixRule -> MetaPrefixRule -> Bool
$c/= :: MetaPrefixRule -> MetaPrefixRule -> Bool
/= :: MetaPrefixRule -> MetaPrefixRule -> Bool
Eq, Int -> MetaPrefixRule -> ShowS
[MetaPrefixRule] -> ShowS
MetaPrefixRule -> String
(Int -> MetaPrefixRule -> ShowS)
-> (MetaPrefixRule -> String)
-> ([MetaPrefixRule] -> ShowS)
-> Show MetaPrefixRule
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MetaPrefixRule -> ShowS
showsPrec :: Int -> MetaPrefixRule -> ShowS
$cshow :: MetaPrefixRule -> String
show :: MetaPrefixRule -> String
$cshowList :: [MetaPrefixRule] -> ShowS
showList :: [MetaPrefixRule] -> ShowS
Show)
data CheckLog = CheckLog
{ CheckLog -> [HoleInfo]
logHolesRev :: [HoleInfo]
, CheckLog -> [CheckWarning]
logWarningsRev :: [CheckWarning]
}
emptyCheckLog :: CheckLog
emptyCheckLog :: CheckLog
emptyCheckLog = [HoleInfo] -> [CheckWarning] -> CheckLog
CheckLog [] []
checkLog :: CheckLog -> ([HoleInfo], [CheckWarning])
checkLog :: CheckLog -> ([HoleInfo], [CheckWarning])
checkLog (CheckLog [HoleInfo]
holes [CheckWarning]
warnings) = ([HoleInfo] -> [HoleInfo]
forall a. [a] -> [a]
reverse [HoleInfo]
holes, [CheckWarning] -> [CheckWarning]
forall a. [a] -> [a]
reverse [CheckWarning]
warnings)
type TypeCheck n =
ReaderT (Context n)
(ExceptT TypeErrorInScopedContext (State CheckLog))
runTypeCheckWith
:: Context n -> TypeCheck n a
-> (Either TypeErrorInScopedContext a, ([HoleInfo], [CheckWarning]))
runTypeCheckWith :: forall (n :: S) a.
Context n
-> TypeCheck n a
-> (Either TypeErrorInScopedContext a,
([HoleInfo], [CheckWarning]))
runTypeCheckWith Context n
ctx TypeCheck n a
tc =
case State CheckLog (Either TypeErrorInScopedContext a)
-> CheckLog -> (Either TypeErrorInScopedContext a, CheckLog)
forall s a. State s a -> s -> (a, s)
runState (ExceptT TypeErrorInScopedContext (StateT CheckLog Identity) a
-> State CheckLog (Either TypeErrorInScopedContext a)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (TypeCheck n a
-> Context n
-> ExceptT TypeErrorInScopedContext (StateT CheckLog Identity) a
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT TypeCheck n a
tc Context n
ctx)) CheckLog
emptyCheckLog of
(Either TypeErrorInScopedContext a
result, CheckLog
logged) -> (Either TypeErrorInScopedContext a
result, CheckLog -> ([HoleInfo], [CheckWarning])
checkLog CheckLog
logged)
runTypeCheck :: TypeCheck Foil.VoidS a -> Either TypeErrorInScopedContext a
runTypeCheck :: forall a. TypeCheck 'VoidS a -> Either TypeErrorInScopedContext a
runTypeCheck = Context 'VoidS
-> TypeCheck 'VoidS a -> Either TypeErrorInScopedContext a
forall (n :: S) a.
Context n -> TypeCheck n a -> Either TypeErrorInScopedContext a
runTypeCheckIn Context 'VoidS
emptyContext
runTypeCheckIn :: Context n -> TypeCheck n a -> Either TypeErrorInScopedContext a
runTypeCheckIn :: forall (n :: S) a.
Context n -> TypeCheck n a -> Either TypeErrorInScopedContext a
runTypeCheckIn Context n
ctx TypeCheck n a
tc = (Either TypeErrorInScopedContext a, ([HoleInfo], [CheckWarning]))
-> Either TypeErrorInScopedContext a
forall a b. (a, b) -> a
fst (Context n
-> TypeCheck n a
-> (Either TypeErrorInScopedContext a,
([HoleInfo], [CheckWarning]))
forall (n :: S) a.
Context n
-> TypeCheck n a
-> (Either TypeErrorInScopedContext a,
([HoleInfo], [CheckWarning]))
runTypeCheckWith Context n
ctx TypeCheck n a
tc)
inContext :: Context l -> TypeCheck l a -> TypeCheck n a
inContext :: forall (l :: S) a (n :: S).
Context l -> TypeCheck l a -> TypeCheck n a
inContext Context l
ctx = ExceptT TypeErrorInScopedContext (StateT CheckLog Identity) a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall (m :: * -> *) a. Monad m => m a -> ReaderT (Context n) m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ExceptT TypeErrorInScopedContext (StateT CheckLog Identity) a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a)
-> (TypeCheck l a
-> ExceptT TypeErrorInScopedContext (StateT CheckLog Identity) a)
-> TypeCheck l a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TypeCheck l a
-> Context l
-> ExceptT TypeErrorInScopedContext (StateT CheckLog Identity) a)
-> Context l
-> TypeCheck l a
-> ExceptT TypeErrorInScopedContext (StateT CheckLog Identity) a
forall a b c. (a -> b -> c) -> b -> a -> c
flip TypeCheck l a
-> Context l
-> ExceptT TypeErrorInScopedContext (StateT CheckLog Identity) a
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT Context l
ctx
issueTypeError :: Distinct n => TypeError n -> TypeCheck n a
issueTypeError :: forall (n :: S) a. Distinct n => TypeError n -> TypeCheck n a
issueTypeError TypeError n
err = do
ctx <- ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
(Context n)
forall r (m :: * -> *). MonadReader r m => m r
ask
throwError (TypeErrorInScopedContext ctx err)
issueWarning :: String -> TypeCheck n ()
issueWarning :: forall (n :: S). String -> TypeCheck n ()
issueWarning String
message = String -> TypeCheck n () -> TypeCheck n ()
forall a. String -> a -> a
trace (String
"Warning: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
message) (() -> TypeCheck n ()
forall a.
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall (m :: * -> *) a. Monad m => a -> m a
return ())
trace' :: Verbosity -> Verbosity -> String -> a -> a
trace' :: forall a. Verbosity -> Verbosity -> String -> a -> a
trace' Verbosity
Silent Verbosity
_ String
_ = a -> a
forall a. a -> a
id
trace' Verbosity
Normal Verbosity
Debug String
_ = a -> a
forall a. a -> a
id
trace' Verbosity
_ Verbosity
_ String
msg = String -> a -> a
forall a. String -> a -> a
trace String
msg
traceTypeCheck :: Verbosity -> String -> TypeCheck n a -> TypeCheck n a
traceTypeCheck :: forall (n :: S) a.
Verbosity -> String -> TypeCheck n a -> TypeCheck n a
traceTypeCheck Verbosity
verbosity String
msg TypeCheck n a
action = do
configuredVerbosity <- (Context n -> Verbosity)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
Verbosity
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks Context n -> Verbosity
forall (n :: S). Context n -> Verbosity
ctxVerbosity
trace' configuredVerbosity verbosity msg action
localVerbosity :: Verbosity -> TypeCheck n a -> TypeCheck n a
localVerbosity :: forall (n :: S) a. Verbosity -> TypeCheck n a -> TypeCheck n a
localVerbosity Verbosity
verbosity = (Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall a.
(Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local ((Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a)
-> (Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall a b. (a -> b) -> a -> b
$ \Context n
ctx -> Context n
ctx { ctxVerbosity = verbosity }
localRenderBackend :: Maybe RenderBackend -> TypeCheck n a -> TypeCheck n a
localRenderBackend :: forall (n :: S) a.
Maybe RenderBackend -> TypeCheck n a -> TypeCheck n a
localRenderBackend Maybe RenderBackend
backend = (Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall a.
(Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local ((Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a)
-> (Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall a b. (a -> b) -> a -> b
$ \Context n
ctx -> Context n
ctx { ctxRenderBackend = backend }
localHideTerm :: Bool -> TypeCheck n a -> TypeCheck n a
localHideTerm :: forall (n :: S) a. Bool -> TypeCheck n a -> TypeCheck n a
localHideTerm Bool
hide = (Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall a.
(Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local ((Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a)
-> (Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall a b. (a -> b) -> a -> b
$ \Context n
ctx -> Context n
ctx { ctxRenderHideTerm = hide }
localWarnOverhang :: Bool -> TypeCheck n a -> TypeCheck n a
localWarnOverhang :: forall (n :: S) a. Bool -> TypeCheck n a -> TypeCheck n a
localWarnOverhang Bool
warn = (Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall a.
(Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local ((Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a)
-> (Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall a b. (a -> b) -> a -> b
$ \Context n
ctx -> Context n
ctx { ctxWarnOverhang = warn }
localMetaPrefixSensitivity :: MetaPrefixSensitivity -> TypeCheck n a -> TypeCheck n a
localMetaPrefixSensitivity :: forall (n :: S) a.
MetaPrefixSensitivity -> TypeCheck n a -> TypeCheck n a
localMetaPrefixSensitivity MetaPrefixSensitivity
sensitivity =
(Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall a.
(Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local ((Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a)
-> (Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall a b. (a -> b) -> a -> b
$ \Context n
ctx -> Context n
ctx { ctxMetaPrefixSensitivity = sensitivity }
hidingTerm :: TypeCheck n a -> TypeCheck n a
hidingTerm :: forall (n :: S) a. TypeCheck n a -> TypeCheck n a
hidingTerm = Bool -> TypeCheck n a -> TypeCheck n a
forall (n :: S) a. Bool -> TypeCheck n a -> TypeCheck n a
localHideTerm Bool
True
switchVariance :: TypeCheck n a -> TypeCheck n a
switchVariance :: forall (n :: S) a. TypeCheck n a -> TypeCheck n a
switchVariance = (Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall a.
(Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local ((Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a)
-> (Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall a b. (a -> b) -> a -> b
$ \Context n
ctx -> Context n
ctx { ctxCovariance = switch (ctxCovariance ctx) }
where
switch :: Covariance -> Covariance
switch Covariance
Covariant = Covariance
Contravariant
switch Covariance
Contravariant = Covariance
Covariant
switch Covariance
Invariant = Covariance
Invariant
setVariance :: Covariance -> TypeCheck n a -> TypeCheck n a
setVariance :: forall (n :: S) a. Covariance -> TypeCheck n a -> TypeCheck n a
setVariance Covariance
variance = (Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall a.
(Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local ((Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a)
-> (Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall a b. (a -> b) -> a -> b
$ \Context n
ctx -> Context n
ctx { ctxCovariance = variance }
maxActionStackDepth :: Int
maxActionStackDepth :: Int
maxActionStackDepth = Int
1000
performing :: Distinct n => Action n -> TypeCheck n a -> TypeCheck n a
performing :: forall (n :: S) a.
Distinct n =>
Action n -> TypeCheck n a -> TypeCheck n a
performing Action n
action TypeCheck n a
tc = do
ctx@Context{..} <- ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
(Context n)
forall r (m :: * -> *). MonadReader r m => m r
ask
unless (ctxActionStackDepth < maxActionStackDepth) $
issueTypeError $ TypeErrorOther "maximum depth reached"
let ctx' = Context n
ctx
{ ctxActionStack = action : ctxActionStack
, ctxActionStackDepth = ctxActionStackDepth + 1
, ctxLocation = narrowLocation action ctxLocation
}
if ctxVerbosity <= Debug
then trace (ppAction (namingOfContext ctx) ctxActionStackDepth action) $
local (const ctx') tc
else local (const ctx') tc
narrowLocation :: Action n -> Maybe LocationInfo -> Maybe LocationInfo
narrowLocation :: forall (n :: S).
Action n -> Maybe LocationInfo -> Maybe LocationInfo
narrowLocation Action n
action Maybe LocationInfo
loc = case Action n -> Maybe (Term n)
forall {n :: S}. Action n -> Maybe (Term n)
termOf Action n
action of
Just Term n
term | Just RzkPosition
pos <- Term n -> Maybe RzkPosition
forall (n :: S). Term n -> Maybe RzkPosition
positionOfTerm Term n
term -> RzkPosition -> LocationInfo -> LocationInfo
atPosition RzkPosition
pos (LocationInfo -> LocationInfo)
-> Maybe LocationInfo -> Maybe LocationInfo
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe LocationInfo
loc
Maybe (Term n)
_ -> Maybe LocationInfo
loc
where
termOf :: Action n -> Maybe (Term n)
termOf (ActionTypeCheck Term n
term TermT n
_) = Term n -> Maybe (Term n)
forall a. a -> Maybe a
Just Term n
term
termOf (ActionInfer Term n
term) = Term n -> Maybe (Term n)
forall a. a -> Maybe a
Just Term n
term
termOf Action n
_ = Maybe (Term n)
forall a. Maybe a
Nothing
modifyLog :: (CheckLog -> CheckLog) -> TypeCheck n ()
modifyLog :: forall (n :: S). (CheckLog -> CheckLog) -> TypeCheck n ()
modifyLog CheckLog -> CheckLog
f = ExceptT TypeErrorInScopedContext (StateT CheckLog Identity) ()
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
()
forall (m :: * -> *) a. Monad m => m a -> ReaderT (Context n) m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (State CheckLog ()
-> ExceptT TypeErrorInScopedContext (StateT CheckLog Identity) ()
forall (m :: * -> *) a.
Monad m =>
m a -> ExceptT TypeErrorInScopedContext m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ((CheckLog -> CheckLog) -> State CheckLog ()
forall (m :: * -> *) s. Monad m => (s -> s) -> StateT s m ()
modify' CheckLog -> CheckLog
f))
suppressing :: TypeCheck n a -> TypeCheck n a
suppressing :: forall (n :: S) a. TypeCheck n a -> TypeCheck n a
suppressing TypeCheck n a
action = do
saved <- ExceptT
TypeErrorInScopedContext (StateT CheckLog Identity) CheckLog
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
CheckLog
forall (m :: * -> *) a. Monad m => m a -> ReaderT (Context n) m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (State CheckLog CheckLog
-> ExceptT
TypeErrorInScopedContext (StateT CheckLog Identity) CheckLog
forall (m :: * -> *) a.
Monad m =>
m a -> ExceptT TypeErrorInScopedContext m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift State CheckLog CheckLog
forall (m :: * -> *) s. Monad m => StateT s m s
get)
let restore = ExceptT TypeErrorInScopedContext (StateT CheckLog Identity) ()
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
()
forall (m :: * -> *) a. Monad m => m a -> ReaderT (Context n) m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (State CheckLog ()
-> ExceptT TypeErrorInScopedContext (StateT CheckLog Identity) ()
forall (m :: * -> *) a.
Monad m =>
m a -> ExceptT TypeErrorInScopedContext m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (CheckLog -> State CheckLog ()
forall (m :: * -> *) s. Monad m => s -> StateT s m ()
put CheckLog
saved))
result <- action `catchError` \TypeErrorInScopedContext
err -> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
()
restore ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
()
-> TypeCheck n a -> TypeCheck n a
forall a b.
ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
b
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> TypeErrorInScopedContext -> TypeCheck n a
forall a.
TypeErrorInScopedContext
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError TypeErrorInScopedContext
err
restore
return result
recordHoleInfo :: HoleInfo -> TypeCheck n ()
recordHoleInfo :: forall (n :: S). HoleInfo -> TypeCheck n ()
recordHoleInfo HoleInfo
info =
(CheckLog -> CheckLog) -> TypeCheck n ()
forall (n :: S). (CheckLog -> CheckLog) -> TypeCheck n ()
modifyLog ((CheckLog -> CheckLog) -> TypeCheck n ())
-> (CheckLog -> CheckLog) -> TypeCheck n ()
forall a b. (a -> b) -> a -> b
$ \CheckLog
l -> CheckLog
l { logHolesRev = info : logHolesRev l }
recordCheckWarning :: CheckWarning -> TypeCheck n ()
recordCheckWarning :: forall (n :: S). CheckWarning -> TypeCheck n ()
recordCheckWarning CheckWarning
warning =
(CheckLog -> CheckLog) -> TypeCheck n ()
forall (n :: S). (CheckLog -> CheckLog) -> TypeCheck n ()
modifyLog ((CheckLog -> CheckLog) -> TypeCheck n ())
-> (CheckLog -> CheckLog) -> TypeCheck n ()
forall a b. (a -> b) -> a -> b
$ \CheckLog
l -> CheckLog
l { logWarningsRev = warning : logWarningsRev l }
withLocation :: LocationInfo -> TypeCheck n a -> TypeCheck n a
withLocation :: forall (n :: S) a. LocationInfo -> TypeCheck n a -> TypeCheck n a
withLocation LocationInfo
loc = (Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall a.
(Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local ((Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a)
-> (Context n -> Context n)
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
-> ReaderT
(Context n)
(ExceptT TypeErrorInScopedContext (StateT CheckLog Identity))
a
forall a b. (a -> b) -> a -> b
$ \Context n
ctx -> Context n
ctx { ctxLocation = Just loc }