Suppose that a map is needed for mapping keys of type key_t to items of type itm_t. The following code essentially sets up an interface for creating and operating on such a map based on a balanced-tree implementation in ATSLIB/libats:
local // typedef key = key_t and itm = itm_t // staload FM = "libats/ML/SATS/funmap.sats" implement $FM.compare_key_key<key>(x, y) = compare(x, y) // in (* in-of-local *) #include "libats/ML/HATS/myfunmap.hats" end // end of [local]
Assume that key_t is string and itm_t is int. The following line of code creates an empty functional map:
The following few lines insert some key/item pairs into mymap:// var mymap = mymap // val-~None_vt() = mymap.insert("a", 0) val-~Some_vt(0) = mymap.insert("a", 1) // val-~None_vt() = mymap.insert("b", 1) val-~Some_vt(1) = mymap.insert("b", 2) // val-~None_vt() = mymap.insert("c", 2) val-~Some_vt(2) = mymap.insert("c", 3) //
// val-true = mymap.remove("a") val-false = mymap.remove("a") // val-~Some_vt(2) = mymap.takeout("b") val-~Some_vt(3) = mymap.takeout("c") //
The following function is available for generating a module (that is, a record) containing various operations on mymap-values:
For instance, a module of the name MYMAP is created as follows: With MYMAP, we can create a (functional) map and then perform various insertion and removal operations:// var mymap = (MYMAP.nil)() // val-~None_vt() = (MYMAP.insert)(mymap, "a", 0) val-~None_vt() = (MYMAP.insert)(mymap, "b", 1) val-~None_vt() = (MYMAP.insert)(mymap, "c", 2) // val-~Some_vt(0) = (MYMAP.insert)(mymap, "a", 1) val-~Some_vt(1) = (MYMAP.insert)(mymap, "b", 2) val-~Some_vt(2) = (MYMAP.insert)(mymap, "c", 3) // val-(true) = (MYMAP.remove)(mymap, "a") val-(true) = (MYMAP.remove)(mymap, "b") val-(true) = (MYMAP.remove)(mymap, "c") // val () = assertloc((MYMAP.size)(mymap) = 0) //
Various common map operations can be found in libats/ML/HATS/myfunmap.hats. By following the types assigned to these operations, one should have no difficulty in figuring out how they are supposed to be called. Please find the entirety of the code used in this section on-line.