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

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


Sorry, I hit `send' to early...

> > 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.

The way to fix the runtime library is to have the runtime system build up a
table containing all the instances, i.e. all the base_typeclass_infos, similar
to the way we currently have support for build a table containing all the
type_ctor_infos.  Then the code for construct/3 can use the table to check
whether the type actually satisfies the constraint, and if so can construct
a typeclass_info for it.

In the mean time, the only work around really is to not throw away the
typeclass info in the first place.  That means don't use univ -- instead
you can use your own existentially quantified type, e.g.

	:- type my_univ ---> some [T] has_tc(T) => tc(T)
			;    some [T] has_tc2(T) => tc2(T)
			;    some [T] other(T).

-- 
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