src/CTT/CTT.thy
author wenzelm
Fri May 21 21:14:18 2004 +0200 (2004-05-21)
changeset 14765 bafb24c150c1
parent 14565 c6dc17aab88a
child 14854 61bdf2ae4dc5
permissions -rw-r--r--
proper use of 'syntax';
     1 (*  Title:      CTT/ctt.thy
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1993  University of Cambridge
     5 
     6 Constructive Type Theory
     7 *)
     8 
     9 CTT = Pure +
    10 
    11 types
    12   i
    13   t
    14   o
    15 
    16 arities
    17    i,t,o :: logic
    18 
    19 consts
    20   (*Types*)
    21   F,T       :: "t"          (*F is empty, T contains one element*)
    22   contr     :: "i=>i"
    23   tt        :: "i"
    24   (*Natural numbers*)
    25   N         :: "t"
    26   succ      :: "i=>i"
    27   rec       :: "[i, i, [i,i]=>i] => i"
    28   (*Unions*)
    29   inl,inr   :: "i=>i"
    30   when      :: "[i, i=>i, i=>i]=>i"
    31   (*General Sum and Binary Product*)
    32   Sum       :: "[t, i=>t]=>t"
    33   fst,snd   :: "i=>i"
    34   split     :: "[i, [i,i]=>i] =>i"
    35   (*General Product and Function Space*)
    36   Prod      :: "[t, i=>t]=>t"
    37   (*Types*)
    38   "+"       :: "[t,t]=>t"           (infixr 40)
    39   (*Equality type*)
    40   Eq        :: "[t,i,i]=>t"
    41   eq        :: "i"
    42   (*Judgements*)
    43   Type      :: "t => prop"          ("(_ type)" [10] 5)
    44   Eqtype    :: "[t,t]=>prop"        ("(_ =/ _)" [10,10] 5)
    45   Elem      :: "[i, t]=>prop"       ("(_ /: _)" [10,10] 5)
    46   Eqelem    :: "[i,i,t]=>prop"      ("(_ =/ _ :/ _)" [10,10,10] 5)
    47   Reduce    :: "[i,i]=>prop"        ("Reduce[_,_]")
    48   (*Types*)
    49 
    50   (*Functions*)
    51   lambda    :: "(i => i) => i"      (binder "lam " 10)
    52   "`"       :: "[i,i]=>i"           (infixl 60)
    53   (*Natural numbers*)
    54   "0"       :: "i"                  ("0")
    55   (*Pairing*)
    56   pair      :: "[i,i]=>i"           ("(1<_,/_>)")
    57 
    58 syntax
    59   "@PROD"   :: "[idt,t,t]=>t"       ("(3PROD _:_./ _)" 10)
    60   "@SUM"    :: "[idt,t,t]=>t"       ("(3SUM _:_./ _)" 10)
    61   "@-->"    :: "[t,t]=>t"           ("(_ -->/ _)" [31,30] 30)
    62   "@*"      :: "[t,t]=>t"           ("(_ */ _)" [51,50] 50)
    63 
    64 translations
    65   "PROD x:A. B" => "Prod(A, %x. B)"
    66   "A --> B"     => "Prod(A, _K(B))"
    67   "SUM x:A. B"  => "Sum(A, %x. B)"
    68   "A * B"       => "Sum(A, _K(B))"
    69 
    70 syntax (xsymbols)
    71   "@-->"    :: "[t,t]=>t"           ("(_ \\<longrightarrow>/ _)" [31,30] 30)
    72   "@*"      :: "[t,t]=>t"           ("(_ \\<times>/ _)"          [51,50] 50)
    73   Elem      :: "[i, t]=>prop"       ("(_ /\\<in> _)" [10,10] 5)
    74   Eqelem    :: "[i,i,t]=>prop"      ("(2_ =/ _ \\<in>/ _)" [10,10,10] 5)
    75   "@SUM"    :: "[idt,t,t] => t"     ("(3\\<Sigma> _\\<in>_./ _)" 10)
    76   "@PROD"   :: "[idt,t,t] => t"     ("(3\\<Pi> _\\<in>_./ _)"    10)
    77   "lam "    :: "[idts, i] => i"     ("(3\\<lambda>\\<lambda>_./ _)" 10)
    78 
    79 syntax (HTML output)
    80   "@*"      :: "[t,t]=>t"           ("(_ \\<times>/ _)"          [51,50] 50)
    81   Elem      :: "[i, t]=>prop"       ("(_ /\\<in> _)" [10,10] 5)
    82   Eqelem    :: "[i,i,t]=>prop"      ("(2_ =/ _ \\<in>/ _)" [10,10,10] 5)
    83   "@SUM"    :: "[idt,t,t] => t"     ("(3\\<Sigma> _\\<in>_./ _)" 10)
    84   "@PROD"   :: "[idt,t,t] => t"     ("(3\\<Pi> _\\<in>_./ _)"    10)
    85   "lam "    :: "[idts, i] => i"     ("(3\\<lambda>\\<lambda>_./ _)" 10)
    86 
    87 rules
    88 
    89   (*Reduction: a weaker notion than equality;  a hack for simplification.
    90     Reduce[a,b] means either that  a=b:A  for some A or else that "a" and "b"
    91     are textually identical.*)
    92 
    93   (*does not verify a:A!  Sound because only trans_red uses a Reduce premise
    94     No new theorems can be proved about the standard judgements.*)
    95   refl_red "Reduce[a,a]"
    96   red_if_equal "a = b : A ==> Reduce[a,b]"
    97   trans_red "[| a = b : A;  Reduce[b,c] |] ==> a = c : A"
    98 
    99   (*Reflexivity*)
   100 
   101   refl_type "A type ==> A = A"
   102   refl_elem "a : A ==> a = a : A"
   103 
   104   (*Symmetry*)
   105 
   106   sym_type  "A = B ==> B = A"
   107   sym_elem  "a = b : A ==> b = a : A"
   108 
   109   (*Transitivity*)
   110 
   111   trans_type   "[| A = B;  B = C |] ==> A = C"
   112   trans_elem   "[| a = b : A;  b = c : A |] ==> a = c : A"
   113 
   114   equal_types  "[| a : A;  A = B |] ==> a : B"
   115   equal_typesL "[| a = b : A;  A = B |] ==> a = b : B"
   116 
   117   (*Substitution*)
   118 
   119   subst_type   "[| a : A;  !!z. z:A ==> B(z) type |] ==> B(a) type"
   120   subst_typeL  "[| a = c : A;  !!z. z:A ==> B(z) = D(z) |] ==> B(a) = D(c)"
   121 
   122   subst_elem   "[| a : A;  !!z. z:A ==> b(z):B(z) |] ==> b(a):B(a)"
   123   subst_elemL
   124     "[| a=c : A;  !!z. z:A ==> b(z)=d(z) : B(z) |] ==> b(a)=d(c) : B(a)"
   125 
   126 
   127   (*The type N -- natural numbers*)
   128 
   129   NF "N type"
   130   NI0 "0 : N"
   131   NI_succ "a : N ==> succ(a) : N"
   132   NI_succL  "a = b : N ==> succ(a) = succ(b) : N"
   133 
   134   NE
   135    "[| p: N;  a: C(0);  !!u v. [| u: N; v: C(u) |] ==> b(u,v): C(succ(u)) |] 
   136    ==> rec(p, a, %u v. b(u,v)) : C(p)"
   137 
   138   NEL
   139    "[| p = q : N;  a = c : C(0);  
   140       !!u v. [| u: N; v: C(u) |] ==> b(u,v) = d(u,v): C(succ(u)) |] 
   141    ==> rec(p, a, %u v. b(u,v)) = rec(q,c,d) : C(p)"
   142 
   143   NC0
   144    "[| a: C(0);  !!u v. [| u: N; v: C(u) |] ==> b(u,v): C(succ(u)) |] 
   145    ==> rec(0, a, %u v. b(u,v)) = a : C(0)"
   146 
   147   NC_succ
   148    "[| p: N;  a: C(0);  
   149        !!u v. [| u: N; v: C(u) |] ==> b(u,v): C(succ(u)) |] ==>  
   150    rec(succ(p), a, %u v. b(u,v)) = b(p, rec(p, a, %u v. b(u,v))) : C(succ(p))"
   151 
   152   (*The fourth Peano axiom.  See page 91 of Martin-Lof's book*)
   153   zero_ne_succ
   154     "[| a: N;  0 = succ(a) : N |] ==> 0: F"
   155 
   156 
   157   (*The Product of a family of types*)
   158 
   159   ProdF  "[| A type; !!x. x:A ==> B(x) type |] ==> PROD x:A. B(x) type"
   160 
   161   ProdFL
   162    "[| A = C;  !!x. x:A ==> B(x) = D(x) |] ==> 
   163    PROD x:A. B(x) = PROD x:C. D(x)"
   164 
   165   ProdI
   166    "[| A type;  !!x. x:A ==> b(x):B(x)|] ==> lam x. b(x) : PROD x:A. B(x)"
   167 
   168   ProdIL
   169    "[| A type;  !!x. x:A ==> b(x) = c(x) : B(x)|] ==> 
   170    lam x. b(x) = lam x. c(x) : PROD x:A. B(x)"
   171 
   172   ProdE  "[| p : PROD x:A. B(x);  a : A |] ==> p`a : B(a)"
   173   ProdEL "[| p=q: PROD x:A. B(x);  a=b : A |] ==> p`a = q`b : B(a)"
   174 
   175   ProdC
   176    "[| a : A;  !!x. x:A ==> b(x) : B(x)|] ==> 
   177    (lam x. b(x)) ` a = b(a) : B(a)"
   178 
   179   ProdC2
   180    "p : PROD x:A. B(x) ==> (lam x. p`x) = p : PROD x:A. B(x)"
   181 
   182 
   183   (*The Sum of a family of types*)
   184 
   185   SumF  "[| A type;  !!x. x:A ==> B(x) type |] ==> SUM x:A. B(x) type"
   186   SumFL
   187     "[| A = C;  !!x. x:A ==> B(x) = D(x) |] ==> SUM x:A. B(x) = SUM x:C. D(x)"
   188 
   189   SumI  "[| a : A;  b : B(a) |] ==> <a,b> : SUM x:A. B(x)"
   190   SumIL "[| a=c:A;  b=d:B(a) |] ==> <a,b> = <c,d> : SUM x:A. B(x)"
   191 
   192   SumE
   193     "[| p: SUM x:A. B(x);  !!x y. [| x:A; y:B(x) |] ==> c(x,y): C(<x,y>) |] 
   194     ==> split(p, %x y. c(x,y)) : C(p)"
   195 
   196   SumEL
   197     "[| p=q : SUM x:A. B(x); 
   198        !!x y. [| x:A; y:B(x) |] ==> c(x,y)=d(x,y): C(<x,y>)|] 
   199     ==> split(p, %x y. c(x,y)) = split(q, % x y. d(x,y)) : C(p)"
   200 
   201   SumC
   202     "[| a: A;  b: B(a);  !!x y. [| x:A; y:B(x) |] ==> c(x,y): C(<x,y>) |] 
   203     ==> split(<a,b>, %x y. c(x,y)) = c(a,b) : C(<a,b>)"
   204 
   205   fst_def   "fst(a) == split(a, %x y. x)"
   206   snd_def   "snd(a) == split(a, %x y. y)"
   207 
   208 
   209   (*The sum of two types*)
   210 
   211   PlusF   "[| A type;  B type |] ==> A+B type"
   212   PlusFL  "[| A = C;  B = D |] ==> A+B = C+D"
   213 
   214   PlusI_inl   "[| a : A;  B type |] ==> inl(a) : A+B"
   215   PlusI_inlL "[| a = c : A;  B type |] ==> inl(a) = inl(c) : A+B"
   216 
   217   PlusI_inr   "[| A type;  b : B |] ==> inr(b) : A+B"
   218   PlusI_inrL "[| A type;  b = d : B |] ==> inr(b) = inr(d) : A+B"
   219 
   220   PlusE
   221     "[| p: A+B;  !!x. x:A ==> c(x): C(inl(x));  
   222                 !!y. y:B ==> d(y): C(inr(y)) |] 
   223     ==> when(p, %x. c(x), %y. d(y)) : C(p)"
   224 
   225   PlusEL
   226     "[| p = q : A+B;  !!x. x: A ==> c(x) = e(x) : C(inl(x));   
   227                      !!y. y: B ==> d(y) = f(y) : C(inr(y)) |] 
   228     ==> when(p, %x. c(x), %y. d(y)) = when(q, %x. e(x), %y. f(y)) : C(p)"
   229 
   230   PlusC_inl
   231     "[| a: A;  !!x. x:A ==> c(x): C(inl(x));  
   232               !!y. y:B ==> d(y): C(inr(y)) |] 
   233     ==> when(inl(a), %x. c(x), %y. d(y)) = c(a) : C(inl(a))"
   234 
   235   PlusC_inr
   236     "[| b: B;  !!x. x:A ==> c(x): C(inl(x));  
   237               !!y. y:B ==> d(y): C(inr(y)) |] 
   238     ==> when(inr(b), %x. c(x), %y. d(y)) = d(b) : C(inr(b))"
   239 
   240 
   241   (*The type Eq*)
   242 
   243   EqF    "[| A type;  a : A;  b : A |] ==> Eq(A,a,b) type"
   244   EqFL "[| A=B;  a=c: A;  b=d : A |] ==> Eq(A,a,b) = Eq(B,c,d)"
   245   EqI "a = b : A ==> eq : Eq(A,a,b)"
   246   EqE "p : Eq(A,a,b) ==> a = b : A"
   247 
   248   (*By equality of types, can prove C(p) from C(eq), an elimination rule*)
   249   EqC "p : Eq(A,a,b) ==> p = eq : Eq(A,a,b)"
   250 
   251   (*The type F*)
   252 
   253   FF "F type"
   254   FE "[| p: F;  C type |] ==> contr(p) : C"
   255   FEL  "[| p = q : F;  C type |] ==> contr(p) = contr(q) : C"
   256 
   257   (*The type T
   258      Martin-Lof's book (page 68) discusses elimination and computation.
   259      Elimination can be derived by computation and equality of types,
   260      but with an extra premise C(x) type x:T.
   261      Also computation can be derived from elimination. *)
   262 
   263   TF "T type"
   264   TI "tt : T"
   265   TE "[| p : T;  c : C(tt) |] ==> c : C(p)"
   266   TEL "[| p = q : T;  c = d : C(tt) |] ==> c = d : C(p)"
   267   TC "p : T ==> p = tt : T"
   268 end
   269 
   270 
   271 ML
   272 
   273 val print_translation =
   274   [("Prod", dependent_tr' ("@PROD", "@-->")),
   275    ("Sum", dependent_tr' ("@SUM", "@*"))];
   276