refactor: Code simplification

This commit is contained in:
Leni Aniva 2024-04-11 16:29:47 -07:00
parent e834765896
commit a864c4d3ff
Signed by: aniva
GPG Key ID: 4D9B1C8D10EA4C50
1 changed files with 8 additions and 10 deletions

View File

@ -48,8 +48,8 @@ protected def GoalState.create (expr: Expr): Elab.TermElabM GoalState := do
let root := goal.mvarId! let root := goal.mvarId!
let savedState ← savedStateMonad { elaborator := .anonymous } |>.run' { goals := [root]} let savedState ← savedStateMonad { elaborator := .anonymous } |>.run' { goals := [root]}
return { return {
savedState,
root, root,
savedState,
newMVars := SSet.insert .empty root, newMVars := SSet.insert .empty root,
parentMVar? := .none, parentMVar? := .none,
} }
@ -126,7 +126,7 @@ protected def GoalState.tryTactic (state: GoalState) (goalId: Nat) (tactic: Stri
(fileName := filename) with (fileName := filename) with
| .ok stx => pure $ stx | .ok stx => pure $ stx
| .error error => return .parseError error | .error error => return .parseError error
match (← executeTactic (state := state.savedState) (goal := goal) (tactic := tactic)) with match ← executeTactic (state := state.savedState) (goal := goal) (tactic := tactic) with
| .error errors => | .error errors =>
return .failure errors return .failure errors
| .ok nextSavedState => | .ok nextSavedState =>
@ -149,13 +149,12 @@ protected def GoalState.assign (state: GoalState) (goal: MVarId) (expr: Expr):
let goalType ← goal.getType let goalType ← goal.getType
try try
-- For some reason this is needed. One of the unit tests will fail if this isn't here -- For some reason this is needed. One of the unit tests will fail if this isn't here
let error?: Option String ← goal.withContext (do let error?: Option String ← goal.withContext do
let exprType ← Meta.inferType expr let exprType ← Meta.inferType expr
if ← Meta.isDefEq goalType exprType then if ← Meta.isDefEq goalType exprType then
pure .none pure .none
else do else do
return .some s!"{← Meta.ppExpr expr} : {← Meta.ppExpr exprType} != {← Meta.ppExpr goalType}" return .some s!"{← Meta.ppExpr expr} : {← Meta.ppExpr exprType} != {← Meta.ppExpr goalType}"
)
if let .some error := error? then if let .some error := error? then
return .parseError error return .parseError error
goal.checkNotAssigned `GoalState.assign goal.checkNotAssigned `GoalState.assign
@ -221,7 +220,7 @@ protected def GoalState.tryHave (state: GoalState) (goalId: Nat) (binderName: St
let binderName := binderName.toName let binderName := binderName.toName
try try
-- Implemented similarly to the intro tactic -- Implemented similarly to the intro tactic
let nextGoals: List MVarId ← goal.withContext $ (do let nextGoals: List MVarId ← goal.withContext do
let type ← Elab.Term.elabType (stx := type) let type ← Elab.Term.elabType (stx := type)
let lctx ← MonadLCtx.getLCtx let lctx ← MonadLCtx.getLCtx
@ -234,15 +233,14 @@ protected def GoalState.tryHave (state: GoalState) (goalId: Nat) (binderName: St
let fvar := mkFVar fvarId let fvar := mkFVar fvarId
let mvarUpstream ← let mvarUpstream ←
withTheReader Meta.Context (fun ctx => { ctx with lctx := lctxUpstream }) do withTheReader Meta.Context (fun ctx => { ctx with lctx := lctxUpstream }) do
Meta.withNewLocalInstances #[fvar] 0 (do Meta.withNewLocalInstances #[fvar] 0 do
let mvarUpstream ← Meta.mkFreshExprMVarAt (← getLCtx) (← Meta.getLocalInstances) let mvarUpstream ← Meta.mkFreshExprMVarAt (← getLCtx) (← Meta.getLocalInstances)
(← goal.getType) (kind := MetavarKind.synthetic) (userName := .anonymous) (← goal.getType) (kind := MetavarKind.synthetic) (userName := .anonymous)
let expr: Expr := .app (.lam binderName type mvarBranch .default) mvarUpstream let expr: Expr := .app (.lam binderName type mvarBranch .default) mvarUpstream
goal.assign expr goal.assign expr
pure mvarUpstream) pure mvarUpstream
pure [mvarBranch.mvarId!, mvarUpstream.mvarId!] pure [mvarBranch.mvarId!, mvarUpstream.mvarId!]
)
return .success { return .success {
root := state.root, root := state.root,
savedState := { savedState := {
@ -359,7 +357,7 @@ protected def GoalState.tryCalc (state: GoalState) (goalId: Nat) (pred: String):
let some (_, lhs, rhs) ← Elab.Term.getCalcRelation? step | let some (_, lhs, rhs) ← Elab.Term.getCalcRelation? step |
throwErrorAt pred "invalid 'calc' step, relation expected{indentExpr step}" throwErrorAt pred "invalid 'calc' step, relation expected{indentExpr step}"
if let some prevRhs := calcPrevRhs? then if let some prevRhs := calcPrevRhs? then
unless (← Meta.isDefEqGuarded lhs prevRhs) do unless ← Meta.isDefEqGuarded lhs prevRhs do
throwErrorAt pred "invalid 'calc' step, left-hand-side is{indentD m!"{lhs} : {← Meta.inferType lhs}"}\nprevious right-hand-side is{indentD m!"{prevRhs} : {← Meta.inferType prevRhs}"}" -- " throwErrorAt pred "invalid 'calc' step, left-hand-side is{indentD m!"{lhs} : {← Meta.inferType lhs}"}\nprevious right-hand-side is{indentD m!"{prevRhs} : {← Meta.inferType prevRhs}"}" -- "
-- Creates a mvar to represent the proof that the calc tactic solves the -- Creates a mvar to represent the proof that the calc tactic solves the
@ -384,7 +382,7 @@ protected def GoalState.tryCalc (state: GoalState) (goalId: Nat) (pred: String):
let lastStep := mkApp2 r rhs rhs' let lastStep := mkApp2 r rhs rhs'
let lastStepGoal ← Meta.mkFreshExprSyntheticOpaqueMVar lastStep tag let lastStepGoal ← Meta.mkFreshExprSyntheticOpaqueMVar lastStep tag
(proof, proofType) ← Elab.Term.mkCalcTrans proof proofType lastStepGoal lastStep (proof, proofType) ← Elab.Term.mkCalcTrans proof proofType lastStepGoal lastStep
unless (← Meta.isDefEq proofType target) do throwFailed unless ← Meta.isDefEq proofType target do throwFailed
remainder := .some lastStepGoal.mvarId! remainder := .some lastStepGoal.mvarId!
goal.assign proof goal.assign proof