forked from leanprover-community/mathlib3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhere.lean
More file actions
279 lines (193 loc) · 8.2 KB
/
Copy pathwhere.lean
File metadata and controls
279 lines (193 loc) · 8.2 KB
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
import data.multiset
import tactic.where
-- First set up the testing framework...
section framework
-- Note that we cannot have any explicit `open`s, we are currently working around this bug:
-- https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/What's.20the.20deal.20with.20.60open.60
@[user_command]
meta def run_parser_from_command_cmd
(_ : interactive.parse $ lean.parser.tk "run_parser_from_command")
: lean.parser unit :=
do ns ← lean.parser.ident,
let ns := if ns = `NONE then name.anonymous else ns,
n ← lean.parser.ident,
prog ← lean.parser.of_tactic $ tactic.mk_const (ns ++ n) >>= tactic.eval_expr (lean.parser unit),
if ns = name.anonymous then tactic.skip else lean.parser.emit_code_here $
"namespace " ++ ns.to_string,
prog,
if ns = name.anonymous then tactic.skip else lean.parser.emit_code_here $
"end " ++ ns.to_string
meta def remove_dot_aux : list char → list char
| [] := []
| (c :: rest) := (if c = '.' then '_' else c) :: remove_dot_aux rest
meta def remove_dot (s : string) : string :=
(remove_dot_aux s.data).as_string
-- NOTE We must emit fully qualified names below in order to not influence the real tests!
@[user_command]
meta def run_parser_from_tactic_cmd
(_ : interactive.parse $ lean.parser.tk "run_parser_from_tactic")
: lean.parser unit :=
do ns ← lean.parser.ident,
let ns := if ns = `NONE then name.anonymous else ns,
n ← lean.parser.ident,
let tac_name := "try_test_" ++ (remove_dot (ns ++ n).to_string),
lean.parser.emit_code_here $
"meta def tactic.interactive." ++ tac_name ++ " (_ : interactive.parse " ++ (ns ++ n).to_string ++ ")"
++ ": tactic unit := tactic.triv",
if ns = name.anonymous then tactic.skip else lean.parser.emit_code_here $
"namespace " ++ ns.to_string,
lean.parser.emit_code_here $
"example : true := by " ++ (name.mk_string tac_name name.anonymous).to_string,
if ns = name.anonymous then tactic.skip else lean.parser.emit_code_here $
"end " ++ ns.to_string
meta def assert_name_eq (n₁ n₂ : name) : lean.parser unit :=
if n₁ = n₂ then return () else tactic.fail sformat!"violation: '{n₁}' ≠ '{n₂}'!"
meta def assert_list_noorder_eq {α : Type} [decidable_eq α] [has_to_string α]
(l₁ l₂ : list α) : lean.parser unit :=
if (l₁ : multiset α) = (l₂ : multiset α) then return () else tactic.fail sformat!"violation: '{l₁}' ≠ '{l₂}'!"
meta def assert_where_msg_eq (s : string) : lean.parser unit :=
do tw ← where.build_msg,
if s = tw then return () else tactic.fail sformat!"violation:\n'\n{tw}\n'\n\n ≠\n\n'\n{s}\n'!"
-- Test the test framework...
meta def dummy : lean.parser unit := return ()
run_parser_from_command NONE dummy
run_parser_from_tactic NONE dummy
end framework
-- TESTS START
-- TEST: `#where` output
-- NOTE: This section must come first because of the `open_namespaces` bug referenced above.
-- NOTE: All other sections have correct answers, but the order of variables (say) may be safely
-- reordered here: changes to `#where` which break this set of tests are possible, without an
-- error.
meta def test_output_1 : lean.parser unit := assert_where_msg_eq "namespace [root namespace]\n\n\n\n\nend [root namespace]\n"
meta def test_output_2 : lean.parser unit := assert_where_msg_eq "namespace [root namespace]\n\nopen list nat\n\n\n\nend [root namespace]\n"
meta def test_output_3 : lean.parser unit := assert_where_msg_eq "namespace [root namespace]\n\nopen list nat\nvariables {c : ℕ → list ℕ} (b a : ℕ) [decidable_eq : ℕ]\n\n\n\nend [root namespace]\n"
meta def test_output_4 : lean.parser unit := assert_where_msg_eq "namespace [root namespace]\n\nopen list nat\nvariables {c : ℕ → list ℕ} (b a : ℕ) [decidable_eq : ℕ]\ninclude c a\n\n\n\nend [root namespace]\n"
meta def test_output_5 : lean.parser unit := assert_where_msg_eq "namespace a\n\nopen list nat\nvariables {c : ℕ → list ℕ} (b a : ℕ) [decidable_eq : ℕ]\ninclude c a\n\n\n\nend a\n"
meta def test_output_6 : lean.parser unit := assert_where_msg_eq "namespace a.b\n\nopen a list nat\nvariables {c : ℕ → list ℕ} (b a : ℕ) [decidable_eq : ℕ]\ninclude c a\n\n\n\nend a.b\n"
section b1
-- #where
run_parser_from_command NONE test_output_1
open nat list
-- #where
run_parser_from_command NONE test_output_2
variables (a b : ℕ) [decidable_eq : ℕ] {c : ℕ → list ℕ}
-- #where
run_parser_from_command NONE test_output_3
include a c
-- #where
run_parser_from_command NONE test_output_4
end b1
namespace a
variables (a b : ℕ) [decidable_eq : ℕ] {c : ℕ → list ℕ}
include a c
open nat list
-- #where
run_parser_from_command NONE test_output_5
namespace b
-- #where
run_parser_from_command NONE test_output_6
end b
end a
-- TEST: `lean.parser.get_current_namespace`
-- Check no namespace
meta def test_no_namespace : lean.parser unit :=
do ns ← lean.parser.get_current_namespace,
assert_name_eq ns name.anonymous,
return ()
run_parser_from_command NONE test_no_namespace
run_parser_from_tactic NONE test_no_namespace
section a1
open nat list
-- Check no namespace with opens
meta def test_no_namespace_w_opens : lean.parser unit :=
do ns ← lean.parser.get_current_namespace,
assert_name_eq ns name.anonymous,
return ()
run_parser_from_command NONE test_no_namespace_w_opens
run_parser_from_tactic NONE test_no_namespace_w_opens
end a1
namespace test1
-- Check a namespace
meta def test_1 : lean.parser unit :=
do ns ← lean.parser.get_current_namespace,
assert_name_eq ns `test1,
return ()
open nat list
-- Check a 2 namespaces with opens
meta def test_2 : lean.parser unit :=
do ns ← lean.parser.get_current_namespace,
assert_name_eq ns `test1,
return ()
end test1
run_parser_from_command test1 test_1
run_parser_from_command test1 test_2
run_parser_from_tactic test1 test_1
run_parser_from_tactic test1 test_2
namespace test1.test2
-- Check a 2 namespaces
meta def test_1 : lean.parser unit :=
do ns ← lean.parser.get_current_namespace,
assert_name_eq ns `test1.test2,
return ()
open nat list
-- Check a 2 namespaces with opens
meta def test_2 : lean.parser unit :=
do ns ← lean.parser.get_current_namespace,
assert_name_eq ns `test1.test2,
return ()
end test1.test2
run_parser_from_command test1.test2 test_1
run_parser_from_command test1.test2 test_2
run_parser_from_tactic test1.test2 test_1
run_parser_from_tactic test1.test2 test_2
-- TEST: `lean.parser.get_variables` and `lean.parser.get_included_variables`
-- Check no variables
meta def test_no_variables : lean.parser unit :=
do ns ← lean.parser.get_variables,
assert_list_noorder_eq (ns.map prod.fst) [],
return ()
run_parser_from_command NONE test_no_variables
run_parser_from_tactic NONE test_no_variables
section a1
variables (a : ℕ)
-- Check 1 variable from command
meta def test_1_variable_from_command : lean.parser unit :=
do ns ← lean.parser.get_variables,
assert_list_noorder_eq (ns.map prod.fst) [`a],
return ()
run_parser_from_command NONE test_1_variable_from_command
-- Check 1 variable from tactic
meta def test_1_variable_from_tactic : lean.parser unit :=
do ns ← lean.parser.get_variables,
assert_list_noorder_eq (ns.map prod.fst) [],
return ()
run_parser_from_tactic NONE test_1_variable_from_tactic
end a1
namespace a2
variables (a : ℕ)
-- Check 1 variable from command inside namespace
meta def test_1_variable_from_command : lean.parser unit :=
do ns ← lean.parser.get_variables,
assert_list_noorder_eq (ns.map prod.fst) [`a],
return ()
run_parser_from_command NONE test_1_variable_from_command
-- Check 1 variable from tactic inside namespace
meta def test_1_variable_from_tactic : lean.parser unit :=
do ns ← lean.parser.get_variables,
assert_list_noorder_eq (ns.map prod.fst) [],
return ()
run_parser_from_tactic NONE test_1_variable_from_tactic
end a2
section a3
-- Check 3 variables with 1 include
meta def test_2_variable_from_command : lean.parser unit :=
do ns ← lean.parser.get_variables,
assert_list_noorder_eq (ns.map prod.fst) [`a, `b, `c],
ns ← lean.parser.get_included_variables,
assert_list_noorder_eq (ns.map prod.fst) [`b],
return ()
variables (a b c : ℕ)
include b
run_parser_from_command NONE test_2_variable_from_command
end a3