2023-10-15 17:15:23 -07:00
|
|
|
|
/-
|
|
|
|
|
Tests pertaining to goals with no interdependencies
|
|
|
|
|
-/
|
2023-05-22 14:49:56 -07:00
|
|
|
|
import LSpec
|
2023-10-15 12:31:22 -07:00
|
|
|
|
import Pantograph.Goal
|
2023-05-22 14:49:56 -07:00
|
|
|
|
import Pantograph.Serial
|
2023-10-25 22:18:59 -07:00
|
|
|
|
import Test.Common
|
2023-05-22 14:49:56 -07:00
|
|
|
|
|
2023-10-06 17:31:36 -07:00
|
|
|
|
namespace Pantograph.Test.Proofs
|
2023-05-22 14:49:56 -07:00
|
|
|
|
open Pantograph
|
2023-05-24 00:54:48 -07:00
|
|
|
|
open Lean
|
2023-05-22 14:49:56 -07:00
|
|
|
|
|
|
|
|
|
inductive Start where
|
|
|
|
|
| copy (name: String) -- Start from some name in the environment
|
|
|
|
|
| expr (expr: String) -- Start from some expression
|
|
|
|
|
|
2024-04-06 21:52:25 -07:00
|
|
|
|
abbrev TestM := StateRefT LSpec.TestSeq (ReaderT Protocol.Options Elab.TermElabM)
|
2023-05-23 05:12:46 -07:00
|
|
|
|
|
2023-10-15 17:15:23 -07:00
|
|
|
|
def addTest (test: LSpec.TestSeq): TestM Unit := do
|
2023-08-27 19:53:09 -07:00
|
|
|
|
set $ (← get) ++ test
|
|
|
|
|
|
2023-10-15 17:15:23 -07:00
|
|
|
|
def startProof (start: Start): TestM (Option GoalState) := do
|
2023-05-23 05:12:46 -07:00
|
|
|
|
let env ← Lean.MonadEnv.getEnv
|
2023-05-22 14:49:56 -07:00
|
|
|
|
match start with
|
|
|
|
|
| .copy name =>
|
2023-11-06 10:45:11 -08:00
|
|
|
|
let cInfo? := name.toName |> env.find?
|
2023-10-15 17:15:23 -07:00
|
|
|
|
addTest $ LSpec.check s!"Symbol exists {name}" cInfo?.isSome
|
2023-05-22 14:49:56 -07:00
|
|
|
|
match cInfo? with
|
2023-05-23 05:12:46 -07:00
|
|
|
|
| .some cInfo =>
|
2023-08-27 19:53:09 -07:00
|
|
|
|
let goal ← GoalState.create (expr := cInfo.type)
|
|
|
|
|
return Option.some goal
|
2023-05-22 14:49:56 -07:00
|
|
|
|
| .none =>
|
2023-08-27 19:53:09 -07:00
|
|
|
|
return Option.none
|
2023-05-22 14:49:56 -07:00
|
|
|
|
| .expr expr =>
|
2024-03-31 15:55:08 -07:00
|
|
|
|
let syn? := parseTerm env expr
|
2023-10-15 17:15:23 -07:00
|
|
|
|
addTest $ LSpec.check s!"Parsing {expr}" (syn?.isOk)
|
2023-05-22 14:49:56 -07:00
|
|
|
|
match syn? with
|
|
|
|
|
| .error error =>
|
|
|
|
|
IO.println error
|
2023-08-27 19:53:09 -07:00
|
|
|
|
return Option.none
|
2023-05-22 14:49:56 -07:00
|
|
|
|
| .ok syn =>
|
2024-03-31 15:55:08 -07:00
|
|
|
|
let expr? ← elabType syn
|
2023-10-15 17:15:23 -07:00
|
|
|
|
addTest $ LSpec.check s!"Elaborating" expr?.isOk
|
2023-05-22 14:49:56 -07:00
|
|
|
|
match expr? with
|
|
|
|
|
| .error error =>
|
|
|
|
|
IO.println error
|
2023-08-27 19:53:09 -07:00
|
|
|
|
return Option.none
|
2023-05-22 14:49:56 -07:00
|
|
|
|
| .ok expr =>
|
2023-08-27 19:53:09 -07:00
|
|
|
|
let goal ← GoalState.create (expr := expr)
|
|
|
|
|
return Option.some goal
|
2023-05-22 14:49:56 -07:00
|
|
|
|
|
2023-10-30 14:44:06 -07:00
|
|
|
|
def buildGoal (nameType: List (String × String)) (target: String) (userName?: Option String := .none): Protocol.Goal :=
|
2023-05-27 23:10:39 -07:00
|
|
|
|
{
|
2023-10-30 14:44:06 -07:00
|
|
|
|
userName?,
|
2023-08-14 21:43:40 -07:00
|
|
|
|
target := { pp? := .some target},
|
|
|
|
|
vars := (nameType.map fun x => ({
|
2023-10-25 22:18:59 -07:00
|
|
|
|
userName := x.fst,
|
2023-08-15 15:40:54 -07:00
|
|
|
|
type? := .some { pp? := .some x.snd },
|
|
|
|
|
isInaccessible? := .some false
|
|
|
|
|
})).toArray
|
2023-05-27 23:10:39 -07:00
|
|
|
|
}
|
2023-10-15 17:15:23 -07:00
|
|
|
|
def proofRunner (env: Lean.Environment) (tests: TestM Unit): IO LSpec.TestSeq := do
|
|
|
|
|
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 #[]
|
|
|
|
|
let metaM := termElabM.run' (ctx := defaultTermElabMContext)
|
2023-10-15 17:15:23 -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-08-27 19:53:09 -07:00
|
|
|
|
|
|
|
|
|
-- Individual test cases
|
2023-05-22 14:49:56 -07:00
|
|
|
|
example: ∀ (a b: Nat), a + b = b + a := by
|
|
|
|
|
intro n m
|
|
|
|
|
rw [Nat.add_comm]
|
2024-04-06 16:33:20 -07:00
|
|
|
|
def test_nat_add_comm (manual: Bool): TestM Unit := do
|
2023-10-15 17:15:23 -07:00
|
|
|
|
let state? ← startProof <| match manual with
|
|
|
|
|
| false => .copy "Nat.add_comm"
|
|
|
|
|
| true => .expr "∀ (a b: Nat), a + b = b + a"
|
|
|
|
|
addTest $ LSpec.check "Start goal" state?.isSome
|
|
|
|
|
let state0 ← match state? with
|
|
|
|
|
| .some state => pure state
|
|
|
|
|
| .none => do
|
|
|
|
|
addTest $ assertUnreachable "Goal could not parse"
|
|
|
|
|
return ()
|
|
|
|
|
|
2024-04-06 16:33:20 -07:00
|
|
|
|
let state1 ← match ← state0.tryTactic (goalId := 0) (tactic := "intro n m") with
|
2023-10-26 22:47:42 -07:00
|
|
|
|
| .success state => pure state
|
2023-10-15 17:15:23 -07:00
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
2023-10-26 22:47:42 -07:00
|
|
|
|
addTest $ LSpec.check "intro n m" ((← state1.serializeGoals (options := ← read)).map (·.devolatilize) =
|
|
|
|
|
#[buildGoal [("n", "Nat"), ("m", "Nat")] "n + m = m + n"])
|
2023-10-15 17:15:23 -07:00
|
|
|
|
|
2024-04-06 16:33:20 -07:00
|
|
|
|
match ← state1.tryTactic (goalId := 0) (tactic := "assumption") with
|
2023-10-15 17:15:23 -07:00
|
|
|
|
| .failure #[message] =>
|
|
|
|
|
addTest $ LSpec.check "assumption" (message = "tactic 'assumption' failed\nn m : Nat\n⊢ n + m = m + n")
|
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
|
2024-04-06 16:33:20 -07:00
|
|
|
|
let state2 ← match ← state1.tryTactic (goalId := 0) (tactic := "rw [Nat.add_comm]") with
|
2023-10-26 22:47:42 -07:00
|
|
|
|
| .success state => pure state
|
2023-10-15 17:15:23 -07:00
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
2023-10-26 22:47:42 -07:00
|
|
|
|
addTest $ LSpec.test "rw [Nat.add_comm]" state2.goals.isEmpty
|
|
|
|
|
|
|
|
|
|
return ()
|
2024-04-06 16:33:20 -07:00
|
|
|
|
def test_delta_variable: TestM Unit := do
|
2023-10-26 22:47:42 -07:00
|
|
|
|
let options: Protocol.Options := { noRepeat := true }
|
|
|
|
|
let state? ← startProof <| .expr "∀ (a b: Nat), a + b = b + a"
|
|
|
|
|
addTest $ LSpec.check "Start goal" state?.isSome
|
|
|
|
|
let state0 ← match state? with
|
|
|
|
|
| .some state => pure state
|
|
|
|
|
| .none => do
|
|
|
|
|
addTest $ assertUnreachable "Goal could not parse"
|
|
|
|
|
return ()
|
2023-10-15 17:15:23 -07:00
|
|
|
|
|
2024-04-06 16:33:20 -07:00
|
|
|
|
let state1 ← match ← state0.tryTactic (goalId := 0) (tactic := "intro n") with
|
2023-10-26 22:47:42 -07:00
|
|
|
|
| .success state => pure state
|
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
|
|
|
|
addTest $ LSpec.check "intro n" ((← state1.serializeGoals (parent := state0) options).map (·.devolatilize) =
|
|
|
|
|
#[buildGoalSelective [("n", .some "Nat")] "∀ (b : Nat), n + b = b + n"])
|
2024-04-06 16:33:20 -07:00
|
|
|
|
let state2 ← match ← state1.tryTactic (goalId := 0) (tactic := "intro m") with
|
2023-10-26 22:47:42 -07:00
|
|
|
|
| .success state => pure state
|
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
|
|
|
|
addTest $ LSpec.check "intro m" ((← state2.serializeGoals (parent := state1) options).map (·.devolatilize) =
|
|
|
|
|
#[buildGoalSelective [("n", .none), ("m", .some "Nat")] "n + m = m + n"])
|
2023-10-15 17:15:23 -07:00
|
|
|
|
return ()
|
2023-10-26 22:47:42 -07:00
|
|
|
|
where
|
|
|
|
|
-- Like `buildGoal` but allow certain variables to be elided.
|
|
|
|
|
buildGoalSelective (nameType: List (String × Option String)) (target: String): Protocol.Goal :=
|
|
|
|
|
{
|
|
|
|
|
target := { pp? := .some target},
|
|
|
|
|
vars := (nameType.map fun x => ({
|
|
|
|
|
userName := x.fst,
|
|
|
|
|
type? := x.snd.map (λ type => { pp? := type }),
|
|
|
|
|
isInaccessible? := x.snd.map (λ _ => false)
|
|
|
|
|
})).toArray
|
|
|
|
|
}
|
2023-08-27 19:53:09 -07:00
|
|
|
|
|
2023-10-26 11:22:02 -07:00
|
|
|
|
example (w x y z : Nat) (p : Nat → Prop)
|
|
|
|
|
(h : p (x * y + z * w * x)) : p (x * w * z + y * x) := by
|
|
|
|
|
simp [Nat.add_assoc, Nat.add_comm, Nat.add_left_comm, Nat.mul_comm, Nat.mul_assoc, Nat.mul_left_comm] at *
|
|
|
|
|
assumption
|
2024-04-06 16:33:20 -07:00
|
|
|
|
def test_arith: TestM Unit := do
|
2023-10-26 11:22:02 -07:00
|
|
|
|
let state? ← startProof (.expr "∀ (w x y z : Nat) (p : Nat → Prop) (h : p (x * y + z * w * x)), p (x * w * z + y * x)")
|
|
|
|
|
let state0 ← match state? with
|
|
|
|
|
| .some state => pure state
|
|
|
|
|
| .none => do
|
|
|
|
|
addTest $ assertUnreachable "Goal could not parse"
|
|
|
|
|
return ()
|
|
|
|
|
|
2024-04-06 16:33:20 -07:00
|
|
|
|
let state1 ← match ← state0.tryTactic (goalId := 0) (tactic := "intros") with
|
2023-10-26 22:47:42 -07:00
|
|
|
|
| .success state => pure state
|
2023-10-26 11:22:02 -07:00
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
2023-10-26 22:47:42 -07:00
|
|
|
|
addTest $ LSpec.check "intros" (state1.goals.length = 1)
|
2023-10-27 15:41:12 -07:00
|
|
|
|
addTest $ LSpec.test "(1 root)" state1.rootExpr?.isNone
|
2024-04-06 16:33:20 -07:00
|
|
|
|
let state2 ← match ← state1.tryTactic (goalId := 0) (tactic := "simp [Nat.add_assoc, Nat.add_comm, Nat.add_left_comm, Nat.mul_comm, Nat.mul_assoc, Nat.mul_left_comm] at *") with
|
2023-10-26 22:47:42 -07:00
|
|
|
|
| .success state => pure state
|
2023-10-26 11:22:02 -07:00
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
2023-10-26 22:47:42 -07:00
|
|
|
|
addTest $ LSpec.check "simp ..." (state2.goals.length = 1)
|
2023-10-27 15:41:12 -07:00
|
|
|
|
addTest $ LSpec.check "(2 root)" state2.rootExpr?.isNone
|
2024-04-06 16:33:20 -07:00
|
|
|
|
let state3 ← match ← state2.tryTactic (goalId := 0) (tactic := "assumption") with
|
2023-10-26 22:47:42 -07:00
|
|
|
|
| .success state => pure state
|
2023-10-26 11:22:02 -07:00
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
2023-10-26 22:47:42 -07:00
|
|
|
|
addTest $ LSpec.test "assumption" state3.goals.isEmpty
|
2023-10-27 15:41:12 -07:00
|
|
|
|
addTest $ LSpec.check "(3 root)" state3.rootExpr?.isSome
|
2023-10-26 11:22:02 -07:00
|
|
|
|
return ()
|
2023-05-22 14:49:56 -07:00
|
|
|
|
|
2023-05-22 22:48:48 -07:00
|
|
|
|
-- Two ways to write the same theorem
|
2023-05-22 14:49:56 -07:00
|
|
|
|
example: ∀ (p q: Prop), p ∨ q → q ∨ p := by
|
|
|
|
|
intro p q h
|
|
|
|
|
cases h
|
|
|
|
|
apply Or.inr
|
|
|
|
|
assumption
|
|
|
|
|
apply Or.inl
|
|
|
|
|
assumption
|
2023-05-22 22:48:48 -07:00
|
|
|
|
example: ∀ (p q: Prop), p ∨ q → q ∨ p := by
|
|
|
|
|
intro p q h
|
|
|
|
|
cases h
|
|
|
|
|
. apply Or.inr
|
|
|
|
|
assumption
|
|
|
|
|
. apply Or.inl
|
|
|
|
|
assumption
|
2024-04-06 16:33:20 -07:00
|
|
|
|
def test_or_comm: TestM Unit := do
|
2023-10-15 17:15:23 -07:00
|
|
|
|
let state? ← startProof (.expr "∀ (p q: Prop), p ∨ q → q ∨ p")
|
|
|
|
|
let state0 ← match state? with
|
|
|
|
|
| .some state => pure state
|
|
|
|
|
| .none => do
|
|
|
|
|
addTest $ assertUnreachable "Goal could not parse"
|
|
|
|
|
return ()
|
2024-01-30 17:22:20 -08:00
|
|
|
|
addTest $ LSpec.check "(0 parent)" state0.parentExpr?.isNone
|
|
|
|
|
addTest $ LSpec.check "(0 root)" state0.rootExpr?.isNone
|
2023-05-22 14:49:56 -07:00
|
|
|
|
|
2024-04-06 21:52:25 -07:00
|
|
|
|
let tactic := "intro p q h"
|
|
|
|
|
let state1 ← match ← state0.tryTactic (goalId := 0) (tactic := tactic) with
|
2023-10-26 22:47:42 -07:00
|
|
|
|
| .success state => pure state
|
2023-10-15 17:15:23 -07:00
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
2024-04-06 21:52:25 -07:00
|
|
|
|
addTest $ LSpec.check tactic ((← state1.serializeGoals (options := ← read)).map (·.devolatilize) =
|
2023-10-26 22:47:42 -07:00
|
|
|
|
#[buildGoal [("p", "Prop"), ("q", "Prop"), ("h", "p ∨ q")] "q ∨ p"])
|
2024-01-30 17:22:20 -08:00
|
|
|
|
addTest $ LSpec.check "(1 parent)" state1.parentExpr?.isSome
|
|
|
|
|
addTest $ LSpec.check "(1 root)" state1.rootExpr?.isNone
|
2024-04-06 21:52:25 -07:00
|
|
|
|
|
|
|
|
|
let tactic := "cases h"
|
|
|
|
|
let state2 ← match ← state1.tryTactic (goalId := 0) (tactic := tactic) with
|
2023-10-26 22:47:42 -07:00
|
|
|
|
| .success state => pure state
|
2023-10-15 17:15:23 -07:00
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
2024-04-06 21:52:25 -07:00
|
|
|
|
addTest $ LSpec.check tactic ((← state2.serializeGoals (options := ← read)).map (·.devolatilize) =
|
2023-10-26 22:47:42 -07:00
|
|
|
|
#[branchGoal "inl" "p", branchGoal "inr" "q"])
|
2024-01-30 17:22:20 -08:00
|
|
|
|
addTest $ LSpec.check "(2 parent)" state2.parentExpr?.isSome
|
|
|
|
|
addTest $ LSpec.check "(2 root)" state2.rootExpr?.isNone
|
2023-10-15 17:15:23 -07:00
|
|
|
|
|
2024-02-15 14:47:09 -08:00
|
|
|
|
let state2parent ← serialize_expression_ast state2.parentExpr?.get! (sanitize := false)
|
|
|
|
|
-- This is due to delayed assignment
|
|
|
|
|
addTest $ LSpec.test "(2 parent)" (state2parent ==
|
2024-03-28 00:06:35 -07:00
|
|
|
|
"((:mv _uniq.43) (:fv _uniq.16) ((:c Eq.refl) ((:c Or) (:fv _uniq.10) (:fv _uniq.13)) (:fv _uniq.16)))")
|
2024-02-15 14:47:09 -08:00
|
|
|
|
|
2024-04-06 16:33:20 -07:00
|
|
|
|
let state3_1 ← match ← state2.tryTactic (goalId := 0) (tactic := "apply Or.inr") with
|
2023-10-26 22:47:42 -07:00
|
|
|
|
| .success state => pure state
|
2023-10-15 17:15:23 -07:00
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
2024-01-24 18:19:04 -08:00
|
|
|
|
let state3_1parent ← serialize_expression_ast state3_1.parentExpr?.get! (sanitize := false)
|
2024-03-28 00:06:35 -07:00
|
|
|
|
addTest $ LSpec.test "(3_1 parent)" (state3_1parent == "((:c Or.inr) (:fv _uniq.13) (:fv _uniq.10) (:mv _uniq.78))")
|
2023-10-26 22:47:42 -07:00
|
|
|
|
addTest $ LSpec.check "· apply Or.inr" (state3_1.goals.length = 1)
|
2024-04-06 16:33:20 -07:00
|
|
|
|
let state4_1 ← match ← state3_1.tryTactic (goalId := 0) (tactic := "assumption") with
|
2023-10-26 22:47:42 -07:00
|
|
|
|
| .success state => pure state
|
2023-10-15 17:15:23 -07:00
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
2023-10-27 15:15:22 -07:00
|
|
|
|
addTest $ LSpec.check " assumption" state4_1.goals.isEmpty
|
2024-01-24 18:19:04 -08:00
|
|
|
|
let state4_1parent ← serialize_expression_ast state4_1.parentExpr?.get! (sanitize := false)
|
2024-03-28 00:06:35 -07:00
|
|
|
|
addTest $ LSpec.test "(4_1 parent)" (state4_1parent == "(:fv _uniq.47)")
|
2023-10-27 15:41:12 -07:00
|
|
|
|
addTest $ LSpec.check "(4_1 root)" state4_1.rootExpr?.isNone
|
2024-04-06 16:33:20 -07:00
|
|
|
|
let state3_2 ← match ← state2.tryTactic (goalId := 1) (tactic := "apply Or.inl") with
|
2023-10-26 22:47:42 -07:00
|
|
|
|
| .success state => pure state
|
2023-10-15 17:15:23 -07:00
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
2023-10-26 22:47:42 -07:00
|
|
|
|
addTest $ LSpec.check "· apply Or.inl" (state3_2.goals.length = 1)
|
2024-04-06 16:33:20 -07:00
|
|
|
|
let state4_2 ← match ← state3_2.tryTactic (goalId := 0) (tactic := "assumption") with
|
2023-10-26 22:47:42 -07:00
|
|
|
|
| .success state => pure state
|
2023-10-15 17:15:23 -07:00
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
2023-10-27 15:15:22 -07:00
|
|
|
|
addTest $ LSpec.check " assumption" state4_2.goals.isEmpty
|
2023-10-27 15:41:12 -07:00
|
|
|
|
addTest $ LSpec.check "(4_2 root)" state4_2.rootExpr?.isNone
|
2023-10-25 16:03:45 -07:00
|
|
|
|
-- Ensure the proof can continue from `state4_2`.
|
2023-11-04 15:33:53 -07:00
|
|
|
|
let state2b ← match state4_2.continue state2 with
|
2023-10-25 16:03:45 -07:00
|
|
|
|
| .error msg => do
|
|
|
|
|
addTest $ assertUnreachable $ msg
|
|
|
|
|
return ()
|
|
|
|
|
| .ok state => pure state
|
2023-10-26 22:47:42 -07:00
|
|
|
|
addTest $ LSpec.test "(resume)" (state2b.goals == [state2.goals.get! 0])
|
2024-04-06 16:33:20 -07:00
|
|
|
|
let state3_1 ← match ← state2b.tryTactic (goalId := 0) (tactic := "apply Or.inr") with
|
2023-10-26 22:47:42 -07:00
|
|
|
|
| .success state => pure state
|
2023-10-25 16:03:45 -07:00
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
2023-10-26 22:47:42 -07:00
|
|
|
|
addTest $ LSpec.check "· apply Or.inr" (state3_1.goals.length = 1)
|
2024-04-06 16:33:20 -07:00
|
|
|
|
let state4_1 ← match ← state3_1.tryTactic (goalId := 0) (tactic := "assumption") with
|
2023-10-26 22:47:42 -07:00
|
|
|
|
| .success state => pure state
|
2023-10-25 16:03:45 -07:00
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
2023-10-27 15:15:22 -07:00
|
|
|
|
addTest $ LSpec.check " assumption" state4_1.goals.isEmpty
|
2023-10-27 15:41:12 -07:00
|
|
|
|
addTest $ LSpec.check "(4_1 root)" state4_1.rootExpr?.isSome
|
2023-10-15 17:15:23 -07:00
|
|
|
|
|
|
|
|
|
return ()
|
|
|
|
|
where
|
|
|
|
|
typeProp: Protocol.Expression := { pp? := .some "Prop" }
|
2023-10-30 14:44:06 -07:00
|
|
|
|
branchGoal (caseName varName: String): Protocol.Goal := {
|
|
|
|
|
userName? := .some caseName,
|
2023-10-15 17:15:23 -07:00
|
|
|
|
target := { pp? := .some "q ∨ p" },
|
|
|
|
|
vars := #[
|
2023-10-25 22:18:59 -07:00
|
|
|
|
{ userName := "p", type? := .some typeProp, isInaccessible? := .some false },
|
|
|
|
|
{ userName := "q", type? := .some typeProp, isInaccessible? := .some false },
|
2023-10-30 14:44:06 -07:00
|
|
|
|
{ userName := "h✝", type? := .some { pp? := .some varName }, isInaccessible? := .some true }
|
2023-10-15 17:15:23 -07:00
|
|
|
|
]
|
|
|
|
|
}
|
2024-04-06 17:22:09 -07:00
|
|
|
|
|
|
|
|
|
def test_have: TestM Unit := do
|
2024-04-06 16:33:20 -07:00
|
|
|
|
let state? ← startProof (.expr "∀ (p q: Prop), p → ((p ∨ q) ∨ (p ∨ q))")
|
|
|
|
|
let state0 ← match state? with
|
|
|
|
|
| .some state => pure state
|
|
|
|
|
| .none => do
|
|
|
|
|
addTest $ assertUnreachable "Goal could not parse"
|
|
|
|
|
return ()
|
2024-04-06 21:52:25 -07:00
|
|
|
|
let tactic := "intro p q h"
|
|
|
|
|
let state1 ← match ← state0.tryTactic (goalId := 0) (tactic := tactic) with
|
2024-04-06 16:33:20 -07:00
|
|
|
|
| .success state => pure state
|
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
2024-04-06 21:52:25 -07:00
|
|
|
|
addTest $ LSpec.check tactic ((← state1.serializeGoals (options := ← read)).map (·.devolatilize) =
|
2024-04-06 16:33:20 -07:00
|
|
|
|
#[buildGoal [("p", "Prop"), ("q", "Prop"), ("h", "p")] "(p ∨ q) ∨ p ∨ q"])
|
|
|
|
|
|
2024-04-06 21:52:25 -07:00
|
|
|
|
let expr := "Or.inl (Or.inl h)"
|
|
|
|
|
let state2 ← match ← state1.tryAssign (goalId := 0) (expr := expr) with
|
|
|
|
|
| .success state => pure state
|
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
|
|
|
|
addTest $ LSpec.check s!":= {expr}" ((← state2.serializeGoals (options := ← read)).map (·.devolatilize) =
|
|
|
|
|
#[])
|
|
|
|
|
|
|
|
|
|
let haveBind := "y"
|
|
|
|
|
let haveType := "p ∨ q"
|
|
|
|
|
let state2 ← match ← state1.tryHave (goalId := 0) (binderName := haveBind) (type := haveType) with
|
2024-04-06 16:33:20 -07:00
|
|
|
|
| .success state => pure state
|
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
2024-04-06 21:52:25 -07:00
|
|
|
|
addTest $ LSpec.check s!"have {haveBind}: {haveType}" ((← state2.serializeGoals (options := ← read)).map (·.devolatilize) =
|
|
|
|
|
#[
|
|
|
|
|
buildGoal [("p", "Prop"), ("q", "Prop"), ("h", "p")] "p ∨ q",
|
|
|
|
|
buildGoal [("p", "Prop"), ("q", "Prop"), ("h", "p"), ("y", "p ∨ q")] "(p ∨ q) ∨ p ∨ q"
|
|
|
|
|
])
|
2024-04-06 16:33:20 -07:00
|
|
|
|
|
2024-04-06 21:52:25 -07:00
|
|
|
|
let expr := "Or.inl h"
|
|
|
|
|
let state3 ← match ← state2.tryAssign (goalId := 0) (expr := expr) with
|
2024-04-06 16:33:20 -07:00
|
|
|
|
| .success state => pure state
|
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
2024-04-06 21:52:25 -07:00
|
|
|
|
addTest $ LSpec.check s!":= {expr}" ((← state3.serializeGoals (options := ← read)).map (·.devolatilize) =
|
|
|
|
|
#[])
|
|
|
|
|
|
|
|
|
|
let state2b ← match state3.continue state2 with
|
|
|
|
|
| .ok state => pure state
|
|
|
|
|
| .error e => do
|
|
|
|
|
addTest $ assertUnreachable e
|
|
|
|
|
return ()
|
|
|
|
|
let expr := "Or.inl y"
|
|
|
|
|
let state4 ← match ← state2b.tryAssign (goalId := 0) (expr := expr) with
|
|
|
|
|
| .success state => pure state
|
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
|
|
|
|
addTest $ LSpec.check s!":= {expr}" ((← state4.serializeGoals (options := ← read)).map (·.devolatilize) =
|
|
|
|
|
#[])
|
|
|
|
|
|
|
|
|
|
addTest $ LSpec.check "(4 root)" state4.rootExpr?.isSome
|
2023-10-15 17:15:23 -07:00
|
|
|
|
|
2024-04-06 17:22:09 -07:00
|
|
|
|
example : ∀ (a b c: Nat), (a + b) + c = (b + a) + c := by
|
|
|
|
|
intro a b c
|
|
|
|
|
conv =>
|
|
|
|
|
lhs
|
|
|
|
|
congr
|
|
|
|
|
rw [Nat.add_comm]
|
|
|
|
|
rfl
|
|
|
|
|
|
|
|
|
|
def test_conv: TestM Unit := do
|
|
|
|
|
let state? ← startProof (.expr "∀ (a b c: Nat), (a + b) + c = (b + a) + c")
|
|
|
|
|
let state0 ← match state? with
|
|
|
|
|
| .some state => pure state
|
|
|
|
|
| .none => do
|
|
|
|
|
addTest $ assertUnreachable "Goal could not parse"
|
|
|
|
|
return ()
|
|
|
|
|
let tactic := "intro a b c"
|
|
|
|
|
let state1 ← match ← state0.tryTactic (goalId := 0) (tactic := tactic) with
|
|
|
|
|
| .success state => pure state
|
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
|
|
|
|
addTest $ LSpec.check tactic ((← state1.serializeGoals (options := ← read)).map (·.devolatilize) =
|
|
|
|
|
#[buildGoal [("a", "Nat"), ("b", "Nat"), ("c", "Nat")] "a + b + c = b + a + c"])
|
|
|
|
|
|
|
|
|
|
-- This solves the state in one-shot
|
|
|
|
|
let tactic := "conv => { lhs; congr; rw [Nat.add_comm]; rfl }"
|
|
|
|
|
let state2 ← match ← state1.tryTactic (goalId := 0) (tactic := tactic) with
|
|
|
|
|
| .success state => pure state
|
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
|
|
|
|
addTest $ LSpec.check tactic ((← state2.serializeGoals (options := ← read)).map (·.devolatilize) =
|
|
|
|
|
#[])
|
|
|
|
|
|
|
|
|
|
example : ∀ (a: Nat), 1 + a + 1 = a + 2 := by
|
|
|
|
|
intro a
|
|
|
|
|
calc 1 + a + 1 = a + 1 + 1 := by conv =>
|
|
|
|
|
rhs
|
|
|
|
|
rw [Nat.add_comm]
|
|
|
|
|
_ = a + 2 := by rw [Nat.add_assoc]
|
|
|
|
|
|
|
|
|
|
def test_calc: TestM Unit := do
|
|
|
|
|
let state? ← startProof (.expr "∀ (a: Nat), 1 + a + 1 = a + 2")
|
|
|
|
|
let state0 ← match state? with
|
|
|
|
|
| .some state => pure state
|
|
|
|
|
| .none => do
|
|
|
|
|
addTest $ assertUnreachable "Goal could not parse"
|
|
|
|
|
return ()
|
|
|
|
|
let tactic := "intro a"
|
|
|
|
|
let state1 ← match ← state0.tryTactic (goalId := 0) (tactic := tactic) with
|
|
|
|
|
| .success state => pure state
|
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
|
|
|
|
addTest $ LSpec.check tactic ((← state1.serializeGoals (options := ← read)).map (·.devolatilize) =
|
|
|
|
|
#[buildGoal [("a", "Nat")] "1 + a + 1 = a + 2"])
|
|
|
|
|
let tactic := "calc"
|
|
|
|
|
let state2 ← match ← state1.tryTactic (goalId := 0) (tactic := tactic) with
|
|
|
|
|
| .success state => pure state
|
|
|
|
|
| other => do
|
|
|
|
|
addTest $ assertUnreachable $ other.toString
|
|
|
|
|
return ()
|
|
|
|
|
addTest $ LSpec.check tactic ((← state1.serializeGoals (options := ← read)).map (·.devolatilize) =
|
|
|
|
|
#[buildGoal [("a", "Nat")] "1 + a + 1 = a + 2"])
|
|
|
|
|
|
2024-04-06 14:07:13 -07:00
|
|
|
|
def suite (env: Environment): List (String × IO LSpec.TestSeq) :=
|
2023-08-27 19:53:09 -07:00
|
|
|
|
let tests := [
|
2024-04-06 16:33:20 -07:00
|
|
|
|
("Nat.add_comm", test_nat_add_comm false),
|
|
|
|
|
("Nat.add_comm manual", test_nat_add_comm true),
|
|
|
|
|
("Nat.add_comm delta", test_delta_variable),
|
|
|
|
|
("arithmetic", test_arith),
|
|
|
|
|
("Or.comm", test_or_comm),
|
2024-04-06 17:22:09 -07:00
|
|
|
|
("have", test_have),
|
|
|
|
|
("conv", test_conv),
|
|
|
|
|
("calc", test_calc),
|
2023-08-27 19:53:09 -07:00
|
|
|
|
]
|
2024-04-06 14:07:13 -07:00
|
|
|
|
tests.map (fun (name, test) => (name, proofRunner env test))
|
2023-05-22 14:49:56 -07:00
|
|
|
|
|
2024-04-06 17:22:09 -07:00
|
|
|
|
|
|
|
|
|
|
2023-10-06 17:31:36 -07:00
|
|
|
|
end Pantograph.Test.Proofs
|