src/CCL/Wfd.thy
author wenzelm
Sat Apr 16 18:11:20 2011 +0200 (2011-04-16)
changeset 42364 8c674b3b8e44
parent 41526 54b4686704af
child 45294 3c5d3d286055
permissions -rw-r--r--
eliminated old List.nth;
tuned;
wenzelm@17456
     1
(*  Title:      CCL/Wfd.thy
clasohm@1474
     2
    Author:     Martin Coen, Cambridge University Computer Laboratory
clasohm@0
     3
    Copyright   1993  University of Cambridge
clasohm@0
     4
*)
clasohm@0
     5
wenzelm@17456
     6
header {* Well-founded relations in CCL *}
wenzelm@17456
     7
wenzelm@17456
     8
theory Wfd
wenzelm@17456
     9
imports Trancl Type Hered
wenzelm@17456
    10
begin
clasohm@0
    11
clasohm@0
    12
consts
clasohm@0
    13
      (*** Predicates ***)
clasohm@0
    14
  Wfd        ::       "[i set] => o"
clasohm@0
    15
      (*** Relations ***)
clasohm@0
    16
  wf         ::       "[i set] => i set"
clasohm@0
    17
  wmap       ::       "[i=>i,i set] => i set"
wenzelm@24825
    18
  lex        ::       "[i set,i set] => i set"      (infixl "**" 70)
clasohm@0
    19
  NatPR      ::       "i set"
clasohm@0
    20
  ListPR     ::       "i set => i set"
clasohm@0
    21
wenzelm@24825
    22
defs
clasohm@0
    23
wenzelm@17456
    24
  Wfd_def:
wenzelm@3837
    25
  "Wfd(R) == ALL P.(ALL x.(ALL y.<y,x> : R --> y:P) --> x:P) --> (ALL a. a:P)"
clasohm@0
    26
wenzelm@17456
    27
  wf_def:         "wf(R) == {x. x:R & Wfd(R)}"
clasohm@0
    28
wenzelm@17456
    29
  wmap_def:       "wmap(f,R) == {p. EX x y. p=<x,y>  &  <f(x),f(y)> : R}"
wenzelm@17456
    30
  lex_def:
wenzelm@3837
    31
  "ra**rb == {p. EX a a' b b'. p = <<a,b>,<a',b'>> & (<a,a'> : ra | (a=a' & <b,b'> : rb))}"
clasohm@0
    32
wenzelm@17456
    33
  NatPR_def:      "NatPR == {p. EX x:Nat. p=<x,succ(x)>}"
wenzelm@17456
    34
  ListPR_def:     "ListPR(A) == {p. EX h:A. EX t:List(A). p=<t,h$t>}"
wenzelm@17456
    35
wenzelm@20140
    36
wenzelm@20140
    37
lemma wfd_induct:
wenzelm@20140
    38
  assumes 1: "Wfd(R)"
wenzelm@20140
    39
    and 2: "!!x.[| ALL y. <y,x>: R --> P(y) |] ==> P(x)"
wenzelm@20140
    40
  shows "P(a)"
wenzelm@20140
    41
  apply (rule 1 [unfolded Wfd_def, rule_format, THEN CollectD])
wenzelm@20140
    42
  using 2 apply blast
wenzelm@20140
    43
  done
wenzelm@20140
    44
wenzelm@20140
    45
lemma wfd_strengthen_lemma:
wenzelm@20140
    46
  assumes 1: "!!x y.<x,y> : R ==> Q(x)"
wenzelm@20140
    47
    and 2: "ALL x. (ALL y. <y,x> : R --> y : P) --> x : P"
wenzelm@20140
    48
    and 3: "!!x. Q(x) ==> x:P"
wenzelm@20140
    49
  shows "a:P"
wenzelm@20140
    50
  apply (rule 2 [rule_format])
wenzelm@20140
    51
  using 1 3
wenzelm@20140
    52
  apply blast
wenzelm@20140
    53
  done
wenzelm@20140
    54
wenzelm@20140
    55
ML {*
wenzelm@27208
    56
  fun wfd_strengthen_tac ctxt s i =
wenzelm@27239
    57
    res_inst_tac ctxt [(("Q", 0), s)] @{thm wfd_strengthen_lemma} i THEN assume_tac (i+1)
wenzelm@20140
    58
*}
wenzelm@20140
    59
wenzelm@20140
    60
lemma wf_anti_sym: "[| Wfd(r);  <a,x>:r;  <x,a>:r |] ==> P"
wenzelm@20140
    61
  apply (subgoal_tac "ALL x. <a,x>:r --> <x,a>:r --> P")
wenzelm@20140
    62
   apply blast
wenzelm@20140
    63
  apply (erule wfd_induct)
wenzelm@20140
    64
  apply blast
wenzelm@20140
    65
  done
wenzelm@20140
    66
wenzelm@20140
    67
lemma wf_anti_refl: "[| Wfd(r);  <a,a>: r |] ==> P"
wenzelm@20140
    68
  apply (rule wf_anti_sym)
wenzelm@20140
    69
  apply assumption+
wenzelm@20140
    70
  done
wenzelm@20140
    71
wenzelm@20140
    72
wenzelm@20140
    73
subsection {* Irreflexive transitive closure *}
wenzelm@20140
    74
wenzelm@20140
    75
lemma trancl_wf:
wenzelm@20140
    76
  assumes 1: "Wfd(R)"
wenzelm@20140
    77
  shows "Wfd(R^+)"
wenzelm@20140
    78
  apply (unfold Wfd_def)
wenzelm@20140
    79
  apply (rule allI ballI impI)+
wenzelm@20140
    80
