src/Pure/net.ML
author wenzelm
Mon, 04 Jul 2005 20:13:39 +0200
changeset 16684 7b58002668c0
parent 16677 6c038c13fd0f
child 16686 cc735c10b44d
permissions -rw-r--r--
methods: added simp_flags argument, added "depth_limit" flag;
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 =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    18
  sig
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    19
  type key
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    20
  type 'a net
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    21
  exception DELETE and INSERT
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    22
  val delete: (key list * 'a) * 'b net * ('a * 'b -> bool) -> 'b net
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    23
  val delete_term: (term * 'a) * 'b net * ('a * 'b -> bool) -> 'b net
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    24
  val empty: 'a net
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    25
  val insert: (key list * 'a) * 'a net * ('a*'a -> bool) -> 'a net
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    26
  val insert_term:   (term * 'a) * 'a net * ('a*'a -> bool) -> 'a net
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    27
  val lookup: 'a net * key list -> 'a list
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    28
  val match_term: 'a net -> term -> 'a list
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    29
  val key_of_term: term -> key list
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    30
  val unify_term: 'a net -> term -> 'a list
3548
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
    31
  val dest: 'a net -> (key list * 'a) list
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
    32
  val merge: 'a net * 'a net * ('a*'a -> bool) -> 'a net
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    33
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    34
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    35
structure Net : NET =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    36
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    37
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    38
datatype key = CombK | VarK | AtomK of string;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    39
225
76f60e6400e8 optimized net for matching of abstractions to speed up simplifier
nipkow
parents: 0
diff changeset
    40
(*Bound variables*)
7943
e31a3c0c2c1e now more than 256 generated bound variables possible
oheimb
parents: 6539
diff changeset
    41
fun string_of_bound i = "*B*" ^ chr (i div 256) ^ chr (i mod 256);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    42
228
4f43430f226e some optimizations of Larry's
nipkow
parents: 225
diff changeset
    43
(*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
    44
  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
    45
  Abstractions are also regarded as Vars;  this covers eta-conversion
4f43430f226e some optimizations of Larry's
nipkow
parents: 225
diff changeset
    46
    and "near" eta-conversions such as %x.?P(?f(x)).
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    47
*)
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    48
fun add_key_of_terms (t, cs) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    49
  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
    50
        | rands (Const(c,_), cs) = AtomK c :: cs
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    51
        | rands (Free(c,_),  cs) = AtomK c :: cs
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    52
        | rands (Bound i,  cs)   = AtomK (string_of_bound i) :: cs
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    53
  in case (head_of t) of
225
76f60e6400e8 optimized net for matching of abstractions to speed up simplifier
nipkow
parents: 0
diff changeset
    54
      Var _ => VarK :: cs
228
4f43430f226e some optimizations of Larry's
nipkow
parents: 225
diff changeset
    55
    | Abs _ => VarK :: cs
225
76f60e6400e8 optimized net for matching of abstractions to speed up simplifier
nipkow
parents: 0
diff changeset
    56
    | _     => rands(t,cs)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    57
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    58
225
76f60e6400e8 optimized net for matching of abstractions to speed up simplifier
nipkow
parents: 0
diff changeset
    59
(*convert a term to a list of keys*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    60
fun key_of_term t = add_key_of_terms (t, []);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    61
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    62
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    63
(*Trees indexed by key lists: each arc is labelled by a key.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    64
  Each node contains a list of items, and arcs to children.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    65
  Keys in the association list (alist) are stored in ascending order.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    66
  The empty key addresses the entire net.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    67
  Lookup functions preserve order in items stored at same level.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    68
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    69
datatype 'a net = Leaf of 'a list
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    70
                | Net of {comb: 'a net,
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    71
                          var: 'a net,
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    72
                          alist: (string * 'a net) list};
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    73
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    74
val empty = Leaf[];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    75
val emptynet = Net{comb=empty, var=empty, alist=[]};
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    76
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    77
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    78
(*** Insertion into a discrimination net ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    79
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    80
exception INSERT;       (*duplicate item in the net*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    81
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    82
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    83
(*Adds item x to the list at the node addressed by the keys.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    84
  Creates node if not already present.
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    85
  eq is the equality test for items.
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    86
  The empty list of keys generates a Leaf node, others a Net node.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    87
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    88
fun insert ((keys,x), net, eq) =
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    89
  let fun ins1 ([], Leaf xs) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    90
            if gen_mem eq (x,xs) then  raise INSERT  else Leaf(x::xs)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    91
        | ins1 (keys, Leaf[]) = ins1 (keys, emptynet)   (*expand empty...*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    92
        | ins1 (CombK :: keys, Net{comb,var,alist}) =
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    93
            Net{comb=ins1(keys,comb), var=var, alist=alist}
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    94
        | ins1 (VarK :: keys, Net{comb,var,alist}) =
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    95
            Net{comb=comb, var=ins1(keys,var), alist=alist}
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    96
        | ins1 (AtomK a :: keys, Net{comb,var,alist}) =
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    97
            let fun newpair net = (a, ins1(keys,net))
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
    98
                fun inslist [] = [newpair empty]
16677
6c038c13fd0f use fast_string_ord;
wenzelm
parents: 15574
diff changeset
    99
                  | inslist ((b, netb) :: alist) =
6c038c13fd0f use fast_string_ord;
wenzelm
parents: 15574
diff changeset
   100
                      (case fast_string_ord (a, b) of
6c038c13fd0f use fast_string_ord;
wenzelm
parents: 15574
diff changeset
   101
                        EQUAL => newpair netb :: alist
6c038c13fd0f use fast_string_ord;
wenzelm
parents: 15574
diff changeset
   102
                      | LESS => (*absent, insert in alist*)
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   103
                          newpair empty :: (b,netb) :: alist
16677
6c038c13fd0f use fast_string_ord;
wenzelm
parents: 15574
diff changeset
   104
                      | GREATER => (b,netb) :: inslist alist)
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   105
            in  Net{comb=comb, var=var, alist= inslist alist}  end
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   106
  in  ins1 (keys,net)  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   107
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   108
fun insert_term ((t,x), net, eq) = insert((key_of_term t, x), net, eq);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   109
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   110
(*** Deletion from a discrimination net ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   111
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   112
exception DELETE;       (*missing item in the net*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   113
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   114
(*Create a new Net node if it would be nonempty*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   115
fun newnet {comb=Leaf[], var=Leaf[], alist=[]} = empty
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   116
  | newnet {comb,var,alist} = Net{comb=comb, var=var, alist=alist};
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   117
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   118
(*add new (b,net) pair to the alist provided net is nonempty*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   119
fun conspair((b, Leaf[]), alist) = alist
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   120
  | conspair((b, net), alist)    = (b, net) :: alist;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   121
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   122
(*Deletes item x from the list at the node addressed by the keys.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   123
  Raises DELETE if absent.  Collapses the net if possible.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   124
  eq is the equality test for items. *)
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   125
fun delete ((keys, x), net, eq) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   126
  let fun del1 ([], Leaf xs) =
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   127
            if gen_mem eq (x,xs) then Leaf (gen_rem (eq o swap) (xs,x))
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   128
            else raise DELETE
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   129
        | del1 (keys, Leaf[]) = raise DELETE
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   130
        | del1 (CombK :: keys, Net{comb,var,alist}) =
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   131
            newnet{comb=del1(keys,comb), var=var, alist=alist}
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   132
        | del1 (VarK :: keys, Net{comb,var,alist}) =
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   133
            newnet{comb=comb, var=del1(keys,var), alist=alist}
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   134
        | del1 (AtomK a :: keys, Net{comb,var,alist}) =
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   135
            let fun newpair net = (a, del1(keys,net))
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   136
                fun dellist [] = raise DELETE
16677
6c038c13fd0f use fast_string_ord;
wenzelm
parents: 15574
diff changeset
   137
                  | dellist((b, netb) :: alist) =
6c038c13fd0f use fast_string_ord;
wenzelm
parents: 15574
diff changeset
   138
                      (case fast_string_ord (a, b) of
6c038c13fd0f use fast_string_ord;
wenzelm
parents: 15574
diff changeset
   139
                        EQUAL => conspair(newpair netb, alist)
6c038c13fd0f use fast_string_ord;
wenzelm
parents: 15574
diff changeset
   140
                      | LESS => (*absent*) raise DELETE
6c038c13fd0f use fast_string_ord;
wenzelm
parents: 15574
diff changeset
   141
                      | GREATER => (b,netb) :: dellist alist)
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   142
            in  newnet{comb=comb, var=var, alist= dellist alist}  end
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   143
  in  del1 (keys,net)  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   144
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   145
fun delete_term ((t,x), net, eq) = delete((key_of_term t, x), net, eq);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   146
16677
6c038c13fd0f use fast_string_ord;
wenzelm
parents: 15574
diff changeset
   147
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   148
(*** Retrieval functions for discrimination nets ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   149
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   150
exception OASSOC;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   151
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   152
(*Ordered association list lookup*)
16677
6c038c13fd0f use fast_string_ord;
wenzelm
parents: 15574
diff changeset
   153
fun oassoc ([], a) = raise OASSOC
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   154
  | oassoc ((b,x)::pairs, a) =
16677
6c038c13fd0f use fast_string_ord;
wenzelm
parents: 15574
diff changeset
   155
      (case fast_string_ord (a, b) of
6c038c13fd0f use fast_string_ord;
wenzelm
parents: 15574
diff changeset
   156
        EQUAL => x
6c038c13fd0f use fast_string_ord;
wenzelm
parents: 15574
diff changeset
   157
      | LESS => raise OASSOC
6c038c13fd0f use fast_string_ord;
wenzelm
parents: 15574
diff changeset
   158
      | GREATER => oassoc(pairs,a));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   159
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   160
(*Return the list of items at the given node, [] if no such node*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   161
fun lookup (Leaf(xs), []) = xs
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   162
  | lookup (Leaf _, _::_) = []  (*non-empty keys and empty net*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   163
  | lookup (Net{comb,var,alist}, CombK :: keys) = lookup(comb,keys)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   164
  | lookup (Net{comb,var,alist}, VarK :: keys) = lookup(var,keys)
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   165
  | lookup (Net{comb,var,alist}, AtomK a :: keys) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   166
      lookup(oassoc(alist,a),keys)  handle  OASSOC => [];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   167
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   168
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   169
(*Skipping a term in a net.  Recursively skip 2 levels if a combination*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   170
fun net_skip (Leaf _, nets) = nets
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   171
  | net_skip (Net{comb,var,alist}, nets) =
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   172
    foldr net_skip
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   173
          (foldr (fn ((_,net), nets) => net::nets) (var::nets) alist)
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   174
          (net_skip (comb,[]))
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   175
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   176
(** Matching and Unification**)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   177
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   178
(*conses the linked net, if present, to nets*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   179
fun look1 (alist, a) nets =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   180
       oassoc(alist,a) :: nets  handle  OASSOC => nets;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   181
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   182
(*Return the nodes accessible from the term (cons them before nets)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   183
  "unif" signifies retrieval for unification rather than matching.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   184
  Var in net matches any term.
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   185
  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
   186
                                   else matches only a variable in net.
76f60e6400e8 optimized net for matching of abstractions to speed up simplifier
nipkow
parents: 0
diff changeset
   187
*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   188
fun matching unif t (net,nets) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   189
  let fun rands _ (Leaf _, nets) = nets
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   190
        | rands t (Net{comb,alist,...}, nets) =
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   191
            case t of
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   192
                f$t => foldr (matching unif t) nets (rands f (comb,[]))
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   193
              | Const(c,_) => look1 (alist, c) nets
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   194
              | Free(c,_)  => look1 (alist, c) nets
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   195
              | Bound i    => look1 (alist, string_of_bound i) nets
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   196
              | _          => nets
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   197
  in
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   198
     case net of
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   199
         Leaf _ => nets
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   200
       | Net{var,...} =>
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   201
             case head_of t of
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   202
                 Var _ => if unif then net_skip (net,nets)
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   203
                          else var::nets           (*only matches Var in net*)
2836
148829646a51 Toby's better treatment of eta-contraction
paulson
parents: 2792
diff changeset
   204
  (*If "unif" then a var instantiation in the abstraction could allow
148829646a51 Toby's better treatment of eta-contraction
paulson
parents: 2792
diff changeset
   205
    an eta-reduction, so regard the abstraction as a wildcard.*)
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   206
               | Abs _ => if unif then net_skip (net,nets)
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   207
                          else var::nets           (*only a Var can match*)
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   208
               | _ => rands t (net, var::nets)  (*var could match also*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   209
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   210
2672
85d7e800d754 Replaced "flat" by the Basis Library function List.concat
paulson
parents: 2226
diff changeset
   211
fun extract_leaves l = List.concat (map (fn Leaf(xs) => xs) l);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   212
225
76f60e6400e8 optimized net for matching of abstractions to speed up simplifier
nipkow
parents: 0
diff changeset
   213
(*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
   214
fun match_term net t =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   215
    extract_leaves (matching false t (net,[]));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   216
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   217
(*return items whose key could unify with t*)
12319
cb3ea5750c3b most general type of delete/delete_term;
wenzelm
parents: 7943
diff changeset
   218
fun unify_term net t =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   219
    extract_leaves (matching true t (net,[]));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   220
3548
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   221
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   222
(** dest **)
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   223
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   224
fun cons_fst x (xs, y) = (x :: xs, y);
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   225
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   226
fun dest (Leaf xs) = map (pair []) xs
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   227
  | dest (Net {comb, var, alist}) =
3560
7db9a44dfa06 fixed polymorphic val;
wenzelm
parents: 3549
diff changeset
   228
      map (cons_fst CombK) (dest comb) @
7db9a44dfa06 fixed polymorphic val;
wenzelm
parents: 3549
diff changeset
   229
      map (cons_fst VarK) (dest var) @
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 12319
diff changeset
   230
      List.concat (map (fn (a, net) => map (cons_fst (AtomK a)) (dest net)) alist);
3548
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   231
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   232
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   233
(** merge **)
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 add eq (net, keys_x) =
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   236
  insert (keys_x, net, eq) handle INSERT => net;
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   237
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   238
fun merge (net1, net2, eq) =
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 12319
diff changeset
   239
  Library.foldl (add eq) (net1, dest net2);
3548
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   240
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   241
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   242
end;