src/HOL/Library/Old_Recdef.thy
changeset 44013 5cfc1c36ae97
parent 39302 d7728f65b353
child 44014 88bd7d74a2c1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Library/Old_Recdef.thy	Tue Aug 02 10:36:50 2011 +0200
     1.3 @@ -0,0 +1,214 @@
     1.4 +(*  Title:      HOL/Library/Old_Recdef.thy
     1.5 +    Author:     Konrad Slind and Markus Wenzel, TU Muenchen
     1.6 +*)
     1.7 +
     1.8 +header {* TFL: recursive function definitions *}
     1.9 +
    1.10 +theory Old_Recdef
    1.11 +imports Main
    1.12 +uses
    1.13 +  ("~~/src/HOL/Tools/TFL/casesplit.ML")
    1.14 +  ("~~/src/HOL/Tools/TFL/utils.ML")
    1.15 +  ("~~/src/HOL/Tools/TFL/usyntax.ML")
    1.16 +  ("~~/src/HOL/Tools/TFL/dcterm.ML")
    1.17 +  ("~~/src/HOL/Tools/TFL/thms.ML")
    1.18 +  ("~~/src/HOL/Tools/TFL/rules.ML")
    1.19 +  ("~~/src/HOL/Tools/TFL/thry.ML")
    1.20 +  ("~~/src/HOL/Tools/TFL/tfl.ML")
    1.21 +  ("~~/src/HOL/Tools/TFL/post.ML")
    1.22 +  ("~~/src/HOL/Tools/recdef.ML")
    1.23 +begin
    1.24 +
    1.25 +inductive
    1.26 +  wfrec_rel :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => 'a => 'b => bool"
    1.27 +  for R :: "('a * 'a) set"
    1.28 +  and F :: "('a => 'b) => 'a => 'b"
    1.29 +where
    1.30 +  wfrecI: "ALL z. (z, x) : R --> wfrec_rel R F z (g z) ==>
    1.31 +            wfrec_rel R F x (F g x)"
    1.32 +
    1.33 +definition
    1.34 +  cut        :: "('a => 'b) => ('a * 'a)set => 'a => 'a => 'b" where
    1.35 +  "cut f r x == (%y. if (y,x):r then f y else undefined)"
    1.36 +
    1.37 +definition
    1.38 +  adm_wf :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => bool" where
    1.39 +  "adm_wf R F == ALL f g x.
    1.40 +     (ALL z. (z, x) : R --> f z = g z) --> F f x = F g x"
    1.41 +
    1.42 +definition
    1.43 +  wfrec :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => 'a => 'b" where
    1.44 +  "wfrec R F == %x. THE y. wfrec_rel R (%f x. F (cut f R x) x) x y"
    1.45 +
    1.46 +subsection{*Well-Founded Recursion*}
    1.47 +
    1.48 +text{*cut*}
    1.49 +
    1.50 +lemma cuts_eq: "(cut f r x = cut g r x) = (ALL y. (y,x):r --> f(y)=g(y))"
    1.51 +by (simp add: fun_eq_iff cut_def)
    1.52 +
    1.53 +lemma cut_apply: "(x,a):r ==> (cut f r a)(x) = f(x)"
    1.54 +by (simp add: cut_def)
    1.55 +
    1.56 +text{*Inductive characterization of wfrec combinator; for details see:
    1.57 +John Harrison, "Inductive definitions: automation and application"*}
    1.58 +
    1.59 +lemma wfrec_unique: "[| adm_wf R F; wf R |] ==> EX! y. wfrec_rel R F x y"
    1.60 +apply (simp add: adm_wf_def)
    1.61 +apply (erule_tac a=x in wf_induct)
    1.62 +apply (rule ex1I)
    1.63 +apply (rule_tac g = "%x. THE y. wfrec_rel R F x y" in wfrec_rel.wfrecI)
    1.64 +apply (fast dest!: theI')
    1.65 +apply (erule wfrec_rel.cases, simp)
    1.66 +apply (erule allE, erule allE, erule allE, erule mp)
    1.67 +apply (fast intro: the_equality [symmetric])
    1.68 +done
    1.69 +
    1.70 +lemma adm_lemma: "adm_wf R (%f x. F (cut f R x) x)"
    1.71 +apply (simp add: adm_wf_def)
    1.72 +apply (intro strip)
    1.73 +apply (rule cuts_eq [THEN iffD2, THEN subst], assumption)
    1.74 +apply (rule refl)
    1.75 +done
    1.76 +
    1.77 +lemma wfrec: "wf(r) ==> wfrec r H a = H (cut (wfrec r H) r a) a"
    1.78 +apply (simp add: wfrec_def)
    1.79 +apply (rule adm_lemma [THEN wfrec_unique, THEN the1_equality], assumption)
    1.80 +apply (rule wfrec_rel.wfrecI)
    1.81 +apply (intro strip)
    1.82 +apply (erule adm_lemma [THEN wfrec_unique, THEN theI'])
    1.83 +done
    1.84 +
    1.85 +
    1.86 +text{** This form avoids giant explosions in proofs.  NOTE USE OF ==*}
    1.87 +lemma def_wfrec: "[| f==wfrec r H;  wf(r) |] ==> f(a) = H (cut f r a) a"
    1.88 +apply auto
    1.89 +apply (blast intro: wfrec)
    1.90 +done
    1.91 +
    1.92 +
    1.93 +subsection {* Nitpick setup *}
    1.94 +
    1.95 +axiomatization wf_wfrec :: "('a \<times> 'a \<Rightarrow> bool) \<Rightarrow> (('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b"
    1.96 +
    1.97 +definition wf_wfrec' :: "('a \<times> 'a \<Rightarrow> bool) \<Rightarrow> (('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b" where
    1.98 +[nitpick_simp]: "wf_wfrec' R F x = F (cut (wf_wfrec R F) R x) x"
    1.99 +
   1.100 +definition wfrec' ::  "('a \<times> 'a \<Rightarrow> bool) \<Rightarrow> (('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b" where
   1.101 +"wfrec' R F x \<equiv> if wf R then wf_wfrec' R F x
   1.102 +                else THE y. wfrec_rel R (%f x. F (cut f R x) x) x y"
   1.103 +
   1.104 +setup {*
   1.105 +  Nitpick_HOL.register_ersatz_global
   1.106 +    [(@{const_name wf_wfrec}, @{const_name wf_wfrec'}),
   1.107 +     (@{const_name wfrec}, @{const_name wfrec'})]
   1.108 +*}
   1.109 +
   1.110 +hide_const (open) wf_wfrec wf_wfrec' wfrec'
   1.111 +hide_fact (open) wf_wfrec'_def wfrec'_def
   1.112 +
   1.113 +
   1.114 +subsection {* Lemmas for TFL *}
   1.115 +
   1.116 +lemma tfl_wf_induct: "ALL R. wf R -->  
   1.117 +       (ALL P. (ALL x. (ALL y. (y,x):R --> P y) --> P x) --> (ALL x. P x))"
   1.118 +apply clarify
   1.119 +apply (rule_tac r = R and P = P and a = x in wf_induct, assumption, blast)
   1.120 +done
   1.121 +
   1.122 +lemma tfl_cut_apply: "ALL f R. (x,a):R --> (cut f R a)(x) = f(x)"
   1.123 +apply clarify
   1.124 +apply (rule cut_apply, assumption)
   1.125 +done
   1.126 +
   1.127 +lemma tfl_wfrec:
   1.128 +     "ALL M R f. (f=wfrec R M) --> wf R --> (ALL x. f x = M (cut f R x) x)"
   1.129 +apply clarify
   1.130 +apply (erule wfrec)
   1.131 +done
   1.132 +
   1.133 +lemma tfl_eq_True: "(x = True) --> x"
   1.134 +  by blast
   1.135 +
   1.136 +lemma tfl_rev_eq_mp: "(x = y) --> y --> x";
   1.137 +  by blast
   1.138 +
   1.139 +lemma tfl_simp_thm: "(x --> y) --> (x = x') --> (x' --> y)"
   1.140 +  by blast
   1.141 +
   1.142 +lemma tfl_P_imp_P_iff_True: "P ==> P = True"
   1.143 +  by blast
   1.144 +
   1.145 +lemma tfl_imp_trans: "(A --> B) ==> (B --> C) ==> (A --> C)"
   1.146 +  by blast
   1.147 +
   1.148 +lemma tfl_disj_assoc: "(a \<or> b) \<or> c == a \<or> (b \<or> c)"
   1.149 +  by simp
   1.150 +
   1.151 +lemma tfl_disjE: "P \<or> Q ==> P --> R ==> Q --> R ==> R"
   1.152 +  by blast
   1.153 +
   1.154 +lemma tfl_exE: "\<exists>x. P x ==> \<forall>x. P x --> Q ==> Q"
   1.155 +  by blast
   1.156 +
   1.157 +use "~~/src/HOL/Tools/TFL/casesplit.ML"
   1.158 +use "~~/src/HOL/Tools/TFL/utils.ML"
   1.159 +use "~~/src/HOL/Tools/TFL/usyntax.ML"
   1.160 +use "~~/src/HOL/Tools/TFL/dcterm.ML"
   1.161 +use "~~/src/HOL/Tools/TFL/thms.ML"
   1.162 +use "~~/src/HOL/Tools/TFL/rules.ML"
   1.163 +use "~~/src/HOL/Tools/TFL/thry.ML"
   1.164 +use "~~/src/HOL/Tools/TFL/tfl.ML"
   1.165 +use "~~/src/HOL/Tools/TFL/post.ML"
   1.166 +use "~~/src/HOL/Tools/recdef.ML"
   1.167 +setup Recdef.setup
   1.168 +
   1.169 +text {*Wellfoundedness of @{text same_fst}*}
   1.170 +
   1.171 +definition
   1.172 + same_fst :: "('a => bool) => ('a => ('b * 'b)set) => (('a*'b)*('a*'b))set"
   1.173 +where
   1.174 +    "same_fst P R == {((x',y'),(x,y)) . x'=x & P x & (y',y) : R x}"
   1.175 +   --{*For @{text rec_def} declarations where the first n parameters
   1.176 +       stay unchanged in the recursive call. *}
   1.177 +
   1.178 +lemma same_fstI [intro!]:
   1.179 +     "[| P x; (y',y) : R x |] ==> ((x,y'),(x,y)) : same_fst P R"
   1.180 +by (simp add: same_fst_def)
   1.181 +
   1.182 +lemma wf_same_fst:
   1.183 +  assumes prem: "(!!x. P x ==> wf(R x))"
   1.184 +  shows "wf(same_fst P R)"
   1.185 +apply (simp cong del: imp_cong add: wf_def same_fst_def)
   1.186 +apply (intro strip)
   1.187 +apply (rename_tac a b)
   1.188 +apply (case_tac "wf (R a)")
   1.189 + apply (erule_tac a = b in wf_induct, blast)
   1.190 +apply (blast intro: prem)
   1.191 +done
   1.192 +
   1.193 +text {*Rule setup*}
   1.194 +
   1.195 +lemmas [recdef_simp] =
   1.196 +  inv_image_def
   1.197 +  measure_def
   1.198 +  lex_prod_def
   1.199 +  same_fst_def
   1.200 +  less_Suc_eq [THEN iffD2]
   1.201 +
   1.202 +lemmas [recdef_cong] =
   1.203 +  if_cong let_cong image_cong INT_cong UN_cong bex_cong ball_cong imp_cong
   1.204 +  map_cong filter_cong takeWhile_cong dropWhile_cong foldl_cong foldr_cong 
   1.205 +
   1.206 +lemmas [recdef_wf] =
   1.207 +  wf_trancl
   1.208 +  wf_less_than
   1.209 +  wf_lex_prod
   1.210 +  wf_inv_image
   1.211 +  wf_measure
   1.212 +  wf_measures
   1.213 +  wf_pred_nat
   1.214 +  wf_same_fst
   1.215 +  wf_empty
   1.216 +
   1.217 +end