[m-dev.] Does dynamic_cast/2 preserve typeclass info?

Fergus Henderson fjh at cs.mu.OZ.AU
Fri Mar 2 04:42:37 AEDT 2001


On 01-Mar-2001, Ralph Becket <rbeck at microsoft.com> wrote:
> If I have
> 
> 	func f(t(T)) = u <= tc(T)
> 
> and do
> 
> 	if dynamic_cast(X, Y) then f(Y) else ...
> 
> should I expect this to work if X is an existentially quantified type
> that may indeed be a t(T) <= tc(T)?

No.  Unless there is some other use of `Y', the type of `Y'
will be 

	Y :: t(T) <= tc(T)

which is not a ground type.  The compiler will issue a warning
about the type variable `T' not being instantiated and/or
an error about the constraint `tc(T)' not being satisfied.

> Any ideas how I might work around this one?

We already have a syntax for it: instead of

	g(X) = (if dynamic_cast(X, Y) then f(Y) else ...).

you can write

	:- type foo ---> some [T] foo(t(T)) => tc(T).

	g(X) = (if simple_construct(X, foo(Y)) then f(Y) else ...).

        % This should go in the standard library...
        :- func simple_construct(ArgT) = T is semidet.
        simple_construct(Object) = Result :-
                   univ_to_type(construct(type_of(Result), 0, [univ(Object)]),
                           Result).

That will compile, and has the declarative semantics that you want.
Unfortunately in the current implementation it doesn't have the operational
semantics that you want: construct/3 will abort with a runtime error if you
give it an existentially quantified type with a type class constraint.

For more discussions on this, try searching the mailing list archives for
"dynamic type class cast".

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "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-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list