src/HOL/Recdef.thy
changeset 32462 c33faa289520
parent 32244 a99723d77ae0
child 35416 d8d7d1b785af
     1.1 --- a/src/HOL/Recdef.thy	Mon Aug 31 20:32:00 2009 +0200
     1.2 +++ b/src/HOL/Recdef.thy	Mon Aug 31 20:34:44 2009 +0200
     1.3 @@ -19,6 +19,65 @@
     1.4    ("Tools/recdef.ML")
     1.5  begin
     1.6  
     1.7 +inductive
     1.8 +  wfrec_rel :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => 'a => 'b => bool"
     1.9 +  for R :: "('a * 'a) set"
    1.10 +  and F :: "('a => 'b) => 'a => 'b"
    1.11 +where
    1.12 +  wfrecI: "ALL z. (z, x) : R --> wfrec_rel R F z (g z) ==>
    1.13 +            wfrec_rel R F x (F g x)"
    1.14 +
    1.15 +constdefs
    1.16 +  cut        :: "('a => 'b) => ('a * 'a)set => 'a => 'a => 'b"
    1.17 +  "cut f r x == (%y. if (y,x):r then f y else undefined)"
    1.18 +
    1.19 +  adm_wf :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => bool"
    1.20 +  "adm_wf R F == ALL f g x.
    1.21 +     (ALL z. (z, x) : R --> f z = g z) --> F f x = F g x"
    1.22 +
    1.23 +  wfrec :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => 'a => 'b"
    1.24 +  [code del]: "wfrec R F == %x. THE y. wfrec_rel R (%f x. F (cut f R x) x) x y"
    1.25 +
    1.26 +subsection{*Well-Founded Recursion*}
    1.27 +
    1.28 +text{*cut*}
    1.29 +
    1.30 +lemma cuts_eq: "(cut f r x = cut g r x) = (ALL y. (y,x):r --> f(y)=g(y))"
    1.31 +by (simp add: expand_fun_eq cut_def)
    1.32 +
    1.33 +lemma cut_apply: "(x,a):r ==> (cut f r a)(x) = f(x)"
    1.34 +by (simp add: cut_def)
    1.35 +
    1.36 +text{*Inductive characterization of wfrec combinator; for details see:
    1.37 +John Harrison, "Inductive definitions: automation and application"*}
    1.38 +
    1.39 +lemma wfrec_unique: "[| adm_wf R F; wf R |] ==> EX! y. wfrec_rel R F x y"
    1.40 +apply (simp add: adm_wf_def)
    1.41 +apply (erule_tac a=x in wf_induct)
    1.42 +apply (rule ex1I)
    1.43 +apply (rule_tac g = "%x. THE y. wfrec_rel R F x y" in wfrec_rel.wfrecI)
    1.44 +apply (fast dest!: theI')
    1.45 +apply (erule wfrec_rel.cases, simp)
    1.46 +apply (erule allE, erule allE, erule allE, erule mp)
    1.47 +apply (fast intro: the_equality [symmetric])
    1.48 +done
    1.49 +
    1.50 +lemma adm_lemma: "adm_wf R (%f x. F (cut f R x) x)"
    1.51 +apply (simp add: adm_wf_def)
    1.52 +apply (intro strip)
    1.53 +apply (rule cuts_eq [THEN iffD2, THEN subst], assumption)
    1.54 +apply (rule refl)
    1.55 +done
    1.56 +
    1.57 +lemma wfrec: "wf(r) ==> wfrec r H a = H (cut (wfrec r H) r a) a"
    1.58 +apply (simp add: wfrec_def)
    1.59 +apply (rule adm_lemma [THEN wfrec_unique, THEN the1_equality], assumption)
    1.60 +apply (rule wfrec_rel.wfrecI)
    1.61 +apply (intro strip)
    1.62 +apply (erule adm_lemma [THEN wfrec_unique, THEN theI'])
    1.63 +done
    1.64 +
    1.65 +
    1.66  text{** This form avoids giant explosions in proofs.  NOTE USE OF ==*}
    1.67  lemma def_wfrec: "[| f==wfrec r H;  wf(r) |] ==> f(a) = H (cut f r a) a"
    1.68  apply auto