src/Pure/net.ML
author skalberg
Thu Mar 03 12:43:01 2005 +0100 (2005-03-03)
changeset 15570 8d8c70b41bab
parent 12319 cb3ea5750c3b
child 15574 b1d1b5bfc464
permissions -rw-r--r--
Move towards standard functions.
wenzelm@12319
     1
(*  Title:      Pure/net.ML
clasohm@0
     2
    ID:         $Id$
wenzelm@12319
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   1993  University of Cambridge
clasohm@0
     5
clasohm@0
     6
Discrimination nets: a data structure for indexing items
clasohm@0
     7
wenzelm@12319
     8
From the book
wenzelm@12319
     9
    E. Charniak, C. K. Riesbeck, D. V. McDermott.
clasohm@0
    10
    Artificial Intelligence Programming.
clasohm@0
    11
    (Lawrence Erlbaum Associates, 1980).  [Chapter 14]
nipkow@225
    12
wenzelm@12319
    13
match_term no longer treats abstractions as wildcards; instead they match
nipkow@228
    14
only wildcards in patterns.  Requires operands to be beta-eta-normal.
clasohm@0
    15
*)
clasohm@0
    16
wenzelm@12319
    17
signature NET =
clasohm@0
    18
  sig
clasohm@0
    19
  type key
clasohm@0
    20
  type 'a net
clasohm@0
    21
  exception DELETE and INSERT
