[mercury-users] typeclasses vs. univ and higher order programming

Fergus Henderson fjh at cs.mu.OZ.AU
Mon Oct 4 20:37:53 AEST 2004


On 04-Oct-2004, obo at ruk.cuni.cz <obo at ruk.cuni.cz> wrote:
> Hello.
> 
> I'd like to create a list (or custom list-like type) of values and 
> operators on them of various types.
> 
> :- type aggrslices --->
>        nil
>      ; some [T] cons(T, aggrslices).
> :- type aggrslice(T) ---> aggrslice(
>      column :: int,
>      val    :: T,
>      folder :: (func(T, T) = T),
>      dumper :: (func(T) = string)
>    ).
> 
> Then, I'd like to a have a simple "configuration" predicate to define 
> handling of those various types.
> 
> :- type aggr --->
>           min
>         ; max
>         ; collect
>         % ...
>         .
> 
> %               identifier, initer, aggregator, dumper
> :- some [T] pred aggregator(
>                    aggr,
>                    (func) = T,
>                    func(string, T) = T,
>                    func(T) = string
>    ).
> :- mode aggregator(in, out, out, out) is det.
> 
> However, I fail to encode the initialization part properly:
> 
>   Aggr = min,
>   InAggrs = nil,
>   aggregator(Aggr, Initer, Folder, Dumper),
>   AggrSlice = aggrslice(Column, Initer, Folder, Dumper),
>   OutAggrs = cons(AggrSlice, InAggrs)

You need "'new cons'" instead of "cons" here, I think, since you are
constructing a term of an existentially quantified type.

>   % ...
> 
> grp.m:343: In clause for function `grp.new_mem/3':
> grp.m:343:   in unification of variable `AggrSlice'
> grp.m:343:   and term `aggrslice(Column, Initer, Folder, Dumper)':
> grp.m:343:   type error in argument(s) of functor `aggrslice/4'.
> grp.m:343:   Argument 2 (Initer) has type `(some [T] ((func) = T))',
> grp.m:343:   expected type was `T';

The fix for this one is to use "apply(Initer)" instead of "Initer".
Nullary functions in Mercury need to be explicitly applied.

> grp.m:343:   argument 3 (Folder) has type `(some [T] (func(string, T) = 
> T))',
> grp.m:343:   expected type was `(func(T, T) = T)';

This one is pointing out the inconsistency in the type of the function's
first argument: string or T?  You need to change either the type declaration
for the third argument of aggregator, or the type declaration for the
folder field of the aggrslice(T) type.

> grp.m:343:   argument 4 (Dumper) has type `(some [T] ((func T) = string))',
> grp.m:343:   expected type was `((func T) = string)'.

That one is a red herring, I think -- it should go away once the other
problems mentioned above are fixed.

-- 
Fergus Henderson                    |  "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-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