[m-users.] Existential types example

Volker Wysk post at volker-wysk.de
Sat Oct 1 03:53:36 AEST 2022


Hi!

I'm trying to learn existential types. Here are two good tutorials. They're
for Haskell:

https://downloads.haskell.org/ghc/latest/docs/users_guide/exts/existential_quantification.html#existentially-quantified-data-constructors
https://en.wikibooks.org/wiki/Haskell/Existentially_quantified_types

I've tried the following example from the Mercury Language Reference Manual,
section 11.3 Existentially typed data types:


:- typeclass showable(T) where [ func show(T) = string ].

:- type s
    --->    some [T] (s(T) => showable(T)).

:- type list_of_showable == list(s).

:- instance showable(int)    where [ show(I) = string.string(I) ].
:- instance showable(string) where [ show(S) = S ].

:- func hetlist = list_of_showable.
hetlist = [ s(123), s("bla") ].         % <-- line 63


But this doesn't work. hetlist can't be defined. The compiler says:

...
exist.m:063:   in argument 1 of functor `s/1':
exist.m:063:   type error in unification of argument
exist.m:063:   and constant `123'.
exist.m:063:   argument has type
exist.m:063:     some [T] (T),
exist.m:063:   constant `123' has type
exist.m:063:     int.
...

What's wrong? How should this be done?

TIA,
Volker
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://lists.mercurylang.org/archives/users/attachments/20220930/3535bd1f/attachment.sig>


More information about the users mailing list