10 CodegenSerializer.code_width := 74; |
10 CodegenSerializer.code_width := 74; |
11 *} |
11 *} |
12 |
12 |
13 syntax |
13 syntax |
14 "_alpha" :: "type" ("\<alpha>") |
14 "_alpha" :: "type" ("\<alpha>") |
15 "_alpha_ofsort" :: "sort \<Rightarrow> type" ("\<alpha>()::_" [0] 1000) |
15 "_alpha_ofsort" :: "sort \<Rightarrow> type" ("\<alpha>()\<Colon>_" [0] 1000) |
16 "_beta" :: "type" ("\<beta>") |
16 "_beta" :: "type" ("\<beta>") |
17 "_beta_ofsort" :: "sort \<Rightarrow> type" ("\<beta>()::_" [0] 1000) |
17 "_beta_ofsort" :: "sort \<Rightarrow> type" ("\<beta>()\<Colon>_" [0] 1000) |
18 "_gamma" :: "type" ("\<gamma>") |
18 "_gamma" :: "type" ("\<gamma>") |
19 "_gamma_ofsort" :: "sort \<Rightarrow> type" ("\<gamma>()::_" [0] 1000) |
19 "_gamma_ofsort" :: "sort \<Rightarrow> type" ("\<gamma>()\<Colon>_" [0] 1000) |
20 "_alpha_f" :: "type" ("\<alpha>\<^sub>f") |
20 "_alpha_f" :: "type" ("\<alpha>\<^sub>f") |
21 "_alpha_f_ofsort" :: "sort \<Rightarrow> type" ("\<alpha>\<^sub>f()::_" [0] 1000) |
21 "_alpha_f_ofsort" :: "sort \<Rightarrow> type" ("\<alpha>\<^sub>f()\<Colon>_" [0] 1000) |
22 "_beta_f" :: "type" ("\<beta>\<^sub>f") |
22 "_beta_f" :: "type" ("\<beta>\<^sub>f") |
23 "_beta_f_ofsort" :: "sort \<Rightarrow> type" ("\<beta>\<^sub>f()::_" [0] 1000) |
23 "_beta_f_ofsort" :: "sort \<Rightarrow> type" ("\<beta>\<^sub>f()\<Colon>_" [0] 1000) |
24 "_gamma_f" :: "type" ("\<gamma>\<^sub>f") |
24 "_gamma_f" :: "type" ("\<gamma>\<^sub>f") |
25 "_gamma_ofsort_f" :: "sort \<Rightarrow> type" ("\<gamma>\<^sub>f()::_" [0] 1000) |
25 "_gamma_ofsort_f" :: "sort \<Rightarrow> type" ("\<gamma>\<^sub>f()\<Colon>_" [0] 1000) |
26 |
26 |
27 parse_ast_translation {* |
27 parse_ast_translation {* |
28 let |
28 let |
29 fun alpha_ast_tr [] = Syntax.Variable "'a" |
29 fun alpha_ast_tr [] = Syntax.Variable "'a" |
30 | alpha_ast_tr asts = raise Syntax.AST ("alpha_ast_tr", asts); |
30 | alpha_ast_tr asts = raise Syntax.AST ("alpha_ast_tr", asts); |
169 \noindent This @{text "\<CLASS>"} specification consists of two |
169 \noindent This @{text "\<CLASS>"} specification consists of two |
170 parts: the \qn{operational} part names the class operation (@{text |
170 parts: the \qn{operational} part names the class operation (@{text |
171 "\<FIXES>"}), the \qn{logical} part specifies properties on them |
171 "\<FIXES>"}), the \qn{logical} part specifies properties on them |
172 (@{text "\<ASSUMES>"}). The local @{text "\<FIXES>"} and @{text |
172 (@{text "\<ASSUMES>"}). The local @{text "\<FIXES>"} and @{text |
173 "\<ASSUMES>"} are lifted to the theory toplevel, yielding the global |
173 "\<ASSUMES>"} are lifted to the theory toplevel, yielding the global |
174 operation @{term [source] "mult :: \<alpha>::semigroup \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>"} and the |
174 operation @{term [source] "mult \<Colon> \<alpha>\<Colon>semigroup \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>"} and the |
175 global theorem @{text "semigroup.assoc:"}~@{prop [source] "\<And>x y |
175 global theorem @{text "semigroup.assoc:"}~@{prop [source] "\<And>x y |
176 z::\<alpha>::semigroup. (x \<otimes> y) \<otimes> z = x \<otimes> (y \<otimes> z)"}. |
176 z \<Colon> \<alpha>\<Colon>semigroup. (x \<otimes> y) \<otimes> z = x \<otimes> (y \<otimes> z)"}. |
177 *} |
177 *} |
178 |
178 |
179 |
179 |
180 subsection {* Class instantiation \label{sec:class_inst} *} |
180 subsection {* Class instantiation \label{sec:class_inst} *} |
181 |
181 |
184 instance by providing a suitable definition for the class operation |
184 instance by providing a suitable definition for the class operation |
185 @{text "mult"} and a proof for the specification of @{text "assoc"}. |
185 @{text "mult"} and a proof for the specification of @{text "assoc"}. |
186 *} |
186 *} |
187 |
187 |
188 instance int :: semigroup |
188 instance int :: semigroup |
189 mult_int_def: "\<And>i j :: int. i \<otimes> j \<equiv> i + j" |
189 mult_int_def: "\<And>i j \<Colon> int. i \<otimes> j \<equiv> i + j" |
190 proof |
190 proof |
191 fix i j k :: int have "(i + j) + k = i + (j + k)" by simp |
191 fix i j k :: int have "(i + j) + k = i + (j + k)" by simp |
192 then show "(i \<otimes> j) \<otimes> k = i \<otimes> (j \<otimes> k)" unfolding mult_int_def . |
192 then show "(i \<otimes> j) \<otimes> k = i \<otimes> (j \<otimes> k)" unfolding mult_int_def . |
193 qed |
193 qed |
194 |
194 |
363 (*<*) setup {* Sign.parent_path *} (*>*) |
363 (*<*) setup {* Sign.parent_path *} (*>*) |
364 |
364 |
365 text {* |
365 text {* |
366 This give you at hand the full power of the Isabelle module system; |
366 This give you at hand the full power of the Isabelle module system; |
367 conclusions in locale @{text idem} are implicitly propagated |
367 conclusions in locale @{text idem} are implicitly propagated |
368 to class @{idem}. |
368 to class @{text idem}. |
369 *} |
369 *} |
370 |
370 |
371 subsection {* Abstract reasoning *} |
371 subsection {* Abstract reasoning *} |
372 |
372 |
373 text {* |
373 text {* |
390 |
390 |
391 text {* |
391 text {* |
392 \noindent Here the \qt{@{text "\<IN> group"}} target specification |
392 \noindent Here the \qt{@{text "\<IN> group"}} target specification |
393 indicates that the result is recorded within that context for later |
393 indicates that the result is recorded within that context for later |
394 use. This local theorem is also lifted to the global one @{text |
394 use. This local theorem is also lifted to the global one @{text |
395 "group.left_cancel:"} @{prop [source] "\<And>x y z::\<alpha>::group. x \<otimes> y = x \<otimes> |
395 "group.left_cancel:"} @{prop [source] "\<And>x y z \<Colon> \<alpha>\<Colon>group. x \<otimes> y = x \<otimes> |
396 z \<longleftrightarrow> y = z"}. Since type @{text "int"} has been made an instance of |
396 z \<longleftrightarrow> y = z"}. Since type @{text "int"} has been made an instance of |
397 @{text "group"} before, we may refer to that fact as well: @{prop |
397 @{text "group"} before, we may refer to that fact as well: @{prop |
398 [source] "\<And>x y z::int. x \<otimes> y = x \<otimes> z \<longleftrightarrow> y = z"}. |
398 [source] "\<And>x y z \<Colon> int. x \<otimes> y = x \<otimes> z \<longleftrightarrow> y = z"}. |
399 *} |
399 *} |
400 |
400 |
401 |
401 |
402 (*subsection {* Derived definitions *} |
402 (*subsection {* Derived definitions *} |
403 |
403 |
437 *} |
437 *} |
438 |
438 |
439 fun |
439 fun |
440 pow_nat :: "nat \<Rightarrow> \<alpha>\<Colon>monoidl \<Rightarrow> \<alpha>\<Colon>monoidl" where |
440 pow_nat :: "nat \<Rightarrow> \<alpha>\<Colon>monoidl \<Rightarrow> \<alpha>\<Colon>monoidl" where |
441 "pow_nat 0 x = \<one>" |
441 "pow_nat 0 x = \<one>" |
442 "pow_nat (Suc n) x = x \<otimes> pow_nat n x" |
442 | "pow_nat (Suc n) x = x \<otimes> pow_nat n x" |
443 |
443 |
444 definition |
444 definition |
445 pow_int :: "int \<Rightarrow> \<alpha>\<Colon>group \<Rightarrow> \<alpha>\<Colon>group" where |
445 pow_int :: "int \<Rightarrow> \<alpha>\<Colon>group \<Rightarrow> \<alpha>\<Colon>group" where |
446 "pow_int k x = (if k >= 0 |
446 "pow_int k x = (if k >= 0 |
447 then pow_nat (nat k) x |
447 then pow_nat (nat k) x |