[mercury-users] "some" and existential types
Fergus Henderson
fjh at cs.mu.OZ.AU
Sun Oct 3 04:42:36 AEST 1999
On 02-Oct-1999, Ralph Becket <rbeck at microsoft.com> wrote:
>
> :- pred give_me_a_value(list(anything)).
...
> :- type anything ---> some [T] thing(T) => printable(T).
>
> means you can do things like
>
> foo :-
> give_me_a_value(Xs),
> print_anythings(Xs).
>
> print_anythings([]) --> [].
> print_anythings([thing(X) | Xs]) --> print(X), print_anythings(Xs).
>
> The things in the list can all be different, but because of the typeclass
> constraint give_me_a_value/1 promises that they will all have a print//1
> method defined for them.
>
> QUESTION FOR THE MERCURY SQUAD: can I write something like the following,
>
> print_anythings(Xs) --> list__foldl(print, Xs).
>
> I suspect not, because the print//1 method may be different in each case.
> However, to disallow this sort of code would be a great shame, IMHO. Not
> sure what the solution is, assuming this is the case.
There's an easy solution here: just declare `anything' to be an
instance of `printable':
:- instance printable(anything) where [
pred(print/3) is print_anything
].
:- pred print_anything(anything::in, io__state::di, io__state::uo)
is det.
print_anything(thing(X)) --> print(X).
Then you can write `print_anythings(Xs) --> list__foldl(print, Xs)'
and it should work just fine.
--
Fergus Henderson <fjh at cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3 | -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-users mailing list
post: mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe: Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------
More information about the users
mailing list