The HashConsMap structure implements functional, finite maps keyed
by hash-consed objects. A balanced tree structure is used in the
representation.
Synopsis
signature HASH_CONS_MAP
structure HashConsMap : HASH_CONS_MAP
Interface
type 'a obj = 'a HashCons.obj
type ('a, 'b) map
val isEmpty : ('a, 'b) map -> bool
val singleton : ('a obj * 'b) -> ('a, 'b) map
val insert : ('a, 'b) map * 'a obj * 'b -> ('a, 'b) map
val insert' : (('a obj * 'b) * ('a, 'b) map) -> ('a, 'b) map
val insertWith : (('b * 'b) -> 'b)
-> ('a, 'b) map * 'a obj * 'b -> ('a, 'b) map
val insertWithi : (('a obj * 'b * 'b) -> 'b)
-> ('a, 'b) map * 'a obj * 'b -> ('a, 'b) map
val find : ('a, 'b) map * 'a obj -> 'b option
val lookup : ('a, 'b) map * 'a obj -> 'b
val inDomain : (('a, 'b) map * 'a obj) -> bool
val remove : ('a, 'b) map * 'a obj -> ('a, 'b) map * 'b
val empty : ('a, 'b) map
val numItems : ('a, 'b) map -> int
val listItems : ('a, 'b) map -> 'b list
val listItemsi : ('a, 'b) map -> ('a obj * 'b) list
val listKeys : ('a, 'b) map -> 'a obj list
val collate : ('b * 'b -> order) -> (('a, 'b) map * ('a, 'b) map) -> order
val unionWith : ('b * 'b -> 'b) -> (('a, 'b) map * ('a, 'b) map)
-> ('a, 'b) map
val unionWithi : ('a obj * 'b * 'b -> 'b) -> (('a, 'b) map * ('a, 'b) map)
-> ('a, 'b) map
val intersectWith : ('b * 'c -> 'd) -> (('a, 'b) map * ('a, 'c) map)
-> ('a, 'd) map
val intersectWithi : ('a obj * 'b * 'c -> 'd) -> (('a, 'b) map * ('a, 'c) map)
-> ('a, 'd) map
val mergeWith : ('b option * 'c option -> 'd option)
-> (('a, 'b) map * ('a, 'c) map) -> ('a, 'd) map
val mergeWithi : ('a obj * 'b option * 'c option -> 'd option)
-> (('a, 'b) map * ('a, 'c) map) -> ('a, 'd) map
val app : ('b -> unit) -> ('a, 'b) map -> unit
val appi : (('a obj * 'b) -> unit) -> ('a, 'b) map -> unit
val map : ('b -> 'c) -> ('a, 'b) map -> ('a, 'c) map
val mapi : ('a obj * 'b -> 'c) -> ('a, 'b) map -> ('a, 'c) map
val fold : ('b * 'c -> 'c) -> 'c -> ('a, 'b) map -> 'c
val foldi : ('a obj * 'b * 'c -> 'c) -> 'c -> ('a, 'b) map -> 'c
val filter : ('b -> bool) -> ('a, 'b) map -> ('a, 'b) map
val filteri : ('a obj * 'b -> bool) -> ('a, 'b) map -> ('a, 'b) map
val mapPartial : ('b -> 'c option) -> ('a, 'b) map -> ('a, 'c) map
val mapPartiali : ('a obj * 'b -> 'c option) -> ('a, 'b) map -> ('a, 'c) map
val exists : ('b -> bool) -> ('a, 'b) map -> bool
val existsi : ('a obj * 'b -> bool) -> ('a, 'b) map -> bool
val all : ('b -> bool) -> ('a, 'b) map -> bool
val alli : ('a obj * 'b -> bool) -> ('a, 'b) map -> bool
Description
In the description of operations below, we write \(\mathbf{dom}(m)\) for the domain of the map \(m\) (i.e, the set of keys for which \(m\) is defined), and \(\mathbf{rng}(m)\) for its range (i.e., the set \(\{ m(k)\;|\;k \in \mathbf{dom}(m) \}\)). It is also useful to view a map as the set of key-value pairs \(\{ (k, m(k))\;|\;k \in \mathbf{dom}(m) \}\), which we call the items of \(m\).
type 'a obj = 'a HashCons.obj-
Hash-consed objects are the search keys for the finite maps.
type ('a, 'b) map-
A finite map from
'a objvalues to'bvalues. val empty : ('a, 'b) map-
The empty map.
val singleton : ('a obj * 'b) -> ('a, 'b) map-
singleton (obj, v)creates the singleton map that mapsobjtov. val insert : ('a, 'b) map * 'a obj * 'b -> ('a, 'b) map-
insert (m, obj, v)adds the mapping fromobjtovtom. This mapping overrides any previous mapping fromobj. val insert' : (('a obj * 'b) * ('a, 'b) map) -> ('a, 'b) map-
insert' ((obj, v), map)adds the mapping fromobjtovtom. This mapping overrides any previous mapping fromobj. val insertWith : (('b * 'b) -> 'b) -> ('a, 'b) map * 'a obj * 'b -> ('a, 'b) map-
insertWith comb (m, obj, v)adds the mapping fromobjtovaluetom, wherevalue = comb(v', v), ifmalready contained a mapping fromobjtov'; otherwise,value = v. val insertWithi : (('a obj * 'b * 'b) -> 'b) -> ('a, 'b) map * 'a obj * 'b -> ('a, 'b) map-
insertWithi comb (m, obj, v)adds the mapping fromobjtovaluetom, wherevalue = comb(obj, v', v), ifmalready contained a mapping fromobjtov'; otherwise,value = v. val find : ('a, 'b) map * 'a obj -> 'b option-
find (m, obj)returnsSOME v, ifmmapsobjtovandNONEotherwise. val lookup : ('a, 'b) map * 'a obj -> 'b-
lookup (m, obj)returnsv, ifmmapsobjtov; otherwise it raises the exceptionNotFound. val inDomain : (('a, 'b) map * 'a obj) -> bool-
inDomain (m, obj)returnstrueifobjis in the domain ofm. val remove : ('a, 'b) map * 'a obj -> ('a, 'b) map * 'b-
remove (m, obj)returns the pair(m', v), ifmmapsobjtovand wherem'ismwithobjremoved from its domain. Ifobjis not in the domain ofm, then it raises the exceptionNotFound. val isEmpty : ('a, 'b) map -> bool-
isEmpty mreturns true if, and only if,mis empty. val numItems : ('a, 'b) map -> int-
numItems mreturns the size ofm's domain. val listItems : ('a, 'b) map -> 'b list-
listItems mreturns a list of the values in the range ofm. Note that this list will contain duplicates when multiple keys inm's domain map to the same value. val listKeys : ('a, 'b) map -> 'a obj list-
listKeys mreturns a list of the objects in the domain ofm. val listItemsi : ('a, 'b) map -> ('a obj * 'b) list-
listItemsi mreturns a list of(obj, v)pairs, wheremmapsobjtov. val collate : ('b * 'b -> order) -> (('a, 'b) map * ('a, 'b) map) -> order-
collate cmpV (m1, m2)returns the order of the two maps, wherecmpVis used to compare the values in the domain. val unionWith : ('b * 'b -> 'b) -> (('a, 'b) map * ('a, 'b) map) -> ('a, 'b) map-
unionWith comb (m1, m2)returns the union of the two maps, using the functioncombto combine values when there is a collision of keys. More formally, this expression returns the map\[ \begin{array}{l} \{ (k, \mathtt{m1}(k)) \;|\;k \in \mathbf{dom}(\mathtt{m1}) \setminus \mathbf{dom}(\mathtt{m2}) \} \cup \\ \{ (k, \mathtt{m2}(k)) \;|\;k \in \mathbf{dom}(\mathtt{m2}) \setminus \mathbf{dom}(\mathtt{m1}) \} \cup \\ \{ (k, \mathtt{comb}(\mathtt{m1}(k), \mathtt{m2}(k)) \;|\;k \in \mathbf{dom}(\mathtt{m1}) \cap \mathbf{dom}(\mathtt{m2}) \} \end{array}\]For example, we could implement a multiset of objects by mapping objects to their multiplicity. Then, the union of two multisets could be defined by
fun union (ms1, ms2) = unionWith Int.+ (ms1, ms2) val unionWithi : ('a obj * 'b * 'b -> 'b) -> (('a, 'b) map * ('a, 'b) map) -> ('a, 'b) map-
unionWithi comb (m1, m2)returns the union of the two maps, using the functioncombto combine values when there is a collision of keys. More formally, this expression returns the map\[ \begin{array}{l} \{ (k, \mathtt{m1}(k)) \;|\;k \in \mathbf{dom}(\mathtt{m1}) \setminus \mathbf{dom}(\mathtt{m2}) \} \cup \\ \{ (k, \mathtt{m2}(k)) \;|\;k \in \mathbf{dom}(\mathtt{m2}) \setminus \mathbf{dom}(\mathtt{m1}) \} \cup \\ \{ (k, \mathtt{comb}(k, \mathtt{m1}(k), \mathtt{m2}(k)) \;|\;k \in \mathbf{dom}(\mathtt{m1}) \cap \mathbf{dom}(\mathtt{m2}) \} \end{array}\] val intersectWith : ('b * 'c -> 'd) -> (('a, 'b) map * ('a, 'c) map) -> ('a, 'd) map-
intersectWith comb (m1, m2)returns the intersection of the two maps, where the values in the range are a computed by applying the functioncombto the values from the two maps. More formally, this expression returns the map\[ \{ (k, \mathtt{comb}(\mathtt{m1}(k), \mathtt{m2}(k)) \;|\;k \in \mathbf{dom}(\mathtt{m1}) \cap \mathbf{dom}(\mathtt{m2}) \}\] val intersectWithi : ('a obj * 'b * 'c -> 'd) -> (('a, 'b) map * ('a, 'c) map) -> ('a, 'd) map-
intersectWithi comb (m1, m2)returns the intersection of the two maps, where the values in the range are a computed by applying the functioncombto the kay and the values from the two maps. More formally, this expression returns the map\[ \{ (k, \mathtt{comb}(k, \mathtt{m1}(k), \mathtt{m2}(k)) \;|\;k \in \mathbf{dom}(\mathtt{m1}) \cap \mathbf{dom}(\mathtt{m2}) \}\] val mergeWith : ('b option * 'c option -> 'd option) -> (('a, 'b) map * ('a, 'c) map) -> ('a, 'd) map-
mergeWith comb (m1, m2)merges the two maps using the functioncombas a decision procedure for adding elements to the new map. For each object \(\mathtt{obj} \in \mathbf{dom}(\mathtt{m1}) \cup \mathbf{dom}(\mathtt{m2})\), we evaluatecomb(optV1, optV2), whereoptV1isSOME vif \((\mathtt{obj}, \mathtt{v}) \in \mathtt{m1}\) and isNONEif latexmath:[\mathtt{obj} \not\in \mathbf{dom}(\mathtt{m1}); likewise foroptV2. Ifcomb(optV1, optV2)returnsSOME v', then we add(obj, v')to the result.The
mergeWithfunction is a generalization of theunionWithandintersectionWithfunctions. val mergeWithi : ('a obj * 'b option * 'c option -> 'd option) -> (('a, 'b) map * ('a, 'c) map) -> ('a, 'd) map-
mergeWithi comb (m1, m2)merges the two maps using the functioncombas a decision procedure for adding elements to the new map. The difference between this function andmergeWithis that thecombfunction takes thekeyvalue in addition to the optional values from the range. val app : ('b -> unit) -> ('a, 'b) map -> unit-
app f mapplies the functionfto the values in the range ofm. val appi : (('a obj * 'b) -> unit) -> ('a, 'b) map -> unit-
appi f mapapplies the functionfto the key-value pairs that definem. val map : ('b -> 'c) -> ('a, 'b) map -> ('a, 'c) map-
map f mcreates a new finite mapm'by applying the functionfto the values in the range ofm. Thus, if \((\mathtt{obj}, \mathtt{v}) \in \mathtt{m}\), then(obj, f v)will be inm'. val mapi : ('a obj * 'b -> 'c) -> ('a, 'b) map -> ('a, 'c) map-
mapi f mcreates a new finite mapm'by applying the functionfto the key-value pairs ofm. Thus, if \((\mathtt{obj}, \mathtt{v}) \in \mathtt{m}\), then(obj, f(obj, v))will be inm'. val fold : ('b * 'c -> 'c) -> 'c -> ('a, 'b) map -> 'c-
fold f init mfolds the functionfover the range ofmusinginitas the initial value. val foldi : ('a obj * 'b * 'c -> 'c) -> 'c -> ('a, 'b) map -> 'c-
foldi f init mfolds the functionfover the key-value pairs inmusinginitas the initial value. val filter : ('b -> bool) -> ('a, 'b) map -> ('a, 'b) map-
filter pred mfilters out those items(obj, v)fromm, such thatpred vreturnsfalse. More formally, this expression returns the map \(\{ (\mathtt{obj}, \mathtt{v})\;|\;\mathtt{obj} \in \mathbf{dom}(\mathtt{m}) \wedge \mathtt{pred}(\mathtt{v}) \}\). val filteri : ('a obj * 'b -> bool) -> ('a, 'b) map -> ('a, 'b) map-
filteri pred mfilters out those items(obj, v)fromm, such thatpred(obj, v)returnsfalse. More formally, this expression returns the map \(\{ (\mathtt{obj}, \mathtt{v})\;|\;\mathtt{obj} \in \mathbf{dom}(\mathtt{m}) \wedge \mathtt{pred}(\mathtt{obj}, \mathtt{v}) \}\). val mapPartial : ('b -> 'c option) -> ('a, 'b) map -> ('a, 'c) map-
mapPartial f mmaps the partial functionfover the items ofm. More formally, this expression returns the map
val mapPartiali : ('a obj * 'b -> 'c option) -> ('a, 'b) map -> ('a, 'c) map-
mapPartiali f mmaps the partial functionfover the items ofm. More formally, this expression returns the map
val exists : ('b -> bool) -> ('a, 'b) map -> bool-
exists pred mreturnstrueif, and only if, there exists an item \((\mathtt{obj}, \mathtt{v}) \in \mathtt{m}\), such thatpred vreturnstrue. val existsi : ('a obj * 'b -> bool) -> ('a, 'b) map -> bool-
exists pred mreturnstrueif, and only if, there exists an item \((\mathtt{obj}, \mathtt{v}) \in \mathtt{m}\), such thatpred(obj, v)returnstrue. val all : ('b -> bool) -> ('a, 'b) map -> bool-
all pred mreturnstrueif, and only if,pred vreturnstruefor all items \((\mathtt{obj}, \mathtt{v}) \in \mathtt{m}\). val alli : ('a obj * 'b -> bool) -> ('a, 'b) map -> bool-
all pred mreturnstrueif, and only if,pred(obj, v)returnstruefor all items \((\mathtt{obj}, \mathtt{v}) \in \mathtt{m}\).