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