(*must retain the universal formula for later use!*)
wenzelm@20140
    81
  apply (rule allE, assumption)
wenzelm@20140
    82
  apply (erule mp)
wenzelm@20140
    83
  apply (rule 1 [THEN wfd_induct])
wenzelm@20140
    84
  apply (rule impI [THEN allI])
wenzelm@20140
    85
  apply (erule tranclE)
wenzelm@20140
    86
   apply blast
wenzelm@20140
    87
  apply (erule spec [THEN mp, THEN spec, THEN mp])
wenzelm@20140
    88
   apply assumption+
wenzelm@20140
    89
  done
wenzelm@20140
    90
wenzelm@20140
    91
wenzelm@20140
    92
subsection {* Lexicographic Ordering *}
wenzelm@20140
    93
wenzelm@20140
    94
lemma lexXH:
wenzelm@20140
    95
  "p : ra**rb <-> (EX a a' b b'. p = <<a,b>,<a',b'>> & (<a,a'> : ra | a=a' & <b,b'> : rb))"
wenzelm@20140
    96
  unfolding lex_def by blast
wenzelm@20140
    97
wenzelm@20140
    98
lemma lexI1: "<a,a'> : ra ==> <<a,b>,<a',b'>> : ra**rb"
wenzelm@20140
    99
  by (blast intro!: lexXH [THEN iffD2])
wenzelm@20140
   100
wenzelm@20140
   101
lemma lexI2: "<b,b'> : rb ==> <<a,b>,<a,b'>> : ra**rb"
wenzelm@20140
   102
  by (blast intro!: lexXH [THEN iffD2])
wenzelm@20140
   103
wenzelm@20140
   104
lemma lexE:
wenzelm@20140
   105
  assumes 1: "p : ra**rb"
wenzelm@20140
   106
    and 2: "!!a a' b b'.[| <a,a'> : ra; p=<<a,b>,<a',b'>> |] ==> R"
wenzelm@20140
   107
    and 3: "!!a b b'.[| <b,b'> : rb;  p = <<a,b>,<a,b'>> |] ==> R"
wenzelm@20140
   108
  shows R
wenzelm@20140
   109
  apply (rule 1 [THEN lexXH [THEN iffD1], THEN exE])
wenzelm@20140
   110
  using 2 3
wenzelm@20140
   111
  apply blast
wenzelm@20140
   112
  done
wenzelm@20140
   113
wenzelm@20140
   114
lemma lex_pair: "[| p : r**s;  !!a a' b b'. p = <<a,b>,<a',b'>> ==> P |] ==>P"
wenzelm@20140
   115
  apply (erule lexE)
wenzelm@20140
   116
   apply blast+
wenzelm@20140
   117
  done
wenzelm@20140
   118
wenzelm@20140
   119
lemma lex_wf:
wenzelm@20140
   120
  assumes 1: "Wfd(R)"
wenzelm@20140
   121
    and 2: "Wfd(S)"
wenzelm@20140
   122
  shows "Wfd(R**S)"
wenzelm@20140
   123
  apply (unfold Wfd_def)
wenzelm@20140
   124
  apply safe
wenzelm@27208
   125
  apply (tactic {* wfd_strengthen_tac @{context} "%x. EX a b. x=<a,b>" 1 *})
wenzelm@20140
   126
   apply (blast elim!: lex_pair)
wenzelm@20140
   127
  apply (subgoal_tac "ALL a b.<a,b>:P")
wenzelm@20140
   128
   apply blast
wenzelm@20140
   129
  apply (rule 1 [THEN wfd_induct, THEN allI])
wenzelm@20140
   130
  apply (rule 2 [THEN wfd_induct, THEN allI]) back
wenzelm@20140
   131
  apply (fast elim!: lexE)
wenzelm@20140
   132
  done
wenzelm@20140
   133
wenzelm@20140
   134
wenzelm@20140
   135
subsection {* Mapping *}
wenzelm@20140
   136
wenzelm@20140
   137
lemma wmapXH: "p : wmap(f,r) <-> (EX x y. p=<x,y>  &  <f(x),f(y)> : r)"
wenzelm@20140
   138
  unfolding wmap_def by blast
wenzelm@20140
   139
wenzelm@20140
   140
lemma wmapI: "<f(a),f(b)> : r ==> <a,b> : wmap(f,r)"
wenzelm@20140
   141
  by (blast intro!: wmapXH [THEN iffD2])
wenzelm@20140
   142
wenzelm@20140
   143
lemma wmapE: "[| p : wmap(f,r);  !!a b.[| <f(a),f(b)> : r;  p=<a,b> |] ==> R |] ==> R"
wenzelm@20140
   144
  by (blast dest!: wmapXH [THEN iffD1])
wenzelm@20140
   145
wenzelm@20140
   146
lemma wmap_wf:
wenzelm@20140
   147
  assumes 1: "Wfd(r)"
wenzelm@20140
   148
  shows "Wfd(wmap(f,r))"
wenzelm@20140
   149
  apply (unfold Wfd_def)
wenzelm@20140
   150
  apply clarify
wenzelm@20140
   151
  apply (subgoal_tac "ALL b. ALL a. f (a) =b-->a:P")
wenzelm@20140
   152
   apply blast
wenzelm@20140
   153
  apply (rule 1 [THEN wfd_induct, THEN allI])
wenzelm@20140
   154
  apply clarify
wenzelm@20140
   155
  apply (erule spec [THEN mp])
wenzelm@20140
   156
  apply (safe elim!: wmapE)
wenzelm@20140
   157
  apply (erule spec [THEN mp, THEN spec, THEN mp])
wenzelm@20140
   158
   apply assumption
wenzelm@20140
   159
   apply (rule refl)
