src/HOL/FunDef.thy
author krauss
Fri Nov 24 13:44:51 2006 +0100 (2006-11-24)
changeset 21512 3786eb1b69d6
parent 21404 eb85850d3eb7
child 22166 0a50d4db234a
permissions -rw-r--r--
Lemma "fundef_default_value" uses predicate instead of set.
     1 (*  Title:      HOL/FunDef.thy
     2     ID:         $Id$
     3     Author:     Alexander Krauss, TU Muenchen
     4 
     5 A package for general recursive function definitions. 
     6 *)
     7 
     8 theory FunDef
     9 imports Accessible_Part Datatype Recdef
    10 uses 
    11 ("Tools/function_package/sum_tools.ML")
    12 ("Tools/function_package/fundef_common.ML")
    13 ("Tools/function_package/fundef_lib.ML")
    14 ("Tools/function_package/inductive_wrap.ML")
    15 ("Tools/function_package/context_tree.ML")
    16 ("Tools/function_package/fundef_prep.ML")
    17 ("Tools/function_package/fundef_proof.ML")
    18 ("Tools/function_package/termination.ML")
    19 ("Tools/function_package/mutual.ML")
    20 ("Tools/function_package/pattern_split.ML")
    21 ("Tools/function_package/fundef_package.ML")
    22 (*("Tools/function_package/fundef_datatype.ML")*)
    23 ("Tools/function_package/auto_term.ML")
    24 begin
    25 
    26 section {* Wellfoundedness and Accessibility: Predicate versions *}
    27 
    28 
    29 constdefs
    30   wfP         :: "('a \<Rightarrow> 'a \<Rightarrow> bool) => bool"
    31   "wfP(r) == (!P. (!x. (!y. r y x --> P(y)) --> P(x)) --> (!x. P(x)))"
    32 
    33 lemma wfP_induct: 
    34     "[| wfP r;           
    35         !!x.[| ALL y. r y x --> P(y) |] ==> P(x)  
    36      |]  ==>  P(a)"
    37 by (unfold wfP_def, blast)
    38 
    39 lemmas wfP_induct_rule = wfP_induct [rule_format, consumes 1, case_names less]
    40 
    41 definition in_rel_def[simp]:
    42   "in_rel R x y == (x, y) \<in> R"
    43 
    44 lemma wf_in_rel:
    45   "wf R \<Longrightarrow> wfP (in_rel R)"
    46   unfolding wfP_def wf_def in_rel_def .
    47 
    48 
    49 inductive2 accP :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> bool"
    50   for r :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
    51 where
    52   accPI: "(!!y. r y x ==> accP r y) ==> accP r x"
    53 
    54 
    55 theorem accP_induct:
    56   assumes major: "accP r a"
    57   assumes hyp: "!!x. accP r x ==> \<forall>y. r y x --> P y ==> P x"
    58   shows "P a"
    59   apply (rule major [THEN accP.induct])
    60   apply (rule hyp)
    61    apply (rule accPI)
    62    apply fast
    63   apply fast
    64   done
    65 
    66 theorems accP_induct_rule = accP_induct [rule_format, induct set: accP]
    67 
    68 theorem accP_downward: "accP r b ==> r a b ==> accP r a"
    69   apply (erule accP.cases)
    70   apply fast
    71   done
    72 
    73 
    74 lemma accP_subset:
    75   assumes sub: "\<And>x y. R1 x y \<Longrightarrow> R2 x y"
    76   shows "\<And>x. accP R2 x \<Longrightarrow> accP R1 x"
    77 proof-
    78   fix x assume "accP R2 x"
    79   then show "accP R1 x"
    80   proof (induct x)
    81     fix x
    82     assume ih: "\<And>y. R2 y x \<Longrightarrow> accP R1 y"
    83     with sub show "accP R1 x"
    84       by (blast intro:accPI)
    85   qed
    86 qed
    87 
    88 
    89 lemma accP_subset_induct:
    90   assumes subset: "\<And>x. D x \<Longrightarrow> accP R x"
    91     and dcl: "\<And>x z. \<lbrakk>D x; R z x\<rbrakk> \<Longrightarrow> D z"
    92     and "D x"
    93     and istep: "\<And>x. \<lbrakk>D x; (\<And>z. R z x \<Longrightarrow> P z)\<rbrakk> \<Longrightarrow> P x"
    94   shows "P x"
    95 proof -
    96   from subset and `D x` 
    97   have "accP R x" .
    98   then show "P x" using `D x`
    99   proof (induct x)
   100     fix x
   101     assume "D x"
   102       and "\<And>y. R y x \<Longrightarrow> D y \<Longrightarrow> P y"
   103     with dcl and istep show "P x" by blast
   104   qed
   105 qed
   106 
   107 
   108 section {* Definitions with default value *}
   109 
   110 definition
   111   THE_default :: "'a \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> 'a" where
   112   "THE_default d P = (if (\<exists>!x. P x) then (THE x. P x) else d)"
   113 
   114 lemma THE_defaultI': "\<exists>!x. P x \<Longrightarrow> P (THE_default d P)"
   115   by (simp add:theI' THE_default_def)
   116 
   117 lemma THE_default1_equality: 
   118   "\<lbrakk>\<exists>!x. P x; P a\<rbrakk> \<Longrightarrow> THE_default d P = a"
   119   by (simp add:the1_equality THE_default_def)
   120 
   121 lemma THE_default_none:
   122   "\<not>(\<exists>!x. P x) \<Longrightarrow> THE_default d P = d"
   123 by (simp add:THE_default_def)
   124 
   125 
   126 lemma fundef_ex1_existence:
   127 assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
   128 assumes ex1: "\<exists>!y. G x y"
   129 shows "G x (f x)"
   130   by (simp only:f_def, rule THE_defaultI', rule ex1)
   131 
   132 
   133 
   134 
   135 
   136 lemma fundef_ex1_uniqueness:
   137 assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
   138 assumes ex1: "\<exists>!y. G x y"
   139 assumes elm: "G x (h x)"
   140 shows "h x = f x"
   141   by (simp only:f_def, rule THE_default1_equality[symmetric], rule ex1, rule elm)
   142 
   143 lemma fundef_ex1_iff:
   144 assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
   145 assumes ex1: "\<exists>!y. G x y"
   146 shows "(G x y) = (f x = y)"
   147   apply (auto simp:ex1 f_def THE_default1_equality)
   148   by (rule THE_defaultI', rule ex1)
   149 
   150 lemma fundef_default_value:
   151 assumes f_def: "f == (\<lambda>x::'a. THE_default (d x) (\<lambda>y. G x y))"
   152 assumes graph: "\<And>x y. G x y \<Longrightarrow> D x"
   153 assumes "\<not> D x"
   154 shows "f x = d x"
   155 proof -
   156   have "\<not>(\<exists>y. G x y)"
   157   proof
   158     assume "\<exists>y. G x y"
   159     hence "D x" using graph ..
   160     with `\<not> D x` show False ..
   161   qed
   162   hence "\<not>(\<exists>!y. G x y)" by blast
   163   
   164   thus ?thesis
   165     unfolding f_def
   166     by (rule THE_default_none)
   167 qed
   168 
   169 
   170 
   171 section {* Projections *}
   172 consts
   173   lpg::"(('a + 'b) * 'a) set"
   174   rpg::"(('a + 'b) * 'b) set"
   175 
   176 inductive lpg
   177 intros
   178   "(Inl x, x) : lpg"
   179 inductive rpg
   180 intros
   181   "(Inr y, y) : rpg"
   182 
   183 definition "lproj x = (THE y. (x,y) : lpg)"
   184 definition "rproj x = (THE y. (x,y) : rpg)"
   185 
   186 lemma lproj_inl:
   187   "lproj (Inl x) = x"
   188   by (auto simp:lproj_def intro: the_equality lpg.intros elim: lpg.cases)
   189 lemma rproj_inr:
   190   "rproj (Inr x) = x"
   191   by (auto simp:rproj_def intro: the_equality rpg.intros elim: rpg.cases)
   192 
   193 use "Tools/function_package/sum_tools.ML"
   194 use "Tools/function_package/fundef_common.ML"
   195 use "Tools/function_package/fundef_lib.ML"
   196 use "Tools/function_package/inductive_wrap.ML"
   197 use "Tools/function_package/context_tree.ML"
   198 use "Tools/function_package/fundef_prep.ML"
   199 use "Tools/function_package/fundef_proof.ML"
   200 use "Tools/function_package/termination.ML"
   201 use "Tools/function_package/mutual.ML"
   202 use "Tools/function_package/pattern_split.ML"
   203 use "Tools/function_package/auto_term.ML"
   204 use "Tools/function_package/fundef_package.ML"
   205 
   206 setup FundefPackage.setup
   207 
   208 lemmas [fundef_cong] = 
   209   let_cong if_cong image_cong INT_cong UN_cong bex_cong ball_cong imp_cong
   210 
   211 
   212 lemma split_cong[fundef_cong]:
   213   "\<lbrakk> \<And>x y. (x, y) = q \<Longrightarrow> f x y = g x y; p = q \<rbrakk> 
   214   \<Longrightarrow> split f p = split g q"
   215   by (auto simp:split_def)
   216 
   217 
   218 end