src/ZF/Tools/typechk.ML
author wenzelm
Sun Mar 15 15:59:44 2009 +0100 (2009-03-15)
changeset 30528 7173bf123335
parent 30510 4120fc59dd85
child 30541 9f168bdc468a
permissions -rw-r--r--
simplified attribute setup;
wenzelm@12189
     1
(*  Title:      ZF/Tools/typechk.ML
paulson@6049
     2
    ID:         $Id$
paulson@6049
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@6153
     4
    Copyright   1999  University of Cambridge
paulson@6049
     5
wenzelm@18736
     6
Automated type checking (cf. CTT).
paulson@6049
     7
*)
paulson@6049
     8
wenzelm@12189
     9
signature TYPE_CHECK =
wenzelm@12189
    10
sig
wenzelm@21506
    11
  val print_tcset: Proof.context -> unit
wenzelm@18736
    12
  val TC_add: attribute
wenzelm@18736
    13
  val TC_del: attribute
wenzelm@18736
    14
  val typecheck_tac: Proof.context -> tactic
wenzelm@18736
    15
  val type_solver_tac: Proof.context -> thm list -> int -> tactic
wenzelm@18736
    16
  val type_solver: solver
wenzelm@18708
    17
  val setup: theory -> theory
wenzelm@12189
    18
end;
wenzelm@12189
    19
wenzelm@12189
    20
structure TypeCheck: TYPE_CHECK =
paulson@6153
    21
struct
wenzelm@12189
    22
wenzelm@18736
    23
(* datatype tcset *)
wenzelm@18736
    24
wenzelm@18736
    25
datatype tcset = TC of
wenzelm@18736
    26
 {rules: thm list,     (*the type-checking rules*)
wenzelm@18736
    27
  net: thm Net.net};   (*discrimination net of the same rules*)
wenzelm@18736
    28
wenzelm@18736
    29
fun add_rule th (tcs as TC {rules, net}) =
wenzelm@22360
    30
  if member Thm.eq_thm_prop rules th then
wenzelm@18736
    31
    (warning ("Ignoring duplicate type-checking rule\n" ^ Display.string_of_thm th); tcs)
wenzelm@18736
    32
  else
wenzelm@18736
    33
    TC {rules = th :: rules,
wenzelm@18736
    34
        net = Net.insert_term (K false) (Thm.concl_of th, th) net};
wenzelm@18736
    35
wenzelm@18736
    36
fun del_rule th (tcs as TC {rules, net}) =
wenzelm@22360
    37
  if member Thm.eq_thm_prop rules th then
wenzelm@22360
    38
    TC {net = Net.delete_term Thm.eq_thm_prop (Thm.concl_of th, th) net,
wenzelm@22360
    39
        rules = remove Thm.eq_thm_prop th rules}
wenzelm@18736
    40
  else (warning ("No such type-checking rule\n" ^ Display.string_of_thm th); tcs);
paulson@6153
    41
paulson@6153
    42
wenzelm@18736
    43
(* generic data *)
wenzelm@18736
    44
wenzelm@18736
    45
structure Data = GenericDataFun
wenzelm@18736
    46
