src/Pure/net.ML
author wenzelm
Sat, 21 Jan 2006 23:02:14 +0100
changeset 18728 6790126ab5f6
parent 17412 e26cb20ef0cc
child 18939 18e2a2676d80
permissions -rw-r--r--
simplified type attribute;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
     1
(*  Title:      Pure/net.ML
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     2
    ID:         $Id$
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     4
    Copyright   1993  University of Cambridge
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     5
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     6
Discrimination nets: a data structure for indexing items
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     7
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
     8
From the book
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
     9
    E. Charniak, C. K. Riesbeck, D. V. McDermott.
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    10
    Artificial Intelligence Programming.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    11
    (Lawrence Erlbaum Associates, 1980).  [Chapter 14]
225
76f60e6400e8 optimized net for matching of abstractions to speed up simplifier
nipkow
parents: 0
diff changeset
    12
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    13
match_term no longer treats abstractions as wildcards; instead they match
228
4f43430f226e some optimizations of Larry's
nipkow
parents: 225
diff changeset
    14
only wildcards in patterns.  Requires operands to be beta-eta-normal.
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    15
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    16
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    17
signature NET =
16808
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
    18
sig
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    19
  type key
16808
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
    20
  val key_of_term: term -> key list
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    21
  type 'a net
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    22
  val empty: 'a net
16808
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
    23
  exception INSERT
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
    24
  val insert: ('a * 'a -> bool) -> key list * 'a -> 'a net -> 'a net
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
    25
  val insert_term: ('a * 'a -> bool) -> term * 'a -> 'a net -> 'a net
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
    26
  exception DELETE
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
    27
  val delete: ('b * 'a -> bool) -> key list * 'b -> 'a net -> 'a net
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
    28
  val delete_term: ('b * 'a -> bool) -> term * 'b -> 'a net -> 'a net
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
    29
  val lookup: 'a net -> key list -> 'a list
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    30
  val match_term: 'a net -> term -> 'a list
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    31
  val unify_term: 'a net -> term -> 'a list
16808
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
    32
  val entries: 'a net -> 'a list
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
    33
  val subtract: ('b * 'a -> bool) -> 'a net -> 'b net -> 'b list
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
    34
  val merge: ('a * 'a -> bool) -> 'a net * 'a net -> 'a net
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
    35
end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    36
16808
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
    37
structure Net: NET =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    38
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    39
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    40
datatype key = CombK | VarK | AtomK of string;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    41
228
4f43430f226e some optimizations of Larry's
nipkow
parents: 225
diff changeset
    42
(*Keys are preorder lists of symbols -- Combinations, Vars, Atoms.
225
76f60e6400e8 optimized net for matching of abstractions to speed up simplifier
nipkow
parents: 0
diff changeset
    43
  Any term whose head is a Var is regarded entirely as a Var.
228
4f43430f226e some optimizations of Larry's
nipkow
parents: 225
diff changeset
    44
  Abstractions are also regarded as Vars;  this covers eta-conversion
4f43430f226e some optimizations of Larry's
nipkow
parents: 225
diff changeset
    45
    and "near" eta-conversions such as %x.?P(?f(x)).
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    46
*)
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    47
fun add_key_of_terms (t, cs) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    48
  let fun rands (f$t, cs) = CombK :: rands (f, add_key_of_terms(t, cs))
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    49
        | rands (Const(c,_), cs) = AtomK c :: cs
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    50
        | rands (Free(c,_),  cs) = AtomK c :: cs
16986
68bc6dbea7d6 nameless Term.bound;
wenzelm
parents: 16938
diff changeset
    51
        | rands (Bound i,  cs)   = AtomK (Term.bound i) :: cs
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    52
  in case (head_of t) of
225
76f60e6400e8 optimized net for matching of abstractions to speed up simplifier
nipkow
parents: 0
diff changeset
    53
      Var _ => VarK :: cs
228
4f43430f226e some optimizations of Larry's
nipkow
parents: 225
diff changeset
    54
    | Abs _ => VarK :: cs
225
76f60e6400e8 optimized net for matching of abstractions to speed up simplifier
nipkow
parents: 0
diff changeset
    55
    | _     => rands(t,cs)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    56
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    57
225
76f60e6400e8 optimized net for matching of abstractions to speed up simplifier
nipkow
parents: 0
diff changeset
    58
(*convert a term to a list of keys*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    59
fun key_of_term t = add_key_of_terms (t, []);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    60
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    61
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    62
(*Trees indexed by key lists: each arc is labelled by a key.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    63
  Each node contains a list of items, and arcs to children.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    64
  The empty key addresses the entire net.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    65
  Lookup functions preserve order in items stored at same level.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    66
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    67
datatype 'a net = Leaf of 'a list
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    68
                | Net of {comb: 'a net,
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    69
                          var: 'a net,
16708
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
    70
                          atoms: 'a net Symtab.table};
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    71
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    72
val empty = Leaf[];
16708
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
    73
fun is_empty (Leaf []) = true | is_empty _ = false;
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
    74
val emptynet = Net{comb=empty, var=empty, atoms=Symtab.empty};
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    75
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    76
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    77
(*** Insertion into a discrimination net ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    78
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    79
exception INSERT;       (*duplicate item in the net*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    80
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    81
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    82
(*Adds item x to the list at the node addressed by the keys.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    83
  Creates node if not already present.
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    84
  eq is the equality test for items.
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    85
  The empty list of keys generates a Leaf node, others a Net node.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    86
*)
16808
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
    87
fun insert eq (keys,x) net =
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    88
  let fun ins1 ([], Leaf xs) =
16686
wenzelm
parents: 16677
diff changeset
    89
            if member eq xs x then  raise INSERT  else Leaf(x::xs)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    90
        | ins1 (keys, Leaf[]) = ins1 (keys, emptynet)   (*expand empty...*)
16708
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
    91
        | ins1 (CombK :: keys, Net{comb,var,atoms}) =
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
    92
            Net{comb=ins1(keys,comb), var=var, atoms=atoms}
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
    93
        | ins1 (VarK :: keys, Net{comb,var,atoms}) =
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
    94
            Net{comb=comb, var=ins1(keys,var), atoms=atoms}
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
    95
        | ins1 (AtomK a :: keys, Net{comb,var,atoms}) =
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
    96
            let
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
    97
              val net' = if_none (Symtab.lookup atoms a) empty;
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
    98
              val atoms' = Symtab.update (a, ins1 (keys, net')) atoms;
16708
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
    99
            in  Net{comb=comb, var=var, atoms=atoms'}  end
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   100
  in  ins1 (keys,net)  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   101
16808
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   102
fun insert_safe eq entry net = insert eq entry net handle INSERT => net;
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   103
fun insert_term eq (t, x) = insert eq (key_of_term t, x);
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   104
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   105
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   106
(*** Deletion from a discrimination net ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   107
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   108
exception DELETE;       (*missing item in the net*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   109
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   110
(*Create a new Net node if it would be nonempty*)
16708
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
   111
fun newnet (args as {comb,var,atoms}) =
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
   112
  if is_empty comb andalso is_empty var andalso Symtab.is_empty atoms
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
   113
  then empty else Net args;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   114
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   115
(*Deletes item x from the list at the node addressed by the keys.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   116
  Raises DELETE if absent.  Collapses the net if possible.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   117
  eq is the equality test for items. *)
16808
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   118
fun delete eq (keys, x) net =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   119
  let fun del1 ([], Leaf xs) =
16686
wenzelm
parents: 16677
diff changeset
   120
            if member eq xs x then Leaf (remove eq x xs)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   121
            else raise DELETE
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   122
        | del1 (keys, Leaf[]) = raise DELETE
16708
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
   123
        | del1 (CombK :: keys, Net{comb,var,atoms}) =
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
   124
            newnet{comb=del1(keys,comb), var=var, atoms=atoms}
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
   125
        | del1 (VarK :: keys, Net{comb,var,atoms}) =
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
   126
            newnet{comb=comb, var=del1(keys,var), atoms=atoms}
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
   127
        | del1 (AtomK a :: keys, Net{comb,var,atoms}) =
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
   128
            let val atoms' =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
   129
              (case Symtab.lookup atoms a of
16708
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
   130
                NONE => raise DELETE
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
   131
              | SOME net' =>
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
   132
                  (case del1 (keys, net') of
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
   133
                    Leaf [] => Symtab.delete a atoms
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
   134
                  | net'' => Symtab.update (a, net'') atoms))
16708
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
   135
            in  newnet{comb=comb, var=var, atoms=atoms'}  end
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   136
  in  del1 (keys,net)  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   137
16808
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   138
fun delete_term eq (t, x) = delete eq (key_of_term t, x);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   139
16677
6c038c13fd0f use fast_string_ord;
wenzelm
parents: 15574
diff changeset
   140
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   141
(*** Retrieval functions for discrimination nets ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   142
16708
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
   143
exception ABSENT;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   144
16708
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
   145
fun the_atom atoms a =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
   146
  (case Symtab.lookup atoms a of
16708
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
   147
    NONE => raise ABSENT
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
   148
  | SOME net => net);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   149
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   150
(*Return the list of items at the given node, [] if no such node*)
16808
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   151
fun lookup (Leaf xs) [] = xs
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   152
  | lookup (Leaf _) (_ :: _) = []  (*non-empty keys and empty net*)
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   153
  | lookup (Net {comb, var, atoms}) (CombK :: keys) = lookup comb keys
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   154
  | lookup (Net {comb, var, atoms}) (VarK :: keys) = lookup var keys
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   155
  | lookup (Net {comb, var, atoms}) (AtomK a :: keys) =
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   156
      lookup (the_atom atoms a) keys handle ABSENT => [];
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   157
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   158
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   159
(*Skipping a term in a net.  Recursively skip 2 levels if a combination*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   160
fun net_skip (Leaf _, nets) = nets
16708
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
   161
  | net_skip (Net{comb,var,atoms}, nets) =
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
   162
      foldr net_skip (Symtab.fold (cons o #2) atoms (var::nets)) (net_skip (comb,[]));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   163
16808
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   164
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   165
(** Matching and Unification **)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   166
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   167
(*conses the linked net, if present, to nets*)
16708
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
   168
fun look1 (atoms, a) nets =
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
   169
  the_atom atoms a :: nets handle ABSENT => nets;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   170
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   171
(*Return the nodes accessible from the term (cons them before nets)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   172
  "unif" signifies retrieval for unification rather than matching.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   173
  Var in net matches any term.
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   174
  Abs or Var in object: if "unif", regarded as wildcard,
225
76f60e6400e8 optimized net for matching of abstractions to speed up simplifier
nipkow
parents: 0
diff changeset
   175
                                   else matches only a variable in net.
76f60e6400e8 optimized net for matching of abstractions to speed up simplifier
nipkow
parents: 0
diff changeset
   176
*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   177
fun matching unif t (net,nets) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   178
  let fun rands _ (Leaf _, nets) = nets
16708
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
   179
        | rands t (Net{comb,atoms,...}, nets) =
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   180
            case t of
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   181
                f$t => foldr (matching unif t) nets (rands f (comb,[]))
16708
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
   182
              | Const(c,_) => look1 (atoms, c) nets
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
   183
              | Free(c,_)  => look1 (atoms, c) nets
16986
68bc6dbea7d6 nameless Term.bound;
wenzelm
parents: 16938
diff changeset
   184
              | Bound i    => look1 (atoms, Term.bound i) nets
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   185
              | _          => nets
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   186
  in
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   187
     case net of
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   188
         Leaf _ => nets
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   189
       | Net{var,...} =>
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   190
             case head_of t of
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   191
                 Var _ => if unif then net_skip (net,nets)
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   192
                          else var::nets           (*only matches Var in net*)
2836
148829646a51 Toby's better treatment of eta-contraction
paulson
parents: 2792
diff changeset
   193
  (*If "unif" then a var instantiation in the abstraction could allow
148829646a51 Toby's better treatment of eta-contraction
paulson
parents: 2792
diff changeset
   194
    an eta-reduction, so regard the abstraction as a wildcard.*)
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   195
               | Abs _ => if unif then net_skip (net,nets)
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   196
                          else var::nets           (*only a Var can match*)
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   197
               | _ => rands t (net, var::nets)  (*var could match also*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   198
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   199
2672
85d7e800d754 Replaced "flat" by the Basis Library function List.concat
paulson
parents: 2226
diff changeset
   200
fun extract_leaves l = List.concat (map (fn Leaf(xs) => xs) l);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   201
225
76f60e6400e8 optimized net for matching of abstractions to speed up simplifier
nipkow
parents: 0
diff changeset
   202
(*return items whose key could match t, WHICH MUST BE BETA-ETA NORMAL*)
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   203
fun match_term net t =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   204
    extract_leaves (matching false t (net,[]));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   205
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   206
(*return items whose key could unify with t*)
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   207
fun unify_term net t =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   208
    extract_leaves (matching true t (net,[]));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   209
3548
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   210
16808
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   211
(** operations on nets **)
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   212
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   213
(*subtraction: collect entries of second net that are NOT present in first net*)
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   214
fun subtract eq net1 net2 =
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   215
  let
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   216
    fun subtr (Net _) (Leaf ys) = append ys
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   217
      | subtr (Leaf xs) (Leaf ys) =
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   218
          fold_rev (fn y => if member eq xs y then I else cons y) ys
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   219
      | subtr (Leaf _) (net as Net _) = subtr emptynet net
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   220
      | subtr (Net {comb = comb1, var = var1, atoms = atoms1})
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   221
            (Net {comb = comb2, var = var2, atoms = atoms2}) =
16842
wenzelm
parents: 16808
diff changeset
   222
          subtr comb1 comb2
wenzelm
parents: 16808
diff changeset
   223
          #> subtr var1 var2
wenzelm
parents: 16808
diff changeset
   224
          #> Symtab.fold (fn (a, net) =>
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
   225
            subtr (if_none (Symtab.lookup atoms1 a) emptynet) net) atoms2
16808
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   226
  in subtr net1 net2 [] end;
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   227
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   228
fun entries net = subtract (K false) empty net;
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   229
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   230
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   231
(* merge *)
3548
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   232
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   233
fun cons_fst x (xs, y) = (x :: xs, y);
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   234
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   235
fun dest (Leaf xs) = map (pair []) xs
16708
479f7ac538b5 use Symtab.table instead of ordered lists;
wenzelm
parents: 16686
diff changeset
   236
  | dest (Net {comb, var, atoms}) =
3560
7db9a44dfa06 fixed polymorphic val;
wenzelm
parents: 3549
diff changeset
   237
      map (cons_fst CombK) (dest comb) @
7db9a44dfa06 fixed polymorphic val;
wenzelm
parents: 3549
diff changeset
   238
      map (cons_fst VarK) (dest var) @
16808
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   239
      List.concat (map (fn (a, net) => map (cons_fst (AtomK a)) (dest net)) (Symtab.dest atoms));
3548
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   240
16808
644fc45c7292 added subtract;
wenzelm
parents: 16730
diff changeset
   241
fun merge eq (net1, net2) = fold (insert_safe eq) (dest net2) net1;
3548
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   242
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   243
end;