src/Pure/net.ML
changeset 0 a5a9c433f639
child 225 76f60e6400e8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Pure/net.ML	Thu Sep 16 12:20:38 1993 +0200
     1.3 @@ -0,0 +1,207 @@
     1.4 +(*  Title: 	net
     1.5 +    ID:         $Id$
     1.6 +    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
     1.7 +    Copyright   1993  University of Cambridge
     1.8 +
     1.9 +Discrimination nets: a data structure for indexing items
    1.10 +
    1.11 +From the book 
    1.12 +    E. Charniak, C. K. Riesbeck, D. V. McDermott. 
    1.13 +    Artificial Intelligence Programming.
    1.14 +    (Lawrence Erlbaum Associates, 1980).  [Chapter 14]
    1.15 +*)
    1.16 +
    1.17 +signature NET = 
    1.18 +  sig
    1.19 +  type key
    1.20 +  type 'a net
    1.21 +  exception DELETE and INSERT
    1.22 +  val delete: (key list * 'a) * 'a net * ('a*'a -> bool) -> 'a net
    1.23 +  val delete_term:   (term * 'a) * 'a net * ('a*'a -> bool) -> 'a net
    1.24 +  val empty: 'a net
    1.25 +  val insert: (key list * 'a) * 'a net * ('a*'a -> bool) -> 'a net
    1.26 +  val insert_term:   (term * 'a) * 'a net * ('a*'a -> bool) -> 'a net
    1.27 +  val lookup: 'a net * key list -> 'a list
    1.28 +  val match_term: 'a net -> term -> 'a list
    1.29 +  val key_of_term: term -> key list
    1.30 +  val unify_term: 'a net -> term -> 'a list
    1.31 +  end;
    1.32 +
    1.33 +
    1.34 +functor NetFun () : NET = 
    1.35 +struct
    1.36 +
    1.37 +datatype key = CombK | VarK | AtomK of string;
    1.38 +
    1.39 +(*Only 'loose' bound variables could arise, since Abs nodes are skipped*)
    1.40 +fun string_of_bound i = "*B*" ^ chr i;
    1.41 +
    1.42 +(*Keys are preorder lists of symbols -- constants, Vars, bound vars, ...
    1.43 +  Any term whose head is a Var is regarded entirely as a Var;
    1.44 +  abstractions are also regarded as Vars (to cover eta-conversion)
    1.45 +*)
    1.46 +fun add_key_of_terms (t, cs) = 
    1.47 +  let fun rands (f$t, cs) = CombK :: rands (f, add_key_of_terms(t, cs))
    1.48 +	| rands (Const(c,_), cs) = AtomK c :: cs
    1.49 +	| rands (Free(c,_),  cs) = AtomK c :: cs
    1.50 +	| rands (Bound i,  cs) = AtomK (string_of_bound i) :: cs
    1.51 +  in case (head_of t) of
    1.52 +      Var _       => VarK :: cs
    1.53 +    | Abs (_,_,t) => VarK :: cs
    1.54 +    | _ => rands(t,cs)
    1.55 +  end;
    1.56 +
    1.57 +(*convert a term to a key -- a list of keys*)
    1.58 +fun key_of_term t = add_key_of_terms (t, []);
    1.59 +
    1.60 +
    1.61 +(*Trees indexed by key lists: each arc is labelled by a key.
    1.62 +  Each node contains a list of items, and arcs to children.
    1.63 +  Keys in the association list (alist) are stored in ascending order.
    1.64 +  The empty key addresses the entire net.
    1.65 +  Lookup functions preserve order in items stored at same level.
    1.66 +*)
    1.67 +datatype 'a net = Leaf of 'a list
    1.68 +		| Net of {comb: 'a net, 
    1.69 +			  var: 'a net,
    1.70 +			  alist: (string * 'a net) list};
    1.71 +
    1.72 +val empty = Leaf[];
    1.73 +val emptynet = Net{comb=empty, var=empty, alist=[]};
    1.74 +
    1.75 +
    1.76 +(*** Insertion into a discrimination net ***)
    1.77 +
    1.78 +exception INSERT;	(*duplicate item in the net*)
    1.79 +
    1.80 +
    1.81 +(*Adds item x to the list at the node addressed by the keys.
    1.82 +  Creates node if not already present.
    1.83 +  eq is the equality test for items. 
    1.84 +  The empty list of keys generates a Leaf node, others a Net node.
    1.85 +*)
    1.86 +fun insert ((keys,x), net, eq) =
    1.87 +  let fun ins1 ([], Leaf xs) = 
    1.88 +            if gen_mem eq (x,xs) then  raise INSERT  else Leaf(x::xs)
    1.89 +        | ins1 (keys, Leaf[]) = ins1 (keys, emptynet)   (*expand empty...*)
    1.90 +        | ins1 (CombK :: keys, Net{comb,var,alist}) =
    1.91 +	    Net{comb=ins1(keys,comb), var=var, alist=alist}
    1.92 +	| ins1 (VarK :: keys, Net{comb,var,alist}) =
    1.93 +	    Net{comb=comb, var=ins1(keys,var), alist=alist}
    1.94 +	| ins1 (AtomK a :: keys, Net{comb,var,alist}) =
    1.95 +	    let fun newpair net = (a, ins1(keys,net)) 
    1.96 +		fun inslist [] = [newpair empty]
    1.97 +		  | inslist((b: string, netb) :: alist) =
    1.98 +		      if a=b then newpair netb :: alist
    1.99 +		      else if a<b then (*absent, ins1ert in alist*)
   1.100 +			  newpair empty :: (b,netb) :: alist
   1.101 +		      else (*a>b*) (b,netb) :: inslist alist
   1.102 +	    in  Net{comb=comb, var=var, alist= inslist alist}  end
   1.103 +  in  ins1 (keys,net)  end;
   1.104 +
   1.105 +fun insert_term ((t,x), net, eq) = insert((key_of_term t, x), net, eq);
   1.106 +
   1.107 +(*** Deletion from a discrimination net ***)
   1.108 +
   1.109 +exception DELETE;	(*missing item in the net*)
   1.110 +
   1.111 +(*Create a new Net node if it would be nonempty*)
   1.112 +fun newnet {comb=Leaf[], var=Leaf[], alist=[]} = empty
   1.113 +  | newnet {comb,var,alist} = Net{comb=comb, var=var, alist=alist};
   1.114 +
   1.115 +(*add new (b,net) pair to the alist provided net is nonempty*)
   1.116 +fun conspair((b, Leaf[]), alist) = alist
   1.117 +  | conspair((b, net), alist)    = (b, net) :: alist;
   1.118 +
   1.119 +(*Deletes item x from the list at the node addressed by the keys.
   1.120 +  Raises DELETE if absent.  Collapses the net if possible.
   1.121 +  eq is the equality test for items. *)
   1.122 +fun delete ((keys, x), net, eq) = 
   1.123 +  let fun del1 ([], Leaf xs) =
   1.124 +            if gen_mem eq (x,xs) then Leaf (gen_rem eq (xs,x))
   1.125 +            else raise DELETE
   1.126 +	| del1 (keys, Leaf[]) = raise DELETE
   1.127 +	| del1 (CombK :: keys, Net{comb,var,alist}) =
   1.128 +	    newnet{comb=del1(keys,comb), var=var, alist=alist}
   1.129 +	| del1 (VarK :: keys, Net{comb,var,alist}) =
   1.130 +	    newnet{comb=comb, var=del1(keys,var), alist=alist}
   1.131 +	| del1 (AtomK a :: keys, Net{comb,var,alist}) =
   1.132 +	    let fun newpair net = (a, del1(keys,net)) 
   1.133 +		fun dellist [] = raise DELETE
   1.134 +		  | dellist((b: string, netb) :: alist) =
   1.135 +		      if a=b then conspair(newpair netb, alist)
   1.136 +		      else if a<b then (*absent*) raise DELETE
   1.137 +		      else (*a>b*)  (b,netb) :: dellist alist
   1.138 +	    in  newnet{comb=comb, var=var, alist= dellist alist}  end
   1.139 +  in  del1 (keys,net)  end;
   1.140 +
   1.141 +fun delete_term ((t,x), net, eq) = delete((key_of_term t, x), net, eq);
   1.142 +
   1.143 +(*** Retrieval functions for discrimination nets ***)
   1.144 +
   1.145 +exception OASSOC;
   1.146 +
   1.147 +(*Ordered association list lookup*)
   1.148 +fun oassoc ([], a: string) = raise OASSOC
   1.149 +  | oassoc ((b,x)::pairs, a) =
   1.150 +      if b<a then oassoc(pairs,a)
   1.151 +      else if a=b then x
   1.152 +      else raise OASSOC;
   1.153 +
   1.154 +(*Return the list of items at the given node, [] if no such node*)
   1.155 +fun lookup (Leaf(xs), []) = xs
   1.156 +  | lookup (Leaf _, _::_) = []	(*non-empty keys and empty net*)
   1.157 +  | lookup (Net{comb,var,alist}, CombK :: keys) = lookup(comb,keys)
   1.158 +  | lookup (Net{comb,var,alist}, VarK :: keys) = lookup(var,keys)
   1.159 +  | lookup (Net{comb,var,alist}, AtomK a :: keys) = 
   1.160 +      lookup(oassoc(alist,a),keys)  handle  OASSOC => [];
   1.161 +
   1.162 +
   1.163 +(*Skipping a term in a net.  Recursively skip 2 levels if a combination*)
   1.164 +fun net_skip (Leaf _, nets) = nets
   1.165 +  | net_skip (Net{comb,var,alist}, nets) = 
   1.166 +    foldr net_skip 
   1.167 +          (net_skip (comb,[]), 
   1.168 +	   foldr (fn ((_,net), nets) => net::nets) (alist, var::nets));
   1.169 +
   1.170 +(** Matching and Unification**)
   1.171 +
   1.172 +(*conses the linked net, if present, to nets*)
   1.173 +fun look1 (alist, a) nets =
   1.174 +       oassoc(alist,a) :: nets  handle  OASSOC => nets;
   1.175 +
   1.176 +(*Return the nodes accessible from the term (cons them before nets) 
   1.177 +  "unif" signifies retrieval for unification rather than matching.
   1.178 +  Var in net matches any term.
   1.179 +  Abs in object (and Var if "unif") regarded as wildcard.
   1.180 +  If not "unif", Var in object only matches a variable in net.*)
   1.181 +fun matching unif t (net,nets) =
   1.182 +  let fun rands _ (Leaf _, nets) = nets
   1.183 +	| rands t (Net{comb,alist,...}, nets) =
   1.184 +	    case t of 
   1.185 +		(f$t) => foldr (matching unif t) (rands f (comb,[]), nets)
   1.186 +	      | (Const(c,_)) => look1 (alist, c) nets
   1.187 +	      | (Free(c,_))  => look1 (alist, c) nets
   1.188 +	      | (Bound i)    => look1 (alist, string_of_bound i) nets
   1.189 +  in 
   1.190 +     case net of
   1.191 +	 Leaf _ => nets
   1.192 +       | Net{var,...} =>
   1.193 +	   case (head_of t) of
   1.194 +	       Var _      => if unif then net_skip (net,nets)
   1.195 +			     else var::nets	   (*only matches Var in net*)
   1.196 +	     | Abs(_,_,u) => net_skip (net,nets)   (*could match anything*)
   1.197 +	     | _ => rands t (net, var::nets)	   (*var could match also*)
   1.198 +  end;
   1.199 +
   1.200 +val extract_leaves = flat o map (fn Leaf(xs) => xs);
   1.201 +
   1.202 +(*return items whose key could match t*)
   1.203 +fun match_term net t = 
   1.204 +    extract_leaves (matching false t (net,[]));
   1.205 +
   1.206 +(*return items whose key could unify with t*)
   1.207 +fun unify_term net t = 
   1.208 +    extract_leaves (matching true t (net,[]));
   1.209 +
   1.210 +end;