{-# LANGUAGE DataKinds           #-}
{-# LANGUAGE GADTs               #-}
{-# LANGUAGE RankNTypes          #-}
{-# LANGUAGE ScopedTypeVariables #-}
-- | The new checker's entry point: check modules, and report the errors,
-- warnings, hints, and holes with their diagnostics.
module Rzk.Check
  ( Checked (..)
  , checkModules
  , module Rzk.Check.Context
  , module Rzk.Check.Error
  ) where

import           Control.Monad.Foil       (Distinct)

import qualified Language.Rzk.Syntax      as Rzk
import           Rzk.Check.Context
import qualified Rzk.Check.Decl           as Decl
import           Rzk.Check.Decl           (Decl)
import           Rzk.Check.Error
import           Rzk.Check.Monad

-- | What a run produced: the final scope's context, the declarations by
-- file, and the errors, warnings, hints, and holes found.
data Checked where
  Checked
    :: Distinct n
    => Context n
    -> [(FilePath, [Decl n])]
    -> [ErrorInContext]
    -> [Warning]
    -> [Hint]
    -> [HoleInfo]
    -> Checked

-- | Check the modules.
checkModules :: Options -> [(FilePath, Rzk.Module)] -> Checked
checkModules :: Options -> [(FilePath, Module)] -> Checked
checkModules Options
options [(FilePath, Module)]
modules =
  case Context 'VoidS
-> Check 'VoidS Checked
-> (Either ErrorInContext Checked, CheckLog)
forall (n :: S) a.
Context n -> Check n a -> (Either ErrorInContext a, CheckLog)
runCheck Context 'VoidS
ctx0 ([(FilePath, Module)]
-> (forall (l :: S).
    (DExt 'VoidS l, Distinct l) =>
    [(FilePath, [Decl l])] -> [ErrorInContext] -> Check l Checked)
-> Check 'VoidS Checked
forall (n :: S) r.
Distinct n =>
[(FilePath, Module)]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [(FilePath, [Decl l])] -> [ErrorInContext] -> Check l r)
-> Check n r
Decl.checkModules [(FilePath, Module)]
modules ((forall (l :: S).
  (DExt 'VoidS l, Distinct l) =>
  [(FilePath, [Decl l])] -> [ErrorInContext] -> Check l Checked)
 -> Check 'VoidS Checked)
-> (forall (l :: S).
    (DExt 'VoidS l, Distinct l) =>
    [(FilePath, [Decl l])] -> [ErrorInContext] -> Check l Checked)
-> Check 'VoidS Checked
forall a b. (a -> b) -> a -> b
$ \[(FilePath, [Decl l])]
decls [ErrorInContext]
errs -> do
                        ctx <- Check l (Context l)
forall (n :: S). Check n (Context n)
askContext
                        pure (Checked ctx decls errs [] [] [])) of
    (Right (Checked Context n
ctx [(FilePath, [Decl n])]
decls [ErrorInContext]
errs [Warning]
_ [Hint]
_ [HoleInfo]
_), CheckLog [HoleInfo]
holes [Warning]
warnings [Hint]
hints) ->
      Context n
-> [(FilePath, [Decl n])]
-> [ErrorInContext]
-> [Warning]
-> [Hint]
-> [HoleInfo]
-> Checked
forall (n :: S).
Distinct n =>
Context n
-> [(FilePath, [Decl n])]
-> [ErrorInContext]
-> [Warning]
-> [Hint]
-> [HoleInfo]
-> Checked
Checked Context n
ctx [(FilePath, [Decl n])]
decls [ErrorInContext]
errs ([Warning] -> [Warning]
forall a. [a] -> [a]
reverse [Warning]
warnings) ([Hint] -> [Hint]
forall a. [a] -> [a]
reverse [Hint]
hints) ([HoleInfo] -> [HoleInfo]
forall a. [a] -> [a]
reverse [HoleInfo]
holes)
    (Left ErrorInContext
err, CheckLog [HoleInfo]
holes [Warning]
warnings [Hint]
hints) ->
      Context 'VoidS
-> [(FilePath, [Decl 'VoidS])]
-> [ErrorInContext]
-> [Warning]
-> [Hint]
-> [HoleInfo]
-> Checked
forall (n :: S).
Distinct n =>
Context n
-> [(FilePath, [Decl n])]
-> [ErrorInContext]
-> [Warning]
-> [Hint]
-> [HoleInfo]
-> Checked
Checked Context 'VoidS
ctx0 [] [ErrorInContext
err] ([Warning] -> [Warning]
forall a. [a] -> [a]
reverse [Warning]
warnings) ([Hint] -> [Hint]
forall a. [a] -> [a]
reverse [Hint]
hints) ([HoleInfo] -> [HoleInfo]
forall a. [a] -> [a]
reverse [HoleInfo]
holes)
  where
    ctx0 :: Context 'VoidS
ctx0 = Context 'VoidS
emptyContext { ctxOptions = options }