[m-dev.] for review: tuples [1]

Simon Taylor stayl at cs.mu.OZ.AU
Mon Aug 14 14:28:53 AEST 2000


Fergus wrote:
> For example, here's how I might implement an instance of `ord' for
> tuples:
> 
>       :- func tuple_compare(T, T, comparison_result) <= tuple(T).
>       tuple_compare(X, Y, R) :-
>               tuple_arity(X, N),
>               tuple_compare_2(X, Y, 0, N).
> 
>       tuple_compare_2(X, Y, I, N, R) :-
>               ( I = N ->
>                       R = (=)
>               ;
>                       XI = tuple_arg(X, I),
>                       YI = tuple_arg(Y, I),
>                       compare(XI, YI, R0),
>                       ( R0 = (=) ->
>                               tuple_compare_2(X, Y, I + 1, N, R)
>                       ;
>                               R = R0
>                       )
>               ).

It's a bit more complicated than this. For the code above,
the typechecker won't be able to work out that the type of
XI and YI is an instance of `ord'.

>       :- func construct_tuple(list(univ)) = univ.
>       construct_tuple(ArgUnivs) = TupleUniv :-
>               TupleTypeCtor = type_ctor(type_of({})),
>               ArgTypes = list__map(univ_type, ArgUnivs),
>               TupleType = make_type(TupleTypeCtor, ArgTypes),
>               TupleUniv = construct(TupleType, 0, ArgUnivs).

That won't quite work. `type_ctor(type_of({}))' gets the
type_ctor for `{}/0'. You need a special way to create tuple
type_ctors of any arity without having a term of the type.
The diff I posted contains an implementation of
`construct_tuple/1' written in C.

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