(
wenzelm@18736
    47
  type T = tcset
wenzelm@18736
    48
  val empty = TC {rules = [], net = Net.empty};
wenzelm@18736
    49
  val extend = I;
paulson@6153
    50
wenzelm@18736
    51
  fun merge _ (TC {rules, net}, TC {rules = rules', net = net'}) =
wenzelm@24039
    52
    TC {rules = Thm.merge_thms (rules, rules'),
wenzelm@22360
    53
        net = Net.merge Thm.eq_thm_prop (net, net')};
wenzelm@18736
    54
);
wenzelm@18736
    55
wenzelm@18736
    56
val TC_add = Thm.declaration_attribute (Data.map o add_rule);
wenzelm@18736
    57
val TC_del = Thm.declaration_attribute (Data.map o del_rule);
wenzelm@18736
    58
wenzelm@18736
    59
val tcset_of = Data.get o Context.Proof;
paulson@6153
    60
wenzelm@22846
    61
fun print_tcset ctxt =
wenzelm@22846
    62
  let val TC {rules, ...} = tcset_of ctxt in
wenzelm@22846
    63
    Pretty.writeln (Pretty.big_list "type-checking rules:"
wenzelm@22846
    64
      (map (ProofContext.pretty_thm ctxt) rules))
wenzelm@22846
    65
  end;
wenzelm@22846
    66
paulson@6153
    67
wenzelm@18736
    68
(* tactics *)
paulson@6153
    69
paulson@6153
    70
(*resolution using a net rather than rules*)
paulson@6153
    71
fun net_res_tac maxr net =
paulson@6153
    72
  SUBGOAL
paulson@6153
    73
    (fn (prem,i) =>
paulson@6153
    74
      let val rls = Net.unify_term net (Logic.strip_assums_concl prem)
wenzelm@12189
    75
      in
wenzelm@12189
    76
         if length rls <= maxr then resolve_tac rls i else no_tac
paulson@6153
    77
      end);
paulson@6153
    78
wenzelm@24826
    79
fun is_rigid_elem (Const("Trueprop",_) $ (Const(@{const_name mem},_) $ a $ _)) =
paulson@6049
    80
      not (is_Var (head_of a))
paulson@6049
    81
  | is_rigid_elem _ = false;
paulson@6049
    82
wenzelm@12189
    83
(*Try solving a:A by assumption provided a is rigid!*)
paulson@6049
    84
val test_assume_tac = SUBGOAL(fn (prem,i) =>
paulson@6049
    85
    if is_rigid_elem (Logic.strip_assums_concl prem)
paulson@6049
    86
    then  assume_tac i  else  eq_assume_tac i);
paulson@6049
    87
wenzelm@12189
    88
(*Type checking solves a:?A (a rigid, ?A maybe flexible).
paulson@6049
    89
  match_tac is too strict; would refuse to instantiate ?A*)
paulson@6153
    90
fun typecheck_step_tac (TC{net,...}) =
paulson@6153
    91
    FIRSTGOAL (test_assume_tac ORELSE' net_res_tac 3 net);
paulson@6049
    92
wenzelm@18736
    93
fun typecheck_tac ctxt = REPEAT (typecheck_step_tac (tcset_of ctxt));
paulson@6049
    94
paulson@6153
    95
(*Compiles a term-net for speed*)
wenzelm@26287
    96
val basic_res_tac = net_resolve_tac [@{thm TrueI}, @{thm refl}, reflexive_thm, @{thm iff_refl},
wenzelm@26287
    97
                                     @{thm ballI}, @{thm allI}, @{thm conjI}, @{thm impI}];
paulson@6049
    98
paulson@6049
    99
(*Instantiates variables in typing conditions.
paulson@6049
   100
  drawback: does not simplify conjunctions*)
wenzelm@18736
   101
fun type_solver_tac ctxt hyps = SELECT_GOAL
paulson@6153
   102
    (DEPTH_SOLVE (etac FalseE 1
wenzelm@12189
   103
                  ORELSE basic_res_tac 1
wenzelm@12189
   104
                  ORELSE (ares_tac hyps 1
wenzelm@18736
   105
                          APPEND typecheck_step_tac (tcset_of ctxt))));
wenzelm@12189
   106
wenzelm@18736
   107
val type_solver = Simplifier.mk_solver' "ZF typecheck" (fn ss =>
wenzelm@18736
   108
  type_solver_tac (Simplifier.the_context ss) (Simplifier.prems_of_ss ss));
paulson@6153
   109
paulson@6153
   110
wenzelm@18736
   111
(* concrete syntax *)
wenzelm@12189
   112
wenzelm@18736
   113
val typecheck_meth =
wenzelm@18736
   114
  Method.only_sectioned_args
wenzelm@18736
   115
    [Args.add -- Args.colon >> K (I, TC_add),
wenzelm@18736
   116
     Args.del -- Args.colon >> K (I, TC_del)]
wenzelm@30510
   117
  (fn ctxt => SIMPLE_METHOD (CHANGED (typecheck_tac ctxt)));
wenzelm@12189
   118
wenzelm@24867
   119
val _ =
wenzelm@24867
   120
  OuterSyntax.improper_command "print_tcset" "print context of ZF typecheck" OuterKeyword.diag
wenzelm@18736
   121
    (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_context o
wenzelm@24867
   122
      Toplevel.keep (print_tcset o Toplevel.context_of)));
wenzelm@12189
   123
wenzelm@12189
   124
wenzelm@18736
   125
(* theory setup *)
wenzelm@12189
   126
wenzelm@12189
   127
val setup =
wenzelm@30528
   128
  Attrib.setup @{binding TC} (Attrib.add_del TC_add TC_del) "declaration of type-checking rule" #>
wenzelm@18736
   129
  Method.add_methods [("typecheck", typecheck_meth, "ZF type-checking")] #>
wenzelm@26496
   130
  Simplifier.map_simpset (fn ss => ss setSolver type_solver);
wenzelm@12189
   131
wenzelm@12189
   132
end;