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