[clean-list] Parametrizing instance declarations
John van Groningen
johnvg at cs.ru.nl
Wed Sep 26 11:58:13 MEST 2007
David Noris wrote:
>How might one define the following three class instances without duplicating code? My attempts to parametrize the declaration fail with errors such as
>
>(1) type variable of type of lifted argument b appear in the specified type
>(2) internal overloading of "_createArray" could not be solved.
>
>----------------------------------------------
>
>class copyElt e :: {#e} Int -> e
>
>instance copyElt Char
>where
> copyElt a i = c.[0]
> where
> c :: {#Char}
> c = {a.[i]}
>
>instance copyElt Int
>where
> copyElt a i = c.[0]
> where
> c :: {#Int}
> c = {a.[i]}
>
>instance copyElt Real
>where
> copyElt a i = c.[0]
> where
> c :: {#Real}
> c = {a.[i]}
You could use a macro (or function) and use it in each instance declaration:
copyEltCharIntOrReal a i :== (c a).[0]
where
c :: {#e} -> {#e} | Array {#} e
c a = {a.[i]}
instance copyElt Char where
copyElt a i = copyEltCharIntOrReal a i
instance copyElt Int where
copyElt a i = copyEltCharIntOrReal a i
instance copyElt Real where
copyElt a i = copyEltCharIntOrReal a i
Unfortunately this doesn't work:
copyEltCharIntOrReal a i :== c.[0]
where
c = {a.[i]}
because the compiler cannot determine what kind of array ({},{!} or {#})
has to be created. Therefore the type of c is specified. The argument a
is added to c, because the type c depends on a.
In future releases of Clean it will be possible to use:
copyEltCharIntOrReal a i :== c.[0]
where
c = {#a.[i]}
to indicate that an unboxed array has to be created.
Kind regards,
John van Groningen
More information about the clean-list
mailing list