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