[mercury-users] Existential types and list.map

Fergus Henderson fjh at cs.mu.OZ.AU
Fri Jul 29 12:29:39 AEST 2005


On 29-Jul-2005, Peter Hawkins <peter at hawkins.emu.id.au> wrote:
> Why doesn't this work?
...
> :- some [T] func get_thing(int) = T.
> get_thing(X) = X.
> 
> main(!IO) :-
>    X = [1,2],
>    Y = map(get_thing, X),
>    io.print(Y, !IO),
>    io.nl(!IO),
>    true.
>
> I get the misleading error message:
> $ mmc --make test3
> Making Mercury/int3s/test3.int3
> Making Mercury/cs/test3.c
> test3.m:016: In clause for predicate `test3.main/2':
> test3.m:016:   in argument 1 of functor `map/2':
> test3.m:016:   error: undefined symbol `get_thing/0'.

The only thing you can do with an existentially-typed
function is call it; you can't pass it to other procedures.

(If you were allowed to "take the address of" an existentially
typed function, i.e. use the function name without supplying
any arguments, what type would it have?
Mercury doesn't support first-class polymorphism,
so only a function can have type "some [T] func(int) = T",
not an expression.)

BTW, it should be reasonably easy to modify the type checker so that
it gives a better error message in this case.  You just need to change
report_error_undef_cons to check for this case.

If you do want to pass an existentially-typed function,
the work-around is to use a type class with an existentially
typed method, and pass an instance of the type class
instead of passing the function.  Then instead of using a
higher-order call (or apply/N) to call the function,
you just call the type class method.

-- 
Fergus Henderson                    |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- 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