2023-10-06 17:31:36 -07:00
|
|
|
|
import LSpec
|
2023-10-15 12:31:22 -07:00
|
|
|
|
import Pantograph.Goal
|
2023-10-06 17:31:36 -07:00
|
|
|
|
import Pantograph.Serial
|
2023-11-04 15:00:51 -07:00
|
|
|
|
import Test.Common
|
2024-03-28 19:27:45 -07:00
|
|
|
|
import Lean
|
2023-10-06 17:31:36 -07:00
|
|
|
|
|
2024-03-28 18:56:42 -07:00
|
|
|
|
namespace Pantograph.Test.Metavar
|
2023-10-06 17:31:36 -07:00
|
|
|
|
open Pantograph
|
|
|
|
|
open Lean
|
|
|
|
|
|
2024-04-06 21:52:25 -07:00
|
|
|
|
abbrev TestM := StateRefT LSpec.TestSeq (ReaderT Protocol.Options Elab.TermElabM)
|
2023-10-06 17:31:36 -07:00
|
|
|
|
|
2023-11-04 15:00:51 -07:00
|
|
|
|
def addTest (test: LSpec.TestSeq): TestM Unit := do
|
2023-10-06 17:31:36 -07:00
|
|
|
|
set $ (← get) ++ test
|
|
|
|
|
|
2024-03-29 23:46:08 -07:00
|
|
|
|
-- Tests that all delay assigned mvars are instantiated
|
|
|
|
|
def test_instantiate_mvar: TestM Unit := do
|
|
|
|
|
let env ← Lean.MonadEnv.getEnv
|
|
|
|
|
let value := "@Nat.le_trans 2 2 5 (@of_eq_true (@LE.le Nat instLENat 2 2) (@eq_true (@LE.le Nat instLENat 2 2) (@Nat.le_refl 2))) (@of_eq_true (@LE.le Nat instLENat 2 5) (@eq_true_of_decide (@LE.le Nat instLENat 2 5) (@Nat.decLe 2 5) (@Eq.refl Bool Bool.true)))"
|
2024-03-31 15:55:08 -07:00
|
|
|
|
let syn ← match parseTerm env value with
|
2024-03-29 23:46:08 -07:00
|
|
|
|
| .ok s => pure $ s
|
|
|
|
|
| .error e => do
|
|
|
|
|
addTest $ assertUnreachable e
|
|
|
|
|
return ()
|
2024-03-31 15:55:08 -07:00
|
|
|
|
let expr ← match ← elabTerm syn with
|
2024-03-29 23:46:08 -07:00
|
|
|
|
| .ok expr => pure $ expr
|
|
|
|
|
| .error e => do
|
|
|
|
|
addTest $ assertUnreachable e
|
|
|
|
|
return ()
|
|
|
|
|
let t ← Lean.Meta.inferType expr
|
2024-04-12 12:37:37 -07:00
|
|
|
|
addTest $ LSpec.check "typing" ((toString (← serializeExpressionSexp t)) =
|
2024-03-29 23:46:08 -07:00
|
|
|
|
"((:c LE.le) (:c Nat) (:c instLENat) ((:c OfNat.ofNat) (:mv _uniq.2) (:lit 2) (:mv _uniq.3)) ((:c OfNat.ofNat) (:mv _uniq.14) (:lit 5) (:mv _uniq.15)))")
|
|
|
|
|
return ()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-11-04 15:00:51 -07:00
|
|
|
|
def startProof (expr: String): TestM (Option GoalState) := do
|
2023-10-06 17:31:36 -07:00
|
|
|
|
let env ← Lean.MonadEnv.getEnv
|
2024-03-31 15:55:08 -07:00
|
|
|
|
let syn? := parseTerm env expr
|
2023-11-04 15:00:51 -07:00
|
|
|
|
addTest $ LSpec.check s!"Parsing {expr}" (syn?.isOk)
|
2023-10-06 17:31:36 -07:00
|
|
|
|
match syn? with
|
|
|
|
|
| .error error =>
|
|
|
|
|
IO.println error
|
|
|
|
|
return Option.none
|
|
|
|
|
| .ok syn =>
|
2024-03-31 15:55:08 -07:00
|
|
|
|
let expr? ← elabType syn
|
2023-11-04 15:00:51 -07:00
|
|
|
|
addTest $ LSpec.check s!"Elaborating" expr?.isOk
|
2023-10-06 17:31:36 -07:00
|
|
|
|
match expr? with
|
|
|
|
|
| .error error =>
|
|
|
|
|
IO.println error
|
|
|
|
|
return Option.none
|
|
|
|
|
| .ok expr =>
|
|
|
|
|
let goal ← GoalState.create (expr := expr)
|
|
|
|
|
return Option.some goal
|
|
|
|
|
|
2023-11-04 15:00:51 -07:00
|
|
|
|
def buildGoal (nameType: List (String × String)) (target: String) (userName?: Option String := .none): Protocol.Goal :=
|
2023-10-06 17:31:36 -07:00
|
|
|
|
{
|
2023-11-04 15:00:51 -07:00
|
|
|
|
userName?,
|
2023-10-06 17:31:36 -07:00
|
|
|
|
target := { pp? := .some target},
|
|
|
|
|
vars := (nameType.map fun x => ({
|
2023-11-04 15:00:51 -07:00
|
|
|
|
userName := x.fst,
|
2023-10-06 17:31:36 -07:00
|
|
|
|
type? := .some { pp? := .some x.snd },
|
|
|
|
|
})).toArray
|
|
|
|
|
}
|
2023-11-04 15:00:51 -07:00
|
|
|
|
def proofRunner (env: Lean.Environment) (tests: TestM Unit): IO LSpec.TestSeq := do
|
2023-10-06 17:31:36 -07:00
|
|
|
|
let termElabM := tests.run LSpec.TestSeq.done |>.run {} -- with default options
|
|
|
|
|
|
2024-03-28 00:06:35 -07:00
|
|
|
|
let coreContext: Lean.Core.Context ← createCoreContext #[]
|
2024-07-22 17:57:01 -07:00
|
|
|
|
let metaM := termElabM.run' (ctx := Condensed.elabContext)
|
2023-10-06 17:31:36 -07:00
|
|
|
|
let coreM := metaM.run'
|
|
|
|
|
match ← (coreM.run' coreContext { env := env }).toBaseIO with
|
|
|
|
|
| .error exception =>
|
|
|
|
|
return LSpec.test "Exception" (s!"internal exception #{← exception.toMessageData.toString}" = "")
|
|
|
|
|
| .ok (_, a) =>
|
|
|
|
|
return a
|
|
|
|
|
|
2023-11-04 15:00:51 -07:00
|
|
|
|
/-- M-coupled goals -/
|
|
|
|
|
def test_m_couple: TestM Unit := do
|
|
|
|
|
let state? ← startProof "(2: Nat) ≤ 5"
|
|
|
|
|
let state0 ← match state? with
|
|
|
|
|
| .some state => pure state
|
|
|
|
|
| .none => do
|
|
|
|
|
addTest $ assertUnreachable "Goal could not parse"
|
|
|
|
|
return ()
|
|
|
|
|
|
2024-09-06 21:07:12 -07:00
|
|
|
|
let state1 ← match ← state0.tacticOn (goalId := 0) (tactic := "apply Nat.le_trans") with
|
2023-11-04 15:00:51 -07:00
|
|
|
|
| .success state => pure state
|
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
|
|
|
|
addTest $ LSpec.check "apply Nat.le_trans" ((← state1.serializeGoals (options := ← read)).map (·.target.pp?) =
|
|
|
|
|
#[.some "2 ≤ ?m", .some "?m ≤ 5", .some "Nat"])
|
|
|
|
|
addTest $ LSpec.test "(1 root)" state1.rootExpr?.isNone
|
|
|
|
|
-- Set m to 3
|
2024-09-06 21:07:12 -07:00
|
|
|
|
let state2 ← match ← state1.tacticOn (goalId := 2) (tactic := "exact 3") with
|
2023-11-04 15:00:51 -07:00
|
|
|
|
| .success state => pure state
|
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
|
|
|
|
addTest $ LSpec.test "(1b root)" state2.rootExpr?.isNone
|
2023-11-04 15:33:53 -07:00
|
|
|
|
let state1b ← match state2.continue state1 with
|
2023-11-04 15:00:51 -07:00
|
|
|
|
| .error msg => do
|
|
|
|
|
addTest $ assertUnreachable $ msg
|
|
|
|
|
return ()
|
|
|
|
|
| .ok state => pure state
|
|
|
|
|
addTest $ LSpec.check "exact 3" ((← state1b.serializeGoals (options := ← read)).map (·.target.pp?) =
|
|
|
|
|
#[.some "2 ≤ 3", .some "3 ≤ 5"])
|
|
|
|
|
addTest $ LSpec.test "(2 root)" state1b.rootExpr?.isNone
|
2024-03-28 19:27:45 -07:00
|
|
|
|
|
|
|
|
|
def test_m_couple_simp: TestM Unit := do
|
|
|
|
|
let state? ← startProof "(2: Nat) ≤ 5"
|
|
|
|
|
let state0 ← match state? with
|
|
|
|
|
| .some state => pure state
|
|
|
|
|
| .none => do
|
|
|
|
|
addTest $ assertUnreachable "Goal could not parse"
|
|
|
|
|
return ()
|
|
|
|
|
|
2024-09-06 21:07:12 -07:00
|
|
|
|
let state1 ← match ← state0.tacticOn (goalId := 0) (tactic := "apply Nat.le_trans") with
|
2024-03-28 19:27:45 -07:00
|
|
|
|
| .success state => pure state
|
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
2024-04-12 20:51:54 -07:00
|
|
|
|
let serializedState1 ← state1.serializeGoals (options := { ← read with printDependentMVars := true })
|
|
|
|
|
addTest $ LSpec.check "apply Nat.le_trans" (serializedState1.map (·.target.pp?) =
|
2024-03-28 19:27:45 -07:00
|
|
|
|
#[.some "2 ≤ ?m", .some "?m ≤ 5", .some "Nat"])
|
2024-04-12 20:51:54 -07:00
|
|
|
|
addTest $ LSpec.check "(metavariables)" (serializedState1.map (·.target.dependentMVars?.get!) =
|
|
|
|
|
#[#["_uniq.38"], #["_uniq.38"], #[]])
|
|
|
|
|
|
2024-09-06 21:07:12 -07:00
|
|
|
|
let state2 ← match ← state1.tacticOn (goalId := 2) (tactic := "exact 2") with
|
2024-03-28 19:27:45 -07:00
|
|
|
|
| .success state => pure state
|
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
|
|
|
|
addTest $ LSpec.test "(1b root)" state2.rootExpr?.isNone
|
|
|
|
|
let state1b ← match state2.continue state1 with
|
|
|
|
|
| .error msg => do
|
|
|
|
|
addTest $ assertUnreachable $ msg
|
|
|
|
|
return ()
|
|
|
|
|
| .ok state => pure state
|
|
|
|
|
addTest $ LSpec.check "exact 2" ((← state1b.serializeGoals (options := ← read)).map (·.target.pp?) =
|
|
|
|
|
#[.some "2 ≤ 2", .some "2 ≤ 5"])
|
|
|
|
|
addTest $ LSpec.test "(2 root)" state1b.rootExpr?.isNone
|
2024-09-06 21:07:12 -07:00
|
|
|
|
let state3 ← match ← state1b.tacticOn (goalId := 0) (tactic := "simp") with
|
2024-03-28 19:27:45 -07:00
|
|
|
|
| .success state => pure state
|
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
|
|
|
|
let state4 ← match state3.continue state1b with
|
|
|
|
|
| .error msg => do
|
|
|
|
|
addTest $ assertUnreachable $ msg
|
|
|
|
|
return ()
|
|
|
|
|
| .ok state => pure state
|
2024-09-06 21:07:12 -07:00
|
|
|
|
let state5 ← match ← state4.tacticOn (goalId := 0) (tactic := "simp") with
|
2024-03-28 19:27:45 -07:00
|
|
|
|
| .success state => pure state
|
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
|
|
|
|
|
|
|
|
|
state5.restoreMetaM
|
|
|
|
|
let root ← match state5.rootExpr? with
|
|
|
|
|
| .some e => pure e
|
|
|
|
|
| .none =>
|
|
|
|
|
addTest $ assertUnreachable "(5 root)"
|
|
|
|
|
return ()
|
|
|
|
|
let rootStr: String := toString (← Lean.Meta.ppExpr root)
|
|
|
|
|
addTest $ LSpec.check "(5 root)" (rootStr = "Nat.le_trans (of_eq_true (Init.Data.Nat.Basic._auxLemma.4 2)) (of_eq_true (eq_true_of_decide (Eq.refl true)))")
|
2024-03-29 23:46:08 -07:00
|
|
|
|
let unfoldedRoot ← unfoldAuxLemmas root
|
|
|
|
|
addTest $ LSpec.check "(5 root)" ((toString (← Lean.Meta.ppExpr unfoldedRoot)) =
|
|
|
|
|
"Nat.le_trans (of_eq_true (eq_true (Nat.le_refl 2))) (of_eq_true (eq_true_of_decide (Eq.refl true)))")
|
2023-11-04 15:00:51 -07:00
|
|
|
|
return ()
|
|
|
|
|
|
|
|
|
|
def test_proposition_generation: TestM Unit := do
|
|
|
|
|
let state? ← startProof "Σ' p:Prop, p"
|
|
|
|
|
let state0 ← match state? with
|
|
|
|
|
| .some state => pure state
|
|
|
|
|
| .none => do
|
|
|
|
|
addTest $ assertUnreachable "Goal could not parse"
|
|
|
|
|
return ()
|
|
|
|
|
|
2024-09-06 21:07:12 -07:00
|
|
|
|
let state1 ← match ← state0.tacticOn (goalId := 0) (tactic := "apply PSigma.mk") with
|
2023-11-04 15:00:51 -07:00
|
|
|
|
| .success state => pure state
|
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
|
|
|
|
addTest $ LSpec.check "apply PSigma.mk" ((← state1.serializeGoals (options := ← read)).map (·.devolatilize) =
|
|
|
|
|
#[
|
|
|
|
|
buildGoal [] "?fst" (userName? := .some "snd"),
|
|
|
|
|
buildGoal [] "Prop" (userName? := .some "fst")
|
|
|
|
|
])
|
|
|
|
|
if let #[goal1, goal2] := ← state1.serializeGoals (options := { (← read) with printExprAST := true }) then
|
|
|
|
|
addTest $ LSpec.test "(1 reference)" (goal1.target.sexp? = .some s!"(:mv {goal2.name})")
|
|
|
|
|
addTest $ LSpec.test "(1 root)" state1.rootExpr?.isNone
|
|
|
|
|
|
2024-09-06 21:07:12 -07:00
|
|
|
|
let state2 ← match ← state1.tryAssign (state1.get! 0) (expr := "λ (x: Nat) => _") with
|
2023-11-04 15:00:51 -07:00
|
|
|
|
| .success state => pure state
|
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
|
|
|
|
addTest $ LSpec.check ":= λ (x: Nat), _" ((← state2.serializeGoals (options := ← read)).map (·.target.pp?) =
|
2024-08-17 00:47:12 -07:00
|
|
|
|
#[.some "?m.29 x"])
|
2023-11-04 15:00:51 -07:00
|
|
|
|
addTest $ LSpec.test "(2 root)" state2.rootExpr?.isNone
|
|
|
|
|
|
2024-08-17 00:47:12 -07:00
|
|
|
|
let assign := "Eq.refl x"
|
2024-09-06 21:07:12 -07:00
|
|
|
|
let state3 ← match ← state2.tryAssign (state2.get! 0) (expr := assign) with
|
2023-11-04 15:00:51 -07:00
|
|
|
|
| .success state => pure state
|
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
2024-08-17 00:47:12 -07:00
|
|
|
|
addTest $ LSpec.check s!":= {assign}" ((← state3.serializeGoals (options := ← read)).map (·.target.pp?) =
|
2023-11-04 15:00:51 -07:00
|
|
|
|
#[])
|
2023-11-04 15:53:57 -07:00
|
|
|
|
|
2023-11-04 15:00:51 -07:00
|
|
|
|
addTest $ LSpec.test "(3 root)" state3.rootExpr?.isSome
|
|
|
|
|
return ()
|
|
|
|
|
|
2023-11-04 15:33:53 -07:00
|
|
|
|
def test_partial_continuation: TestM Unit := do
|
|
|
|
|
let state? ← startProof "(2: Nat) ≤ 5"
|
|
|
|
|
let state0 ← match state? with
|
|
|
|
|
| .some state => pure state
|
|
|
|
|
| .none => do
|
|
|
|
|
addTest $ assertUnreachable "Goal could not parse"
|
|
|
|
|
return ()
|
|
|
|
|
|
2024-09-06 21:07:12 -07:00
|
|
|
|
let state1 ← match ← state0.tacticOn (goalId := 0) (tactic := "apply Nat.le_trans") with
|
2023-11-04 15:33:53 -07:00
|
|
|
|
| .success state => pure state
|
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
|
|
|
|
addTest $ LSpec.check "apply Nat.le_trans" ((← state1.serializeGoals (options := ← read)).map (·.target.pp?) =
|
|
|
|
|
#[.some "2 ≤ ?m", .some "?m ≤ 5", .some "Nat"])
|
|
|
|
|
|
2024-09-06 21:07:12 -07:00
|
|
|
|
let state2 ← match ← state1.tacticOn (goalId := 2) (tactic := "apply Nat.succ") with
|
2023-11-04 15:33:53 -07:00
|
|
|
|
| .success state => pure state
|
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
|
|
|
|
addTest $ LSpec.check "apply Nat.succ" ((← state2.serializeGoals (options := ← read)).map (·.target.pp?) =
|
|
|
|
|
#[.some "Nat"])
|
|
|
|
|
|
|
|
|
|
-- Execute a partial continuation
|
|
|
|
|
let coupled_goals := state1.goals ++ state2.goals
|
|
|
|
|
let state1b ← match state2.resume (goals := coupled_goals) with
|
|
|
|
|
| .error msg => do
|
|
|
|
|
addTest $ assertUnreachable $ msg
|
|
|
|
|
return ()
|
|
|
|
|
| .ok state => pure state
|
|
|
|
|
addTest $ LSpec.check "(continue)" ((← state1b.serializeGoals (options := ← read)).map (·.target.pp?) =
|
2024-03-28 00:06:35 -07:00
|
|
|
|
#[.some "2 ≤ ?m.succ", .some "?m.succ ≤ 5", .some "Nat"])
|
2023-11-04 15:33:53 -07:00
|
|
|
|
addTest $ LSpec.test "(2 root)" state1b.rootExpr?.isNone
|
|
|
|
|
|
2023-11-06 10:45:11 -08:00
|
|
|
|
-- Roundtrip
|
|
|
|
|
--let coupled_goals := coupled_goals.map (λ g =>
|
2024-04-12 12:37:37 -07:00
|
|
|
|
-- { name := str_to_name $ serializeName g.name (sanitize := false)})
|
|
|
|
|
let coupled_goals := coupled_goals.map (λ g => serializeName g.name (sanitize := false))
|
2023-11-06 10:45:11 -08:00
|
|
|
|
let coupled_goals := coupled_goals.map (λ g => { name := g.toName })
|
|
|
|
|
let state1b ← match state2.resume (goals := coupled_goals) with
|
|
|
|
|
| .error msg => do
|
|
|
|
|
addTest $ assertUnreachable $ msg
|
|
|
|
|
return ()
|
|
|
|
|
| .ok state => pure state
|
|
|
|
|
addTest $ LSpec.check "(continue)" ((← state1b.serializeGoals (options := ← read)).map (·.target.pp?) =
|
2024-03-28 00:06:35 -07:00
|
|
|
|
#[.some "2 ≤ ?m.succ", .some "?m.succ ≤ 5", .some "Nat"])
|
2023-11-06 10:45:11 -08:00
|
|
|
|
addTest $ LSpec.test "(2 root)" state1b.rootExpr?.isNone
|
|
|
|
|
|
2023-11-04 15:33:53 -07:00
|
|
|
|
-- Continuation should fail if the state does not exist:
|
|
|
|
|
match state0.resume coupled_goals with
|
2024-05-12 22:33:38 -07:00
|
|
|
|
| .error error => addTest $ LSpec.check "(continuation failure message)" (error = "Goals [_uniq.40, _uniq.41, _uniq.38, _uniq.47] are not in scope")
|
2023-11-04 15:33:53 -07:00
|
|
|
|
| .ok _ => addTest $ assertUnreachable "(continuation failure)"
|
2023-11-04 15:53:57 -07:00
|
|
|
|
-- Continuation should fail if some goals have not been solved
|
|
|
|
|
match state2.continue state1 with
|
|
|
|
|
| .error error => addTest $ LSpec.check "(continuation failure message)" (error = "Target state has unresolved goals")
|
|
|
|
|
| .ok _ => addTest $ assertUnreachable "(continuation failure)"
|
2023-11-04 15:33:53 -07:00
|
|
|
|
return ()
|
|
|
|
|
|
2023-11-04 15:00:51 -07:00
|
|
|
|
|
2024-04-06 14:07:13 -07:00
|
|
|
|
def suite (env: Environment): List (String × IO LSpec.TestSeq) :=
|
2023-10-06 17:31:36 -07:00
|
|
|
|
let tests := [
|
2024-03-29 23:46:08 -07:00
|
|
|
|
("Instantiate", test_instantiate_mvar),
|
2023-11-04 15:00:51 -07:00
|
|
|
|
("2 < 5", test_m_couple),
|
2024-03-28 19:27:45 -07:00
|
|
|
|
("2 < 5", test_m_couple_simp),
|
2023-11-04 15:33:53 -07:00
|
|
|
|
("Proposition Generation", test_proposition_generation),
|
|
|
|
|
("Partial Continuation", test_partial_continuation)
|
2023-10-06 17:31:36 -07:00
|
|
|
|
]
|
2024-04-06 14:07:13 -07:00
|
|
|
|
tests.map (fun (name, test) => (name, proofRunner env test))
|
2023-10-06 17:31:36 -07:00
|
|
|
|
|
2024-03-28 18:56:42 -07:00
|
|
|
|
end Pantograph.Test.Metavar
|