1 (* Title: HOL/Library/Wfrec.thy
3 Author: Lawrence C Paulson
7 header {* Well-Founded Recursion Combinator *}
14 wfrec_rel :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => 'a => 'b => bool"
15 for R :: "('a * 'a) set"
16 and F :: "('a => 'b) => 'a => 'b"
18 wfrecI: "ALL z. (z, x) : R --> wfrec_rel R F z (g z) ==>
19 wfrec_rel R F x (F g x)"
22 cut :: "('a => 'b) => ('a * 'a)set => 'a => 'a => 'b" where
23 "cut f r x == (%y. if (y,x):r then f y else undefined)"
26 adm_wf :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => bool" where
27 "adm_wf R F == ALL f g x.
28 (ALL z. (z, x) : R --> f z = g z) --> F f x = F g x"
31 wfrec :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => 'a => 'b" where
32 "wfrec R F == %x. THE y. wfrec_rel R (%f x. F (cut f R x) x) x y"
34 lemma cuts_eq: "(cut f r x = cut g r x) = (ALL y. (y,x):r --> f(y)=g(y))"
35 by (simp add: fun_eq_iff cut_def)
37 lemma cut_apply: "(x,a):r ==> (cut f r a)(x) = f(x)"
38 by (simp add: cut_def)
40 text{*Inductive characterization of wfrec combinator; for details see:
41 John Harrison, "Inductive definitions: automation and application"*}
43 lemma wfrec_unique: "[| adm_wf R F; wf R |] ==> EX! y. wfrec_rel R F x y"
44 apply (simp add: adm_wf_def)
45 apply (erule_tac a=x in wf_induct)
47 apply (rule_tac g = "%x. THE y. wfrec_rel R F x y" in wfrec_rel.wfrecI)
48 apply (fast dest!: theI')
49 apply (erule wfrec_rel.cases, simp)
50 apply (erule allE, erule allE, erule allE, erule mp)
51 apply (fast intro: the_equality [symmetric])
54 lemma adm_lemma: "adm_wf R (%f x. F (cut f R x) x)"
55 apply (simp add: adm_wf_def)
57 apply (rule cuts_eq [THEN iffD2, THEN subst], assumption)
61 lemma wfrec: "wf(r) ==> wfrec r H a = H (cut (wfrec r H) r a) a"
62 apply (simp add: wfrec_def)
63 apply (rule adm_lemma [THEN wfrec_unique, THEN the1_equality], assumption)
64 apply (rule wfrec_rel.wfrecI)
66 apply (erule adm_lemma [THEN wfrec_unique, THEN theI'])
70 text{** This form avoids giant explosions in proofs. NOTE USE OF ==*}
71 lemma def_wfrec: "[| f==wfrec r H; wf(r) |] ==> f(a) = H (cut f r a) a"
73 apply (blast intro: wfrec)
77 subsection {* Nitpick setup *}
79 axiomatization wf_wfrec :: "('a \<times> 'a) set \<Rightarrow> (('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b"
81 definition wf_wfrec' :: "('a \<times> 'a) set \<Rightarrow> (('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b" where
82 [nitpick_simp]: "wf_wfrec' R F x = F (cut (wf_wfrec R F) R x) x"
84 definition wfrec' :: "('a \<times> 'a) set \<Rightarrow> (('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b" where
85 "wfrec' R F x \<equiv> if wf R then wf_wfrec' R F x
86 else THE y. wfrec_rel R (%f x. F (cut f R x) x) x y"
89 Nitpick_HOL.register_ersatz_global
90 [(@{const_name wf_wfrec}, @{const_name wf_wfrec'}),
91 (@{const_name wfrec}, @{const_name wfrec'})]
94 hide_const (open) wf_wfrec wf_wfrec' wfrec'
95 hide_fact (open) wf_wfrec'_def wfrec'_def
97 subsection {* Wellfoundedness of @{text same_fst} *}
100 same_fst :: "('a => bool) => ('a => ('b * 'b)set) => (('a*'b)*('a*'b))set"
102 "same_fst P R == {((x',y'),(x,y)) . x'=x & P x & (y',y) : R x}"
103 --{*For @{text rec_def} declarations where the first n parameters
104 stay unchanged in the recursive call. *}
106 lemma same_fstI [intro!]:
107 "[| P x; (y',y) : R x |] ==> ((x,y'),(x,y)) : same_fst P R"
108 by (simp add: same_fst_def)
111 assumes prem: "(!!x. P x ==> wf(R x))"
112 shows "wf(same_fst P R)"
113 apply (simp cong del: imp_cong add: wf_def same_fst_def)
115 apply (rename_tac a b)
116 apply (case_tac "wf (R a)")
117 apply (erule_tac a = b in wf_induct, blast)
118 apply (blast intro: prem)