wenzelm@12319
    22
  val delete: (key list * 'a) * 'b net * ('a * 'b -> bool) -> 'b net
wenzelm@12319
    23
  val delete_term: (term * 'a) * 'b net * ('a * 'b -> bool) -> 'b net
clasohm@0
    24
  val empty: 'a net
clasohm@0
    25
  val insert: (key list * 'a) * 'a net * ('a*'a -> bool) -> 'a net
clasohm@0
    26
  val insert_term:   (term * 'a) * 'a net * ('a*'a -> bool) -> 'a net
clasohm@0
    27
  val lookup: 'a net * key list -> 'a list
clasohm@0
    28
  val match_term: 'a net -> term -> 'a list
clasohm@0
    29
  val key_of_term: term -> key list
clasohm@0
    30
  val unify_term: 'a net -> term -> 'a list
wenzelm@3548
    31
  val dest: 'a net -> (key list * 'a) list
wenzelm@3548
    32
  val merge: 'a net * 'a net * ('a*'a -> bool) -> 'a net
clasohm@0
    33
  end;
clasohm@0
    34
wenzelm@12319
    35
structure Net : NET =
clasohm@0
    36
struct
clasohm@0
    37
clasohm@0
    38
datatype key = CombK | VarK | AtomK of string;
clasohm@0
    39
nipkow@225
    40
(*Bound variables*)
oheimb@7943
    41
fun string_of_bound i = "*B*" ^ chr (i div 256) ^ chr (i mod 256);
clasohm@0
    42
nipkow@228
    43
(*Keys are preorder lists of symbols -- Combinations, Vars, Atoms.
nipkow@225
    44
  Any term whose head is a Var is regarded entirely as a Var.
nipkow@228
    45
  Abstractions are also regarded as Vars;  this covers eta-conversion
nipkow@228
    46
    and "near" eta-conversions such as %x.?P(?f(x)).
clasohm@0
    47
*)
wenzelm@12319
    48
fun add_key_of_terms (t, cs) =
clasohm@0
    49
  let fun rands (f$t, cs) = CombK :: rands (f, add_key_of_terms(t, cs))
wenzelm@12319
    50
        | rands (Const(c,_), cs) = AtomK c :: cs
wenzelm@12319
    51
        | rands (Free(c,_),  cs) = AtomK c :: cs
wenzelm@12319
    52
        | rands (Bound i,  cs)   = AtomK (string_of_bound i) :: cs
clasohm@0
    53
  in case (head_of t) of
nipkow@225
    54
      Var _ => VarK :: cs
nipkow@228
    55
    | Abs _ => VarK :: cs
nipkow@225
    56
    | _     => rands(t,cs)
clasohm@0
    57
  end;
clasohm@0
    58
nipkow@225
    59
(*convert a term to a list of keys*)
clasohm@0
    60
fun key_of_term t = add_key_of_terms (t, []);
clasohm@0
    61
clasohm@0
    62
clasohm@0
    63
(*Trees indexed by key lists: each arc is labelled by a key.
clasohm@0
    64
  Each node contains a list of items, and arcs to children.
clasohm@0
    65
  Keys in the association list (alist) are stored in ascending order.
clasohm@0
    66
  The empty key addresses the entire net.
clasohm@0
    67
  Lookup functions preserve order in items stored at same level.
clasohm@0
    68
*)
clasohm@0
    69
datatype 'a net = Leaf of 'a list
wenzelm@12319
    70
                | Net of {comb: 'a net,
wenzelm@12319
    71
                          var: 'a net,
wenzelm@12319
    72
                          alist: (string * 'a net) list};
clasohm@0
    73
clasohm@0
    74
val empty = Leaf[];
clasohm@0
    75
val emptynet = Net{comb=empty, var=empty, alist=[]};
clasohm@0
    76
clasohm@0
    77
clasohm@0
    78
(*** Insertion into a discrimination net ***)
clasohm@0
    79
wenzelm@12319
    80
exception INSERT;       (*duplicate item in the net*)
clasohm@0
    81
clasohm@0
    82
clasohm@0
    83
(*Adds item x to the list at the node addressed by the keys.
clasohm@0
    84
  Creates node if not already present.
wenzelm@12319
    85
  eq is the equality test for items.
clasohm@0
    86
  The empty list of keys generates a Leaf node, others a Net node.
clasohm@0
    87
*)
clasohm@0
    88
fun insert ((keys,x), net, eq) =
wenzelm@12319
    89
  let fun ins1 ([], Leaf xs) =
clasohm@0
    90
            if gen_mem eq (x,xs) then  raise INSERT  else Leaf(x::xs)
clasohm@0
    91
        | ins1 (keys, Leaf[]) = ins1 (keys, emptynet)   (*expand empty...*)
clasohm@0
    92
        | ins1 (CombK :: keys, Net{comb,var,alist}) =
wenzelm@12319
    93
            Net{comb=ins1(keys,comb), var=var, alist=alist}
wenzelm@12319
    94
        | ins1 (VarK :: keys, Net{comb,var,alist}) =
wenzelm@12319
    95
            Net{comb=comb, var=ins1(keys,var), alist=alist}
wenzelm@12319
    96
        | ins1 (AtomK a :: keys, Net{comb,var,alist}) =
wenzelm@12319
    97
            let fun newpair net = (a, ins1(keys,net))
wenzelm@12319
    98
                fun inslist [] = [newpair empty]
wenzelm@12319
    99
                  | inslist((b: string, netb) :: alist) =
wenzelm@12319
   100
                      if a=b then newpair netb :: alist
wenzelm@12319
   101
                      else if a<b then (*absent, ins1ert in alist*)
wenzelm@12319
   102
                          newpair empty :: (b,netb) :: alist
wenzelm@12319
   103
                      else (*a>b*) (b,netb) :: inslist alist
wenzelm@12319
   104
            in  Net{comb=comb, var=var, alist= inslist alist}  end
clasohm@0
   105
  in  ins1 (keys,net)  end;
clasohm@0
   106
clasohm@0
   107
fun insert_term ((t,x), net, eq) = insert((key_of_term t, x), net, eq);
clasohm@0
   108
clasohm@0
   109
(*** Deletion from a discrimination net ***)
clasohm@0
   110
wenzelm@12319
   111
exception DELETE;       (*missing item in the net*)
clasohm@0
   112
clasohm@0
   113
(*Create a new Net node if it would be nonempty*)
clasohm@0
   114
fun newnet {comb=Leaf[], var=Leaf[], alist=[]} = empty
clasohm@0
   115
  | newnet {comb,var,alist} = Net{comb=comb, var=var, alist=alist};
clasohm@0
   116
clasohm@0
   117
(*add new (b,net) pair to the alist provided net is nonempty*)
clasohm@0
   118
fun conspair((b, Leaf[]), alist) = alist
clasohm@0
   119
  | conspair((b, net), alist)    = (b, net) :: alist;
clasohm@0
   120
clasohm@0
   121
(*Deletes item x from the list at the node addressed by the keys.
clasohm@0
   122
  Raises DELETE if absent.  Collapses the net if possible.
clasohm@0
   123
  eq is the equality test for items. *)
wenzelm@12319
   124
fun delete ((keys, x), net, eq) =
clasohm@0
   125
  let fun del1 ([], Leaf xs) =
wenzelm@12319
   126
            if gen_mem eq (x,xs) then Leaf (gen_rem (eq o swap) (xs,x))
clasohm@0
   127
            else raise DELETE
wenzelm@12319
   128
        | del1 (keys, Leaf[]) = raise DELETE
wenzelm@12319
   129
        | del1 (CombK :: keys, Net{comb,var,alist}) =
wenzelm@12319
   130
            newnet{comb=del1(keys,comb), var=var, alist=alist}
wenzelm@12319
   131
        | del1 (VarK :: keys, Net{comb,var,alist}) =
wenzelm@12319
   132
            newnet{comb=comb, var=del1(keys,var), alist=alist}
wenzelm@12319
   133
        | del1 (AtomK a :: keys, Net{comb,var,alist}) =
wenzelm@12319
   134
            let fun newpair net = (a, del1(keys,net))
wenzelm@12319
   135
                fun dellist [] = raise DELETE
wenzelm@12319
   136
                  | dellist((b: string, netb) :: alist) =
wenzelm@12319
   137
                      if a=b then conspair(newpair netb, alist)
wenzelm@12319
   138
                      else if a<b then (*absent*) raise DELETE
wenzelm@12319
   139
                      else (*a>b*)  (b,netb) :: dellist alist
wenzelm@12319
   140
            in  newnet{comb=comb, var=var, alist= dellist alist}  end
clasohm@0
   141
  in  del1 (keys,net)  end;
clasohm@0
   142
clasohm@0
   143
fun delete_term ((t,x), net, eq) = delete((key_of_term t, x), net, eq);
clasohm@0
   144
clasohm@0
   145
(*** Retrieval functions for discrimination nets ***)
clasohm@0
   146
clasohm@0
   147
exception OASSOC;
clasohm@0
   148
clasohm@0
   149
(*Ordered association list lookup*)
clasohm@0
   150
fun oassoc ([], a: string) = raise OASSOC
clasohm@0
   151
  | oassoc ((b,x)::pairs, a) =
clasohm@0
   152
      if b<a then oassoc(pairs,a)
clasohm@0
   153
      else if a=b then x
clasohm@0
   154
      else raise OASSOC;
clasohm@0
   155
clasohm@0
   156
(*Return the list of items at the given node, [] if no such node*)
clasohm@0
   157
fun lookup (Leaf(xs), []) = xs
wenzelm@12319
   158
  | lookup (Leaf _, _::_) = []  (*non-empty keys and empty net*)
clasohm@0
   159
  | lookup (Net{comb,var,alist}, CombK :: keys) = lookup(comb,keys)
clasohm@0
   160
  | lookup (Net{comb,var,alist}, VarK :: keys) = lookup(var,keys)
wenzelm@12319
   161
  | lookup (Net{comb,var,alist}, AtomK a :: keys) =
clasohm@0
   162
      lookup(oassoc(alist,a),keys)  handle  OASSOC => [];
clasohm@0
   163
clasohm@0
   164
clasohm@0
   165
(*Skipping a term in a net.  Recursively skip 2 levels if a combination*)
clasohm@0
   166
fun net_skip (Leaf _, nets) = nets
wenzelm@12319
   167
  | net_skip (Net{comb,var,alist}, nets) =
skalberg@15570
   168
    Library.foldr net_skip
wenzelm@12319
   169
          (net_skip (comb,[]),
skalberg@15570
   170
           Library.foldr (fn ((_,net), nets) => net::nets) (alist, var::nets));
clasohm@0
   171
clasohm@0
   172
(** Matching and Unification**)
clasohm@0
   173
clasohm@0
   174
(*conses the linked net, if present, to nets*)
clasohm@0
   175
fun look1 (alist, a) nets =
clasohm@0
   176
       oassoc(alist,a) :: nets  handle  OASSOC => nets;
clasohm@0
   177
wenzelm@12319
   178
(*Return the nodes accessible from the term (cons them before nets)
clasohm@0
   179
  "unif" signifies retrieval for unification rather than matching.
clasohm@0
   180
  Var in net matches any term.
wenzelm@12319
   181
  Abs or Var in object: if "unif", regarded as wildcard,
nipkow@225
   182
                                   else matches only a variable in net.
nipkow@225
   183
*)
clasohm@0
   184
fun matching unif t (net,nets) =
clasohm@0
   185
  let fun rands _ (Leaf _, nets) = nets
wenzelm@12319
   186
        | rands t (Net{comb,alist,...}, nets) =
wenzelm@12319
   187
            case t of
skalberg@15570
   188
                f$t => Library.foldr (matching unif t) (rands f (comb,[]), nets)
wenzelm@12319
   189
              | Const(c,_) => look1 (alist, c) nets
wenzelm@12319
   190
              | Free(c,_)  => look1 (alist, c) nets
wenzelm@12319
   191
              | Bound i    => look1 (alist, string_of_bound i) nets
wenzelm@12319
   192
              | _          => nets
wenzelm@12319
   193
  in
clasohm@0
   194
     case net of
wenzelm@12319
   195
         Leaf _ => nets
clasohm@0
   196
       | Net{var,...} =>
wenzelm@12319
   197
             case head_of t of
wenzelm@12319
   198
                 Var _ => if unif then net_skip (net,nets)
wenzelm@12319
   199
                          else var::nets           (*only matches Var in net*)
paulson@2836
   200
  (*If "unif" then a var instantiation in the abstraction could allow
paulson@2836
   201
    an eta-reduction, so regard the abstraction as a wildcard.*)
wenzelm@12319
   202
               | Abs _ => if unif then net_skip (net,nets)
wenzelm@12319
   203
                          else var::nets           (*only a Var can match*)
wenzelm@12319
   204
               | _ => rands t (net, var::nets)  (*var could match also*)
clasohm@0
   205
  end;
clasohm@0
   206
paulson@2672
   207
fun extract_leaves l = List.concat (map (fn Leaf(xs) => xs) l);
clasohm@0
   208
nipkow@225
   209
(*return items whose key could match t, WHICH MUST BE BETA-ETA NORMAL*)
wenzelm@12319
   210
fun match_term net t =
clasohm@0
   211
    extract_leaves (matching false t (net,[]));
clasohm@0
   212
clasohm@0
   213
(*return items whose key could unify with t*)
wenzelm@12319
   214
fun unify_term net t =
clasohm@0
   215
    extract_leaves (matching true t (net,[]));
clasohm@0
   216
wenzelm@3548
   217
wenzelm@3548
   218
(** dest **)
wenzelm@3548
   219
wenzelm@3548
   220
fun cons_fst x (xs, y) = (x :: xs, y);
wenzelm@3548
   221
wenzelm@3548
   222
fun dest (Leaf xs) = map (pair []) xs
wenzelm@3548
   223
  | dest (Net {comb, var, alist}) =
wenzelm@3560
   224
      map (cons_fst CombK) (dest comb) @
wenzelm@3560
   225
      map (cons_fst VarK) (dest var) @
skalberg@15570
   226
      List.concat (map (fn (a, net) => map (cons_fst (AtomK a)) (dest net)) alist);
wenzelm@3548
   227
wenzelm@3548
   228
wenzelm@3548
   229
(** merge **)
wenzelm@3548
   230
wenzelm@3548
   231
fun add eq (net, keys_x) =
wenzelm@3548
   232
  insert (keys_x, net, eq) handle INSERT => net;
wenzelm@3548
   233
wenzelm@3548
   234
fun merge (net1, net2, eq) =
skalberg@15570
   235
  Library.foldl (add eq) (net1, dest net2);
wenzelm@3548
   236
wenzelm@3548
   237
clasohm@0
   238
end;