src/CTT/CTT.thy
author wenzelm
Fri Jun 02 18:15:38 2006 +0200 (2006-06-02)
changeset 19761 5cd82054c2c6
parent 17782 b3846df9d643
child 21210 c17fd2df4e9e
permissions -rw-r--r--
removed obsolete ML files;
wenzelm@17441
     1
(*  Title:      CTT/CTT.thy
clasohm@0
     2
    ID:         $Id$
clasohm@0
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   1993  University of Cambridge
clasohm@0
     5
*)
clasohm@0
     6
wenzelm@17441
     7
header {* Constructive Type Theory *}
clasohm@0
     8
wenzelm@17441
     9
theory CTT
wenzelm@17441
    10
imports Pure
wenzelm@19761
    11
uses "~~/src/Provers/typedsimp.ML" ("rew.ML")
wenzelm@17441
    12
begin
wenzelm@17441
    13
wenzelm@17441
    14
typedecl i
wenzelm@17441
    15
typedecl t
wenzelm@17441
    16
typedecl o
clasohm@0
    17
clasohm@0
    18
consts
clasohm@0
    19
  (*Types*)
wenzelm@17441
    20
  F         :: "t"
wenzelm@17441
    21
  T         :: "t"          (*F is empty, T contains one element*)
clasohm@0
    22
  contr     :: "i=>i"
clasohm@0
    23
  tt        :: "i"
clasohm@0
    24
  (*Natural numbers*)
clasohm@0
    25
  N         :: "t"
clasohm@0
    26
  succ      :: "i=>i"
clasohm@0
    27
  rec       :: "[i, i, [i,i]=>i] => i"
clasohm@0
    28
  (*Unions*)
wenzelm@17441
    29
  inl       :: "i=>i"
wenzelm@17441
    30
  inr       :: "i=>i"
clasohm@0
    31
  when      :: "[i, i=>i, i=>i]=>i"
clasohm@0
    32
  (*General Sum and Binary Product*)
clasohm@0
    33
  Sum       :: "[t, i=>t]=>t"
wenzelm@17441
    34
  fst       :: "i=>i"
wenzelm@17441
    35
  snd       :: "i=>i"
clasohm@0
    36
  split     :: "[i, [i,i]=>i] =>i"
clasohm@0
    37
  (*General Product and Function Space*)
clasohm@0
    38
  Prod      :: "[t, i=>t]=>t"
wenzelm@14765
    39
  (*Types*)
wenzelm@14765
    40
  "+"       :: "[t,t]=>t"           (infixr 40)
clasohm@0
    41
  (*Equality type*)
clasohm@0
    42
  Eq        :: "[t,i,i]=>t"
clasohm@0
    43
  eq        :: "i"
clasohm@0
    44
  (*Judgements*)
clasohm@0
    45
  Type      :: "t => prop"          ("(_ type)" [10] 5)
paulson@10467
    46
  Eqtype    :: "[t,t]=>prop"        ("(_ =/ _)" [10,10] 5)
clasohm@0
    47
  Elem      :: "[i, t]=>prop"       ("(_ /: _)" [10,10] 5)
paulson@10467
    48
  Eqelem    :: "[i,i,t]=>prop"      ("(_ =/ _ :/ _)" [10,10,10] 5)
clasohm@0
    49
  Reduce    :: "[i,i]=>prop"        ("Reduce[_,_]")
clasohm@0
    50
  (*Types*)
wenzelm@14765
    51
clasohm@0
    52
  (*Functions*)
clasohm@0
    53
  lambda    :: "(i => i) => i"      (binder "lam " 10)
clasohm@0
    54
  "`"       :: "[i,i]=>i"           (infixl 60)
clasohm@0
    55
  (*Natural numbers*)
clasohm@0
    56
  "0"       :: "i"                  ("0")
clasohm@0
    57
  (*Pairing*)
clasohm@0
    58
  pair      :: "[i,i]=>i"           ("(1<_,/_>)")
clasohm@0
    59
wenzelm@14765
    60
syntax
wenzelm@19761
    61
  "_PROD"   :: "[idt,t,t]=>t"       ("(3PROD _:_./ _)" 10)
wenzelm@19761
    62
  "_SUM"    :: "[idt,t,t]=>t"       ("(3SUM _:_./ _)" 10)
clasohm@0
    63
translations
wenzelm@19761
    64
  "PROD x:A. B" == "Prod(A, %x. B)"
wenzelm@19761
    65
  "SUM x:A. B"  == "Sum(A, %x. B)"
wenzelm@19761
    66
