2022-05-19 22:36:21 +00:00
|
|
|
|
/-
|
|
|
|
|
Copyright (c) 2022 Henrik Böving. All rights reserved.
|
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
Authors: Henrik Böving
|
|
|
|
|
-/
|
|
|
|
|
import Lean
|
|
|
|
|
|
|
|
|
|
import DocGen4.Process.Base
|
|
|
|
|
import DocGen4.Process.AxiomInfo
|
|
|
|
|
import DocGen4.Process.TheoremInfo
|
|
|
|
|
import DocGen4.Process.OpaqueInfo
|
|
|
|
|
import DocGen4.Process.InstanceInfo
|
|
|
|
|
import DocGen4.Process.DefinitionInfo
|
|
|
|
|
import DocGen4.Process.ClassInfo
|
|
|
|
|
import DocGen4.Process.StructureInfo
|
|
|
|
|
import DocGen4.Process.InductiveInfo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DocGen4.Process
|
|
|
|
|
namespace DocInfo
|
|
|
|
|
|
|
|
|
|
open Lean Meta Widget
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
| classInductiveInfo i => i.declarationRange
|
2023-02-16 18:51:35 +00:00
|
|
|
|
| ctorInfo i => i.declarationRange
|
2022-05-19 22:36:21 +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
|
|
|
|
|
| classInductiveInfo i => i.name
|
2023-02-16 18:51:35 +00:00
|
|
|
|
| ctorInfo i => i.name
|
2022-05-19 22:36:21 +00:00
|
|
|
|
|
|
|
|
|
def getKind : DocInfo → String
|
|
|
|
|
| axiomInfo _ => "axiom"
|
|
|
|
|
| theoremInfo _ => "theorem"
|
2022-06-19 14:41:59 +00:00
|
|
|
|
| opaqueInfo _ => "opaque"
|
2022-05-19 22:36:21 +00:00
|
|
|
|
| definitionInfo _ => "def"
|
|
|
|
|
| instanceInfo _ => "instance"
|
|
|
|
|
| inductiveInfo _ => "inductive"
|
|
|
|
|
| structureInfo _ => "structure"
|
|
|
|
|
| classInfo _ => "class"
|
|
|
|
|
| classInductiveInfo _ => "class"
|
2023-02-16 18:51:35 +00:00
|
|
|
|
| ctorInfo _ => "ctor" -- TODO: kind ctor support in js
|
2022-05-19 22:36:21 +00:00
|
|
|
|
|
|
|
|
|
def getType : DocInfo → CodeWithInfos
|
|
|
|
|
| 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
|
|
|
|
|
| classInductiveInfo i => i.type
|
2023-02-16 18:51:35 +00:00
|
|
|
|
| ctorInfo i => i.type
|
2022-05-19 22:36:21 +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
|
|
|
|
|
| classInductiveInfo i => i.args
|
2023-02-16 18:51:35 +00:00
|
|
|
|
| ctorInfo i => i.args
|
2022-05-19 22:36:21 +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
|
2023-02-16 18:51:35 +00:00
|
|
|
|
| ctorInfo i => i.attrs
|
2022-05-19 22:36:21 +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
|
2023-02-16 18:51:35 +00:00
|
|
|
|
| ctorInfo i => i.doc
|
|
|
|
|
|
|
|
|
|
def shouldRender : DocInfo → Bool
|
|
|
|
|
| axiomInfo i => i.render
|
|
|
|
|
| theoremInfo i => i.render
|
|
|
|
|
| opaqueInfo i => i.render
|
|
|
|
|
| definitionInfo i => i.render
|
|
|
|
|
| instanceInfo i => i.render
|
|
|
|
|
| inductiveInfo i => i.render
|
|
|
|
|
| structureInfo i => i.render
|
|
|
|
|
| classInfo i => i.render
|
|
|
|
|
| classInductiveInfo i => i.render
|
|
|
|
|
| ctorInfo i => i.render
|
2022-05-19 22:36:21 +00:00
|
|
|
|
|
|
|
|
|
def isBlackListed (declName : Name) : MetaM Bool := do
|
2023-01-01 18:30:28 +00:00
|
|
|
|
match ← findDeclarationRanges? declName with
|
2022-05-19 22:36:21 +00:00
|
|
|
|
| some _ =>
|
|
|
|
|
let env ← getEnv
|
|
|
|
|
pure (declName.isInternal)
|
2022-07-23 11:01:25 +00:00
|
|
|
|
<||> (pure <| isAuxRecursor env declName)
|
|
|
|
|
<||> (pure <| isNoConfusion env declName)
|
2022-05-19 22:36:21 +00:00
|
|
|
|
<||> isRec declName
|
|
|
|
|
<||> isMatcher declName
|
|
|
|
|
-- TODO: Evaluate whether filtering out declarations without range is sensible
|
2023-01-01 18:30:28 +00:00
|
|
|
|
| none => return true
|
2022-05-19 22:36:21 +00:00
|
|
|
|
|
|
|
|
|
-- TODO: Is this actually the best way?
|
|
|
|
|
def isProjFn (declName : Name) : MetaM Bool := do
|
|
|
|
|
let env ← getEnv
|
|
|
|
|
match declName with
|
2022-07-20 14:18:57 +00:00
|
|
|
|
| Name.str parent name =>
|
2022-05-19 22:36:21 +00:00
|
|
|
|
if isStructure env parent then
|
|
|
|
|
match getStructureInfo? env parent with
|
|
|
|
|
| some i =>
|
|
|
|
|
match i.fieldNames.find? (· == name) with
|
2023-01-01 18:30:28 +00:00
|
|
|
|
| some _ => return true
|
|
|
|
|
| none => return false
|
2022-05-19 22:36:21 +00:00
|
|
|
|
| none => panic! s!"{parent} is not a structure"
|
|
|
|
|
else
|
2023-01-01 18:30:28 +00:00
|
|
|
|
return false
|
|
|
|
|
| _ => return false
|
2022-05-19 22:36:21 +00:00
|
|
|
|
|
2023-01-01 18:30:28 +00:00
|
|
|
|
def ofConstant : (Name × ConstantInfo) → MetaM (Option DocInfo) := fun (name, info) => do
|
|
|
|
|
if ← isBlackListed name then
|
2022-05-19 22:36:21 +00:00
|
|
|
|
return none
|
|
|
|
|
match info with
|
2023-01-01 18:30:28 +00:00
|
|
|
|
| ConstantInfo.axiomInfo i => return some <| axiomInfo (← AxiomInfo.ofAxiomVal i)
|
|
|
|
|
| ConstantInfo.thmInfo i => return some <| theoremInfo (← TheoremInfo.ofTheoremVal i)
|
|
|
|
|
| ConstantInfo.opaqueInfo i => return some <| opaqueInfo (← OpaqueInfo.ofOpaqueVal i)
|
2022-05-19 22:36:21 +00:00
|
|
|
|
| ConstantInfo.defnInfo i =>
|
2023-01-01 18:30:28 +00:00
|
|
|
|
if ← isProjFn i.name then
|
2023-02-16 18:51:35 +00:00
|
|
|
|
let info ← DefinitionInfo.ofDefinitionVal i
|
|
|
|
|
return some <| definitionInfo { info with render := false }
|
2022-05-19 22:36:21 +00:00
|
|
|
|
else
|
2023-01-01 18:30:28 +00:00
|
|
|
|
if ← isInstance i.name then
|
2022-05-19 22:36:21 +00:00
|
|
|
|
let info ← InstanceInfo.ofDefinitionVal i
|
2023-01-01 18:30:28 +00:00
|
|
|
|
return some <| instanceInfo info
|
2022-05-19 22:36:21 +00:00
|
|
|
|
else
|
|
|
|
|
let info ← DefinitionInfo.ofDefinitionVal i
|
2023-01-01 18:30:28 +00:00
|
|
|
|
return some <| definitionInfo info
|
2022-05-19 22:36:21 +00:00
|
|
|
|
| ConstantInfo.inductInfo i =>
|
|
|
|
|
let env ← getEnv
|
|
|
|
|
if isStructure env i.name then
|
|
|
|
|
if isClass env i.name then
|
2023-01-01 18:30:28 +00:00
|
|
|
|
return some <| classInfo (← ClassInfo.ofInductiveVal i)
|
2022-05-19 22:36:21 +00:00
|
|
|
|
else
|
2023-01-01 18:30:28 +00:00
|
|
|
|
return some <| structureInfo (← StructureInfo.ofInductiveVal i)
|
2022-05-19 22:36:21 +00:00
|
|
|
|
else
|
|
|
|
|
if isClass env i.name then
|
2023-01-01 18:30:28 +00:00
|
|
|
|
return some <| classInductiveInfo (← ClassInductiveInfo.ofInductiveVal i)
|
2022-05-19 22:36:21 +00:00
|
|
|
|
else
|
2023-01-01 18:30:28 +00:00
|
|
|
|
return some <| inductiveInfo (← InductiveInfo.ofInductiveVal i)
|
2023-02-16 18:51:35 +00:00
|
|
|
|
| ConstantInfo.ctorInfo i =>
|
|
|
|
|
let info ← Info.ofConstantVal i.toConstantVal
|
|
|
|
|
return some <| ctorInfo { info with render := false }
|
2022-05-19 22:36:21 +00:00
|
|
|
|
-- we ignore these for now
|
2023-02-16 18:51:35 +00:00
|
|
|
|
| ConstantInfo.recInfo _ | ConstantInfo.quotInfo _ => return none
|
2022-05-19 22:36:21 +00:00
|
|
|
|
|
|
|
|
|
def getKindDescription : DocInfo → String
|
|
|
|
|
| axiomInfo i => if i.isUnsafe then "unsafe axiom" else "axiom"
|
|
|
|
|
| theoremInfo _ => "theorem"
|
|
|
|
|
| opaqueInfo i =>
|
2022-07-23 11:37:17 +00:00
|
|
|
|
match i.definitionSafety with
|
2022-06-19 14:41:59 +00:00
|
|
|
|
| DefinitionSafety.safe => "opaque"
|
|
|
|
|
| DefinitionSafety.unsafe => "unsafe opaque"
|
2022-05-19 22:36:21 +00:00
|
|
|
|
| DefinitionSafety.partial => "partial def"
|
|
|
|
|
| definitionInfo i => Id.run do
|
|
|
|
|
if i.hints.isAbbrev then
|
2023-01-01 18:30:28 +00:00
|
|
|
|
return "abbrev"
|
2022-05-19 22:36:21 +00:00
|
|
|
|
else
|
|
|
|
|
let mut modifiers := #[]
|
|
|
|
|
if i.isUnsafe then
|
|
|
|
|
modifiers := modifiers.push "unsafe"
|
|
|
|
|
if i.isNonComputable then
|
|
|
|
|
modifiers := modifiers.push "noncomputable"
|
|
|
|
|
|
|
|
|
|
modifiers := modifiers.push "def"
|
2023-01-01 18:30:28 +00:00
|
|
|
|
return String.intercalate " " modifiers.toList
|
2022-05-19 22:36:21 +00:00
|
|
|
|
| instanceInfo i => Id.run do
|
|
|
|
|
let mut modifiers := #[]
|
|
|
|
|
if i.isUnsafe then
|
|
|
|
|
modifiers := modifiers.push "unsafe"
|
|
|
|
|
if i.isNonComputable then
|
|
|
|
|
modifiers := modifiers.push "noncomputable"
|
|
|
|
|
|
|
|
|
|
modifiers := modifiers.push "instance"
|
2023-01-01 18:30:28 +00:00
|
|
|
|
return String.intercalate " " modifiers.toList
|
2022-05-19 22:36:21 +00:00
|
|
|
|
| inductiveInfo i => if i.isUnsafe then "unsafe inductive" else "inductive"
|
|
|
|
|
| structureInfo _ => "structure"
|
|
|
|
|
| classInfo _ => "class"
|
|
|
|
|
| classInductiveInfo _ => "class inductive"
|
2023-02-16 18:51:35 +00:00
|
|
|
|
| ctorInfo _ => "constructor"
|
2022-05-19 22:36:21 +00:00
|
|
|
|
|
|
|
|
|
end DocInfo
|
|
|
|
|
|
|
|
|
|
end DocGen4.Process
|