src/HOL/Library/Old_Recdef.thy
author krauss
Tue Aug 02 10:36:50 2011 +0200 (2011-08-02)
changeset 44013 5cfc1c36ae97
parent 39302 src/HOL/Recdef.thy@d7728f65b353
child 44014 88bd7d74a2c1
permissions -rw-r--r--
moved recdef package to HOL/Library/Old_Recdef.thy
krauss@44013
     1
(*  Title:      HOL/Library/Old_Recdef.thy
wenzelm@10773
     2
    Author:     Konrad Slind and Markus Wenzel, TU Muenchen
wenzelm@12023
     3
*)
wenzelm@5123
     4
wenzelm@12023
     5
header {* TFL: recursive function definitions *}
wenzelm@7701
     6
krauss@44013
     7
theory Old_Recdef
krauss@44013
     8
imports Main
haftmann@16417
     9
uses
krauss@44013
    10
  ("~~/src/HOL/Tools/TFL/casesplit.ML")
krauss@44013
    11
  ("~~/src/HOL/Tools/TFL/utils.ML")
krauss@44013
    12
  ("~~/src/HOL/Tools/TFL/usyntax.ML")
krauss@44013
    13
  ("~~/src/HOL/Tools/TFL/dcterm.ML")
krauss@44013
    14
  ("~~/src/HOL/Tools/TFL/thms.ML")
krauss@44013
    15
  ("~~/src/HOL/Tools/TFL/rules.ML")
krauss@44013
    16
  ("~~/src/HOL/Tools/TFL/thry.ML")
krauss@44013
    17
  ("~~/src/HOL/Tools/TFL/tfl.ML")
krauss@44013
    18
  ("~~/src/HOL/Tools/TFL/post.ML")
krauss@44013
    19
  ("~~/src/HOL/Tools/recdef.ML")
nipkow@15131
    20
begin
wenzelm@10773
    21
krauss@32462
    22
inductive
krauss@32462
    23
  wfrec_rel :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => 'a => 'b => bool"
krauss@32462
    24
  for R :: "('a * 'a) set"
krauss@32462
    25
  and F :: "('a => 'b) => 'a => 'b"
krauss@32462
    26
where
krauss@32462
    27
  wfrecI: "ALL z. (z, x) : R --> wfrec_rel R F z (g z) ==>
krauss@32462
    28
            wfrec_rel R F x (F g x)"
krauss@32462
    29
haftmann@35416
    30
definition
haftmann@35416
    31
  cut        :: "('a => 'b) => ('a * 'a)set => 'a => 'a => 'b" where
krauss@32462
    32
  "cut f r x == (%y. if (y,x):r then f y else undefined)"
krauss@32462
    33
haftmann@35416
    34
definition
haftmann@35416
    35
  adm_wf :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => bool" where
krauss@32462
    36
  "adm_wf R F == ALL f g x.
krauss@32462
    37
     (ALL z. (z, x) : R --> f z = g z) --> F f x = F g x"
krauss@32462
    38
haftmann@35416
    39
definition
haftmann@35416
    40
  wfrec :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => 'a => 'b" where
haftmann@37767
    41
  "wfrec R F == %x. THE y. wfrec_rel R (%f x. F (cut f R x) x) x y"
krauss@32462
    42
krauss@32462
    43
subsection{*Well-Founded Recursion*}
krauss@32462
    44
krauss@32462
    45
text{*cut*}
krauss@32462
    46
krauss@32462
    47
lemma cuts_eq: "(cut f r x = cut g r x) = (ALL y. (y,x):r --> f(y)=g(y))"
nipkow@39302
    48
by (simp add: fun_eq_iff cut_def)
krauss@32462
    49
krauss@32462
    50
lemma cut_apply: "(x,a):r ==> (cut f r a)(x) = f(x)"
krauss@32462
    51
by (simp add: cut_def)
krauss@32462
    52
krauss@32462
    53
text{*Inductive characterization of wfrec combinator; for details see:
krauss@32462
    54
John Harrison, "Inductive definitions: automation and application"*}
krauss@32462
    55
krauss@32462
    56
lemma wfrec_unique: "[| adm_wf R F; wf R |] ==> EX! y. wfrec_rel R F x y"
krauss@32462
    57
apply (simp add: adm_wf_def)
krauss@32462
    58
apply (erule_tac a=x in wf_induct)
krauss@32462
    59
apply (rule ex1I)
krauss@32462
    60
apply (rule_tac g = "%x. THE y. wfrec_rel R F x y" in wfrec_rel.wfrecI)
krauss@32462
    61
