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