{-# LANGUAGE DataKinds      #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE RankNTypes     #-}
-- | The checker's monad: a reader of the context and a state of what the
-- run records (holes and warnings), with errors raised in their context.
-- The context keeps the diagnostics: the stack of judgements and the
-- location narrowed to the term a judgement is about.
module Rzk.Check.Monad
  ( Check
  , CheckLog (..)
  , emptyCheckLog
  , runCheck
  , askContext
  , inContext
  , localContext
  , failWith
  , attempt
  , performing
  , recordWarning
  , recordHint
  , recordHole
    -- * Helpers
  , asksContext
  , withLocation
  , withOptions
  , issueTypeError
  , underTope
  , withVarianceM
  ) where

import           Control.Monad.Foil       (Distinct)
import           Debug.Trace              (trace)

import           Rzk.Check.Context
import           Rzk.Check.Error
import           Rzk.Check.Syntax
import           Rzk.Check.Topes

-- | The checker's monad.
newtype Check n a = Check
  { forall (n :: S) a.
Check n a
-> Context n -> CheckLog -> (Either ErrorInContext a, CheckLog)
unCheck :: Context n -> CheckLog -> (Either ErrorInContext a, CheckLog) }

instance Functor (Check n) where
  fmap :: forall a b. (a -> b) -> Check n a -> Check n b
fmap a -> b
f (Check Context n -> CheckLog -> (Either ErrorInContext a, CheckLog)
m) = (Context n -> CheckLog -> (Either ErrorInContext b, CheckLog))
-> Check n b
forall (n :: S) a.
(Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
-> Check n a
Check ((Context n -> CheckLog -> (Either ErrorInContext b, CheckLog))
 -> Check n b)
-> (Context n -> CheckLog -> (Either ErrorInContext b, CheckLog))
-> Check n b
forall a b. (a -> b) -> a -> b
$ \Context n
ctx CheckLog
l -> case Context n -> CheckLog -> (Either ErrorInContext a, CheckLog)
m Context n
ctx CheckLog
l of
    (Left ErrorInContext
e, CheckLog
l')  -> (ErrorInContext -> Either ErrorInContext b
forall a b. a -> Either a b
Left ErrorInContext
e, CheckLog
l')
    (Right a
a, CheckLog
l') -> (b -> Either ErrorInContext b
forall a b. b -> Either a b
Right (a -> b
f a
a), CheckLog
l')
  {-# INLINE fmap #-}

instance Applicative (Check n) where
  pure :: forall a. a -> Check n a
pure a
a = (Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
-> Check n a
forall (n :: S) a.
(Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
-> Check n a
Check ((Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
 -> Check n a)
-> (Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
-> Check n a
forall a b. (a -> b) -> a -> b
$ \Context n
_ CheckLog
l -> (a -> Either ErrorInContext a
forall a b. b -> Either a b
Right a
a, CheckLog
l)
  {-# INLINE pure #-}
  Check Context n -> CheckLog -> (Either ErrorInContext (a -> b), CheckLog)
mf <*> :: forall a b. Check n (a -> b) -> Check n a -> Check n b
<*> Check Context n -> CheckLog -> (Either ErrorInContext a, CheckLog)
ma = (Context n -> CheckLog -> (Either ErrorInContext b, CheckLog))
-> Check n b
forall (n :: S) a.
(Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
-> Check n a
Check ((Context n -> CheckLog -> (Either ErrorInContext b, CheckLog))
 -> Check n b)
-> (Context n -> CheckLog -> (Either ErrorInContext b, CheckLog))
-> Check n b
forall a b. (a -> b) -> a -> b
$ \Context n
ctx CheckLog
l -> case Context n -> CheckLog -> (Either ErrorInContext (a -> b), CheckLog)
mf Context n
ctx CheckLog
l of
    (Left ErrorInContext
e, CheckLog
l')  -> (ErrorInContext -> Either ErrorInContext b
forall a b. a -> Either a b
Left ErrorInContext
e, CheckLog
l')
    (Right a -> b
f, CheckLog
l') -> case Context n -> CheckLog -> (Either ErrorInContext a, CheckLog)
ma Context n
ctx CheckLog
l' of
      (Left ErrorInContext
e, CheckLog
l'')  -> (ErrorInContext -> Either ErrorInContext b
forall a b. a -> Either a b
Left ErrorInContext
e, CheckLog
l'')
      (Right a
a, CheckLog
l'') -> (b -> Either ErrorInContext b
forall a b. b -> Either a b
Right (a -> b
f a
a), CheckLog
l'')
  {-# INLINE (<*>) #-}

instance Monad (Check n) where
  Check Context n -> CheckLog -> (Either ErrorInContext a, CheckLog)
m >>= :: forall a b. Check n a -> (a -> Check n b) -> Check n b
>>= a -> Check n b
k = (Context n -> CheckLog -> (Either ErrorInContext b, CheckLog))
-> Check n b
forall (n :: S) a.
(Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
-> Check n a
Check ((Context n -> CheckLog -> (Either ErrorInContext b, CheckLog))
 -> Check n b)
-> (Context n -> CheckLog -> (Either ErrorInContext b, CheckLog))
-> Check n b
forall a b. (a -> b) -> a -> b
$ \Context n
ctx CheckLog
l -> case Context n -> CheckLog -> (Either ErrorInContext a, CheckLog)
m Context n
ctx CheckLog
l of
    (Left ErrorInContext
e, CheckLog
l')  -> (ErrorInContext -> Either ErrorInContext b
forall a b. a -> Either a b
Left ErrorInContext
e, CheckLog
l')
    (Right a
a, CheckLog
l') -> Check n b
-> Context n -> CheckLog -> (Either ErrorInContext b, CheckLog)
forall (n :: S) a.
Check n a
-> Context n -> CheckLog -> (Either ErrorInContext a, CheckLog)
unCheck (a -> Check n b
k a
a) Context n
ctx CheckLog
l'
  {-# INLINE (>>=) #-}

-- | What a run records: the holes, warnings, and hints it found, newest first.
data CheckLog = CheckLog
  { CheckLog -> [HoleInfo]
logHoles    :: [HoleInfo]
  , CheckLog -> [Warning]
logWarnings :: [Warning]
  , CheckLog -> [Hint]
logHints    :: [Hint]
  }

emptyCheckLog :: CheckLog
emptyCheckLog :: CheckLog
emptyCheckLog = [HoleInfo] -> [Warning] -> [Hint] -> CheckLog
CheckLog [] [] []

runCheck :: Context n -> Check n a -> (Either ErrorInContext a, CheckLog)
runCheck :: forall (n :: S) a.
Context n -> Check n a -> (Either ErrorInContext a, CheckLog)
runCheck Context n
ctx (Check Context n -> CheckLog -> (Either ErrorInContext a, CheckLog)
m) = Context n -> CheckLog -> (Either ErrorInContext a, CheckLog)
m Context n
ctx CheckLog
emptyCheckLog

-- | The depth of nested judgements at which checking gives up: the cap
-- catches a non-terminating search.
maxDepth :: Int
maxDepth :: Int
maxDepth = Int
1000

askContext :: Check n (Context n)
askContext :: forall (n :: S). Check n (Context n)
askContext = (Context n
 -> CheckLog -> (Either ErrorInContext (Context n), CheckLog))
-> Check n (Context n)
forall (n :: S) a.
(Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
-> Check n a
Check ((Context n
  -> CheckLog -> (Either ErrorInContext (Context n), CheckLog))
 -> Check n (Context n))
-> (Context n
    -> CheckLog -> (Either ErrorInContext (Context n), CheckLog))
-> Check n (Context n)
forall a b. (a -> b) -> a -> b
$ \Context n
ctx CheckLog
l -> (Context n -> Either ErrorInContext (Context n)
forall a b. b -> Either a b
Right Context n
ctx, CheckLog
l)
{-# INLINE askContext #-}

-- | Run a judgement in another scope's context.
inContext :: Context l -> Check l a -> Check n a
inContext :: forall (l :: S) a (n :: S). Context l -> Check l a -> Check n a
inContext Context l
ctx (Check Context l -> CheckLog -> (Either ErrorInContext a, CheckLog)
m) = (Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
-> Check n a
forall (n :: S) a.
(Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
-> Check n a
Check ((Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
 -> Check n a)
-> (Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
-> Check n a
forall a b. (a -> b) -> a -> b
$ \Context n
_ CheckLog
l -> Context l -> CheckLog -> (Either ErrorInContext a, CheckLog)
m Context l
ctx CheckLog
l
{-# INLINE inContext #-}

localContext :: (Context n -> Context n) -> Check n a -> Check n a
localContext :: forall (n :: S) a.
(Context n -> Context n) -> Check n a -> Check n a
localContext Context n -> Context n
f (Check Context n -> CheckLog -> (Either ErrorInContext a, CheckLog)
m) = (Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
-> Check n a
forall (n :: S) a.
(Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
-> Check n a
Check ((Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
 -> Check n a)
-> (Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
-> Check n a
forall a b. (a -> b) -> a -> b
$ \Context n
ctx CheckLog
l -> Context n -> CheckLog -> (Either ErrorInContext a, CheckLog)
m (Context n -> Context n
f Context n
ctx) CheckLog
l
{-# INLINE localContext #-}

-- | Raise an error, in the current context.
failWith :: Distinct n => TypeError n -> Check n a
failWith :: forall (n :: S) a. Distinct n => TypeError n -> Check n a
failWith TypeError n
err = (Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
-> Check n a
forall (n :: S) a.
(Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
-> Check n a
Check ((Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
 -> Check n a)
-> (Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
-> Check n a
forall a b. (a -> b) -> a -> b
$ \Context n
ctx CheckLog
l -> (ErrorInContext -> Either ErrorInContext a
forall a b. a -> Either a b
Left (Context n -> TypeError n -> ErrorInContext
forall (n :: S).
Distinct n =>
Context n -> TypeError n -> ErrorInContext
ErrorInContext Context n
ctx TypeError n
err), CheckLog
l)
{-# INLINE failWith #-}

-- | Run a judgement for its outcome, keeping what it recorded.
attempt :: Check n a -> Check n (Either ErrorInContext a)
attempt :: forall (n :: S) a. Check n a -> Check n (Either ErrorInContext a)
attempt (Check Context n -> CheckLog -> (Either ErrorInContext a, CheckLog)
m) = (Context n
 -> CheckLog
 -> (Either ErrorInContext (Either ErrorInContext a), CheckLog))
-> Check n (Either ErrorInContext a)
forall (n :: S) a.
(Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
-> Check n a
Check ((Context n
  -> CheckLog
  -> (Either ErrorInContext (Either ErrorInContext a), CheckLog))
 -> Check n (Either ErrorInContext a))
-> (Context n
    -> CheckLog
    -> (Either ErrorInContext (Either ErrorInContext a), CheckLog))
-> Check n (Either ErrorInContext a)
forall a b. (a -> b) -> a -> b
$ \Context n
ctx CheckLog
l -> case Context n -> CheckLog -> (Either ErrorInContext a, CheckLog)
m Context n
ctx CheckLog
l of
  (Left ErrorInContext
e, CheckLog
l')  -> (Either ErrorInContext a
-> Either ErrorInContext (Either ErrorInContext a)
forall a b. b -> Either a b
Right (ErrorInContext -> Either ErrorInContext a
forall a b. a -> Either a b
Left ErrorInContext
e), CheckLog
l')
  (Right a
a, CheckLog
l') -> (Either ErrorInContext a
-> Either ErrorInContext (Either ErrorInContext a)
forall a b. b -> Either a b
Right (a -> Either ErrorInContext a
forall a b. b -> Either a b
Right a
a), CheckLog
l')
{-# INLINE attempt #-}

-- | Run a judgement as an action on the stack, with the location narrowed
-- to the term it is about.
performing :: Distinct n => Action n -> Check n a -> Check n a
performing :: forall (n :: S) a. Distinct n => Action n -> Check n a -> Check n a
performing Action n
action (Check Context n -> CheckLog -> (Either ErrorInContext a, CheckLog)
m) = (Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
-> Check n a
forall (n :: S) a.
(Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
-> Check n a
Check ((Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
 -> Check n a)
-> (Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
-> Check n a
forall a b. (a -> b) -> a -> b
$ \Context n
ctx CheckLog
l ->
  let diag :: Diag n
diag = Context n -> Diag n
forall (n :: S). Context n -> Diag n
ctxDiag Context n
ctx
      depth :: Int
depth = Diag n -> Int
forall (n :: S). Diag n -> Int
diagDepth Diag n
diag
      ctx' :: Context n
ctx' = Context n
ctx
        { ctxDiag = Diag (action : diagActions diag) (depth + 1)
        , ctxLocation = narrowLocation action (ctxLocation ctx)
        }
  in if Int
depth Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
maxDepth
       then (ErrorInContext -> Either ErrorInContext a
forall a b. a -> Either a b
Left (Context n -> TypeError n -> ErrorInContext
forall (n :: S).
Distinct n =>
Context n -> TypeError n -> ErrorInContext
ErrorInContext Context n
ctx (String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorOther String
"maximum depth reached")), CheckLog
l)
       else if Options -> Verbosity
optVerbosity (Context n -> Options
forall (n :: S). Context n -> Options
ctxOptions Context n
ctx) Verbosity -> Verbosity -> Bool
forall a. Ord a => a -> a -> Bool
<= Verbosity
Debug
         then String
-> (Either ErrorInContext a, CheckLog)
-> (Either ErrorInContext a, CheckLog)
forall a. String -> a -> a
trace ([String] -> String
unlines ((String -> String) -> [String] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (Int -> Char -> String
forall a. Int -> a -> [a]
replicate (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
depth) Char
' ' String -> String -> String
forall a. Semigroup a => a -> a -> a
<>) (String -> [String]
lines (Context n -> Action n -> String
forall (n :: S). Distinct n => Context n -> Action n -> String
ppAction Context n
ctx Action n
action)))) (Context n -> CheckLog -> (Either ErrorInContext a, CheckLog)
m Context n
ctx' CheckLog
l)
         else Context n -> CheckLog -> (Either ErrorInContext a, CheckLog)
m Context n
ctx' CheckLog
l
{-# INLINE performing #-}

-- | Record a warning, rendered from the context, once.
recordWarning :: (Context n -> Warning) -> Check n ()
recordWarning :: forall (n :: S). (Context n -> Warning) -> Check n ()
recordWarning Context n -> Warning
render = (Context n -> CheckLog -> (Either ErrorInContext (), CheckLog))
-> Check n ()
forall (n :: S) a.
(Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
-> Check n a
Check ((Context n -> CheckLog -> (Either ErrorInContext (), CheckLog))
 -> Check n ())
-> (Context n -> CheckLog -> (Either ErrorInContext (), CheckLog))
-> Check n ()
forall a b. (a -> b) -> a -> b
$ \Context n
ctx CheckLog
l ->
  let w :: Warning
w = Context n -> Warning
render Context n
ctx
  in (() -> Either ErrorInContext ()
forall a b. b -> Either a b
Right (), if Warning
w Warning -> [Warning] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` CheckLog -> [Warning]
logWarnings CheckLog
l then CheckLog
l else CheckLog
l { logWarnings = w : logWarnings l })
{-# INLINE recordWarning #-}

-- | Record a hint, rendered from the context, once.
recordHint :: (Context n -> Hint) -> Check n ()
recordHint :: forall (n :: S). (Context n -> Hint) -> Check n ()
recordHint Context n -> Hint
render = (Context n -> CheckLog -> (Either ErrorInContext (), CheckLog))
-> Check n ()
forall (n :: S) a.
(Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
-> Check n a
Check ((Context n -> CheckLog -> (Either ErrorInContext (), CheckLog))
 -> Check n ())
-> (Context n -> CheckLog -> (Either ErrorInContext (), CheckLog))
-> Check n ()
forall a b. (a -> b) -> a -> b
$ \Context n
ctx CheckLog
l ->
  let hint :: Hint
hint = Context n -> Hint
render Context n
ctx
  in (() -> Either ErrorInContext ()
forall a b. b -> Either a b
Right (), if Hint
hint Hint -> [Hint] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` CheckLog -> [Hint]
logHints CheckLog
l then CheckLog
l else CheckLog
l { logHints = hint : logHints l })
{-# INLINE recordHint #-}

-- | Record a hole, rendered from the context.
recordHole :: (Context n -> HoleInfo) -> Check n ()
recordHole :: forall (n :: S). (Context n -> HoleInfo) -> Check n ()
recordHole Context n -> HoleInfo
render = (Context n -> CheckLog -> (Either ErrorInContext (), CheckLog))
-> Check n ()
forall (n :: S) a.
(Context n -> CheckLog -> (Either ErrorInContext a, CheckLog))
-> Check n a
Check ((Context n -> CheckLog -> (Either ErrorInContext (), CheckLog))
 -> Check n ())
-> (Context n -> CheckLog -> (Either ErrorInContext (), CheckLog))
-> Check n ()
forall a b. (a -> b) -> a -> b
$ \Context n
ctx CheckLog
l -> (() -> Either ErrorInContext ()
forall a b. b -> Either a b
Right (), CheckLog
l { logHoles = render ctx : logHoles l })
{-# INLINE recordHole #-}

-- | Point the location at the sub-term an action is about, when the
-- action's term carries a position.
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
action of
  ActionCheck Raw n
term Value n
_ | Just RzkPosition
pos <- Raw n -> Maybe RzkPosition
forall (n :: S). Raw n -> Maybe RzkPosition
positionOfRaw Raw 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
  ActionInfer Raw n
term   | Just RzkPosition
pos <- Raw n -> Maybe RzkPosition
forall (n :: S). Raw n -> Maybe RzkPosition
positionOfRaw Raw 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
  Action n
_ -> Maybe LocationInfo
loc

-- * Helpers

asksContext :: (Context n -> a) -> Check n a
asksContext :: forall (n :: S) a. (Context n -> a) -> Check n a
asksContext Context n -> a
f = Context n -> a
f (Context n -> a) -> Check n (Context n) -> Check n a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Check n (Context n)
forall (n :: S). Check n (Context n)
askContext
{-# INLINE asksContext #-}

withLocation :: LocationInfo -> Check n a -> Check n a
withLocation :: forall (n :: S) a. LocationInfo -> Check n a -> Check n a
withLocation LocationInfo
loc = (Context n -> Context n) -> Check n a -> Check n a
forall (n :: S) a.
(Context n -> Context n) -> Check n a -> Check n a
localContext ((Context n -> Context n) -> Check n a -> Check n a)
-> (Context n -> Context n) -> Check n a -> Check n a
forall a b. (a -> b) -> a -> b
$ \Context n
ctx -> Context n
ctx { ctxLocation = Just loc }

withOptions :: (Options -> Options) -> Check n a -> Check n a
withOptions :: forall (n :: S) a. (Options -> Options) -> Check n a -> Check n a
withOptions Options -> Options
f = (Context n -> Context n) -> Check n a -> Check n a
forall (n :: S) a.
(Context n -> Context n) -> Check n a -> Check n a
localContext ((Context n -> Context n) -> Check n a -> Check n a)
-> (Context n -> Context n) -> Check n a -> Check n a
forall a b. (a -> b) -> a -> b
$ \Context n
ctx -> Context n
ctx { ctxOptions = f (ctxOptions ctx) }

issueTypeError :: Distinct n => TypeError n -> Check n a
issueTypeError :: forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError = TypeError n -> Check n a
forall (n :: S) a. Distinct n => TypeError n -> Check n a
failWith
{-# INLINE issueTypeError #-}

-- | Run a judgement with a tope assumed.
underTope :: Tope n -> Check n a -> Check n a
underTope :: forall (n :: S) a. Tope n -> Check n a -> Check n a
underTope Tope n
tope = (Context n -> Context n) -> Check n a -> Check n a
forall (n :: S) a.
(Context n -> Context n) -> Check n a -> Check n a
localContext ((Context n -> Context n) -> Check n a -> Check n a)
-> (Context n -> Context n) -> Check n a -> Check n a
forall a b. (a -> b) -> a -> b
$ \Context n
ctx -> Context n
ctx { ctxTopes = assume tope (ctxTopes ctx) }
{-# INLINE underTope #-}

withVarianceM :: Variance -> Check n a -> Check n a
withVarianceM :: forall (n :: S) a. Variance -> Check n a -> Check n a
withVarianceM Variance
v = (Context n -> Context n) -> Check n a -> Check n a
forall (n :: S) a.
(Context n -> Context n) -> Check n a -> Check n a
localContext (Variance -> Context n -> Context n
forall (n :: S). Variance -> Context n -> Context n
withVariance Variance
v)
{-# INLINE withVarianceM #-}