[Haskell-cafe] Fwd: Re: 123

Mario Blažević mblazevic at stilo.com
Tue Aug 22 21:50:01 UTC 2017


On 2017-08-16 04:54 PM, Dmitriy Matrosov wrote:
> And even sent the answer not to the list.. Huh.

I'm not sure I fully undestand your use case, but your examples can be 
handled by the rank2classes package 
(http://hackage.haskell.org/package/rank2classes).


> I want to have a type with many records:
> 
>> data Volume t     = Volume
>>                       { _volName  :: t String
>>                       , _volSize  :: t Int
>>                       }
>>
>> showVolume :: (Show (t String), Show (t Int)) => Volume t -> String
>> showVolume x      = "Volume " ++ show (_volName x) ++ ", " ++ show 
> (_volSize x)
> 
> with instances parametrized by some other type. E.g. i want to define a
> `Monoid` based on that other type properties:
> 
>> instance Alternative t => Monoid (Volume t) where
>>   mempty          = Volume {_volName = empty, _volSize = empty}
>>   x `mappend` y   = Volume
>>                       { _volName = _volName x <|> _volName y
>>                       , _volSize = _volSize x <|> _volSize y
>>                       }


instance Rank2.Apply Volume where
    x <*> y   = Volume
                    { _volName = _volName x `Rank2.apply` _volName y
                    , _volSize = _volSize x `Rank2.apply` _volSize y
                    }

instance Rank2.Applicative Volume where
    pure x = Volume {_volName = x, _volSize = x}

instance Alternative t => Monoid (Volume t) where
   mempty          = Rank2.pure empty
   x `mappend` y   = Rank2.liftA2 (<|>) x y



More information about the Haskell-Cafe mailing list