maps and set
jmchapman opened this issue Β· comments
Add support for maps and sets
I have some code that I can contribute:
I figured that module names Haskell.Data.Map
and Haskell.Data.Set
are appropriate.
I'm wondering how to deal with the Setoid question. π€
The issue is this:
Consider two x y : β a
that are equal x == y β‘ True
. In general, it does not follow that x β‘ y
. For example, if we were to provide an implementation in terms of binary search trees as part of agda2hs
, then postulating x β‘ y
would actually make the theory inconsistent because the trees can be distinguished internally.
Unfortunately, distinguishing x
and y
has ripple effects on structures built on top of Data.Set
, e.g. the monoid Set.union
will not be a lawful Monoid
instance β because "lawful" is defined in terms of β‘
at the moment.
In general, it would be desirable to be able to substitute x
for y
in any context, which is equivalent to x β‘ y
. In Haskell, this is "solved" through abstraction: All exported functions of Data.Set
β except for a few debugging functions β preserve the equivalence relation, and we can pretend that x β‘ y
as long as we only use functions from the public interface.
The alternative is Setoids β which implies that all "lawful" class instances will have to be redefined in terms of an equivalence relation, e.g. mempty <> x β x
instead of mempty <> x β‘ x
.
My postulate
-based axiomatization of Data.Map
is now public:
This is an axiomatization in the sense a finite map m
is completely determined by:
- The extensional behavior of the
lookup
function. - The fact that the
lookup
function isJust
on only finitely many elements of its domain.
I'm not yet fully happy with it, though:
- Prefixing all properties with
prop-
is cumbersome. I think that a separate moduleHaskell.Law.Data.Map
orHaskell.Data.Map.Law
is more appropriate. - On the setoid question, I now believe that the best way to proceed is to add an axiomatization of quotient types. Then, we have two types:
Data.Map
the implementation in terms of binary trees, andData.Map
the quotient type uniquely determined bylookup
. (The fact aboutlookup
probably makes this an instance of a definable quotient type, with the twist that extensional equality onA β Maybe B
is also a quotient of sorts.)