src/HOL/FunDef.thy
author krauss
Fri Apr 25 16:28:06 2008 +0200 (2008-04-25)
changeset 26749 397a1aeede7d
parent 26748 4d51ddd6aa5c
child 26875 e18574413bc4
permissions -rw-r--r--
* New attribute "termination_simp": Simp rules for termination proofs
* General lemmas about list_size
     1 (*  Title:      HOL/FunDef.thy
     2     ID:         $Id$
     3     Author:     Alexander Krauss, TU Muenchen
     4 *)
     5 
     6 header {* General recursive function definitions *}
     7 
     8 theory FunDef
     9 imports Wellfounded
    10 uses
    11   ("Tools/function_package/fundef_lib.ML")
    12   ("Tools/function_package/fundef_common.ML")
    13   ("Tools/function_package/inductive_wrap.ML")
    14   ("Tools/function_package/context_tree.ML")
    15   ("Tools/function_package/fundef_core.ML")
    16   ("Tools/function_package/sum_tree.ML")
    17   ("Tools/function_package/mutual.ML")
    18   ("Tools/function_package/pattern_split.ML")
    19   ("Tools/function_package/fundef_package.ML")
    20   ("Tools/function_package/auto_term.ML")
    21   ("Tools/function_package/induction_scheme.ML")
    22   ("Tools/function_package/lexicographic_order.ML")
    23   ("Tools/function_package/fundef_datatype.ML")
    24 begin
    25 
    26 text {* Definitions with default value. *}
    27 
    28 definition
    29   THE_default :: "'a \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> 'a" where
    30   "THE_default d P = (if (\<exists>!x. P x) then (THE x. P x) else d)"
    31 
    32 lemma THE_defaultI': "\<exists>!x. P x \<Longrightarrow> P (THE_default d P)"
    33   by (simp add: theI' THE_default_def)
    34 
    35 lemma THE_default1_equality:
    36     "\<lbrakk>\<exists>!x. P x; P a\<rbrakk> \<Longrightarrow> THE_default d P = a"
    37   by (simp add: the1_equality THE_default_def)
    38 
    39 lemma THE_default_none:
    40     "\<not>(\<exists>!x. P x) \<Longrightarrow> THE_default d P = d"
    41   by (simp add:THE_default_def)
    42 
    43 
    44 lemma fundef_ex1_existence:
    45   assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
    46   assumes ex1: "\<exists>!y. G x y"
    47   shows "G x (f x)"
    48   apply (simp only: f_def)
    49   apply (rule THE_defaultI')
    50   apply (rule ex1)
    51   done
    52 
    53 lemma fundef_ex1_uniqueness:
    54   assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
    55   assumes ex1: "\<exists>!y. G x y"
    56   assumes elm: "G x (h x)"
    57   shows "h x = f x"
    58   apply (simp only: f_def)
    59   apply (rule THE_default1_equality [symmetric])
    60    apply (rule ex1)
    61   apply (rule elm)
    62   done
    63 
    64 lemma fundef_ex1_iff:
    65   assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
    66   assumes ex1: "\<exists>!y. G x y"
    67   shows "(G x y) = (f x = y)"
    68   apply (auto simp:ex1 f_def THE_default1_equality)
    69   apply (rule THE_defaultI')
    70   apply (rule ex1)
    71   done
    72 
    73 lemma fundef_default_value:
    74   assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
    75   assumes graph: "\<And>x y. G x y \<Longrightarrow> D x"
    76   assumes "\<not> D x"
    77   shows "f x = d x"
    78 proof -
    79   have "\<not>(\<exists>y. G x y)"
    80   proof
    81     assume "\<exists>y. G x y"
    82     hence "D x" using graph ..
    83     with `\<not> D x` show False ..
    84   qed
    85   hence "\<not>(\<exists>!y. G x y)" by blast
    86 
    87   thus ?thesis
    88     unfolding f_def
    89     by (rule THE_default_none)
    90 qed
    91 
    92 definition in_rel_def[simp]:
    93   "in_rel R x y == (x, y) \<in> R"
    94 
    95 lemma wf_in_rel:
    96   "wf R \<Longrightarrow> wfP (in_rel R)"
    97   by (simp add: wfP_def)
    98 
    99 
   100 use "Tools/function_package/fundef_lib.ML"
   101 use "Tools/function_package/fundef_common.ML"
   102 use "Tools/function_package/inductive_wrap.ML"
   103 use "Tools/function_package/context_tree.ML"
   104 use "Tools/function_package/fundef_core.ML"
   105 use "Tools/function_package/sum_tree.ML"
   106 use "Tools/function_package/mutual.ML"
   107 use "Tools/function_package/pattern_split.ML"
   108 use "Tools/function_package/auto_term.ML"
   109 use "Tools/function_package/fundef_package.ML"
   110 use "Tools/function_package/induction_scheme.ML"
   111 use "Tools/function_package/lexicographic_order.ML"
   112 use "Tools/function_package/fundef_datatype.ML"
   113 
   114 setup {* 
   115   FundefPackage.setup 
   116   #> InductionScheme.setup
   117   #> LexicographicOrder.setup 
   118   #> FundefDatatype.setup
   119 *}
   120 
   121 lemma let_cong [fundef_cong]:
   122   "M = N \<Longrightarrow> (\<And>x. x = N \<Longrightarrow> f x = g x) \<Longrightarrow> Let M f = Let N g"
   123   unfolding Let_def by blast
   124 
   125 lemmas [fundef_cong] =
   126   if_cong image_cong INT_cong UN_cong
   127   bex_cong ball_cong imp_cong
   128 
   129 lemma split_cong [fundef_cong]:
   130   "(\<And>x y. (x, y) = q \<Longrightarrow> f x y = g x y) \<Longrightarrow> p = q
   131     \<Longrightarrow> split f p = split g q"
   132   by (auto simp: split_def)
   133 
   134 lemma comp_cong [fundef_cong]:
   135   "f (g x) = f' (g' x') \<Longrightarrow> (f o g) x = (f' o g') x'"
   136   unfolding o_apply .
   137 
   138 lemma termination_basic_simps[termination_simp]:
   139   "x < y \<Longrightarrow> x < Suc y"
   140   "x < (y::nat) \<Longrightarrow> x < y + z" 
   141   "x < z \<Longrightarrow> x < y + z"
   142 by arith+
   143 
   144 end