src/HOL/Relation_Power.thy
author wenzelm
Wed Sep 17 21:27:14 2008 +0200 (2008-09-17)
changeset 28263 69eaa97e7e96
parent 26799 5bd38256ce5b
child 28517 dd46786d4f95
permissions -rw-r--r--
moved global ML bindings to global place;
nipkow@10213
     1
(*  Title:      HOL/Relation_Power.thy
nipkow@10213
     2
    ID:         $Id$
nipkow@10213
     3
    Author:     Tobias Nipkow
nipkow@10213
     4
    Copyright   1996  TU Muenchen
paulson@15410
     5
*)
nipkow@11306
     6
paulson@15410
     7
header{*Powers of Relations and Functions*}
nipkow@10213
     8
nipkow@15131
     9
theory Relation_Power
haftmann@26072
    10
imports Power Transitive_Closure
nipkow@15131
    11
begin
nipkow@10213
    12
nipkow@10213
    13
instance
berghofe@26799
    14
  "fun" :: (type, type) power ..
berghofe@26799
    15
      --{* only type @{typ "'a => 'a"} should be in class @{text power}!*}
nipkow@10213
    16
haftmann@25861
    17
overloading
haftmann@25861
    18
  relpow \<equiv> "power \<Colon> ('a \<times> 'a) set \<Rightarrow> nat \<Rightarrow> ('a \<times> 'a) set"  (unchecked)
haftmann@25861
    19
begin
haftmann@25861
    20
haftmann@25861
    21
text {* @{text "R ^ n = R O ... O R"}, the n-fold composition of @{text R} *}
nipkow@10213
    22
haftmann@25861
    23
primrec relpow where
haftmann@25861
    24
  "(R \<Colon> ('a \<times> 'a) set)  ^ 0 = Id"
haftmann@25861
    25
  | "(R \<Colon> ('a \<times> 'a) set) ^ Suc n = R O (R ^ n)"
haftmann@25861
    26
haftmann@25861
    27
end
nipkow@11305
    28
haftmann@25861
    29
overloading
haftmann@25861
    30
  funpow \<equiv> "power \<Colon>  ('a \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a" (unchecked)
haftmann@25861
    31
begin
haftmann@25861
    32
haftmann@25861
    33
text {* @{text "f ^ n = f o ... o f"}, the n-fold composition of @{text f} *}
haftmann@25861
    34
haftmann@25861
    35
primrec funpow where
haftmann@25861
    36
  "(f \<Colon> 'a \<Rightarrow> 'a) ^ 0 = id"
haftmann@25861
    37
  | "(f \<Colon> 'a \<Rightarrow> 'a) ^ Suc n = f o (f ^ n)"
haftmann@25861
    38
haftmann@25861
    39
end
nipkow@11305
    40
paulson@15410
    41
text{*WARNING: due to the limits of Isabelle's type classes, exponentiation on
paulson@15410
    42
functions and relations has too general a domain, namely @{typ "('a * 'b)set"}
paulson@15410
    43
and @{typ "'a => 'b"}.  Explicit type constraints may therefore be necessary.
paulson@15410
    44
For example, @{term "range(f^n) = A"} and @{term "Range(R^n) = B"} need
paulson@15410
    45
constraints.*}
paulson@15410
    46
haftmann@21414
    47
text {*
haftmann@21414
    48
  Circumvent this problem for code generation:
haftmann@21414
    49
*}
haftmann@21414
    50
haftmann@25861
    51
primrec
haftmann@25861
    52
  fun_pow :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@22737
    53
where
haftmann@25861
    54
  "fun_pow 0 f = id"
haftmann@25861
    55
  | "fun_pow (Suc n) f = f o fun_pow n f"
haftmann@21414
    56
haftmann@25861
    57
lemma funpow_fun_pow [code inline]: "f ^ n = fun_pow n f"
haftmann@25861
    58
  unfolding funpow_def fun_pow_def ..
haftmann@21414
    59
nipkow@15112
    60
lemma funpow_add: "f ^ (m+n) = f^m o f^n"
wenzelm@18398
    61
  by (induct m) simp_all
nipkow@15112
    62
nipkow@18049
    63
lemma funpow_swap1: "f((f^n) x) = (f^n)(f x)"
nipkow@18049
    64
