Documentation

Lean.Environment

Opaque environment extension state.

Equations
Instances For
    @[reducible, inline]
    Equations
    • midx.toNat = midx
    Instances For
      @[reducible, inline]
      Equations
      Instances For
        structure Lean.Import :
        Instances For
          Equations
          Equations
          Equations

          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
            @[extern lean_compacted_region_is_memory_mapped]
            @[extern lean_compacted_region_free]

            Free a compacted region and its contents. No live references to the contents may exist at the time of invocation.

            Opaque persistent environment extension entry.

            Content of a .olean file. We use compact.cpp to generate the image of this object in disk.

            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

                quotInit = true if the command init_quot has already been executed for the environment, and Quot declarations have been added to the environment.

              • mainModule : Lean.Name

                Name of the module being compiled.

              • imports : Array Lean.Import

                Direct imports

              • Compacted regions for all imported modules. Objects in compacted memory regions do no require any memory management.

              • moduleNames : Array Lean.Name

                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.

                • 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 the ModuleIdx 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.

                • Environment extensions. It also includes user-defined extensions.

                • extraConstNames : Lean.NameSet

                  Constant names to be saved in the field extraConstNames at ModuleData. It contains auxiliary declaration names created by the code generator which are not in constants. When importing modules, we want to insert them at const2ModIdx.

                • The header contains additional information that is not updated often.

                Instances For

                  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
                    @[export lean_environment_find]
                    Equations
                    Instances For
                      Equations
                      Instances For
                        Equations
                        • env.imports = env.header.imports
                        Instances For
                          Equations
                          • env.allImportedModuleNames = env.header.moduleNames
                          Instances For
                            @[export lean_environment_set_main_module]
                            Equations
                            • One or more equations did not get rendered due to their size.
                            Instances For
                              @[export lean_environment_main_module]
                              Equations
                              • env.mainModule = env.header.mainModule
                              Instances For
                                Equations
                                • env.getModuleIdxFor? declName = env.const2ModIdx[declName]?
                                Instances For
                                  Equations
                                  Instances For
                                    Equations
                                    • One or more equations did not get rendered due to their size.
                                    Instances For
                                      Equations
                                      Instances For

                                        Exceptions that can be raised by the Kernel when type checking new declarations.

                                        Instances For
                                          @[extern lean_add_decl]

                                          Type check given declaration and add it to the environment

                                          @[extern lean_add_decl_without_checking]

                                          Add declaration to kernel without type checking it. WARNING This function is meant for temporarily working around kernel performance issues. It compromises soundness because, for example, a buggy tactic may produce an invalid proof, and the kernel will not catch it if the new option is set to true.

                                          Equations
                                          • c.instantiateTypeLevelParams ls = c.type.instantiateLevelParams c.levelParams ls
                                          Instances For
                                            Equations
                                            • c.instantiateValueLevelParams! ls = c.value!.instantiateLevelParams c.levelParams ls
                                            Instances For

                                              Interface for managing environment extensions.

                                              Instances For
                                                Equations
                                                • One or more equations did not get rendered due to their size.

                                                Unsafe implementation of EnvExtensionInterface #

                                                • idx : Nat
                                                • mkInitial : IO σ
                                                Instances For
                                                  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 imports.

                                                  Equations
                                                  Instances For
                                                    Equations
                                                    • One or more equations did not get rendered due to their size.
                                                    Instances For
                                                      @[inline]
                                                      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
                                                                @[implemented_by Lean.EnvExtensionInterfaceUnsafe.imp]
                                                                Equations
                                                                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
                                                                      def Lean.registerEnvExtension {σ : Type} (mkInitial : IO σ) :

                                                                      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 imports. For that, you need to register a persistent environment extension.

                                                                      Equations
                                                                      Instances For
                                                                        @[export lean_mk_empty_environment]
                                                                        Equations
                                                                        • One or more equations did not get rendered due to their size.
                                                                        Instances For
                                                                          Instances For
                                                                            @[reducible, inline]
                                                                            abbrev Lean.ImportM (α : Type) :
                                                                            Equations
                                                                            Instances For
                                                                              structure Lean.PersistentEnvExtension (α β σ : Type) :

                                                                              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.

                                                                              For most extensions, α and β coincide. α and ‵β` do not coincide for extensions where the data used to update the state contains elements which cannot be stored in files (for example, closures).

                                                                              During elaboration of a module, state of type σ can be both read and written. When elaboration is complete, the state of type σ is converted to serialized state of type Array α by exportEntriesFn. To read the current module's state, use PersistentEnvExtension.getState. To modify it, use PersistentEnvExtension.addEntry, with an addEntryFn that performs the appropriate modification.

                                                                              When a module is loaded, the values saved by all of its dependencies for this PersistentEnvExtension are available as an Array (Array α) via the environment extension, with one array per transitively imported module. The state of type σ used in the current module can be initialized from these imports by specifying a suitable addImportedFn. The addImportedFn runs at the beginning of elaboration for every module, so it's usually better for performance to query the array of imported modules directly, because only a fraction of imported entries is usually queried during elaboration of a module.

                                                                              The most typical pattern for using PersistentEnvExtension is to set σ to a datatype such as NameMap that efficiently tracks data for the current module. Then, in exportEntriesFn, this type is converted to an array of pairs, sorted by the key. Given ext : PersistentEnvExtension α β σ and env : Environment, the complete array of imported entries sorted by module index can be obtained using (ext.toEnvExtension.getState env).importedEntries. To query the extension for some constant name n, first use env.getModuleIdxFor? n. If it returns none, look up n in the current module's state (the NameMap). If it returns some idx, use ext.getModuleEntries env idx to get the array of entries for n's defining module, and query it using Array.binSearch. This pattern imposes a constraint that the extension can only track metadata that is declared in the same module as the definition to which it applies; relaxing this restriction can make queries slower due to needing to search all modules. If it is necessary to search all modules, it is usually better to initialize the state of type σ once from all imported entries and choose a more efficient search datastructure for it.

                                                                              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.

                                                                              Instances For
                                                                                Equations
                                                                                • Lean.instInhabitedPersistentEnvExtensionState = { default := { importedEntries := #[], state := default } }
                                                                                Equations
                                                                                • One or more equations did not get rendered due to their size.
                                                                                Equations
                                                                                • ext.getModuleEntries env m = (ext.toEnvExtension.getState env).importedEntries.get! 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
                                                                                    • ext.getState env = (ext.toEnvExtension.getState env).state
                                                                                    Instances For

                                                                                      Set the current state of the given extension in the given environment.

                                                                                      Equations
                                                                                      Instances For

                                                                                        Modify the state of the given extension in the given environment by applying the given function.

                                                                                        Equations
                                                                                        • ext.modifyState env f = ext.toEnvExtension.modifyState env fun (ps : Lean.PersistentEnvExtensionState α σ) => { importedEntries := ps.importedEntries, state := f ps.state }
                                                                                        Instances For
                                                                                          Instances For
                                                                                            Equations
                                                                                            • One or more equations did not get rendered due to their size.
                                                                                            Instances For
                                                                                              @[implemented_by Lean.registerPersistentEnvExtensionUnsafe]

                                                                                              Simple PersistentEnvExtension that implements exportEntriesFn using a list of entries.

                                                                                              Equations
                                                                                              Instances For
                                                                                                @[specialize #[]]
                                                                                                def Lean.mkStateFromImportedEntries {α σ : Type} (addEntryFn : σασ) (initState : σ) (as : Array (Array α)) :
                                                                                                σ
                                                                                                Equations
                                                                                                Instances For
                                                                                                  Instances For
                                                                                                    Equations
                                                                                                    • One or more equations did not get rendered due to their size.
                                                                                                    Instances For
                                                                                                      Equations

                                                                                                      Get the list of values used to update the state of the given SimplePersistentEnvExtension in the current file.

                                                                                                      Equations
                                                                                                      Instances For

                                                                                                        Get the current state of the given SimplePersistentEnvExtension.

                                                                                                        Equations
                                                                                                        Instances For

                                                                                                          Set the current state of the given SimplePersistentEnvExtension. This change is not persisted across files.

                                                                                                          Equations
                                                                                                          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
                                                                                                            Instances For

                                                                                                              Environment extension for tagging declarations. Declarations must only be tagged in the module where they were declared.

                                                                                                              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

                                                                                                                      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
                                                                                                                        def Lean.mkMapDeclarationExtension {α : Type} (name : Lean.Name := by exact decl_name%) :
                                                                                                                        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
                                                                                                                                @[extern lean_save_module_data]
                                                                                                                                @[extern lean_read_module_data]
                                                                                                                                @[noinline, export lean_environment_free_regions]

                                                                                                                                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
                                                                                                                                Instances For
                                                                                                                                  Equations
                                                                                                                                  • One or more equations did not get rendered due to their size.
                                                                                                                                  Instances For
                                                                                                                                    @[export lean_write_module]
                                                                                                                                    Equations
                                                                                                                                    Instances For

                                                                                                                                      Construct a mapping from persistent extension name to extension 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
                                                                                                                                        @[extern 2 lean_update_env_attributes]

                                                                                                                                        "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.

                                                                                                                                        @[extern 1 lean_get_num_attributes]

                                                                                                                                        "Forward declaration" for retrieving the number of builtin attributes.

                                                                                                                                        Instances For
                                                                                                                                          def Lean.throwAlreadyImported {α : Type} (s : Lean.ImportState) (const2ModIdx : Std.HashMap Lean.Name Lean.ModuleIdx) (modIdx : Nat) (cname : Lean.Name) :
                                                                                                                                          IO α
                                                                                                                                          Equations
                                                                                                                                          • One or more equations did not get rendered due to their size.
                                                                                                                                          Instances For
                                                                                                                                            @[reducible, inline]
                                                                                                                                            abbrev Lean.ImportStateM (α : Type) :
                                                                                                                                            Equations
                                                                                                                                            Instances For
                                                                                                                                              @[inline]
                                                                                                                                              def Lean.ImportStateM.run {α : Type} (x : Lean.ImportStateM α) (s : Lean.ImportState := { moduleNameSet := , moduleNames := #[], moduleData := #[], regions := #[] }) :
                                                                                                                                              Equations
                                                                                                                                              Instances For
                                                                                                                                                def Lean.finalizeImport (s : Lean.ImportState) (imports : Array Lean.Import) (opts : Lean.Options) (trustLevel : UInt32 := 0) (leakEnv : Bool := false) :

                                                                                                                                                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
                                                                                                                                                  @[export lean_import_modules]
                                                                                                                                                  def Lean.importModules (imports : Array Lean.Import) (opts : Lean.Options) (trustLevel : UInt32 := 0) (leakEnv : Bool := false) :
                                                                                                                                                  Equations
                                                                                                                                                  • One or more equations did not get rendered due to their size.
                                                                                                                                                  Instances For
                                                                                                                                                    unsafe def Lean.withImportModules {α : Type} (imports : Array Lean.Import) (opts : Lean.Options) (trustLevel : UInt32 := 0) (act : Lean.EnvironmentIO α) :
                                                                                                                                                    IO α

                                                                                                                                                    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
                                                                                                                                                    Instances For

                                                                                                                                                      Environment extension for tracking all namespace declared by users.

                                                                                                                                                      • unfoldCounter : Lean.PHashMap Lean.Name Nat

                                                                                                                                                        Number of times each declaration has been unfolded by the kernel.

                                                                                                                                                      • enabled : Bool

                                                                                                                                                        If enabled = true, kernel records declarations that have been unfolded.

                                                                                                                                                      Instances For
                                                                                                                                                        Equations

                                                                                                                                                        Extension for storting diagnostic information.

                                                                                                                                                        Remark: We store kernel diagnostic information in an environment extension to simplify the interface with the kernel implemented in C/C++. Thus, we can only track declarations in methods, such as addDecl, which return a new environment. Kernel.isDefEq and Kernel.whnf do not update the statistics. We claim this is ok since these methods are mainly used for debugging.

                                                                                                                                                        @[export lean_kernel_diag_is_enabled]
                                                                                                                                                        Equations
                                                                                                                                                        • d.isEnabled = d.enabled
                                                                                                                                                        Instances For

                                                                                                                                                          Enables/disables kernel diagnostics.

                                                                                                                                                          Equations
                                                                                                                                                          Instances For
                                                                                                                                                            Equations
                                                                                                                                                            • One or more equations did not get rendered due to their size.
                                                                                                                                                            Instances For
                                                                                                                                                              @[export lean_kernel_record_unfold]
                                                                                                                                                              Equations
                                                                                                                                                              • One or more equations did not get rendered due to their size.
                                                                                                                                                              Instances For
                                                                                                                                                                @[export lean_kernel_get_diag]
                                                                                                                                                                Equations
                                                                                                                                                                Instances For
                                                                                                                                                                  @[export lean_kernel_set_diag]
                                                                                                                                                                  Equations
                                                                                                                                                                  Instances For

                                                                                                                                                                    Register a new namespace in the environment.

                                                                                                                                                                    Equations
                                                                                                                                                                    Instances For

                                                                                                                                                                      Return true if n is the name of a namespace in env.

                                                                                                                                                                      Equations
                                                                                                                                                                      Instances For

                                                                                                                                                                        Return a set containing all namespaces in env.

                                                                                                                                                                        Equations
                                                                                                                                                                        Instances For
                                                                                                                                                                          @[export lean_display_stats]
                                                                                                                                                                          Equations
                                                                                                                                                                          • One or more equations did not get rendered due to their size.
                                                                                                                                                                          Instances For
                                                                                                                                                                            @[extern lean_eval_const]
                                                                                                                                                                            unsafe opaque Lean.Environment.evalConst (α : Type u_1) (env : Lean.Environment) (opts : Lean.Options) (constName : Lean.Name) :

                                                                                                                                                                            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.

                                                                                                                                                                            unsafe def Lean.Environment.evalConstCheck (α : Type) (env : Lean.Environment) (opts : Lean.Options) (typeName constName : Lean.Name) :

                                                                                                                                                                            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 #

                                                                                                                                                                                @[extern lean_kernel_is_def_eq]

                                                                                                                                                                                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
                                                                                                                                                                                Instances For
                                                                                                                                                                                  @[extern lean_kernel_whnf]

                                                                                                                                                                                  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.

                                                                                                                                                                                  @[extern lean_kernel_check]

                                                                                                                                                                                  Kernel typecheck 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.

                                                                                                                                                                                  class Lean.MonadEnv (m : TypeType) :
                                                                                                                                                                                  Instances
                                                                                                                                                                                    @[always_inline]
                                                                                                                                                                                    Equations

                                                                                                                                                                                    Constructs a DefinitionVal, inferring the unsafe field

                                                                                                                                                                                    Equations
                                                                                                                                                                                    • One or more equations did not get rendered due to their size.
                                                                                                                                                                                    Instances For