File size: 13,479 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import Pantograph.Goal
import Test.Common

open Lean

namespace Pantograph.Test.Tactic.Fragment

private def buildGoal (nameType: List (Name Γ— String)) (target: String):
    Protocol.Goal :=
  {
    target := { pp? := .some target},
    vars := (nameType.map fun x => ({
      userName := x.fst,
      type? := .some { pp? := .some x.snd },
    })).toArray
  }

abbrev TestM := TestT $ Elab.TermElabM

example : βˆ€ (a b c1 c2: Nat), (b + a) + c1 = (b + a) + c2 β†’ (a + b) + c1 = (b + a) + c2 := by
  intro a b c1 c2 h
  conv =>
    lhs
    congr
    . rw [Nat.add_comm]
    . rfl
  exact h

def test_conv_simple: TestM Unit := do
  let rootTarget ← parseSentence "βˆ€ (a b c1 c2: Nat), (b + a) + c1 = (b + a) + c2 β†’ (a + b) + c1 = (b + a) + c2"
  let state0 ← GoalState.create rootTarget

  let tactic := "intro a b c1 c2 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) =
    #[interiorGoal [] "a + b + c1 = b + a + c2"])

  let goalConv := state1.goals[0]!
  let state2 ← match ← state1.convEnter (state1.get! 0) with
    | .success state _ => pure state
    | other => do
      addTest $ assertUnreachable $ other.toString
      return ()
  addTest $ LSpec.check "conv => ..." ((← state2.serializeGoals).map (Β·.devolatilize) =
    #[{ interiorGoal [] "a + b + c1 = b + a + c2" with fragment := .conv }])

  let convTactic := "rhs"
  let state3R ← match ← state2.tacticOn (goalId := 0) convTactic with
    | .success state _ => pure state
    | other => do
      addTest $ assertUnreachable $ other.toString
      return ()
  addTest $ LSpec.check s!"  {convTactic} (discard)" ((← state3R.serializeGoals).map (Β·.devolatilize) =
    #[{ interiorGoal [] "b + a + c2" with fragment := .conv }])

  let convTactic := "lhs"
  let state3L ← match ← state2.tacticOn (goalId := 0) convTactic with
    | .success state _ => pure state
    | other => do
      addTest $ assertUnreachable $ other.toString
      return ()
  addTest $ LSpec.check s!"  {convTactic}" ((← state3L.serializeGoals).map (Β·.devolatilize) =
    #[{ interiorGoal [] "a + b + c1" with fragment := .conv }])

  let convTactic := "congr"
  let state4 ← match ← state3L.tacticOn (goalId := 0) convTactic with
    | .success state _ => pure state
    | other => do
      addTest $ assertUnreachable $ other.toString
      return ()
  addTest $ LSpec.check s!"  {convTactic}" ((← state4.serializeGoals).map (Β·.devolatilize) =
    #[
      { interiorGoal [] "a + b" with fragment := .conv, userName? := `a },
      { interiorGoal [] "c1" with fragment := .conv, userName? := `a }
    ])

  let convTactic := "rw [Nat.add_comm]"
  let state5_1 ← match ← state4.tacticOn (goalId := 0) convTactic with
    | .success state _ => pure state
    | other => do
      addTest $ assertUnreachable $ other.toString
      return ()
  addTest $ LSpec.check s!"  Β· {convTactic}" ((← state5_1.serializeGoals).map (Β·.devolatilize) =
    #[{ interiorGoal [] "b + a" with fragment := .conv, userName? := `a }])

  let convTactic := "rfl"
  let state6_1 ← match ← state5_1.tacticOn (goalId := 0) convTactic with
    | .success state _ => pure state
    | other => do
      addTest $ assertUnreachable $ other.toString
      return ()
  addTest $ LSpec.check s!"    {convTactic}" ((← state6_1.serializeGoals).map (Β·.devolatilize) =
    #[])

  let state4_1 ← match state6_1.continue state4 with
    | .ok state => pure state
    | .error e => do
      addTest $ expectationFailure "continue" e
      return ()

  let convTactic := "rfl"
  let state1_1 ← match ← state4_1.tacticOn (goalId := 0) convTactic with
    | .success state _ => pure state
    | other => do
      addTest $ assertUnreachable $ other.toString
      return ()
  addTest $ LSpec.check s!"  Β· {convTactic}" ((← state1_1.serializeGoals).map (Β·.devolatilize) =
    #[interiorGoal [] "b + a + c1 = b + a + c2"])
  checkEq "(fragments)" state1_1.fragments.size 0

  /-
  let state1_1 ← match ← state6.fragmentExit goalConv with
    | .success state _ => pure state
    | other => do
      addTest $ assertUnreachable $ other.toString
      return ()
  -/

  let tactic := "exact h"
  let stateF ← match ← state1_1.tacticOn (goalId := 0) (tactic := tactic) with
    | .success state _ => pure state
    | other => do
      addTest $ assertUnreachable $ other.toString
      return ()
  checkEq tactic ((← stateF.serializeGoals).map (Β·.devolatilize)) #[]
  checkEq "fragments" stateF.fragments.size 0

  where
    h := "b + a + c1 = b + a + c2"
    interiorGoal (free: List (Name Γ— String)) (target: String) :=
      let free := [(`a, "Nat"), (`b, "Nat"), (`c1, "Nat"), (`c2, "Nat"), (`h, h)] ++ free
      buildGoal free target

example (p : Prop) (x y z : Nat) : p β†’ (p β†’ x = y) β†’ x + z = y + z ∧ p := by
  intro hp hi
  apply And.intro
  conv =>
    rhs
    arg 1
    rw [←hi]
    rfl
    tactic => exact hp
  exact hp

def test_conv_unshielded : TestM Unit := do
  let rootTarget ← parseSentence "βˆ€ (p : Prop) (x y z : Nat), p β†’ (p β†’ x = y) β†’ x + z = y + z ∧ p"
  let state ← GoalState.create rootTarget
  let tactic := "intro p x y z hp hi"
  let .success state _ ← state.tacticOn 0 tactic | fail "intro failed"
  let tactic := "apply And.intro"
  let .success state _ ← state.tacticOn 0 tactic | fail "apply failed"
  let .success state _ ← state.convEnter (.prefer state.goals[0]!) | fail "Cannot enter conversion tactic mode"
  let .success state _ ← state.tryTactic .unfocus "rhs" | fail "rhs failed"
  let tactic := "arg 1"
  let .success state _ ← state.tryTactic .unfocus tactic | fail s!"{tactic} failed"
  checkEq s!"  {tactic}" ((← state.serializeGoals).map (Β·.devolatilize))
    #[
      { interiorGoal [] "y" with fragment := .conv },
      { interiorGoal [] "p" with userName? := `right, },
    ]
  let tactic := "rw [←hi]"
  let .success state _ ← state.tryTactic .unfocus tactic | fail s!"{tactic} failed"
  checkEq s!"  {tactic}" state.goals.length 3
  let tactic := "rfl"
  let .success state _ ← state.tryTactic .unfocus tactic | fail s!"{tactic} failed"
  checkEq s!"  {tactic}" ((← state.serializeGoals).map (Β·.devolatilize))
    #[
      interiorGoal [] "p",
      { interiorGoal [] "p" with userName? := `right, },
    ]
  checkEq "(n goals)" state.goals.length 2
  checkEq "(fragments)" state.fragments.size 0
  let tactic := "exact hp"
  let .success state _ ← state.tryTactic .unfocus tactic | fail s!"{tactic} failed"
  let tactic := "exact hp"
  let .success state _ ← state.tryTactic .unfocus tactic | fail s!"{tactic} failed"
  let root? := state.rootExpr?
  checkTrue "root" root?.isSome
  checkEq "fragments" state.fragments.size 0
  where
  interiorGoal (free: List (Name Γ— String)) (target: String) :=
    let free := [(`p, "Prop"), (`x, "Nat"), (`y, "Nat"), (`z, "Nat"), (`hp, "p"), (`hi, "p β†’ x = y")] ++ free
    buildGoal free target

example : βˆ€ (x y z w : Nat), y = z β†’ x + z = w β†’ x + y = w := by
  intro x y z w hyz hxzw
  conv =>
    lhs
    arg 2
    rw [hyz]
    rfl
  exact hxzw

def test_conv_unfinished : TestM Unit := do
  let rootTarget ← parseSentence "βˆ€ (x y z w : Nat), y = z β†’ x + z = w β†’ x + y = w"
  let state ← GoalState.create rootTarget
  let tactic := "intro x y z w hyz hxzw"
  let .success state _ ← state.tacticOn 0 tactic | fail "intro failed"
  let convParent := state.goals[0]!
  let .success state _ ← state.convEnter (.prefer convParent) | fail "Cannot enter conversion tactic mode"
  let .success state _ ← state.tryTactic .unfocus "lhs" | fail "rhs failed"
  let tactic := "arg 2"
  let .success state _ ← state.tryTactic .unfocus tactic | fail s!"{tactic} failed"
  checkEq s!"  {tactic}" ((← state.serializeGoals).map (Β·.devolatilize))
    #[
      { interiorGoal [] "y" with fragment := .conv },
    ]
  let tactic := "rw [hyz]"
  let .success state _ ← state.tryTactic .unfocus tactic | fail s!"{tactic} failed"
  checkEq s!"  {tactic}" ((← state.serializeGoals).map (Β·.devolatilize))
    #[
      { interiorGoal [] "z" with fragment := .conv },
    ]
  checkTrue "  (fragment)" $ state.fragments.contains state.mainGoal?.get!
  checkTrue "  (fragment parent)" $ state.fragments.contains convParent
  checkTrue "  (main goal)" state.mainGoal?.isSome
  let tactic := "rfl"
  let .success state _ ← state.tryTactic .unfocus tactic | fail s!"{tactic} failed"
  checkEq s!"  {tactic}" ((← state.serializeGoals).map (Β·.devolatilize))
    #[
      interiorGoal [] "x + z = w",
    ]
  checkEq "(fragments)" state.fragments.size 0
  checkEq s!"  {tactic}" state.goals.length 1
  let tactic := "exact hxzw"
  let .success state _ ← state.tryTactic .unfocus tactic | fail s!"{tactic} failed"
  let root? := state.rootExpr?
  checkTrue "root" root?.isSome
  where
  interiorGoal (free: List (Name Γ— String)) (target: String) :=
    let free := [(`x, "Nat"), (`y, "Nat"), (`z, "Nat"), (`w, "Nat"), (`hyz, "y = z"), (`hxzw, "x + z = w")] ++ free
    buildGoal free target

def test_conv_exit : TestM Unit := do
  let rootTarget ← parseSentence "βˆ€ (a b : Nat), b = 2 β†’ 1 + a + 1 = a + b"
  let state ← GoalState.create rootTarget
  let tactic := "intro a b h"
  let .success state _ ← state.tacticOn 0 tactic | fail "intro failed"
  let .success state _ ← state.calcEnter (.prefer state.goals[0]!) | fail "Cannot enter conversion tactic mode"
  let .success state _ ← state.tryTactic .unfocus "1 + a + 1 = a + 1 + 1" | fail "rhs failed"
  let .success state _ ← state.convEnter (.prefer state.goals[0]!) | fail "Cannot enter conversion tactic mode"
  let .success state _ ← state.tryTactic .unfocus "rhs" | fail "rhs failed"
  let .success state _ ← state.tryTactic .unfocus "rw [Nat.add_comm]" | fail "rhs failed"
  let .success state _ ← state.fragmentExit .unfocus | fail "exit failed"
  checkEq "(parents)" state.parentExprs.length 1
  where
  interiorGoal (free: List (Name Γ— String)) (target: String) :=
    let free := [(`a, "Nat"), (`b, "Nat"), (`h, "b = 2")] ++ free
    buildGoal free target

example : βˆ€ (a b c d: Nat), a + b = b + c β†’ b + c = c + d β†’ a + b = c + d := by
  intro a b c d h1 h2
  calc a + b = b + c := by apply h1
    _ = c + d := by apply h2

def test_calc: TestM Unit := do
  let rootTarget ← parseSentence "βˆ€ (a b c d: Nat), a + b = b + c β†’ b + c = c + d β†’ a + b = c + d"
  let state0 ← GoalState.create rootTarget
  let tactic := "intro a b c d h1 h2"
  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))
    #[interiorGoal [] "a + b = c + d"]
  let pred := "a + b = b + c"
  let .success state1 _ ← state1.calcEnter state1.mainGoal?.get! | fail "Could not enter calc"
  let state2 ← match ← state1.tacticOn 0 pred with
    | .success state _ => pure state
    | other => do
      addTest $ assertUnreachable $ other.toString
      return ()
  checkEq s!"calc {pred} := _" ((← state2.serializeGoals).map (Β·.devolatilize))
    #[
      { interiorGoal [] "a + b = b + c" with userName? := `calc },
      { interiorGoal [] "b + c = c + d" with fragment := .calc },
    ]
  checkFalse "(2.0 prev rhs)" <| state2.fragments.contains (state2.get! 0)
  checkTrue "(2.1 prev rhs)" <| state2.fragments[(state2.get! 1)]? matches .some (.calc ..)

  let tactic := "apply h1"
  let state2m ← match ← state2.tacticOn (goalId := 0) (tactic := tactic) with
    | .success state _ => pure state
    | other => do
      addTest $ assertUnreachable $ other.toString
      return ()
  let state3 ← match state2m.continue state2 with
    | .ok state => pure state
    | .error e => do
      addTest $ expectationFailure "continue" e
      return ()
  let pred := "_ = c + d"
  let state4 ← match ← state3.tacticOn 0 pred with
    | .success state _ => pure state
    | other => do
      addTest $ assertUnreachable $ other.toString
      return ()
  checkEq s!"calc {pred} := _" ((← state4.serializeGoals).map (Β·.devolatilize))
    #[
      { interiorGoal [] "b + c = c + d" with userName? := `calc },
    ]
  checkFalse "(4.0 prev rhs)" <| state4.fragments.contains (state4.get! 0)
  let tactic := "apply h2"
  let state4m ← match ← state4.tacticOn (goalId := 0) (tactic := tactic) with
    | .success state _ => pure state
    | other => do
      addTest $ assertUnreachable $ other.toString
      return ()
  checkEq "(fragments)" state4m.fragments.size 0
  addTest $ LSpec.test "(4m root)" state4m.rootExpr?.isSome
  where
  interiorGoal (free: List (Name Γ— String)) (target: String) :=
    let free := [(`a, "Nat"), (`b, "Nat"), (`c, "Nat"), (`d, "Nat"),
      (`h1, "a + b = b + c"), (`h2, "b + c = c + d")] ++ free
    buildGoal free target

def suite (env: Environment): List (String Γ— IO LSpec.TestSeq) :=
  [
    ("conv simple", test_conv_simple),
    ("conv unshielded", test_conv_unshielded),
    ("conv unfinished", test_conv_unfinished),
    ("conv exit", test_conv_exit),
    ("calc", test_calc),
  ] |>.map (Ξ» (name, t) => (name, runTestTermElabM env t))

end Pantograph.Test.Tactic.Fragment