proof -
nipkow@18049
    65
  have "f((f^n) x) = (f^(n+1)) x" by simp
wenzelm@18398
    66
  also have "\<dots>  = (f^n o f^1) x" by (simp only: funpow_add)
nipkow@18049
    67
  also have "\<dots> = (f^n)(f x)" by simp
nipkow@18049
    68
  finally show ?thesis .
nipkow@18049
    69
qed
nipkow@18049
    70
wenzelm@18398
    71
lemma rel_pow_1 [simp]:
wenzelm@18398
    72
  fixes R :: "('a*'a)set"
wenzelm@18398
    73
  shows "R^1 = R"
wenzelm@18398
    74
  by simp
paulson@15410
    75
paulson@15410
    76
lemma rel_pow_0_I: "(x,x) : R^0"
wenzelm@18398
    77
  by simp
paulson@15410
    78
paulson@15410
    79
lemma rel_pow_Suc_I: "[| (x,y) : R^n; (y,z):R |] ==> (x,z):R^(Suc n)"
wenzelm@18398
    80
  by auto
paulson@15410
    81
wenzelm@18398
    82
lemma rel_pow_Suc_I2:
wenzelm@18398
    83
    "(x, y) : R \<Longrightarrow> (y, z) : R^n \<Longrightarrow> (x,z) : R^(Suc n)"
wenzelm@20503
    84
  apply (induct n arbitrary: z)
wenzelm@18398
    85
   apply simp
wenzelm@18398
    86
  apply fastsimp
wenzelm@18398
    87
  done
paulson@15410
    88
paulson@15410
    89
lemma rel_pow_0_E: "[| (x,y) : R^0; x=y ==> P |] ==> P"
wenzelm@18398
    90
  by simp
paulson@15410
    91
wenzelm@18398
    92
lemma rel_pow_Suc_E:
wenzelm@18398
    93
    "[| (x,z) : R^(Suc n);  !!y. [| (x,y) : R^n; (y,z) : R |] ==> P |] ==> P"
wenzelm@18398
    94
  by auto
paulson@15410
    95
wenzelm@18398
    96
lemma rel_pow_E:
wenzelm@18398
    97
    "[| (x,z) : R^n;  [| n=0; x = z |] ==> P;
wenzelm@18398
    98
        !!y m. [| n = Suc m; (x,y) : R^m; (y,z) : R |] ==> P
paulson@15410
    99
     |] ==> P"
wenzelm@18398
   100
  by (cases n) auto
paulson@15410
   101
wenzelm@18398
   102
lemma rel_pow_Suc_D2:
wenzelm@18398
   103
    "(x, z) : R^(Suc n) \<Longrightarrow> (\<exists>y. (x,y) : R & (y,z) : R^n)"
wenzelm@20503
   104
  apply (induct n arbitrary: x z)
wenzelm@18398
   105
   apply (blast intro: rel_pow_0_I elim: rel_pow_0_E rel_pow_Suc_E)
wenzelm@18398
   106
  apply (blast intro: rel_pow_Suc_I elim: rel_pow_0_E rel_pow_Suc_E)
wenzelm@18398
   107
  done
paulson@15410
   108
paulson@15410
   109
lemma rel_pow_Suc_D2':
wenzelm@18398
   110
    "\<forall>x y z. (x,y) : R^n & (y,z) : R --> (\<exists>w. (x,w) : R & (w,z) : R^n)"
wenzelm@18398
   111
  by (induct n) (simp_all, blast)
paulson@15410
   112
wenzelm@18398
   113
lemma rel_pow_E2:
wenzelm@18398
   114
    "[| (x,z) : R^n;  [| n=0; x = z |] ==> P;
wenzelm@18398
   115
        !!y m. [| n = Suc m; (x,y) : R; (y,z) : R^m |] ==> P
paulson@15410
   116
     |] ==> P"
wenzelm@18398
   117
  apply (case_tac n, simp)
