[m-users.] How to declare a set of typeclasses?

Zoltan Somogyi zoltan.somogyi at runbox.com
Wed Oct 11 09:42:59 AEDT 2023


On 2023-10-11 09:19 +11:00 AEDT, "Sean Charles (emacstheviking)" <objitsu at gmail.com> wrote:
> Then I did what I thought would work:
> 
> :- type l1_set(T) == set(hittable(T)) <= hittable(T).

If what you are after is "the type of a set of things where each thing is hittable",
what you need to do is to simply use "set(T)" where you want this, *and*
add the typeclass constraint "<= hittable(T)" to the declarations of the
predicates and functions where such an argument appears. For an example,
have a look at e.g. sparse_bitset.m in the standard library.

> And here is the actual code where I am trying to use it, I am operating on the assumption
> that by default Mercury has some way of creating a unique hash for each object

That assumption is incorrect. The int.m, uint.m and string.m modules
of the standard library define hash functions, but for other types,
providing a hash function is up to you. And while there exist algorithms
for creating perfect hash functions (i.e. hash functions that don't generate
any collisions on a given set of input values),

- they guarantee the absence of collisions *only* for that set of values,
- they require the values of that set to be specified in advance,
- and even then they fail to generate a hash function for some sets.

Even cryptographically secure hash functions can have collisions.
The pigeon-hole principle guarantees this for any hash function
that generates a hash value of a fixed size for inputs of unbounded size.

>  Ah.. it is section 3.8 of the online page https://mercury-in.space/crash.html#orga13c54a

That section does not talk about hashing.

Zoltan.


More information about the users mailing list