wenzelm@20140
   160
  done
wenzelm@20140
   161
wenzelm@20140
   162
wenzelm@20140
   163
subsection {* Projections *}
wenzelm@20140
   164
wenzelm@20140
   165
lemma wfstI: "<xa,ya> : r ==> <<xa,xb>,<ya,yb>> : wmap(fst,r)"
wenzelm@20140
   166
  apply (rule wmapI)
wenzelm@20140
   167
  apply simp
wenzelm@20140
   168
  done
wenzelm@20140
   169
wenzelm@20140
   170
lemma wsndI: "<xb,yb> : r ==> <<xa,xb>,<ya,yb>> : wmap(snd,r)"
wenzelm@20140
   171
  apply (rule wmapI)
wenzelm@20140
   172
  apply simp
wenzelm@20140
   173
  done
wenzelm@20140
   174
wenzelm@20140
   175
lemma wthdI: "<xc,yc> : r ==> <<xa,<xb,xc>>,<ya,<yb,yc>>> : wmap(thd,r)"
wenzelm@20140
   176
  apply (rule wmapI)
wenzelm@20140
   177
  apply simp
wenzelm@20140
   178
  done
wenzelm@20140
   179
wenzelm@20140
   180
wenzelm@20140
   181
subsection {* Ground well-founded relations *}
wenzelm@20140
   182
wenzelm@20140
   183
lemma wfI: "[| Wfd(r);  a : r |] ==> a : wf(r)"
wenzelm@20140
   184
  unfolding wf_def by blast
wenzelm@20140
   185
wenzelm@20140
   186
lemma Empty_wf: "Wfd({})"
wenzelm@20140
   187
  unfolding Wfd_def by (blast elim: EmptyXH [THEN iffD1, THEN FalseE])
wenzelm@20140
   188
wenzelm@20140
   189
lemma wf_wf: "Wfd(wf(R))"
wenzelm@20140
   190
  unfolding wf_def
wenzelm@20140
   191
  apply (rule_tac Q = "Wfd(R)" in excluded_middle [THEN disjE])
wenzelm@20140
   192
   apply simp_all
wenzelm@20140
   193
  apply (rule Empty_wf)
wenzelm@20140
   194
  done
wenzelm@20140
   195
wenzelm@20140
   196
lemma NatPRXH: "p : NatPR <-> (EX x:Nat. p=<x,succ(x)>)"
wenzelm@20140
   197
  unfolding NatPR_def by blast
wenzelm@20140
   198
wenzelm@20140
   199
lemma ListPRXH: "p : ListPR(A) <-> (EX h:A. EX t:List(A).p=<t,h$t>)"
wenzelm@20140
   200
  unfolding ListPR_def by blast
wenzelm@20140
   201
wenzelm@20140
   202
lemma NatPRI: "x : Nat ==> <x,succ(x)> : NatPR"
wenzelm@20140
   203
  by (auto simp: NatPRXH)
wenzelm@20140
   204
wenzelm@20140
   205
lemma ListPRI: "[| t : List(A); h : A |] ==> <t,h $ t> : ListPR(A)"
wenzelm@20140
   206
  by (auto simp: ListPRXH)
wenzelm@20140
   207
wenzelm@20140
   208
lemma NatPR_wf: "Wfd(NatPR)"
wenzelm@20140
   209
  apply (unfold Wfd_def)
wenzelm@20140
   210
  apply clarify
wenzelm@27208
   211
  apply (tactic {* wfd_strengthen_tac @{context} "%x. x:Nat" 1 *})
wenzelm@20140
   212
   apply (fastsimp iff: NatPRXH)
wenzelm@20140
   213
  apply (erule Nat_ind)
wenzelm@20140
   214
   apply (fastsimp iff: NatPRXH)+
wenzelm@20140
   215
  done
wenzelm@20140
   216
wenzelm@20140
   217
lemma ListPR_wf: "Wfd(ListPR(A))"
wenzelm@20140
   218
  apply (unfold Wfd_def)
wenzelm@20140
   219
  apply clarify
wenzelm@27208
   220
  apply (tactic {* wfd_strengthen_tac @{context} "%x. x:List (A)" 1 *})
wenzelm@20140
   221
   apply (fastsimp iff: ListPRXH)
wenzelm@20140
   222
  apply (erule List_ind)
wenzelm@20140
   223
   apply (fastsimp iff: ListPRXH)+
wenzelm@20140
   224
  done
wenzelm@20140
   225
wenzelm@20140
   226
wenzelm@20140
   227
subsection {* General Recursive Functions *}
wenzelm@20140
   228
wenzelm@20140
   229
lemma letrecT:
wenzelm@20140
   230
  assumes 1: "a : A"
wenzelm@20140
   231
    and 2: "!!p g.[| p:A; ALL x:{x: A. <x,p>:wf(R)}. g(x) : D(x) |] ==> h(p,g) : D(p)"
wenzelm@20140
   232
  shows "letrec g x be h(x,g) in g(a) : D(a)"
wenzelm@20140
   233
  apply (rule 1 [THEN rev_mp])
wenzelm@20140
   234
  apply (rule wf_wf [THEN wfd_induct])
wenzelm@20140
   235
  apply (subst letrecB)
wenzelm@20140
   236
  apply (rule impI)
wenzelm@20140
   237
  apply (erule 2)
wenzelm@20140
   238
  apply blast
wenzelm@20140
   239
  done
wenzelm@20140
   240
wenzelm@20140
   241
lemma SPLITB: "SPLIT(<a,b>,B) = B(a,b)"
wenzelm@20140
   242
  unfolding SPLIT_def
wenzelm@20140
   243
  apply (rule set_ext)
wenzelm@20140
   244
  apply blast
