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