src/HOL/Library/Wfrec.thy
 author haftmann Tue Feb 19 19:44:10 2013 +0100 (2013-02-19) changeset 51188 9b5bf1a9a710 parent 44259 b922e91dd1d9 child 54482 a2874c8b3558 permissions -rw-r--r--
dropped spurious left-over from 0a2371e7ced3
1 (*  Title:      HOL/Library/Wfrec.thy
2     Author:     Tobias Nipkow
3     Author:     Lawrence C Paulson
5 *)
7 header {* Well-Founded Recursion Combinator *}
9 theory Wfrec
10 imports Main
11 begin
13 inductive
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"
17 where
18   wfrecI: "ALL z. (z, x) : R --> wfrec_rel R F z (g z) ==>
19             wfrec_rel R F x (F g x)"
21 definition
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)"
25 definition
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"
30 definition
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)"
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"
45 apply (erule_tac a=x in wf_induct)
46 apply (rule ex1I)
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])
52 done
54 lemma adm_lemma: "adm_wf R (%f x. F (cut f R x) x)"
56 apply (intro strip)
57 apply (rule cuts_eq [THEN iffD2, THEN subst], assumption)
58 apply (rule refl)
59 done
61 lemma wfrec: "wf(r) ==> wfrec r H a = H (cut (wfrec r H) r a) a"
63 apply (rule adm_lemma [THEN wfrec_unique, THEN the1_equality], assumption)
64 apply (rule wfrec_rel.wfrecI)
65 apply (intro strip)
66 apply (erule adm_lemma [THEN wfrec_unique, THEN theI'])
67 done
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"
72 apply auto
73 apply (blast intro: wfrec)
74 done
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"
88 setup {*
89   Nitpick_HOL.register_ersatz_global
90     [(@{const_name wf_wfrec}, @{const_name wf_wfrec'}),
91      (@{const_name wfrec}, @{const_name wfrec'})]
92 *}
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} *}
99 definition
100  same_fst :: "('a => bool) => ('a => ('b * 'b)set) => (('a*'b)*('a*'b))set"
101 where
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"