src/CCL/typecheck.ML
author berghofe
Fri, 07 Apr 2006 11:17:44 +0200
changeset 19357 dade85a75c9f
parent 17456 bcf7544875b2
permissions -rw-r--r--
Added alternative version of thms_of_proof that does not recursively descend into proofs of (named) theorems.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 15570
diff changeset
     1
(*  Title:      CCL/typecheck.ML
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     2
    ID:         $Id$
1459
d12da312eff4 expanded tabs
clasohm
parents: 757
diff changeset
     3
    Author:     Martin Coen, Cambridge University Computer Laboratory
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     4
    Copyright   1993  University of Cambridge
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     5
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     6
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     7
(*** Lemmas for constructors and subtypes ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     8
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     9
(* 0-ary constructors do not need additional rules as they are handled *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    10
(*                                      correctly by applying SubtypeI *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    11
(*
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 15570
diff changeset
    12
val Subtype_canTs =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    13
       let fun tac prems = cut_facts_tac prems 1 THEN
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    14
                REPEAT (ares_tac (SubtypeI::canTs@icanTs) 1 ORELSE
642
0db578095e6a CCL/Gfp/coinduct2, coinduct3: modified proofs to suppress deep unification
lcp
parents: 289
diff changeset
    15
                        etac SubtypeE 1)
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 15570
diff changeset
    16
           fun solve s = prove_goal (the_context ()) s (fn prems => [tac prems])
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    17
       in map solve
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    18
           ["P(one) ==> one : {x:Unit.P(x)}",
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    19
            "P(true) ==> true : {x:Bool.P(x)}",
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    20
            "P(false) ==> false : {x:Bool.P(x)}",
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    21
            "a : {x:A. b:{y:B(a).P(<x,y>)}} ==> <a,b> : {x:Sigma(A,B).P(x)}",
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    22
            "a : {x:A.P(inl(x))} ==> inl(a) : {x:A+B.P(x)}",
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    23
            "b : {x:B.P(inr(x))} ==> inr(b) : {x:A+B.P(x)}",
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    24
            "P(zero) ==> zero : {x:Nat.P(x)}",
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    25
            "a : {x:Nat.P(succ(x))} ==> succ(a) : {x:Nat.P(x)}",
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    26
            "P([]) ==> [] : {x:List(A).P(x)}",
289
78541329ff35 changed "." to "$" to eliminate ambiguity
clasohm
parents: 0
diff changeset
    27
            "h : {x:A. t : {y:List(A).P(x$y)}} ==> h$t : {x:List(A).P(x)}"
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    28
        ] end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    29
*)
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 15570
diff changeset
    30
val Subtype_canTs =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    31
       let fun tac prems = cut_facts_tac prems 1 THEN
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    32
                REPEAT (ares_tac (SubtypeI::canTs@icanTs) 1 ORELSE
642
0db578095e6a CCL/Gfp/coinduct2, coinduct3: modified proofs to suppress deep unification
lcp
parents: 289
diff changeset
    33
                        etac SubtypeE 1)
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 15570
diff changeset
    34
           fun solve s = prove_goal (the_context ()) s (fn prems => [tac prems])
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    35
       in map solve
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    36
           ["a : {x:A. b:{y:B(a).P(<x,y>)}} ==> <a,b> : {x:Sigma(A,B).P(x)}",
3837
d7f033c74b38 fixed dots;
wenzelm
parents: 3537
diff changeset
    37
            "a : {x:A. P(inl(x))} ==> inl(a) : {x:A+B. P(x)}",
d7f033c74b38 fixed dots;
wenzelm
parents: 3537
diff changeset
    38
            "b : {x:B. P(inr(x))} ==> inr(b) : {x:A+B. P(x)}",
d7f033c74b38 fixed dots;
wenzelm
parents: 3537
diff changeset
    39
            "a : {x:Nat. P(succ(x))} ==> succ(a) : {x:Nat. P(x)}",
289
78541329ff35 changed "." to "$" to eliminate ambiguity
clasohm
parents: 0
diff changeset
    40
            "h : {x:A. t : {y:List(A).P(x$y)}} ==> h$t : {x:List(A).P(x)}"
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    41
        ] end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    42
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 15570
diff changeset
    43
val prems = goal (the_context ())
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    44
     "[| f(t):B;  ~t=bot  |] ==> let x be t in f(x) : B";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    45
by (cut_facts_tac prems 1);
642
0db578095e6a CCL/Gfp/coinduct2, coinduct3: modified proofs to suppress deep unification
lcp
parents: 289
diff changeset
    46
by (etac (letB RS ssubst) 1);
0db578095e6a CCL/Gfp/coinduct2, coinduct3: modified proofs to suppress deep unification
lcp
parents: 289
diff changeset
    47
by (assume_tac 1);
757
2ca12511676d added qed and qed_goal[w]
clasohm
parents: 642
diff changeset
    48
qed "letT";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    49
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 15570
diff changeset
    50
val prems = goal (the_context ())
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    51
     "[| a:A;  f : Pi(A,B)  |] ==> f ` a  : B(a)";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    52
by (REPEAT (ares_tac (applyT::prems) 1));
757
2ca12511676d added qed and qed_goal[w]
clasohm
parents: 642
diff changeset
    53
qed "applyT2";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    54
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 15570
diff changeset
    55
val prems = goal (the_context ())
3837
d7f033c74b38 fixed dots;
wenzelm
parents: 3537
diff changeset
    56
    "[| a:A;  a:A ==> P(a) |] ==> a : {x:A. P(x)}";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    57
by (fast_tac (type_cs addSIs prems) 1);
757
2ca12511676d added qed and qed_goal[w]
clasohm
parents: 642
diff changeset
    58
qed "rcall_lemma1";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    59
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 15570
diff changeset
    60
val prems = goal (the_context ())
3837
d7f033c74b38 fixed dots;
wenzelm
parents: 3537
diff changeset
    61
    "[| a:{x:A. Q(x)};  [| a:A; Q(a) |] ==> P(a) |] ==> a : {x:A. P(x)}";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    62
by (cut_facts_tac prems 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    63
by (fast_tac (type_cs addSIs prems) 1);
757
2ca12511676d added qed and qed_goal[w]
clasohm
parents: 642
diff changeset
    64
qed "rcall_lemma2";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    65
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    66
val rcall_lemmas = [asm_rl,rcall_lemma1,SubtypeD1,rcall_lemma2];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    67
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    68
(***********************************TYPECHECKING*************************************)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    69
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    70
fun bvars (Const("all",_) $ Abs(s,_,t)) l = bvars t (s::l)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    71
  | bvars _ l = l;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    72
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 15570
diff changeset
    73
fun get_bno l n (Const("all",_) $ Abs(s,_,t)) = get_bno (s::l) n t
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    74
  | get_bno l n (Const("Trueprop",_) $ t) = get_bno l n t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    75
  | get_bno l n (Const("Ball",_) $ _ $ Abs(s,_,t)) = get_bno (s::l) (n+1) t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    76
  | get_bno l n (Const("op :",_) $ t $ _) = get_bno l n t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    77
  | get_bno l n (t $ s) = get_bno l n t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    78
  | get_bno l n (Bound m) = (m-length(l),n);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    79
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    80
(* Not a great way of identifying induction hypothesis! *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    81
fun could_IH x = could_unify(x,hd (prems_of rcallT)) orelse
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    82
                 could_unify(x,hd (prems_of rcall2T)) orelse
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    83
                 could_unify(x,hd (prems_of rcall3T));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    84
3537
79ac9b475621 Removal of the tactical STATE
paulson
parents: 1459
diff changeset
    85
fun IHinst tac rls = SUBGOAL (fn (Bi,i) =>
79ac9b475621 Removal of the tactical STATE
paulson
parents: 1459
diff changeset
    86
  let val bvs = bvars Bi [];
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 3837
diff changeset
    87
      val ihs = List.filter could_IH (Logic.strip_assums_hyp Bi);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    88
      val rnames = map (fn x=>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    89
                    let val (a,b) = get_bno [] 0 x
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 3837
diff changeset
    90
                    in (List.nth(bvs,a),b) end) ihs;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    91
      fun try_IHs [] = no_tac
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 3837
diff changeset
    92
        | try_IHs ((x,y)::xs) = tac [("g",x)] (List.nth(rls,y-1)) i ORELSE (try_IHs xs);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    93
  in try_IHs rnames end);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    94
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    95
(*****)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    96
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    97
val type_rls = canTs@icanTs@(applyT2::ncanTs)@incanTs@
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    98
               precTs@letrecTs@[letT]@Subtype_canTs;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    99
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 15570
diff changeset
   100
fun is_rigid_prog t =
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 15570
diff changeset
   101
     case (Logic.strip_assums_concl t) of
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   102
        (Const("Trueprop",_) $ (Const("op :",_) $ a $ _)) => (term_vars a = [])
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   103
       | _ => false;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   104
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   105
fun rcall_tac i = let fun tac ps rl i = res_inst_tac ps rl i THEN atac i;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   106
                       in IHinst tac rcallTs i end THEN
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   107
                  eresolve_tac rcall_lemmas i;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   108
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   109
fun raw_step_tac prems i = ares_tac (prems@type_rls) i ORELSE
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   110
                           rcall_tac i ORELSE
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   111
                           ematch_tac [SubtypeE] i ORELSE
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   112
                           match_tac [SubtypeI] i;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   113
3537
79ac9b475621 Removal of the tactical STATE
paulson
parents: 1459
diff changeset
   114
fun tc_step_tac prems = SUBGOAL (fn (Bi,i) =>
79ac9b475621 Removal of the tactical STATE
paulson
parents: 1459
diff changeset
   115
          if is_rigid_prog Bi then raw_step_tac prems i else no_tac);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   116
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   117
fun typechk_tac rls i = SELECT_GOAL (REPEAT_FIRST (tc_step_tac rls)) i;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   118
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   119
val tac = typechk_tac [] 1;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   120
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   121
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   122
(*** Clean up Correctness Condictions ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   123
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   124
val clean_ccs_tac = REPEAT_FIRST (eresolve_tac ([SubtypeE]@rmIHs) ORELSE'
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   125
                                 hyp_subst_tac);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   126
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 15570
diff changeset
   127
val clean_ccs_tac =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   128
       let fun tac ps rl i = eres_inst_tac ps rl i THEN atac i;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   129
       in TRY (REPEAT_FIRST (IHinst tac hyprcallTs ORELSE'
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   130
                       eresolve_tac ([asm_rl,SubtypeE]@rmIHs) ORELSE'
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   131
                       hyp_subst_tac)) end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   132
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   133
fun gen_ccs_tac rls i = SELECT_GOAL (REPEAT_FIRST (tc_step_tac rls) THEN
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   134
                                     clean_ccs_tac) i;