File size: 11,512 Bytes
0115dcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import Pantograph.Tactic.Prograde
import Test.Common

open Lean

namespace Pantograph.Test.Tactic.Prograde

def test_define : TestT Elab.TermElabM Unit := do
  let expr := "forall (p q : Prop) (h: p), And (Or p q) (Or p q)"
  let expr ← parseSentence expr
  Meta.forallTelescope expr $ Ξ» _ body => do
    let e ← match Parser.runParserCategory
      (env := ← MonadEnv.getEnv)
      (catName := `term)
      (input := "Or.inl h")
      (fileName := ← getFileName) with
      | .ok syn => pure syn
      | .error error => throwError "Failed to parse: {error}"
    -- Apply the tactic
    let goal ← Meta.mkFreshExprSyntheticOpaqueMVar body
    let target: Expr := mkAnd
      (mkOr (.fvar ⟨uniq 8⟩) (.fvar ⟨uniq 9⟩))
      (mkOr (.fvar ⟨uniq 8⟩) (.fvar ⟨uniq 9⟩))
    let h := .fvar ⟨uniq 8⟩
    addTest $ LSpec.test "goals before" ((← toCondensedGoal goal.mvarId!).devolatilize == {
      context := #[
        cdeclOf `p (.sort 0),
        cdeclOf `q (.sort 0),
        cdeclOf `h h
      ],
      target,
    })
    let tactic := Tactic.evalDefine `h2 e
    let m := .mvar ⟨uniq 13⟩
    let [newGoal] ← runTacticOnMVar tactic goal.mvarId! | panic! "Incorrect goal number"
    addTest $ LSpec.test "goals after" ((← toCondensedGoal newGoal).devolatilize == {
      context := #[
        cdeclOf `p (.sort 0),
        cdeclOf `q (.sort 0),
        cdeclOf `h h,
        {
          userName := `h2,
          type := mkOr h m,
          value? := .some $ mkApp3 (mkConst `Or.inl) h m (.fvar ⟨uniq 10⟩)
        }
      ],
      target,
    })
    let .some e ← getExprMVarAssignment? goal.mvarId! | panic! "Tactic must assign"
    addTest $ LSpec.test "assign" e.isLet

