src/HOL/Library/Wfrec.thy
author Andreas Lochbihler
Wed Feb 27 10:33:30 2013 +0100 (2013-02-27)
changeset 51288 be7e9a675ec9
parent 44259 b922e91dd1d9
child 54482 a2874c8b3558
permissions -rw-r--r--
add wellorder instance for Numeral_Type (suggested by Jesus Aransay)
krauss@44014
     1
(*  Title:      HOL/Library/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
krauss@44014
    10
imports Main
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)
krauss@44014
    51
apply (fast 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 {* Nitpick setup *}
krauss@44014
    78
huffman@44259
    79
axiomatization wf_wfrec :: "('a \<times> 'a) set \<Rightarrow> (('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b"
krauss@44014
    80
huffman@44259
    81
definition wf_wfrec' :: "('a \<times> 'a) set \<Rightarrow> (('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b" where
krauss@44014
    82
[nitpick_simp]: "wf_wfrec' R F x = F (cut (wf_wfrec R F) R x) x"
krauss@44014
    83
huffman@44259
    84
definition wfrec' ::  "('a \<times> 'a) set \<Rightarrow> (('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b" where
krauss@44014
    85
"wfrec' R F x \<equiv> if wf R then wf_wfrec' R F x
krauss@44014
    86
                else THE y. wfrec_rel R (%f x. F (cut f R x) x) x y"
krauss@44014
    87
krauss@44014
    88
setup {*
krauss@44014
    89
  Nitpick_HOL.register_ersatz_global
krauss@44014
    90
    [(@{const_name wf_wfrec}, @{const_name wf_wfrec'}),
krauss@44014
    91
     (@{const_name wfrec}, @{const_name wfrec'})]
krauss@44014
    92
*}
krauss@44014
    93
krauss@44014
    94
hide_const (open) wf_wfrec wf_wfrec' wfrec'
krauss@44014
    95
hide_fact (open) wf_wfrec'_def wfrec'_def
krauss@44014
    96
krauss@44014
    97
subsection {* Wellfoundedness of @{text same_fst} *}
krauss@44014
    98
krauss@44014
    99
definition
krauss@44014
   100
 same_fst :: "('a => bool) => ('a => ('b * 'b)set) => (('a*'b)*('a*'b))set"
krauss@44014
   101
where
krauss@44014
   102
    "same_fst P R == {((x',y'),(x,y)) . x'=x & P x & (y',y) : R x}"
krauss@44014
   103
   --{*For @{text rec_def} declarations where the first n parameters
krauss@44014
   104
       stay unchanged in the recursive call. *}
krauss@44014
   105
krauss@44014
   106
lemma same_fstI [intro!]:
krauss@44014
   107
     "[| P x; (y',y) : R x |] ==> ((x,y'),(x,y)) : same_fst P R"
krauss@44014
   108
by (simp add: same_fst_def)
krauss@44014
   109
krauss@44014
   110
lemma wf_same_fst:
krauss@44014
   111
  assumes prem: "(!!x. P x ==> wf(R x))"
krauss@44014
   112
  shows "wf(same_fst P R)"
krauss@44014
   113
apply (simp cong del: imp_cong add: wf_def same_fst_def)
krauss@44014
   114
apply (intro strip)
krauss@44014
   115
apply (rename_tac a b)
krauss@44014
   116
apply (case_tac "wf (R a)")
krauss@44014
   117
 apply (erule_tac a = b in wf_induct, blast)
krauss@44014
   118
apply (blast intro: prem)
krauss@44014
   119
done
krauss@44014
   120
krauss@44014
   121
end