wenzelm@19761
    67
abbreviation
wenzelm@19761
    68
  Arrow     :: "[t,t]=>t"           (infixr "-->" 30)
wenzelm@19761
    69
  "A --> B == PROD _:A. B"
wenzelm@19761
    70
  Times     :: "[t,t]=>t"           (infixr "*" 50)
wenzelm@19761
    71
  "A * B == SUM _:A. B"
clasohm@0
    72
wenzelm@19761
    73
const_syntax (xsymbols)
wenzelm@19761
    74
  Elem  ("(_ /\<in> _)" [10,10] 5)
wenzelm@19761
    75
  Eqelem  ("(2_ =/ _ \<in>/ _)" [10,10,10] 5)
wenzelm@19761
    76
  Arrow  (infixr "\<longrightarrow>" 30)
wenzelm@19761
    77
  Times  (infixr "\<times>" 50)
wenzelm@17441
    78
wenzelm@19761
    79
const_syntax (HTML output)
wenzelm@19761
    80
  Elem  ("(_ /\<in> _)" [10,10] 5)
wenzelm@19761
    81
  Eqelem  ("(2_ =/ _ \<in>/ _)" [10,10,10] 5)
wenzelm@19761
    82
  Times  (infixr "\<times>" 50)
wenzelm@17441
    83
paulson@10467
    84
syntax (xsymbols)
wenzelm@17441
    85
  "@SUM"    :: "[idt,t,t] => t"     ("(3\<Sigma> _\<in>_./ _)" 10)
wenzelm@17441
    86
  "@PROD"   :: "[idt,t,t] => t"     ("(3\<Pi> _\<in>_./ _)"    10)
wenzelm@17441
    87
  "lam "    :: "[idts, i] => i"     ("(3\<lambda>\<lambda>_./ _)" 10)
paulson@10467
    88
kleing@14565
    89
syntax (HTML output)
wenzelm@17441
    90
  "@SUM"    :: "[idt,t,t] => t"     ("(3\<Sigma> _\<in>_./ _)" 10)
wenzelm@17441
    91
  "@PROD"   :: "[idt,t,t] => t"     ("(3\<Pi> _\<in>_./ _)"    10)
wenzelm@17441
    92
  "lam "    :: "[idts, i] => i"     ("(3\<lambda>\<lambda>_./ _)" 10)
kleing@14565
    93
wenzelm@17441
    94
axioms
clasohm@0
    95
clasohm@0
    96
  (*Reduction: a weaker notion than equality;  a hack for simplification.
clasohm@0
    97
    Reduce[a,b] means either that  a=b:A  for some A or else that "a" and "b"
clasohm@0
    98
    are textually identical.*)
clasohm@0
    99
clasohm@0
   100
  (*does not verify a:A!  Sound because only trans_red uses a Reduce premise
clasohm@0
   101
    No new theorems can be proved about the standard judgements.*)
wenzelm@17441
   102
  refl_red: "Reduce[a,a]"
wenzelm@17441
   103
  red_if_equal: "a = b : A ==> Reduce[a,b]"
wenzelm@17441
   104
  trans_red: "[| a = b : A;  Reduce[b,c] |] ==> a = c : A"
clasohm@0
   105
clasohm@0
   106
  (*Reflexivity*)
clasohm@0
   107
wenzelm@17441
   108
  refl_type: "A type ==> A = A"
wenzelm@17441
   109
  refl_elem: "a : A ==> a = a : A"
clasohm@0
   110
clasohm@0
   111
  (*Symmetry*)
clasohm@0
   112
wenzelm@17441
   113
  sym_type:  "A = B ==> B = A"
wenzelm@17441
   114
  sym_elem:  "a = b : A ==> b = a : A"
clasohm@0
   115
clasohm@0
   116
  (*Transitivity*)
clasohm@0
   117
wenzelm@17441
   118
  trans_type:   "[| A = B;  B = C |] ==> A = C"
wenzelm@17441
   119
  trans_elem:   "[| a = b : A;  b = c : A |] ==> a = c : A"
clasohm@0
   120
wenzelm@17441
   121
  equal_types:  "[| a : A;  A = B |] ==> a : B"
wenzelm@17441
   122
  equal_typesL: "[| a = b : A;  A = B |] ==> a = b : B"
clasohm@0
   123
clasohm@0
   124
  (*Substitution*)
clasohm@0
   125
wenzelm@17441
   126
  subst_type:   "[| a : A;  !!z. z:A ==> B(z) type |] ==> B(a) type"
