src/CTT/CTT.ML
author paulson
Wed Jul 05 17:42:06 2000 +0200 (2000-07-05)
changeset 9249 c71db8c28727
parent 4440 9ed4098074bc
child 9251 bd57acd44fc1
permissions -rw-r--r--
removed batch proofs
     1 (*  Title:      CTT/ctt.ML
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1991  University of Cambridge
     5 
     6 Tactics and lemmas for ctt.thy (Constructive Type Theory)
     7 *)
     8 
     9 (*Formation rules*)
    10 val form_rls = [NF, ProdF, SumF, PlusF, EqF, FF, TF]
    11 and formL_rls = [ProdFL, SumFL, PlusFL, EqFL];
    12 
    13  
    14 (*Introduction rules
    15   OMITTED: EqI, because its premise is an eqelem, not an elem*)
    16 val intr_rls = [NI0, NI_succ, ProdI, SumI, PlusI_inl, PlusI_inr, TI]
    17 and intrL_rls = [NI_succL, ProdIL, SumIL, PlusI_inlL, PlusI_inrL];
    18 
    19 
    20 (*Elimination rules
    21   OMITTED: EqE, because its conclusion is an eqelem,  not an elem
    22            TE, because it does not involve a constructor *)
    23 val elim_rls = [NE, ProdE, SumE, PlusE, FE]
    24 and elimL_rls = [NEL, ProdEL, SumEL, PlusEL, FEL];
    25 
    26 (*OMITTED: eqC are TC because they make rewriting loop: p = un = un = ... *)
    27 val comp_rls = [NC0, NC_succ, ProdC, SumC, PlusC_inl, PlusC_inr];
    28 
    29 (*rules with conclusion a:A, an elem judgement*)
    30 val element_rls = intr_rls @ elim_rls;
    31 
    32 (*Definitions are (meta)equality axioms*)
    33 val basic_defs = [fst_def,snd_def];
    34 
    35 (*Compare with standard version: B is applied to UNSIMPLIFIED expression! *)
    36 val prems= goal CTT.thy
    37     "[| c=a : A;  d=b : B(a) |] ==> <c,d> = <a,b> : Sum(A,B)";
    38 by (rtac sym_elem 1);
    39 by (rtac SumIL 1);
    40 by (ALLGOALS (rtac sym_elem ));
    41 by (ALLGOALS (resolve_tac prems)) ;
    42 qed "SumIL2";
    43 
    44 val intrL2_rls = [NI_succL, ProdIL, SumIL2, PlusI_inlL, PlusI_inrL];
    45 
    46 (*Exploit p:Prod(A,B) to create the assumption z:B(a).  
    47   A more natural form of product elimination. *)
    48 val prems= goal CTT.thy
    49     "[| p: Prod(A,B);  a: A;  !!z. z: B(a) ==> c(z): C(z) \
    50 \    |] ==> c(p`a): C(p`a)";
    51 by (REPEAT (resolve_tac (prems@[ProdE]) 1)) ;
    52 qed "subst_prodE";
    53 
    54 (** Tactics for type checking **)
    55 
    56 fun is_rigid_elem (Const("CTT.Elem",_) $ a $ _) = not (is_Var (head_of a))
    57   | is_rigid_elem _ = false;
    58 
    59 (*Try solving a:A by assumption provided a is rigid!*) 
    60 val test_assume_tac = SUBGOAL(fn (prem,i) =>
    61     if is_rigid_elem (Logic.strip_assums_concl prem)
    62     then  assume_tac i  else  no_tac);
    63 
    64 fun ASSUME tf i = test_assume_tac i  ORELSE  tf i;
    65 
    66 
    67 (*For simplification: type formation and checking,
    68   but no equalities between terms*)
    69 val routine_rls = form_rls @ formL_rls @ [refl_type] @ element_rls;
    70 
    71 fun routine_tac rls prems = ASSUME (filt_resolve_tac (prems @ rls) 4);
    72 
    73 
    74 (*Solve all subgoals "A type" using formation rules. *)
    75 val form_tac = REPEAT_FIRST (filt_resolve_tac(form_rls) 1);
    76 
    77 
    78 (*Type checking: solve a:A (a rigid, A flexible) by intro and elim rules. *)
    79 fun typechk_tac thms =
    80   let val tac = filt_resolve_tac (thms @ form_rls @ element_rls) 3
    81   in  REPEAT_FIRST (ASSUME tac)  end;
    82 
    83 
    84 (*Solve a:A (a flexible, A rigid) by introduction rules. 
    85   Cannot use stringtrees (filt_resolve_tac) since
    86   goals like ?a:SUM(A,B) have a trivial head-string *)
    87 fun intr_tac thms =
    88   let val tac = filt_resolve_tac(thms@form_rls@intr_rls) 1
    89   in  REPEAT_FIRST (ASSUME tac)  end;
    90 
    91 
    92 (*Equality proving: solve a=b:A (where a is rigid) by long rules. *)
    93 fun equal_tac thms =
    94   let val rls = thms @ form_rls @ element_rls @ intrL_rls @
    95                 elimL_rls @ [refl_elem]
    96   in  REPEAT_FIRST (ASSUME (filt_resolve_tac rls 3))  end;
    97 
    98 (*** Simplification ***)
    99 
   100 (*To simplify the type in a goal*)
   101 val prems= goal CTT.thy
   102     "[| B = A;  a : A |] ==> a : B";
   103 by (rtac equal_types 1);
   104 by (rtac sym_type 2);
   105 by (ALLGOALS (resolve_tac prems)) ;
   106 qed "replace_type";
   107 
   108 (*Simplify the parameter of a unary type operator.*)
   109 val prems= goal CTT.thy
   110     "a=c : A ==> (!!z. z:A ==> B(z) type) ==> B(a)=B(c)";
   111 by (rtac subst_typeL 1);
   112 by (rtac refl_type 2);
   113 by (ALLGOALS (resolve_tac prems));
   114 by (assume_tac 1) ;
   115 qed "subst_eqtyparg";
   116 
   117 (*Make a reduction rule for simplification.
   118   A goal a=c becomes b=c, by virtue of a=b *)
   119 fun resolve_trans rl = rl RS trans_elem;
   120 
   121 (*Simplification rules for Constructive Type Theory*)
   122 val reduction_rls = map resolve_trans comp_rls;
   123 
   124 (*Converts each goal "e : Eq(A,a,b)" into "a=b:A" for simplification.
   125   Uses other intro rules to avoid changing flexible goals.*)
   126 val eqintr_tac = REPEAT_FIRST (ASSUME (filt_resolve_tac(EqI::intr_rls) 1));
   127 
   128 (** Tactics that instantiate CTT-rules.
   129     Vars in the given terms will be incremented! 
   130     The (rtac EqE i) lets them apply to equality judgements. **)
   131 
   132 fun NE_tac (sp: string) i = 
   133   TRY (rtac EqE i) THEN res_inst_tac [ ("p",sp) ] NE i;
   134 
   135 fun SumE_tac (sp: string) i = 
   136   TRY (rtac EqE i) THEN res_inst_tac [ ("p",sp) ] SumE i;
   137 
   138 fun PlusE_tac (sp: string) i = 
   139   TRY (rtac EqE i) THEN res_inst_tac [ ("p",sp) ] PlusE i;
   140 
   141 (** Predicate logic reasoning, WITH THINNING!!  Procedures adapted from NJ. **)
   142 
   143 (*Finds f:Prod(A,B) and a:A in the assumptions, concludes there is z:B(a) *)
   144 fun add_mp_tac i = 
   145     rtac subst_prodE i  THEN  assume_tac i  THEN  assume_tac i;
   146 
   147 (*Finds P-->Q and P in the assumptions, replaces implication by Q *)
   148 fun mp_tac i = etac subst_prodE i  THEN  assume_tac i;
   149 
   150 (*"safe" when regarded as predicate calculus rules*)
   151 val safe_brls = sort (make_ord lessb)
   152     [ (true,FE), (true,asm_rl), 
   153       (false,ProdI), (true,SumE), (true,PlusE) ];
   154 
   155 val unsafe_brls =
   156     [ (false,PlusI_inl), (false,PlusI_inr), (false,SumI), 
   157       (true,subst_prodE) ];
   158 
   159 (*0 subgoals vs 1 or more*)
   160 val (safe0_brls, safep_brls) =
   161     partition (apl(0,op=) o subgoals_of_brl) safe_brls;
   162 
   163 fun safestep_tac thms i =
   164     form_tac  ORELSE  
   165     resolve_tac thms i  ORELSE
   166     biresolve_tac safe0_brls i  ORELSE  mp_tac i  ORELSE
   167     DETERM (biresolve_tac safep_brls i);
   168 
   169 fun safe_tac thms i = DEPTH_SOLVE_1 (safestep_tac thms i); 
   170 
   171 fun step_tac thms = safestep_tac thms  ORELSE'  biresolve_tac unsafe_brls;
   172 
   173 (*Fails unless it solves the goal!*)
   174 fun pc_tac thms = DEPTH_SOLVE_1 o (step_tac thms);
   175 
   176 (** The elimination rules for fst/snd **)
   177 
   178 val [major] = goalw CTT.thy basic_defs
   179     "p : Sum(A,B) ==> fst(p) : A";
   180 by (rtac (major RS SumE) 1);
   181 by (assume_tac 1) ;
   182 qed "SumE_fst";
   183 
   184 (*The first premise must be p:Sum(A,B) !!*)
   185 val major::prems= goalw CTT.thy basic_defs
   186     "[| p: Sum(A,B);  A type;  !!x. x:A ==> B(x) type \
   187 \    |] ==> snd(p) : B(fst(p))";
   188 by (rtac (major RS SumE) 1);
   189 by (resolve_tac [SumC RS subst_eqtyparg RS replace_type] 1);
   190 by (typechk_tac prems) ;
   191 qed "SumE_snd";