[Haskell-cafe] Systematic treatment of static arguments

Erik Hesselink hesselink at gmail.com
Sun Oct 17 05:12:45 EDT 2010

On Sat, Oct 16, 2010 at 21:39, Stephen Tetley <stephen.tetley at gmail.com> wrote:
> Hello list
> The Monad and Applicative instances for functions are "equivalent" to
> the respective Reader vesions (I use "equivalent" along the lines of -
> operationally the same but without the type distinction / newtype).
> There is also the Monoid instance for functions which is pretty slick.
> Has anyone looked at variations with two or more static arguments though?
> For example mappend with two static arguments needs to be defined
> either as a new function:
>> mappendR2 :: Monoid a => (r1 -> r2 -> a) -> (r1 -> r2 -> a) -> r1 -> r2 -> a
>> mappendR2 f g = \x y -> f x y `mappend` g x y
> or an overlapping instance:
>> instance Monoid a => OPlus (r1 -> r2 -> a) where
>>   f `mappend` g = \x y -> f x y `mappend` g x y

I think you can just use the original instance for Monoid for this. It declares

instance Monoid b => Monoid a -> b

since 'b' can itself be a function (and r1 -> r2 -> a is parenthesized
as r1 -> (r2 -> a)), this will work. 'b' can be instantiated to (r2 ->
a) in your case, which is a Monoid due to another instance for
functions, this time with 'b' instantiated to 'a'.

This doesn't work for Reader, though. There, you're probably easier
off using just a tuple, or, even better, a record, where you can use
the 'asks' function to extract a single component.


More information about the Haskell-Cafe mailing list