src/HOL/Wfrec.thy
 changeset 55016 9fc7e7753d86 parent 54482 a2874c8b3558 child 55017 2df6ad1dbd66
```     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/HOL/Wfrec.thy	Thu Jan 16 15:47:33 2014 +0100
1.3 @@ -0,0 +1,121 @@
1.4 +(*  Title:      HOL/Library/Wfrec.thy
1.5 +    Author:     Tobias Nipkow
1.6 +    Author:     Lawrence C Paulson
1.8 +*)
1.9 +
1.10 +header {* Well-Founded Recursion Combinator *}
1.11 +
1.12 +theory Wfrec
1.13 +imports Main
1.14 +begin
1.15 +
1.16 +inductive
1.17 +  wfrec_rel :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => 'a => 'b => bool"
1.18 +  for R :: "('a * 'a) set"
1.19 +  and F :: "('a => 'b) => 'a => 'b"
1.20 +where
1.21 +  wfrecI: "ALL z. (z, x) : R --> wfrec_rel R F z (g z) ==>
1.22 +            wfrec_rel R F x (F g x)"
1.23 +
1.24 +definition
1.25 +  cut        :: "('a => 'b) => ('a * 'a)set => 'a => 'a => 'b" where
1.26 +  "cut f r x == (%y. if (y,x):r then f y else undefined)"
1.27 +
1.28 +definition
1.29 +  adm_wf :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => bool" where
1.30 +  "adm_wf R F == ALL f g x.
1.31 +     (ALL z. (z, x) : R --> f z = g z) --> F f x = F g x"
1.32 +
1.33 +definition
1.34 +  wfrec :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => 'a => 'b" where
1.35 +  "wfrec R F == %x. THE y. wfrec_rel R (%f x. F (cut f R x) x) x y"
1.36 +
1.37 +lemma cuts_eq: "(cut f r x = cut g r x) = (ALL y. (y,x):r --> f(y)=g(y))"
1.38 +by (simp add: fun_eq_iff cut_def)
1.39 +
1.40 +lemma cut_apply: "(x,a):r ==> (cut f r a)(x) = f(x)"
1.42 +
1.43 +text{*Inductive characterization of wfrec combinator; for details see:
1.44 +John Harrison, "Inductive definitions: automation and application"*}
1.45 +
1.46 +lemma wfrec_unique: "[| adm_wf R F; wf R |] ==> EX! y. wfrec_rel R F x y"
1.48 +apply (erule_tac a=x in wf_induct)
1.49 +apply (rule ex1I)
1.50 +apply (rule_tac g = "%x. THE y. wfrec_rel R F x y" in wfrec_rel.wfrecI)
1.51 +apply (fast dest!: theI')
1.52 +apply (erule wfrec_rel.cases, simp)
1.53 +apply (erule allE, erule allE, erule allE, erule mp)
1.54 +apply (blast intro: the_equality [symmetric])
1.55 +done
1.56 +
1.57 +lemma adm_lemma: "adm_wf R (%f x. F (cut f R x) x)"
1.59 +apply (intro strip)
1.60 +apply (rule cuts_eq [THEN iffD2, THEN subst], assumption)
1.61 +apply (rule refl)
1.62 +done
1.63 +
1.64 +lemma wfrec: "wf(r) ==> wfrec r H a = H (cut (wfrec r H) r a) a"
1.66 +apply (rule adm_lemma [THEN wfrec_unique, THEN the1_equality], assumption)
1.67 +apply (rule wfrec_rel.wfrecI)
1.68 +apply (intro strip)
1.69 +apply (erule adm_lemma [THEN wfrec_unique, THEN theI'])
1.70 +done
1.71 +
1.72 +
1.73 +text{** This form avoids giant explosions in proofs.  NOTE USE OF ==*}
1.74 +lemma def_wfrec: "[| f==wfrec r H;  wf(r) |] ==> f(a) = H (cut f r a) a"
1.75 +apply auto
1.76 +apply (blast intro: wfrec)
1.77 +done
1.78 +
1.79 +
1.80 +subsection {* Nitpick setup *}
1.81 +
1.82 +axiomatization wf_wfrec :: "('a \<times> 'a) set \<Rightarrow> (('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b"
1.83 +
1.84 +definition wf_wfrec' :: "('a \<times> 'a) set \<Rightarrow> (('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b" where
1.85 +[nitpick_simp]: "wf_wfrec' R F x = F (cut (wf_wfrec R F) R x) x"
1.86 +
1.87 +definition wfrec' ::  "('a \<times> 'a) set \<Rightarrow> (('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b" where
1.88 +"wfrec' R F x \<equiv> if wf R then wf_wfrec' R F x
1.89 +                else THE y. wfrec_rel R (%f x. F (cut f R x) x) x y"
1.90 +
1.91 +setup {*
1.92 +  Nitpick_HOL.register_ersatz_global
1.93 +    [(@{const_name wf_wfrec}, @{const_name wf_wfrec'}),
1.94 +     (@{const_name wfrec}, @{const_name wfrec'})]
1.95 +*}
1.96 +
1.97 +hide_const (open) wf_wfrec wf_wfrec' wfrec'
1.98 +hide_fact (open) wf_wfrec'_def wfrec'_def
1.99 +
1.100 +subsection {* Wellfoundedness of @{text same_fst} *}
1.101 +
1.102 +definition
1.103 + same_fst :: "('a => bool) => ('a => ('b * 'b)set) => (('a*'b)*('a*'b))set"
1.104 +where
1.105 +    "same_fst P R == {((x',y'),(x,y)) . x'=x & P x & (y',y) : R x}"
1.106 +   --{*For @{text rec_def} declarations where the first n parameters
1.107 +       stay unchanged in the recursive call. *}
1.108 +
1.109 +lemma same_fstI [intro!]:
1.110 +     "[| P x; (y',y) : R x |] ==> ((x,y'),(x,y)) : same_fst P R"
1.112 +
1.113 +lemma wf_same_fst:
1.114 +  assumes prem: "(!!x. P x ==> wf(R x))"
1.115 +  shows "wf(same_fst P R)"
1.116 +apply (simp cong del: imp_cong add: wf_def same_fst_def)
1.117 +apply (intro strip)
1.118 +apply (rename_tac a b)
1.119 +apply (case_tac "wf (R a)")
1.120 + apply (erule_tac a = b in wf_induct, blast)
1.121 +apply (blast intro: prem)
1.122 +done
1.123 +
1.124 +end
```