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