Documentation

ImportGraph.Shake.DeclNeeds

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 #

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.

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

                                  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
                                    @[inline]

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

                                                Information about the source of a compile-time dependency.

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

                                                    A location at which a ConstantInfo can reference another.

                                                    Instances For
                                                      Equations
                                                      • One or more equations did not get rendered due to their size.
                                                      Instances For
                                                        @[instance_reducible]
                                                        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 by downstream on src's location given by upstream. isReExported := none indicates that the exported value is inherited from tgt's isPublic or isExposed stance, as appropriate. (Occasionally this will be some false if, 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 tgt on src due to the IR of tgt, which is meta-available. When isReExported := none, this is inherited from the tgt's isPublic stance (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 tgt on src in the IR of tgt, 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 for tgt).

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

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

                                                              The intrinsic information that determines subsequent placement under different manners of import.

                                                              • isPublic : Bool

                                                                Whether the declaration is public or not. We assume we are only discussing declarations that appear in the environment. (If we want to relax this in the future, this might become Option Bool.)

                                                              • isExposed : Option Bool

                                                                Whether the declaration is exposed. none if the notion of exposure does not apply (e.g. a theorem).

                                                              • isPublic_if_isExposed : (self.isExposed != some true || self.isPublic) = true

                                                                Only public declarations can be exposed.

                                                              • isMeta : Option Bool

                                                                Whether the declaration is meta, i.e. has exported IR. none if there is no IR to be exported. If some 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
                                                                @[macro_inline]

                                                                Whether a Stance is not isPublic.

                                                                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.

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

                                                                      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
                                                                            @[inline]
                                                                            Equations
                                                                            Instances For
                                                                              @[reducible, inline]

                                                                              A collection of needs per declaration.

                                                                              Equations
                                                                              Instances For
                                                                                @[inline]
                                                                                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
                                                                                    @[inline]
                                                                                    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
                                                                                                def ImportGraph.Shake.Stance.toImportNeedsKind (downstreamStance : Stance) (demand : DeclDeclNeedsKind) (upstreamStance : Stance) :

                                                                                                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
                                                                                                  @[reducible, inline]

                                                                                                  CoreM together with a cache for Stances.

                                                                                                  Equations
                                                                                                  Instances For
                                                                                                    @[reducible, inline]
                                                                                                    Equations
                                                                                                    Instances For
                                                                                                      @[reducible, inline]
                                                                                                      Equations
                                                                                                      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
                                                                                                          @[inline]

                                                                                                          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
                                                                                                            partial def ImportGraph.Shake.calcDeclConstInfoNeeds (decl : Lean.Name) (env : Lean.Environment) (currentDecls : DeclNeeds := ∅) (isReExported : Option Bool := none) :

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