src/HOL/Quotient_Examples/Int_Pow.thy
author haftmann
Sun, 21 Sep 2014 16:56:11 +0200
changeset 58410 6d46ad54a2ab
parent 57512 cc97b347b301
child 62378 85ed00c1fe7c
permissions -rw-r--r--
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
53653
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
     1
(*  Title:      HOL/Quotient_Examples/Int_Pow.thy
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
     2
    Author:     Ondrej Kuncar
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
     3
    Author:     Lars Noschinski
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
     4
*)
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
     5
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
     6
theory Int_Pow
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
     7
imports Main "~~/src/HOL/Algebra/Group"
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
     8
begin                          
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
     9
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    10
(*
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    11
  This file demonstrates how to restore Lifting/Transfer enviromenent.
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    12
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    13
  We want to define int_pow (a power with an integer exponent) by directly accessing 
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    14
  the representation type "nat * nat" that was used to define integers.
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    15
*)
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    16
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    17
context monoid
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    18
begin
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    19
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    20
(* first some additional lemmas that are missing in monoid *)
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    21
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    22
lemma Units_nat_pow_Units [intro, simp]: 
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    23
  "a \<in> Units G \<Longrightarrow> a (^) (c :: nat) \<in> Units G" by (induct c) auto
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    24
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    25
lemma Units_r_cancel [simp]:
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    26
  "[| z \<in> Units G; x \<in> carrier G; y \<in> carrier G |] ==>
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    27
   (x \<otimes> z = y \<otimes> z) = (x = y)"
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    28
proof
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    29
  assume eq: "x \<otimes> z = y \<otimes> z"
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    30
    and G: "z \<in> Units G"  "x \<in> carrier G"  "y \<in> carrier G"
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    31
  then have "x \<otimes> (z \<otimes> inv z) = y \<otimes> (z \<otimes> inv z)"
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    32
    by (simp add: m_assoc[symmetric] Units_closed del: Units_r_inv)
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    33
  with G show "x = y" by simp
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    34
next
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    35
  assume eq: "x = y"
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    36
    and G: "z \<in> Units G"  "x \<in> carrier G"  "y \<in> carrier G"
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    37
  then show "x \<otimes> z = y \<otimes> z" by simp
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    38
qed
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    39
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    40
lemma inv_mult_units:
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    41
  "[| x \<in> Units G; y \<in> Units G |] ==> inv (x \<otimes> y) = inv y \<otimes> inv x"
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    42
proof -
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    43
  assume G: "x \<in> Units G"  "y \<in> Units G"
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    44
  moreover then have "x \<in> carrier G"  "y \<in> carrier G" by auto
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    45
  ultimately have "inv (x \<otimes> y) \<otimes> (x \<otimes> y) = (inv y \<otimes> inv x) \<otimes> (x \<otimes> y)"
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    46
    by (simp add: m_assoc) (simp add: m_assoc [symmetric])
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    47
  with G show ?thesis by (simp del: Units_l_inv)
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    48
qed
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    49
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    50
lemma mult_same_comm: 
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    51
  assumes [simp, intro]: "a \<in> Units G" 
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    52
  shows "a (^) (m::nat) \<otimes> inv (a (^) (n::nat)) = inv (a (^) n) \<otimes> a (^) m"
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    53
proof (cases "m\<ge>n")
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    54
  have [simp]: "a \<in> carrier G" using `a \<in> _` by (rule Units_closed)
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    55
  case True
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 53682
diff changeset
    56
    then obtain k where *:"m = k + n" and **:"m = n + k" by (metis Nat.le_iff_add add.commute)
53653
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    57
    have "a (^) (m::nat) \<otimes> inv (a (^) (n::nat)) = a (^) k"
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    58
      using * by (auto simp add: nat_pow_mult[symmetric] m_assoc)
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    59
    also have "\<dots> = inv (a (^) n) \<otimes> a (^) m"
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    60
      using ** by (auto simp add: nat_pow_mult[symmetric] m_assoc[symmetric])
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    61
    finally show ?thesis .
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    62
next
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    63
  have [simp]: "a \<in> carrier G" using `a \<in> _` by (rule Units_closed)
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    64
  case False
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    65
    then obtain k where *:"n = k + m" and **:"n = m + k" 
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 53682
diff changeset
    66
      by (metis Nat.le_iff_add add.commute nat_le_linear)