wenzelm@20140
   245
  done
wenzelm@17456
   246
wenzelm@20140
   247
lemma letrec2T:
wenzelm@20140
   248
  assumes "a : A"
wenzelm@20140
   249
    and "b : B"
wenzelm@20140
   250
    and "!!p q g.[| p:A; q:B;
wenzelm@20140
   251
              ALL x:A. ALL y:{y: B. <<x,y>,<p,q>>:wf(R)}. g(x,y) : D(x,y) |] ==> 
wenzelm@20140
   252
                h(p,q,g) : D(p,q)"
wenzelm@20140
   253
  shows "letrec g x y be h(x,y,g) in g(a,b) : D(a,b)"
wenzelm@20140
   254
  apply (unfold letrec2_def)
wenzelm@20140
   255
  apply (rule SPLITB [THEN subst])
wenzelm@41526
   256
  apply (assumption | rule letrecT pairT splitT assms)+
wenzelm@20140
   257
  apply (subst SPLITB)
wenzelm@41526
   258
  apply (assumption | rule ballI SubtypeI assms)+
wenzelm@20140
   259
  apply (rule SPLITB [THEN subst])
wenzelm@41526
   260
  apply (assumption | rule letrecT SubtypeI pairT splitT assms |
wenzelm@20140
   261
    erule bspec SubtypeE sym [THEN subst])+
wenzelm@20140
   262
  done
wenzelm@20140
   263
wenzelm@20140
   264
lemma lem: "SPLIT(<a,<b,c>>,%x xs. SPLIT(xs,%y z. B(x,y,z))) = B(a,b,c)"
wenzelm@20140
   265
  by (simp add: SPLITB)
wenzelm@20140
   266
wenzelm@20140
   267
lemma letrec3T:
wenzelm@20140
   268
  assumes "a : A"
wenzelm@20140
   269
    and "b : B"
wenzelm@20140
   270
    and "c : C"
wenzelm@20140
   271
    and "!!p q r g.[| p:A; q:B; r:C;
wenzelm@20140
   272
       ALL x:A. ALL y:B. ALL z:{z:C. <<x,<y,z>>,<p,<q,r>>> : wf(R)}.  
wenzelm@20140
   273
                                                        g(x,y,z) : D(x,y,z) |] ==> 
wenzelm@20140
   274
                h(p,q,r,g) : D(p,q,r)"
wenzelm@20140
   275
  shows "letrec g x y z be h(x,y,z,g) in g(a,b,c) : D(a,b,c)"
wenzelm@20140
   276
  apply (unfold letrec3_def)
wenzelm@20140
   277
  apply (rule lem [THEN subst])
wenzelm@41526
   278
  apply (assumption | rule letrecT pairT splitT assms)+
wenzelm@20140
   279
  apply (simp add: SPLITB)
wenzelm@41526
   280
  apply (assumption | rule ballI SubtypeI assms)+
wenzelm@20140
   281
  apply (rule lem [THEN subst])
wenzelm@41526
   282
  apply (assumption | rule letrecT SubtypeI pairT splitT assms |
wenzelm@20140
   283
    erule bspec SubtypeE sym [THEN subst])+
wenzelm@20140
   284
  done
wenzelm@20140
   285
wenzelm@20140
   286
lemmas letrecTs = letrecT letrec2T letrec3T
wenzelm@20140
   287
wenzelm@20140
   288
wenzelm@20140
   289
subsection {* Type Checking for Recursive Calls *}
wenzelm@20140
   290
wenzelm@20140
   291
lemma rcallT:
wenzelm@20140
   292
  "[| ALL x:{x:A.<x,p>:wf(R)}.g(x):D(x);  
wenzelm@20140
   293
      g(a) : D(a) ==> g(a) : E;  a:A;  <a,p>:wf(R) |] ==>  
wenzelm@20140
   294
  g(a) : E"
wenzelm@20140
   295
  by blast
wenzelm@20140
   296
wenzelm@20140
   297
lemma rcall2T:
wenzelm@20140
   298
  "[| ALL x:A. ALL y:{y:B.<<x,y>,<p,q>>:wf(R)}.g(x,y):D(x,y);  
wenzelm@20140
   299
      g(a,b) : D(a,b) ==> g(a,b) : E;  a:A;  b:B;  <<a,b>,<p,q>>:wf(R) |] ==>  
wenzelm@20140
   300
  g(a,b) : E"
wenzelm@20140
   301
  by blast
wenzelm@20140
   302
wenzelm@20140
   303
lemma rcall3T:
wenzelm@20140
   304
  "[| ALL x:A. ALL y:B. ALL z:{z:C.<<x,<y,z>>,<p,<q,r>>>:wf(R)}. g(x,y,z):D(x,y,z);  
wenzelm@20140
   305
      g(a,b,c) : D(a,b,c) ==> g(a,b,c) : E;   
wenzelm@20140
   306
      a:A;  b:B;  c:C;  <<a,<b,c>>,<p,<q,r>>> : wf(R) |] ==>  
wenzelm@20140
   307
  g(a,b,c) : E"
wenzelm@20140
   308
  by blast
wenzelm@20140
   309
wenzelm@20140
   310
lemmas rcallTs = rcallT rcall2T rcall3T
wenzelm@20140
   311
wenzelm@20140
   312
wenzelm@20140
   313
subsection {* Instantiating an induction hypothesis with an equality assumption *}
wenzelm@20140
   314
wenzelm@20140
   315
lemma hyprcallT:
wenzelm@20140
   316
  assumes 1: "g(a) = b"
wenzelm@20140
   317
    and 2: "ALL x:{x:A.<x,p>:wf(R)}.g(x):D(x)"
