src/Pure/Isar/net_rules.ML
author wenzelm
Fri May 05 22:09:41 2000 +0200 (2000-05-05 ago)
changeset 8807 0046be1769f9
parent 8635 2f699cd7b8d7
child 11693 63b0b2ec5830
permissions -rw-r--r--
GPLed;
wenzelm@8298
     1
(*  Title:      Pure/Isar/net_rules.ML
wenzelm@8298
     2
    ID:         $Id$
wenzelm@8298
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@8807
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@8298
     5
wenzelm@8298
     6
Efficient storage of rules: preserves order, prefers later entries.
wenzelm@8298
     7
*)
wenzelm@8298
     8
wenzelm@8298
     9
signature NET_RULES =
wenzelm@8298
    10
sig
wenzelm@8298
    11
  type 'a T
wenzelm@8298
    12
  val rules: 'a T -> 'a list
wenzelm@8298
    13
  val may_unify: 'a T -> term -> 'a list
wenzelm@8298
    14
  val init: ('a * 'a -> bool) -> ('a -> term) -> 'a T
wenzelm@8298
    15
  val merge: 'a T * 'a T -> 'a T
wenzelm@8298
    16
  val delete: 'a -> 'a T -> 'a T
wenzelm@8298
    17
  val insert: 'a -> 'a T -> 'a T
wenzelm@8298
    18
  val deletes: 'a list -> 'a T -> 'a T
wenzelm@8298
    19
  val inserts: 'a list -> 'a T -> 'a T
wenzelm@8298
    20
  val init_intro: thm T
wenzelm@8298
    21
  val init_elim: thm T
wenzelm@8298
    22
end;
wenzelm@8298
    23
wenzelm@8298
    24
structure NetRules: NET_RULES =
wenzelm@8298
    25
struct
wenzelm@8298
    26
wenzelm@8298
    27
(* datatype rules *)
wenzelm@8298
    28
wenzelm@8298
    29
datatype 'a T =
wenzelm@8298
    30
  Rules of {
wenzelm@8298
    31
    eq: 'a * 'a -> bool,
wenzelm@8298
    32
    index: 'a -> term,
wenzelm@8298
    33
    rules: 'a list,
wenzelm@8298
    34
    next: int,
wenzelm@8298
    35
    net: (int * 'a) Net.net};
wenzelm@8298
    36
wenzelm@8298
    37
fun mk_rules eq index rules next net =
wenzelm@8298
    38
  Rules {eq = eq, index = index, rules = rules, next = next, net = net};
wenzelm@8298
    39
wenzelm@8298
    40
fun rules (Rules {rules = rs, ...}) = rs;
wenzelm@8635
    41
fun may_unify (Rules {rules, net, ...}) tm = Tactic.orderlist (Net.unify_term net tm);
wenzelm@8298
    42
wenzelm@8298
    43
wenzelm@8298
    44
(* build rules *)
wenzelm@8298
    45
wenzelm@8298
    46
fun init eq index = mk_rules eq index [] ~1 Net.empty;
wenzelm@8298
    47
wenzelm@8298
    48
fun add rule (Rules {eq, index, rules, next, net}) =
wenzelm@8298
    49
  mk_rules eq index (rule :: rules) (next - 1)
wenzelm@8298
    50
    (Net.insert_term ((index rule, (next, rule)), net, K false));
wenzelm@8298
    51
wenzelm@8298
    52
fun make eq index rules = foldr (uncurry add) (rules, init eq index);
wenzelm@8298
    53
wenzelm@8298
    54
wenzelm@8298
    55
fun merge (Rules {eq, index, rules = rules1, ...}, Rules {rules = rules2, ...}) =
wenzelm@8298
    56
  make eq index (Library.generic_merge eq I I rules1 rules2);
wenzelm@8298
    57
wenzelm@8298
    58
fun delete rule (rs as Rules {eq, index, rules, next, net}) =
wenzelm@8298
    59
  if not (Library.gen_mem eq (rule, rules)) then rs
wenzelm@8298
    60
  else mk_rules eq index (Library.gen_rem eq (rules, rule)) next
wenzelm@8298
    61
    (Net.delete_term ((index rule, (0, rule)), net, eq o pairself #2));
wenzelm@8298
    62
wenzelm@8298
    63
fun insert rule rs = add rule (delete rule rs);    (*ensure new rule gets precedence*)
wenzelm@8298
    64
wenzelm@8298
    65
fun deletes rules rs = foldr (uncurry delete) (rules, rs);
wenzelm@8298
    66
fun inserts rules rs = foldr (uncurry insert) (rules, rs);
wenzelm@8298
    67
wenzelm@8298
    68
wenzelm@8298
    69
(* intro/elim rules *)
wenzelm@8298
    70
wenzelm@8298
    71
val init_intro = init Thm.eq_thm Thm.concl_of;
wenzelm@8635
    72
val init_elim = init Thm.eq_thm (Logic.strip_assums_concl o Thm.major_prem_of);
wenzelm@8298
    73
wenzelm@8298
    74
wenzelm@8298
    75
end;