src/HOL/Wfrec.thy
author blanchet
Sun May 04 18:14:58 2014 +0200 (2014-05-04)
changeset 56846 9df717fef2bb
parent 55210 d1e3b708d74b
child 58184 db1381d811ab
permissions -rw-r--r--
renamed 'xxx_size' to 'size_xxx' for old datatype package
wenzelm@55210
     1
(*  Title:      HOL/Wfrec.thy
krauss@44014
     2
    Author:     Tobias Nipkow
krauss@44014
     3
    Author:     Lawrence C Paulson
krauss@44014
     4
    Author:     Konrad Slind
krauss@44014
     5
*)
krauss@44014
     6
krauss@44014
     7
header {* Well-Founded Recursion Combinator *}
krauss@44014
     8
krauss@44014
     9
theory Wfrec
blanchet@55017
    10
imports Wellfounded
krauss@44014
    11
begin
krauss@44014
    12
krauss@44014
    13
inductive
krauss@44014
    14
  wfrec_rel :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => 'a => 'b => bool"
krauss@44014
    15
  for R :: "('a * 'a) set"
krauss@44014
    16
  and F :: "('a => 'b) => 'a => 'b"
krauss@44014
    17
where
krauss@44014
    18
  wfrecI: "ALL z. (z, x) : R --> wfrec_rel R F z (g z) ==>
krauss@44014
    19
            wfrec_rel R F x (F g x)"
krauss@44014
    20
krauss@44014
    21
definition
krauss@44014
    22
  cut        :: "('a => 'b) => ('a * 'a)set => 'a => 'a => 'b" where
krauss@44014
    23
  "cut f r x == (%y. if (y,x):r then f y else undefined)"
krauss@44014
    24
krauss@44014
    25
definition
krauss@44014
    26
  adm_wf :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => bool" where
krauss@44014
    27
  "adm_wf R F == ALL f g x.
krauss@44014
    28
     (ALL z. (z, x) : R --> f z = g z) --> F f x = F g x"
krauss@44014
    29
krauss@44014
    30
definition
krauss@44014
    31
  wfrec :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => 'a => 'b" where
krauss@44014
    32
  "wfrec R F == %x. THE y. wfrec_rel R (%f x. F (cut f R x) x) x y"
krauss@44014
    33
krauss@44014
    34
lemma cuts_eq: "(cut f r x = cut g r x) = (ALL y. (y,x):r --> f(y)=g(y))"
krauss@44014
    35
by (simp add: fun_eq_iff cut_def)
krauss@44014
    36
krauss@44014
    37
lemma cut_apply: "(x,a):r ==> (cut f r a)(x) = f(x)"
krauss@44014
    38
by (simp add: cut_def)
krauss@44014
    39
krauss@44014
    40
text{*Inductive characterization of wfrec combinator; for details see:
krauss@44014
    41
John Harrison, "Inductive definitions: automation and application"*}
krauss@44014
    42
krauss@44014
    43
lemma wfrec_unique: "[| adm_wf R F; wf R |] ==> EX! y. wfrec_rel R F x y"
krauss@44014
    44
apply (simp add: adm_wf_def)
krauss@44014
    45
apply (erule_tac a=x in wf_induct)
krauss@44014
    46
apply (rule ex1I)
krauss@44014
    47
apply (rule_tac g = "%x. THE y. wfrec_rel R F x y" in wfrec_rel.wfrecI)
krauss@44014
    48
apply (fast dest!: theI')
krauss@44014
    49
apply (erule wfrec_rel.cases, simp)
krauss@44014
    50
apply (erule allE, erule allE, erule allE, erule mp)
blanchet@54482
    51
apply (blast intro: the_equality [symmetric])
krauss@44014
    52
done
krauss@44014
    53
krauss@44014
    54
lemma adm_lemma: "adm_wf R (%f x. F (cut f R x) x)"
krauss@44014
    55
apply (simp add: adm_wf_def)
krauss@44014
    56
apply (intro strip)
krauss@44014
    57
apply (rule cuts_eq [THEN iffD2, THEN subst], assumption)
krauss@44014
    58
apply (rule refl)
krauss@44014
    59
done
krauss@44014
    60
krauss@44014
    61
lemma wfrec: "wf(r) ==> wfrec r H a = H (cut (wfrec r H) r a) a"
krauss@44014
    62
apply (simp add: wfrec_def)
krauss@44014
    63
apply (rule adm_lemma [THEN wfrec_unique, THEN the1_equality], assumption)
krauss@44014
    64
apply (rule wfrec_rel.wfrecI)
krauss@44014
    65
apply (intro strip)
krauss@44014
    66
apply (erule adm_lemma [THEN wfrec_unique, THEN theI'])
krauss@44014
    67
done
krauss@44014
    68
krauss@44014
    69
krauss@44014
    70
text{** This form avoids giant explosions in proofs.  NOTE USE OF ==*}
krauss@44014
    71
lemma def_wfrec: "[| f==wfrec r H;  wf(r) |] ==> f(a) = H (cut f r a) a"
krauss@44014
    72
apply auto
krauss@44014
    73
apply (blast intro: wfrec)
krauss@44014
    74
done
krauss@44014
    75
krauss@44014
    76
krauss@44014
    77
subsection {* Wellfoundedness of @{text same_fst} *}
krauss@44014
    78
krauss@44014
    79
definition
krauss@44014
    80
 same_fst :: "('a => bool) => ('a => ('b * 'b)set) => (('a*'b)*('a*'b))set"
krauss@44014
    81
where
krauss@44014
    82
    "same_fst P R == {((x',y'),(x,y)) . x'=x & P x & (y',y) : R x}"
krauss@44014
    83
   --{*For @{text rec_def} declarations where the first n parameters
krauss@44014
    84
       stay unchanged in the recursive call. *}
krauss@44014
    85
krauss@44014
    86
lemma same_fstI [intro!]:
krauss@44014
    87
     "[| P x; (y',y) : R x |] ==> ((x,y'),(x,y)) : same_fst P R"
krauss@44014
    88
by (simp add: same_fst_def)
krauss@44014
    89
krauss@44014
    90
lemma wf_same_fst:
krauss@44014
    91
  assumes prem: "(!!x. P x ==> wf(R x))"
krauss@44014
    92
  shows "wf(same_fst P R)"
krauss@44014
    93
apply (simp cong del: imp_cong add: wf_def same_fst_def)
krauss@44014
    94
apply (intro strip)
krauss@44014
    95
apply (rename_tac a b)
krauss@44014
    96
apply (case_tac "wf (R a)")
krauss@44014
    97
 apply (erule_tac a = b in wf_induct, blast)
krauss@44014
    98
apply (blast intro: prem)
krauss@44014
    99
done
krauss@44014
   100
krauss@44014
   101
end