wenzelm@20140
   318
    and 3: "ALL x:{x:A.<x,p>:wf(R)}.g(x):D(x) ==> b=g(a) ==> g(a) : D(a) ==> P"
wenzelm@20140
   319
    and 4: "ALL x:{x:A.<x,p>:wf(R)}.g(x):D(x) ==> a:A"
wenzelm@20140
   320
    and 5: "ALL x:{x:A.<x,p>:wf(R)}.g(x):D(x) ==> <a,p>:wf(R)"
wenzelm@20140
   321
  shows P
wenzelm@20140
   322
  apply (rule 3 [OF 2, OF 1 [symmetric]])
wenzelm@20140
   323
  apply (rule rcallT [OF 2])
wenzelm@20140
   324
    apply assumption
wenzelm@20140
   325
   apply (rule 4 [OF 2])
wenzelm@20140
   326
  apply (rule 5 [OF 2])
wenzelm@20140
   327
  done
wenzelm@20140
   328
wenzelm@20140
   329
lemma hyprcall2T:
wenzelm@20140
   330
  assumes 1: "g(a,b) = c"
wenzelm@20140
   331
    and 2: "ALL x:A. ALL y:{y:B.<<x,y>,<p,q>>:wf(R)}.g(x,y):D(x,y)"
wenzelm@20140
   332
    and 3: "[| c=g(a,b);  g(a,b) : D(a,b) |] ==> P"
wenzelm@20140
   333
    and 4: "a:A"
wenzelm@20140
   334
    and 5: "b:B"
wenzelm@20140
   335
    and 6: "<<a,b>,<p,q>>:wf(R)"
wenzelm@20140
   336
  shows P
wenzelm@20140
   337
  apply (rule 3)
wenzelm@20140
   338
    apply (rule 1 [symmetric])
wenzelm@20140
   339
  apply (rule rcall2T)
wenzelm@23467
   340
      apply (rule 2)
wenzelm@23467
   341
     apply assumption
wenzelm@23467
   342
    apply (rule 4)
wenzelm@23467
   343
   apply (rule 5)
wenzelm@23467
   344
  apply (rule 6)
wenzelm@20140
   345
  done
wenzelm@20140
   346
wenzelm@20140
   347
lemma hyprcall3T:
wenzelm@20140
   348
  assumes 1: "g(a,b,c) = d"
wenzelm@20140
   349
    and 2: "ALL x:A. ALL y:B. ALL z:{z:C.<<x,<y,z>>,<p,<q,r>>>:wf(R)}.g(x,y,z):D(x,y,z)"
wenzelm@20140
   350
    and 3: "[| d=g(a,b,c);  g(a,b,c) : D(a,b,c) |] ==> P"
wenzelm@20140
   351
    and 4: "a:A"
wenzelm@20140
   352
    and 5: "b:B"
wenzelm@20140
   353
    and 6: "c:C"
wenzelm@20140
   354
    and 7: "<<a,<b,c>>,<p,<q,r>>> : wf(R)"
wenzelm@20140
   355
  shows P
wenzelm@20140
   356
  apply (rule 3)
wenzelm@20140
   357
   apply (rule 1 [symmetric])
wenzelm@20140
   358
  apply (rule rcall3T)
wenzelm@23467
   359
       apply (rule 2)
wenzelm@23467
   360
      apply assumption
wenzelm@23467
   361
     apply (rule 4)
wenzelm@23467
   362
    apply (rule 5)
wenzelm@23467
   363
   apply (rule 6)
wenzelm@23467
   364
  apply (rule 7)
wenzelm@20140
   365
  done
wenzelm@20140
   366
wenzelm@20140
   367
lemmas hyprcallTs = hyprcallT hyprcall2T hyprcall3T
wenzelm@20140
   368
wenzelm@20140
   369
wenzelm@20140
   370
subsection {* Rules to Remove Induction Hypotheses after Type Checking *}
wenzelm@20140
   371
wenzelm@20140
   372
lemma rmIH1: "[| ALL x:{x:A.<x,p>:wf(R)}.g(x):D(x); P |] ==> P" .
wenzelm@20140
   373
wenzelm@20140
   374
lemma rmIH2: "[| ALL x:A. ALL y:{y:B.<<x,y>,<p,q>>:wf(R)}.g(x,y):D(x,y); P |] ==> P" .
wenzelm@20140
   375
  
wenzelm@20140
   376
lemma rmIH3:
wenzelm@20140
   377
 "[| ALL x:A. ALL y:B. ALL z:{z:C.<<x,<y,z>>,<p,<q,r>>>:wf(R)}.g(x,y,z):D(x,y,z);  
wenzelm@20140
   378
     P |] ==>  
wenzelm@20140
   379
     P" .
wenzelm@20140
   380
wenzelm@20140
   381
lemmas rmIHs = rmIH1 rmIH2 rmIH3
wenzelm@20140
   382
wenzelm@20140
   383
wenzelm@20140
   384
subsection {* Lemmas for constructors and subtypes *}
wenzelm@20140
   385
wenzelm@20140
   386
(* 0-ary constructors do not need additional rules as they are handled *)
wenzelm@20140
   387
(*                                      correctly by applying SubtypeI *)
wenzelm@20140
   388
wenzelm@20140
   389
lemma Subtype_canTs:
wenzelm@20140
   390
  "!!a b A B P. a : {x:A. b:{y:B(a).P(<x,y>)}} ==> <a,b> : {x:Sigma(A,B).P(x)}"
wenzelm@20140
   391
  "!!a A B P. a : {x:A. P(inl(x))} ==> inl(a) : {x:A+B. P(x)}"