wenzelm@17441
   127
  subst_typeL:  "[| a = c : A;  !!z. z:A ==> B(z) = D(z) |] ==> B(a) = D(c)"
clasohm@0
   128
wenzelm@17441
   129
  subst_elem:   "[| a : A;  !!z. z:A ==> b(z):B(z) |] ==> b(a):B(a)"
wenzelm@17441
   130
  subst_elemL:
clasohm@0
   131
    "[| a=c : A;  !!z. z:A ==> b(z)=d(z) : B(z) |] ==> b(a)=d(c) : B(a)"
clasohm@0
   132
clasohm@0
   133
clasohm@0
   134
  (*The type N -- natural numbers*)
clasohm@0
   135
wenzelm@17441
   136
  NF: "N type"
wenzelm@17441
   137
  NI0: "0 : N"
wenzelm@17441
   138
  NI_succ: "a : N ==> succ(a) : N"
wenzelm@17441
   139
  NI_succL:  "a = b : N ==> succ(a) = succ(b) : N"
clasohm@0
   140
wenzelm@17441
   141
  NE:
wenzelm@17441
   142
   "[| p: N;  a: C(0);  !!u v. [| u: N; v: C(u) |] ==> b(u,v): C(succ(u)) |]
wenzelm@3837
   143
   ==> rec(p, a, %u v. b(u,v)) : C(p)"
clasohm@0
   144
wenzelm@17441
   145
  NEL:
wenzelm@17441
   146
   "[| p = q : N;  a = c : C(0);
wenzelm@17441
   147
      !!u v. [| u: N; v: C(u) |] ==> b(u,v) = d(u,v): C(succ(u)) |]
wenzelm@3837
   148
   ==> rec(p, a, %u v. b(u,v)) = rec(q,c,d) : C(p)"
clasohm@0
   149
wenzelm@17441
   150
  NC0:
wenzelm@17441
   151
   "[| a: C(0);  !!u v. [| u: N; v: C(u) |] ==> b(u,v): C(succ(u)) |]
wenzelm@3837
   152
   ==> rec(0, a, %u v. b(u,v)) = a : C(0)"
clasohm@0
   153
wenzelm@17441
   154
  NC_succ:
wenzelm@17441
   155
   "[| p: N;  a: C(0);
wenzelm@17441
   156
       !!u v. [| u: N; v: C(u) |] ==> b(u,v): C(succ(u)) |] ==>
wenzelm@3837
   157
   rec(succ(p), a, %u v. b(u,v)) = b(p, rec(p, a, %u v. b(u,v))) : C(succ(p))"
clasohm@0
   158
clasohm@0
   159
  (*The fourth Peano axiom.  See page 91 of Martin-Lof's book*)
wenzelm@17441
   160
  zero_ne_succ:
clasohm@0
   161
    "[| a: N;  0 = succ(a) : N |] ==> 0: F"
clasohm@0
   162
clasohm@0
   163
clasohm@0
   164
  (*The Product of a family of types*)
clasohm@0
   165
wenzelm@17441
   166
  ProdF:  "[| A type; !!x. x:A ==> B(x) type |] ==> PROD x:A. B(x) type"
clasohm@0
   167
wenzelm@17441
   168
  ProdFL:
wenzelm@17441
   169
   "[| A = C;  !!x. x:A ==> B(x) = D(x) |] ==>
wenzelm@3837
   170
   PROD x:A. B(x) = PROD x:C. D(x)"
clasohm@0
   171
wenzelm@17441
   172
  ProdI:
wenzelm@3837
   173
   "[| A type;  !!x. x:A ==> b(x):B(x)|] ==> lam x. b(x) : PROD x:A. B(x)"
clasohm@0
   174
wenzelm@17441
   175
  ProdIL:
wenzelm@17441
   176
   "[| A type;  !!x. x:A ==> b(x) = c(x) : B(x)|] ==>
wenzelm@3837
   177
   lam x. b(x) = lam x. c(x) : PROD x:A. B(x)"
clasohm@0
   178
wenzelm@17441
   179
  ProdE:  "[| p : PROD x:A. B(x);  a : A |] ==> p`a : B(a)"
wenzelm@17441
   180
  ProdEL: "[| p=q: PROD x:A. B(x);  a=b : A |] ==> p`a = q`b : B(a)"
clasohm@0
   181
wenzelm@17441
   182
  ProdC:
wenzelm@17441
   183
   "[| a : A;  !!x. x:A ==> b(x) : B(x)|] ==>
wenzelm@3837
   184
   (lam x. b(x)) ` a = b(a) : B(a)"
clasohm@0
   185
wenzelm@17441
   186
  ProdC2:
wenzelm@3837
   187
   "p : PROD x:A. B(x) ==> (lam x. p`x) = p : PROD x:A. B(x)"
