src/Pure/Isar/net_rules.ML
author kleing
Mon Jun 21 10:25:57 2004 +0200 (2004-06-21)
changeset 14981 e73f8140af78
parent 14472 cba7c0a3ffb3
child 15570 8d8c70b41bab
permissions -rw-r--r--
Merged in license change from Isabelle2004
wenzelm@8298
     1
(*  Title:      Pure/Isar/net_rules.ML
wenzelm@8298
     2
    ID:         $Id$
wenzelm@8298
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@8298
     4
wenzelm@8298
     5
Efficient storage of rules: preserves order, prefers later entries.
wenzelm@8298
     6
*)
wenzelm@8298
     7
wenzelm@8298
     8
signature NET_RULES =
wenzelm@8298
     9
sig
wenzelm@8298
    10
  type 'a T
wenzelm@8298
    11
  val rules: 'a T -> 'a list
wenzelm@11776
    12
  val retrieve: 'a T -> term -> 'a list
wenzelm@12385
    13
  val init: ('a * 'a -> bool) -> ('a -> term) -> 'a T
wenzelm@8298
    14
  val merge: 'a T * 'a T -> 'a T
wenzelm@8298
    15
  val delete: 'a -> 'a T -> 'a T
wenzelm@8298
    16
  val insert: 'a -> 'a T -> 'a T
wenzelm@11776
    17
  val intro: thm T
wenzelm@11776
    18
  val elim: thm T
wenzelm@8298
    19
end;
wenzelm@8298
    20
wenzelm@8298
    21
structure NetRules: NET_RULES =
wenzelm@8298
    22
struct
wenzelm@8298
    23
wenzelm@8298
    24
(* datatype rules *)
wenzelm@8298
    25
wenzelm@8298
    26
datatype 'a T =
wenzelm@8298
    27
  Rules of {
wenzelm@8298
    28
    eq: 'a * 'a -> bool,
wenzelm@8298
    29
    index: 'a -> term,
wenzelm@8298
    30
    rules: 'a list,
wenzelm@8298
    31
    next: int,
wenzelm@12385
    32
    net: (int * 'a) Net.net};
wenzelm@8298
    33
wenzelm@12385
    34
fun mk_rules eq index rules next net =
wenzelm@12385
    35
  Rules {eq = eq, index = index, rules = rules, next = next, net = net};
wenzelm@8298
    36
wenzelm@8298
    37
fun rules (Rules {rules = rs, ...}) = rs;
wenzelm@11776
    38
wenzelm@12385
    39
fun retrieve (Rules {rules, net, ...}) tm =
paulson@14472
    40
  Tactic.untaglist 
paulson@14472
    41
     ((Library.sort (Int.compare 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@13105
    66
val intro = init Drule.eq_thm_prop Thm.concl_of;
wenzelm@13105
    67
val elim = init Drule.eq_thm_prop Thm.major_prem_of;
wenzelm@8298
    68
wenzelm@8298
    69
wenzelm@8298
    70
end;