0

I have to use a sorted map/index structure in ACL2. Currently I have the following:

( (key1 . (val1 val2)) (key2 . (val3)) (key3 . (val4 val5 val6)) )

Is there any other way of doing this more efficiently?

user720491
  • 589
  • 1
  • 9
  • 20

1 Answers1

0

It isn't clear to me, from your example, what you are trying to do.

You could certainly define operations on an association list that keep it in a key-sorted order. In that case:

  • your get function would look just like acons
  • your put function would need to look for the "right" place to put the element.

But this isn't particularly efficient. Both operations would be O(n). Also, as a practical consideration, the put operation would require O(n) conses, which is especially expensive since cons allocates memory.

Usually it's better to just use ordinary association lists, i.e., using acons and assoc. The main advantage is that, since it simply conses the new key/value pair onto the front of the list, acons is O(1), so constructing your mappings is generally much cheaper. You can always sort the keys of the alist if you need to, e.g., using set::mergesort or some custom sorting function.

Accessing alists is still O(n). However, fast-alists are available in ACL2(h) and essentially provide a way to get alists with hash table speeds. See also the documentation for std/alists.

Jared Davis
  • 559
  • 4
  • 12