clasohm@0
   188
clasohm@0
   189
clasohm@0
   190
  (*The Sum of a family of types*)
clasohm@0
   191
wenzelm@17441
   192
  SumF:  "[| A type;  !!x. x:A ==> B(x) type |] ==> SUM x:A. B(x) type"
wenzelm@17441
   193
  SumFL:
wenzelm@3837
   194
    "[| A = C;  !!x. x:A ==> B(x) = D(x) |] ==> SUM x:A. B(x) = SUM x:C. D(x)"
clasohm@0
   195
wenzelm@17441
   196
  SumI:  "[| a : A;  b : B(a) |] ==> <a,b> : SUM x:A. B(x)"
wenzelm@17441
   197
  SumIL: "[| a=c:A;  b=d:B(a) |] ==> <a,b> = <c,d> : SUM x:A. B(x)"
clasohm@0
   198
wenzelm@17441
   199
  SumE:
wenzelm@17441
   200
    "[| p: SUM x:A. B(x);  !!x y. [| x:A; y:B(x) |] ==> c(x,y): C(<x,y>) |]
wenzelm@3837
   201
    ==> split(p, %x y. c(x,y)) : C(p)"
clasohm@0
   202
wenzelm@17441
   203
  SumEL:
wenzelm@17441
   204
    "[| p=q : SUM x:A. B(x);
wenzelm@17441
   205
       !!x y. [| x:A; y:B(x) |] ==> c(x,y)=d(x,y): C(<x,y>)|]
wenzelm@3837
   206
    ==> split(p, %x y. c(x,y)) = split(q, % x y. d(x,y)) : C(p)"
clasohm@0
   207
wenzelm@17441
   208
  SumC:
wenzelm@17441
   209
    "[| a: A;  b: B(a);  !!x y. [| x:A; y:B(x) |] ==> c(x,y): C(<x,y>) |]
wenzelm@3837
   210
    ==> split(<a,b>, %x y. c(x,y)) = c(a,b) : C(<a,b>)"
clasohm@0
   211
wenzelm@17441
   212
  fst_def:   "fst(a) == split(a, %x y. x)"
wenzelm@17441
   213
  snd_def:   "snd(a) == split(a, %x y. y)"
clasohm@0
   214
clasohm@0
   215
clasohm@0
   216
  (*The sum of two types*)
clasohm@0
   217
wenzelm@17441
   218
  PlusF:   "[| A type;  B type |] ==> A+B type"
wenzelm@17441
   219
  PlusFL:  "[| A = C;  B = D |] ==> A+B = C+D"
clasohm@0
   220
wenzelm@17441
   221
  PlusI_inl:   "[| a : A;  B type |] ==> inl(a) : A+B"
wenzelm@17441
   222
  PlusI_inlL: "[| a = c : A;  B type |] ==> inl(a) = inl(c) : A+B"
clasohm@0
   223
wenzelm@17441
   224
  PlusI_inr:   "[| A type;  b : B |] ==> inr(b) : A+B"
wenzelm@17441
   225
  PlusI_inrL: "[| A type;  b = d : B |] ==> inr(b) = inr(d) : A+B"
clasohm@0
   226
wenzelm@17441
   227
  PlusE:
wenzelm@17441
   228
    "[| p: A+B;  !!x. x:A ==> c(x): C(inl(x));
wenzelm@17441
   229
                !!y. y:B ==> d(y): C(inr(y)) |]
wenzelm@3837
   230
    ==> when(p, %x. c(x), %y. d(y)) : C(p)"
clasohm@0
   231
wenzelm@17441
   232
  PlusEL:
wenzelm@17441
   233
    "[| p = q : A+B;  !!x. x: A ==> c(x) = e(x) : C(inl(x));
wenzelm@17441
   234
                     !!y. y: B ==> d(y) = f(y) : C(inr(y)) |]
wenzelm@3837
   235
    ==> when(p, %x. c(x), %y. d(y)) = when(q, %x. e(x), %y. f(y)) : C(p)"
clasohm@0
   236
wenzelm@17441
   237
  PlusC_inl:
wenzelm@17441
   238
    "[| a: A;  !!x. x:A ==> c(x): C(inl(x));
wenzelm@17441
   239
              !!y. y:B ==> d(y): C(inr(y)) |]
wenzelm@3837
   240
    ==> when(inl(a), %x. c(x), %y. d(y)) = c(a) : C(inl(a))"
