author | wenzelm |
Mon, 20 Dec 2010 13:24:04 +0100 | |
changeset 41305 | 42967939ea81 |
parent 41272 | b806a7678083 |
child 41435 | 12585dfb86fe |
permissions | -rw-r--r-- |
37134 | 1 |
(* Title: FOL/ex/Locale_Test/Locale_Test1.thy |
2 |
Author: Clemens Ballarin, TU Muenchen |
|
3 |
||
4 |
Test environment for the locale implementation. |
|
5 |
*) |
|
6 |
||
7 |
theory Locale_Test1 |
|
8 |
imports FOL |
|
9 |
begin |
|
10 |
||
11 |
typedecl int arities int :: "term" |
|
12 |
consts plus :: "int => int => int" (infixl "+" 60) |
|
13 |
zero :: int ("0") |
|
14 |
minus :: "int => int" ("- _") |
|
15 |
||
16 |
axioms |
|
17 |
int_assoc: "(x + y::int) + z = x + (y + z)" |
|
18 |
int_zero: "0 + x = x" |
|
19 |
int_minus: "(-x) + x = 0" |
|
20 |
int_minus2: "-(-x) = x" |
|
21 |
||
22 |
section {* Inference of parameter types *} |
|
23 |
||
24 |
locale param1 = fixes p |
|
25 |
print_locale! param1 |
|
26 |
||
27 |
locale param2 = fixes p :: 'b |
|
28 |
print_locale! param2 |
|
29 |
||
30 |
(* |
|
31 |
locale param_top = param2 r for r :: "'b :: {}" |
|
32 |
Fails, cannot generalise parameter. |
|
33 |
*) |
|
34 |
||
35 |
locale param3 = fixes p (infix ".." 50) |
|
36 |
print_locale! param3 |
|
37 |
||
38 |
locale param4 = fixes p :: "'a => 'a => 'a" (infix ".." 50) |
|
39 |
print_locale! param4 |
|
40 |
||
41 |
||
42 |
subsection {* Incremental type constraints *} |
|
43 |
||
44 |
locale constraint1 = |
|
45 |
fixes prod (infixl "**" 65) |
|
46 |
assumes l_id: "x ** y = x" |
|
47 |
assumes assoc: "(x ** y) ** z = x ** (y ** z)" |
|
48 |
print_locale! constraint1 |
|
49 |
||
50 |
locale constraint2 = |
|
51 |
fixes p and q |
|
52 |
assumes "p = q" |
|
53 |
print_locale! constraint2 |
|
54 |
||
55 |
||
56 |
section {* Inheritance *} |
|
57 |
||
58 |
locale semi = |
|
59 |
fixes prod (infixl "**" 65) |
|
60 |
assumes assoc: "(x ** y) ** z = x ** (y ** z)" |
|
61 |
print_locale! semi thm semi_def |
|
62 |
||
63 |
locale lgrp = semi + |
|
64 |
fixes one and inv |
|
65 |
assumes lone: "one ** x = x" |
|
66 |
and linv: "inv(x) ** x = one" |
|
67 |
print_locale! lgrp thm lgrp_def lgrp_axioms_def |
|
68 |
||
69 |
locale add_lgrp = semi "op ++" for sum (infixl "++" 60) + |
|
70 |
fixes zero and neg |
|
71 |
assumes lzero: "zero ++ x = x" |
|
72 |
and lneg: "neg(x) ++ x = zero" |
|
73 |
print_locale! add_lgrp thm add_lgrp_def add_lgrp_axioms_def |
|
74 |
||
75 |
locale rev_lgrp = semi "%x y. y ++ x" for sum (infixl "++" 60) |
|
76 |
print_locale! rev_lgrp thm rev_lgrp_def |
|
77 |
||
78 |
locale hom = f: semi f + g: semi g for f and g |
|
79 |
print_locale! hom thm hom_def |
|
80 |
||
81 |
locale perturbation = semi + d: semi "%x y. delta(x) ** delta(y)" for delta |
|
82 |
print_locale! perturbation thm perturbation_def |
|
83 |
||
84 |
locale pert_hom = d1: perturbation f d1 + d2: perturbation f d2 for f d1 d2 |
|
85 |
print_locale! pert_hom thm pert_hom_def |
|
86 |
||
87 |
text {* Alternative expression, obtaining nicer names in @{text "semi f"}. *} |
|
88 |
locale pert_hom' = semi f + d1: perturbation f d1 + d2: perturbation f d2 for f d1 d2 |
|
89 |
print_locale! pert_hom' thm pert_hom'_def |
|
90 |
||
91 |
||
92 |
section {* Syntax declarations *} |
|
93 |
||
94 |
locale logic = |
|
95 |
fixes land (infixl "&&" 55) |
|
96 |
and lnot ("-- _" [60] 60) |
|
97 |
assumes assoc: "(x && y) && z = x && (y && z)" |
|
98 |
and notnot: "-- (-- x) = x" |
|
99 |
begin |
|
100 |
||
101 |
definition lor (infixl "||" 50) where |
|
102 |
"x || y = --(-- x && -- y)" |
|
103 |
||
104 |
end |
|
105 |
print_locale! logic |
|
106 |
||
107 |
locale use_decl = logic + semi "op ||" |
|
108 |
print_locale! use_decl thm use_decl_def |
|
109 |
||
110 |
locale extra_type = |
|
111 |
fixes a :: 'a |
|
112 |
and P :: "'a => 'b => o" |
|
113 |
begin |
|
114 |
||
115 |
definition test :: "'a => o" where |
|
116 |
"test(x) <-> (ALL b. P(x, b))" |
|
117 |
||
118 |
end |
|
119 |
||
120 |
term extra_type.test thm extra_type.test_def |
|
121 |
||
122 |
interpretation var?: extra_type "0" "%x y. x = 0" . |
|
123 |
||
124 |
thm var.test_def |
|
125 |
||
126 |
||
127 |
text {* Under which circumstances term syntax remains active. *} |
|
128 |
||
129 |
locale "syntax" = |
|
130 |
fixes p1 :: "'a => 'b" |
|
131 |
and p2 :: "'b => o" |
|
132 |
begin |
|
133 |
||
134 |
definition d1 :: "'a => o" where "d1(x) <-> ~ p2(p1(x))" |
|
135 |
definition d2 :: "'b => o" where "d2(x) <-> ~ p2(x)" |
|
136 |
||
137 |
thm d1_def d2_def |
|
138 |
||
139 |
end |
|
140 |
||
141 |
thm syntax.d1_def syntax.d2_def |
|
142 |
||
143 |
locale syntax' = "syntax" p1 p2 for p1 :: "'a => 'a" and p2 :: "'a => o" |
|
144 |
begin |
|
145 |
||
146 |
thm d1_def d2_def (* should print as "d1(?x) <-> ..." and "d2(?x) <-> ..." *) |
|
147 |
||
148 |
ML {* |
|
149 |
fun check_syntax ctxt thm expected = |
|
150 |
let |
|
37146
f652333bbf8e
renamed structure PrintMode to Print_Mode, keeping the old name as legacy alias for some time;
wenzelm
parents:
37134
diff
changeset
|
151 |
val obtained = Print_Mode.setmp [] (Display.string_of_thm ctxt) thm; |
37134 | 152 |
in |
153 |
if obtained <> expected |
|
154 |
then error ("Theorem syntax '" ^ obtained ^ "' obtained, but '" ^ expected ^ "' expected.") |
|
155 |
else () |
|
156 |
end; |
|
157 |
*} |
|
158 |
||
41305
42967939ea81
actually enable show_hyps option, unlike local_setup in 6da953d30f48 which merely affects the (temporary) auxiliary context;
wenzelm
parents:
41272
diff
changeset
|
159 |
declare [[show_hyps]] |
41271
6da953d30f48
Enable show_hyps, which appears to be set in batch mode but in an interactive session.
ballarin
parents:
39557
diff
changeset
|
160 |
|
37134 | 161 |
ML {* |
162 |
check_syntax @{context} @{thm d1_def} "d1(?x) <-> ~ p2(p1(?x))"; |
|
163 |
check_syntax @{context} @{thm d2_def} "d2(?x) <-> ~ p2(?x)"; |
|
164 |
*} |
|
165 |
||
166 |
end |
|
167 |
||
168 |
locale syntax'' = "syntax" p3 p2 for p3 :: "'a => 'b" and p2 :: "'b => o" |
|
169 |
begin |
|
170 |
||
171 |
thm d1_def d2_def |
|
172 |
(* should print as "syntax.d1(p3, p2, ?x) <-> ..." and "d2(?x) <-> ..." *) |
|
173 |
||
174 |
ML {* |
|
175 |
check_syntax @{context} @{thm d1_def} "syntax.d1(p3, p2, ?x) <-> ~ p2(p3(?x))"; |
|
176 |
check_syntax @{context} @{thm d2_def} "d2(?x) <-> ~ p2(?x)"; |
|
177 |
*} |
|
178 |
||
179 |
end |
|
180 |
||
181 |
||
182 |
section {* Foundational versions of theorems *} |
|
183 |
||
184 |
thm logic.assoc |
|
185 |
thm logic.lor_def |
|
186 |
||
187 |
||
188 |
section {* Defines *} |
|
189 |
||
190 |
locale logic_def = |
|
191 |
fixes land (infixl "&&" 55) |
|
192 |
and lor (infixl "||" 50) |
|
193 |
and lnot ("-- _" [60] 60) |
|
194 |
assumes assoc: "(x && y) && z = x && (y && z)" |
|
195 |
and notnot: "-- (-- x) = x" |
|
196 |
defines "x || y == --(-- x && -- y)" |
|
197 |
begin |
|
198 |
||
199 |
thm lor_def |
|
200 |
||
201 |
lemma "x || y = --(-- x && --y)" |
|
202 |
by (unfold lor_def) (rule refl) |
|
203 |
||
204 |
end |
|
205 |
||
206 |
(* Inheritance of defines *) |
|
207 |
||
208 |
locale logic_def2 = logic_def |
|
209 |
begin |
|
210 |
||
211 |
lemma "x || y = --(-- x && --y)" |
|
212 |
by (unfold lor_def) (rule refl) |
|
213 |
||
214 |
end |
|
215 |
||
216 |
||
217 |
section {* Notes *} |
|
218 |
||
219 |
(* A somewhat arcane homomorphism example *) |
|
220 |
||
221 |
definition semi_hom where |
|
222 |
"semi_hom(prod, sum, h) <-> (ALL x y. h(prod(x, y)) = sum(h(x), h(y)))" |
|
223 |
||
224 |
lemma semi_hom_mult: |
|
225 |
"semi_hom(prod, sum, h) ==> h(prod(x, y)) = sum(h(x), h(y))" |
|
226 |
by (simp add: semi_hom_def) |
|
227 |
||
228 |
locale semi_hom_loc = prod: semi prod + sum: semi sum |
|
229 |
for prod and sum and h + |
|
230 |
assumes semi_homh: "semi_hom(prod, sum, h)" |
|
231 |
notes semi_hom_mult = semi_hom_mult [OF semi_homh] |
|
232 |
||
233 |
thm semi_hom_loc.semi_hom_mult |
|
234 |
(* unspecified, attribute not applied in backgroud theory !!! *) |
|
235 |
||
236 |
lemma (in semi_hom_loc) "h(prod(x, y)) = sum(h(x), h(y))" |
|
237 |
by (rule semi_hom_mult) |
|
238 |
||
239 |
(* Referring to facts from within a context specification *) |
|
240 |
||
241 |
lemma |
|
242 |
assumes x: "P <-> P" |
|
243 |
notes y = x |
|
244 |
shows True .. |
|
245 |
||
246 |
||
247 |
section {* Theorem statements *} |
|
248 |
||
249 |
lemma (in lgrp) lcancel: |
|
250 |
"x ** y = x ** z <-> y = z" |
|
251 |
proof |
|
252 |
assume "x ** y = x ** z" |
|
253 |
then have "inv(x) ** x ** y = inv(x) ** x ** z" by (simp add: assoc) |
|
254 |
then show "y = z" by (simp add: lone linv) |
|
255 |
qed simp |
|
256 |
print_locale! lgrp |
|
257 |
||
258 |
||
259 |
locale rgrp = semi + |
|
260 |
fixes one and inv |
|
261 |
assumes rone: "x ** one = x" |
|
262 |
and rinv: "x ** inv(x) = one" |
|
263 |
begin |
|
264 |
||
265 |
lemma rcancel: |
|
266 |
"y ** x = z ** x <-> y = z" |
|
267 |
proof |
|
268 |
assume "y ** x = z ** x" |
|
269 |
then have "y ** (x ** inv(x)) = z ** (x ** inv(x))" |
|
270 |
by (simp add: assoc [symmetric]) |
|
271 |
then show "y = z" by (simp add: rone rinv) |
|
272 |
qed simp |
|
273 |
||
274 |
end |
|
275 |
print_locale! rgrp |
|
276 |
||
277 |
||
278 |
subsection {* Patterns *} |
|
279 |
||
280 |
lemma (in rgrp) |
|
281 |
assumes "y ** x = z ** x" (is ?a) |
|
282 |
shows "y = z" (is ?t) |
|
283 |
proof - |
|
284 |
txt {* Weird proof involving patterns from context element and conclusion. *} |
|
285 |
{ |
|
286 |
assume ?a |
|
287 |
then have "y ** (x ** inv(x)) = z ** (x ** inv(x))" |
|
288 |
by (simp add: assoc [symmetric]) |
|
289 |
then have ?t by (simp add: rone rinv) |
|
290 |
} |
|
291 |
note x = this |
|
292 |
show ?t by (rule x [OF `?a`]) |
|
293 |
qed |
|
294 |
||
295 |
||
296 |
section {* Interpretation between locales: sublocales *} |
|
297 |
||
298 |
sublocale lgrp < right: rgrp |
|
299 |
print_facts |
|
300 |
proof unfold_locales |
|
301 |
{ |
|
302 |
fix x |
|
303 |
have "inv(x) ** x ** one = inv(x) ** x" by (simp add: linv lone) |
|
304 |
then show "x ** one = x" by (simp add: assoc lcancel) |
|
305 |
} |
|
306 |
note rone = this |
|
307 |
{ |
|
308 |
fix x |
|
309 |
have "inv(x) ** x ** inv(x) = inv(x) ** one" |
|
310 |
by (simp add: linv lone rone) |
|
311 |
then show "x ** inv(x) = one" by (simp add: assoc lcancel) |
|
312 |
} |
|
313 |
qed |
|
314 |
||
315 |
(* effect on printed locale *) |
|
316 |
||
317 |
print_locale! lgrp |
|
318 |
||
319 |
(* use of derived theorem *) |
|
320 |
||
321 |
lemma (in lgrp) |
|
322 |
"y ** x = z ** x <-> y = z" |
|
323 |
apply (rule rcancel) |
|
324 |
done |
|
325 |
||
326 |
(* circular interpretation *) |
|
327 |
||
328 |
sublocale rgrp < left: lgrp |
|
329 |
proof unfold_locales |
|
330 |
{ |
|
331 |
fix x |
|
332 |
have "one ** (x ** inv(x)) = x ** inv(x)" by (simp add: rinv rone) |
|
333 |
then show "one ** x = x" by (simp add: assoc [symmetric] rcancel) |
|
334 |
} |
|
335 |
note lone = this |
|
336 |
{ |
|
337 |
fix x |
|
338 |
have "inv(x) ** (x ** inv(x)) = one ** inv(x)" |
|
339 |
by (simp add: rinv lone rone) |
|
340 |
then show "inv(x) ** x = one" by (simp add: assoc [symmetric] rcancel) |
|
341 |
} |
|
342 |
qed |
|
343 |
||
344 |
(* effect on printed locale *) |
|
345 |
||
346 |
print_locale! rgrp |
|
347 |
print_locale! lgrp |
|
348 |
||
349 |
||
350 |
(* Duality *) |
|
351 |
||
352 |
locale order = |
|
353 |
fixes less :: "'a => 'a => o" (infix "<<" 50) |
|
354 |
assumes refl: "x << x" |
|
355 |
and trans: "[| x << y; y << z |] ==> x << z" |
|
356 |
||
357 |
sublocale order < dual: order "%x y. y << x" |
|
358 |
apply unfold_locales apply (rule refl) apply (blast intro: trans) |
|
359 |
done |
|
360 |
||
361 |
print_locale! order (* Only two instances of order. *) |
|
362 |
||
363 |
locale order' = |
|
364 |
fixes less :: "'a => 'a => o" (infix "<<" 50) |
|
365 |
assumes refl: "x << x" |
|
366 |
and trans: "[| x << y; y << z |] ==> x << z" |
|
367 |
||
368 |
locale order_with_def = order' |
|
369 |
begin |
|
370 |
||
371 |
definition greater :: "'a => 'a => o" (infix ">>" 50) where |
|
372 |
"x >> y <-> y << x" |
|
373 |
||
374 |
end |
|
375 |
||
376 |
sublocale order_with_def < dual: order' "op >>" |
|
377 |
apply unfold_locales |
|
378 |
unfolding greater_def |
|
379 |
apply (rule refl) apply (blast intro: trans) |
|
380 |
done |
|
381 |
||
382 |
print_locale! order_with_def |
|
383 |
(* Note that decls come after theorems that make use of them. *) |
|
384 |
||
385 |
||
386 |
(* locale with many parameters --- |
|
387 |
interpretations generate alternating group A5 *) |
|
388 |
||
389 |
||
390 |
locale A5 = |
|
391 |
fixes A and B and C and D and E |
|
392 |
assumes eq: "A <-> B <-> C <-> D <-> E" |
|
393 |
||
394 |
sublocale A5 < 1: A5 _ _ D E C |
|
395 |
print_facts |
|
396 |
using eq apply (blast intro: A5.intro) done |
|
397 |
||
398 |
sublocale A5 < 2: A5 C _ E _ A |
|
399 |
print_facts |
|
400 |
using eq apply (blast intro: A5.intro) done |
|
401 |
||
402 |
sublocale A5 < 3: A5 B C A _ _ |
|
403 |
print_facts |
|
404 |
using eq apply (blast intro: A5.intro) done |
|
405 |
||
406 |
(* Any even permutation of parameters is subsumed by the above. *) |
|
407 |
||
408 |
print_locale! A5 |
|
409 |
||
410 |
||
411 |
(* Free arguments of instance *) |
|
412 |
||
413 |
locale trivial = |
|
414 |
fixes P and Q :: o |
|
415 |
assumes Q: "P <-> P <-> Q" |
|
416 |
begin |
|
417 |
||
418 |
lemma Q_triv: "Q" using Q by fast |
|
419 |
||
420 |
end |
|
421 |
||
422 |
sublocale trivial < x: trivial x _ |
|
423 |
apply unfold_locales using Q by fast |
|
424 |
||
425 |
print_locale! trivial |
|
426 |
||
427 |
context trivial begin thm x.Q [where ?x = True] end |
|
428 |
||
429 |
sublocale trivial < y: trivial Q Q |
|
430 |
by unfold_locales |
|
431 |
(* Succeeds since previous interpretation is more general. *) |
|
432 |
||
433 |
print_locale! trivial (* No instance for y created (subsumed). *) |
|
434 |
||
435 |
||
436 |
subsection {* Sublocale, then interpretation in theory *} |
|
437 |
||
438 |
interpretation int?: lgrp "op +" "0" "minus" |
|
439 |
proof unfold_locales |
|
440 |
qed (rule int_assoc int_zero int_minus)+ |
|
441 |
||
442 |
thm int.assoc int.semi_axioms |
|
443 |
||
444 |
interpretation int2?: semi "op +" |
|
445 |
by unfold_locales (* subsumed, thm int2.assoc not generated *) |
|
446 |
||
39557
fe5722fce758
renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents:
38109
diff
changeset
|
447 |
ML {* (Global_Theory.get_thms @{theory} "int2.assoc"; |
37134 | 448 |
error "thm int2.assoc was generated") |
449 |
handle ERROR "Unknown fact \"int2.assoc\"" => ([]:thm list); *} |
|
450 |
||
451 |
thm int.lone int.right.rone |
|
452 |
(* the latter comes through the sublocale relation *) |
|
453 |
||
454 |
||
455 |
subsection {* Interpretation in theory, then sublocale *} |
|
456 |
||
457 |
interpretation fol: logic "op +" "minus" |
|
458 |
by unfold_locales (rule int_assoc int_minus2)+ |
|
459 |
||
460 |
locale logic2 = |
|
461 |
fixes land (infixl "&&" 55) |
|
462 |
and lnot ("-- _" [60] 60) |
|
463 |
assumes assoc: "(x && y) && z = x && (y && z)" |
|
464 |
and notnot: "-- (-- x) = x" |
|
465 |
begin |
|
466 |
||
467 |
definition lor (infixl "||" 50) where |
|
468 |
"x || y = --(-- x && -- y)" |
|
469 |
||
470 |
end |
|
471 |
||
472 |
sublocale logic < two: logic2 |
|
473 |
by unfold_locales (rule assoc notnot)+ |
|
474 |
||
475 |
thm fol.two.assoc |
|
476 |
||
477 |
||
478 |
subsection {* Declarations and sublocale *} |
|
479 |
||
480 |
locale logic_a = logic |
|
481 |
locale logic_b = logic |
|
482 |
||
483 |
sublocale logic_a < logic_b |
|
484 |
by unfold_locales |
|
485 |
||
486 |
||
487 |
subsection {* Equations *} |
|
488 |
||
489 |
locale logic_o = |
|
490 |
fixes land (infixl "&&" 55) |
|
491 |
and lnot ("-- _" [60] 60) |
|
492 |
assumes assoc_o: "(x && y) && z <-> x && (y && z)" |
|
493 |
and notnot_o: "-- (-- x) <-> x" |
|
494 |
begin |
|
495 |
||
496 |
definition lor_o (infixl "||" 50) where |
|
497 |
"x || y <-> --(-- x && -- y)" |
|
498 |
||
499 |
end |
|
500 |
||
501 |
interpretation x: logic_o "op &" "Not" |
|
502 |
where bool_logic_o: "logic_o.lor_o(op &, Not, x, y) <-> x | y" |
|
503 |
proof - |
|
504 |
show bool_logic_o: "PROP logic_o(op &, Not)" by unfold_locales fast+ |
|
505 |
show "logic_o.lor_o(op &, Not, x, y) <-> x | y" |
|
506 |
by (unfold logic_o.lor_o_def [OF bool_logic_o]) fast |
|
507 |
qed |
|
508 |
||
509 |
thm x.lor_o_def bool_logic_o |
|
510 |
||
511 |
lemma lor_triv: "z <-> z" .. |
|
512 |
||
513 |
lemma (in logic_o) lor_triv: "x || y <-> x || y" by fast |
|
514 |
||
515 |
thm lor_triv [where z = True] (* Check strict prefix. *) |
|
516 |
x.lor_triv |
|
517 |
||
518 |
||
519 |
subsection {* Inheritance of mixins *} |
|
520 |
||
521 |
locale reflexive = |
|
522 |
fixes le :: "'a => 'a => o" (infix "\<sqsubseteq>" 50) |
|
523 |
assumes refl: "x \<sqsubseteq> x" |
|
524 |
begin |
|
525 |
||
526 |
definition less (infix "\<sqsubset>" 50) where "x \<sqsubset> y <-> x \<sqsubseteq> y & x ~= y" |
|
527 |
||
528 |
end |
|
529 |
||
530 |
consts |
|
531 |
gle :: "'a => 'a => o" gless :: "'a => 'a => o" |
|
532 |
gle' :: "'a => 'a => o" gless' :: "'a => 'a => o" |
|
533 |
||
534 |
axioms |
|
535 |
grefl: "gle(x, x)" gless_def: "gless(x, y) <-> gle(x, y) & x ~= y" |
|
536 |
grefl': "gle'(x, x)" gless'_def: "gless'(x, y) <-> gle'(x, y) & x ~= y" |
|
537 |
||
538 |
text {* Setup *} |
|
539 |
||
540 |
locale mixin = reflexive |
|
541 |
begin |
|
542 |
lemmas less_thm = less_def |
|
543 |
end |
|
544 |
||
545 |
interpretation le: mixin gle where "reflexive.less(gle, x, y) <-> gless(x, y)" |
|
546 |
proof - |
|
547 |
show "mixin(gle)" by unfold_locales (rule grefl) |
|
548 |
note reflexive = this[unfolded mixin_def] |
|
549 |
show "reflexive.less(gle, x, y) <-> gless(x, y)" |
|
550 |
by (simp add: reflexive.less_def[OF reflexive] gless_def) |
|
551 |
qed |
|
552 |
||
553 |
text {* Mixin propagated along the locale hierarchy *} |
|
554 |
||
555 |
locale mixin2 = mixin |
|
556 |
begin |
|
557 |
lemmas less_thm2 = less_def |
|
558 |
end |
|
559 |
||
560 |
interpretation le: mixin2 gle |
|
561 |
by unfold_locales |
|
562 |
||
563 |
thm le.less_thm2 (* mixin applied *) |
|
564 |
lemma "gless(x, y) <-> gle(x, y) & x ~= y" |
|
565 |
by (rule le.less_thm2) |
|
566 |
||
567 |
text {* Mixin does not leak to a side branch. *} |
|
568 |
||
569 |
locale mixin3 = reflexive |
|
570 |
begin |
|
571 |
lemmas less_thm3 = less_def |
|
572 |
end |
|
573 |
||
574 |
interpretation le: mixin3 gle |
|
575 |
by unfold_locales |
|
576 |
||
577 |
thm le.less_thm3 (* mixin not applied *) |
|
578 |
lemma "reflexive.less(gle, x, y) <-> gle(x, y) & x ~= y" by (rule le.less_thm3) |
|
579 |
||
580 |
text {* Mixin only available in original context *} |
|
581 |
||
582 |
locale mixin4_base = reflexive |
|
583 |
||
584 |
locale mixin4_mixin = mixin4_base |
|
585 |
||
586 |
interpretation le: mixin4_mixin gle |
|
587 |
where "reflexive.less(gle, x, y) <-> gless(x, y)" |
|
588 |
proof - |
|
589 |
show "mixin4_mixin(gle)" by unfold_locales (rule grefl) |
|
590 |
note reflexive = this[unfolded mixin4_mixin_def mixin4_base_def mixin_def] |
|
591 |
show "reflexive.less(gle, x, y) <-> gless(x, y)" |
|
592 |
by (simp add: reflexive.less_def[OF reflexive] gless_def) |
|
593 |
qed |
|
594 |
||
595 |
locale mixin4_copy = mixin4_base |
|
596 |
begin |
|
597 |
lemmas less_thm4 = less_def |
|
598 |
end |
|
599 |
||
600 |
locale mixin4_combined = le1: mixin4_mixin le' + le2: mixin4_copy le for le' le |
|
601 |
begin |
|
602 |
lemmas less_thm4' = less_def |
|
603 |
end |
|
604 |
||
605 |
interpretation le4: mixin4_combined gle' gle |
|
606 |
by unfold_locales (rule grefl') |
|
607 |
||
608 |
thm le4.less_thm4' (* mixin not applied *) |
|
609 |
lemma "reflexive.less(gle, x, y) <-> gle(x, y) & x ~= y" |
|
610 |
by (rule le4.less_thm4') |
|
611 |
||
612 |
text {* Inherited mixin applied to new theorem *} |
|
613 |
||
614 |
locale mixin5_base = reflexive |
|
615 |
||
616 |
locale mixin5_inherited = mixin5_base |
|
617 |
||
618 |
interpretation le5: mixin5_base gle |
|
619 |
where "reflexive.less(gle, x, y) <-> gless(x, y)" |
|
620 |
proof - |
|
621 |
show "mixin5_base(gle)" by unfold_locales |
|
622 |
note reflexive = this[unfolded mixin5_base_def mixin_def] |
|
623 |
show "reflexive.less(gle, x, y) <-> gless(x, y)" |
|
624 |
by (simp add: reflexive.less_def[OF reflexive] gless_def) |
|
625 |
qed |
|
626 |
||
627 |
interpretation le5: mixin5_inherited gle |
|
628 |
by unfold_locales |
|
629 |
||
630 |
lemmas (in mixin5_inherited) less_thm5 = less_def |
|
631 |
||
632 |
thm le5.less_thm5 (* mixin applied *) |
|
633 |
lemma "gless(x, y) <-> gle(x, y) & x ~= y" |
|
634 |
by (rule le5.less_thm5) |
|
635 |
||
636 |
text {* Mixin pushed down to existing inherited locale *} |
|
637 |
||
638 |
locale mixin6_base = reflexive |
|
639 |
||
640 |
locale mixin6_inherited = mixin5_base |
|
641 |
||
642 |
interpretation le6: mixin6_base gle |
|
643 |
by unfold_locales |
|
644 |
interpretation le6: mixin6_inherited gle |
|
645 |
by unfold_locales |
|
646 |
interpretation le6: mixin6_base gle |
|
647 |
where "reflexive.less(gle, x, y) <-> gless(x, y)" |
|
648 |
proof - |
|
649 |
show "mixin6_base(gle)" by unfold_locales |
|
650 |
note reflexive = this[unfolded mixin6_base_def mixin_def] |
|
651 |
show "reflexive.less(gle, x, y) <-> gless(x, y)" |
|
652 |
by (simp add: reflexive.less_def[OF reflexive] gless_def) |
|
653 |
qed |
|
654 |
||
655 |
lemmas (in mixin6_inherited) less_thm6 = less_def |
|
656 |
||
657 |
thm le6.less_thm6 (* mixin applied *) |
|
658 |
lemma "gless(x, y) <-> gle(x, y) & x ~= y" |
|
659 |
by (rule le6.less_thm6) |
|
660 |
||
661 |
text {* Existing mixin inherited through sublocale relation *} |
|
662 |
||
663 |
locale mixin7_base = reflexive |
|
664 |
||
665 |
locale mixin7_inherited = reflexive |
|
666 |
||
667 |
interpretation le7: mixin7_base gle |
|
668 |
where "reflexive.less(gle, x, y) <-> gless(x, y)" |
|
669 |
proof - |
|
670 |
show "mixin7_base(gle)" by unfold_locales |
|
671 |
note reflexive = this[unfolded mixin7_base_def mixin_def] |
|
672 |
show "reflexive.less(gle, x, y) <-> gless(x, y)" |
|
673 |
by (simp add: reflexive.less_def[OF reflexive] gless_def) |
|
674 |
qed |
|
675 |
||
676 |
interpretation le7: mixin7_inherited gle |
|
677 |
by unfold_locales |
|
678 |
||
679 |
lemmas (in mixin7_inherited) less_thm7 = less_def |
|
680 |
||
681 |
thm le7.less_thm7 (* before, mixin not applied *) |
|
682 |
lemma "reflexive.less(gle, x, y) <-> gle(x, y) & x ~= y" |
|
683 |
by (rule le7.less_thm7) |
|
684 |
||
685 |
sublocale mixin7_inherited < mixin7_base |
|
686 |
by unfold_locales |
|
687 |
||
688 |
lemmas (in mixin7_inherited) less_thm7b = less_def |
|
689 |
||
690 |
thm le7.less_thm7b (* after, mixin applied *) |
|
691 |
lemma "gless(x, y) <-> gle(x, y) & x ~= y" |
|
692 |
by (rule le7.less_thm7b) |
|
693 |
||
694 |
||
695 |
text {* This locale will be interpreted in later theories. *} |
|
696 |
||
697 |
locale mixin_thy_merge = le: reflexive le + le': reflexive le' for le le' |
|
698 |
||
699 |
||
41272 | 700 |
subsection {* Mixins in sublocale *} |
701 |
||
702 |
text {* Simulate a specification of left groups where unit and inverse are defined |
|
703 |
rather than specified. This is possible, but not in FOL, due to the lack of a |
|
704 |
selection operator. *} |
|
705 |
||
706 |
axiomatization glob_one and glob_inv |
|
707 |
where glob_lone: "prod(glob_one(prod), x) = x" |
|
708 |
and glob_linv: "prod(glob_inv(prod, x), x) = glob_one(prod)" |
|
709 |
||
710 |
locale dgrp = semi |
|
711 |
begin |
|
712 |
||
713 |
definition one where "one = glob_one(prod)" |
|
714 |
||
715 |
lemma lone: "one ** x = x" |
|
716 |
unfolding one_def by (rule glob_lone) |
|
717 |
||
718 |
definition inv where "inv(x) = glob_inv(prod, x)" |
|
719 |
||
720 |
lemma linv: "inv(x) ** x = one" |
|
721 |
unfolding one_def inv_def by (rule glob_linv) |
|
722 |
||
723 |
end |
|
724 |
||
725 |
sublocale lgrp < "def": dgrp |
|
726 |
where one_equation: "dgrp.one(prod) = one" and inv_equation: "dgrp.inv(prod, x) = inv(x)" |
|
727 |
proof - |
|
728 |
show "dgrp(prod)" by unfold_locales |
|
729 |
from this interpret d: dgrp . |
|
730 |
-- Unit |
|
731 |
have "dgrp.one(prod) = glob_one(prod)" by (rule d.one_def) |
|
732 |
also have "... = glob_one(prod) ** one" by (simp add: rone) |
|
733 |
also have "... = one" by (simp add: glob_lone) |
|
734 |
finally show "dgrp.one(prod) = one" . |
|
735 |
-- Inverse |
|
736 |
then have "dgrp.inv(prod, x) ** x = inv(x) ** x" by (simp add: glob_linv d.linv linv) |
|
737 |
then show "dgrp.inv(prod, x) = inv(x)" by (simp add: rcancel) |
|
738 |
qed |
|
739 |
||
740 |
print_locale! lgrp |
|
741 |
||
742 |
context lgrp begin |
|
743 |
||
744 |
text {* Equations stored in target *} |
|
745 |
||
746 |
lemma "dgrp.one(prod) = one" by (rule one_equation) |
|
747 |
lemma "dgrp.inv(prod, x) = inv(x)" by (rule inv_equation) |
|
748 |
||
749 |
text {* Mixins applied *} |
|
750 |
||
751 |
lemma "one = glob_one(prod)" by (rule one_def) |
|
752 |
lemma "inv(x) = glob_inv(prod, x)" by (rule inv_def) |
|
753 |
||
754 |
end |
|
755 |
||
756 |
text {* Interpreted versions *} |
|
757 |
||
758 |
lemma "0 = glob_one (op +)" by (rule int.def.one_def) |
|
759 |
lemma "- x = glob_inv(op +, x)" by (rule int.def.inv_def) |
|
760 |
||
761 |
||
762 |
section {* Interpretation in proofs *} |
|
37134 | 763 |
|
764 |
lemma True |
|
765 |
proof |
|
766 |
interpret "local": lgrp "op +" "0" "minus" |
|
767 |
by unfold_locales (* subsumed *) |
|
768 |
{ |
|
769 |
fix zero :: int |
|
770 |
assume "!!x. zero + x = x" "!!x. (-x) + x = zero" |
|
771 |
then interpret local_fixed: lgrp "op +" zero "minus" |
|
772 |
by unfold_locales |
|
773 |
thm local_fixed.lone |
|
774 |
} |
|
775 |
assume "!!x zero. zero + x = x" "!!x zero. (-x) + x = zero" |
|
776 |
then interpret local_free: lgrp "op +" zero "minus" for zero |
|
777 |
by unfold_locales |
|
778 |
thm local_free.lone [where ?zero = 0] |
|
779 |
qed |
|
780 |
||
38108 | 781 |
lemma True |
782 |
proof |
|
783 |
{ |
|
784 |
fix pand and pnot and por |
|
785 |
assume passoc: "!!x y z. pand(pand(x, y), z) <-> pand(x, pand(y, z))" |
|
786 |
and pnotnot: "!!x. pnot(pnot(x)) <-> x" |
|
787 |
and por_def: "!!x y. por(x, y) <-> pnot(pand(pnot(x), pnot(y)))" |
|
788 |
interpret loc: logic_o pand pnot |
|
789 |
where por_eq: "!!x y. logic_o.lor_o(pand, pnot, x, y) <-> por(x, y)" (* FIXME *) |
|
790 |
proof - |
|
791 |
show logic_o: "PROP logic_o(pand, pnot)" using passoc pnotnot by unfold_locales |
|
792 |
fix x y |
|
793 |
show "logic_o.lor_o(pand, pnot, x, y) <-> por(x, y)" |
|
794 |
by (unfold logic_o.lor_o_def [OF logic_o]) (rule por_def [symmetric]) |
|
795 |
qed |
|
38109 | 796 |
print_interps logic_o |
38108 | 797 |
have "!!x y. por(x, y) <-> pnot(pand(pnot(x), pnot(y)))" by (rule loc.lor_o_def) |
798 |
} |
|
799 |
qed |
|
800 |
||
37134 | 801 |
end |