Opaque environment extension state.
Equations
Instances For
Equations
- Lean.instInhabitedModuleIdx = { default := 0 }
Equations
Instances For
Equations
- Lean.instReprImport = { reprPrec := Lean.reprImport✝ }
Equations
- Lean.instInhabitedImport = { default := { module := default, runtimeOnly := default } }
Equations
- Lean.instCoeNameImport = { coe := fun (x : Lake.Name) => { module := x, runtimeOnly := false } }
Equations
- Lean.instToStringImport = { toString := fun (imp : Lean.Import) => toString imp.module ++ if imp.runtimeOnly = true then " (runtime)" else "" }
A compacted region holds multiple Lean objects in a contiguous memory region, which can be read/written to/from disk. Objects inside the region do not have reference counters and cannot be freed individually. The contents of .olean files are compacted regions.
Equations
Instances For
Free a compacted region and its contents. No live references to the contents may exist at the time of invocation.
Instances For
Content of a .olean file.
We use compact.cpp
to generate the image of this object in disk.
- imports : Array Lean.Import
constNames
contains all constant names inconstants
. This information is redundant. It is equal toconstants.map fun c => c.name
, but it improves the performance ofimportModules
.perf
reports that 12% of the runtime was being spent onConstantInfo.name
when importing a file containing onlyimport Lean
- constants : Array Lean.ConstantInfo
Extra entries for the
const2ModIdx
map in theEnvironment
object. The code generator creates auxiliary declarations that are not in the mappingconstants
, but we want to know in which module they were generated.- entries : Array (Lake.Name × Array Lean.EnvExtensionEntry)
Instances For
Equations
- Lean.instInhabitedModuleData = { default := { imports := default, constNames := default, constants := default, extraConstNames := default, entries := default } }
Environment fields that are not used often.
- trustLevel : UInt32
The trust level used by the kernel. For example, the kernel assumes imported constants are type correct when the trust level is greater than zero.
- quotInit : Bool
- mainModule : Lake.Name
Name of the module being compiled.
- imports : Array Lean.Import
Direct imports
- regions : Array Lean.CompactedRegion
Compacted regions for all imported modules. Objects in compacted memory regions do no require any memory management.
Name of all imported modules (directly and indirectly).
- moduleData : Array Lean.ModuleData
Module data for all imported modules.
Instances For
An environment stores declarations provided by the user. The kernel
currently supports different kinds of declarations such as definitions, theorems,
and inductive families. Each has a unique identifier (i.e., Name
), and can be
parameterized by a sequence of universe parameters.
A constant in Lean is just a reference to a ConstantInfo
object. The main task of
the kernel is to type check these declarations and refuse type incorrect ones. The
kernel does not allow declarations containing metavariables and/or free variables
to be added to an environment. Environments are never destructively updated.
The environment also contains a collection of extensions. For example, the simp
theorems
declared by users are stored in an environment extension. Users can declare new extensions
using meta-programming.
- const2ModIdx : Lean.HashMap Lake.Name Lean.ModuleIdx
Mapping from constant name to module (index) where constant has been declared. Recall that a Lean file has a header where previously compiled modules can be imported. Each imported module has a unique
ModuleIdx
. Many extensions use theModuleIdx
to efficiently retrieve information stored in imported modules.Remark: this mapping also contains auxiliary constants, created by the code generator, that are not in the field
constants
. These auxiliary constants are invisible to the Lean kernel and elaborator. Only the code generator uses them. - constants : Lean.ConstMap
Mapping from constant name to
ConstantInfo
. It contains all constants (definitions, theorems, axioms, etc) that have been already type checked by the kernel. - extensions : Array Lean.EnvExtensionState
Environment extensions. It also includes user-defined extensions.
- extraConstNames : Lean.NameSet
Constant names to be saved in the field
extraConstNames
atModuleData
. It contains auxiliary declaration names created by the code generator which are not inconstants
. When importing modules, we want to insert them atconst2ModIdx
. - header : Lean.EnvironmentHeader
The header contains additional information that is not updated often.
Instances For
Equations
Save an extra constant name that is used to populate const2ModIdx
when we import
.olean files. We use this feature to save in which module an auxiliary declaration
created by the code generator has been created.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- Lean.Environment.find? env n = Lean.SMap.find?' env.constants n
Instances For
Equations
- Lean.Environment.contains env n = Lean.SMap.contains env.constants n
Instances For
Equations
- Lean.Environment.imports env = env.header.imports
Instances For
Equations
- Lean.Environment.allImportedModuleNames env = env.header.moduleNames
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- Lean.Environment.mainModule env = env.header.mainModule
Instances For
Equations
- Lean.Environment.getModuleIdxFor? env declName = Lean.HashMap.find? env.const2ModIdx declName
Instances For
Equations
- Lean.Environment.isConstructor env declName = match Lean.Environment.find? env declName with | some (Lean.ConstantInfo.ctorInfo val) => true | x => false
Instances For
Equations
- Lean.Environment.getModuleIdx? env moduleName = Array.findIdx? env.header.moduleNames fun (x : Lake.Name) => x == moduleName
Instances For
Exceptions that can be raised by the Kernel when type checking new declarations.
- unknownConstant: Lean.Environment → Lake.Name → Lean.KernelException
- alreadyDeclared: Lean.Environment → Lake.Name → Lean.KernelException
- declTypeMismatch: Lean.Environment → Lean.Declaration → Lean.Expr → Lean.KernelException
- declHasMVars: Lean.Environment → Lake.Name → Lean.Expr → Lean.KernelException
- declHasFVars: Lean.Environment → Lake.Name → Lean.Expr → Lean.KernelException
- funExpected: Lean.Environment → Lean.LocalContext → Lean.Expr → Lean.KernelException
- typeExpected: Lean.Environment → Lean.LocalContext → Lean.Expr → Lean.KernelException
- letTypeMismatch: Lean.Environment → Lean.LocalContext → Lake.Name → Lean.Expr → Lean.Expr → Lean.KernelException
- exprTypeMismatch: Lean.Environment → Lean.LocalContext → Lean.Expr → Lean.Expr → Lean.KernelException
- appTypeMismatch: Lean.Environment → Lean.LocalContext → Lean.Expr → Lean.Expr → Lean.Expr → Lean.KernelException
- invalidProj: Lean.Environment → Lean.LocalContext → Lean.Expr → Lean.KernelException
- other: String → Lean.KernelException
- deterministicTimeout: Lean.KernelException
- excessiveMemory: Lean.KernelException
- deepRecursion: Lean.KernelException
- interrupted: Lean.KernelException
Instances For
Type check given declaration and add it to the environment
Equations
Instances For
Equations
Instances For
Interface for managing environment extensions.
- setState : {σ : Type} → self.ext σ → Array Lean.EnvExtensionState → σ → Array Lean.EnvExtensionState
- modifyState : {σ : Type} → self.ext σ → Array Lean.EnvExtensionState → (σ → σ) → Array Lean.EnvExtensionState
- getState : {σ : Type} → [inst : Inhabited σ] → self.ext σ → Array Lean.EnvExtensionState → σ
- mkInitialExtStates : IO (Array Lean.EnvExtensionState)
- ensureExtensionsSize : Array Lean.EnvExtensionState → IO (Array Lean.EnvExtensionState)
Instances For
Equations
- One or more equations did not get rendered due to their size.
Unsafe implementation of EnvExtensionInterface
#
Equations
- Lean.EnvExtensionInterfaceUnsafe.instInhabitedExt = { default := { idx := default, mkInitial := default } }
User-defined environment extensions are declared using the initialize
command.
This command is just syntax sugar for the init
attribute.
When we import
lean modules, the vector stored at envExtensionsRef
may increase in size because of
user-defined environment extensions. When this happens, we must adjust the size of the env.extensions
.
This method is invoked when processing import
s.
Equations
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
Instances For
Equations
- Lean.EnvExtension.instInhabitedEnvExtension = Lean.EnvExtensionInterfaceImp.inhabitedExt s
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- Lean.EnvExtension.getState ext env = Lean.EnvExtensionInterfaceImp.getState ext env.extensions
Instances For
Environment extensions can only be registered during initialization.
Reasons:
1- Our implementation assumes the number of extensions does not change after an environment object is created.
2- We do not use any synchronization primitive to access envExtensionsRef
.
Note that by default, extension state is not stored in .olean files and will not propagate across import
s.
For that, you need to register a persistent environment extension.
Equations
- Lean.registerEnvExtension mkInitial = Lean.EnvExtensionInterfaceImp.registerExt mkInitial
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
Instances For
An environment extension with support for storing/retrieving entries from a .olean file.
- α is the type of the entries that are stored in .olean files.
- β is the type of values used to update the state.
- σ is the actual state.
Remark: for most extensions α and β coincide.
Note that addEntryFn
is not in IO
. This is intentional, and allows us to write simple functions such as
def addAlias (env : Environment) (a : Name) (e : Name) : Environment :=
aliasExtension.addEntry env (a, e)
without using IO
. We have many functions like addAlias
.
α
and ‵β` do not coincide for extensions where the data used to update the state contains, for example,
closures which we currently cannot store in files.
- toEnvExtension : Lean.EnvExtension (Lean.PersistentEnvExtensionState α σ)
- name : Lake.Name
- addImportedFn : Array (Array α) → Lean.ImportM σ
- addEntryFn : σ → β → σ
- exportEntriesFn : σ → Array α
- statsFn : σ → Lean.Format
Instances For
Equations
- Lean.instInhabitedPersistentEnvExtensionState = { default := { importedEntries := #[], state := default } }
Equations
- One or more equations did not get rendered due to their size.
Equations
- Lean.PersistentEnvExtension.getModuleEntries ext env m = Array.get! (Lean.EnvExtension.getState ext.toEnvExtension env).importedEntries m
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Get the current state of the given extension in the given environment.
Equations
- Lean.PersistentEnvExtension.getState ext env = (Lean.EnvExtension.getState ext.toEnvExtension env).state
Instances For
Set the current state of the given extension in the given environment. This change is not persisted across files.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Modify the state of the given extension in the given environment by applying the given function. This change is not persisted across files.
Equations
- One or more equations did not get rendered due to their size.
Instances For
- name : Lake.Name
- mkInitial : IO σ
- addImportedFn : Array (Array α) → Lean.ImportM σ
- addEntryFn : σ → β → σ
- exportEntriesFn : σ → Array α
- statsFn : σ → Lean.Format
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Simple PersistentEnvExtension
that implements exportEntriesFn
using a list of entries.
Equations
- Lean.SimplePersistentEnvExtension α σ = Lean.PersistentEnvExtension α α (List α × σ)
Instances For
Equations
- Lean.mkStateFromImportedEntries addEntryFn initState as = Array.foldl (fun (r : σ) (es : Array α) => Array.foldl (fun (r : σ) (e : α) => addEntryFn r e) r es 0) initState as 0
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- Lean.SimplePersistentEnvExtension.instInhabitedSimplePersistentEnvExtension = inferInstanceAs (Inhabited (Lean.PersistentEnvExtension α α (List α × σ)))
Get the list of values used to update the state of the given
SimplePersistentEnvExtension
in the current file.
Equations
- Lean.SimplePersistentEnvExtension.getEntries ext env = (Lean.PersistentEnvExtension.getState ext env).fst
Instances For
Get the current state of the given SimplePersistentEnvExtension
.
Equations
- Lean.SimplePersistentEnvExtension.getState ext env = (Lean.PersistentEnvExtension.getState ext env).snd
Instances For
Set the current state of the given SimplePersistentEnvExtension
. This change is not persisted across files.
Equations
- Lean.SimplePersistentEnvExtension.setState ext env s = Lean.PersistentEnvExtension.modifyState ext env fun (x : List α × σ) => match x with | (entries, snd) => (entries, s)
Instances For
Modify the state of the given extension in the given environment by applying the given function. This change is not persisted across files.
Equations
- Lean.SimplePersistentEnvExtension.modifyState ext env f = Lean.PersistentEnvExtension.modifyState ext env fun (x : List α × σ) => match x with | (entries, s) => (entries, f s)
Instances For
Environment extension for tagging declarations. Declarations must only be tagged in the module where they were declared.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Environment extension for mapping declarations to values. Declarations must only be inserted into the mapping in the module where they were declared.
Equations
Instances For
Equations
- Lean.MapDeclarationExtension.instInhabitedMapDeclarationExtension = inferInstanceAs (Inhabited (Lean.SimplePersistentEnvExtension (Lake.Name × α) (Lake.NameMap α)))
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Free compacted regions of imports. No live references to imported objects may exist at the time of invocation; in
particular, env
should be the last reference to any Environment
derived from these imports.
Equations
- Lean.Environment.freeRegions env = Array.forM Lean.CompactedRegion.free env.header.regions 0
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- Lean.writeModule env fname = do let __do_lift ← Lean.mkModuleData env Lean.saveModuleData fname (Lean.Environment.mainModule env) __do_lift
Instances For
Construct a mapping from persistent extension name to entension index at the array of persistent extensions.
We only consider extensions starting with index >= startingAt
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
"Forward declaration" needed for updating the attribute table with user-defined attributes.
User-defined attributes are declared using the initialize
command. The initialize
command is just syntax sugar for the init
attribute.
The init
attribute is initialized after the attributeExtension
is initialized. We cannot change the order since the init
attribute is an attribute,
and requires this extension.
The attributeExtension
initializer uses attributeMapRef
to initialize the attribute mapping.
When we a new user-defined attribute declaration is imported, attributeMapRef
is updated.
Later, we set this method with code that adds the user-defined attributes that were imported after we initialized attributeExtension
.
"Forward declaration" for retrieving the number of builtin attributes.
- moduleNameSet : Lean.NameHashSet
- moduleData : Array Lean.ModuleData
- regions : Array Lean.CompactedRegion
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Instances For
Equations
- Lean.ImportStateM.run x s = StateRefT'.run x s
Instances For
Construct environment from importModulesCore
results.
If leakEnv
is true, we mark the environment as persistent, which means it
will not be freed. We set this when the object would survive until the end of
the process anyway. In exchange, RC updates are avoided, which is especially
important when they would be atomic because the environment is shared across
threads (potentially, storing it in an IO.Ref
is sufficient for marking it
as such).
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Create environment object from imports and free compacted regions after calling act
. No live references to the
environment object or imported objects may exist after act
finishes.
Equations
- Lean.withImportModules imports opts trustLevel act = do let env ← Lean.importModules imports opts trustLevel tryFinally (act env) (Lean.Environment.freeRegions env)
Instances For
Environment extension for tracking all namespace
declared by users.
Register a new namespace in the environment.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Evaluate the given declaration under the given environment to a value of the given type.
This function is only safe to use if the type matches the declaration's type in the environment
and if enableInitializersExecution
has been used before importing any modules.
Like evalConst
, but first check that constName
indeed is a declaration of type typeName
.
Note that this function cannot guarantee that typeName
is in fact the name of the type α
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Kernel API #
Kernel isDefEq predicate. We use it mainly for debugging purposes.
Recall that the Kernel type checker does not support metavariables.
When implementing automation, consider using the MetaM
methods.
Equations
- Lean.Kernel.isDefEqGuarded env lctx a b = match Lean.Kernel.isDefEq env lctx a b with | Except.ok result => result | x => false
Instances For
Kernel WHNF function. We use it mainly for debugging purposes.
Recall that the Kernel type checker does not support metavariables.
When implementing automation, consider using the MetaM
methods.
- getEnv : m Lean.Environment
- modifyEnv : (Lean.Environment → Lean.Environment) → m Unit
Instances
Equations
- Lean.instMonadEnv m n = { getEnv := liftM Lean.getEnv, modifyEnv := fun (f : Lean.Environment → Lean.Environment) => liftM (Lean.modifyEnv f) }