apply (fast dest!: theI')
krauss@32462
    62
apply (erule wfrec_rel.cases, simp)
krauss@32462
    63
apply (erule allE, erule allE, erule allE, erule mp)
krauss@32462
    64
apply (fast intro: the_equality [symmetric])
krauss@32462
    65
done
krauss@32462
    66
krauss@32462
    67
lemma adm_lemma: "adm_wf R (%f x. F (cut f R x) x)"
krauss@32462
    68
apply (simp add: adm_wf_def)
krauss@32462
    69
apply (intro strip)
krauss@32462
    70
apply (rule cuts_eq [THEN iffD2, THEN subst], assumption)
krauss@32462
    71
apply (rule refl)
krauss@32462
    72
done
krauss@32462
    73
krauss@32462
    74
lemma wfrec: "wf(r) ==> wfrec r H a = H (cut (wfrec r H) r a) a"
krauss@32462
    75
apply (simp add: wfrec_def)
krauss@32462
    76
apply (rule adm_lemma [THEN wfrec_unique, THEN the1_equality], assumption)
krauss@32462
    77
apply (rule wfrec_rel.wfrecI)
krauss@32462
    78
apply (intro strip)
krauss@32462
    79
apply (erule adm_lemma [THEN wfrec_unique, THEN theI'])
krauss@32462
    80
done
krauss@32462
    81
krauss@32462
    82
krauss@26748
    83
text{** This form avoids giant explosions in proofs.  NOTE USE OF ==*}
krauss@26748
    84
lemma def_wfrec: "[| f==wfrec r H;  wf(r) |] ==> f(a) = H (cut f r a) a"
krauss@26748
    85
apply auto
krauss@26748
    86
apply (blast intro: wfrec)
krauss@26748
    87
done
krauss@26748
    88
krauss@26748
    89
krauss@44013
    90
subsection {* Nitpick setup *}
krauss@44013
    91
krauss@44013
    92
axiomatization wf_wfrec :: "('a \<times> 'a \<Rightarrow> bool) \<Rightarrow> (('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b"
krauss@44013
    93
krauss@44013
    94
definition wf_wfrec' :: "('a \<times> 'a \<Rightarrow> bool) \<Rightarrow> (('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b" where
krauss@44013
    95
[nitpick_simp]: "wf_wfrec' R F x = F (cut (wf_wfrec R F) R x) x"
krauss@44013
    96
krauss@44013
    97
definition wfrec' ::  "('a \<times> 'a \<Rightarrow> bool) \<Rightarrow> (('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b" where
krauss@44013
    98
"wfrec' R F x \<equiv> if wf R then wf_wfrec' R F x
krauss@44013
    99
                else THE y. wfrec_rel R (%f x. F (cut f R x) x) x y"
krauss@44013
   100
krauss@44013
   101
setup {*
krauss@44013
   102
  Nitpick_HOL.register_ersatz_global
krauss@44013
   103
    [(@{const_name wf_wfrec}, @{const_name wf_wfrec'}),
krauss@44013
   104
     (@{const_name wfrec}, @{const_name wfrec'})]
krauss@44013
   105
*}
krauss@44013
   106
krauss@44013
   107
hide_const (open) wf_wfrec wf_wfrec' wfrec'
krauss@44013
   108
hide_fact (open) wf_wfrec'_def wfrec'_def
krauss@44013
   109
krauss@44013
   110
krauss@44013
   111
subsection {* Lemmas for TFL *}
krauss@44013
   112
krauss@26748
   113
lemma tfl_wf_induct: "ALL R. wf R -->  
krauss@26748
   114
       (ALL P. (ALL x. (ALL y. (y,x):R --> P y) --> P x) --> (ALL x. P x))"
krauss@26748
   115
apply clarify
krauss@26748
   116
apply (rule_tac r = R and P = P and a = x in wf_induct, assumption, blast)
krauss@26748
   117
done
krauss@26748
   118
krauss@26748
   119
lemma tfl_cut_apply: "ALL f R. (x,a):R --> (cut f R a)(x) = f(x)"
krauss@26748
   120
apply clarify
krauss@26748
   121
apply (rule cut_apply, assumption)
krauss@26748
   122
done
krauss@26748
   123
krauss@26748
   124
lemma tfl_wfrec:
krauss@26748
   125
     "ALL M R f. (f=wfrec R M) --> wf R --> (ALL x. f x = M (cut f R x) x)"
krauss@26748
   126
apply clarify
krauss@26748
   127
apply (erule wfrec)
krauss@26748
   128
done
krauss@26748
   129
wenzelm@10773
   130
lemma tfl_eq_True: "(x = True) --> x"
wenzelm@10773
   131
  by blast
wenzelm@10773
   132
wenzelm@10773
   133
lemma tfl_rev_eq_mp: "(x = y) --> y --> x";
wenzelm@10773
   134
  by blast
wenzelm@10773
   135
wenzelm@10773
   136
lemma tfl_simp_thm: "(x --> y) --> (x = x') --> (x' --> y)"
wenzelm@10773
   137
  by blast
wenzelm@6438
   138
wenzelm@10773
   139
lemma tfl_P_imp_P_iff_True: "P ==> P = True"
wenzelm@10773
   140
  by blast
wenzelm@10773
   141
wenzelm@10773
   142
lemma tfl_imp_trans: "(A --> B) ==> (B --> C) ==> (A --> C)"
wenzelm@10773
   143
  by blast
wenzelm@10773
   144
wenzelm@12023
   145
lemma tfl_disj_assoc: "(a \<or> b) \<or> c == a \<or> (b \<or> c)"
wenzelm@10773
   146
  by simp
wenzelm@10773
   147
wenzelm@12023
   148
lemma tfl_disjE: "P \<or> Q ==> P --> R ==> Q --> R ==> R"
wenzelm@10773
   149
  by blast
wenzelm@10773
   150
wenzelm@12023
   151
lemma tfl_exE: "\<exists>x. P x ==> \<forall>x. P x --> Q ==> Q"
wenzelm@10773
   152
  by blast
wenzelm@10773
   153
krauss@44013
   154
use "~~/src/HOL/Tools/TFL/casesplit.ML"
krauss@44013
   155
use "~~/src/HOL/Tools/TFL/utils.ML"
krauss@44013
   156
use "~~/src/HOL/Tools/TFL/usyntax.ML"
krauss@44013
   157
use "~~/src/HOL/Tools/TFL/dcterm.ML"
krauss@44013
   158
use "~~/src/HOL/Tools/TFL/thms.ML"
krauss@44013
   159
use "~~/src/HOL/Tools/TFL/rules.ML"
krauss@44013
   160
use "~~/src/HOL/Tools/TFL/thry.ML"
krauss@44013
   161
use "~~/src/HOL/Tools/TFL/tfl.ML"
krauss@44013
   162
use "~~/src/HOL/Tools/TFL/post.ML"
krauss@44013
   163
use "~~/src/HOL/Tools/recdef.ML"
haftmann@31723
   164
setup Recdef.setup
wenzelm@6438
   165
krauss@32244
   166
text {*Wellfoundedness of @{text same_fst}*}
krauss@32244
   167
krauss@32244
   168
definition
krauss@32244
   169
 same_fst :: "('a => bool) => ('a => ('b * 'b)set) => (('a*'b)*('a*'b))set"
krauss@32244
   170
where
krauss@32244
   171
    "same_fst P R == {((x',y'),(x,y)) . x'=x & P x & (y',y) : R x}"
krauss@32244
   172
   --{*For @{text rec_def} declarations where the first n parameters
krauss@32244
   173
       stay unchanged in the recursive call. *}
krauss@32244
   174
krauss@32244
   175
lemma same_fstI [intro!]:
krauss@32244
   176
     "[| P x; (y',y) : R x |] ==> ((x,y'),(x,y)) : same_fst P R"
krauss@32244
   177
by (simp add: same_fst_def)
krauss@32244
   178
krauss@32244
   179
lemma wf_same_fst:
krauss@32244
   180
  assumes prem: "(!!x. P x ==> wf(R x))"
krauss@32244
   181
  shows "wf(same_fst P R)"
krauss@32244
   182
apply (simp cong del: imp_cong add: wf_def same_fst_def)
krauss@32244
   183
apply (intro strip)
krauss@32244
   184
apply (rename_tac a b)
krauss@32244
   185
apply (case_tac "wf (R a)")
krauss@32244
   186
 apply (erule_tac a = b in wf_induct, blast)
krauss@32244
   187
apply (blast intro: prem)
krauss@32244
   188
done
krauss@32244
   189
krauss@32244
   190
text {*Rule setup*}
krauss@32244
   191
wenzelm@9855
   192
lemmas [recdef_simp] =
wenzelm@9855
   193
  inv_image_def
wenzelm@9855
   194
  measure_def
wenzelm@9855
   195
  lex_prod_def
nipkow@11284
   196
  same_fst_def
wenzelm@9855
   197
  less_Suc_eq [THEN iffD2]
wenzelm@9855
   198
wenzelm@23150
   199
lemmas [recdef_cong] =
krauss@22622
   200
  if_cong let_cong image_cong INT_cong UN_cong bex_cong ball_cong imp_cong
krauss@44013
   201
  map_cong filter_cong takeWhile_cong dropWhile_cong foldl_cong foldr_cong 
wenzelm@9855
   202
wenzelm@9855
   203
lemmas [recdef_wf] =
wenzelm@9855
   204
  wf_trancl
wenzelm@9855
   205
  wf_less_than
wenzelm@9855
   206
  wf_lex_prod
wenzelm@9855
   207
  wf_inv_image
wenzelm@9855
   208
  wf_measure
krauss@44013
   209
  wf_measures
wenzelm@9855
   210
  wf_pred_nat
nipkow@10653
   211
  wf_same_fst
wenzelm@9855
   212
  wf_empty
wenzelm@9855
   213
wenzelm@6438
   214
end