[mercury-users] Mercury existential types / foreign code question

Peter Hawkins peter at hawkins.emu.id.au
Mon Aug 1 17:53:31 AEST 2005


Why doesn't this compile?
:- module test3.
:- interface.
:- import_module io.
:- pred main(io.state::di, io.state::uo) is det.
:- implementation.
:- import_module list.
:- import_module int.

    % A type
:- type cvar(T) ---> cvar(v :: T, x :: list(int)).
:- inst cvar ---> cvar(ground, non_empty_list).

    % A typeclass.
:- typeclass cvar_type(T) where [].
:- instance cvar_type(int) where [].

    % Existential wrapper type
:- type cvar_wrapper ---> some [T] (
    cvar_wrapper(cvar :: cvar(T)) => cvar_type(T)

    % Wrap and unwrap predicates
:- func wrap_cvar(cvar(T)) = cvar_wrapper <= (cvar_type(T)).
:- some [T] func unwrap_cvar(cvar_wrapper::in) = (cvar(T)::out(cvar)) is det
    => (cvar_type(T)).

:- func unsafe_any_to_cvar(cvar(T)::ia) = (cvar(T)::out(cvar)) is det.

:- pragma foreign_proc("C", unsafe_any_to_cvar(X::ia) = (Y::out(cvar)),
    [will_not_call_mercury, promise_pure], "Y = X;").

wrap_cvar(V) = 'new cvar_wrapper'(V).
unwrap_cvar(cvar_wrapper(V)) = unsafe_any_to_cvar(V).

main(!IO) :- io.print("Hello!\n", !IO).

I get this error:
test3.m:037: In clause for `unwrap_cvar(in) = out((test3.cvar))':
test3.m:037:   in argument 1 of call to predicate 
test3.m:037:   mode error: variable `TypeClassInfo_for_cvar_type' has 
instantiatedness `free',
test3.m:037:   expected instantiatedness was `ground'.
For more information, try recompiling with `-E'.

Any suggestions as to how to fix it? I can't see why this doesn't build, 
since unsafe_any_to_cvar is universally quantified, so the typeclass 
info should be communicated across.

In general what happens when you pass a polymorphic type to a 
foreign_proc? How can you access the typeinfo?)

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