Declaration needs #
This file records the needs of declarations in a "non-baked-in" manner via DeclNeeds.
We record in what manner one declaration needs another (e.g. in its type), and then postpone the
calculation of what import needs this implies. This sets us up to provide finer explanations.
We also define ImportNeeds so that we can express when an import is allowed to be meta (or not).
Note that, crucially, DeclNeeds is agnostic to the source of our import hierarchy, as it uses
module names instead of e.g. ModuleIdxs/ModIdxs. Hence these functions are compatible with both
a WorkspaceModel approach and an Environment approach.
This file culminates in withElabCommandCapturingNeeds, which elaborates a command and captures
the (transitive) DeclNeeds of declarations produced in that command, including e.g. the needs
implied by the command's syntax.
Future Work #
- Write up more thorough documentation when this settles down.
- Handle meta IR.
- Performance: many of the data structures here could be improved. There's a lot of pointer-chasing.
- Revamp indirect mod uses. Currently they are associated with the declaration indirectly requiring
them and added to the decl import needs specially. Instead, we should split them out. Also, we
currently spoof the
kind. - Split out extra mod uses into a special command needs. Also put the syntax needs there; these should not be decl needs.
- Handle autogenerated declarations more consistently.
- Give a closer look to things like
@[csimp].
Shake declarations #
This section (fromShake) is inlined verbatim from shake, modulo whitespace.
Copyright (c) 2023 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Sebastian Ullrich
Equations
- One or more equations did not get rendered due to their size.
Instances For
Given an Expr reference, returns the declaration name that should be considered the reference, if
any, but from the environment directly.
Equations
- One or more equations did not get rendered due to their size.
Instances For
A way in which a declaration might demand an imported module. Note that there may be multiple demands on the same module from the same declaration, which thus may require multiple of these.
- allowMeta : Bool
A flag for the case where the ambient declaration needs a meta declaration from the needed module. In that case, the import is allowed to be meta, but does not have to be. Note that ordinary expression uses also
allowMeta; it is only usage in the computational content (LCNF) that will disallow meta and set this to false.However, to allow
NeedsKindsconstructors to not conflict with.allowMetaPub, we set this toisMetaby default. Needing a meta import implies
allowMeta = true.
Instances For
Equations
- ImportGraph.Shake.ImportNeedsKind.pubNoMeta = { toNeedsKind := ImportGraph.Shake.NeedsKind.pub, allowMeta := false, allowMeta_if_isMeta := ImportGraph.Shake.ImportNeedsKind.pubNoMeta._proof_2 }
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
- 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.
Equations
Instances For
Like Needs, but preserves allowMeta. We interpret pub/priv/privOfPriv from
NeedsKind as having allowMeta := false, i.e. as specifically disallowing a meta import.
- allowMetaPub : Bitset
The modules which may be imported as meta or non-meta which are needed in the public scope.
- allowMetaPriv : Bitset
The modules which may be imported as meta or non-meta whose public scopes must be available in the current private scope.
- allowMetaPrivOfPriv : Bitset
The modules which may be imported as meta or non-meta whose private scopes must be available in the current private scope.
Instances For
Equations
Equations
Equations
- One or more equations did not get rendered due to their size.
- ImportGraph.Shake.instBEqImportNeeds.beq x✝¹ x✝ = false
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
Equations
- ImportGraph.Shake.ImportNeeds.empty = { toSubNeeds := ImportGraph.Shake.Needs.empty, allowMetaPub := ∅, allowMetaPriv := ∅, allowMetaPrivOfPriv := ∅ }
Instances For
Equations
- ImportGraph.Shake.ImportNeeds.instEmptyCollection = { emptyCollection := ImportGraph.Shake.ImportNeeds.empty }
Turns ImportNeeds into a concrete Needs with an opinionated choice of whether allowMeta
should revert to public imports (useMeta := false) or be regarded as meta imports
(useMeta := true)
Equations
- One or more equations did not get rendered due to their size.
Instances For
Assumes that Provides is well-formed, i.e. is linearized and transitively closed.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- needs.get { isExported := true, isMeta := false, not_isExported_and_isAll := ⋯, allowMeta_if_isMeta := ⋯ } = needs.pub
- needs.get { isExported := false, isMeta := false, not_isExported_and_isAll := ⋯, allowMeta_if_isMeta := ⋯ } = needs.priv
- needs.get { isExported := true, isMeta := true, not_isExported_and_isAll := ⋯, allowMeta_if_isMeta := ⋯ } = needs.metaPub
- needs.get { isExported := false, isMeta := true, not_isExported_and_isAll := ⋯, allowMeta_if_isMeta := ⋯ } = needs.metaPriv
- needs.get { isExported := false, isMeta := false, isAll := true, not_isExported_and_isAll := ⋯, allowMeta_if_isMeta := ⋯ } = needs.privOfPriv
- needs.get { isExported := false, isMeta := true, isAll := true, not_isExported_and_isAll := ⋯, allowMeta_if_isMeta := ⋯ } = needs.metaPrivOfPriv
- needs.get { isExported := true, isMeta := false, not_isExported_and_isAll := ⋯, allowMeta := true, allowMeta_if_isMeta := ⋯ } = needs.allowMetaPub
- needs.get { isExported := false, isMeta := false, not_isExported_and_isAll := ⋯, allowMeta := true, allowMeta_if_isMeta := ⋯ } = needs.allowMetaPriv
- needs.get { isExported := false, isMeta := false, isAll := true, not_isExported_and_isAll := ⋯, allowMeta := true, allowMeta_if_isMeta := ⋯ } = needs.allowMetaPrivOfPriv
Instances For
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
- needs.union k s = needs.modify k fun (x : ImportGraph.Shake.Bitset) => x ∪ s
Instances For
Equations
Instances For
Information about the source of a compile-time dependency.
- indirect
(use : Lean.IndirectModUse)
(imposingModules : Array Lean.Name)
: ComptimeDependency
A potential compile-time dependency due to an indirect mod use.
imposingModulerecords the module that imposed the indirect mod use (not the source module of the declaration). - stx
(kind : Lean.SyntaxNodeKind)
(pos? : Option Lean.Syntax.Range)
: ComptimeDependency
A compile-time dependency due to parsing.
Instances For
Equations
- ImportGraph.Shake.instBEqComptimeDependency.beq (ImportGraph.Shake.ComptimeDependency.indirect a a_1) (ImportGraph.Shake.ComptimeDependency.indirect b b_1) = (a == b && a_1 == b_1)
- ImportGraph.Shake.instBEqComptimeDependency.beq (ImportGraph.Shake.ComptimeDependency.stx a a_1) (ImportGraph.Shake.ComptimeDependency.stx b b_1) = (a == b && a_1 == b_1)
- ImportGraph.Shake.instBEqComptimeDependency.beq x✝¹ x✝ = false
Instances For
Equations
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
Instances For
Equations
Equations
- One or more equations did not get rendered due to their size.
- ImportGraph.Shake.instOrdComptimeDependency.ord (ImportGraph.Shake.ComptimeDependency.indirect a a_1) x✝ = Ordering.lt
- ImportGraph.Shake.instOrdComptimeDependency.ord x✝ (ImportGraph.Shake.ComptimeDependency.indirect a a_1) = Ordering.gt
Instances For
A location at which a ConstantInfo can reference another.
- type : ConstLocation
- value : ConstLocation
Instances For
Equations
- ImportGraph.Shake.instBEqConstLocation.beq x✝ y✝ = (x✝.ctorIdx == y✝.ctorIdx)
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
Equations
- ImportGraph.Shake.instOrdConstLocation.ord x✝ y✝ = compare x✝.ctorIdx y✝.ctorIdx
Instances For
Equations
Equations
- One or more equations did not get rendered due to their size.
A way in which a declaration tgt might need another declaration src. In general, the same
src and tgt may be related by multiple DeclDeclNeedsKinds.
- expr
(downstream : ConstLocation)
(isReExported : Option Bool := none)
(upstream : ConstLocation := ConstLocation.type)
: DeclDeclNeedsKind
Expresses a dependency of
tgt's location given bydownstreamonsrc's location given byupstream.isReExported := noneindicates that the exported value is inherited fromtgt'sisPublicorisExposedstance, as appropriate. (Occasionally this will besome falseif, for example, a proof is abstracted, since we count the dependencies of autogenerated declarations as part of the parent declaration.) - metaIR
(isReExported : Option Bool := none)
(through : List Lean.Name := [])
: DeclDeclNeedsKind
Expresses a dependency of
tgtonsrcdue to the IR oftgt, which is meta-available. WhenisReExported := none, this is inherited from thetgt'sisPublicstance (the same as its type, not its value.)Meta IR is unusual in that while a private meta def may not re-export its references, a public meta def that uses that private meta def will re-export that private meta def's references. Note, also, that this dependency is determined by Lean from the initial LCNF (unless a same-module transitive dependency, in which case it is determined from that declaration's base LCNF), but is intended to capture IR dependency.
- runtimeIR : DeclDeclNeedsKind
Expresses a dependency of
tgtonsrcin the IR oftgt, which is only available at runtime. This does not have the complicated reference semantics of IR used for meta declarations. We assume, for now, that we do not need to think about changing runtime defs to meta defs, and thus do not need to record the same information. As such,checkMetas recursive check for non-aux decls is irrelevant, as the runtime IR constraints such a recursive check puts on a reference are redundant with the runtime IR constraints that already exist on that reference. However, note that, like meta declarations, this dependency is determined by Lean from the initial LCNF, though is intended to capture IR dependency. - comptime
(source : ComptimeDependency)
: DeclDeclNeedsKind
A compile-time dependency of
tgt(e.g. a parser used for notation in the command fortgt).If this arises due to an indirect mod use, we record the declaration that demanded it (if we can). Note that shake extensions do not expose this; this is known during the expression traversal. So some indirect mod uses will end up in
extraModUseswithout any indication as to the declaration that drew them (at least, not in a way that's accessible to us, though this information is traced by shake).We record this separately mainly for reporting.
Instances For
Equations
- ImportGraph.Shake.instBEqDeclDeclNeedsKind.beq (ImportGraph.Shake.DeclDeclNeedsKind.expr a a_1 a_2) (ImportGraph.Shake.DeclDeclNeedsKind.expr b b_1 b_2) = (a == b && (a_1 == b_1 && a_2 == b_2))
- ImportGraph.Shake.instBEqDeclDeclNeedsKind.beq (ImportGraph.Shake.DeclDeclNeedsKind.metaIR a a_1) (ImportGraph.Shake.DeclDeclNeedsKind.metaIR b b_1) = (a == b && a_1 == b_1)
- ImportGraph.Shake.instBEqDeclDeclNeedsKind.beq ImportGraph.Shake.DeclDeclNeedsKind.runtimeIR ImportGraph.Shake.DeclDeclNeedsKind.runtimeIR = true
- ImportGraph.Shake.instBEqDeclDeclNeedsKind.beq (ImportGraph.Shake.DeclDeclNeedsKind.comptime a) (ImportGraph.Shake.DeclDeclNeedsKind.comptime b) = (a == b)
- ImportGraph.Shake.instBEqDeclDeclNeedsKind.beq x✝¹ x✝ = false
Instances For
Equations
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- ImportGraph.Shake.instHashableDeclDeclNeedsKind.hash (ImportGraph.Shake.DeclDeclNeedsKind.expr a a_1 a_2) = mixHash (mixHash (mixHash 0 (hash a)) (hash a_1)) (hash a_2)
- ImportGraph.Shake.instHashableDeclDeclNeedsKind.hash (ImportGraph.Shake.DeclDeclNeedsKind.metaIR a a_1) = mixHash (mixHash 1 (hash a)) (hash a_1)
- ImportGraph.Shake.instHashableDeclDeclNeedsKind.hash ImportGraph.Shake.DeclDeclNeedsKind.runtimeIR = 2
- ImportGraph.Shake.instHashableDeclDeclNeedsKind.hash (ImportGraph.Shake.DeclDeclNeedsKind.comptime a) = mixHash 3 (hash a)
Instances For
Equations
- One or more equations did not get rendered due to their size.
- ImportGraph.Shake.instOrdDeclDeclNeedsKind.ord (ImportGraph.Shake.DeclDeclNeedsKind.expr a a_1 a_2) x✝ = Ordering.lt
- ImportGraph.Shake.instOrdDeclDeclNeedsKind.ord x✝ (ImportGraph.Shake.DeclDeclNeedsKind.expr a a_1 a_2) = Ordering.gt
- ImportGraph.Shake.instOrdDeclDeclNeedsKind.ord (ImportGraph.Shake.DeclDeclNeedsKind.metaIR a a_1) x✝ = Ordering.lt
- ImportGraph.Shake.instOrdDeclDeclNeedsKind.ord x✝ (ImportGraph.Shake.DeclDeclNeedsKind.metaIR a a_1) = Ordering.gt
- ImportGraph.Shake.instOrdDeclDeclNeedsKind.ord ImportGraph.Shake.DeclDeclNeedsKind.runtimeIR ImportGraph.Shake.DeclDeclNeedsKind.runtimeIR = Ordering.eq
- ImportGraph.Shake.instOrdDeclDeclNeedsKind.ord ImportGraph.Shake.DeclDeclNeedsKind.runtimeIR x✝ = Ordering.lt
- ImportGraph.Shake.instOrdDeclDeclNeedsKind.ord x✝ ImportGraph.Shake.DeclDeclNeedsKind.runtimeIR = Ordering.gt
- ImportGraph.Shake.instOrdDeclDeclNeedsKind.ord (ImportGraph.Shake.DeclDeclNeedsKind.comptime a) (ImportGraph.Shake.DeclDeclNeedsKind.comptime b) = (compare a b).then Ordering.eq
Instances For
Equations
An English-language description of a DeclDeclNeedsKind.
Equations
- (ImportGraph.Shake.DeclDeclNeedsKind.expr down (some true) up).pretty = toString "uses " ++ toString up ++ toString " in " ++ toString down ++ toString " (public)"
- (ImportGraph.Shake.DeclDeclNeedsKind.expr down (some false) up).pretty = toString "uses " ++ toString up ++ toString " in " ++ toString down ++ toString " (private)"
- (ImportGraph.Shake.DeclDeclNeedsKind.expr down none up).pretty = toString "uses " ++ toString up ++ toString " in " ++ toString down ++ toString ""
- ImportGraph.Shake.DeclDeclNeedsKind.runtimeIR.pretty = "used in runtime IR"
- (ImportGraph.Shake.DeclDeclNeedsKind.metaIR isReExported through).pretty = "used in meta IR"
- (ImportGraph.Shake.DeclDeclNeedsKind.comptime source).pretty = "comptime dependency"
Instances For
some .comptime if this demands the source declaration's IRPhases includes .comptime;
some .runtime if this demands the source declaration's IRPhases includes .runtime. (.all
includes both.) none if there is no demand either way (as is the case for expressions).
Equations
- (ImportGraph.Shake.DeclDeclNeedsKind.expr downstream isReExported upstream).irPhaseNeeds? = none
- (ImportGraph.Shake.DeclDeclNeedsKind.metaIR isReExported through).irPhaseNeeds? = some Lean.IRPhases.comptime
- (ImportGraph.Shake.DeclDeclNeedsKind.comptime source).irPhaseNeeds? = some Lean.IRPhases.comptime
- ImportGraph.Shake.DeclDeclNeedsKind.runtimeIR.irPhaseNeeds? = some Lean.IRPhases.runtime
Instances For
The intrinsic information that determines subsequent placement under different manners of import.
- isPublic : Bool
Whether the declaration is exposed.
noneif the notion of exposure does not apply (e.g. a theorem).Only public declarations can be exposed.
Whether the declaration is meta, i.e. has exported IR.
noneif there is no IR to be exported. Ifsome false, the declaration has runtime IR.- isExact : Bool
A flag indicating whether this is the exact stance (determined within the same module) or a lower bound.
Instances For
A set of declaration needs.
Equations
Instances For
The demands of a single declaration, organized to facilitate movement. Free declarations are those whose modules may change; fixed ones must remain in the same module.
- freeDecls : Lean.NameMap DeclDeclNeedsKindSet
Declarations whose location may be altered.
- fixedDecls : Std.TreeMap Lean.Name (Lean.NameMap DeclDeclNeedsKindSet) compare
Declarations whose location may not be altered. A map
[module name] ↦ [needs of declarations from that module]. - extraModUses : Lean.NameMap (Std.TreeSet NeedsKind compare)
Extra module uses recorded in shake extensions during the command. Note that if several declarations were created during the command, the extra mod uses are the same among all of them.
The parent declaration, if the ambient declaration is autogenerated.
Any autogenerated declarations used by this declaration.
Instances For
Equations
Equations
Equations
- One or more equations did not get rendered due to their size.
Instances For
Records that the ambient declaration needs the declaration decl at availability k, where
the module of decl is left free.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Records that the ambient declaration needs the imported declaration decl from module i at
availability k, where decl is expected to stay in i.
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
A collection of needs per declaration.
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
Returns false if s.isExposed = none. Because of that, this is not necessarily equivalent
to !(s.isPrivateAt loc).
Equations
Instances For
Returns false if s.isExposed = none. Because of that, this is not necessarily equivalent
to !(s.isPublicAt loc).
Equations
Instances For
Finds the stance of the given declaration, assuming decl is not imported. Returns none if
decl is either not in the environment or is imported.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Like shouldGenerateCode, but for imported constants.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Approximates the stance of the imported declaration decl by a "lower bound". This means that
we assume that any "extra information" is due to provisioning rather than the stance. For example,
an import all will provide the body of every definition regardless of whether it is exposed or
not. We therefore assume it is not exposed and that import all is doing the work. This allows
us to ensure we have the imports we need. In most cases, however, this lower bound is sharp and
matches the actual stance.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The ImportNeedsKind implied by a downstream constant with stance downstreamStance demanding
a declaration with stance upstreamStance via demand : DeclDeclNeedsKind. Assumes that the
demand is satisfiable by the upstream declaration (e.g. that we are not trying to use an
intrinsically private declaration in a public position).
Note that this does not account for indirect module uses.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- x.run s = StateRefT'.run x s
Instances For
Equations
- x.run' s = StateRefT'.run' x s
Instances For
Gets the stance of the given declaration. Records none if no stance could be found. This is
insensitive to the isExporting flag on the ambient environment.
Equations
- One or more equations did not get rendered due to their size.
Instances For
An approximation to whether something is an autogenerated declaration whose base decl is its prefix. This should be improved.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Calculate the needs of the given declaration based on its ConstInfo (type and value).
Calcualte the syntax needs of some syntax, and attaches it as a comptime need for all decls.
This design will likely change in the future, and command needs will have a dedicated location
instead of being duplicated across different declarations.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Calculates the IR needs of a declaration. Currently ignores meta IR.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Elaborates the command and captures the DeclNeeds of all new declarations. Attaches the needs
implied by the command's syntax to each new declaration. Note: does not capture extra rev mod
uses influencing the file as a whole.
Equations
- One or more equations did not get rendered due to their size.