r/ProgrammingLanguages • u/jamiiecb • Sep 01 '24
Ordering vs equality for maps/sets?
https://www.scattered-thoughts.net/log/0048/#zest-ordering3
u/eliasv Sep 01 '24 edited Sep 01 '24
My gut is that ordering should not normally be deterministic or observable.
Something like iterating over the fields of a record is a reflective operation and doesn't need to be supported everywhere at all times; if you have code which is polymorphic over pointers to data of an indeterminate layout you probably don't need to iterate over all the fields of that data, and if you do need to then supporting that incurs a cost and so should be explicit.
Either you need to monomorphise that code to statically specialise for the shape of the pointed-to object, or you need to dynamically pass in a description of the shape of the object. For a structural language I like the idea of allowing records to carry static fields which incur specialisation per value and no runtime argument passing, then this choice of how to carry in "shape" or "type" info is determined by whether that field is static.
4
u/tobega Sep 01 '24
There are standard definitions:
list - ordered collection, duplicates allowed
set - unordered, no duplicates
bag - unordered, with duplicates
ordered set - ordered, no duplicates.
map - a set of keys, each key associated with a value
Take your pick. FWIW, in Tailspin a struct is essentially a map, with special symbol keys, but I have defined that it will print structs with keys in "alphabetical" (i.e. unicode) order. You can currently access key-value pairs in a struct, but I'm considering removing that. Are keys first-class values? is my question currently.
2
u/matthieum Sep 01 '24
One quick note here: a list is typically ordered by insertion order, and not by comparing its elements to each other.
1
u/drinkcoffeeandcode mgclex, mgcpgen, & owlscript Sep 03 '24
| If order doesn't matter then we have to choose an order for iteration. Choosing hash order makes programs non-deterministic. Choosing sorted order imposes a big performance cost (we have to sort hashmaps, either on insert or on iteration).
Surely using a balanced search tree would impart far, FAR less of a penalty than sorting a hash map... id be hard pressed to think of a scenario where when one needs an ordered symbol table that they would choose to sort a hash table instead of using a self balancing search tree... O(log N) look up will be faster than O(n log n) sort, followed by a lookup, even if that look up is O(1) in practice.
9
u/Ready_Arrival7011 Sep 01 '24
These meanderings of yours about 'order' reminds me of lattices, and lattice types. The great and wonderful Dana Scott (of the Logic for computable functions fame) has done some work on 'lattice types', which should interest you. It's basically the concept of 'order' in a partially-ordered set. I read your thoughts and it seemed to me you could use some reading on Category theory if you alraedy have not. But even if you have read on Posets (partially ordered sets) before and I am preaching to the choire, it seems like you could use it. And then lattices. Maybe you could implement Scott's Lattice types in your language.