{-# LANGUAGE DataKinds           #-}
{-# LANGUAGE FlexibleContexts    #-}
{-# LANGUAGE GADTs               #-}
{-# LANGUAGE LambdaCase          #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE RankNTypes          #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies        #-}
{-# LANGUAGE PatternSynonyms     #-}
{-# LANGUAGE TupleSections       #-}
-- | Declarations, commands and the module driver.
--
-- A top-level entry is a name bound in the outermost scope, so the scope
-- grows as a module is checked: each @#define@, @#postulate@ and @#assume@
-- enters a binder and the rest of the module is checked under it. The
-- driver is written in continuation-passing style, and the result is
-- packaged with the scope it was produced in.
--
-- A section (and a module, which is an anonymous section) is closed by
-- abstracting its definitions over the assumptions they use. The
-- dependencies are read off the elaborated 'Core' each top-level entry
-- keeps, as the old checker read them off its terms ("Rzk.TypeCheck.Decl").
--
-- @#data@ is not covered yet: such a command is reported and skipped.
module Rzk.Check.Decl
  ( Decl (..)
  , checkModules
  , resolveNames
  ) where

import           Control.Applicative      ((<|>))
import           Control.Monad            (forM, forM_, when)
import           Control.Monad.Foil       (DExt, Distinct)
import qualified Control.Monad.Foil       as Foil
import           Control.Monad.Free.Foil  (AST (..), ScopedAST (..))
import           Data.List                (genericLength)
import qualified Data.Map                 as Map
import           Data.Maybe               (fromMaybe)
import qualified FreeFoil.NbE             as NbE
import           Debug.Trace              (trace)

import           Language.Rzk.Foil.Names  (TModality (..),
                                           VarIdent, markUnresolved,
                                           patternToTerm, varIdentAt)
import qualified Language.Rzk.Syntax      as Rzk
import           Rzk.Check.Context
import           Rzk.Check.Convert
import           Rzk.Check.Display
import           Rzk.Check.Error
import           Rzk.Check.Monad
import           Rzk.Check.Rules
import           Rzk.Check.Syntax
import           Rzk.Check.Value
import           Rzk.TypeCheck.Decl       (duplicateBinders)

-- | A top-level declaration, as checked.
data Decl n = Decl
  { forall (n :: S). Decl n -> VarIdent
declName     :: VarIdent
    -- ^ the surface name of the declaration
  , forall (n :: S). Decl n -> Name n
declNameOf   :: Foil.Name n
    -- ^ its scoped name
  , forall (n :: S). Decl n -> Value n
declType     :: Value n
    -- ^ its checked type
  , forall (n :: S). Decl n -> Maybe (Value n)
declValue    :: Maybe (Value n)
    -- ^ its checked value, or 'Nothing' for an assumption or failed body
  , forall (n :: S). Decl n -> Maybe LocationInfo
declLocation :: Maybe LocationInfo
    -- ^ where the declaration was written
  }

instance Foil.Sinkable Decl where
  sinkabilityProof :: forall (n :: S) (l :: S). (Name n -> Name l) -> Decl n -> Decl l
sinkabilityProof Name n -> Name l
rename Decl n
decl = Decl n
decl
    { declNameOf = rename (declNameOf decl)
    , declType = Foil.sinkabilityProof rename (declType decl)
    , declValue = Foil.sinkabilityProof rename <$> declValue decl
    }

sinkDecls :: DExt n l => [Decl n] -> [Decl l]
sinkDecls :: forall (n :: S) (l :: S). DExt n l => [Decl n] -> [Decl l]
sinkDecls = [Decl n] -> [Decl l]
forall (f :: * -> *) (e :: S -> *) (n :: S) (l :: S).
(Functor f, Sinkable e, DExt n l) =>
f (e n) -> f (e l)
Foil.sink1

-- | Resolve surface names and convert a term to raw syntax. A known identifier
-- resolves to its scoped name; an unknown one is marked for the type checker to
-- report where it is used. Type elaboration happens later in 'check' or 'infer'.
resolveNames :: Distinct n => Rzk.Term -> Check n (Raw n)
resolveNames :: forall (n :: S). Distinct n => Term -> Check n (Raw n)
resolveNames Term
term = do
  ctx <- Check n (Context n)
forall (n :: S). Check n (Context n)
askContext
  forM_ (hintsOf term) $ \(Term
original, Term
suggestion) ->
    (Context n -> Hint) -> Check n ()
forall (n :: S). (Context n -> Hint) -> Check n ()
recordHint ((Context n -> Hint) -> Check n ())
-> (Context n -> Hint) -> Check n ()
forall a b. (a -> b) -> a -> b
$ \Context n
c -> String -> String -> Maybe LocationInfo -> Hint
SimplificationHint
      (Term -> String
forall a. Print a => a -> String
Rzk.printTree Term
original) (Term -> String
forall a. Print a => a -> String
Rzk.printTree Term
suggestion) (Term -> Context n -> Maybe LocationInfo
forall a (n :: S).
HasPosition a =>
a -> Context n -> Maybe LocationInfo
surfaceLocation Term
original Context n
c)
  let env VarIdent
name = case VarIdent -> Context n -> Maybe (Name n)
forall (n :: S). VarIdent -> Context n -> Maybe (Name n)
lookupNamed VarIdent
name Context n
ctx of
        Just Name n
v  -> Name n -> AST Pattern (AnnSig SrcPos Sig) n
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> AST binder sig n
Var Name n
v
        Maybe (Name n)
Nothing -> RzkPosition
-> Sig (ScopedRaw n) (AST Pattern (AnnSig SrcPos Sig) n)
-> AST Pattern (AnnSig SrcPos Sig) n
forall (n :: S). RzkPosition -> Sig (ScopedRaw n) (Raw n) -> Raw n
rawNode RzkPosition
noPosition (Maybe VarIdent
-> Sig (ScopedRaw n) (AST Pattern (AnnSig SrcPos Sig) n)
forall scope term. Maybe VarIdent -> Sig scope term
HoleF (VarIdent -> Maybe VarIdent
forall a. a -> Maybe a
Just (VarIdent -> VarIdent
markUnresolved VarIdent
name)))
  pure (toRaw (ctxScope ctx) env term)

-- | Bind a top-level entry and run the rest of the module under it. The
-- entry keeps its elaborated type and body, and is recorded in the
-- innermost open section.
withTopLevel
  :: (Distinct n)
  => VarIdent -> Core n -> Value n -> Maybe (Core n) -> EntryKind -> [Foil.Name n]
  -> (forall l. (DExt n l, Distinct l) => Decl l -> Check l r)
  -> Check n r
withTopLevel :: forall (n :: S) r.
Distinct n =>
VarIdent
-> Core n
-> Value n
-> Maybe (Core n)
-> EntryKind
-> [Name n]
-> (forall (l :: S). (DExt n l, Distinct l) => Decl l -> Check l r)
-> Check n r
withTopLevel VarIdent
name Core n
tyCore Value n
ty Maybe (Core n)
mval EntryKind
kind [Name n]
uses forall (l :: S). (DExt n l, Distinct l) => Decl l -> Check l r
k = do
  VarIdent -> Check n ()
forall (n :: S). Distinct n => VarIdent -> Check n ()
checkTopLevelDuplicate VarIdent
name
  ctx <- Check n (Context n)
forall (n :: S). Check n (Context n)
askContext
  let scope = Context n -> Scope n
forall (n :: S). Context n -> Scope n
ctxScope Context n
ctx
  Foil.withFresh scope $ \NameBinder n l
binder -> do
    let pat :: Pattern n l
pat = Maybe VarIdent -> NameBinder n l -> Pattern n l
forall (n :: S) (l :: S).
Maybe VarIdent -> NameBinder n l -> Pattern n l
PatternVar (VarIdent -> Maybe VarIdent
forall a. a -> Maybe a
Just VarIdent
name) NameBinder n l
binder
        value :: Maybe (Value n)
value = (Core n -> Value n) -> Maybe (Core n) -> Maybe (Value n)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Scope n -> Context n -> Core n -> Value n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Core n -> Value n
evalCore Scope n
scope Context n
ctx) Maybe (Core n)
mval
        entry :: Entry l
entry = Entry
          { entryType :: Value l
entryType = Value n -> Value l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Value n
ty
          , entryValue :: Maybe (Value l)
entryValue = Maybe (Value n) -> Maybe (Value l)
forall (f :: * -> *) (e :: S -> *) (n :: S) (l :: S).
(Functor f, Sinkable e, DExt n l) =>
f (e n) -> f (e l)
Foil.sink1 Maybe (Value n)
value
          , entryModality :: TModality
entryModality = TModality
Id
          , entryLocks :: TModality
entryLocks = TModality
Id
          , entryKind :: EntryKind
entryKind = EntryKind
kind
          , entryName :: Maybe VarIdent
entryName = VarIdent -> Maybe VarIdent
forall a. a -> Maybe a
Just VarIdent
name
          , entryLocation :: Maybe LocationInfo
entryLocation = Context n -> Maybe LocationInfo
forall (n :: S). Context n -> Maybe LocationInfo
ctxLocation Context n
ctx
          , entryUses :: [Name l]
entryUses = [Name n] -> [Name l]
forall (f :: * -> *) (e :: S -> *) (n :: S) (l :: S).
(Functor f, Sinkable e, DExt n l) =>
f (e n) -> f (e l)
Foil.sink1 [Name n]
uses
          , entryTypeCore :: Maybe (Core l)
entryTypeCore = Core l -> Maybe (Core l)
forall a. a -> Maybe a
Just (Core n -> Core l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Core n
tyCore)
          , entryValueCore :: Maybe (Core l)
entryValueCore = Maybe (Core n) -> Maybe (Core l)
forall (f :: * -> *) (e :: S -> *) (n :: S) (l :: S).
(Functor f, Sinkable e, DExt n l) =>
f (e n) -> f (e l)
Foil.sink1 Maybe (Core n)
mval
          }
        ctx' :: Context l
ctx' = Name l -> Context l -> Context l
forall (n :: S). Name n -> Context n -> Context n
recordInSection (NameBinder n l -> Name l
forall (n :: S) (l :: S). NameBinder n l -> Name l
Foil.nameOf NameBinder n l
binder) (Pattern n l -> [Entry l] -> Context n -> Context l
forall (n :: S) (l :: S).
(Distinct n, DExt n l) =>
Pattern n l -> [Entry l] -> Context n -> Context l
enterPattern Pattern n l
pat [Entry l
entry] Context n
ctx)
        decl :: Decl l
decl = Decl
          { declName :: VarIdent
declName = VarIdent
name
          , declNameOf :: Name l
declNameOf = NameBinder n l -> Name l
forall (n :: S) (l :: S). NameBinder n l -> Name l
Foil.nameOf NameBinder n l
binder
          , declType :: Value l
declType = Value n -> Value l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Value n
ty
          , declValue :: Maybe (Value l)
declValue = Maybe (Value n) -> Maybe (Value l)
forall (f :: * -> *) (e :: S -> *) (n :: S) (l :: S).
(Functor f, Sinkable e, DExt n l) =>
f (e n) -> f (e l)
Foil.sink1 Maybe (Value n)
value
          , declLocation :: Maybe LocationInfo
declLocation = Context n -> Maybe LocationInfo
forall (n :: S). Context n -> Maybe LocationInfo
ctxLocation Context n
ctx
          }
    Context l -> Check l r -> Check n r
forall (l :: S) a (n :: S). Context l -> Check l a -> Check n a
inContext Context l
ctx' (Decl l -> Check l r
forall (l :: S). (DExt n l, Distinct l) => Decl l -> Check l r
k Decl l
decl)

-- | Record a new entry in the innermost open section.
recordInSection :: Foil.Name n -> Context n -> Context n
recordInSection :: forall (n :: S). Name n -> Context n -> Context n
recordInSection Name n
name Context n
ctx = Context n
ctx
  { ctxSections = case ctxSections ctx of
      []       -> [Maybe String -> [Name n] -> Section n
forall (n :: S). Maybe String -> [Name n] -> Section n
Section Maybe String
forall a. Maybe a
Nothing [Name n
name]]
      Section n
s : [Section n]
rest -> Section n
s { sectionEntries = name : sectionEntries s } Section n -> [Section n] -> [Section n]
forall a. a -> [a] -> [a]
: [Section n]
rest
  }

-- | Assume names of one type, each a top-level entry.
assume
  :: (Distinct n)
  => [VarIdent] -> Core n -> Value n
  -> (forall l. (DExt n l, Distinct l) => [Decl l] -> Check l r)
  -> Check n r
assume :: forall (n :: S) r.
Distinct n =>
[VarIdent]
-> Core n
-> Value n
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> Check l r)
-> Check n r
assume [] Core n
_ Value n
_ forall (l :: S). (DExt n l, Distinct l) => [Decl l] -> Check l r
k = [Decl n] -> Check n r
forall (l :: S). (DExt n l, Distinct l) => [Decl l] -> Check l r
k []
assume (VarIdent
name : [VarIdent]
names) Core n
tyCore Value n
ty forall (l :: S). (DExt n l, Distinct l) => [Decl l] -> Check l r
k =
  VarIdent
-> Core n
-> Value n
-> Maybe (Core n)
-> EntryKind
-> [Name n]
-> (forall (l :: S). (DExt n l, Distinct l) => Decl l -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
VarIdent
-> Core n
-> Value n
-> Maybe (Core n)
-> EntryKind
-> [Name n]
-> (forall (l :: S). (DExt n l, Distinct l) => Decl l -> Check l r)
-> Check n r
withTopLevel VarIdent
name Core n
tyCore Value n
ty Maybe (Core n)
forall a. Maybe a
Nothing EntryKind
EntryAssumption [] ((forall (l :: S). (DExt n l, Distinct l) => Decl l -> Check l r)
 -> Check n r)
-> (forall (l :: S). (DExt n l, Distinct l) => Decl l -> Check l r)
-> Check n r
forall a b. (a -> b) -> a -> b
$ \Decl l
decl ->
    [VarIdent]
-> Core l
-> Value l
-> (forall (l :: S).
    (DExt l l, Distinct l) =>
    [Decl l] -> Check l r)
-> Check l r
forall (n :: S) r.
Distinct n =>
[VarIdent]
-> Core n
-> Value n
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> Check l r)
-> Check n r
assume [VarIdent]
names (Core n -> Core l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Core n
tyCore) (Value n -> Value l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Value n
ty) ((forall (l :: S). (DExt l l, Distinct l) => [Decl l] -> Check l r)
 -> Check l r)
-> (forall (l :: S).
    (DExt l l, Distinct l) =>
    [Decl l] -> Check l r)
-> Check l r
forall a b. (a -> b) -> a -> b
$ \[Decl l]
decls ->
      [Decl l] -> Check l r
forall (l :: S). (DExt n l, Distinct l) => [Decl l] -> Check l r
k (Decl l -> Decl l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Decl l
decl Decl l -> [Decl l] -> [Decl l]
forall a. a -> [a] -> [a]
: [Decl l]
decls)

-- | The names a declaration says it uses.
usedNames :: Distinct n => Maybe FilePath -> [Rzk.VarIdent] -> Check n [Foil.Name n]
usedNames :: forall (n :: S).
Distinct n =>
Maybe String -> [VarIdent] -> Check n [Name n]
usedNames Maybe String
path [VarIdent]
vars = [VarIdent] -> (VarIdent -> Check n (Name n)) -> Check n [Name n]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [VarIdent]
vars ((VarIdent -> Check n (Name n)) -> Check n [Name n])
-> (VarIdent -> Check n (Name n)) -> Check n [Name n]
forall a b. (a -> b) -> a -> b
$ \VarIdent
var -> do
  ctx <- Check n (Context n)
forall (n :: S). Check n (Context n)
askContext
  let name = Maybe String -> VarIdent -> VarIdent
varIdentAt Maybe String
path VarIdent
var
  maybe (issueTypeError (TypeErrorUndefined name)) pure (lookupNamed name ctx)

checkTopLevelDuplicate :: Distinct n => VarIdent -> Check n ()
checkTopLevelDuplicate :: forall (n :: S). Distinct n => VarIdent -> Check n ()
checkTopLevelDuplicate VarIdent
name = do
  ctx <- Check n (Context n)
forall (n :: S). Check n (Context n)
askContext
  case lookupNamed name ctx of
    Maybe (Name n)
Nothing -> () -> Check n ()
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    Just Name n
v -> case Entry n -> Maybe VarIdent
forall (n :: S). Entry n -> Maybe VarIdent
entryName (Name n -> Context n -> Entry n
forall (n :: S). Name n -> Context n -> Entry n
lookupEntry Name n
v Context n
ctx) of
      Just VarIdent
previous -> TypeError n -> Check n ()
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError ([VarIdent] -> VarIdent -> TypeError n
forall (n :: S). [VarIdent] -> VarIdent -> TypeError n
TypeErrorDuplicateTopLevel [VarIdent
previous] VarIdent
name)
      Maybe VarIdent
Nothing       -> TypeError n -> Check n ()
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError ([VarIdent] -> VarIdent -> TypeError n
forall (n :: S). [VarIdent] -> VarIdent -> TypeError n
TypeErrorDuplicateTopLevel [] VarIdent
name)

-- * Commands

-- | The surface parameters of a declaration as binders of its type.
paramToParamDecl :: Rzk.Param -> Either String [Rzk.ParamDecl]
paramToParamDecl :: Param -> Either String [ParamDecl]
paramToParamDecl = \case
  Rzk.ParamPatternShape BNFC'Position
loc [Pattern' BNFC'Position]
pats Term
cube Term
tope ->
    [ParamDecl] -> Either String [ParamDecl]
forall a b. b -> Either a b
Right [ BNFC'Position -> Term -> Term -> Term -> ParamDecl
forall a. a -> Term' a -> Term' a -> Term' a -> ParamDecl' a
Rzk.ParamTermShape BNFC'Position
loc (Pattern' BNFC'Position -> Term
patternToTerm Pattern' BNFC'Position
pat) Term
cube Term
tope | Pattern' BNFC'Position
pat <- [Pattern' BNFC'Position]
pats ]
  Rzk.ParamPatternType BNFC'Position
loc [Pattern' BNFC'Position]
pats Term
ty ->
    [ParamDecl] -> Either String [ParamDecl]
forall a b. b -> Either a b
Right [ BNFC'Position -> Term -> Term -> ParamDecl
forall a. a -> Term' a -> Term' a -> ParamDecl' a
Rzk.ParamTermType BNFC'Position
loc (Pattern' BNFC'Position -> Term
patternToTerm Pattern' BNFC'Position
pat) Term
ty | Pattern' BNFC'Position
pat <- [Pattern' BNFC'Position]
pats ]
  Rzk.ParamPattern{} -> String -> Either String [ParamDecl]
forall a b. a -> Either a b
Left String
"untyped pattern in parameters"
  Rzk.ParamPatternModalType BNFC'Position
loc [Pattern' BNFC'Position]
pats ModalColon' BNFC'Position
mc Term
ty ->
    [ParamDecl] -> Either String [ParamDecl]
forall a b. b -> Either a b
Right [ BNFC'Position
-> Term -> ModalColon' BNFC'Position -> Term -> ParamDecl
forall a. a -> Term' a -> ModalColon' a -> Term' a -> ParamDecl' a
Rzk.ParamTermModalType BNFC'Position
loc (Pattern' BNFC'Position -> Term
patternToTerm Pattern' BNFC'Position
pat) ModalColon' BNFC'Position
mc Term
ty | Pattern' BNFC'Position
pat <- [Pattern' BNFC'Position]
pats ]
  Rzk.ParamPatternModalShape BNFC'Position
loc [Pattern' BNFC'Position]
pats ModalColon' BNFC'Position
mc Term
cube Term
tope ->
    [ParamDecl] -> Either String [ParamDecl]
forall a b. b -> Either a b
Right [ BNFC'Position
-> Term -> ModalColon' BNFC'Position -> Term -> Term -> ParamDecl
forall a.
a -> Term' a -> ModalColon' a -> Term' a -> Term' a -> ParamDecl' a
Rzk.ParamTermModalShape BNFC'Position
loc (Pattern' BNFC'Position -> Term
patternToTerm Pattern' BNFC'Position
pat) ModalColon' BNFC'Position
mc Term
cube Term
tope | Pattern' BNFC'Position
pat <- [Pattern' BNFC'Position]
pats ]

addParamDecls :: [Rzk.ParamDecl] -> Rzk.Term -> Rzk.Term
addParamDecls :: [ParamDecl] -> Term -> Term
addParamDecls [] = Term -> Term
forall a. a -> a
id
addParamDecls (ParamDecl
paramDecl : [ParamDecl]
paramDecls) = BNFC'Position -> ParamDecl -> Term -> Term
forall a. a -> ParamDecl' a -> Term' a -> Term' a
Rzk.TypeFun BNFC'Position
forall a. Maybe a
Nothing ParamDecl
paramDecl (Term -> Term) -> (Term -> Term) -> Term -> Term
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ParamDecl] -> Term -> Term
addParamDecls [ParamDecl]
paramDecls

addParams :: [Rzk.Param] -> Rzk.Term -> Rzk.Term
addParams :: [Param] -> Term -> Term
addParams []     = Term -> Term
forall a. a -> a
id
addParams [Param]
params = BNFC'Position -> [Param] -> Term -> Term
forall a. a -> [Param' a] -> Term' a -> Term' a
Rzk.Lambda BNFC'Position
forall a. Maybe a
Nothing [Param]
params

-- | The type of a declaration with its parameters, checked: the names it
-- says it uses, the type as elaborated, and as a value.
checkDeclType
  :: (Distinct n)
  => Maybe FilePath -> [Rzk.VarIdent] -> [Rzk.Param] -> Rzk.Term
  -> Check n ([Foil.Name n], Core n, Value n)
checkDeclType :: forall (n :: S).
Distinct n =>
Maybe String
-> [VarIdent]
-> [Param]
-> Term
-> Check n ([Name n], Core n, Value n)
checkDeclType Maybe String
path [VarIdent]
vars [Param]
params Term
ty = do
  used <- Maybe String -> [VarIdent] -> Check n [Name n]
forall (n :: S).
Distinct n =>
Maybe String -> [VarIdent] -> Check n [Name n]
usedNames Maybe String
path [VarIdent]
vars
  paramDecls <- either (issueTypeError . TypeErrorOther) (pure . concat) (mapM paramToParamDecl params)
  tyTerm <- resolveNames (addParamDecls paramDecls ty)
  ty' <- atSurface ty $ check tyTerm universe
  (used, ty',) <$> evalM ty'

setOption :: Distinct n => String -> String -> Check n a -> Check n a
setOption :: forall (n :: S) a.
Distinct n =>
String -> String -> Check n a -> Check n a
setOption String
name String
value = case String
name of
  String
"verbosity" -> case String
value of
    String
"debug"  -> (Options -> Options) -> Check n a -> Check n a
forall (n :: S) a. (Options -> Options) -> Check n a -> Check n a
withOptions (\Options
o -> Options
o { optVerbosity = Debug })
    String
"normal" -> (Options -> Options) -> Check n a -> Check n a
forall (n :: S) a. (Options -> Options) -> Check n a -> Check n a
withOptions (\Options
o -> Options
o { optVerbosity = Normal })
    String
"silent" -> (Options -> Options) -> Check n a -> Check n a
forall (n :: S) a. (Options -> Options) -> Check n a -> Check n a
withOptions (\Options
o -> Options
o { optVerbosity = Silent })
    String
_ -> Check n a -> Check n a -> Check n a
forall a b. a -> b -> a
const (Check n a -> Check n a -> Check n a)
-> Check n a -> Check n a -> Check n a
forall a b. (a -> b) -> a -> b
$ TypeError n -> Check n a
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (TypeError n -> Check n a) -> TypeError n -> Check n a
forall a b. (a -> b) -> a -> b
$ String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorOther
      String
"unknown verbosity level (use \"debug\", \"normal\", or \"silent\")"
  String
"warn-overhang" -> (Bool -> Options -> Options) -> Check n a -> Check n a
yesNo ((Bool -> Options -> Options) -> Check n a -> Check n a)
-> (Bool -> Options -> Options) -> Check n a -> Check n a
forall a b. (a -> b) -> a -> b
$ \Bool
b Options
o -> Options
o { optWarnOverhang = b }
  String
"warn-tope-family-domain" -> (Bool -> Options -> Options) -> Check n a -> Check n a
yesNo ((Bool -> Options -> Options) -> Check n a -> Check n a)
-> (Bool -> Options -> Options) -> Check n a -> Check n a
forall a b. (a -> b) -> a -> b
$ \Bool
b Options
o -> Options
o { optWarnTopeFamilyDomain = b }
  String
"render" -> case String
value of
    String
"svg"   -> (Options -> Options) -> Check n a -> Check n a
forall (n :: S) a. (Options -> Options) -> Check n a -> Check n a
withOptions (\Options
o -> Options
o { optRender = Just "svg" })
    String
"latex" -> (Options -> Options) -> Check n a -> Check n a
forall (n :: S) a. (Options -> Options) -> Check n a -> Check n a
withOptions (\Options
o -> Options
o { optRender = Just "latex" })
    String
"none"  -> (Options -> Options) -> Check n a -> Check n a
forall (n :: S) a. (Options -> Options) -> Check n a -> Check n a
withOptions (\Options
o -> Options
o { optRender = Nothing })
    String
_ -> Check n a -> Check n a -> Check n a
forall a b. a -> b -> a
const (Check n a -> Check n a -> Check n a)
-> Check n a -> Check n a -> Check n a
forall a b. (a -> b) -> a -> b
$ TypeError n -> Check n a
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (TypeError n -> Check n a) -> TypeError n -> Check n a
forall a b. (a -> b) -> a -> b
$ String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorOther
      String
"unknown render backend (use \"svg\", \"latex\", or \"none\")"
  String
_ | String
name String -> [String] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String]
ignoredOptions -> String -> Check n a -> Check n a
forall (n :: S) a. String -> Check n a -> Check n a
warnIgnoredOption String
name
  String
_ -> Check n a -> Check n a -> Check n a
forall a b. a -> b -> a
const (Check n a -> Check n a -> Check n a)
-> Check n a -> Check n a -> Check n a
forall a b. (a -> b) -> a -> b
$ TypeError n -> Check n a
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (TypeError n -> Check n a) -> TypeError n -> Check n a
forall a b. (a -> b) -> a -> b
$ String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorOther (String
"unknown option " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
forall a. Show a => a -> String
show String
name)
  where
    yesNo :: (Bool -> Options -> Options) -> Check n a -> Check n a
yesNo Bool -> Options -> Options
set = case String
value of
      String
"yes" -> (Options -> Options) -> Check n a -> Check n a
forall (n :: S) a. (Options -> Options) -> Check n a -> Check n a
withOptions (Bool -> Options -> Options
set Bool
True)
      String
"no"  -> (Options -> Options) -> Check n a -> Check n a
forall (n :: S) a. (Options -> Options) -> Check n a -> Check n a
withOptions (Bool -> Options -> Options
set Bool
False)
      String
_ -> Check n a -> Check n a -> Check n a
forall a b. a -> b -> a
const (Check n a -> Check n a -> Check n a)
-> Check n a -> Check n a -> Check n a
forall a b. (a -> b) -> a -> b
$ TypeError n -> Check n a
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (TypeError n -> Check n a) -> TypeError n -> Check n a
forall a b. (a -> b) -> a -> b
$ String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorOther
        (String
"unknown value for " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
forall a. Show a => a -> String
show String
name String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" (use \"yes\" or \"no\")")

unsetOption :: Distinct n => String -> Check n a -> Check n a
unsetOption :: forall (n :: S) a. Distinct n => String -> Check n a -> Check n a
unsetOption String
name = case String
name of
  String
"verbosity" -> (Options -> Options) -> Check n a -> Check n a
forall (n :: S) a. (Options -> Options) -> Check n a -> Check n a
withOptions (\Options
o -> Options
o { optVerbosity = optVerbosity defaultOptions })
  String
"warn-overhang" -> (Options -> Options) -> Check n a -> Check n a
forall (n :: S) a. (Options -> Options) -> Check n a -> Check n a
withOptions (\Options
o -> Options
o { optWarnOverhang = optWarnOverhang defaultOptions })
  String
"warn-tope-family-domain" ->
    (Options -> Options) -> Check n a -> Check n a
forall (n :: S) a. (Options -> Options) -> Check n a -> Check n a
withOptions (\Options
o -> Options
o { optWarnTopeFamilyDomain = optWarnTopeFamilyDomain defaultOptions })
  String
"render" -> (Options -> Options) -> Check n a -> Check n a
forall (n :: S) a. (Options -> Options) -> Check n a -> Check n a
withOptions (\Options
o -> Options
o { optRender = Nothing })
  String
_ | String
name String -> [String] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String]
ignoredOptions -> String -> Check n a -> Check n a
forall (n :: S) a. String -> Check n a -> Check n a
warnIgnoredOption String
name
  String
_ -> Check n a -> Check n a -> Check n a
forall a b. a -> b -> a
const (Check n a -> Check n a -> Check n a)
-> Check n a -> Check n a -> Check n a
forall a b. (a -> b) -> a -> b
$ TypeError n -> Check n a
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (TypeError n -> Check n a) -> TypeError n -> Check n a
forall a b. (a -> b) -> a -> b
$ String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorOther (String
"unknown option " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
forall a. Show a => a -> String
show String
name)

-- | The options of checks the new checker does not run yet: accepted, and
-- without effect.
ignoredOptions :: [String]
ignoredOptions :: [String]
ignoredOptions =
  [ String
"render-hide-term", String
"rstt-safe", String
"warn-meta-prefix"
  , String
"warn-free-standing-restriction", String
"warn-shape-dependency", String
"warn-meta-binder" ]

-- | Record that an accepted option has no effect in the new checker.
warnIgnoredOption :: String -> Check n a -> Check n a
warnIgnoredOption :: forall (n :: S) a. String -> Check n a -> Check n a
warnIgnoredOption String
name Check n a
action = do
  (Context n -> Warning) -> Check n ()
forall (n :: S). (Context n -> Warning) -> Check n ()
recordWarning ((Context n -> Warning) -> Check n ())
-> (Context n -> Warning) -> Check n ()
forall a b. (a -> b) -> a -> b
$ \Context n
ctx -> String -> Maybe LocationInfo -> Warning
UnsupportedOptionWarning String
name (Context n -> Maybe LocationInfo
forall (n :: S). Context n -> Maybe LocationInfo
ctxLocation Context n
ctx)
  Check n a
action

-- | Run a check at a command: the location points at the command, and a
-- judgement inside it narrows it to the sub-term it is about.
atCommand :: Rzk.Command -> Check n a -> Check n a
atCommand :: forall (n :: S) a. Command -> Check n a -> Check n a
atCommand Command
command = (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 = at <$> ctxLocation ctx }
  where
    at :: LocationInfo -> LocationInfo
at LocationInfo
loc = LocationInfo
loc { locationLine = fst <$> pos, locationColumn = snd <$> pos }
    pos :: BNFC'Position
pos = Command -> BNFC'Position
forall a. HasPosition a => a -> BNFC'Position
Rzk.hasPosition Command
command

-- | Run a check with the location narrowed to where a surface term was
-- written.
atSurface :: (Rzk.HasPosition a) => a -> Check n b -> Check n b
atSurface :: forall a (n :: S) b. HasPosition a => a -> Check n b -> Check n b
atSurface a
x = (Context n -> Context n) -> Check n b -> Check n b
forall (n :: S) a.
(Context n -> Context n) -> Check n a -> Check n a
localContext ((Context n -> Context n) -> Check n b -> Check n b)
-> (Context n -> Context n) -> Check n b -> Check n b
forall a b. (a -> b) -> a -> b
$ \Context n
ctx -> Context n
ctx { ctxLocation = surfaceLocation x ctx }

-- | The location of a surface term in the current file.
surfaceLocation :: (Rzk.HasPosition a) => a -> Context n -> Maybe LocationInfo
surfaceLocation :: forall a (n :: S).
HasPosition a =>
a -> Context n -> Maybe LocationInfo
surfaceLocation a
x Context n
ctx = LocationInfo -> LocationInfo
narrow (LocationInfo -> LocationInfo)
-> Maybe LocationInfo -> Maybe LocationInfo
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Context n -> Maybe LocationInfo
forall (n :: S). Context n -> Maybe LocationInfo
ctxLocation Context n
ctx
  where
    narrow :: LocationInfo -> LocationInfo
narrow LocationInfo
loc = case a -> BNFC'Position
forall a. HasPosition a => a -> BNFC'Position
Rzk.hasPosition a
x of
      BNFC'Position
Nothing          -> LocationInfo
loc
      Just (Int
line, Int
col) -> LocationInfo
loc { locationLine = Just line, locationColumn = Just col }

-- * The module driver

-- | Check a module's commands, extending the scope with each definition.
-- The continuation runs in the final scope with the declarations and the
-- errors found.
checkCommands
  :: forall n r. (Distinct n)
  => Maybe FilePath -> Integer -> Integer -> [Rzk.Command]
  -> (forall l. (DExt n l, Distinct l) => [Decl l] -> [ErrorInContext] -> Check l r)
  -> Check n r
checkCommands :: forall (n :: S) r.
Distinct n =>
Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
checkCommands Maybe String
path Integer
i Integer
total [Command]
commands forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k = case [Command]
commands of
  [] -> [Decl n] -> [ErrorInContext] -> Check n r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k [] []
  Command
command : [Command]
more | (VarIdent
dup, [VarIdent]
previous) : [(VarIdent, [VarIdent])]
_ <- Command -> [(VarIdent, [VarIdent])]
forall a. Data a => a -> [(VarIdent, [VarIdent])]
duplicateBinders Command
command -> do
    result <- Check n () -> Check n (Either ErrorInContext ())
forall (n :: S) a. Check n a -> Check n (Either ErrorInContext a)
attempt (Check n () -> Check n (Either ErrorInContext ()))
-> Check n () -> Check n (Either ErrorInContext ())
forall a b. (a -> b) -> a -> b
$ Command -> Check n () -> Check n ()
forall (n :: S) a. Command -> Check n a -> Check n a
atCommand Command
command (Check n () -> Check n ()) -> Check n () -> Check n ()
forall a b. (a -> b) -> a -> b
$ TypeError n -> Check n ()
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (VarIdent -> [VarIdent] -> TypeError n
forall (n :: S). VarIdent -> [VarIdent] -> TypeError n
TypeErrorRepeatedBinder VarIdent
dup [VarIdent]
previous)
    case result of
      Left ErrorInContext
err -> ErrorInContext
-> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
ErrorInContext
-> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
skipping ErrorInContext
err Maybe String
path Integer
i Integer
total [Command]
more [Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k
      Right () -> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
checkCommands Maybe String
path (Integer
i Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1) Integer
total [Command]
more [Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k

  command :: Command
command@(Rzk.CommandUnsetOption BNFC'Position
_loc String
optionName) : [Command]
more ->
    String -> Check n r -> Check n r
forall (n :: S) a. String -> Check n a -> Check n a
announce (String
"Unsetting option " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
optionName) (Check n r -> Check n r) -> Check n r -> Check n r
forall a b. (a -> b) -> a -> b
$ do
      result <- Check n () -> Check n (Either ErrorInContext ())
forall (n :: S) a. Check n a -> Check n (Either ErrorInContext a)
attempt (Check n () -> Check n (Either ErrorInContext ()))
-> Check n () -> Check n (Either ErrorInContext ())
forall a b. (a -> b) -> a -> b
$ Command -> Check n () -> Check n ()
forall (n :: S) a. Command -> Check n a -> Check n a
atCommand Command
command (Check n () -> Check n ()) -> Check n () -> Check n ()
forall a b. (a -> b) -> a -> b
$ String -> Check n () -> Check n ()
forall (n :: S) a. Distinct n => String -> Check n a -> Check n a
unsetOption String
optionName (() -> Check n ()
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())
      case result of
        Left ErrorInContext
err -> ErrorInContext
-> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
ErrorInContext
-> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
skipping ErrorInContext
err Maybe String
path Integer
i Integer
total [Command]
more [Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k
        Right () -> Command -> Check n r -> Check n r
forall (n :: S) a. Command -> Check n a -> Check n a
atCommand Command
command (Check n r -> Check n r) -> Check n r -> Check n r
forall a b. (a -> b) -> a -> b
$ String -> Check n r -> Check n r
forall (n :: S) a. Distinct n => String -> Check n a -> Check n a
unsetOption String
optionName (Check n r -> Check n r) -> Check n r -> Check n r
forall a b. (a -> b) -> a -> b
$ Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
checkCommands Maybe String
path (Integer
i Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1) Integer
total [Command]
more [Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k

  command :: Command
command@(Rzk.CommandSetOption BNFC'Position
_loc String
optionName String
optionValue) : [Command]
more ->
    String -> Check n r -> Check n r
forall (n :: S) a. String -> Check n a -> Check n a
announce (String
"Setting option " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
optionName String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" = " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
optionValue) (Check n r -> Check n r) -> Check n r -> Check n r
forall a b. (a -> b) -> a -> b
$ do
      result <- Check n () -> Check n (Either ErrorInContext ())
forall (n :: S) a. Check n a -> Check n (Either ErrorInContext a)
attempt (Check n () -> Check n (Either ErrorInContext ()))
-> Check n () -> Check n (Either ErrorInContext ())
forall a b. (a -> b) -> a -> b
$ Command -> Check n () -> Check n ()
forall (n :: S) a. Command -> Check n a -> Check n a
atCommand Command
command (Check n () -> Check n ()) -> Check n () -> Check n ()
forall a b. (a -> b) -> a -> b
$ String -> String -> Check n () -> Check n ()
forall (n :: S) a.
Distinct n =>
String -> String -> Check n a -> Check n a
setOption String
optionName String
optionValue (() -> Check n ()
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())
      case result of
        Left ErrorInContext
err -> ErrorInContext
-> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
ErrorInContext
-> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
skipping ErrorInContext
err Maybe String
path Integer
i Integer
total [Command]
more [Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k
        Right () -> Command -> Check n r -> Check n r
forall (n :: S) a. Command -> Check n a -> Check n a
atCommand Command
command (Check n r -> Check n r) -> Check n r -> Check n r
forall a b. (a -> b) -> a -> b
$ String -> String -> Check n r -> Check n r
forall (n :: S) a.
Distinct n =>
String -> String -> Check n a -> Check n a
setOption String
optionName String
optionValue (Check n r -> Check n r) -> Check n r -> Check n r
forall a b. (a -> b) -> a -> b
$ Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
checkCommands Maybe String
path (Integer
i Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1) Integer
total [Command]
more [Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k

  command :: Command
command@(Rzk.CommandDefine BNFC'Position
_loc VarIdent
name (Rzk.DeclUsedVars BNFC'Position
_ [VarIdent]
vars) [Param]
params Term
ty Term
term) : [Command]
more ->
    String -> Check n r -> Check n r
forall (n :: S) a. String -> Check n a -> Check n a
announce (String
" Checking #define " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> VarIdent -> String
forall a. Print a => a -> String
Rzk.printTree VarIdent
name) (Check n r -> Check n r) -> Check n r -> Check n r
forall a b. (a -> b) -> a -> b
$ Command -> Check n r -> Check n r
forall (n :: S) a. Command -> Check n a -> Check n a
atCommand Command
command (Check n r -> Check n r) -> Check n r -> Check n r
forall a b. (a -> b) -> a -> b
$ do
      typeResult <- Check n ([Name n], Core n, Value n)
-> Check n (Either ErrorInContext ([Name n], Core n, Value n))
forall (n :: S) a. Check n a -> Check n (Either ErrorInContext a)
attempt (Check n ([Name n], Core n, Value n)
 -> Check n (Either ErrorInContext ([Name n], Core n, Value n)))
-> Check n ([Name n], Core n, Value n)
-> Check n (Either ErrorInContext ([Name n], Core n, Value n))
forall a b. (a -> b) -> a -> b
$ Maybe String
-> [VarIdent]
-> [Param]
-> Term
-> Check n ([Name n], Core n, Value n)
forall (n :: S).
Distinct n =>
Maybe String
-> [VarIdent]
-> [Param]
-> Term
-> Check n ([Name n], Core n, Value n)
checkDeclType Maybe String
path [VarIdent]
vars [Param]
params Term
ty
      case typeResult of
        Left ErrorInContext
err -> ErrorInContext
-> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
ErrorInContext
-> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
skipping ErrorInContext
err Maybe String
path Integer
i Integer
total [Command]
more [Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k
        Right ([Name n]
used, Core n
tyC, Value n
tyV) -> do
          -- the body is checked on its own, so that a definition whose
          -- type is fine but whose proof is not still enters the scope
          result <- Check n (Core n) -> Check n (Either ErrorInContext (Core n))
forall (n :: S) a. Check n a -> Check n (Either ErrorInContext a)
attempt (Check n (Core n) -> Check n (Either ErrorInContext (Core n)))
-> Check n (Core n) -> Check n (Either ErrorInContext (Core n))
forall a b. (a -> b) -> a -> b
$ do
            valTerm <- Term -> Check n (Raw n)
forall (n :: S). Distinct n => Term -> Check n (Raw n)
resolveNames ([Param] -> Term -> Term
addParams [Param]
params Term
term)
            val' <- atSurface term $ check valTerm tyV
            case force tyV of
              Whnf n
VUniverse -> Core n -> Check n (Value n)
forall (n :: S). Distinct n => Core n -> Check n (Value n)
evalM Core n
val' Check n (Value n) -> (Value n -> Check n ()) -> Check n ()
forall a b. Check n a -> (a -> Check n b) -> Check n b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> Value n -> Check n ()
forall (n :: S). Distinct n => String -> Value n -> Check n ()
ensureTypeAtU String
"a definition at type U"
              Whnf n
_ -> () -> Check n ()
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
            pure val'
          let (mvalue, bodyErrors) = case result of
                Right Core n
val' -> (Core n -> Maybe (Core n)
forall a. a -> Maybe a
Just Core n
val', [])
                Left ErrorInContext
err   -> (Maybe (Core n)
forall a. Maybe a
Nothing, [ErrorInContext
err])
          renderResult <- attempt $ atCommand command $ do
            render <- asksContext (optRender . ctxOptions)
            case render of
              Just String
"latex" | Just Core n
_ <- Maybe (Core n)
mvalue -> TypeError n -> Check n ()
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (TypeError n -> Check n ()) -> TypeError n -> Check n ()
forall a b. (a -> b) -> a -> b
$
                String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorOther String
"\"latex\" rendering is not yet supported"
              Maybe String
_ -> () -> Check n ()
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
          let renderErrors = (ErrorInContext -> [ErrorInContext])
-> (() -> [ErrorInContext])
-> Either ErrorInContext ()
-> [ErrorInContext]
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either ErrorInContext -> [ErrorInContext]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([ErrorInContext] -> () -> [ErrorInContext]
forall a b. a -> b -> a
const []) Either ErrorInContext ()
renderResult
          withTopLevel (varIdentAt path name) tyC tyV mvalue EntryTopLevel used $ \Decl l
decl ->
            Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt l l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check l r
forall (n :: S) r.
Distinct n =>
Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
checkCommands Maybe String
path (Integer
i Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1) Integer
total [Command]
more ((forall (l :: S).
  (DExt l l, Distinct l) =>
  [Decl l] -> [ErrorInContext] -> Check l r)
 -> Check l r)
-> (forall (l :: S).
    (DExt l l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check l r
forall a b. (a -> b) -> a -> b
$ \[Decl l]
decls [ErrorInContext]
errs ->
              [Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k (Decl l -> Decl l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Decl l
decl Decl l -> [Decl l] -> [Decl l]
forall a. a -> [a] -> [a]
: [Decl l]
decls) ([ErrorInContext]
bodyErrors [ErrorInContext] -> [ErrorInContext] -> [ErrorInContext]
forall a. Semigroup a => a -> a -> a
<> [ErrorInContext]
renderErrors [ErrorInContext] -> [ErrorInContext] -> [ErrorInContext]
forall a. Semigroup a => a -> a -> a
<> [ErrorInContext]
errs)

  command :: Command
command@(Rzk.CommandPostulate BNFC'Position
_loc VarIdent
name (Rzk.DeclUsedVars BNFC'Position
_ [VarIdent]
vars) [Param]
params Term
ty) : [Command]
more ->
    String -> Check n r -> Check n r
forall (n :: S) a. String -> Check n a -> Check n a
announce (String
" Checking #postulate " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> VarIdent -> String
forall a. Print a => a -> String
Rzk.printTree VarIdent
name) (Check n r -> Check n r) -> Check n r -> Check n r
forall a b. (a -> b) -> a -> b
$ Command -> Check n r -> Check n r
forall (n :: S) a. Command -> Check n a -> Check n a
atCommand Command
command (Check n r -> Check n r) -> Check n r -> Check n r
forall a b. (a -> b) -> a -> b
$ do
      typeResult <- Check n ([Name n], Core n, Value n)
-> Check n (Either ErrorInContext ([Name n], Core n, Value n))
forall (n :: S) a. Check n a -> Check n (Either ErrorInContext a)
attempt (Check n ([Name n], Core n, Value n)
 -> Check n (Either ErrorInContext ([Name n], Core n, Value n)))
-> Check n ([Name n], Core n, Value n)
-> Check n (Either ErrorInContext ([Name n], Core n, Value n))
forall a b. (a -> b) -> a -> b
$ Maybe String
-> [VarIdent]
-> [Param]
-> Term
-> Check n ([Name n], Core n, Value n)
forall (n :: S).
Distinct n =>
Maybe String
-> [VarIdent]
-> [Param]
-> Term
-> Check n ([Name n], Core n, Value n)
checkDeclType Maybe String
path [VarIdent]
vars [Param]
params Term
ty
      case typeResult of
        Left ErrorInContext
err -> ErrorInContext
-> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
ErrorInContext
-> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
skipping ErrorInContext
err Maybe String
path Integer
i Integer
total [Command]
more [Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k
        Right ([Name n]
used, Core n
tyC, Value n
tyV) ->
          VarIdent
-> Core n
-> Value n
-> Maybe (Core n)
-> EntryKind
-> [Name n]
-> (forall (l :: S). (DExt n l, Distinct l) => Decl l -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
VarIdent
-> Core n
-> Value n
-> Maybe (Core n)
-> EntryKind
-> [Name n]
-> (forall (l :: S). (DExt n l, Distinct l) => Decl l -> Check l r)
-> Check n r
withTopLevel (Maybe String -> VarIdent -> VarIdent
varIdentAt Maybe String
path VarIdent
name) Core n
tyC Value n
tyV Maybe (Core n)
forall a. Maybe a
Nothing EntryKind
EntryTopLevel [Name n]
used ((forall (l :: S). (DExt n l, Distinct l) => Decl l -> Check l r)
 -> Check n r)
-> (forall (l :: S). (DExt n l, Distinct l) => Decl l -> Check l r)
-> Check n r
forall a b. (a -> b) -> a -> b
$ \Decl l
decl ->
            Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt l l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check l r
forall (n :: S) r.
Distinct n =>
Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
checkCommands Maybe String
path (Integer
i Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1) Integer
total [Command]
more ((forall (l :: S).
  (DExt l l, Distinct l) =>
  [Decl l] -> [ErrorInContext] -> Check l r)
 -> Check l r)
-> (forall (l :: S).
    (DExt l l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check l r
forall a b. (a -> b) -> a -> b
$ \[Decl l]
decls [ErrorInContext]
errs ->
              [Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k (Decl l -> Decl l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Decl l
decl Decl l -> [Decl l] -> [Decl l]
forall a. a -> [a] -> [a]
: [Decl l]
decls) [ErrorInContext]
errs

  command :: Command
command@(Rzk.CommandCheck BNFC'Position
_loc Term
term Term
ty) : [Command]
more ->
    String -> Check n r -> Check n r
forall (n :: S) a. String -> Check n a -> Check n a
announce (String
" Checking " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Term -> String
forall a. Print a => a -> String
Rzk.printTree Term
term String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" : " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Term -> String
forall a. Print a => a -> String
Rzk.printTree Term
ty) (Check n r -> Check n r) -> Check n r -> Check n r
forall a b. (a -> b) -> a -> b
$ Command -> Check n r -> Check n r
forall (n :: S) a. Command -> Check n a -> Check n a
atCommand Command
command (Check n r -> Check n r) -> Check n r -> Check n r
forall a b. (a -> b) -> a -> b
$ do
      result <- Check n () -> Check n (Either ErrorInContext ())
forall (n :: S) a. Check n a -> Check n (Either ErrorInContext a)
attempt (Check n () -> Check n (Either ErrorInContext ()))
-> Check n () -> Check n (Either ErrorInContext ())
forall a b. (a -> b) -> a -> b
$ do
        tyTerm <- Term -> Check n (Raw n)
forall (n :: S). Distinct n => Term -> Check n (Raw n)
resolveNames Term
ty
        ty' <- atSurface ty $ check tyTerm universe
        tyV <- evalM ty'
        termTerm <- resolveNames term
        _ <- atSurface term $ check termTerm tyV
        pure ()
      case result of
        Left ErrorInContext
err -> ErrorInContext
-> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
ErrorInContext
-> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
skipping ErrorInContext
err Maybe String
path Integer
i Integer
total [Command]
more [Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k
        Right () -> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
checkCommands Maybe String
path (Integer
i Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1) Integer
total [Command]
more [Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k

  Rzk.CommandCompute BNFC'Position
loc Term
term : [Command]
more ->
    Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
checkCommands Maybe String
path Integer
i Integer
total (BNFC'Position -> Term -> Command
forall a. a -> Term' a -> Command' a
Rzk.CommandComputeWHNF BNFC'Position
loc Term
term Command -> [Command] -> [Command]
forall a. a -> [a] -> [a]
: [Command]
more) [Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k

  command :: Command
command@(Rzk.CommandComputeNF BNFC'Position
_loc Term
term) : [Command]
more ->
    String -> Check n r -> Check n r
forall (n :: S) a. String -> Check n a -> Check n a
announce (String
" Computing NF for " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Term -> String
forall a. Print a => a -> String
Rzk.printTree Term
term) (Check n r -> Check n r) -> Check n r -> Check n r
forall a b. (a -> b) -> a -> b
$ Command -> Check n r -> Check n r
forall (n :: S) a. Command -> Check n a -> Check n a
atCommand Command
command (Check n r -> Check n r) -> Check n r -> Check n r
forall a b. (a -> b) -> a -> b
$ do
      result <- Check n String -> Check n (Either ErrorInContext String)
forall (n :: S) a. Check n a -> Check n (Either ErrorInContext a)
attempt (Check n String -> Check n (Either ErrorInContext String))
-> Check n String -> Check n (Either ErrorInContext String)
forall a b. (a -> b) -> a -> b
$ do
        (term', _) <- Term -> Check n (Core n, Value n) -> Check n (Core n, Value n)
forall a (n :: S) b. HasPosition a => a -> Check n b -> Check n b
atSurface Term
term (Check n (Core n, Value n) -> Check n (Core n, Value n))
-> Check n (Core n, Value n) -> Check n (Core n, Value n)
forall a b. (a -> b) -> a -> b
$ Term -> Check n (Raw n)
forall (n :: S). Distinct n => Term -> Check n (Raw n)
resolveNames Term
term Check n (Raw n)
-> (Raw n -> Check n (Core n, Value n))
-> Check n (Core n, Value n)
forall a b. Check n a -> (a -> Check n b) -> Check n b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Raw n -> Check n (Core n, Value n)
forall (n :: S). Distinct n => Raw n -> Check n (Core n, Value n)
infer
        ctx <- askContext
        pure (ppCore (namingOfContext ctx) (nfCore (ctxScope ctx) ctx term'))
      case result of
        Left ErrorInContext
err -> ErrorInContext
-> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
ErrorInContext
-> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
skipping ErrorInContext
err Maybe String
path Integer
i Integer
total [Command]
more [Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k
        Right String
shown -> String -> Check n r -> Check n r
forall (n :: S) a. String -> Check n a -> Check n a
announce (String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
shown) (Check n r -> Check n r) -> Check n r -> Check n r
forall a b. (a -> b) -> a -> b
$ Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
checkCommands Maybe String
path (Integer
i Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1) Integer
total [Command]
more [Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k

  command :: Command
command@(Rzk.CommandComputeWHNF BNFC'Position
_loc Term
term) : [Command]
more ->
    String -> Check n r -> Check n r
forall (n :: S) a. String -> Check n a -> Check n a
announce (String
" Computing WHNF for " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Term -> String
forall a. Print a => a -> String
Rzk.printTree Term
term) (Check n r -> Check n r) -> Check n r -> Check n r
forall a b. (a -> b) -> a -> b
$ Command -> Check n r -> Check n r
forall (n :: S) a. Command -> Check n a -> Check n a
atCommand Command
command (Check n r -> Check n r) -> Check n r -> Check n r
forall a b. (a -> b) -> a -> b
$ do
      result <- Check n String -> Check n (Either ErrorInContext String)
forall (n :: S) a. Check n a -> Check n (Either ErrorInContext a)
attempt (Check n String -> Check n (Either ErrorInContext String))
-> Check n String -> Check n (Either ErrorInContext String)
forall a b. (a -> b) -> a -> b
$ do
        (term', _) <- Term -> Check n (Core n, Value n) -> Check n (Core n, Value n)
forall a (n :: S) b. HasPosition a => a -> Check n b -> Check n b
atSurface Term
term (Check n (Core n, Value n) -> Check n (Core n, Value n))
-> Check n (Core n, Value n) -> Check n (Core n, Value n)
forall a b. (a -> b) -> a -> b
$ Term -> Check n (Raw n)
forall (n :: S). Distinct n => Term -> Check n (Raw n)
resolveNames Term
term Check n (Raw n)
-> (Raw n -> Check n (Core n, Value n))
-> Check n (Core n, Value n)
forall a b. Check n a -> (a -> Check n b) -> Check n b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Raw n -> Check n (Core n, Value n)
forall (n :: S). Distinct n => Raw n -> Check n (Core n, Value n)
infer
        ctx <- askContext
        pure (ppCore (namingOfContext ctx) (quoteWhnfCore (ctxScope ctx) ctx term'))
      case result of
        Left ErrorInContext
err -> ErrorInContext
-> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
ErrorInContext
-> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
skipping ErrorInContext
err Maybe String
path Integer
i Integer
total [Command]
more [Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k
        Right String
shown -> String -> Check n r -> Check n r
forall (n :: S) a. String -> Check n a -> Check n a
announce (String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
shown) (Check n r -> Check n r) -> Check n r -> Check n r
forall a b. (a -> b) -> a -> b
$ Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
checkCommands Maybe String
path (Integer
i Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1) Integer
total [Command]
more [Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k

  command :: Command
command@(Rzk.CommandAssume BNFC'Position
_loc [VarIdent]
names Term
ty) : [Command]
more ->
    String -> Check n r -> Check n r
forall (n :: S) a. String -> Check n a -> Check n a
announce (String
" Checking #assume " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> [String] -> String
unwords ((VarIdent -> String) -> [VarIdent] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map VarIdent -> String
forall a. Print a => a -> String
Rzk.printTree [VarIdent]
names)) (Check n r -> Check n r) -> Check n r -> Check n r
forall a b. (a -> b) -> a -> b
$ Command -> Check n r -> Check n r
forall (n :: S) a. Command -> Check n a -> Check n a
atCommand Command
command (Check n r -> Check n r) -> Check n r -> Check n r
forall a b. (a -> b) -> a -> b
$ do
      typeResult <- Check n (Core n, Value n)
-> Check n (Either ErrorInContext (Core n, Value n))
forall (n :: S) a. Check n a -> Check n (Either ErrorInContext a)
attempt (Check n (Core n, Value n)
 -> Check n (Either ErrorInContext (Core n, Value n)))
-> Check n (Core n, Value n)
-> Check n (Either ErrorInContext (Core n, Value n))
forall a b. (a -> b) -> a -> b
$ do
        tyTerm <- Term -> Check n (Raw n)
forall (n :: S). Distinct n => Term -> Check n (Raw n)
resolveNames Term
ty
        ty' <- atSurface ty $ check tyTerm universe
        (ty',) <$> evalM ty'
      case typeResult of
        Left ErrorInContext
err -> ErrorInContext
-> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
ErrorInContext
-> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
skipping ErrorInContext
err Maybe String
path Integer
i Integer
total [Command]
more [Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k
        Right (Core n
tyC, Value n
tyV) ->
          [VarIdent]
-> Core n
-> Value n
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
[VarIdent]
-> Core n
-> Value n
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> Check l r)
-> Check n r
assume ((VarIdent -> VarIdent) -> [VarIdent] -> [VarIdent]
forall a b. (a -> b) -> [a] -> [b]
map (Maybe String -> VarIdent -> VarIdent
varIdentAt Maybe String
path) [VarIdent]
names) Core n
tyC Value n
tyV ((forall (l :: S). (DExt n l, Distinct l) => [Decl l] -> Check l r)
 -> Check n r)
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> Check l r)
-> Check n r
forall a b. (a -> b) -> a -> b
$ \[Decl l]
assumed ->
            Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt l l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check l r
forall (n :: S) r.
Distinct n =>
Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
checkCommands Maybe String
path (Integer
i Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1) Integer
total [Command]
more ((forall (l :: S).
  (DExt l l, Distinct l) =>
  [Decl l] -> [ErrorInContext] -> Check l r)
 -> Check l r)
-> (forall (l :: S).
    (DExt l l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check l r
forall a b. (a -> b) -> a -> b
$ \[Decl l]
decls [ErrorInContext]
errs ->
              [Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k ([Decl l] -> [Decl l]
forall (n :: S) (l :: S). DExt n l => [Decl n] -> [Decl l]
sinkDecls [Decl l]
assumed [Decl l] -> [Decl l] -> [Decl l]
forall a. Semigroup a => a -> a -> a
<> [Decl l]
decls) [ErrorInContext]
errs

  -- A malformed section stops the module, as does an error inside a
  -- section: the declarations after it are not checked.
  command :: Command
command@(Rzk.CommandSection BNFC'Position
_loc SectionName' BNFC'Position
name) : [Command]
more -> do
    split <- Check n ([Command], [Command])
-> Check n (Either ErrorInContext ([Command], [Command]))
forall (n :: S) a. Check n a -> Check n (Either ErrorInContext a)
attempt (Check n ([Command], [Command])
 -> Check n (Either ErrorInContext ([Command], [Command])))
-> Check n ([Command], [Command])
-> Check n (Either ErrorInContext ([Command], [Command]))
forall a b. (a -> b) -> a -> b
$ Command
-> Check n ([Command], [Command]) -> Check n ([Command], [Command])
forall (n :: S) a. Command -> Check n a -> Check n a
atCommand Command
command (Check n ([Command], [Command]) -> Check n ([Command], [Command]))
-> Check n ([Command], [Command]) -> Check n ([Command], [Command])
forall a b. (a -> b) -> a -> b
$ SectionName' BNFC'Position
-> [Command] -> Check n ([Command], [Command])
forall (n :: S).
Distinct n =>
SectionName' BNFC'Position
-> [Command] -> Check n ([Command], [Command])
splitSectionCommands SectionName' BNFC'Position
name [Command]
more
    case split of
      Left ErrorInContext
err -> [Decl n] -> [ErrorInContext] -> Check n r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k [] [ErrorInContext
err]
      Right ([Command]
sectionCommands, [Command]
more') ->
        Maybe String
-> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
Maybe String
-> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
withSection (String -> Maybe String
forall a. a -> Maybe a
Just (SectionName' BNFC'Position -> String
forall a. Print a => a -> String
Rzk.printTree SectionName' BNFC'Position
name)) Maybe String
path (Integer
i Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1) Integer
total [Command]
sectionCommands ((forall (l :: S).
  (DExt n l, Distinct l) =>
  [Decl l] -> [ErrorInContext] -> Check l r)
 -> Check n r)
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall a b. (a -> b) -> a -> b
$ \[Decl l]
sectionDecls [ErrorInContext]
sectionErrs ->
          case [ErrorInContext]
sectionErrs of
            [] -> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt l l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check l r
forall (n :: S) r.
Distinct n =>
Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
checkCommands Maybe String
path (Integer
i Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ [Command] -> Integer
forall i a. Num i => [a] -> i
genericLength [Command]
sectionCommands) Integer
total [Command]
more' ((forall (l :: S).
  (DExt l l, Distinct l) =>
  [Decl l] -> [ErrorInContext] -> Check l r)
 -> Check l r)
-> (forall (l :: S).
    (DExt l l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check l r
forall a b. (a -> b) -> a -> b
$ \[Decl l]
decls [ErrorInContext]
errs ->
                    [Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k ([Decl l] -> [Decl l]
forall (n :: S) (l :: S). DExt n l => [Decl n] -> [Decl l]
sinkDecls [Decl l]
sectionDecls [Decl l] -> [Decl l] -> [Decl l]
forall a. Semigroup a => a -> a -> a
<> [Decl l]
decls) [ErrorInContext]
errs
            [ErrorInContext]
_  -> [Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k [Decl l]
sectionDecls [ErrorInContext]
sectionErrs

  command :: Command
command@(Rzk.CommandSectionEnd BNFC'Position
_loc SectionName' BNFC'Position
endName) : [Command]
_more -> do
    err <- Check n () -> Check n ErrorInContext
forall (n :: S). Check n () -> Check n ErrorInContext
captureError (Check n () -> Check n ErrorInContext)
-> Check n () -> Check n ErrorInContext
forall a b. (a -> b) -> a -> b
$ Command -> Check n () -> Check n ()
forall (n :: S) a. Command -> Check n a -> Check n a
atCommand Command
command (Check n () -> Check n ()) -> Check n () -> Check n ()
forall a b. (a -> b) -> a -> b
$ TypeError n -> Check n ()
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (TypeError n -> Check n ()) -> TypeError n -> Check n ()
forall a b. (a -> b) -> a -> b
$ String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorOther (String -> TypeError n) -> String -> TypeError n
forall a b. (a -> b) -> a -> b
$
      String
"unexpected #end " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> SectionName' BNFC'Position -> String
forall a. Print a => a -> String
Rzk.printTree SectionName' BNFC'Position
endName String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
", no section was declared!"
    k [] [err]

  Rzk.CommandData{} : [Command]
more -> String -> [Command] -> Check n r
unsupported String
"#data" [Command]
more
  where
    announce :: String -> Check l a -> Check l a
    announce :: forall (n :: S) a. String -> Check n a -> Check n a
announce String
what = String -> Check l a -> Check l a
forall (n :: S) a. String -> Check n a -> Check n a
traceNormal (String
"[ " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Integer -> String
forall a. Show a => a -> String
show Integer
i String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" out of " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Integer -> String
forall a. Show a => a -> String
show Integer
total String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" ]" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
what)

    unsupported :: String -> [Rzk.Command] -> Check n r
    unsupported :: String -> [Command] -> Check n r
unsupported String
what [Command]
more = do
      result <- Check n () -> Check n (Either ErrorInContext ())
forall (n :: S) a. Check n a -> Check n (Either ErrorInContext a)
attempt (Check n () -> Check n (Either ErrorInContext ()))
-> Check n () -> Check n (Either ErrorInContext ())
forall a b. (a -> b) -> a -> b
$ TypeError n -> Check n ()
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorUnsupported String
what)
      case result of
        Left ErrorInContext
err -> ErrorInContext
-> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
ErrorInContext
-> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
skipping ErrorInContext
err Maybe String
path Integer
i Integer
total [Command]
more [Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k
        Right () -> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
checkCommands Maybe String
path (Integer
i Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1) Integer
total [Command]
more [Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k

-- | The commands of a section up to its @#end@, and the commands after it.
-- A nested section is merged into the enclosing one, as in the old checker.
splitSectionCommands
  :: (Distinct n)
  => Rzk.SectionName -> [Rzk.Command] -> Check n ([Rzk.Command], [Rzk.Command])
splitSectionCommands :: forall (n :: S).
Distinct n =>
SectionName' BNFC'Position
-> [Command] -> Check n ([Command], [Command])
splitSectionCommands SectionName' BNFC'Position
name = \case
  [] -> TypeError n -> Check n ([Command], [Command])
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (TypeError n -> Check n ([Command], [Command]))
-> TypeError n -> Check n ([Command], [Command])
forall a b. (a -> b) -> a -> b
$ String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorOther (String -> TypeError n) -> String -> TypeError n
forall a b. (a -> b) -> a -> b
$
    String
"Section " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> SectionName' BNFC'Position -> String
forall a. Print a => a -> String
Rzk.printTree SectionName' BNFC'Position
name String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" is not closed with an #end"
  Rzk.CommandSection BNFC'Position
_loc SectionName' BNFC'Position
name' : [Command]
more -> do
    (cs1, cs2) <- SectionName' BNFC'Position
-> [Command] -> Check n ([Command], [Command])
forall (n :: S).
Distinct n =>
SectionName' BNFC'Position
-> [Command] -> Check n ([Command], [Command])
splitSectionCommands SectionName' BNFC'Position
name' [Command]
more
    (cs3, cs4) <- splitSectionCommands name cs2
    pure (cs1 <> cs3, cs4)
  Rzk.CommandSectionEnd BNFC'Position
_loc SectionName' BNFC'Position
endName : [Command]
more -> do
    Bool -> Check n () -> Check n ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (SectionName' BNFC'Position -> String
forall a. Print a => a -> String
Rzk.printTree SectionName' BNFC'Position
name String -> String -> Bool
forall a. Eq a => a -> a -> Bool
/= SectionName' BNFC'Position -> String
forall a. Print a => a -> String
Rzk.printTree SectionName' BNFC'Position
endName) (Check n () -> Check n ()) -> Check n () -> Check n ()
forall a b. (a -> b) -> a -> b
$
      TypeError n -> Check n ()
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError (TypeError n -> Check n ()) -> TypeError n -> Check n ()
forall a b. (a -> b) -> a -> b
$ String -> TypeError n
forall (n :: S). String -> TypeError n
TypeErrorOther (String -> TypeError n) -> String -> TypeError n
forall a b. (a -> b) -> a -> b
$
        String
"unexpected #end " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> SectionName' BNFC'Position -> String
forall a. Print a => a -> String
Rzk.printTree SectionName' BNFC'Position
endName String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
", expecting #end " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> SectionName' BNFC'Position -> String
forall a. Print a => a -> String
Rzk.printTree SectionName' BNFC'Position
name
    ([Command], [Command]) -> Check n ([Command], [Command])
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([], [Command]
more)
  Command
command : [Command]
more -> do
    (cs1, cs2) <- SectionName' BNFC'Position
-> [Command] -> Check n ([Command], [Command])
forall (n :: S).
Distinct n =>
SectionName' BNFC'Position
-> [Command] -> Check n ([Command], [Command])
splitSectionCommands SectionName' BNFC'Position
name [Command]
more
    pure (command : cs1, cs2)

-- | An error raised by a judgement, captured rather than propagated.
captureError :: Check n () -> Check n (ErrorInContext)
captureError :: forall (n :: S). Check n () -> Check n ErrorInContext
captureError Check n ()
action = Check n () -> Check n (Either ErrorInContext ())
forall (n :: S) a. Check n a -> Check n (Either ErrorInContext a)
attempt Check n ()
action Check n (Either ErrorInContext ())
-> (Either ErrorInContext () -> Check n ErrorInContext)
-> Check n ErrorInContext
forall a b. Check n a -> (a -> Check n b) -> Check n b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
  Left ErrorInContext
err -> ErrorInContext -> Check n ErrorInContext
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ErrorInContext
err
  Right () -> String -> Check n ErrorInContext
forall a. String -> a
panicImpossible String
"captureError: the judgement did not fail"

-- | Check the commands of a section, then close it.
withSection
  :: forall n r. (Distinct n)
  => Maybe String -> Maybe FilePath -> Integer -> Integer -> [Rzk.Command]
  -> (forall l. (DExt n l, Distinct l) => [Decl l] -> [ErrorInContext] -> Check l r)
  -> Check n r
withSection :: forall (n :: S) r.
Distinct n =>
Maybe String
-> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
withSection Maybe String
name Maybe String
path Integer
i Integer
total [Command]
commands forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k =
  (Context n -> Context n) -> Check n r -> Check n r
forall (n :: S) a.
(Context n -> Context n) -> Check n a -> Check n a
localContext (\Context n
ctx -> Context n
ctx { ctxSections = Section name [] : ctxSections ctx }) (Check n r -> Check n r) -> Check n r -> Check n r
forall a b. (a -> b) -> a -> b
$
    Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
checkCommands Maybe String
path Integer
i Integer
total [Command]
commands ((forall (l :: S).
  (DExt n l, Distinct l) =>
  [Decl l] -> [ErrorInContext] -> Check l r)
 -> Check n r)
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall a b. (a -> b) -> a -> b
$ \[Decl l]
_decls [ErrorInContext]
errs -> do
      outer <- Check l (Context l)
forall (n :: S). Check n (Context n)
askContext
      result <- attempt $ performing (ActionCloseSection name) (endSection errs)
      case result of
        -- the section is closed even so, so that the enclosing one does not
        -- process its entries again
        Left ErrorInContext
err ->
          (Context l -> Context l) -> Check l r -> Check l r
forall (n :: S) a.
(Context n -> Context n) -> Check n a -> Check n a
localContext (\Context l
c -> Context l
c { ctxSections = drop 1 (ctxSections c) }) (Check l r -> Check l r) -> Check l r -> Check l r
forall a b. (a -> b) -> a -> b
$
            [Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k [] ([ErrorInContext]
errs [ErrorInContext] -> [ErrorInContext] -> [ErrorInContext]
forall a. Semigroup a => a -> a -> a
<> [ErrorInContext
err])
        -- the rest of the module is not checked under the closing action
        Right ([Decl l]
decls, [ErrorInContext]
errs', Context l
ctx') ->
          Context l -> Check l r -> Check l r
forall (l :: S) a (n :: S). Context l -> Check l a -> Check n a
inContext Context l
ctx' { ctxDiag = ctxDiag outer, ctxLocation = ctxLocation outer } ([Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k [Decl l]
decls [ErrorInContext]
errs')

-- * Closing a section

-- | Close the innermost section: abstract each of its assumptions out of
-- the definitions that use it, report the assumptions and the @uses@ that
-- went unused, and take the assumptions out of scope.
--
-- The definitions stay in scope with their entries replaced: the type and
-- the body are abstracted over the assumptions with Π and λ, and later
-- uses of an abstracted definition are rewritten to its application to the
-- assumption. The assumptions' names stay in the scope index, as a scope
-- only grows, but leave the surface names and the list of what is bound,
-- so they can no longer be referred to or shown.
endSection
  :: forall n. (Distinct n)
  => [ErrorInContext]
  -> Check n ([Decl n], [ErrorInContext], Context n)
endSection :: forall (n :: S).
Distinct n =>
[ErrorInContext] -> Check n ([Decl n], [ErrorInContext], Context n)
endSection [ErrorInContext]
errs = do
  ctx <- Check n (Context n)
forall (n :: S). Check n (Context n)
askContext
  let scope = Context n -> Scope n
forall (n :: S). Context n -> Scope n
ctxScope Context n
ctx
      entries = [ (Name n
name, Name n -> Context n -> Entry n
forall (n :: S). Name n -> Context n -> Entry n
lookupEntry Name n
name Context n
ctx)    -- newest first
                | Name n
name <- case Context n -> [Section n]
forall (n :: S). Context n -> [Section n]
ctxSections Context n
ctx of
                    []    -> []
                    Section n
s : [Section n]
_ -> Section n -> [Name n]
forall (n :: S). Section n -> [Name n]
sectionEntries Section n
s ]
      isAssumptionEntry Entry n
entry = Entry n -> EntryKind
forall (n :: S). Entry n -> EntryKind
entryKind Entry n
entry EntryKind -> EntryKind -> Bool
forall a. Eq a => a -> a -> Bool
== EntryKind
EntryAssumption
      assumptions = [Name n] -> NameSet n
forall (n :: S). [Name n] -> NameSet n
Foil.nameSetFromList [ Name n
name | (Name n
name, Entry n
entry) <- [(Name n, Entry n)]
entries, Entry n -> Bool
forall {n :: S}. Entry n -> Bool
isAssumptionEntry Entry n
entry ]
      isAssumption Name n
name = Name n -> NameSet n -> Bool
forall (n :: S). Name n -> NameSet n -> Bool
Foil.nameSetMember Name n
name NameSet n
assumptions

      -- In a sketch, a hole may still come to use an assumption, so the
      -- unused-variable errors are not reported in a section with a hole.
      tolerateUnused = Options -> HoleMode
optHoles (Context n -> Options
forall (n :: S). Context n -> Options
ctxOptions Context n
ctx) HoleMode -> HoleMode -> Bool
forall a. Eq a => a -> a -> Bool
/= HoleMode
HolesAreErrors
        Bool -> Bool -> Bool
&& ((Name n, Entry n) -> Bool) -> [(Name n, Entry n)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Bool -> (Core n -> Bool) -> Maybe (Core n) -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False Core n -> Bool
forall (n :: S). Core n -> Bool
containsHole (Maybe (Core n) -> Bool)
-> ((Name n, Entry n) -> Maybe (Core n))
-> (Name n, Entry n)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Entry n -> Maybe (Core n)
forall (n :: S). Entry n -> Maybe (Core n)
entryValueCore (Entry n -> Maybe (Core n))
-> ((Name n, Entry n) -> Entry n)
-> (Name n, Entry n)
-> Maybe (Core n)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Name n, Entry n) -> Entry n
forall a b. (a, b) -> b
snd) [(Name n, Entry n)]
entries
      reportUnused = [ErrorInContext] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [ErrorInContext]
errs Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
tolerateUnused

      typeCoreOf Entry n
entry = Core n -> Maybe (Core n) -> Core n
forall a. a -> Maybe a -> a
fromMaybe (Scope n -> Context n -> Value n -> Core n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Core n
quoteFolded Scope n
scope Context n
ctx (Entry n -> Value n
forall (n :: S). Entry n -> Value n
entryType Entry n
entry)) (Entry n -> Maybe (Core n)
forall (n :: S). Entry n -> Maybe (Core n)
entryTypeCore Entry n
entry)
      namesOf = [Name n] -> (Core n -> [Name n]) -> Maybe (Core n) -> [Name n]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] Core n -> [Name n]
forall (n :: S). Distinct n => Core n -> [Name n]
freeNamesOf

      -- The names an entry depends on: those its type and body mention,
      -- and those the types of these mention, transitively. The types are
      -- the ones written, read off the context.
      dependencies Entry n
entry =
        NameSet n -> [Name n] -> NameSet n
closeUnderTypes NameSet n
forall (n :: S). NameSet n
Foil.emptyNameSet (Maybe (Core n) -> [Name n]
namesOf (Entry n -> Maybe (Core n)
forall (n :: S). Entry n -> Maybe (Core n)
entryTypeCore Entry n
entry) [Name n] -> [Name n] -> [Name n]
forall a. Semigroup a => a -> a -> a
<> Maybe (Core n) -> [Name n]
namesOf (Entry n -> Maybe (Core n)
forall (n :: S). Entry n -> Maybe (Core n)
entryValueCore Entry n
entry))
      closeUnderTypes NameSet n
seen = \case
        [] -> NameSet n
seen
        Name n
name : [Name n]
more
          | Name n -> NameSet n -> Bool
forall (n :: S). Name n -> NameSet n -> Bool
Foil.nameSetMember Name n
name NameSet n
seen -> NameSet n -> [Name n] -> NameSet n
closeUnderTypes NameSet n
seen [Name n]
more
          | Bool
otherwise -> NameSet n -> [Name n] -> NameSet n
closeUnderTypes (Name n -> NameSet n -> NameSet n
forall (n :: S). Name n -> NameSet n -> NameSet n
Foil.nameSetInsert Name n
name NameSet n
seen)
              (Core n -> [Name n]
forall (n :: S). Distinct n => Core n -> [Name n]
freeNamesOf (Entry n -> Core n
typeCoreOf (Name n -> Context n -> Entry n
forall (n :: S). Name n -> Context n -> Entry n
lookupEntry Name n
name Context n
ctx)) [Name n] -> [Name n] -> [Name n]
forall a. Semigroup a => a -> a -> a
<> [Name n]
more)

      -- Abstract an assumption out of the definitions (oldest first) that
      -- depend on it.
      abstractAssumption Name n
a Entry n
aEntry = [(Name n, Entry n, Bool)]
-> Check n (Bool, [(Name n, Entry n, Bool)])
go
        where
          go :: [(Name n, Entry n, Bool)]
-> Check n (Bool, [(Name n, Entry n, Bool)])
go [] = (Bool, [(Name n, Entry n, Bool)])
-> Check n (Bool, [(Name n, Entry n, Bool)])
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool
False, [])
          go ((Name n
x, Entry n
xEntry, Bool
changed) : [(Name n, Entry n, Bool)]
xs) = do
            let written :: Entry n
written = Name n -> Context n -> Entry n
forall (n :: S). Name n -> Context n -> Entry n
lookupEntry Name n
x Context n
ctx
                mentioned :: Bool
mentioned = (Name n -> Bool) -> [Name n] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Name n -> Name n -> Bool
forall a. Eq a => a -> a -> Bool
sameName Name n
a) (Maybe (Core n) -> [Name n]
namesOf (Entry n -> Maybe (Core n)
forall (n :: S). Entry n -> Maybe (Core n)
entryTypeCore Entry n
written) [Name n] -> [Name n] -> [Name n]
forall a. Semigroup a => a -> a -> a
<> Maybe (Core n) -> [Name n]
namesOf (Entry n -> Maybe (Core n)
forall (n :: S). Entry n -> Maybe (Core n)
entryValueCore Entry n
written))
                declared :: Bool
declared = (Name n -> Bool) -> [Name n] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Name n -> Name n -> Bool
forall a. Eq a => a -> a -> Bool
sameName Name n
a) (Entry n -> [Name n]
forall (n :: S). Entry n -> [Name n]
entryUses Entry n
xEntry)
            if Name n -> NameSet n -> Bool
forall (n :: S). Name n -> NameSet n -> Bool
Foil.nameSetMember Name n
a (Entry n -> NameSet n
dependencies Entry n
xEntry)
              then do
                -- used, but neither written in the type or the body nor
                -- declared with @uses@
                Bool -> Check n () -> Check n ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not Bool
mentioned Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
declared) (Check n () -> Check n ()) -> Check n () -> Check n ()
forall a b. (a -> b) -> a -> b
$
                  (Context n -> Context n) -> Check n () -> Check n ()
forall (n :: S) a.
(Context n -> Context n) -> Check n a -> Check n a
localContext (\Context n
c -> Context n
c { ctxLocation = entryLocation xEntry <|> ctxLocation c }) (Check n () -> Check n ()) -> Check n () -> Check n ()
forall a b. (a -> b) -> a -> b
$
                    TypeError n -> Check n ()
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError ((Name n, Value n) -> Name n -> TypeError n
forall (n :: S). (Name n, Value n) -> Name n -> TypeError n
TypeErrorImplicitAssumption (Name n
a, Entry n -> Value n
forall (n :: S). Entry n -> Value n
entryType Entry n
aEntry) Name n
x)
                let xs' :: [(Name n, Entry n, Bool)]
xs' = [ (Name n
y, Name n -> Name n -> Entry n -> Entry n
applyToAssumption Name n
a Name n
x Entry n
yEntry, Bool
True) | (Name n
y, Entry n
yEntry, Bool
_) <- [(Name n, Entry n, Bool)]
xs ]
                (_, xs'') <- [(Name n, Entry n, Bool)]
-> Check n (Bool, [(Name n, Entry n, Bool)])
go [(Name n, Entry n, Bool)]
xs'
                pure (True, (x, abstractOver a aEntry xEntry, True) : xs'')
              else do
                (used, xs'') <- [(Name n, Entry n, Bool)]
-> Check n (Bool, [(Name n, Entry n, Bool)])
go [(Name n, Entry n, Bool)]
xs
                pure (used, (x, xEntry, changed) : xs'')

      -- Give an entry the assumption as an explicit parameter.
      abstractOver Name n
a Entry n
aEntry Entry n
entry =
        let aType :: Core n
aType = Entry n -> Core n
typeCoreOf Entry n
aEntry
            cube :: Bool
cube = Scope n -> Context n -> Value n -> Bool
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Bool
isCube Scope n
scope Context n
ctx (Entry n -> Value n
forall (n :: S). Entry n -> Value n
entryType Entry n
aEntry)
            mshape :: Maybe (Core n)
mshape = if Bool
cube then Core n -> Maybe (Core n)
forall a. a -> Maybe a
Just (Scope n -> Context n -> Value n -> Core n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Value n -> Core n
quoteFolded Scope n
scope Context n
ctx (Scope n -> Value n -> Value n
forall (n :: S). Distinct n => Scope n -> Value n -> Value n
topShapeValue Scope n
scope (Entry n -> Value n
forall (n :: S). Entry n -> Value n
entryType Entry n
aEntry))) else Maybe (Core n)
forall a. Maybe a
Nothing
            family :: Bool
family = Bool
cube Bool -> Bool -> Bool
&& Value n -> Bool
forall {n :: S}. Value Pattern Sig n -> Bool
isTopeUniverse (Entry n -> Value n
forall (n :: S). Entry n -> Value n
entryType Entry n
entry)
            newType :: Core n
newType = Scope n
-> Maybe VarIdent
-> Name n
-> Core n
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    Pattern n l -> Core l -> Core n)
-> Core n
forall (n :: S) r.
Distinct n =>
Scope n
-> Maybe VarIdent
-> Name n
-> Core n
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    Pattern n l -> Core l -> r)
-> r
abstractName Scope n
scope (Entry n -> Maybe VarIdent
forall (n :: S). Entry n -> Maybe VarIdent
entryName Entry n
aEntry) Name n
a (Entry n -> Core n
typeCoreOf Entry n
entry) ((forall (l :: S).
  (DExt n l, Distinct l) =>
  Pattern n l -> Core l -> Core n)
 -> Core n)
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    Pattern n l -> Core l -> Core n)
-> Core n
forall a b. (a -> b) -> a -> b
$ \Pattern n l
pat Core l
body ->
              if Bool
family
                then TModality -> Core n -> Maybe (Core n) -> Core n
forall (n :: S). TModality -> Core n -> Maybe (Core n) -> Core n
TypeShape TModality
Id Core n
aType Maybe (Core n)
mshape
                else TModality -> Core n -> Maybe (Core n) -> ScopedCore n -> Core n
forall (n :: S).
TModality -> Core n -> Maybe (Core n) -> ScopedCore n -> Core n
TypeFun TModality
Id Core n
aType Maybe (Core n)
mshape (Pattern n l -> Core l -> ScopedCore n
forall (binder :: S -> S -> *) (n :: S) (l :: S)
       (sig :: * -> * -> *).
binder n l -> AST binder sig l -> ScopedAST binder sig n
ScopedAST Pattern n l
pat Core l
body)
            abstractBody :: Core n -> Core n
abstractBody Core n
body = Scope n
-> Maybe VarIdent
-> Name n
-> Core n
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    Pattern n l -> Core l -> Core n)
-> Core n
forall (n :: S) r.
Distinct n =>
Scope n
-> Maybe VarIdent
-> Name n
-> Core n
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    Pattern n l -> Core l -> r)
-> r
abstractName Scope n
scope (Entry n -> Maybe VarIdent
forall (n :: S). Entry n -> Maybe VarIdent
entryName Entry n
aEntry) Name n
a Core n
body ((forall (l :: S).
  (DExt n l, Distinct l) =>
  Pattern n l -> Core l -> Core n)
 -> Core n)
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    Pattern n l -> Core l -> Core n)
-> Core n
forall a b. (a -> b) -> a -> b
$ \Pattern n l
pat Core l
body' ->
              Maybe (Param (Core n)) -> ScopedCore n -> Core n
forall (n :: S). Maybe (Param (Core n)) -> ScopedCore n -> Core n
Lambda (Param (Core n) -> Maybe (Param (Core n))
forall a. a -> Maybe a
Just (TModality -> Core n -> Maybe (Core n) -> Param (Core n)
forall term. TModality -> term -> Maybe term -> Param term
Param TModality
Id Core n
aType Maybe (Core n)
mshape)) (Pattern n l -> Core l -> ScopedCore n
forall (binder :: S -> S -> *) (n :: S) (l :: S)
       (sig :: * -> * -> *).
binder n l -> AST binder sig l -> ScopedAST binder sig n
ScopedAST Pattern n l
pat Core l
body')
        in Entry n
entry
             { entryTypeCore = Just newType
             , entryValueCore = abstractBody <$> entryValueCore entry
             , entryModality = Id
             , entryUses = filter (not . sameName a) (entryUses entry)
             }

      -- Rewrite every use of a definition into its application to the
      -- assumption it has just been abstracted over.
      applyToAssumption Name n
a Name n
x Entry n
entry = Entry n
entry
        { entryTypeCore = Just (rewrite (typeCoreOf entry))
        , entryValueCore = rewrite <$> entryValueCore entry
        }
        where rewrite :: Core n -> Core n
rewrite = Scope n -> Name n -> Core n -> Core n -> Core n
forall (n :: S).
Distinct n =>
Scope n -> Name n -> Core n -> Core n -> Core n
substituteName Scope n
scope Name n
x (Core n -> Core n -> Core n
forall (n :: S). Core n -> Core n -> Core n
App (Name n -> Core n
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> AST binder sig n
Var Name n
x) (Name n -> Core n
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> AST binder sig n
Var Name n
a))

      -- The entries newest first; the definitions met so far, oldest first.
      collect [(Name n, Entry n, Bool)]
recent [] = ([(Name n, Entry n, Bool)], [ErrorInContext])
-> Check n ([(Name n, Entry n, Bool)], [ErrorInContext])
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([(Name n, Entry n, Bool)]
recent, [])
      collect [(Name n, Entry n, Bool)]
recent ((Name n
name, Entry n
entry) : [(Name n, Entry n)]
more)
        | Entry n -> Bool
forall {n :: S}. Entry n -> Bool
isAssumptionEntry Entry n
entry = do
            (used, recent') <- Name n
-> Entry n
-> [(Name n, Entry n, Bool)]
-> Check n (Bool, [(Name n, Entry n, Bool)])
abstractAssumption Name n
name Entry n
entry [(Name n, Entry n, Bool)]
recent
            unused <- if used || not reportUnused
              then pure []
              else fmap pure $ captureError $
                localContext (\Context n
c -> Context n
c { ctxLocation = entryLocation entry <|> ctxLocation c }) $
                  issueTypeError (TypeErrorUnusedVariable name (entryType entry))
            (kept, unusedMore) <- collect recent' more
            pure (kept, unused <> unusedMore)
        | Bool
otherwise = [(Name n, Entry n, Bool)]
-> [(Name n, Entry n)]
-> Check n ([(Name n, Entry n, Bool)], [ErrorInContext])
collect ((Name n
name, Entry n
entry, Bool
False) (Name n, Entry n, Bool)
-> [(Name n, Entry n, Bool)] -> [(Name n, Entry n, Bool)]
forall a. a -> [a] -> [a]
: [(Name n, Entry n, Bool)]
recent) [(Name n, Entry n)]
more

  (kept, unusedAssumptions) <- collect [] entries

  -- an abstracted entry is evaluated again, in the context it is now in
  let reevaluate Entry n
entry =
        let ty :: Value n
ty = Scope n -> Context n -> Core n -> Value n
forall (n :: S).
Distinct n =>
Scope n -> Context n -> Core n -> Value n
evalCore Scope n
scope Context n
ctx' (Entry n -> Core n
typeCoreOf Entry n
entry)
        in Entry n
entry
             { entryType = ty
             , entryValue = evalCore scope ctx' <$> entryValueCore entry
             }
      final = [ (Name n
name, if Bool
changed then Entry n -> Entry n
reevaluate Entry n
entry else Entry n
entry) | (Name n
name, Entry n
entry, Bool
changed) <- [(Name n, Entry n, Bool)]
kept ]
      ctx1 = Context n
ctx
        { ctxSections = closeInnermost (ctxSections ctx)
        , ctxBound = filter (not . isAssumption) (ctxBound ctx)
        , ctxNamed = Map.filter (not . isAssumption) (ctxNamed ctx)
        }
      ctx' = ((Name n, Entry n) -> Context n -> Context n)
-> Context n -> [(Name n, Entry n)] -> Context n
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ((Name n -> Entry n -> Context n -> Context n)
-> (Name n, Entry n) -> Context n -> Context n
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Name n -> Entry n -> Context n -> Context n
forall (n :: S). Name n -> Entry n -> Context n -> Context n
insertEntry) Context n
ctx1 [(Name n, Entry n)]
final

      -- The section's definitions outlive it, so the enclosing section
      -- adopts them, to abstract them over its own assumptions in turn.
      closeInnermost [Section n]
sections = case Int -> [Section n] -> [Section n]
forall a. Int -> [a] -> [a]
drop Int
1 [Section n]
sections of
        []       -> []
        Section n
s : [Section n]
rest -> Section n
s { sectionEntries = reverse (map fst final) <> sectionEntries s } Section n -> [Section n] -> [Section n]
forall a. a -> [a] -> [a]
: [Section n]
rest

      decls =
        [ Decl
            { declName :: VarIdent
declName = VarIdent -> Maybe VarIdent -> VarIdent
forall a. a -> Maybe a -> a
fromMaybe VarIdent
"_" (Entry n -> Maybe VarIdent
forall (n :: S). Entry n -> Maybe VarIdent
entryName Entry n
entry)
            , declNameOf :: Name n
declNameOf = Name n
name
            , declType :: Value n
declType = Entry n -> Value n
forall (n :: S). Entry n -> Value n
entryType Entry n
entry
            , declValue :: Maybe (Value n)
declValue = Entry n -> Maybe (Value n)
forall (n :: S). Entry n -> Maybe (Value n)
entryValue Entry n
entry
            , declLocation :: Maybe LocationInfo
declLocation = Entry n -> Maybe LocationInfo
forall (n :: S). Entry n -> Maybe LocationInfo
entryLocation Entry n
entry Maybe LocationInfo -> Maybe LocationInfo -> Maybe LocationInfo
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Context n -> Maybe LocationInfo
forall (n :: S). Context n -> Maybe LocationInfo
ctxLocation Context n
ctx
            }
        | (Name n
name, Entry n
entry) <- [(Name n, Entry n)]
final ]

  -- the uses that name an assumption of this section which went unused
  unusedUses <- if not reportUnused then pure [] else
    fmap concat $ forM final $ \(Name n
name, Entry n
entry) ->
      case (Name n -> Bool) -> [Name n] -> [Name n]
forall a. (a -> Bool) -> [a] -> [a]
filter Name n -> Bool
isAssumption (Entry n -> [Name n]
forall (n :: S). Entry n -> [Name n]
entryUses Entry n
entry) of
        [] -> [ErrorInContext] -> Check n [ErrorInContext]
forall a. a -> Check n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
        [Name n]
unused -> (ErrorInContext -> [ErrorInContext])
-> Check n ErrorInContext -> Check n [ErrorInContext]
forall a b. (a -> b) -> Check n a -> Check n b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ErrorInContext -> [ErrorInContext]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Check n ErrorInContext -> Check n [ErrorInContext])
-> Check n ErrorInContext -> Check n [ErrorInContext]
forall a b. (a -> b) -> a -> b
$ Check n () -> Check n ErrorInContext
forall (n :: S). Check n () -> Check n ErrorInContext
captureError (Check n () -> Check n ErrorInContext)
-> Check n () -> Check n ErrorInContext
forall a b. (a -> b) -> a -> b
$
          (Context n -> Context n) -> Check n () -> Check n ()
forall (n :: S) a.
(Context n -> Context n) -> Check n a -> Check n a
localContext (\Context n
c -> Context n
c { ctxLocation = entryLocation entry <|> ctxLocation c }) (Check n () -> Check n ()) -> Check n () -> Check n ()
forall a b. (a -> b) -> a -> b
$
            TypeError n -> Check n ()
forall (n :: S) a. Distinct n => TypeError n -> Check n a
issueTypeError ([Name n] -> Name n -> TypeError n
forall (n :: S). [Name n] -> Name n -> TypeError n
TypeErrorUnusedUsedVariables [Name n]
unused Name n
name)

  pure (decls, errs <> unusedAssumptions <> unusedUses, ctx')
  where
    sameName :: a -> a -> Bool
sameName a
a a
b = a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
b
    isTopeUniverse :: Value Pattern Sig n -> Bool
isTopeUniverse Value Pattern Sig n
ty = case Value Pattern Sig n -> Whnf Pattern Sig n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (n :: S).
Value binder sig n -> Whnf binder sig n
NbE.force Value Pattern Sig n
ty of
      Whnf Pattern Sig n
VUniverseTope -> Bool
True
      Whnf Pattern Sig n
_                       -> Bool
False

-- | Report a command's error and go on to the next one.
skipping
  :: forall n r. (Distinct n)
  => ErrorInContext -> Maybe FilePath -> Integer -> Integer -> [Rzk.Command]
  -> (forall l. (DExt n l, Distinct l) => [Decl l] -> [ErrorInContext] -> Check l r)
  -> Check n r
skipping :: forall (n :: S) r.
Distinct n =>
ErrorInContext
-> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
skipping ErrorInContext
err Maybe String
path Integer
i Integer
total [Command]
more forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k =
  Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
checkCommands Maybe String
path (Integer
i Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1) Integer
total [Command]
more ((forall (l :: S).
  (DExt n l, Distinct l) =>
  [Decl l] -> [ErrorInContext] -> Check l r)
 -> Check n r)
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall a b. (a -> b) -> a -> b
$ \[Decl l]
decls [ErrorInContext]
errs -> [Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k [Decl l]
decls (ErrorInContext
err ErrorInContext -> [ErrorInContext] -> [ErrorInContext]
forall a. a -> [a] -> [a]
: [ErrorInContext]
errs)

-- | A trace at normal verbosity.
traceNormal :: String -> Check n a -> Check n a
traceNormal :: forall (n :: S) a. String -> Check n a -> Check n a
traceNormal String
msg Check n a
action = do
  verbosity <- (Context n -> Verbosity) -> Check n Verbosity
forall (n :: S) a. (Context n -> a) -> Check n a
asksContext (Options -> Verbosity
optVerbosity (Options -> Verbosity)
-> (Context n -> Options) -> Context n -> Verbosity
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Context n -> Options
forall (n :: S). Context n -> Options
ctxOptions)
  if verbosity <= Normal then trace msg action else action

-- | Check one module.
checkModule
  :: forall n r. (Distinct n)
  => Maybe FilePath -> Rzk.Module
  -> (forall l. (DExt n l, Distinct l) => [Decl l] -> [ErrorInContext] -> Check l r)
  -> Check n r
checkModule :: forall (n :: S) r.
Distinct n =>
Maybe String
-> Module
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
checkModule Maybe String
path (Rzk.Module BNFC'Position
_loc LanguageDecl' BNFC'Position
_lang [Command]
commands) forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k =
  Maybe String
-> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
Maybe String
-> Maybe String
-> Integer
-> Integer
-> [Command]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
withSection Maybe String
forall a. Maybe a
Nothing Maybe String
path Integer
1 ([Command] -> Integer
forall i a. Num i => [a] -> i
genericLength [Command]
commands) [Command]
commands [Decl l] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[Decl l] -> [ErrorInContext] -> Check l r
k

-- | Check a list of modules, one after another, in a scope that grows as
-- it goes. Checking stops at the first module with an error.
checkModules
  :: forall n r. (Distinct n)
  => [(FilePath, Rzk.Module)]
  -> (forall l. (DExt n l, Distinct l) => [(FilePath, [Decl l])] -> [ErrorInContext] -> Check l r)
  -> Check n r
checkModules :: forall (n :: S) r.
Distinct n =>
[(String, Module)]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [(String, [Decl l])] -> [ErrorInContext] -> Check l r)
-> Check n r
checkModules [] forall (l :: S).
(DExt n l, Distinct l) =>
[(String, [Decl l])] -> [ErrorInContext] -> Check l r
k = [(String, [Decl n])] -> [ErrorInContext] -> Check n r
forall (l :: S).
(DExt n l, Distinct l) =>
[(String, [Decl l])] -> [ErrorInContext] -> Check l r
k [] []
checkModules ((String
path, Module
m) : [(String, Module)]
ms) forall (l :: S).
(DExt n l, Distinct l) =>
[(String, [Decl l])] -> [ErrorInContext] -> Check l r
k =
  String -> Check n r -> Check n r
forall (n :: S) a. String -> Check n a -> Check n a
traceNormal (String
"Checking module from " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
path) (Check n r -> Check n r) -> Check n r -> Check n r
forall a b. (a -> b) -> a -> b
$
    LocationInfo -> Check n r -> Check n r
forall (n :: S) a. LocationInfo -> Check n a -> Check n a
withLocation (Maybe String -> Maybe Int -> Maybe Int -> LocationInfo
LocationInfo (String -> Maybe String
forall a. a -> Maybe a
Just String
path) Maybe Int
forall a. Maybe a
Nothing Maybe Int
forall a. Maybe a
Nothing) (Check n r -> Check n r) -> Check n r -> Check n r
forall a b. (a -> b) -> a -> b
$
      Maybe String
-> Module
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall (n :: S) r.
Distinct n =>
Maybe String
-> Module
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
checkModule (String -> Maybe String
forall a. a -> Maybe a
Just String
path) Module
m ((forall (l :: S).
  (DExt n l, Distinct l) =>
  [Decl l] -> [ErrorInContext] -> Check l r)
 -> Check n r)
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [Decl l] -> [ErrorInContext] -> Check l r)
-> Check n r
forall a b. (a -> b) -> a -> b
$ \[Decl l]
decls [ErrorInContext]
errs ->
        case [ErrorInContext]
errs of
          ErrorInContext
_ : [ErrorInContext]
_ -> [(String, [Decl l])] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[(String, [Decl l])] -> [ErrorInContext] -> Check l r
k [(String
path, [Decl l]
decls)] [ErrorInContext]
errs
          []    -> [(String, Module)]
-> (forall (l :: S).
    (DExt l l, Distinct l) =>
    [(String, [Decl l])] -> [ErrorInContext] -> Check l r)
-> Check l r
forall (n :: S) r.
Distinct n =>
[(String, Module)]
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    [(String, [Decl l])] -> [ErrorInContext] -> Check l r)
-> Check n r
checkModules [(String, Module)]
ms ((forall (l :: S).
  (DExt l l, Distinct l) =>
  [(String, [Decl l])] -> [ErrorInContext] -> Check l r)
 -> Check l r)
-> (forall (l :: S).
    (DExt l l, Distinct l) =>
    [(String, [Decl l])] -> [ErrorInContext] -> Check l r)
-> Check l r
forall a b. (a -> b) -> a -> b
$ \[(String, [Decl l])]
rest [ErrorInContext]
errors ->
            [(String, [Decl l])] -> [ErrorInContext] -> Check l r
forall (l :: S).
(DExt n l, Distinct l) =>
[(String, [Decl l])] -> [ErrorInContext] -> Check l r
k ((String
path, [Decl l] -> [Decl l]
forall (n :: S) (l :: S). DExt n l => [Decl n] -> [Decl l]
sinkDecls [Decl l]
decls) (String, [Decl l]) -> [(String, [Decl l])] -> [(String, [Decl l])]
forall a. a -> [a] -> [a]
: [(String, [Decl l])]
rest) [ErrorInContext]
errors