wenzelm@20140
   392
  "!!b A B P. b : {x:B. P(inr(x))} ==> inr(b) : {x:A+B. P(x)}"
wenzelm@20140
   393
  "!!a P. a : {x:Nat. P(succ(x))} ==> succ(a) : {x:Nat. P(x)}"
wenzelm@20140
   394
  "!!h t A P. h : {x:A. t : {y:List(A).P(x$y)}} ==> h$t : {x:List(A).P(x)}"
wenzelm@20140
   395
  by (assumption | rule SubtypeI canTs icanTs | erule SubtypeE)+
wenzelm@20140
   396
wenzelm@20140
   397
lemma letT: "[| f(t):B;  ~t=bot  |] ==> let x be t in f(x) : B"
wenzelm@20140
   398
  apply (erule letB [THEN ssubst])
wenzelm@20140
   399
  apply assumption
wenzelm@20140
   400
  done
wenzelm@20140
   401
wenzelm@20140
   402
lemma applyT2: "[| a:A;  f : Pi(A,B)  |] ==> f ` a  : B(a)"
wenzelm@20140
   403
  apply (erule applyT)
wenzelm@20140
   404
  apply assumption
wenzelm@20140
   405
  done
wenzelm@20140
   406
wenzelm@20140
   407
lemma rcall_lemma1: "[| a:A;  a:A ==> P(a) |] ==> a : {x:A. P(x)}"
wenzelm@20140
   408
  by blast
wenzelm@20140
   409
wenzelm@20140
   410
lemma rcall_lemma2: "[| a:{x:A. Q(x)};  [| a:A; Q(a) |] ==> P(a) |] ==> a : {x:A. P(x)}"
wenzelm@20140
   411
  by blast
wenzelm@20140
   412
wenzelm@20140
   413
lemmas rcall_lemmas = asm_rl rcall_lemma1 SubtypeD1 rcall_lemma2
wenzelm@20140
   414
wenzelm@20140
   415
wenzelm@20140
   416
subsection {* Typechecking *}
wenzelm@20140
   417
wenzelm@20140
   418
ML {*
wenzelm@20140
   419
wenzelm@20140
   420
local
wenzelm@20140
   421
wenzelm@20140
   422
val type_rls =
wenzelm@27221
   423
  @{thms canTs} @ @{thms icanTs} @ @{thms applyT2} @ @{thms ncanTs} @ @{thms incanTs} @
wenzelm@27221
   424
  @{thms precTs} @ @{thms letrecTs} @ @{thms letT} @ @{thms Subtype_canTs};
wenzelm@20140
   425
haftmann@38500
   426
fun bvars (Const(@{const_name all},_) $ Abs(s,_,t)) l = bvars t (s::l)
wenzelm@20140
   427
  | bvars _ l = l
wenzelm@20140
   428
haftmann@38500
   429
fun get_bno l n (Const(@{const_name all},_) $ Abs(s,_,t)) = get_bno (s::l) n t
haftmann@38500
   430
  | get_bno l n (Const(@{const_name Trueprop},_) $ t) = get_bno l n t
haftmann@38500
   431
  | get_bno l n (Const(@{const_name Ball},_) $ _ $ Abs(s,_,t)) = get_bno (s::l) (n+1) t
haftmann@38500
   432
  | get_bno l n (Const(@{const_name mem},_) $ t $ _) = get_bno l n t
wenzelm@20140
   433
  | get_bno l n (t $ s) = get_bno l n t
wenzelm@20140
   434
  | get_bno l n (Bound m) = (m-length(l),n)
wenzelm@20140
   435
wenzelm@20140
   436
(* Not a great way of identifying induction hypothesis! *)
wenzelm@29269
   437
fun could_IH x = Term.could_unify(x,hd (prems_of @{thm rcallT})) orelse
wenzelm@29269
   438
                 Term.could_unify(x,hd (prems_of @{thm rcall2T})) orelse
wenzelm@29269
   439
                 Term.could_unify(x,hd (prems_of @{thm rcall3T}))
wenzelm@20140
   440
wenzelm@20140
   441
fun IHinst tac rls = SUBGOAL (fn (Bi,i) =>
wenzelm@20140
   442
  let val bvs = bvars Bi []
wenzelm@33317
   443
      val ihs = filter could_IH (Logic.strip_assums_hyp Bi)
wenzelm@20140
   444
      val rnames = map (fn x=>
wenzelm@20140
   445
                    let val (a,b) = get_bno [] 0 x
wenzelm@42364
   446
                    in (nth bvs a, b) end) ihs
wenzelm@20140
   447
      fun try_IHs [] = no_tac
wenzelm@42364
   448
        | try_IHs ((x,y)::xs) = tac [(("g", 0), x)] (nth rls (y - 1)) i ORELSE (try_IHs xs)
wenzelm@20140
   449
  in try_IHs rnames end)
wenzelm@20140
   450
wenzelm@20140
   451
fun is_rigid_prog t =
wenzelm@20140
   452
     case (Logic.strip_assums_concl t) of
haftmann@38500
   453
        (Const(@{const_name Trueprop},_) $ (Const(@{const_name mem},_) $ a $ _)) => null (Term.add_vars a [])
wenzelm@20140
   454
       | _ => false
wenzelm@20140
   455
in
wenzelm@20140
   456
wenzelm@27221
   457
fun rcall_tac ctxt i =
wenzelm@27239
   458
  let fun tac ps rl i = res_inst_tac ctxt ps rl i THEN atac i
wenzelm@27221
   459
  in IHinst tac @{thms rcallTs} i end
wenzelm@27221
   460
  THEN eresolve_tac @{thms rcall_lemmas} i
wenzelm@20140
   461
wenzelm@27221
   462
fun raw_step_tac ctxt prems i = ares_tac (prems@type_rls) i ORELSE
wenzelm@27221
   463
                           rcall_tac ctxt i ORELSE
wenzelm@27221
   464
                           ematch_tac [@{thm SubtypeE}] i ORELSE
wenzelm@27221
   465
                           match_tac [@{thm SubtypeI}] i
wenzelm@20140
   466
wenzelm@27221
   467
fun tc_step_tac ctxt prems = SUBGOAL (fn (Bi,i) =>
wenzelm@27221
   468
          if is_rigid_prog Bi then raw_step_tac ctxt prems i else no_tac)
wenzelm@20140
   469
wenzelm@27221
   470
fun typechk_tac ctxt rls i = SELECT_GOAL (REPEAT_FIRST (tc_step_tac ctxt rls)) i
wenzelm@20140
   471
wenzelm@27221
   472
fun tac ctxt = typechk_tac ctxt [] 1
wenzelm@20140
   473
wenzelm@20140
   474
(*** Clean up Correctness Condictions ***)
wenzelm@20140
   475
wenzelm@27221
   476
val clean_ccs_tac = REPEAT_FIRST (eresolve_tac ([@{thm SubtypeE}] @ @{thms rmIHs}) ORELSE'
wenzelm@20140
   477
                                 hyp_subst_tac)
wenzelm@20140
   478
wenzelm@27221
   479
fun clean_ccs_tac ctxt =
wenzelm@27239
   480
  let fun tac ps rl i = eres_inst_tac ctxt ps rl i THEN atac i in
wenzelm@27221
   481
    TRY (REPEAT_FIRST (IHinst tac @{thms hyprcallTs} ORELSE'
wenzelm@27221
   482
    eresolve_tac ([asm_rl, @{thm SubtypeE}] @ @{thms rmIHs}) ORELSE'
wenzelm@27221
   483
    hyp_subst_tac))
wenzelm@27221
   484
  end
wenzelm@20140
   485
wenzelm@27221
   486
fun gen_ccs_tac ctxt rls i =
wenzelm@27221
   487
  SELECT_GOAL (REPEAT_FIRST (tc_step_tac ctxt rls) THEN clean_ccs_tac ctxt) i
wenzelm@17456
   488
clasohm@0
   489
end
wenzelm@20140
   490
*}
wenzelm@20140
   491
