src/HOL/ex/Classpackage.thy
 author haftmann Wed Nov 22 10:20:20 2006 +0100 (2006-11-22) changeset 21460 cda5cd8bfd16 parent 21404 eb85850d3eb7 child 21545 54cc492d80a9 permissions -rw-r--r--
example tuned
```     1 (*  ID:         \$Id\$
```
```     2     Author:     Florian Haftmann, TU Muenchen
```
```     3 *)
```
```     4
```
```     5 header {* Test and Examples for Pure/Tools/class_package.ML *}
```
```     6
```
```     7 theory Classpackage
```
```     8 imports Main
```
```     9 begin
```
```    10
```
```    11 class semigroup =
```
```    12   fixes mult :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "\<^loc>\<otimes>" 70)
```
```    13   assumes assoc: "x \<^loc>\<otimes> y \<^loc>\<otimes> z = x \<^loc>\<otimes> (y \<^loc>\<otimes> z)"
```
```    14
```
```    15 instance nat :: semigroup
```
```    16   "m \<otimes> n \<equiv> m + n"
```
```    17 proof
```
```    18   fix m n q :: nat
```
```    19   from semigroup_nat_def show "m \<otimes> n \<otimes> q = m \<otimes> (n \<otimes> q)" by simp
```
```    20 qed
```
```    21
```
```    22 instance int :: semigroup
```
```    23   "k \<otimes> l \<equiv> k + l"
```
```    24 proof
```
```    25   fix k l j :: int
```
```    26   from semigroup_int_def show "k \<otimes> l \<otimes> j = k \<otimes> (l \<otimes> j)" by simp
```
```    27 qed
```
```    28
```
```    29 instance list :: (type) semigroup
```
```    30   "xs \<otimes> ys \<equiv> xs @ ys"
```
```    31 proof
```
```    32   fix xs ys zs :: "'a list"
```
```    33   show "xs \<otimes> ys \<otimes> zs = xs \<otimes> (ys \<otimes> zs)"
```
```    34   proof -
```
```    35     from semigroup_list_def have "\<And>xs ys\<Colon>'a list. xs \<otimes> ys \<equiv> xs @ ys" .
```
```    36     thus ?thesis by simp
```
```    37   qed
```
```    38 qed
```
```    39
```
```    40 class monoidl = semigroup +
```
```    41   fixes one :: 'a ("\<^loc>\<one>")
```
```    42   assumes neutl: "\<^loc>\<one> \<^loc>\<otimes> x = x"
```
```    43
```
```    44 instance monoidl_num_def: nat :: monoidl and int :: monoidl
```
```    45   "\<one> \<equiv> 0"
```
```    46   "\<one> \<equiv> 0"
```
```    47 proof
```
```    48   fix n :: nat
```
```    49   from monoidl_num_def show "\<one> \<otimes> n = n" by simp
```
```    50 next
```
```    51   fix k :: int
```
```    52   from monoidl_num_def show "\<one> \<otimes> k = k" by simp
```
```    53 qed
```
```    54
```
```    55 instance list :: (type) monoidl
```
```    56   "\<one> \<equiv> []"
```
```    57 proof
```
```    58   fix xs :: "'a list"
```
```    59   show "\<one> \<otimes> xs = xs"
```
```    60   proof -
```
```    61     from mult_list_def have "\<And>xs ys\<Colon>'a list. xs \<otimes> ys \<equiv> xs @ ys" .
```
```    62     moreover from monoidl_list_def have "\<one> \<equiv> []\<Colon>'a list" by simp
```
```    63     ultimately show ?thesis by simp
```
```    64   qed
```
```    65 qed
```
```    66
```
```    67 class monoid = monoidl +
```
```    68   assumes neutr: "x \<^loc>\<otimes> \<^loc>\<one> = x"
```
```    69
```
```    70 instance monoid_list_def: list :: (type) monoid
```
```    71 proof
```
```    72   fix xs :: "'a list"
```
```    73   show "xs \<otimes> \<one> = xs"
```
```    74   proof -
```
```    75     from mult_list_def have "\<And>xs ys\<Colon>'a list. xs \<otimes> ys \<equiv> xs @ ys" .
```
```    76     moreover from monoid_list_def have "\<one> \<equiv> []\<Colon>'a list" by simp
```
```    77     ultimately show ?thesis by simp
```
```    78   qed
```
```    79 qed
```
```    80
```
```    81 class monoid_comm = monoid +
```
```    82   assumes comm: "x \<^loc>\<otimes> y = y \<^loc>\<otimes> x"
```
```    83
```
```    84 instance monoid_comm_num_def: nat :: monoid_comm and int :: monoid_comm
```
```    85 proof
```
```    86   fix n :: nat
```
```    87   from monoid_comm_num_def show "n \<otimes> \<one> = n" by simp
```
```    88 next
```
```    89   fix n m :: nat
```
```    90   from monoid_comm_num_def show "n \<otimes> m = m \<otimes> n" by simp
```
```    91 next
```
```    92   fix k :: int
```
```    93   from monoid_comm_num_def show "k \<otimes> \<one> = k" by simp
```
```    94 next
```
```    95   fix k l :: int
```
```    96   from monoid_comm_num_def show "k \<otimes> l = l \<otimes> k" by simp
```
```    97 qed
```
```    98
```
```    99 definition (in monoid)
```
```   100   units :: "'a set" where
```
```   101   "units = { y. \<exists>x. x \<^loc>\<otimes> y = \<^loc>\<one> \<and> y \<^loc>\<otimes> x = \<^loc>\<one> }"
```
```   102
```
```   103 lemma (in monoid) inv_obtain:
```
```   104   assumes ass: "x \<in> units"
```
```   105   obtains y where "y \<^loc>\<otimes> x = \<^loc>\<one>" and "x \<^loc>\<otimes> y = \<^loc>\<one>"
```
```   106 proof -
```
```   107   from ass units_def obtain y
```
```   108     where "y \<^loc>\<otimes> x = \<^loc>\<one>" and "x \<^loc>\<otimes> y = \<^loc>\<one>" by auto
```
```   109   thus ?thesis ..
```
```   110 qed
```
```   111
```
```   112 lemma (in monoid) inv_unique:
```
```   113   assumes eq: "y \<^loc>\<otimes> x = \<^loc>\<one>" "x \<^loc>\<otimes> y' = \<^loc>\<one>"
```
```   114   shows "y = y'"
```
```   115 proof -
```
```   116   from eq neutr have "y = y \<^loc>\<otimes> (x \<^loc>\<otimes> y')" by simp
```
```   117   also with assoc have "... = (y \<^loc>\<otimes> x) \<^loc>\<otimes> y'" by simp
```
```   118   also with eq neutl have "... = y'" by simp
```
```   119   finally show ?thesis .
```
```   120 qed
```
```   121
```
```   122 lemma (in monoid) units_inv_comm:
```
```   123   assumes inv: "x \<^loc>\<otimes> y = \<^loc>\<one>"
```
```   124     and G: "x \<in> units"
```
```   125   shows "y \<^loc>\<otimes> x = \<^loc>\<one>"
```
```   126 proof -
```
```   127   from G inv_obtain obtain z
```
```   128     where z_choice: "z \<^loc>\<otimes> x = \<^loc>\<one>" by blast
```
```   129   from inv neutl neutr have "x \<^loc>\<otimes> y \<^loc>\<otimes> x = x \<^loc>\<otimes> \<^loc>\<one>" by simp
```
```   130   with assoc have "z \<^loc>\<otimes> x \<^loc>\<otimes> y \<^loc>\<otimes> x = z \<^loc>\<otimes> x \<^loc>\<otimes> \<^loc>\<one>" by simp
```
```   131   with neutl z_choice show ?thesis by simp
```
```   132 qed
```
```   133
```
```   134 consts
```
```   135   reduce :: "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a"
```
```   136
```
```   137 primrec
```
```   138   "reduce f g 0 x = g"
```
```   139   "reduce f g (Suc n) x = f x (reduce f g n x)"
```
```   140
```
```   141 definition (in monoid)
```
```   142   npow :: "nat \<Rightarrow> 'a \<Rightarrow> 'a" where
```
```   143   npow_def_prim: "npow n x = reduce (op \<^loc>\<otimes>) \<^loc>\<one> n x"
```
```   144
```
```   145 abbreviation (in monoid)
```
```   146   abbrev_npow :: "'a \<Rightarrow> nat \<Rightarrow> 'a" (infix "\<^loc>\<up>" 75) where
```
```   147   "x \<^loc>\<up> n \<equiv> npow n x"
```
```   148
```
```   149 lemma (in monoid) npow_def:
```
```   150   "x \<^loc>\<up> 0 = \<^loc>\<one>"
```
```   151   "x \<^loc>\<up> Suc n = x \<^loc>\<otimes> x \<^loc>\<up> n"
```
```   152 using npow_def_prim by simp_all
```
```   153
```
```   154 lemma (in monoid) nat_pow_one:
```
```   155   "\<^loc>\<one> \<^loc>\<up> n = \<^loc>\<one>"
```
```   156 using npow_def neutl by (induct n) simp_all
```
```   157
```
```   158 lemma (in monoid) nat_pow_mult:
```
```   159   "npow n x \<^loc>\<otimes> npow m x = npow (n + m) x"
```
```   160 proof (induct n)
```
```   161   case 0 with neutl npow_def show ?case by simp
```
```   162 next
```
```   163   case (Suc n) with Suc.hyps assoc npow_def show ?case by simp
```
```   164 qed
```
```   165
```
```   166 lemma (in monoid) nat_pow_pow:
```
```   167   "npow n (npow m x) = npow (n * m) x"
```
```   168 using npow_def nat_pow_mult by (induct n) simp_all
```
```   169
```
```   170 class group = monoidl +
```
```   171   fixes inv :: "'a \<Rightarrow> 'a" ("\<^loc>\<div> _" [81] 80)
```
```   172   assumes invl: "\<^loc>\<div> x \<^loc>\<otimes> x = \<^loc>\<one>"
```
```   173
```
```   174 class group_comm = group + monoid_comm
```
```   175
```
```   176 instance group_comm_int_def: int :: group_comm
```
```   177   "\<div> k \<equiv> - (k\<Colon>int)"
```
```   178 proof
```
```   179   fix k :: int
```
```   180   from group_comm_int_def show "\<div> k \<otimes> k = \<one>" by simp
```
```   181 qed
```
```   182
```
```   183 lemma (in group) cancel:
```
```   184   "(x \<^loc>\<otimes> y = x \<^loc>\<otimes> z) = (y = z)"
```
```   185 proof
```
```   186   fix x y z :: 'a
```
```   187   assume eq: "x \<^loc>\<otimes> y = x \<^loc>\<otimes> z"
```
```   188   hence "\<^loc>\<div> x \<^loc>\<otimes> (x \<^loc>\<otimes> y) = \<^loc>\<div> x \<^loc>\<otimes> (x \<^loc>\<otimes> z)" by simp
```
```   189   with assoc have "\<^loc>\<div> x \<^loc>\<otimes> x \<^loc>\<otimes> y = \<^loc>\<div> x \<^loc>\<otimes> x \<^loc>\<otimes> z" by simp
```
```   190   with neutl invl show "y = z" by simp
```
```   191 next
```
```   192   fix x y z :: 'a
```
```   193   assume eq: "y = z"
```
```   194   thus "x \<^loc>\<otimes> y = x \<^loc>\<otimes> z" by simp
```
```   195 qed
```
```   196
```
```   197 lemma (in group) neutr:
```
```   198   "x \<^loc>\<otimes> \<^loc>\<one> = x"
```
```   199 proof -
```
```   200   from invl have "\<^loc>\<div> x \<^loc>\<otimes> x = \<^loc>\<one>" by simp
```
```   201   with assoc [symmetric] neutl invl have "\<^loc>\<div> x \<^loc>\<otimes> (x \<^loc>\<otimes> \<^loc>\<one>) = \<^loc>\<div> x \<^loc>\<otimes> x" by simp
```
```   202   with cancel show ?thesis by simp
```
```   203 qed
```
```   204
```
```   205 lemma (in group) invr:
```
```   206   "x \<^loc>\<otimes> \<^loc>\<div> x = \<^loc>\<one>"
```
```   207 proof -
```
```   208   from neutl invl have "\<^loc>\<div> x \<^loc>\<otimes> x \<^loc>\<otimes> \<^loc>\<div> x = \<^loc>\<div> x" by simp
```
```   209   with neutr have "\<^loc>\<div> x \<^loc>\<otimes> x \<^loc>\<otimes> \<^loc>\<div> x = \<^loc>\<div> x \<^loc>\<otimes> \<^loc>\<one> " by simp
```
```   210   with assoc have "\<^loc>\<div> x \<^loc>\<otimes> (x \<^loc>\<otimes> \<^loc>\<div> x) = \<^loc>\<div> x \<^loc>\<otimes> \<^loc>\<one> " by simp
```
```   211   with cancel show ?thesis ..
```
```   212 qed
```
```   213
```
```   214 instance group < monoid
```
```   215 proof -
```
```   216   fix x
```
```   217   from neutr show "x \<^loc>\<otimes> \<^loc>\<one> = x" .
```
```   218 qed
```
```   219
```
```   220 lemma (in group) all_inv [intro]:
```
```   221   "(x\<Colon>'a) \<in> monoid.units (op \<^loc>\<otimes>) \<^loc>\<one>"
```
```   222   unfolding units_def
```
```   223 proof -
```
```   224   fix x :: "'a"
```
```   225   from invl invr have "\<^loc>\<div> x \<^loc>\<otimes> x = \<^loc>\<one>" and "x \<^loc>\<otimes> \<^loc>\<div> x = \<^loc>\<one>" .
```
```   226   then obtain y where "y \<^loc>\<otimes> x = \<^loc>\<one>" and "x \<^loc>\<otimes> y = \<^loc>\<one>" ..
```
```   227   hence "\<exists>y\<Colon>'a. y \<^loc>\<otimes> x = \<^loc>\<one> \<and> x \<^loc>\<otimes> y = \<^loc>\<one>" by blast
```
```   228   thus "x \<in> {y\<Colon>'a. \<exists>x\<Colon>'a. x \<^loc>\<otimes> y = \<^loc>\<one> \<and> y \<^loc>\<otimes> x = \<^loc>\<one>}" by simp
```
```   229 qed
```
```   230
```
```   231 lemma (in group) cancer:
```
```   232   "(y \<^loc>\<otimes> x = z \<^loc>\<otimes> x) = (y = z)"
```
```   233 proof
```
```   234   assume eq: "y \<^loc>\<otimes> x = z \<^loc>\<otimes> x"
```
```   235   with assoc [symmetric] have "y \<^loc>\<otimes> (x \<^loc>\<otimes> \<^loc>\<div> x) = z \<^loc>\<otimes> (x \<^loc>\<otimes> \<^loc>\<div> x)" by (simp del: invr)
```
```   236   with invr neutr show "y = z" by simp
```
```   237 next
```
```   238   assume eq: "y = z"
```
```   239   thus "y \<^loc>\<otimes> x = z \<^loc>\<otimes> x" by simp
```
```   240 qed
```
```   241
```
```   242 lemma (in group) inv_one:
```
```   243   "\<^loc>\<div> \<^loc>\<one> = \<^loc>\<one>"
```
```   244 proof -
```
```   245   from neutl have "\<^loc>\<div> \<^loc>\<one> = \<^loc>\<one> \<^loc>\<otimes> (\<^loc>\<div> \<^loc>\<one>)" ..
```
```   246   moreover from invr have "... = \<^loc>\<one>" by simp
```
```   247   finally show ?thesis .
```
```   248 qed
```
```   249
```
```   250 lemma (in group) inv_inv:
```
```   251   "\<^loc>\<div> (\<^loc>\<div> x) = x"
```
```   252 proof -
```
```   253   from invl invr neutr
```
```   254     have "\<^loc>\<div> (\<^loc>\<div> x) \<^loc>\<otimes> \<^loc>\<div> x \<^loc>\<otimes> x = x \<^loc>\<otimes> \<^loc>\<div> x \<^loc>\<otimes> x" by simp
```
```   255   with assoc [symmetric]
```
```   256     have "\<^loc>\<div> (\<^loc>\<div> x) \<^loc>\<otimes> (\<^loc>\<div> x \<^loc>\<otimes> x) = x \<^loc>\<otimes> (\<^loc>\<div> x \<^loc>\<otimes> x)" by simp
```
```   257   with invl neutr show ?thesis by simp
```
```   258 qed
```
```   259
```
```   260 lemma (in group) inv_mult_group:
```
```   261   "\<^loc>\<div> (x \<^loc>\<otimes> y) = \<^loc>\<div> y \<^loc>\<otimes> \<^loc>\<div> x"
```
```   262 proof -
```
```   263   from invl have "\<^loc>\<div> (x \<^loc>\<otimes> y) \<^loc>\<otimes> (x \<^loc>\<otimes> y) = \<^loc>\<one>" by simp
```
```   264   with assoc have "\<^loc>\<div> (x \<^loc>\<otimes> y) \<^loc>\<otimes> x \<^loc>\<otimes> y = \<^loc>\<one>" by simp
```
```   265   with neutl have "\<^loc>\<div> (x \<^loc>\<otimes> y) \<^loc>\<otimes> x \<^loc>\<otimes> y \<^loc>\<otimes> \<^loc>\<div> y \<^loc>\<otimes> \<^loc>\<div> x = \<^loc>\<div> y \<^loc>\<otimes> \<^loc>\<div> x" by simp
```
```   266   with assoc have "\<^loc>\<div> (x \<^loc>\<otimes> y) \<^loc>\<otimes> (x \<^loc>\<otimes> (y \<^loc>\<otimes> \<^loc>\<div> y) \<^loc>\<otimes> \<^loc>\<div> x) = \<^loc>\<div> y \<^loc>\<otimes> \<^loc>\<div> x" by simp
```
```   267   with invr neutr show ?thesis by simp
```
```   268 qed
```
```   269
```
```   270 lemma (in group) inv_comm:
```
```   271   "x \<^loc>\<otimes> \<^loc>\<div> x = \<^loc>\<div> x \<^loc>\<otimes> x"
```
```   272 using invr invl by simp
```
```   273
```
```   274 definition (in group)
```
```   275   pow :: "int \<Rightarrow> 'a \<Rightarrow> 'a" where
```
```   276   "pow k x = (if k < 0 then \<^loc>\<div> (monoid.npow (op \<^loc>\<otimes>) \<^loc>\<one> (nat (-k)) x)
```
```   277     else (monoid.npow (op \<^loc>\<otimes>) \<^loc>\<one> (nat k) x))"
```
```   278
```
```   279 abbreviation (in group)
```
```   280   abbrev_pow :: "'a \<Rightarrow> int \<Rightarrow> 'a" (infix "\<^loc>\<up>" 75) where
```
```   281   "x \<^loc>\<up> k \<equiv> pow k x"
```
```   282
```
```   283 lemma (in group) int_pow_zero:
```
```   284   "x \<^loc>\<up> (0\<Colon>int) = \<^loc>\<one>"
```
```   285 using npow_def pow_def by simp
```
```   286
```
```   287 lemma (in group) int_pow_one:
```
```   288   "\<^loc>\<one> \<^loc>\<up> (k\<Colon>int) = \<^loc>\<one>"
```
```   289 using pow_def nat_pow_one inv_one by simp
```
```   290
```
```   291 instance semigroup_prod_def: * :: (semigroup, semigroup) semigroup
```
```   292   mult_prod_def: "x \<otimes> y \<equiv> let (x1, x2) = x; (y1, y2) = y in
```
```   293               (x1 \<otimes> y1, x2 \<otimes> y2)"
```
```   294 by default (simp_all add: split_paired_all semigroup_prod_def assoc)
```
```   295
```
```   296 instance monoidl_prod_def: * :: (monoidl, monoidl) monoidl
```
```   297   one_prod_def: "\<one> \<equiv> (\<one>, \<one>)"
```
```   298 by default (simp_all add: split_paired_all monoidl_prod_def neutl)
```
```   299
```
```   300 instance monoid_prod_def: * :: (monoid, monoid) monoid
```
```   301 by default (simp_all add: split_paired_all monoid_prod_def neutr)
```
```   302
```
```   303 instance monoid_comm_prod_def: * :: (monoid_comm, monoid_comm) monoid_comm
```
```   304 by default (simp_all add: split_paired_all monoidl_prod_def comm)
```
```   305
```
```   306 instance group_prod_def: * :: (group, group) group
```
```   307   inv_prod_def: "\<div> x \<equiv> let (x1, x2) = x in (\<div> x1, \<div> x2)"
```
```   308 by default (simp_all add: split_paired_all group_prod_def invl)
```
```   309
```
```   310 instance group_comm_prod_def: * :: (group_comm, group_comm) group_comm
```
```   311 by default (simp_all add: split_paired_all group_prod_def comm)
```
```   312
```
```   313 definition
```
```   314   "X a b c = (a \<otimes> \<one> \<otimes> b, a \<otimes> \<one> \<otimes> b, [a, b] \<otimes> \<one> \<otimes> [a, b, c])"
```
```   315 definition
```
```   316   "Y a b c = (a, \<div> a) \<otimes> \<one> \<otimes> \<div> (b, \<div> c)"
```
```   317
```
```   318 definition "x1 = X (1::nat) 2 3"
```
```   319 definition "x2 = X (1::int) 2 3"
```
```   320 definition "y2 = Y (1::int) 2 3"
```
```   321
```
```   322 code_gen "op \<otimes>" \<one> inv
```
```   323 code_gen X Y (SML *) (Haskell -)
```
```   324 code_gen x1 x2 y2 (SML *) (Haskell -)
```
```   325
```
```   326 end
```