src/Pure/Isar/net_rules.ML
author wenzelm
Wed Dec 05 03:18:03 2001 +0100 (2001-12-05 ago)
changeset 12385 389d11fb62c8
parent 12290 29b1a4ef4d9f
child 13105 3d1e7a199bdc
permissions -rw-r--r--
removed unused functionality (weight etc.);
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@11776
    13
  val retrieve: 'a T -> term -> 'a list
wenzelm@12385
    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@11776
    18
  val intro: thm T
wenzelm@11776
    19
  val elim: thm T
wenzelm@8298
    20
end;
wenzelm@8298
    21
wenzelm@8298
    22
structure NetRules: NET_RULES =
wenzelm@8298
    23
struct
wenzelm@8298
    24
wenzelm@8298
    25
(* datatype rules *)
wenzelm@8298
    26
wenzelm@8298
    27
datatype 'a T =
wenzelm@8298
    28
  Rules of {
wenzelm@8298
    29
    eq: 'a * 'a -> bool,
wenzelm@8298
    30
    index: 'a -> term,
wenzelm@8298
    31
    rules: 'a list,
wenzelm@8298
    32
    next: int,
wenzelm@12385
    33
    net: (int * 'a) Net.net};
wenzelm@8298
    34
wenzelm@12385
    35
fun mk_rules eq index rules next net =
wenzelm@12385
    36
  Rules {eq = eq, index = index, rules = rules, next = next, net = net};
wenzelm@8298
    37
wenzelm@8298
    38
fun rules (Rules {rules = rs, ...}) = rs;
wenzelm@11776
    39
wenzelm@12385
    40
fun retrieve (Rules {rules, net, ...}) tm =
wenzelm@12385
    41
  Tactic.untaglist ((Library.sort (int_ord o pairself #1) (Net.unify_term net tm)));
wenzelm@8298
    42
wenzelm@8298
    43
wenzelm@8298
    44
(* build rules *)
wenzelm@8298
    45
wenzelm@12385
    46
fun init eq index = mk_rules eq index [] ~1 Net.empty;
wenzelm@8298
    47
wenzelm@12385
    48
fun add rule (Rules {eq, index, rules, next, net}) =
wenzelm@12385
    49
  mk_rules eq index (rule :: rules) (next - 1)
wenzelm@12385
    50
    (Net.insert_term ((index rule, (next, rule)), net, K false));
wenzelm@8298
    51
wenzelm@12385
    52
fun merge (Rules {eq, index, rules = rules1, ...}, Rules {rules = rules2, ...}) =
wenzelm@12385
    53
  let val rules = Library.gen_merge_lists' eq rules1 rules2
wenzelm@12385
    54
  in foldr (uncurry add) (rules, init eq index) end;
wenzelm@8298
    55
wenzelm@12385
    56
fun delete rule (rs as Rules {eq, index, rules, next, net}) =
wenzelm@8298
    57
  if not (Library.gen_mem eq (rule, rules)) then rs
wenzelm@12385
    58
  else mk_rules eq index (Library.gen_rem eq (rules, rule)) next
wenzelm@12385
    59
    (Net.delete_term ((index rule, (0, rule)), net, eq o pairself #2));
wenzelm@8298
    60
wenzelm@12385
    61
fun insert rule rs = add rule (delete rule rs);    (*ensure that new rule gets precedence*)
wenzelm@8298
    62
wenzelm@8298
    63
wenzelm@8298
    64
(* intro/elim rules *)
wenzelm@8298
    65
wenzelm@12385
    66
val intro = init Thm.eq_thm Thm.concl_of;
wenzelm@12385
    67
val elim = init Thm.eq_thm Thm.major_prem_of;
wenzelm@8298
    68
wenzelm@8298
    69
wenzelm@8298
    70
end;