src/Pure/net.ML
author wenzelm
Mon, 06 Oct 1997 18:22:22 +0200
changeset 3776 38f8ec304b95
parent 3560 7db9a44dfa06
child 6539 2e7d2fba9f6c
permissions -rw-r--r--
added sort_to_ast; eliminated raise_ast;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3549
e8c8d76810a6 tuned title;
wenzelm
parents: 3548
diff changeset
     1
(*  Title: 	Pure/net.ML
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     2
    ID:         $Id$
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
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
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     8
From the book 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     9
    E. Charniak, C. K. Riesbeck, D. V. McDermott. 
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
228
4f43430f226e some optimizations of Larry's
nipkow
parents: 225
diff changeset
    13
match_term no longer treats abstractions as wildcards; instead they match 
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
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    17
signature NET = 
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
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    22
  val delete: (key list * 'a) * 'a net * ('a*'a -> bool) -> 'a net
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    23
  val delete_term:   (term * 'a) * 'a net * ('a*'a -> bool) -> 'a net
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
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    35
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
    36
structure Net : NET = 
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    37
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    38
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    39
datatype key = CombK | VarK | AtomK of string;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    40
225
76f60e6400e8 optimized net for matching of abstractions to speed up simplifier
nipkow
parents: 0
diff changeset
    41
(*Bound variables*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    42
fun string_of_bound i = "*B*" ^ chr i;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    43
228
4f43430f226e some optimizations of Larry's
nipkow
parents: 225
diff changeset
    44
(*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
    45
  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
    46
  Abstractions are also regarded as Vars;  this covers eta-conversion
4f43430f226e some optimizations of Larry's
nipkow
parents: 225
diff changeset
    47
    and "near" eta-conversions such as %x.?P(?f(x)).
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    48
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    49
fun add_key_of_terms (t, cs) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    50
  let fun rands (f$t, cs) = CombK :: rands (f, add_key_of_terms(t, cs))
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    51
	| rands (Const(c,_), cs) = AtomK c :: cs
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    52
	| rands (Free(c,_),  cs) = AtomK c :: cs
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    53
	| rands (Bound i,  cs)   = AtomK (string_of_bound i) :: cs
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    54
  in case (head_of t) of
225
76f60e6400e8 optimized net for matching of abstractions to speed up simplifier
nipkow
parents: 0
diff changeset
    55
      Var _ => VarK :: cs
228
4f43430f226e some optimizations of Larry's
nipkow
parents: 225
diff changeset
    56
    | Abs _ => VarK :: cs
225
76f60e6400e8 optimized net for matching of abstractions to speed up simplifier
nipkow
parents: 0
diff changeset
    57
    | _     => rands(t,cs)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    58
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    59
225
76f60e6400e8 optimized net for matching of abstractions to speed up simplifier
nipkow
parents: 0
diff changeset
    60
(*convert a term to a list of keys*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    61
fun key_of_term t = add_key_of_terms (t, []);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    62
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    63
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    64
(*Trees indexed by key lists: each arc is labelled by a key.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    65
  Each node contains a list of items, and arcs to children.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    66
  Keys in the association list (alist) are stored in ascending order.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    67
  The empty key addresses the entire net.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    68
  Lookup functions preserve order in items stored at same level.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    69
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    70
datatype 'a net = Leaf of 'a list
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    71
		| Net of {comb: 'a net, 
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    72
			  var: 'a net,
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    73
			  alist: (string * 'a net) list};
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    74
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    75
val empty = Leaf[];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    76
val emptynet = Net{comb=empty, var=empty, alist=[]};
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    77
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    78
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    79
(*** Insertion into a discrimination net ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    80
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    81
exception INSERT;	(*duplicate item in the net*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    82
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    83
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    84
(*Adds item x to the list at the node addressed by the keys.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    85
  Creates node if not already present.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    86
  eq is the equality test for items. 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    87
  The empty list of keys generates a Leaf node, others a Net node.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    88
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    89
fun insert ((keys,x), net, eq) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    90
  let fun ins1 ([], Leaf xs) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    91
            if gen_mem eq (x,xs) then  raise INSERT  else Leaf(x::xs)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    92
        | ins1 (keys, Leaf[]) = ins1 (keys, emptynet)   (*expand empty...*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    93
        | ins1 (CombK :: keys, Net{comb,var,alist}) =
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    94
	    Net{comb=ins1(keys,comb), var=var, alist=alist}
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    95
	| ins1 (VarK :: keys, Net{comb,var,alist}) =
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    96
	    Net{comb=comb, var=ins1(keys,var), alist=alist}
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    97
	| ins1 (AtomK a :: keys, Net{comb,var,alist}) =
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    98
	    let fun newpair net = (a, ins1(keys,net)) 
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    99
		fun inslist [] = [newpair empty]
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   100
		  | inslist((b: string, netb) :: alist) =
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   101
		      if a=b then newpair netb :: alist
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   102
		      else if a<b then (*absent, ins1ert in alist*)
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   103
			  newpair empty :: (b,netb) :: alist
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   104
		      else (*a>b*) (b,netb) :: inslist alist
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
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
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
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. *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   125
fun delete ((keys, x), net, eq) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   126
  let fun del1 ([], Leaf xs) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   127
            if gen_mem eq (x,xs) then Leaf (gen_rem eq (xs,x))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   128
            else raise DELETE
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   129
	| del1 (keys, Leaf[]) = raise DELETE
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   130
	| del1 (CombK :: keys, Net{comb,var,alist}) =
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   131
	    newnet{comb=del1(keys,comb), var=var, alist=alist}
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   132
	| del1 (VarK :: keys, Net{comb,var,alist}) =
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   133
	    newnet{comb=comb, var=del1(keys,var), alist=alist}
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   134
	| del1 (AtomK a :: keys, Net{comb,var,alist}) =
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   135
	    let fun newpair net = (a, del1(keys,net)) 
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   136
		fun dellist [] = raise DELETE
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   137
		  | dellist((b: string, netb) :: alist) =
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   138
		      if a=b then conspair(newpair netb, alist)
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   139
		      else if a<b then (*absent*) raise DELETE
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   140
		      else (*a>b*)  (b,netb) :: dellist alist
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   141
	    in  newnet{comb=comb, var=var, alist= dellist alist}  end
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   142
  in  del1 (keys,net)  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   143
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   144
fun delete_term ((t,x), net, eq) = delete((key_of_term t, x), net, eq);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   145
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   146
(*** Retrieval functions for discrimination nets ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   147
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   148
exception OASSOC;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   149
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   150
(*Ordered association list lookup*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   151
fun oassoc ([], a: string) = raise OASSOC
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   152
  | oassoc ((b,x)::pairs, a) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   153
      if b<a then oassoc(pairs,a)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   154
      else if a=b then x
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   155
      else raise OASSOC;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   156
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   157
(*Return the list of items at the given node, [] if no such node*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   158
fun lookup (Leaf(xs), []) = xs
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   159
  | lookup (Leaf _, _::_) = []	(*non-empty keys and empty net*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   160
  | lookup (Net{comb,var,alist}, CombK :: keys) = lookup(comb,keys)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   161
  | lookup (Net{comb,var,alist}, VarK :: keys) = lookup(var,keys)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   162
  | lookup (Net{comb,var,alist}, AtomK a :: keys) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   163
      lookup(oassoc(alist,a),keys)  handle  OASSOC => [];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   164
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   165
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   166
(*Skipping a term in a net.  Recursively skip 2 levels if a combination*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   167
fun net_skip (Leaf _, nets) = nets
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   168
  | net_skip (Net{comb,var,alist}, nets) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   169
    foldr net_skip 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   170
          (net_skip (comb,[]), 
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   171
	   foldr (fn ((_,net), nets) => net::nets) (alist, var::nets));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   172
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   173
(** Matching and Unification**)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   174
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   175
(*conses the linked net, if present, to nets*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   176
fun look1 (alist, a) nets =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   177
       oassoc(alist,a) :: nets  handle  OASSOC => nets;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   178
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   179
(*Return the nodes accessible from the term (cons them before nets) 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   180
  "unif" signifies retrieval for unification rather than matching.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   181
  Var in net matches any term.
225
76f60e6400e8 optimized net for matching of abstractions to speed up simplifier
nipkow
parents: 0
diff changeset
   182
  Abs or Var in object: if "unif", regarded as wildcard, 
76f60e6400e8 optimized net for matching of abstractions to speed up simplifier
nipkow
parents: 0
diff changeset
   183
                                   else matches only a variable in net.
76f60e6400e8 optimized net for matching of abstractions to speed up simplifier
nipkow
parents: 0
diff changeset
   184
*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   185
fun matching unif t (net,nets) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   186
  let fun rands _ (Leaf _, nets) = nets
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   187
	| rands t (Net{comb,alist,...}, nets) =
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   188
	    case t of 
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   189
		f$t => foldr (matching unif t) (rands f (comb,[]), nets)
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   190
	      | Const(c,_) => look1 (alist, c) nets
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   191
	      | Free(c,_)  => look1 (alist, c) nets
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   192
	      | Bound i    => look1 (alist, string_of_bound i) nets
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   193
	      | _      	   => nets
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   194
  in 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   195
     case net of
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
   196
	 Leaf _ => nets
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   197
       | Net{var,...} =>
2836
148829646a51 Toby's better treatment of eta-contraction
paulson
parents: 2792
diff changeset
   198
	     let val etat = Pattern.eta_contract_atom t
148829646a51 Toby's better treatment of eta-contraction
paulson
parents: 2792
diff changeset
   199
	     in case (head_of etat) of
148829646a51 Toby's better treatment of eta-contraction
paulson
parents: 2792
diff changeset
   200
		 Var _ => if unif then net_skip (net,nets)
148829646a51 Toby's better treatment of eta-contraction
paulson
parents: 2792
diff changeset
   201
			  else var::nets	   (*only matches Var in net*)
148829646a51 Toby's better treatment of eta-contraction
paulson
parents: 2792
diff changeset
   202
  (*If "unif" then a var instantiation in the abstraction could allow
148829646a51 Toby's better treatment of eta-contraction
paulson
parents: 2792
diff changeset
   203
    an eta-reduction, so regard the abstraction as a wildcard.*)
148829646a51 Toby's better treatment of eta-contraction
paulson
parents: 2792
diff changeset
   204
	       | Abs _ => if unif then net_skip (net,nets)
148829646a51 Toby's better treatment of eta-contraction
paulson
parents: 2792
diff changeset
   205
			  else var::nets	   (*only a Var can match*)
148829646a51 Toby's better treatment of eta-contraction
paulson
parents: 2792
diff changeset
   206
	       | _ => rands etat (net, var::nets)  (*var could match also*)
148829646a51 Toby's better treatment of eta-contraction
paulson
parents: 2792
diff changeset
   207
	     end
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   208
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   209
2672
85d7e800d754 Replaced "flat" by the Basis Library function List.concat
paulson
parents: 2226
diff changeset
   210
fun extract_leaves l = List.concat (map (fn Leaf(xs) => xs) l);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   211
225
76f60e6400e8 optimized net for matching of abstractions to speed up simplifier
nipkow
parents: 0
diff changeset
   212
(*return items whose key could match t, WHICH MUST BE BETA-ETA NORMAL*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   213
fun match_term net t = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   214
    extract_leaves (matching false t (net,[]));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   215
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   216
(*return items whose key could unify with t*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   217
fun unify_term net t = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   218
    extract_leaves (matching true t (net,[]));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   219
3548
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   220
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   221
(** dest **)
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   222
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   223
fun cons_fst x (xs, y) = (x :: xs, y);
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   224
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   225
fun dest (Leaf xs) = map (pair []) xs
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   226
  | dest (Net {comb, var, alist}) =
3560
7db9a44dfa06 fixed polymorphic val;
wenzelm
parents: 3549
diff changeset
   227
      map (cons_fst CombK) (dest comb) @
7db9a44dfa06 fixed polymorphic val;
wenzelm
parents: 3549
diff changeset
   228
      map (cons_fst VarK) (dest var) @
7db9a44dfa06 fixed polymorphic val;
wenzelm
parents: 3549
diff changeset
   229
      flat (map (fn (a, net) => map (cons_fst (AtomK a)) (dest net)) alist);
3548
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   230
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   231
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   232
(** merge **)
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   233
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   234
fun add eq (net, keys_x) =
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   235
  insert (keys_x, net, eq) handle INSERT => net;
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   236
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   237
fun merge (net1, net2, eq) =
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   238
  foldl (add eq) (net1, dest net2);
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   239
108d09eb3454 added dest and merge operations;
wenzelm
parents: 2836
diff changeset
   240
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   241
end;