wenzelm@18398
   118
  apply (cut_tac n=nat and R=R in rel_pow_Suc_D2', simp, blast)
wenzelm@18398
   119
  done
paulson@15410
   120
paulson@15410
   121
lemma rtrancl_imp_UN_rel_pow: "!!p. p:R^* ==> p : (UN n. R^n)"
wenzelm@18398
   122
  apply (simp only: split_tupled_all)
wenzelm@18398
   123
  apply (erule rtrancl_induct)
wenzelm@18398
   124
   apply (blast intro: rel_pow_0_I rel_pow_Suc_I)+
wenzelm@18398
   125
  done
paulson@15410
   126
paulson@15410
   127
lemma rel_pow_imp_rtrancl: "!!p. p:R^n ==> p:R^*"
wenzelm@18398
   128
  apply (simp only: split_tupled_all)
wenzelm@18398
   129
  apply (induct n)
wenzelm@18398
   130
   apply (blast intro: rtrancl_refl elim: rel_pow_0_E)
wenzelm@18398
   131
  apply (blast elim: rel_pow_Suc_E intro: rtrancl_into_rtrancl)
wenzelm@18398
   132
  done
paulson@15410
   133
paulson@15410
   134
lemma rtrancl_is_UN_rel_pow: "R^* = (UN n. R^n)"
wenzelm@18398
   135
  by (blast intro: rtrancl_imp_UN_rel_pow rel_pow_imp_rtrancl)
paulson@15410
   136
kleing@25295
   137
lemma trancl_power:
kleing@25295
   138
  "x \<in> r^+ = (\<exists>n > 0. x \<in> r^n)"
kleing@25295
   139
  apply (cases x)
kleing@25295
   140
  apply simp
kleing@25295
   141
  apply (rule iffI)
kleing@25295
   142
   apply (drule tranclD2)
kleing@25295
   143
   apply (clarsimp simp: rtrancl_is_UN_rel_pow)
kleing@25295
   144
   apply (rule_tac x="Suc x" in exI)
kleing@25295
   145
   apply (clarsimp simp: rel_comp_def)
kleing@25295
   146
   apply fastsimp
kleing@25295
   147
  apply clarsimp
kleing@25295
   148
  apply (case_tac n, simp)
kleing@25295
   149
  apply clarsimp
kleing@25295
   150
  apply (drule rel_pow_imp_rtrancl)
kleing@25295
   151
  apply fastsimp
kleing@25295
   152
  done
paulson@15410
   153
wenzelm@18398
   154
lemma single_valued_rel_pow:
wenzelm@18398
   155
    "!!r::('a * 'a)set. single_valued r ==> single_valued (r^n)"
wenzelm@18398
   156
  apply (rule single_valuedI)
wenzelm@18398
   157
  apply (induct n)
wenzelm@18398
   158
   apply simp
wenzelm@18398
   159
  apply (fast dest: single_valuedD elim: rel_pow_Suc_E)
wenzelm@18398
   160
  done
paulson@15410
   161
paulson@15410
   162
ML
paulson@15410
   163
{*
paulson@15410
   164
val funpow_add = thm "funpow_add";
paulson@15410
   165
val rel_pow_1 = thm "rel_pow_1";
paulson@15410
   166
val rel_pow_0_I = thm "rel_pow_0_I";
paulson@15410
   167
val rel_pow_Suc_I = thm "rel_pow_Suc_I";
paulson@15410
   168
val rel_pow_Suc_I2 = thm "rel_pow_Suc_I2";
paulson@15410
   169
val rel_pow_0_E = thm "rel_pow_0_E";
paulson@15410
   170
val rel_pow_Suc_E = thm "rel_pow_Suc_E";
paulson@15410
   171
val rel_pow_E = thm "rel_pow_E";
paulson@15410
   172
val rel_pow_Suc_D2 = thm "rel_pow_Suc_D2";
paulson@15410
   173
val rel_pow_Suc_D2 = thm "rel_pow_Suc_D2";
paulson@15410
   174
val rel_pow_E2 = thm "rel_pow_E2";
paulson@15410
   175
val rtrancl_imp_UN_rel_pow = thm "rtrancl_imp_UN_rel_pow";
paulson@15410
   176
val rel_pow_imp_rtrancl = thm "rel_pow_imp_rtrancl";
paulson@15410
   177
val rtrancl_is_UN_rel_pow = thm "rtrancl_is_UN_rel_pow";
paulson@15410
   178
val single_valued_rel_pow = thm "single_valued_rel_pow";
paulson@15410
   179
*}
paulson@15410
   180
nipkow@10213
   181
end