src/HOL/ex/Functions.thy
changeset 70468 8406a2c296e0
parent 66453 cc19f7ca2ed6
equal deleted inserted replaced
70467:b32d571f1190 70468:8406a2c296e0
    92 qed
    92 qed
    93 
    93 
    94 text \<open>Now trivial (even though it does not belong here):\<close>
    94 text \<open>Now trivial (even though it does not belong here):\<close>
    95 lemma "f91 n = (if 100 < n then n - 10 else 91)"
    95 lemma "f91 n = (if 100 < n then n - 10 else 91)"
    96   by (induct n rule: f91.induct) auto
    96   by (induct n rule: f91.induct) auto
       
    97 
       
    98 
       
    99 subsubsection \<open>Here comes Takeuchi's function\<close>
       
   100 
       
   101 definition tak_m1 where "tak_m1 = (\<lambda>(x,y,z). if x \<le> y then 0 else 1)"
       
   102 definition tak_m2 where "tak_m2 = (\<lambda>(x,y,z). nat (Max {x, y, z} - Min {x, y, z}))"
       
   103 definition tak_m3 where "tak_m3 = (\<lambda>(x,y,z). nat (x - Min {x, y, z}))"
       
   104 
       
   105 function tak :: "int \<Rightarrow> int \<Rightarrow> int \<Rightarrow> int" where
       
   106   "tak x y z = (if x \<le> y then y else tak (tak (x-1) y z) (tak (y-1) z x) (tak (z-1) x y))"
       
   107   by auto
       
   108 
       
   109 lemma tak_pcorrect:
       
   110   "tak_dom (x, y, z) \<Longrightarrow> tak x y z = (if x \<le> y then y else if y \<le> z then z else x)"
       
   111   by (induction x y z rule: tak.pinduct) (auto simp: tak.psimps)
       
   112 
       
   113 termination
       
   114   by (relation "tak_m1 <*mlex*> tak_m2 <*mlex*> tak_m3 <*mlex*> {}")
       
   115      (auto simp: mlex_iff wf_mlex tak_pcorrect tak_m1_def tak_m2_def tak_m3_def min_def max_def)
       
   116 
       
   117 theorem tak_correct: "tak x y z = (if x \<le> y then y else if y \<le> z then z else x)"
       
   118   by (induction x y z rule: tak.induct) auto
    97 
   119 
    98 
   120 
    99 subsection \<open>More general patterns\<close>
   121 subsection \<open>More general patterns\<close>
   100 
   122 
   101 subsubsection \<open>Overlapping patterns\<close>
   123 subsubsection \<open>Overlapping patterns\<close>
   242 fun_cases pred0E[elim]: "pred n = 0"
   264 fun_cases pred0E[elim]: "pred n = 0"
   243 
   265 
   244 lemma "pred n = 0 \<Longrightarrow> n = 0 \<or> n = Suc 0"
   266 lemma "pred n = 0 \<Longrightarrow> n = 0 \<or> n = Suc 0"
   245   by (erule pred0E) metis+
   267   by (erule pred0E) metis+
   246 
   268 
   247 
       
   248 text \<open>
   269 text \<open>
   249   Other expressions on the right-hand side also work, but whether the
   270   Other expressions on the right-hand side also work, but whether the
   250   generated rule is useful depends on how well the simplifier can
   271   generated rule is useful depends on how well the simplifier can
   251   simplify it. This example works well:
   272   simplify it. This example works well:
   252 \<close>
   273 \<close>