2021-12-10 12:29:04 +00:00
|
|
|
|
/-
|
|
|
|
|
Copyright (c) 2021 Henrik Böving. All rights reserved.
|
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
Authors: Henrik Böving
|
|
|
|
|
-/
|
|
|
|
|
|
2021-11-27 15:19:56 +00:00
|
|
|
|
import Lean
|
|
|
|
|
import Lean.PrettyPrinter
|
2021-11-28 20:31:22 +00:00
|
|
|
|
import Std.Data.HashMap
|
2021-12-03 19:37:25 +00:00
|
|
|
|
import Lean.Meta.SynthInstance
|
|
|
|
|
|
2021-12-12 12:21:53 +00:00
|
|
|
|
import DocGen4.Hierarchy
|
2022-02-13 02:32:53 +00:00
|
|
|
|
import DocGen4.Attributes
|
2021-12-12 12:21:53 +00:00
|
|
|
|
|
2021-11-27 15:19:56 +00:00
|
|
|
|
namespace DocGen4
|
|
|
|
|
|
2021-12-25 13:08:09 +00:00
|
|
|
|
open Lean Meta PrettyPrinter Std Widget
|
2021-12-17 14:59:43 +00:00
|
|
|
|
|
2021-12-05 01:54:38 +00:00
|
|
|
|
structure NameInfo where
|
|
|
|
|
name : Name
|
2021-12-25 13:08:09 +00:00
|
|
|
|
type : CodeWithInfos
|
2022-04-09 15:27:06 +00:00
|
|
|
|
doc : Option String
|
2021-12-25 13:08:09 +00:00
|
|
|
|
deriving Inhabited
|
2021-12-05 01:54:38 +00:00
|
|
|
|
|
2021-12-25 15:55:30 +00:00
|
|
|
|
structure Arg where
|
|
|
|
|
name : Name
|
|
|
|
|
type : CodeWithInfos
|
|
|
|
|
binderInfo : BinderInfo
|
|
|
|
|
|
2021-12-05 01:54:38 +00:00
|
|
|
|
structure Info extends NameInfo where
|
2021-12-25 15:55:30 +00:00
|
|
|
|
args : Array Arg
|
2021-12-05 14:45:19 +00:00
|
|
|
|
declarationRange : DeclarationRange
|
2022-02-13 02:32:53 +00:00
|
|
|
|
attrs : Array String
|
2021-12-25 13:08:09 +00:00
|
|
|
|
deriving Inhabited
|
2021-11-27 15:19:56 +00:00
|
|
|
|
|
|
|
|
|
structure AxiomInfo extends Info where
|
|
|
|
|
isUnsafe : Bool
|
2021-12-25 13:08:09 +00:00
|
|
|
|
deriving Inhabited
|
2021-11-27 15:19:56 +00:00
|
|
|
|
|
2021-12-17 14:59:43 +00:00
|
|
|
|
structure TheoremInfo extends Info
|
2021-12-25 13:08:09 +00:00
|
|
|
|
deriving Inhabited
|
2021-11-27 15:19:56 +00:00
|
|
|
|
|
|
|
|
|
structure OpaqueInfo extends Info where
|
2021-12-25 13:08:09 +00:00
|
|
|
|
value : CodeWithInfos
|
2022-02-06 16:06:22 +00:00
|
|
|
|
-- A value of partial is interpreted as this constant being part of a partial def
|
|
|
|
|
-- since the actual definition for a partial def is hidden behind an inaccessible value
|
|
|
|
|
unsafeInformation : DefinitionSafety
|
2021-12-25 13:08:09 +00:00
|
|
|
|
deriving Inhabited
|
2021-11-27 15:19:56 +00:00
|
|
|
|
|
2021-12-01 17:25:22 +00:00
|
|
|
|
structure DefinitionInfo extends Info where
|
2022-02-06 16:06:22 +00:00
|
|
|
|
-- partial defs are handled by OpaqueInfo
|
|
|
|
|
isUnsafe : Bool
|
2021-12-01 17:25:22 +00:00
|
|
|
|
hints : ReducibilityHints
|
2022-02-02 10:22:15 +00:00
|
|
|
|
equations : Option (Array CodeWithInfos)
|
2022-02-17 17:59:42 +00:00
|
|
|
|
isNonComputable : Bool
|
2021-12-25 13:08:09 +00:00
|
|
|
|
deriving Inhabited
|
2021-12-01 17:25:22 +00:00
|
|
|
|
|
2021-12-03 19:37:25 +00:00
|
|
|
|
abbrev InstanceInfo := DefinitionInfo
|
|
|
|
|
|
2021-12-01 17:25:22 +00:00
|
|
|
|
structure InductiveInfo extends Info where
|
2021-12-05 01:54:38 +00:00
|
|
|
|
ctors : List NameInfo -- List of all constructors and their type for this inductive datatype
|
2021-12-01 17:25:22 +00:00
|
|
|
|
isUnsafe : Bool
|
2021-12-25 13:08:09 +00:00
|
|
|
|
deriving Inhabited
|
2021-12-01 17:25:22 +00:00
|
|
|
|
|
2021-12-02 09:34:20 +00:00
|
|
|
|
structure StructureInfo extends Info where
|
2022-01-04 07:23:39 +00:00
|
|
|
|
fieldInfo : Array NameInfo
|
2021-12-02 09:34:20 +00:00
|
|
|
|
parents : Array Name
|
2021-12-05 01:54:38 +00:00
|
|
|
|
ctor : NameInfo
|
2021-12-25 13:08:09 +00:00
|
|
|
|
deriving Inhabited
|
2021-12-02 09:34:20 +00:00
|
|
|
|
|
2021-12-02 10:17:46 +00:00
|
|
|
|
structure ClassInfo extends StructureInfo where
|
2022-01-06 13:51:46 +00:00
|
|
|
|
instances : Array Name
|
2021-12-25 13:08:09 +00:00
|
|
|
|
deriving Inhabited
|
2021-12-02 10:17:46 +00:00
|
|
|
|
|
2022-02-06 19:14:36 +00:00
|
|
|
|
structure ClassInductiveInfo extends InductiveInfo where
|
|
|
|
|
instances : Array Name
|
|
|
|
|
deriving Inhabited
|
|
|
|
|
|
2021-11-27 15:19:56 +00:00
|
|
|
|
inductive DocInfo where
|
|
|
|
|
| axiomInfo (info : AxiomInfo) : DocInfo
|
|
|
|
|
| theoremInfo (info : TheoremInfo) : DocInfo
|
|
|
|
|
| opaqueInfo (info : OpaqueInfo) : DocInfo
|
2021-12-01 17:25:22 +00:00
|
|
|
|
| definitionInfo (info : DefinitionInfo) : DocInfo
|
2021-12-03 19:37:25 +00:00
|
|
|
|
| instanceInfo (info : InstanceInfo) : DocInfo
|
2021-12-01 17:25:22 +00:00
|
|
|
|
| inductiveInfo (info : InductiveInfo) : DocInfo
|
2021-12-02 09:34:20 +00:00
|
|
|
|
| structureInfo (info : StructureInfo) : DocInfo
|
2021-12-02 10:17:46 +00:00
|
|
|
|
| classInfo (info : ClassInfo) : DocInfo
|
2022-02-06 19:14:36 +00:00
|
|
|
|
| classInductiveInfo (info : ClassInductiveInfo) : DocInfo
|
2021-12-25 13:08:09 +00:00
|
|
|
|
deriving Inhabited
|
2021-11-27 15:19:56 +00:00
|
|
|
|
|
2022-01-07 17:25:26 +00:00
|
|
|
|
namespace DocInfo
|
|
|
|
|
|
|
|
|
|
def getDeclarationRange : DocInfo → DeclarationRange
|
|
|
|
|
| axiomInfo i => i.declarationRange
|
|
|
|
|
| theoremInfo i => i.declarationRange
|
|
|
|
|
| opaqueInfo i => i.declarationRange
|
|
|
|
|
| definitionInfo i => i.declarationRange
|
|
|
|
|
| instanceInfo i => i.declarationRange
|
|
|
|
|
| inductiveInfo i => i.declarationRange
|
|
|
|
|
| structureInfo i => i.declarationRange
|
|
|
|
|
| classInfo i => i.declarationRange
|
2022-02-06 19:14:36 +00:00
|
|
|
|
| classInductiveInfo i => i.declarationRange
|
2022-01-07 17:25:26 +00:00
|
|
|
|
|
|
|
|
|
end DocInfo
|
|
|
|
|
|
2022-02-15 17:06:46 +00:00
|
|
|
|
inductive ModuleMember where
|
|
|
|
|
| docInfo (info : DocInfo) : ModuleMember
|
|
|
|
|
| modDoc (doc : ModuleDoc) : ModuleMember
|
|
|
|
|
deriving Inhabited
|
|
|
|
|
|
2021-11-28 20:31:22 +00:00
|
|
|
|
structure Module where
|
|
|
|
|
name : Name
|
2022-01-07 17:25:26 +00:00
|
|
|
|
-- Sorted according to their line numbers
|
2022-02-15 17:06:46 +00:00
|
|
|
|
members : Array ModuleMember
|
2021-12-01 17:25:22 +00:00
|
|
|
|
deriving Inhabited
|
2021-11-28 20:31:22 +00:00
|
|
|
|
|
2021-12-25 15:55:30 +00:00
|
|
|
|
partial def typeToArgsType (e : Expr) : (Array (Name × Expr × BinderInfo) × Expr) :=
|
2022-01-15 14:12:55 +00:00
|
|
|
|
let helper := λ name type body data =>
|
|
|
|
|
-- Once we hit a name with a macro scope we stop traversing the expression
|
|
|
|
|
-- and print what is left after the : instead. The only exception
|
|
|
|
|
-- to this is instances since these almost never have a name
|
|
|
|
|
-- but should still be printed as arguments instead of after the :.
|
|
|
|
|
if name.hasMacroScopes ∧ ¬data.binderInfo.isInstImplicit then
|
|
|
|
|
(#[], e)
|
|
|
|
|
else
|
|
|
|
|
let name := name.eraseMacroScopes
|
|
|
|
|
let arg := (name, type, data.binderInfo)
|
|
|
|
|
let (args, final) := typeToArgsType (Expr.instantiate1 body (mkFVar ⟨name⟩))
|
|
|
|
|
(#[arg] ++ args, final)
|
|
|
|
|
|
2021-12-25 15:55:30 +00:00
|
|
|
|
match e.consumeMData with
|
2022-01-15 14:12:55 +00:00
|
|
|
|
| Expr.lam name type body data => helper name type body data
|
|
|
|
|
| Expr.forallE name type body data => helper name type body data
|
2021-12-25 15:55:30 +00:00
|
|
|
|
| _ => (#[], e)
|
|
|
|
|
|
2021-12-25 13:08:09 +00:00
|
|
|
|
def prettyPrintTerm (expr : Expr) : MetaM CodeWithInfos := do
|
|
|
|
|
let (fmt, infos) ← formatInfos expr
|
|
|
|
|
let tt := TaggedText.prettyTagged fmt
|
|
|
|
|
let ctx := {
|
|
|
|
|
env := ← getEnv
|
|
|
|
|
mctx := ← getMCtx
|
|
|
|
|
options := ← getOptions
|
|
|
|
|
currNamespace := ← getCurrNamespace
|
|
|
|
|
openDecls := ← getOpenDecls
|
2022-01-16 13:22:53 +00:00
|
|
|
|
fileMap := default
|
2021-12-25 13:08:09 +00:00
|
|
|
|
}
|
2022-02-12 14:09:13 +00:00
|
|
|
|
pure $ tagExprInfos ctx infos tt
|
2021-11-28 20:31:22 +00:00
|
|
|
|
|
2022-04-09 15:27:06 +00:00
|
|
|
|
def NameInfo.ofTypedName (n : Name) (t : Expr) : MetaM NameInfo := do
|
|
|
|
|
let env ← getEnv
|
|
|
|
|
pure { name := n, type := ←prettyPrintTerm t, doc := ←findDocString? env n}
|
|
|
|
|
|
2021-11-28 20:31:22 +00:00
|
|
|
|
def Info.ofConstantVal (v : ConstantVal) : MetaM Info := do
|
|
|
|
|
let env ← getEnv
|
2021-12-25 15:55:30 +00:00
|
|
|
|
let (args, type) := typeToArgsType v.type
|
2022-02-09 21:45:28 +00:00
|
|
|
|
let args ← args.mapM (λ (n, e, b) => do pure $ Arg.mk n (←prettyPrintTerm e) b)
|
2021-12-11 13:26:32 +00:00
|
|
|
|
let doc ← findDocString? env v.name
|
2022-04-09 15:27:06 +00:00
|
|
|
|
let nameInfo ← NameInfo.ofTypedName v.name type
|
2021-12-05 14:45:19 +00:00
|
|
|
|
match ←findDeclarationRanges? v.name with
|
|
|
|
|
-- TODO: Maybe selection range is more relevant? Figure this out in the future
|
2022-04-09 15:27:06 +00:00
|
|
|
|
| some range => pure $ Info.mk nameInfo args range.range (←getAllAttributes v.name)
|
2021-12-05 14:45:19 +00:00
|
|
|
|
| none => panic! s!"{v.name} is a declaration without position"
|
2021-11-28 20:31:22 +00:00
|
|
|
|
|
|
|
|
|
def AxiomInfo.ofAxiomVal (v : AxiomVal) : MetaM AxiomInfo := do
|
|
|
|
|
let info ← Info.ofConstantVal v.toConstantVal
|
2022-02-12 14:09:13 +00:00
|
|
|
|
pure $ AxiomInfo.mk info v.isUnsafe
|
2021-11-28 20:31:22 +00:00
|
|
|
|
|
|
|
|
|
def TheoremInfo.ofTheoremVal (v : TheoremVal) : MetaM TheoremInfo := do
|
|
|
|
|
let info ← Info.ofConstantVal v.toConstantVal
|
2022-02-12 14:09:13 +00:00
|
|
|
|
pure $ TheoremInfo.mk info
|
2021-11-28 20:31:22 +00:00
|
|
|
|
|
|
|
|
|
def OpaqueInfo.ofOpaqueVal (v : OpaqueVal) : MetaM OpaqueInfo := do
|
|
|
|
|
let info ← Info.ofConstantVal v.toConstantVal
|
2021-12-17 14:59:43 +00:00
|
|
|
|
let t ← prettyPrintTerm v.value
|
2022-02-06 16:06:22 +00:00
|
|
|
|
let env ← getEnv
|
|
|
|
|
let isPartial := env.find? (Compiler.mkUnsafeRecName v.name) |>.isSome
|
|
|
|
|
if isPartial then
|
2022-02-12 14:09:13 +00:00
|
|
|
|
pure $ OpaqueInfo.mk info t DefinitionSafety.partial
|
2022-02-06 16:06:22 +00:00
|
|
|
|
else
|
|
|
|
|
let safety := if v.isUnsafe then DefinitionSafety.unsafe else DefinitionSafety.safe
|
2022-02-12 14:09:13 +00:00
|
|
|
|
pure $ OpaqueInfo.mk info t safety
|
2021-11-28 20:31:22 +00:00
|
|
|
|
|
2021-12-03 19:37:25 +00:00
|
|
|
|
def isInstance (declName : Name) : MetaM Bool := do
|
2022-02-12 14:09:13 +00:00
|
|
|
|
pure $ (instanceExtension.getState (←getEnv)).instanceNames.contains declName
|
2021-12-03 19:37:25 +00:00
|
|
|
|
|
2022-02-02 10:22:15 +00:00
|
|
|
|
partial def stripArgs (e : Expr) : Expr :=
|
|
|
|
|
match e.consumeMData with
|
|
|
|
|
| Expr.lam name type body data =>
|
|
|
|
|
let name := name.eraseMacroScopes
|
|
|
|
|
stripArgs (Expr.instantiate1 body (mkFVar ⟨name⟩))
|
|
|
|
|
| Expr.forallE name type body data =>
|
|
|
|
|
let name := name.eraseMacroScopes
|
|
|
|
|
stripArgs (Expr.instantiate1 body (mkFVar ⟨name⟩))
|
|
|
|
|
| _ => e
|
|
|
|
|
|
|
|
|
|
def processEq (eq : Name) : MetaM CodeWithInfos := do
|
|
|
|
|
let type ← (mkConstWithFreshMVarLevels eq >>= inferType)
|
|
|
|
|
let final := stripArgs type
|
|
|
|
|
prettyPrintTerm final
|
|
|
|
|
|
2022-02-05 01:26:03 +00:00
|
|
|
|
def valueToEq (v : DefinitionVal) : MetaM Expr := withLCtx {} {} do
|
|
|
|
|
let env ← getEnv
|
|
|
|
|
withOptions (tactic.hygienic.set . false) do
|
|
|
|
|
lambdaTelescope v.value fun xs body => do
|
|
|
|
|
let us := v.levelParams.map mkLevelParam
|
|
|
|
|
let type ← mkEq (mkAppN (Lean.mkConst v.name us) xs) body
|
|
|
|
|
let type ← mkForallFVars xs type
|
2022-02-09 21:45:28 +00:00
|
|
|
|
pure type
|
2022-02-05 01:26:03 +00:00
|
|
|
|
|
2021-12-01 17:25:22 +00:00
|
|
|
|
def DefinitionInfo.ofDefinitionVal (v : DefinitionVal) : MetaM DefinitionInfo := do
|
|
|
|
|
let info ← Info.ofConstantVal v.toConstantVal
|
2022-02-06 16:06:22 +00:00
|
|
|
|
let isUnsafe := v.safety == DefinitionSafety.unsafe
|
2022-02-17 17:59:42 +00:00
|
|
|
|
let isNonComput := isNoncomputable (←getEnv) v.name
|
2022-02-02 10:22:15 +00:00
|
|
|
|
try
|
2022-02-05 01:26:03 +00:00
|
|
|
|
let eqs? ← getEqnsFor? v.name
|
|
|
|
|
match eqs? with
|
|
|
|
|
| some eqs =>
|
|
|
|
|
let prettyEqs ← eqs.mapM processEq
|
2022-02-17 17:59:42 +00:00
|
|
|
|
pure $ DefinitionInfo.mk info isUnsafe v.hints prettyEqs isNonComput
|
2022-02-05 01:26:03 +00:00
|
|
|
|
| none =>
|
|
|
|
|
let eq ← prettyPrintTerm $ stripArgs (←valueToEq v)
|
2022-02-17 17:59:42 +00:00
|
|
|
|
pure $ DefinitionInfo.mk info isUnsafe v.hints (some #[eq]) isNonComput
|
2022-02-02 10:22:15 +00:00
|
|
|
|
catch err =>
|
|
|
|
|
IO.println s!"WARNING: Failed to calculate equational lemmata for {v.name}: {←err.toMessageData.toString}"
|
2022-02-17 17:59:42 +00:00
|
|
|
|
pure $ DefinitionInfo.mk info isUnsafe v.hints none isNonComput
|
2021-12-01 17:25:22 +00:00
|
|
|
|
|
2022-02-13 02:32:53 +00:00
|
|
|
|
def InstanceInfo.ofDefinitionVal (v : DefinitionVal) : MetaM InstanceInfo := do
|
|
|
|
|
let info ← DefinitionInfo.ofDefinitionVal v
|
|
|
|
|
let some className := getClassName (←getEnv) v.type | unreachable!
|
|
|
|
|
if let some instAttr ← getDefaultInstance v.name className then
|
|
|
|
|
pure { info with attrs := info.attrs.push instAttr }
|
|
|
|
|
else
|
|
|
|
|
pure info
|
|
|
|
|
|
2022-04-09 15:27:06 +00:00
|
|
|
|
def getConstructorType (ctor : Name) : MetaM Expr := do
|
2021-12-01 17:25:22 +00:00
|
|
|
|
let env ← getEnv
|
|
|
|
|
match env.find? ctor with
|
2022-04-09 15:27:06 +00:00
|
|
|
|
| some (ConstantInfo.ctorInfo i) => pure i.type
|
2021-12-01 17:25:22 +00:00
|
|
|
|
| _ => panic! s!"Constructor {ctor} was requested but does not exist"
|
|
|
|
|
|
|
|
|
|
def InductiveInfo.ofInductiveVal (v : InductiveVal) : MetaM InductiveInfo := do
|
|
|
|
|
let info ← Info.ofConstantVal v.toConstantVal
|
|
|
|
|
let env ← getEnv
|
2022-04-09 15:27:06 +00:00
|
|
|
|
let ctors ← v.ctors.mapM (λ name => do NameInfo.ofTypedName name (←getConstructorType name))
|
2022-02-12 14:09:13 +00:00
|
|
|
|
pure $ InductiveInfo.mk info ctors v.isUnsafe
|
2021-12-01 17:25:22 +00:00
|
|
|
|
|
2022-01-04 07:23:39 +00:00
|
|
|
|
def dropArgs (type : Expr) (n : Nat) : (Expr × List (Name × Expr)) :=
|
|
|
|
|
match type, n with
|
|
|
|
|
| e, 0 => (e, [])
|
|
|
|
|
| Expr.forallE name type body _, x + 1 =>
|
|
|
|
|
let body := body.instantiate1 $ mkFVar ⟨name⟩
|
|
|
|
|
let next := dropArgs body x
|
|
|
|
|
{ next with snd := (name, type) :: next.snd}
|
|
|
|
|
| e, x + 1 => panic! s!"No forallE left"
|
|
|
|
|
|
2022-01-09 13:07:50 +00:00
|
|
|
|
def getFieldTypes (struct : Name) (ctor : ConstructorVal) (parents : Nat) : MetaM (Array NameInfo) := do
|
2022-01-04 07:23:39 +00:00
|
|
|
|
let type := ctor.type
|
2022-04-09 15:27:06 +00:00
|
|
|
|
let (fieldFunction, params) := dropArgs type (ctor.numParams + parents)
|
|
|
|
|
let (_, fields) := dropArgs fieldFunction (ctor.numFields - parents)
|
|
|
|
|
let mut fieldInfos := #[]
|
2022-01-04 07:23:39 +00:00
|
|
|
|
for (name, type) in fields do
|
2022-04-09 15:27:06 +00:00
|
|
|
|
fieldInfos := fieldInfos.push $ ←NameInfo.ofTypedName (struct.append name) type
|
|
|
|
|
pure $ fieldInfos
|
2021-12-05 01:54:38 +00:00
|
|
|
|
|
2021-12-02 09:34:20 +00:00
|
|
|
|
def StructureInfo.ofInductiveVal (v : InductiveVal) : MetaM StructureInfo := do
|
|
|
|
|
let info ← Info.ofConstantVal v.toConstantVal
|
|
|
|
|
let env ← getEnv
|
|
|
|
|
let parents := getParentStructures env v.name
|
2022-01-04 07:23:39 +00:00
|
|
|
|
let ctor := getStructureCtor env v.name
|
2021-12-02 09:34:20 +00:00
|
|
|
|
match getStructureInfo? env v.name with
|
2021-12-04 21:33:00 +00:00
|
|
|
|
| some i =>
|
2022-01-04 07:23:39 +00:00
|
|
|
|
if i.fieldNames.size - parents.size > 0 then
|
2022-04-09 15:27:06 +00:00
|
|
|
|
pure $ StructureInfo.mk info (←getFieldTypes v.name ctor parents.size) parents (←NameInfo.ofTypedName ctor.name ctor.type)
|
2022-01-04 07:23:39 +00:00
|
|
|
|
else
|
2022-04-09 15:27:06 +00:00
|
|
|
|
pure $ StructureInfo.mk info #[] parents (←NameInfo.ofTypedName ctor.name ctor.type)
|
2021-12-02 09:34:20 +00:00
|
|
|
|
| none => panic! s!"{v.name} is not a structure"
|
|
|
|
|
|
2022-02-06 19:14:36 +00:00
|
|
|
|
def getInstances (className : Name) : MetaM (Array Name) := do
|
|
|
|
|
let fn ← mkConstWithFreshMVarLevels className
|
2021-12-03 19:37:25 +00:00
|
|
|
|
let (xs, _, _) ← forallMetaTelescopeReducing (← inferType fn)
|
|
|
|
|
let insts ← SynthInstance.getInstances (mkAppN fn xs)
|
2022-02-12 14:09:13 +00:00
|
|
|
|
pure $ insts.map Expr.constName!
|
2022-02-06 19:14:36 +00:00
|
|
|
|
|
|
|
|
|
def ClassInfo.ofInductiveVal (v : InductiveVal) : MetaM ClassInfo := do
|
|
|
|
|
let sinfo ← StructureInfo.ofInductiveVal v
|
2022-02-12 14:09:13 +00:00
|
|
|
|
pure $ ClassInfo.mk sinfo (←getInstances v.name)
|
2022-02-06 19:14:36 +00:00
|
|
|
|
|
|
|
|
|
def ClassInductiveInfo.ofInductiveVal (v : InductiveVal) : MetaM ClassInductiveInfo := do
|
|
|
|
|
let info ← InductiveInfo.ofInductiveVal v
|
2022-02-12 14:09:13 +00:00
|
|
|
|
pure $ ClassInductiveInfo.mk info (←getInstances v.name)
|
2021-12-02 10:17:46 +00:00
|
|
|
|
|
2021-11-27 15:19:56 +00:00
|
|
|
|
namespace DocInfo
|
|
|
|
|
|
2021-12-02 09:34:20 +00:00
|
|
|
|
def isBlackListed (declName : Name) : MetaM Bool := do
|
2021-12-05 14:45:19 +00:00
|
|
|
|
match ←findDeclarationRanges? declName with
|
|
|
|
|
| some _ =>
|
|
|
|
|
let env ← getEnv
|
2022-02-09 21:45:28 +00:00
|
|
|
|
pure (declName.isInternal)
|
|
|
|
|
<||> (pure $ isAuxRecursor env declName)
|
|
|
|
|
<||> (pure $ isNoConfusion env declName)
|
2021-12-05 14:45:19 +00:00
|
|
|
|
<||> isRec declName
|
|
|
|
|
<||> isMatcher declName
|
|
|
|
|
-- TODO: Evaluate whether filtering out declarations without range is sensible
|
2022-02-09 21:45:28 +00:00
|
|
|
|
| none => pure true
|
2021-11-27 15:19:56 +00:00
|
|
|
|
|
2021-12-02 10:09:54 +00:00
|
|
|
|
-- TODO: Is this actually the best way?
|
|
|
|
|
def isProjFn (declName : Name) : MetaM Bool := do
|
|
|
|
|
let env ← getEnv
|
2022-02-12 14:09:13 +00:00
|
|
|
|
match declName with
|
2021-12-02 10:09:54 +00:00
|
|
|
|
| Name.str parent name _ =>
|
|
|
|
|
if isStructure env parent then
|
|
|
|
|
match getStructureInfo? env parent with
|
|
|
|
|
| some i =>
|
|
|
|
|
match i.fieldNames.find? (· == name) with
|
2022-02-12 14:09:13 +00:00
|
|
|
|
| some _ => pure true
|
|
|
|
|
| none => pure false
|
2021-12-02 10:09:54 +00:00
|
|
|
|
| none => panic! s!"{parent} is not a structure"
|
|
|
|
|
else
|
2022-02-12 14:09:13 +00:00
|
|
|
|
pure false
|
|
|
|
|
| _ => pure false
|
2021-12-01 17:25:22 +00:00
|
|
|
|
|
2021-11-27 15:19:56 +00:00
|
|
|
|
def ofConstant : (Name × ConstantInfo) → MetaM (Option DocInfo) := λ (name, info) => do
|
|
|
|
|
if (←isBlackListed name) then
|
|
|
|
|
return none
|
|
|
|
|
match info with
|
2022-02-09 21:45:28 +00:00
|
|
|
|
| ConstantInfo.axiomInfo i => pure <| some <| axiomInfo (←AxiomInfo.ofAxiomVal i)
|
|
|
|
|
| ConstantInfo.thmInfo i => pure <| some <| theoremInfo (←TheoremInfo.ofTheoremVal i)
|
|
|
|
|
| ConstantInfo.opaqueInfo i => pure <| some <| opaqueInfo (←OpaqueInfo.ofOpaqueVal i)
|
2021-12-02 10:09:54 +00:00
|
|
|
|
| ConstantInfo.defnInfo i =>
|
2021-12-03 19:37:25 +00:00
|
|
|
|
if ← (isProjFn i.name) then
|
2022-02-09 21:45:28 +00:00
|
|
|
|
pure none
|
2021-12-02 10:09:54 +00:00
|
|
|
|
else
|
2021-12-03 19:37:25 +00:00
|
|
|
|
if (←isInstance i.name) then
|
2022-02-13 02:32:53 +00:00
|
|
|
|
let info ← InstanceInfo.ofDefinitionVal i
|
2022-02-09 21:45:28 +00:00
|
|
|
|
pure <| some <| instanceInfo info
|
2021-12-03 19:37:25 +00:00
|
|
|
|
else
|
2022-02-13 02:32:53 +00:00
|
|
|
|
let info ← DefinitionInfo.ofDefinitionVal i
|
|
|
|
|
pure <| some <| definitionInfo info
|
2021-12-02 09:34:20 +00:00
|
|
|
|
| ConstantInfo.inductInfo i =>
|
2021-12-02 10:17:46 +00:00
|
|
|
|
let env ← getEnv
|
|
|
|
|
if isStructure env i.name then
|
|
|
|
|
if isClass env i.name then
|
2022-02-09 21:45:28 +00:00
|
|
|
|
pure <| some <| classInfo (←ClassInfo.ofInductiveVal i)
|
2021-12-02 10:17:46 +00:00
|
|
|
|
else
|
2022-02-09 21:45:28 +00:00
|
|
|
|
pure <| some <| structureInfo (←StructureInfo.ofInductiveVal i)
|
2021-12-02 09:34:20 +00:00
|
|
|
|
else
|
2022-02-06 19:14:36 +00:00
|
|
|
|
if isClass env i.name then
|
2022-02-09 21:45:28 +00:00
|
|
|
|
pure <| some <| classInductiveInfo (←ClassInductiveInfo.ofInductiveVal i)
|
2022-02-06 19:14:36 +00:00
|
|
|
|
else
|
2022-02-09 21:45:28 +00:00
|
|
|
|
pure <| some <| inductiveInfo (←InductiveInfo.ofInductiveVal i)
|
2021-11-27 15:19:56 +00:00
|
|
|
|
-- we ignore these for now
|
2022-02-09 21:45:28 +00:00
|
|
|
|
| ConstantInfo.ctorInfo i => pure none
|
|
|
|
|
| ConstantInfo.recInfo i => pure none
|
|
|
|
|
| ConstantInfo.quotInfo i => pure none
|
2021-11-27 15:19:56 +00:00
|
|
|
|
|
2021-12-15 10:59:36 +00:00
|
|
|
|
def getName : DocInfo → Name
|
|
|
|
|
| axiomInfo i => i.name
|
|
|
|
|
| theoremInfo i => i.name
|
|
|
|
|
| opaqueInfo i => i.name
|
|
|
|
|
| definitionInfo i => i.name
|
|
|
|
|
| instanceInfo i => i.name
|
|
|
|
|
| inductiveInfo i => i.name
|
|
|
|
|
| structureInfo i => i.name
|
|
|
|
|
| classInfo i => i.name
|
2022-02-06 19:14:36 +00:00
|
|
|
|
| classInductiveInfo i => i.name
|
2021-12-15 10:59:36 +00:00
|
|
|
|
|
|
|
|
|
def getKind : DocInfo → String
|
|
|
|
|
| axiomInfo _ => "axiom"
|
|
|
|
|
| theoremInfo _ => "theorem"
|
|
|
|
|
| opaqueInfo _ => "constant"
|
|
|
|
|
| definitionInfo _ => "def"
|
2022-02-06 16:06:22 +00:00
|
|
|
|
| instanceInfo _ => "instance"
|
2021-12-15 10:59:36 +00:00
|
|
|
|
| inductiveInfo _ => "inductive"
|
|
|
|
|
| structureInfo _ => "structure"
|
2022-02-06 16:06:22 +00:00
|
|
|
|
| classInfo _ => "class"
|
2022-02-06 19:14:36 +00:00
|
|
|
|
| classInductiveInfo _ => "class"
|
2022-02-06 16:06:22 +00:00
|
|
|
|
|
|
|
|
|
def getKindDescription : DocInfo → String
|
|
|
|
|
| axiomInfo i => if i.isUnsafe then "unsafe axiom" else "axiom"
|
|
|
|
|
| theoremInfo _ => "theorem"
|
|
|
|
|
| opaqueInfo i =>
|
|
|
|
|
match i.unsafeInformation with
|
2022-02-06 16:28:56 +00:00
|
|
|
|
| DefinitionSafety.safe => "constant"
|
|
|
|
|
| DefinitionSafety.unsafe => "unsafe constant"
|
|
|
|
|
| DefinitionSafety.partial => "partial def"
|
2022-02-12 14:57:45 +00:00
|
|
|
|
| definitionInfo i => Id.run do
|
2022-02-06 16:28:56 +00:00
|
|
|
|
if i.hints.isAbbrev then
|
2022-02-12 14:57:45 +00:00
|
|
|
|
pure "abbrev"
|
2022-02-06 16:28:56 +00:00
|
|
|
|
else
|
2022-02-12 14:57:45 +00:00
|
|
|
|
let mut modifiers := #[]
|
|
|
|
|
if i.isUnsafe then
|
|
|
|
|
modifiers := modifiers.push "unsafe"
|
2022-02-17 17:59:42 +00:00
|
|
|
|
if i.isNonComputable then
|
2022-02-12 14:57:45 +00:00
|
|
|
|
modifiers := modifiers.push "noncomputable"
|
|
|
|
|
|
|
|
|
|
modifiers := modifiers.push "def"
|
|
|
|
|
pure $ String.intercalate " " modifiers.toList
|
|
|
|
|
| instanceInfo i => Id.run do
|
|
|
|
|
let mut modifiers := #[]
|
|
|
|
|
if i.isUnsafe then
|
|
|
|
|
modifiers := modifiers.push "unsafe"
|
2022-02-17 17:59:42 +00:00
|
|
|
|
if i.isNonComputable then
|
2022-02-12 14:57:45 +00:00
|
|
|
|
modifiers := modifiers.push "noncomputable"
|
|
|
|
|
|
|
|
|
|
modifiers := modifiers.push "instance"
|
|
|
|
|
pure $ String.intercalate " " modifiers.toList
|
2022-02-06 16:06:22 +00:00
|
|
|
|
| inductiveInfo i => if i.isUnsafe then "unsafe inductive" else "inductive"
|
|
|
|
|
| structureInfo _ => "structure"
|
|
|
|
|
| classInfo _ => "class"
|
2022-02-06 19:14:36 +00:00
|
|
|
|
| classInductiveInfo _ => "class inductive"
|
2021-12-15 10:59:36 +00:00
|
|
|
|
|
2021-12-25 13:08:09 +00:00
|
|
|
|
def getType : DocInfo → CodeWithInfos
|
2021-12-17 14:59:43 +00:00
|
|
|
|
| axiomInfo i => i.type
|
|
|
|
|
| theoremInfo i => i.type
|
|
|
|
|
| opaqueInfo i => i.type
|
|
|
|
|
| definitionInfo i => i.type
|
|
|
|
|
| instanceInfo i => i.type
|
|
|
|
|
| inductiveInfo i => i.type
|
|
|
|
|
| structureInfo i => i.type
|
|
|
|
|
| classInfo i => i.type
|
2022-02-06 19:14:36 +00:00
|
|
|
|
| classInductiveInfo i => i.type
|
2021-12-17 14:59:43 +00:00
|
|
|
|
|
2021-12-25 15:55:30 +00:00
|
|
|
|
def getArgs : DocInfo → Array Arg
|
|
|
|
|
| axiomInfo i => i.args
|
|
|
|
|
| theoremInfo i => i.args
|
|
|
|
|
| opaqueInfo i => i.args
|
|
|
|
|
| definitionInfo i => i.args
|
|
|
|
|
| instanceInfo i => i.args
|
|
|
|
|
| inductiveInfo i => i.args
|
|
|
|
|
| structureInfo i => i.args
|
|
|
|
|
| classInfo i => i.args
|
2022-02-06 19:14:36 +00:00
|
|
|
|
| classInductiveInfo i => i.args
|
2021-12-25 15:55:30 +00:00
|
|
|
|
|
2022-02-13 02:32:53 +00:00
|
|
|
|
def getAttrs : DocInfo → Array String
|
|
|
|
|
| axiomInfo i => i.attrs
|
|
|
|
|
| theoremInfo i => i.attrs
|
|
|
|
|
| opaqueInfo i => i.attrs
|
|
|
|
|
| definitionInfo i => i.attrs
|
|
|
|
|
| instanceInfo i => i.attrs
|
|
|
|
|
| inductiveInfo i => i.attrs
|
|
|
|
|
| structureInfo i => i.attrs
|
|
|
|
|
| classInfo i => i.attrs
|
|
|
|
|
| classInductiveInfo i => i.attrs
|
|
|
|
|
|
2022-02-13 14:03:49 +00:00
|
|
|
|
def getDocString : DocInfo → Option String
|
|
|
|
|
| axiomInfo i => i.doc
|
|
|
|
|
| theoremInfo i => i.doc
|
|
|
|
|
| opaqueInfo i => i.doc
|
|
|
|
|
| definitionInfo i => i.doc
|
|
|
|
|
| instanceInfo i => i.doc
|
|
|
|
|
| inductiveInfo i => i.doc
|
|
|
|
|
| structureInfo i => i.doc
|
|
|
|
|
| classInfo i => i.doc
|
|
|
|
|
| classInductiveInfo i => i.doc
|
|
|
|
|
|
2021-11-27 15:19:56 +00:00
|
|
|
|
end DocInfo
|
|
|
|
|
|
2022-02-15 17:06:46 +00:00
|
|
|
|
namespace ModuleMember
|
|
|
|
|
|
|
|
|
|
def getDeclarationRange : ModuleMember → DeclarationRange
|
|
|
|
|
| docInfo i => i.getDeclarationRange
|
|
|
|
|
| modDoc i => i.declarationRange
|
|
|
|
|
|
|
|
|
|
def order (l r : ModuleMember) : Bool :=
|
|
|
|
|
Position.lt l.getDeclarationRange.pos r.getDeclarationRange.pos
|
|
|
|
|
|
|
|
|
|
def getName : ModuleMember → Name
|
|
|
|
|
| docInfo i => i.getName
|
|
|
|
|
| modDoc i => Name.anonymous
|
|
|
|
|
|
|
|
|
|
def getDocString : ModuleMember → Option String
|
|
|
|
|
| docInfo i => i.getDocString
|
|
|
|
|
| modDoc i => i.doc
|
|
|
|
|
|
|
|
|
|
end ModuleMember
|
|
|
|
|
|
2022-02-18 04:52:01 +00:00
|
|
|
|
def filterMapDocInfo (ms : Array ModuleMember) : Array DocInfo :=
|
|
|
|
|
ms.filterMap filter
|
|
|
|
|
where
|
|
|
|
|
filter : ModuleMember → Option DocInfo
|
|
|
|
|
| ModuleMember.docInfo i => some i
|
|
|
|
|
| _ => none
|
|
|
|
|
|
2021-12-12 12:21:53 +00:00
|
|
|
|
structure AnalyzerResult where
|
2021-12-17 16:20:44 +00:00
|
|
|
|
name2ModIdx : HashMap Name ModuleIdx
|
|
|
|
|
moduleNames : Array Name
|
|
|
|
|
moduleInfo : HashMap Name Module
|
2021-12-12 12:21:53 +00:00
|
|
|
|
hierarchy : Hierarchy
|
2022-01-16 13:22:53 +00:00
|
|
|
|
-- Indexed by ModIdx
|
|
|
|
|
importAdj : Array (Array Bool)
|
2021-12-12 12:21:53 +00:00
|
|
|
|
deriving Inhabited
|
|
|
|
|
|
2022-02-17 17:59:42 +00:00
|
|
|
|
deriving instance Inhabited for ModuleDoc
|
|
|
|
|
|
2021-12-12 12:21:53 +00:00
|
|
|
|
def process : MetaM AnalyzerResult := do
|
2021-11-28 20:31:22 +00:00
|
|
|
|
let env ← getEnv
|
|
|
|
|
let mut res := mkHashMap env.header.moduleNames.size
|
|
|
|
|
for module in env.header.moduleNames do
|
2022-02-15 17:06:46 +00:00
|
|
|
|
let modDocs := match getModuleDoc? env module with
|
|
|
|
|
| none => #[]
|
|
|
|
|
| some ds => ds
|
|
|
|
|
|>.map (λ doc => ModuleMember.modDoc doc)
|
|
|
|
|
res := res.insert module (Module.mk module modDocs)
|
2021-11-28 20:31:22 +00:00
|
|
|
|
|
2021-11-27 15:19:56 +00:00
|
|
|
|
for cinfo in env.constants.toList do
|
2022-02-02 10:22:15 +00:00
|
|
|
|
try
|
|
|
|
|
let analysis := Prod.fst <$> Meta.MetaM.toIO (DocInfo.ofConstant cinfo) { maxHeartbeats := 5000000, options := ⟨[(`pp.tagAppFns, true)]⟩ } { env := env} {} {}
|
|
|
|
|
if let some dinfo ← analysis then
|
2022-02-09 21:45:28 +00:00
|
|
|
|
let some modidx := env.getModuleIdxFor? dinfo.getName | unreachable!
|
2022-02-02 10:22:15 +00:00
|
|
|
|
let moduleName := env.allImportedModuleNames.get! modidx
|
|
|
|
|
let module := res.find! moduleName
|
2022-02-15 17:06:46 +00:00
|
|
|
|
res := res.insert moduleName {module with members := module.members.push (ModuleMember.docInfo dinfo)}
|
2022-02-02 10:22:15 +00:00
|
|
|
|
catch e =>
|
|
|
|
|
IO.println s!"WARNING: Failed to obtain information for: {cinfo.fst}: {←e.toMessageData.toString}"
|
2022-01-07 17:25:26 +00:00
|
|
|
|
|
2022-01-16 13:22:53 +00:00
|
|
|
|
-- TODO: This is definitely not the most efficient way to store this data
|
|
|
|
|
let mut adj := Array.mkArray res.size (Array.mkArray res.size false)
|
|
|
|
|
-- TODO: This could probably be faster if we did an insertion sort above instead
|
2022-01-07 17:25:26 +00:00
|
|
|
|
for (moduleName, module) in res.toArray do
|
2022-02-15 17:06:46 +00:00
|
|
|
|
res := res.insert moduleName {module with members := module.members.qsort ModuleMember.order}
|
2022-02-09 21:45:28 +00:00
|
|
|
|
let some modIdx := env.getModuleIdx? moduleName | unreachable!
|
2022-01-16 13:22:53 +00:00
|
|
|
|
let moduleData := env.header.moduleData.get! modIdx
|
|
|
|
|
for imp in moduleData.imports do
|
2022-02-09 21:45:28 +00:00
|
|
|
|
let some importIdx := env.getModuleIdx? imp.module | unreachable!
|
2022-01-16 13:22:53 +00:00
|
|
|
|
adj := adj.set! modIdx (adj.get! modIdx |>.set! importIdx true)
|
2022-01-07 17:25:26 +00:00
|
|
|
|
|
2022-02-12 14:09:13 +00:00
|
|
|
|
pure {
|
2021-12-17 16:20:44 +00:00
|
|
|
|
name2ModIdx := env.const2ModIdx,
|
|
|
|
|
moduleNames := env.header.moduleNames,
|
|
|
|
|
moduleInfo := res,
|
2022-01-16 13:22:53 +00:00
|
|
|
|
hierarchy := Hierarchy.fromArray env.header.moduleNames,
|
|
|
|
|
importAdj := adj
|
2021-12-17 16:20:44 +00:00
|
|
|
|
}
|
2021-11-27 15:19:56 +00:00
|
|
|
|
|
|
|
|
|
end DocGen4
|