wenzelm@20140
   492
wenzelm@20140
   493
subsection {* Evaluation *}
wenzelm@20140
   494
wenzelm@20140
   495
ML {*
wenzelm@20140
   496
wenzelm@20140
   497
local
wenzelm@31902
   498
  structure Data = Named_Thms(val name = "eval" val description = "evaluation rules");
wenzelm@20140
   499
in
wenzelm@20140
   500
wenzelm@32282
   501
fun eval_tac ths =
wenzelm@32283
   502
  Subgoal.FOCUS_PREMS (fn {context, prems, ...} =>
wenzelm@32282
   503
    DEPTH_SOLVE_1 (resolve_tac (ths @ prems @ Data.get context) 1));
wenzelm@20140
   504
wenzelm@20140
   505
val eval_setup =
wenzelm@24034
   506
  Data.setup #>
wenzelm@30515
   507
  Method.setup @{binding eval}
wenzelm@32282
   508
    (Attrib.thms >> (fn ths => fn ctxt => SIMPLE_METHOD' (CHANGED o eval_tac ths ctxt)))
wenzelm@30515
   509
    "evaluation";
wenzelm@20140
   510
wenzelm@20140
   511
end;
wenzelm@20140
   512
wenzelm@20140
   513
*}
wenzelm@20140
   514
wenzelm@20140
   515
setup eval_setup
wenzelm@20140
   516
wenzelm@20140
   517
lemmas eval_rls [eval] = trueV falseV pairV lamV caseVtrue caseVfalse caseVpair caseVlam
wenzelm@20140
   518
wenzelm@20140
   519
lemma applyV [eval]:
wenzelm@20140
   520
  assumes "f ---> lam x. b(x)"
wenzelm@20140
   521
    and "b(a) ---> c"
wenzelm@20140
   522
  shows "f ` a ---> c"
wenzelm@41526
   523
  unfolding apply_def by (eval assms)
wenzelm@20140
   524
wenzelm@20140
   525
lemma letV:
wenzelm@20140
   526
  assumes 1: "t ---> a"
wenzelm@20140
   527
    and 2: "f(a) ---> c"
wenzelm@20140
   528
  shows "let x be t in f(x) ---> c"
wenzelm@20140
   529
  apply (unfold let_def)
wenzelm@20140
   530
  apply (rule 1 [THEN canonical])
wenzelm@20140
   531
  apply (tactic {*
wenzelm@26391
   532
    REPEAT (DEPTH_SOLVE_1 (resolve_tac (@{thms assms} @ @{thms eval_rls}) 1 ORELSE
wenzelm@26391
   533
      etac @{thm substitute} 1)) *})
wenzelm@20140
   534
  done
wenzelm@20140
   535
wenzelm@20140
   536
lemma fixV: "f(fix(f)) ---> c ==> fix(f) ---> c"
wenzelm@20140
   537
  apply (unfold fix_def)
wenzelm@20140
   538
  apply (rule applyV)
wenzelm@20140
   539
   apply (rule lamV)
wenzelm@20140
   540
  apply assumption
wenzelm@20140
   541
  done
wenzelm@20140
   542
wenzelm@20140
   543
lemma letrecV:
wenzelm@20140
   544
  "h(t,%y. letrec g x be h(x,g) in g(y)) ---> c ==>  
wenzelm@20140
   545
                 letrec g x be h(x,g) in g(t) ---> c"
wenzelm@20140
   546
  apply (unfold letrec_def)
wenzelm@20140
   547
  apply (assumption | rule fixV applyV  lamV)+
wenzelm@20140
   548
  done
wenzelm@20140
   549
wenzelm@20140
   550
lemmas [eval] = letV letrecV fixV
wenzelm@20140
   551
wenzelm@20140
   552
lemma V_rls [eval]:
wenzelm@20140
   553
  "true ---> true"
wenzelm@20140
   554
  "false ---> false"
wenzelm@20140
   555
  "!!b c t u. [| b--->true;  t--->c |] ==> if b then t else u ---> c"
wenzelm@20140
   556
  "!!b c t u. [| b--->false;  u--->c |] ==> if b then t else u ---> c"
wenzelm@20140
   557
  "!!a b. <a,b> ---> <a,b>"
wenzelm@20140
   558
  "!!a b c t h. [| t ---> <a,b>;  h(a,b) ---> c |] ==> split(t,h) ---> c"
wenzelm@20140
   559
  "zero ---> zero"
wenzelm@20140
   560
  "!!n. succ(n) ---> succ(n)"
wenzelm@20140
   561
  "!!c n t u. [| n ---> zero; t ---> c |] ==> ncase(n,t,u) ---> c"
wenzelm@20140
   562
  "!!c n t u x. [| n ---> succ(x); u(x) ---> c |] ==> ncase(n,t,u) ---> c"
wenzelm@20140
   563
  "!!c n t u. [| n ---> zero; t ---> c |] ==> nrec(n,t,u) ---> c"
wenzelm@20140
   564
  "!!c n t u x. [| n--->succ(x); u(x,nrec(x,t,u))--->c |] ==> nrec(n,t,u)--->c"
wenzelm@20140
   565
  "[] ---> []"
wenzelm@20140
   566
  "!!h t. h$t ---> h$t"
wenzelm@20140
   567
  "!!c l t u. [| l ---> []; t ---> c |] ==> lcase(l,t,u) ---> c"
wenzelm@20140
   568
  "!!c l t u x xs. [| l ---> x$xs; u(x,xs) ---> c |] ==> lcase(l,t,u) ---> c"
wenzelm@20140
   569
  "!!c l t u. [| l ---> []; t ---> c |] ==> lrec(l,t,u) ---> c"
wenzelm@20140
   570
  "!!c l t u x xs. [| l--->x$xs; u(x,xs,lrec(xs,t,u))--->c |] ==> lrec(l,t,u)--->c"
wenzelm@20140
   571
  unfolding data_defs by eval+
wenzelm@20140
   572
wenzelm@20140
   573
wenzelm@20140
   574
subsection {* Factorial *}
wenzelm@20140
   575
wenzelm@36319
   576
schematic_lemma
wenzelm@20140
   577
  "letrec f n be ncase(n,succ(zero),%x. nrec(n,zero,%y g. nrec(f(x),g,%z h. succ(h))))  
wenzelm@20140
   578
   in f(succ(succ(zero))) ---> ?a"
wenzelm@20140
   579
  by eval
wenzelm@20140
   580
wenzelm@36319
   581
schematic_lemma
wenzelm@20140
   582
  "letrec f n be ncase(n,succ(zero),%x. nrec(n,zero,%y g. nrec(f(x),g,%z h. succ(h))))  
wenzelm@20140
   583
   in f(succ(succ(succ(zero)))) ---> ?a"
wenzelm@20140
   584
  by eval
wenzelm@20140
   585
wenzelm@20140
   586
subsection {* Less Than Or Equal *}
wenzelm@20140
   587
wenzelm@36319
   588
schematic_lemma
wenzelm@20140
   589
  "letrec f p be split(p,%m n. ncase(m,true,%x. ncase(n,false,%y. f(<x,y>))))
wenzelm@20140
   590
   in f(<succ(zero), succ(zero)>) ---> ?a"
wenzelm@20140
   591
  by eval
wenzelm@20140
   592
wenzelm@36319
   593
schematic_lemma
wenzelm@20140
   594
  "letrec f p be split(p,%m n. ncase(m,true,%x. ncase(n,false,%y. f(<x,y>))))
wenzelm@20140
   595
   in f(<succ(zero), succ(succ(succ(succ(zero))))>) ---> ?a"
wenzelm@20140
   596
  by eval
wenzelm@20140
   597
wenzelm@36319
   598
schematic_lemma
wenzelm@20140
   599
  "letrec f p be split(p,%m n. ncase(m,true,%x. ncase(n,false,%y. f(<x,y>))))
wenzelm@20140
   600
   in f(<succ(succ(succ(succ(succ(zero))))), succ(succ(succ(succ(zero))))>) ---> ?a"
wenzelm@20140
   601
  by eval
wenzelm@20140
   602
wenzelm@20140
   603
wenzelm@20140
   604
subsection {* Reverse *}
wenzelm@20140
   605
wenzelm@36319
   606
schematic_lemma
wenzelm@20140
   607
  "letrec id l be lcase(l,[],%x xs. x$id(xs))  
wenzelm@20140
   608
   in id(zero$succ(zero)$[]) ---> ?a"
wenzelm@20140
   609
  by eval
wenzelm@20140
   610
wenzelm@36319
   611
schematic_lemma
wenzelm@20140
   612
  "letrec rev l be lcase(l,[],%x xs. lrec(rev(xs),x$[],%y ys g. y$g))  
wenzelm@20140
   613
   in rev(zero$succ(zero)$(succ((lam x. x)`succ(zero)))$([])) ---> ?a"
wenzelm@20140
   614
  by eval
wenzelm@20140
   615
wenzelm@20140
   616
end