clasohm@0
   241
wenzelm@17441
   242
  PlusC_inr:
wenzelm@17441
   243
    "[| b: B;  !!x. x:A ==> c(x): C(inl(x));
wenzelm@17441
   244
              !!y. y:B ==> d(y): C(inr(y)) |]
wenzelm@3837
   245
    ==> when(inr(b), %x. c(x), %y. d(y)) = d(b) : C(inr(b))"
clasohm@0
   246
clasohm@0
   247
clasohm@0
   248
  (*The type Eq*)
clasohm@0
   249
wenzelm@17441
   250
  EqF:    "[| A type;  a : A;  b : A |] ==> Eq(A,a,b) type"
wenzelm@17441
   251
  EqFL: "[| A=B;  a=c: A;  b=d : A |] ==> Eq(A,a,b) = Eq(B,c,d)"
wenzelm@17441
   252
  EqI: "a = b : A ==> eq : Eq(A,a,b)"
wenzelm@17441
   253
  EqE: "p : Eq(A,a,b) ==> a = b : A"
clasohm@0
   254
clasohm@0
   255
  (*By equality of types, can prove C(p) from C(eq), an elimination rule*)
wenzelm@17441
   256
  EqC: "p : Eq(A,a,b) ==> p = eq : Eq(A,a,b)"
clasohm@0
   257
clasohm@0
   258
  (*The type F*)
clasohm@0
   259
wenzelm@17441
   260
  FF: "F type"
wenzelm@17441
   261
  FE: "[| p: F;  C type |] ==> contr(p) : C"
wenzelm@17441
   262
  FEL:  "[| p = q : F;  C type |] ==> contr(p) = contr(q) : C"
clasohm@0
   263
clasohm@0
   264
  (*The type T
clasohm@0
   265
     Martin-Lof's book (page 68) discusses elimination and computation.
clasohm@0
   266
     Elimination can be derived by computation and equality of types,
clasohm@0
   267
     but with an extra premise C(x) type x:T.
clasohm@0
   268
     Also computation can be derived from elimination. *)
clasohm@0
   269
wenzelm@17441
   270
  TF: "T type"
wenzelm@17441
   271
  TI: "tt : T"
wenzelm@17441
   272
  TE: "[| p : T;  c : C(tt) |] ==> c : C(p)"
wenzelm@17441
   273
  TEL: "[| p = q : T;  c = d : C(tt) |] ==> c = d : C(p)"
wenzelm@17441
   274
  TC: "p : T ==> p = tt : T"
clasohm@0
   275
wenzelm@19761
   276
wenzelm@19761
   277
subsection "Tactics and derived rules for Constructive Type Theory"
wenzelm@19761
   278
wenzelm@19761
   279
(*Formation rules*)
wenzelm@19761
   280
lemmas form_rls = NF ProdF SumF PlusF EqF FF TF
wenzelm@19761
   281
  and formL_rls = ProdFL SumFL PlusFL EqFL
wenzelm@19761
   282
wenzelm@19761
   283
(*Introduction rules
wenzelm@19761
   284
  OMITTED: EqI, because its premise is an eqelem, not an elem*)
wenzelm@19761
   285
lemmas intr_rls = NI0 NI_succ ProdI SumI PlusI_inl PlusI_inr TI
wenzelm@19761
   286
  and intrL_rls = NI_succL ProdIL SumIL PlusI_inlL PlusI_inrL
wenzelm@19761
   287
wenzelm@19761
   288
(*Elimination rules
wenzelm@19761
   289
  OMITTED: EqE, because its conclusion is an eqelem,  not an elem
wenzelm@19761
   290
           TE, because it does not involve a constructor *)
wenzelm@19761
   291
lemmas elim_rls = NE ProdE SumE PlusE FE
wenzelm@19761
   292
  and elimL_rls = NEL ProdEL SumEL PlusEL FEL
wenzelm@19761
   293
wenzelm@19761
   294
(*OMITTED: eqC are TC because they make rewriting loop: p = un = un = ... *)
wenzelm@19761
   295
lemmas comp_rls = NC0 NC_succ ProdC SumC PlusC_inl PlusC_inr
wenzelm@19761
   296
wenzelm@19761
   297
(*rules with conclusion a:A, an elem judgement*)
wenzelm@19761
   298
lemmas element_rls = intr_rls elim_rls
wenzelm@19761
   299
wenzelm@19761
   300
(*Definitions are (meta)equality axioms*)
wenzelm@19761
   301
lemmas basic_defs = fst_def snd_def
wenzelm@19761
   302
