2023-05-22 14:49:56 -07:00
|
|
|
|
import LSpec
|
|
|
|
|
import Pantograph.Meta
|
|
|
|
|
import Pantograph.Serial
|
|
|
|
|
|
|
|
|
|
namespace Pantograph.Test
|
|
|
|
|
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
|
|
|
|
|
|
2023-05-24 00:54:48 -07:00
|
|
|
|
abbrev TestM := StateRefT ProofTree M
|
2023-05-23 05:12:46 -07:00
|
|
|
|
|
2023-05-24 00:54:48 -07:00
|
|
|
|
def start_proof (start: Start): M (LSpec.TestSeq × Option ProofTree) := do
|
2023-05-23 05:12:46 -07:00
|
|
|
|
let env ← Lean.MonadEnv.getEnv
|
2023-05-22 14:49:56 -07:00
|
|
|
|
let mut testSeq := LSpec.TestSeq.done
|
|
|
|
|
match start with
|
|
|
|
|
| .copy name =>
|
|
|
|
|
let cInfo? := str_to_name name |> env.find?
|
|
|
|
|
testSeq := testSeq ++ LSpec.check s!"Symbol exists {name}" cInfo?.isSome
|
|
|
|
|
match cInfo? with
|
2023-05-23 05:12:46 -07:00
|
|
|
|
| .some cInfo =>
|
2023-05-24 00:54:48 -07:00
|
|
|
|
let state ← ProofTree.create
|
2023-05-23 05:12:46 -07:00
|
|
|
|
(name := str_to_name "TestExample")
|
|
|
|
|
(expr := cInfo.type)
|
2023-05-22 14:49:56 -07:00
|
|
|
|
return (testSeq, Option.some state)
|
|
|
|
|
| .none =>
|
|
|
|
|
return (testSeq, Option.none)
|
|
|
|
|
| .expr expr =>
|
2023-05-24 00:54:48 -07:00
|
|
|
|
let syn? := syntax_from_str env expr
|
2023-05-22 14:49:56 -07:00
|
|
|
|
testSeq := testSeq ++ LSpec.check s!"Parsing {expr}" (syn?.isOk)
|
|
|
|
|
match syn? with
|
|
|
|
|
| .error error =>
|
|
|
|
|
IO.println error
|
|
|
|
|
return (testSeq, Option.none)
|
|
|
|
|
| .ok syn =>
|
2023-05-24 00:54:48 -07:00
|
|
|
|
let expr? ← syntax_to_expr syn
|
2023-05-22 14:49:56 -07:00
|
|
|
|
testSeq := testSeq ++ LSpec.check s!"Elaborating" expr?.isOk
|
|
|
|
|
match expr? with
|
|
|
|
|
| .error error =>
|
|
|
|
|
IO.println error
|
|
|
|
|
return (testSeq, Option.none)
|
|
|
|
|
| .ok expr =>
|
2023-05-24 00:54:48 -07:00
|
|
|
|
let state ← ProofTree.create
|
2023-05-23 05:12:46 -07:00
|
|
|
|
(name := str_to_name "TestExample")
|
|
|
|
|
(expr := expr)
|
2023-05-22 14:49:56 -07:00
|
|
|
|
return (testSeq, Option.some state)
|
|
|
|
|
|
2023-05-27 23:10:39 -07:00
|
|
|
|
deriving instance DecidableEq, Repr for Variable
|
|
|
|
|
deriving instance DecidableEq, Repr for Goal
|
2023-05-24 00:54:48 -07:00
|
|
|
|
deriving instance DecidableEq, Repr for TacticResult
|
2023-05-22 14:49:56 -07:00
|
|
|
|
|
|
|
|
|
def proof_step (stateId: Nat) (goalId: Nat) (tactic: String)
|
2023-05-24 00:54:48 -07:00
|
|
|
|
(expected: TacticResult) : TestM LSpec.TestSeq := do
|
|
|
|
|
let result: TacticResult ← ProofTree.execute stateId goalId tactic
|
2023-05-22 14:49:56 -07:00
|
|
|
|
match expected, result with
|
|
|
|
|
| .success (.some i) #[], .success (.some _) goals =>
|
|
|
|
|
-- If the goals are omitted but the next state is specified, we imply that
|
|
|
|
|
-- the tactic succeeded.
|
|
|
|
|
let expected := .success (.some i) goals
|
|
|
|
|
return LSpec.test s!"{stateId}.{goalId} {tactic}" (result = expected)
|
|
|
|
|
| _, _ =>
|
|
|
|
|
return LSpec.test s!"{stateId}.{goalId} {tactic}" (result = expected)
|
2023-05-23 05:12:46 -07:00
|
|
|
|
|
|
|
|
|
def proof_inspect (expected: Array String) : TestM LSpec.TestSeq := do
|
2023-05-22 22:48:48 -07:00
|
|
|
|
let result := (← get).structure_array
|
2023-05-23 05:12:46 -07:00
|
|
|
|
return LSpec.test s!"tree structure" (result = expected)
|
|
|
|
|
|
|
|
|
|
def proof_runner (env: Lean.Environment) (start: Start) (steps: List (TestM LSpec.TestSeq)): IO LSpec.TestSeq := do
|
|
|
|
|
let termElabM := do
|
|
|
|
|
let (testSeq, state?) ← start_proof start
|
|
|
|
|
match state? with
|
|
|
|
|
| .none => return testSeq
|
|
|
|
|
| .some state => steps.foldlM (fun tests m => do pure $ tests ++ (← m)) testSeq |>.run' state
|
|
|
|
|
|
|
|
|
|
let coreContext: Lean.Core.Context := {
|
|
|
|
|
currNamespace := str_to_name "Aniva",
|
|
|
|
|
openDecls := [], -- No 'open' directives needed
|
|
|
|
|
fileName := "<Pantograph>",
|
|
|
|
|
fileMap := { source := "", positions := #[0], lines := #[1] }
|
|
|
|
|
}
|
|
|
|
|
let metaM := termElabM.run' (ctx := {
|
|
|
|
|
declName? := some "_pantograph",
|
|
|
|
|
errToSorry := false
|
|
|
|
|
})
|
|
|
|
|
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-05-22 14:49:56 -07:00
|
|
|
|
|
2023-05-27 23:10:39 -07:00
|
|
|
|
def build_goal (nameType: List (String × String)) (target: String): Goal :=
|
|
|
|
|
{
|
|
|
|
|
target := target,
|
|
|
|
|
vars := (nameType.map fun x => ({ name := x.fst, type := x.snd }: Variable)).toArray
|
|
|
|
|
}
|
2023-05-22 14:49:56 -07:00
|
|
|
|
|
|
|
|
|
example: ∀ (a b: Nat), a + b = b + a := by
|
|
|
|
|
intro n m
|
|
|
|
|
rw [Nat.add_comm]
|
2023-05-27 23:10:39 -07:00
|
|
|
|
def proof_nat_add_comm (env: Lean.Environment): IO LSpec.TestSeq := do
|
|
|
|
|
let goal1: Goal := {
|
|
|
|
|
target := "n + m = m + n",
|
|
|
|
|
vars := #[{ name := "n", type := "Nat" }, { name := "m", type := "Nat" }]
|
|
|
|
|
}
|
2023-05-23 05:12:46 -07:00
|
|
|
|
proof_runner env (.copy "Nat.add_comm") [
|
2023-05-22 14:49:56 -07:00
|
|
|
|
proof_step 0 0 "intro n m"
|
|
|
|
|
(.success (.some 1) #[goal1]),
|
|
|
|
|
proof_step 1 0 "assumption"
|
2023-05-27 23:10:39 -07:00
|
|
|
|
(.failure #[s!"tactic 'assumption' failed\nn m : Nat\n⊢ n + m = m + n"]),
|
2023-05-22 14:49:56 -07:00
|
|
|
|
proof_step 1 0 "rw [Nat.add_comm]"
|
|
|
|
|
(.success .none #[])
|
|
|
|
|
]
|
2023-05-23 05:12:46 -07:00
|
|
|
|
def proof_nat_add_comm_manual (env: Lean.Environment): IO LSpec.TestSeq := do
|
2023-05-27 23:10:39 -07:00
|
|
|
|
let goal1: Goal := build_goal [("n", "Nat"), ("m", "Nat")] "n + m = m + n"
|
2023-05-23 05:12:46 -07:00
|
|
|
|
proof_runner env (.expr "∀ (a b: Nat), a + b = b + a") [
|
2023-05-22 14:49:56 -07:00
|
|
|
|
proof_step 0 0 "intro n m"
|
|
|
|
|
(.success (.some 1) #[goal1]),
|
|
|
|
|
proof_step 1 0 "assumption"
|
2023-05-27 23:10:39 -07:00
|
|
|
|
(.failure #[s!"tactic 'assumption' failed\nn m : Nat\n⊢ n + m = m + n"]),
|
2023-05-22 14:49:56 -07:00
|
|
|
|
proof_step 1 0 "rw [Nat.add_comm]"
|
|
|
|
|
(.success .none #[])
|
|
|
|
|
]
|
|
|
|
|
|
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
|
2023-05-23 05:12:46 -07:00
|
|
|
|
def proof_or_comm (env: Lean.Environment): IO LSpec.TestSeq := do
|
2023-05-27 23:10:39 -07:00
|
|
|
|
let branchGoal (caseName name: String): Goal := {
|
|
|
|
|
caseName? := .some caseName,
|
|
|
|
|
target := "q ∨ p",
|
|
|
|
|
vars := #[
|
|
|
|
|
{ name := "p", type := "Prop" },
|
|
|
|
|
{ name := "q", type := "Prop" },
|
|
|
|
|
{ name := "h✝", type := name, isInaccessible := true }
|
|
|
|
|
]
|
|
|
|
|
}
|
2023-05-23 05:12:46 -07:00
|
|
|
|
proof_runner env (.expr "∀ (p q: Prop), p ∨ q → q ∨ p") [
|
2023-05-22 14:49:56 -07:00
|
|
|
|
proof_step 0 0 "intro p q h"
|
2023-05-27 23:10:39 -07:00
|
|
|
|
(.success (.some 1) #[build_goal [("p", "Prop"), ("q", "Prop"), ("h", "p ∨ q")] "q ∨ p"]),
|
2023-05-22 14:49:56 -07:00
|
|
|
|
proof_step 1 0 "cases h"
|
2023-05-27 23:10:39 -07:00
|
|
|
|
(.success (.some 2) #[branchGoal "inl" "p", branchGoal "inr" "q"]),
|
2023-05-22 22:48:48 -07:00
|
|
|
|
proof_inspect #["", "0.0", "1.0"],
|
2023-05-22 14:49:56 -07:00
|
|
|
|
proof_step 2 0 "apply Or.inr"
|
2023-05-23 05:12:46 -07:00
|
|
|
|
(.success (.some 3) #[]),
|
2023-05-22 22:48:48 -07:00
|
|
|
|
proof_inspect #["", "0.0", "1.0", "2.0"],
|
2023-05-22 14:49:56 -07:00
|
|
|
|
proof_step 3 0 "assumption"
|
|
|
|
|
(.success .none #[]),
|
|
|
|
|
proof_step 2 1 "apply Or.inl"
|
|
|
|
|
(.success (.some 4) #[]),
|
|
|
|
|
proof_step 4 0 "assumption"
|
2023-05-22 22:48:48 -07:00
|
|
|
|
(.success .none #[]),
|
2023-05-23 05:12:46 -07:00
|
|
|
|
proof_inspect #["", "0.0", "1.0", "2.0", "2.1"]
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
def proof_arith_1 (env: Lean.Environment): IO LSpec.TestSeq := do
|
|
|
|
|
proof_runner env (.expr "∀ (w x y z : Nat) (p : Nat → Prop) (h : p (x * y + z * w * x)), p (x * w * z + y * x)") [
|
|
|
|
|
proof_step 0 0 "intros"
|
|
|
|
|
(.success (.some 1) #[]),
|
|
|
|
|
proof_step 1 0 "simp [Nat.add_assoc, Nat.add_comm, Nat.add_left_comm, Nat.mul_comm, Nat.mul_assoc, Nat.mul_left_comm] at *"
|
|
|
|
|
(.success (.some 2) #[]),
|
|
|
|
|
proof_step 2 0 "assumption"
|
|
|
|
|
(.success .none #[])
|
2023-05-22 14:49:56 -07:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
def test_proofs : IO LSpec.TestSeq := do
|
2023-05-23 05:12:46 -07:00
|
|
|
|
let env: Lean.Environment ← Lean.importModules
|
|
|
|
|
(imports := ["Init"].map (λ str => { module := str_to_name str, runtimeOnly := false }))
|
|
|
|
|
(opts := {})
|
|
|
|
|
(trustLevel := 1)
|
|
|
|
|
|
2023-05-22 14:56:43 -07:00
|
|
|
|
return LSpec.group "Proofs" $
|
2023-05-23 05:12:46 -07:00
|
|
|
|
(LSpec.group "Nat.add_comm" $ (← proof_nat_add_comm env)) ++
|
|
|
|
|
(LSpec.group "Nat.add_comm manual" $ (← proof_nat_add_comm_manual env)) ++
|
|
|
|
|
(LSpec.group "Or.comm" $ (← proof_or_comm env)) ++
|
|
|
|
|
(LSpec.group "Arithmetic 1" $ (← proof_arith_1 env))
|
2023-05-22 14:49:56 -07:00
|
|
|
|
|
|
|
|
|
end Pantograph.Test
|
|
|
|
|
|