src/HOL/Recdef.thy
author haftmann
Mon Mar 01 13:40:23 2010 +0100 (2010-03-01)
changeset 35416 d8d7d1b785af
parent 32462 c33faa289520
child 37767 a2b7a20d6ea3
permissions -rw-r--r--
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
wenzelm@7701
     1
(*  Title:      HOL/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
nipkow@15131
     7
theory Recdef
haftmann@29654
     8
imports FunDef Plain
haftmann@16417
     9
uses
wenzelm@23150
    10
  ("Tools/TFL/casesplit.ML")
wenzelm@23150
    11
  ("Tools/TFL/utils.ML")
wenzelm@23150
    12
  ("Tools/TFL/usyntax.ML")
wenzelm@23150
    13
  ("Tools/TFL/dcterm.ML")
wenzelm@23150
    14
  ("Tools/TFL/thms.ML")
wenzelm@23150
    15
  ("Tools/TFL/rules.ML")
wenzelm@23150
    16
  ("Tools/TFL/thry.ML")
wenzelm@23150
    17
  ("Tools/TFL/tfl.ML")
wenzelm@23150
    18
  ("Tools/TFL/post.ML")
haftmann@31723
    19
  ("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
krauss@32462
    41
  [code del]: "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))"
krauss@32462
    48
by (simp add: expand_fun_eq 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@26748
    90
lemma tfl_wf_induct: "ALL R. wf R -->  
krauss@26748
    91
       (ALL P. (ALL x. (ALL y. (y,x):R --> P y) --> P x) --> (ALL x. P x))"
krauss@26748
    92
apply clarify
krauss@26748
    93
apply (rule_tac r = R and P = P and a = x in wf_induct, assumption, blast)
krauss@26748
    94
done
krauss@26748
    95
krauss@26748
    96
lemma tfl_cut_apply: "ALL f R. (x,a):R --> (cut f R a)(x) = f(x)"
krauss@26748
    97
apply clarify
krauss@26748
    98
apply (rule cut_apply, assumption)
krauss@26748
    99
done
krauss@26748
   100
krauss@26748
   101
lemma tfl_wfrec:
krauss@26748
   102
     "ALL M R f. (f=wfrec R M) --> wf R --> (ALL x. f x = M (cut f R x) x)"
krauss@26748
   103
apply clarify
krauss@26748
   104
apply (erule wfrec)
krauss@26748
   105
done
krauss@26748
   106
wenzelm@10773
   107
lemma tfl_eq_True: "(x = True) --> x"
wenzelm@10773
   108
  by blast
wenzelm@10773
   109
wenzelm@10773
   110
lemma tfl_rev_eq_mp: "(x = y) --> y --> x";
wenzelm@10773
   111
  by blast
wenzelm@10773
   112
wenzelm@10773
   113
lemma tfl_simp_thm: "(x --> y) --> (x = x') --> (x' --> y)"
wenzelm@10773
   114
  by blast
wenzelm@6438
   115
wenzelm@10773
   116
lemma tfl_P_imp_P_iff_True: "P ==> P = True"
wenzelm@10773
   117
  by blast
wenzelm@10773
   118
wenzelm@10773
   119
lemma tfl_imp_trans: "(A --> B) ==> (B --> C) ==> (A --> C)"
wenzelm@10773
   120
  by blast
wenzelm@10773
   121
wenzelm@12023
   122
lemma tfl_disj_assoc: "(a \<or> b) \<or> c == a \<or> (b \<or> c)"
wenzelm@10773
   123
  by simp
wenzelm@10773
   124
wenzelm@12023
   125
lemma tfl_disjE: "P \<or> Q ==> P --> R ==> Q --> R ==> R"
wenzelm@10773
   126
  by blast
wenzelm@10773
   127
wenzelm@12023
   128
lemma tfl_exE: "\<exists>x. P x ==> \<forall>x. P x --> Q ==> Q"
wenzelm@10773
   129
  by blast
wenzelm@10773
   130
wenzelm@23150
   131
use "Tools/TFL/casesplit.ML"
wenzelm@23150
   132
use "Tools/TFL/utils.ML"
wenzelm@23150
   133
use "Tools/TFL/usyntax.ML"
wenzelm@23150
   134
use "Tools/TFL/dcterm.ML"
wenzelm@23150
   135
use "Tools/TFL/thms.ML"
wenzelm@23150
   136
use "Tools/TFL/rules.ML"
wenzelm@23150
   137
use "Tools/TFL/thry.ML"
wenzelm@23150
   138
use "Tools/TFL/tfl.ML"
wenzelm@23150
   139
use "Tools/TFL/post.ML"
haftmann@31723
   140
use "Tools/recdef.ML"
haftmann@31723
   141
setup Recdef.setup
wenzelm@6438
   142
krauss@32244
   143
text {*Wellfoundedness of @{text same_fst}*}
krauss@32244
   144
krauss@32244
   145
definition
krauss@32244
   146
 same_fst :: "('a => bool) => ('a => ('b * 'b)set) => (('a*'b)*('a*'b))set"
krauss@32244
   147
where
krauss@32244
   148
    "same_fst P R == {((x',y'),(x,y)) . x'=x & P x & (y',y) : R x}"
krauss@32244
   149
   --{*For @{text rec_def} declarations where the first n parameters
krauss@32244
   150
       stay unchanged in the recursive call. *}
krauss@32244
   151
krauss@32244
   152
lemma same_fstI [intro!]:
krauss@32244
   153
     "[| P x; (y',y) : R x |] ==> ((x,y'),(x,y)) : same_fst P R"
krauss@32244
   154
by (simp add: same_fst_def)
krauss@32244
   155
krauss@32244
   156
lemma wf_same_fst:
krauss@32244
   157
  assumes prem: "(!!x. P x ==> wf(R x))"
krauss@32244
   158
  shows "wf(same_fst P R)"
krauss@32244
   159
apply (simp cong del: imp_cong add: wf_def same_fst_def)
krauss@32244
   160
apply (intro strip)
krauss@32244
   161
apply (rename_tac a b)
krauss@32244
   162
apply (case_tac "wf (R a)")
krauss@32244
   163
 apply (erule_tac a = b in wf_induct, blast)
krauss@32244
   164
apply (blast intro: prem)
krauss@32244
   165
done
krauss@32244
   166
krauss@32244
   167
text {*Rule setup*}
krauss@32244
   168
wenzelm@9855
   169
lemmas [recdef_simp] =
wenzelm@9855
   170
  inv_image_def
wenzelm@9855
   171
  measure_def
wenzelm@9855
   172
  lex_prod_def
nipkow@11284
   173
  same_fst_def
wenzelm@9855
   174
  less_Suc_eq [THEN iffD2]
wenzelm@9855
   175
wenzelm@23150
   176
lemmas [recdef_cong] =
krauss@22622
   177
  if_cong let_cong image_cong INT_cong UN_cong bex_cong ball_cong imp_cong
wenzelm@9855
   178
wenzelm@9855
   179
lemmas [recdef_wf] =
wenzelm@9855
   180
  wf_trancl
wenzelm@9855
   181
  wf_less_than
wenzelm@9855
   182
  wf_lex_prod
wenzelm@9855
   183
  wf_inv_image
wenzelm@9855
   184
  wf_measure
wenzelm@9855
   185
  wf_pred_nat
nipkow@10653
   186
  wf_same_fst
wenzelm@9855
   187
  wf_empty
wenzelm@9855
   188
wenzelm@6438
   189
end