Local definitions in the class instances

John Meacham john at repetae.net
Sun Jan 30 11:54:12 CET 2011


On Thu, Jan 27, 2011 at 3:07 AM, Boris Lykah <lykahb at gmail.com> wrote:
> I think it would be convenient to allow adding variables and
> functions, which are not members of the class,  to a class instance so
> that they are visible only in the instance scope. It will help if the
> same functions are used by several class functions.
> ....
>> newtype Wrapped = Wrapped Integer deriving (Show, Eq)
>
>> instance Num Wrapped where
>>   (+) = lift2 (+)
>>   (-) = lift2 (-)
>>   (*) = lift2 (*)
>>   abs = lift abs
>>   signum = lift signum
>>   fromInteger = Wrapped
>>   lift2 f (Wrapped a) (Wrapped b) = Wrapped (f a b)
>>   lift f (Wrapped a) = Wrapped (f a)

Is this meant to mean anything different than

>instance Num Wrapped where
>  (+) = lift2 (+)
>  (-) = lift2 (-)
>  (*) = lift2 (*)
>  abs = lift abs
>  signum = lift signum
>  fromInteger = Wrapped
>
>lift2 f (Wrapped a) (Wrapped b) = Wrapped (f a b)
>lift f (Wrapped a) = Wrapped (f a)

where lift2 and lift are not exported? I think the following syntax
would make more sense.

>instance Num Wrapped where
>    (+) = lift2 (+)
>    (-) = lift2 (-)
>    (*) = lift2 (*)
>    abs = lift abs
>    signum = lift signum
>    fromInteger = Wrapped
>  where
>    lift2 f (Wrapped a) (Wrapped b) = Wrapped (f a b)
>    lift f (Wrapped a) = Wrapped (f a)

so 'where' indroduces the local instance scope.

    John



More information about the Haskell-prime mailing list