[mercury-users] Is there a better way to write is_list/1?

Ralph Becket rbeck at microsoft.com
Tue May 23 22:04:10 AEST 2000


Working on the pretty printer, I thought it'd be good to have special
treatment for lists in the generic term typesetter.  My first attempt 
at writing is_list/1 didn't work:


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

main -->
    ( if
        { univ_to_type(univ([1, 2, 3]), List) },
        { is_list(List) }
      then
        io__print("yes\n")
      else
        io__print("no\n")
    ).

:- pred is_list(list(T)).
:- mode is_list(in) is det.

is_list(_).


This doesn't work because list(T) \= list(int) [this may be one for the
FAQ].  My working solution isn't very elegant:


:- pred is_list_2(T).
:- mode is_list_2(in) is semidet.

is_list_2(X) :-
    type_ctor_and_args(type_of(X),  ListTypeCtor, _),
    type_ctor_and_args(type_of([]), ListTypeCtor, _).


I feel there ought to be a better way of doing this.  Any ideas?

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