wenzelm@19761
   303
(*Compare with standard version: B is applied to UNSIMPLIFIED expression! *)
wenzelm@19761
   304
lemma SumIL2: "[| c=a : A;  d=b : B(a) |] ==> <c,d> = <a,b> : Sum(A,B)"
wenzelm@19761
   305
apply (rule sym_elem)
wenzelm@19761
   306
apply (rule SumIL)
wenzelm@19761
   307
apply (rule_tac [!] sym_elem)
wenzelm@19761
   308
apply assumption+
wenzelm@19761
   309
done
wenzelm@19761
   310
wenzelm@19761
   311
lemmas intrL2_rls = NI_succL ProdIL SumIL2 PlusI_inlL PlusI_inrL
wenzelm@19761
   312
wenzelm@19761
   313
(*Exploit p:Prod(A,B) to create the assumption z:B(a).
wenzelm@19761
   314
  A more natural form of product elimination. *)
wenzelm@19761
   315
lemma subst_prodE:
wenzelm@19761
   316
  assumes "p: Prod(A,B)"
wenzelm@19761
   317
    and "a: A"
wenzelm@19761
   318
    and "!!z. z: B(a) ==> c(z): C(z)"
wenzelm@19761
   319
  shows "c(p`a): C(p`a)"
wenzelm@19761
   320
apply (rule prems ProdE)+
wenzelm@19761
   321
done
wenzelm@19761
   322
wenzelm@19761
   323
wenzelm@19761
   324
subsection {* Tactics for type checking *}
wenzelm@19761
   325
wenzelm@19761
   326
ML {*
wenzelm@19761
   327
wenzelm@19761
   328
local
wenzelm@19761
   329
wenzelm@19761
   330
fun is_rigid_elem (Const("CTT.Elem",_) $ a $ _) = not(is_Var (head_of a))
wenzelm@19761
   331
  | is_rigid_elem (Const("CTT.Eqelem",_) $ a $ _ $ _) = not(is_Var (head_of a))
wenzelm@19761
   332
  | is_rigid_elem (Const("CTT.Type",_) $ a) = not(is_Var (head_of a))
wenzelm@19761
   333
  | is_rigid_elem _ = false
wenzelm@19761
   334
wenzelm@19761
   335
in
wenzelm@19761
   336
wenzelm@19761
   337
(*Try solving a:A or a=b:A by assumption provided a is rigid!*)
wenzelm@19761
   338
val test_assume_tac = SUBGOAL(fn (prem,i) =>
wenzelm@19761
   339
    if is_rigid_elem (Logic.strip_assums_concl prem)
wenzelm@19761
   340
    then  assume_tac i  else  no_tac)
wenzelm@19761
   341
wenzelm@19761
   342
fun ASSUME tf i = test_assume_tac i  ORELSE  tf i
wenzelm@19761
   343
wenzelm@19761
   344
end;
wenzelm@19761
   345
wenzelm@19761
   346
*}
wenzelm@19761
   347
wenzelm@19761
   348
(*For simplification: type formation and checking,
wenzelm@19761
   349
  but no equalities between terms*)
wenzelm@19761
   350
lemmas routine_rls = form_rls formL_rls refl_type element_rls
wenzelm@19761
   351
wenzelm@19761
   352
ML {*
wenzelm@19761
   353
local
wenzelm@19761
   354
  val routine_rls = thms "routine_rls";
wenzelm@19761
   355
  val form_rls = thms "form_rls";
wenzelm@19761
   356
  val intr_rls = thms "intr_rls";
wenzelm@19761
   357
  val element_rls = thms "element_rls";
wenzelm@19761
   358
  val equal_rls = form_rls @ element_rls @ thms "intrL_rls" @
wenzelm@19761
   359
    thms "elimL_rls" @ thms "refl_elem"
wenzelm@19761
   360
in
wenzelm@19761
   361
wenzelm@19761
   362
fun routine_tac rls prems = ASSUME (filt_resolve_tac (prems @ rls) 4);
wenzelm@19761
   363
wenzelm@19761
   364
(*Solve all subgoals "A type" using formation rules. *)
wenzelm@19761
   365
val form_tac = REPEAT_FIRST (ASSUME (filt_resolve_tac(form_rls) 1));
wenzelm@19761
   366
wenzelm@19761
   367
(*Type checking: solve a:A (a rigid, A flexible) by intro and elim rules. *)
wenzelm@19761
   368
fun typechk_tac thms =
wenzelm@19761
   369
  let val tac = filt_resolve_tac (thms @ form_rls @ element_rls) 3
wenzelm@19761
   370
  in  REPEAT_FIRST (ASSUME tac)  end
wenzelm@19761
   371
wenzelm@19761
   372
(*Solve a:A (a flexible, A rigid) by introduction rules.
wenzelm@19761
   373
  Cannot use stringtrees (filt_resolve_tac) since
wenzelm@19761
   374
  goals like ?a:SUM(A,B) have a trivial head-string *)
wenzelm@19761
   375
fun intr_tac thms =
wenzelm@19761
   376
  let val tac = filt_resolve_tac(thms@form_rls@intr_rls) 1
wenzelm@19761
   377
  in  REPEAT_FIRST (ASSUME tac)  end
wenzelm@19761
   378
wenzelm@19761
   379
(*Equality proving: solve a=b:A (where a is rigid) by long rules. *)
wenzelm@19761
   380
fun equal_tac thms =
wenzelm@19761
   381
  REPEAT_FIRST (ASSUME (filt_resolve_tac (thms @ equal_rls) 3))
clasohm@0
   382
wenzelm@17441
   383
end
wenzelm@19761
   384
wenzelm@19761
   385
*}
wenzelm@19761
   386