53653
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    67
    have "a (^) (m::nat) \<otimes> inv (a (^) (n::nat)) = inv(a (^) k)"
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    68
      using * by (auto simp add: nat_pow_mult[symmetric] m_assoc[symmetric] inv_mult_units)
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    69
    also have "\<dots> = inv (a (^) n) \<otimes> a (^) m"
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    70
      using ** by (auto simp add: nat_pow_mult[symmetric] m_assoc inv_mult_units)
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    71
    finally show ?thesis .
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    72
qed
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    73
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    74
lemma mult_inv_same_comm: 
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    75
  "a \<in> Units G \<Longrightarrow> inv (a (^) (m::nat)) \<otimes> inv (a (^) (n::nat)) = inv (a (^) n) \<otimes> inv (a (^) m)"
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    76
by (simp add: inv_mult_units[symmetric] nat_pow_mult ac_simps Units_closed)
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    77
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    78
context
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    79
includes int.lifting (* restores Lifting/Transfer for integers *)
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    80
begin
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    81
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    82
lemma int_pow_rsp:
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    83
  assumes eq: "(b::nat) + e = d + c"
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    84
  assumes a_in_G [simp, intro]: "a \<in> Units G"
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    85
  shows "a (^) b \<otimes> inv (a (^) c) = a (^) d \<otimes> inv (a (^) e)"
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    86
proof(cases "b\<ge>c")
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    87
  have [simp]: "a \<in> carrier G" using `a \<in> _` by (rule Units_closed)
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    88
  case True
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 53682
diff changeset
    89
    then obtain n where "b = n + c" by (metis Nat.le_iff_add add.commute)
53653
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    90
    then have "d = n + e" using eq by arith
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    91
    from `b = _` have "a (^) b \<otimes> inv (a (^) c) = a (^) n" 
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    92
      by (auto simp add: nat_pow_mult[symmetric] m_assoc)
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    93
    also from `d = _`  have "\<dots> = a (^) d \<otimes> inv (a (^) e)"   
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    94
      by (auto simp add: nat_pow_mult[symmetric] m_assoc)
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    95
    finally show ?thesis .
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    96
next
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    97
  have [simp]: "a \<in> carrier G" using `a \<in> _` by (rule Units_closed)
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
    98
  case False
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 53682
diff changeset
    99
    then obtain n where "c = n + b" by (metis Nat.le_iff_add add.commute nat_le_linear)
53653
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   100
    then have "e = n + d" using eq by arith
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   101
    from `c = _` have "a (^) b \<otimes> inv (a (^) c) = inv (a (^) n)" 
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   102
      by (auto simp add: nat_pow_mult[symmetric] m_assoc[symmetric] inv_mult_units)
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   103
    also from `e = _` have "\<dots> = a (^) d \<otimes> inv (a (^) e)"   
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   104
      by (auto simp add: nat_pow_mult[symmetric] m_assoc[symmetric] inv_mult_units)
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   105
    finally show ?thesis .
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   106
qed
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   107
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   108
(*
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   109
  This definition is more convinient than the definition in HOL/Algebra/Group because
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   110
  it doesn't contain a test z < 0 when a (^) z is being defined.
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   111
*)
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   112
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   113
lift_definition int_pow :: "('a, 'm) monoid_scheme \<Rightarrow> 'a \<Rightarrow> int \<Rightarrow> 'a" is 
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   114
  "\<lambda>G a (n1, n2). if a \<in> Units G \<and> monoid G then (a (^)\<^bsub>G\<^esub> n1) \<otimes>\<^bsub>G\<^esub> (inv\<^bsub>G\<^esub> (a (^)\<^bsub>G\<^esub> n2)) else \<one>\<^bsub>G\<^esub>" 
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   115
unfolding intrel_def by (auto intro: monoid.int_pow_rsp)
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   116
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   117
(*
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   118
  Thus, for example, the proof of distributivity of int_pow and addition 
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   119
  doesn't require a substantial number of case distinctions.
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   120
*)
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   121
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   122
lemma int_pow_dist:
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   123
  assumes [simp]: "a \<in> Units G"
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   124
  shows "int_pow G a ((n::int) + m) = int_pow G a n \<otimes>\<^bsub>G\<^esub> int_pow G a m"
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   125
proof -
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   126
  {
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   127
    fix k l m :: nat
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   128
    have "a (^) l \<otimes> (inv (a (^) m) \<otimes> inv (a (^) k)) = (a (^) l \<otimes> inv (a (^) k)) \<otimes> inv (a (^) m)" 
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   129
      (is "?lhs = _")
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   130
      by (simp add: mult_inv_same_comm m_assoc Units_closed)
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   131
    also have "\<dots> = (inv (a (^) k) \<otimes> a (^) l) \<otimes> inv (a (^) m)"
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   132
      by (simp add: mult_same_comm)
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   133
    also have "\<dots> = inv (a (^) k) \<otimes> (a (^) l \<otimes> inv (a (^) m))" (is "_ = ?rhs")
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   134
      by (simp add: m_assoc Units_closed)
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   135
    finally have "?lhs = ?rhs" .
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   136
  }
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   137
  then show ?thesis
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   138
    by (transfer fixing: G) (auto simp add: nat_pow_mult[symmetric] inv_mult_units m_assoc Units_closed)
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   139
qed
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   140
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   141
end
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   142
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   143
lifting_update int.lifting
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   144
lifting_forget int.lifting
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   145
a792a504ff22 example using restoring Transfer/Lifting context
kuncar
parents:
diff changeset
   146
end
53682
1b55aeda0e46 include Int_Pow into Quotient_Examples; add end of the theory
kuncar
parents: 53653
diff changeset
   147
1b55aeda0e46 include Int_Pow into Quotient_Examples; add end of the theory
kuncar
parents: 53653
diff changeset
   148
end