refactor: Less manual manipulation of expressions

main
Henrik 2023-09-10 10:13:30 +02:00
parent 6abc8bb769
commit 7dc8018130
1 changed files with 18 additions and 18 deletions

View File

@ -12,24 +12,24 @@ namespace DocGen4.Process
open Lean Meta open Lean Meta
-- TODO: replace with Leos variant from Zulip /--
def dropArgs (type : Expr) (n : Nat) : (Expr × List (Name × Expr)) := Execute `k` with an array containing pairs `(fieldName, fieldType)`.
match type, n with `k` is executed in an updated local context which contains local declarations for the `structName` parameters.
| e, 0 => (e, []) -/
| Expr.forallE name type body _, x + 1 => def withFields (info : InductiveVal) (k : Array (Name × Expr) → MetaM α) (includeSubobjectFields : Bool := false) : MetaM α := do
let body := body.instantiate1 <| mkFVar ⟨name⟩ let structName := info.name
let next := dropArgs body x let us := info.levelParams.map mkLevelParam
{ next with snd := (name, type) :: next.snd} forallTelescopeReducing info.type fun params _ =>
| _, _ + 1 => panic! s!"No forallE left" withLocalDeclD `s (mkAppN (mkConst structName us) params) fun s => do
let mut info := #[]
for fieldName in getStructureFieldsFlattened (← getEnv) structName includeSubobjectFields do
let proj ← mkProjection s fieldName
info := info.push (fieldName, (← inferType proj))
k info
def getFieldTypes (struct : Name) (ctor : ConstructorVal) (parents : Nat) : MetaM (Array NameInfo) := do def getFieldTypes (v : InductiveVal) : MetaM (Array NameInfo) := do
let type := ctor.type withFields v fun fields =>
let (fieldFunction, _) := dropArgs type (ctor.numParams + parents) fields.foldlM (init := #[]) (fun acc (name, type) => do return acc.push (← NameInfo.ofTypedName (v.name.append name) type))
let (_, fields) := dropArgs fieldFunction (ctor.numFields - parents)
let mut fieldInfos := #[]
for (name, type) in fields do
fieldInfos := fieldInfos.push <| ← NameInfo.ofTypedName (struct.append name) type
return fieldInfos
def StructureInfo.ofInductiveVal (v : InductiveVal) : MetaM StructureInfo := do def StructureInfo.ofInductiveVal (v : InductiveVal) : MetaM StructureInfo := do
let info ← Info.ofConstantVal v.toConstantVal let info ← Info.ofConstantVal v.toConstantVal
@ -42,7 +42,7 @@ def StructureInfo.ofInductiveVal (v : InductiveVal) : MetaM StructureInfo := do
if i.fieldNames.size - parents.size > 0 then if i.fieldNames.size - parents.size > 0 then
return { return {
toInfo := info, toInfo := info,
fieldInfo := ← getFieldTypes v.name ctorVal parents.size, fieldInfo := ← getFieldTypes v,
parents, parents,
ctor ctor
} }