wenzelm@19761
   387
wenzelm@19761
   388
subsection {* Simplification *}
wenzelm@19761
   389
wenzelm@19761
   390
(*To simplify the type in a goal*)
wenzelm@19761
   391
lemma replace_type: "[| B = A;  a : A |] ==> a : B"
wenzelm@19761
   392
apply (rule equal_types)
wenzelm@19761
   393
apply (rule_tac [2] sym_type)
wenzelm@19761
   394
apply assumption+
wenzelm@19761
   395
done
wenzelm@19761
   396
wenzelm@19761
   397
(*Simplify the parameter of a unary type operator.*)
wenzelm@19761
   398
lemma subst_eqtyparg:
wenzelm@19761
   399
  assumes "a=c : A"
wenzelm@19761
   400
    and "!!z. z:A ==> B(z) type"
wenzelm@19761
   401
  shows "B(a)=B(c)"
wenzelm@19761
   402
apply (rule subst_typeL)
wenzelm@19761
   403
apply (rule_tac [2] refl_type)
wenzelm@19761
   404
apply (rule prems)
wenzelm@19761
   405
apply assumption
wenzelm@19761
   406
done
wenzelm@19761
   407
wenzelm@19761
   408
(*Simplification rules for Constructive Type Theory*)
wenzelm@19761
   409
lemmas reduction_rls = comp_rls [THEN trans_elem]
wenzelm@19761
   410
wenzelm@19761
   411
