[mercury-users] Dynamic typecasts to polymorphic types

Ralph Becket rbeck at microsoft.com
Thu Sep 21 01:18:38 AEDT 2000


I has looking at the pprint code and thought it would be nice if the
generic to_doc/2 function spotted lists and generated [x, y, z] style
output rather than '.'(x, '.'(y, '.'(z, []))) as it currently does.

My attempt at this went along the following lines:

:- module foo.
:- interface.
:- import_module io.
:- pred main(io__state::di, io__state::uo) is det.
:- implementation.
:- import_module pprint, std_util, list.

main -->
        pprint__write(40, my_to_doc([1, 2, 3, 4])), nl,
        pprint__write(40, my_to_doc([a, b, c, d])), nl.


:- func my_to_doc(T) = doc.

my_to_doc(X) = ( if cast(X, Xs) then to_list_doc(Xs) else to_doc(X) ).


:- pred cast(X::in, Y::out) is semidet.

cast(X, Y) :- univ_to_type(univ(X), Y).


:- func to_list_doc(list(T)) = doc.

to_list_doc(Xs) = brackets(separated(to_doc, group(comma_space_line), Xs)).


This results in a warning:

foo.m:013: In function `foo:my_to_doc/1':
foo.m:013:   warning: unresolved polymorphism.
foo.m:013:   The variable with an unbound type was:
foo.m:013:       Xs :: (list:list(T))
foo.m:013:   The unbound type variable(s) will be implicitly
foo.m:013:   bound to the builtin type `void'.

and to_list_doc/1 never gets called.  If I change its signature to

:- func to_list_doc(list(int)) = doc.

then at least [1, 2, 3, 4] gets printed as required.  The problem is
obviously that univ_to_type(X, Y) only works if the type of Y is
identical to that of X, rather than just subsuming it.  Is this a
bug?  If not, I suspect we need another predicate that does as
required.

Ralph

--
Ralph Becket      |      MSR Cambridge      |      rbeck at microsoft.com 
--------------------------------------------------------------------------
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