def test_define_proof : TestT Elab.TermElabM Unit := do
  let rootExpr ← parseSentence "βˆ€ (p q: Prop), p β†’ ((p ∨ q) ∨ (p ∨ q))"
  let state0 ← GoalState.create rootExpr
  let tactic := "intro p q h"
  let state1 ← match ← state0.tacticOn (goalId := 0) (tactic := tactic) with
    | .success state _ => pure state
    | other => do
      addTest $ assertUnreachable $ other.toString
      return ()
  checkEq tactic ((← state1.serializeGoals).map (Β·.devolatilize))
    #[buildGoal [(`p, "Prop"), (`q, "Prop"), (`h, "p")] "(p ∨ q) ∨ p ∨ q"]

  let expr := "Or.inl (Or.inl h)"
  let state2 ← match ← state1.tryAssign (state1.get! 0) (expr := expr) with
    | .success state _ => pure state
    | other => do
      addTest $ assertUnreachable $ other.toString
      return ()
  addTest $ LSpec.check s!":= {expr}" ((← state2.serializeGoals).map (Β·.devolatilize) =
    #[])

  let evalBind := `y
  let evalExpr := "Or.inl h"
  let state2 ← match ← state1.tryDefine (state1.get! 0) (binderName := evalBind) (expr := evalExpr) with
    | .success state _ => pure state
    | other => do
      addTest $ assertUnreachable $ other.toString
      return ()
  checkEq s!"eval {evalBind} := {evalExpr}" ((← state2.serializeGoals).map (Β·.devolatilize))
    #[{
      target := { pp? := .some  "(p ∨ q) ∨ p ∨ q"},
      vars := #[
        { userName := `p, type? := .some { pp? := .some "Prop" } },
        { userName := `q, type? := .some { pp? := .some "Prop" } },
        { userName := `h, type? := .some { pp? := .some "p" } },
        { userName := `y,
          type? := .some { pp? := .some "p ∨ ?m.9" },
          value? := .some { pp? := .some "Or.inl h" },
        }
      ]
    }]

  let expr := "Or.inl y"
  let state3 ← match ← state2.tryAssign (state2.get! 0) (expr := expr) with
    | .success state _ => pure state
    | other => do
      addTest $ assertUnreachable $ other.toString
      return ()
  addTest $ LSpec.check s!":= {expr}" ((← state3.serializeGoals).map (Β·.devolatilize) =
    #[])

  addTest $ LSpec.check "(3 root)" state3.rootExpr?.isSome

def fun_define_root_expr: βˆ€ (p: Prop), PProd (Nat β†’ p) Unit β†’ p := by
  intro p x
  apply x.fst
  exact 5

def test_define_root_expr : TestT Elab.TermElabM Unit := do
  --let rootExpr ← parseSentence "Nat"
  --let state0 ← GoalState.create rootExpr
  --let .success state1 _ ← state0.tacticOn (goalId := 0) "exact 5" | addTest $ assertUnreachable "exact 5"
  --let .some rootExpr := state1.rootExpr? | addTest $ assertUnreachable "Root expr"
  --addTest $ LSpec.check "root" ((toString $ ← Meta.ppExpr rootExpr) = "5")
  let rootExpr ← parseSentence "βˆ€ (p: Prop), PProd (Nat β†’ p) Unit β†’ p"
  let state0 ← GoalState.create rootExpr
  let tactic := "intro p x"
  let .success state1 _ ← state0.tacticOn (goalId := 0) tactic | addTest $ assertUnreachable tactic
  let binderName := `binder
  let value := "x.fst"
  let expr ← state1.goals[0]!.withContext $ strToTermSyntax value
  let tacticM := Tactic.evalDefine binderName expr
  let .success state2 _ ← state1.tryTacticM (state1.get! 0) tacticM | addTest $ assertUnreachable s!"define {binderName} := {value}"
  let tactic := s!"apply {binderName}"
  let .success state3 _ ← state2.tacticOn (goalId := 0) tactic | addTest $ assertUnreachable tactic
  let tactic := s!"exact 5"
  let .success state4 _ ← state3.tacticOn (goalId := 0) tactic | addTest $ assertUnreachable tactic
  let .some rootExpr := state4.rootExpr? | addTest $ assertUnreachable "Root expr"
  addTest $ LSpec.check "root" ((toString $ ← Meta.ppExpr rootExpr) = "fun p x =>\n  let binder := x.fst;\n  binder 5")

def test_have_proof : TestT Elab.TermElabM Unit := do
  let rootExpr ← parseSentence "βˆ€ (p q: Prop), p β†’ ((p ∨ q) ∨ (p ∨ q))"
  let state0 ← GoalState.create rootExpr
  let tactic := "intro p q h"
  let state1 ← match ← state0.tacticOn (goalId := 0) (tactic := tactic) with
    | .success state _ => pure state
    | other => do
      addTest $ assertUnreachable $ other.toString
      return ()
  checkEq tactic ((← state1.serializeGoals).map (Β·.devolatilize))
    #[buildGoal [(`p, "Prop"), (`q, "Prop"), (`h, "p")] "(p ∨ q) ∨ p ∨ q"]

  let expr := "Or.inl (Or.inl h)"
  let state2 ← match ← state1.tryAssign (state1.get! 0) (expr := expr) with
    | .success state _ => pure state
    | other => do
      addTest $ assertUnreachable $ other.toString
      return ()
  addTest $ LSpec.check s!":= {expr}" ((← state2.serializeGoals).map (Β·.devolatilize) =
    #[])

  let haveBind := `y
  let haveType := "p ∨ q"
  let state2 ← match ← state1.tryHave (state1.get! 0) (binderName := haveBind) (type := haveType) with
    | .success state _ => pure state
    | other => do
      addTest $ assertUnreachable $ other.toString
      return ()
  checkEq s!"have {haveBind}: {haveType}" ((← state2.serializeGoals).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"
    ]

  let expr := "Or.inl h"
  let state3 ← match ← state2.tryAssign (state2.get! 0) (expr := expr) with
    | .success state _ => pure state
    | other => do
      addTest $ assertUnreachable $ other.toString
      return ()
  checkEq s!":= {expr}" ((← state3.serializeGoals).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 (state2b.get! 0) (expr := expr) with
    | .success state _ => pure state
    | other => do
      addTest $ assertUnreachable $ other.toString
      return ()
  checkEq s!":= {expr}" ((← state4.serializeGoals).map (Β·.devolatilize)) #[]

  let .some rootExpr := state4.rootExpr? | fail "Root expr"
  checkEq "root" (toString $ ← Meta.ppExpr rootExpr) "fun p q h y => Or.inl y"

def test_let (specialized: Bool): TestT Elab.TermElabM Unit := do
  let rootExpr ← parseSentence "βˆ€ (p q: Prop), p β†’ ((p ∨ q) ∨ (p ∨ q))"
  let state0 ← GoalState.create rootExpr
  let tactic := "intro a p h"
  let state1 ← match ← state0.tacticOn (goalId := 0) (tactic := tactic) with
    | .success state _ => pure state
    | other => do
      addTest $ assertUnreachable $ other.toString
      return ()
  addTest $ LSpec.check tactic ((← state1.serializeGoals).map (Β·.devolatilize) =
    #[{
      target := { pp? := .some mainTarget },
      vars := interiorVars,
    }])

  let letType := "Nat"
  let expr := s!"let b: {letType} := _; _"
  let result2 ← match specialized with
    | true => state1.tryLet (state1.get! 0) (binderName := `b) (type := letType)
    | false => state1.tryAssign (state1.get! 0) (expr := expr)
  let state2 ← match result2 with
    | .success state _ => pure state
    | other => do
      addTest $ assertUnreachable $ other.toString
      return ()
  let serializedState2 ← state2.serializeGoals
  let letBindName := if specialized then `b else `_1
  checkEq expr (serializedState2.map (Β·.devolatilize))
    #[{
      target := { pp? := .some letType },
      vars := interiorVars,
      userName? := .some letBindName
    },
    {
      target := { pp? := .some mainTarget },
      vars := interiorVars ++ #[{
        userName := `b,
        type? := .some { pp? := .some letType },
        value? := .some { pp? := .some s!"?{letBindName}" },
      }],
      userName? := if specialized then .none else .some `_2,
    }]

  let tactic := "exact 1"
  let state3 ← match ← state2.tacticOn (goalId := 0) (tactic := tactic) with
    | .success state _ => pure state
    | other => do
      addTest $ assertUnreachable $ other.toString
      return ()
  addTest $ LSpec.check tactic ((← state3.serializeGoals).map (Β·.devolatilize) = #[])

  let state3r ← match state3.continue state2 with
    | .error msg => do
      addTest $ assertUnreachable $ msg
      return ()
    | .ok state => pure state
  checkEq "(continue)" ((← state3r.serializeGoals).map (Β·.devolatilize))
    #[{
      target := { pp? := .some mainTarget },
      vars := interiorVars ++ #[{
        userName := `b,
        type? := .some { pp? := .some "Nat" },
        value? := .some { pp? := .some "1" },
      }],
      userName? := if specialized then .none else `_2,
    }]

  let tactic := "exact h"
  match ← state3r.tacticOn (goalId := 0) (tactic := tactic) with
  | .failure #[message] =>
    checkEq tactic
      (← message.toString)
      s!"{← getFileName}:0:0: error: Type mismatch\n  h\nhas type\n  a\nbut is expected to have type\n  {mainTarget}\n"
  | other => do
    fail s!"Should be a failure: {other.toString}"

  let tactic := "exact Or.inl (Or.inl h)"
  let state4 ← match ← state3r.tacticOn (goalId := 0) (tactic := tactic) with
    | .success state _ => pure state
    | other => do
      addTest $ assertUnreachable $ other.toString
      return ()
  addTest $ LSpec.test "(4 root)" state4.rootExpr?.isSome
  where
  mainTarget := "(a ∨ p) ∨ a ∨ p"
  interiorVars: Array Protocol.Variable := #[
      { userName := `a, type? := .some { pp? := .some "Prop" }, },
      { userName := `p, type? := .some { pp? := .some "Prop" }, },
      { userName := `h, type? := .some { pp? := .some "a" }, }
    ]

def suite (env: Environment): List (String Γ— IO LSpec.TestSeq) :=
  [
    ("define", test_define),
    ("define proof", test_define_proof),
    ("define root expr", test_define_root_expr),
    ("have proof", test_have_proof),
    ("let via assign", test_let false),
    ("let via tryLet", test_let true),
  ] |>.map (Ξ» (name, t) => (name, runTestTermElabM env t))