ML {*
wenzelm@19761
   412
local
wenzelm@19761
   413
  val EqI = thm "EqI";
wenzelm@19761
   414
  val EqE = thm "EqE";
wenzelm@19761
   415
  val NE = thm "NE";
wenzelm@19761
   416
  val FE = thm "FE";
wenzelm@19761
   417
  val ProdI = thm "ProdI";
wenzelm@19761
   418
  val SumI = thm "SumI";
wenzelm@19761
   419
  val SumE = thm "SumE";
wenzelm@19761
   420
  val PlusE = thm "PlusE";
wenzelm@19761
   421
  val PlusI_inl = thm "PlusI_inl";
wenzelm@19761
   422
  val PlusI_inr = thm "PlusI_inr";
wenzelm@19761
   423
  val subst_prodE = thm "subst_prodE";
wenzelm@19761
   424
  val intr_rls = thms "intr_rls";
wenzelm@19761
   425
in
wenzelm@19761
   426
wenzelm@19761
   427
(*Converts each goal "e : Eq(A,a,b)" into "a=b:A" for simplification.
wenzelm@19761
   428
  Uses other intro rules to avoid changing flexible goals.*)
wenzelm@19761
   429
val eqintr_tac = REPEAT_FIRST (ASSUME (filt_resolve_tac(EqI::intr_rls) 1))
wenzelm@19761
   430
wenzelm@19761
   431
(** Tactics that instantiate CTT-rules.
wenzelm@19761
   432
    Vars in the given terms will be incremented!
wenzelm@19761
   433
    The (rtac EqE i) lets them apply to equality judgements. **)
wenzelm@19761
   434
wenzelm@19761
   435
fun NE_tac (sp: string) i =
wenzelm@19761
   436
  TRY (rtac EqE i) THEN res_inst_tac [ ("p",sp) ] NE i
wenzelm@19761
   437
wenzelm@19761
   438
fun SumE_tac (sp: string) i =
wenzelm@19761
   439
  TRY (rtac EqE i) THEN res_inst_tac [ ("p",sp) ] SumE i
wenzelm@19761
   440
wenzelm@19761
   441
fun PlusE_tac (sp: string) i =
wenzelm@19761
   442
  TRY (rtac EqE i) THEN res_inst_tac [ ("p",sp) ] PlusE i
wenzelm@19761
   443
wenzelm@19761
   444
(** Predicate logic reasoning, WITH THINNING!!  Procedures adapted from NJ. **)
wenzelm@19761
   445
wenzelm@19761
   446
(*Finds f:Prod(A,B) and a:A in the assumptions, concludes there is z:B(a) *)
wenzelm@19761
   447
fun add_mp_tac i =
wenzelm@19761
   448
    rtac subst_prodE i  THEN  assume_tac i  THEN  assume_tac i
wenzelm@19761
   449
wenzelm@19761
   450
(*Finds P-->Q and P in the assumptions, replaces implication by Q *)
wenzelm@19761
   451
fun mp_tac i = etac subst_prodE i  THEN  assume_tac i
wenzelm@19761
   452
wenzelm@19761
   453
(*"safe" when regarded as predicate calculus rules*)
wenzelm@19761
   454
val safe_brls = sort (make_ord lessb)
wenzelm@19761
   455
    [ (true,FE), (true,asm_rl),
wenzelm@19761
   456
      (false,ProdI), (true,SumE), (true,PlusE) ]
wenzelm@19761
   457
wenzelm@19761
   458
val unsafe_brls =
wenzelm@19761
   459
    [ (false,PlusI_inl), (false,PlusI_inr), (false,SumI),
wenzelm@19761
   460
      (true,subst_prodE) ]
wenzelm@19761
   461
wenzelm@19761
   462
(*0 subgoals vs 1 or more*)
wenzelm@19761
   463
val (safe0_brls, safep_brls) =
wenzelm@19761
   464
    List.partition (curry (op =) 0 o subgoals_of_brl) safe_brls
wenzelm@19761
   465
wenzelm@19761
   466
fun safestep_tac thms i =
wenzelm@19761
   467
    form_tac  ORELSE
wenzelm@19761
   468
    resolve_tac thms i  ORELSE
wenzelm@19761
   469
    biresolve_tac safe0_brls i  ORELSE  mp_tac i  ORELSE
wenzelm@19761
   470
    DETERM (biresolve_tac safep_brls i)
wenzelm@19761
   471
wenzelm@19761
   472
fun safe_tac thms i = DEPTH_SOLVE_1 (safestep_tac thms i)
wenzelm@19761
   473
wenzelm@19761
   474
fun step_tac thms = safestep_tac thms  ORELSE'  biresolve_tac unsafe_brls
wenzelm@19761
   475
wenzelm@19761
   476
(*Fails unless it solves the goal!*)
wenzelm@19761
   477
fun pc_tac thms = DEPTH_SOLVE_1 o (step_tac thms)
wenzelm@19761
   478
wenzelm@19761
   479
end
wenzelm@19761
   480
*}
wenzelm@19761
   481
wenzelm@19761
   482
use "rew.ML"
wenzelm@19761
   483
wenzelm@19761
   484
wenzelm@19761
   485
subsection {* The elimination rules for fst/snd *}
wenzelm@19761
   486
wenzelm@19761
   487
lemma SumE_fst: "p : Sum(A,B) ==> fst(p) : A"
wenzelm@19761
   488
apply (unfold basic_defs)
wenzelm@19761
   489
apply (erule SumE)
wenzelm@19761
   490
apply assumption
wenzelm@19761
   491
done
wenzelm@19761
   492
wenzelm@19761
   493
(*The first premise must be p:Sum(A,B) !!*)
wenzelm@19761
   494
lemma SumE_snd:
wenzelm@19761
   495
  assumes major: "p: Sum(A,B)"
wenzelm@19761
   496
    and "A type"
wenzelm@19761
   497
    and "!!x. x:A ==> B(x) type"
wenzelm@19761
   498
  shows "snd(p) : B(fst(p))"
wenzelm@19761
   499
  apply (unfold basic_defs)
wenzelm@19761
   500
  apply (rule major [THEN SumE])
wenzelm@19761
   501
  apply (rule SumC [THEN subst_eqtyparg, THEN replace_type])
wenzelm@19761
   502
  apply (tactic {* typechk_tac (thms "prems") *})
wenzelm@19761
   503
  done
wenzelm@19761
   504
wenzelm@19761
   505
end