src/HOL/Wfrec.thy
author wenzelm
Fri Jan 31 14:33:02 2014 +0100 (2014-01-31)
changeset 55210 d1e3b708d74b
parent 55017 2df6ad1dbd66
child 58184 db1381d811ab
permissions -rw-r--r--
tuned headers;
     1 (*  Title:      HOL/Wfrec.thy
     2     Author:     Tobias Nipkow
     3     Author:     Lawrence C Paulson
     4     Author:     Konrad Slind
     5 *)
     6 
     7 header {* Well-Founded Recursion Combinator *}
     8 
     9 theory Wfrec
    10 imports Wellfounded
    11 begin
    12 
    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)"
    20 
    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)"
    24 
    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"
    29 
    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"
    33 
    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)
    36 
    37 lemma cut_apply: "(x,a):r ==> (cut f r a)(x) = f(x)"
    38 by (simp add: cut_def)
    39 
    40 text{*Inductive characterization of wfrec combinator; for details see:
    41 John Harrison, "Inductive definitions: automation and application"*}
    42 
    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)
    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 (blast intro: the_equality [symmetric])
    52 done
    53 
    54 lemma adm_lemma: "adm_wf R (%f x. F (cut f R x) x)"
    55 apply (simp add: adm_wf_def)
    56 apply (intro strip)
    57 apply (rule cuts_eq [THEN iffD2, THEN subst], assumption)
    58 apply (rule refl)
    59 done
    60 
    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)
    65 apply (intro strip)
    66 apply (erule adm_lemma [THEN wfrec_unique, THEN theI'])
    67 done
    68 
    69 
    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
    75 
    76 
    77 subsection {* Wellfoundedness of @{text same_fst} *}
    78 
    79 definition
    80  same_fst :: "('a => bool) => ('a => ('b * 'b)set) => (('a*'b)*('a*'b))set"
    81 where
    82     "same_fst P R == {((x',y'),(x,y)) . x'=x & P x & (y',y) : R x}"
    83    --{*For @{text rec_def} declarations where the first n parameters
    84        stay unchanged in the recursive call. *}
    85 
    86 lemma same_fstI [intro!]:
    87      "[| P x; (y',y) : R x |] ==> ((x,y'),(x,y)) : same_fst P R"
    88 by (simp add: same_fst_def)
    89 
    90 lemma wf_same_fst:
    91   assumes prem: "(!!x. P x ==> wf(R x))"
    92   shows "wf(same_fst P R)"
    93 apply (simp cong del: imp_cong add: wf_def same_fst_def)
    94 apply (intro strip)
    95 apply (rename_tac a b)
    96 apply (case_tac "wf (R a)")
    97  apply (erule_tac a = b in wf_induct, blast)
    98 apply (blast intro: prem)
    99 done
   100 
   101 end