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