[Haskell-cafe] point-free ADT pattern matching ?

Mike Dillon mike at embody.org
Fri Jul 16 00:39:15 EDT 2010


begin Mike Dillon quotation:
> begin Vo Minh Thu quotation:
> > I guess it is short because you make use of second... so you can
> > define second' for your B data type, or make B an instance of Arrow.
> 
> I don't think that's the case. The code for "f" is making use of the
> Arrow instance for (->):
> 
>     second          :: Arrow a => a b c -> a (d, b) (d, c)
>     (str ++)        :: [Char] -> [Char]
>     second (str ++) :: (d, [Char]) -> (d, [Char])
> 
> All the caller can control here is what sort of "d" is passed through
> unchanged, not the fact that the resulting function expects a pair and
> returns a pair.

BTW, I was only addressing whether making B an instance of Arrow would
help somehow. Creating a second' function could indeed help, provided it
had the right signature:

    second' :: (String -> String) -> B -> B
    second' f (B i s) = B i (f s)

Then you can indeed write "g" like so:

    g :: String -> B -> B
    g = second' . (++)

Also, I'm pretty sure it isn't possible to write an instance of Arrow
for B in principle because the type constructor has the kind "*" and the
Arrow class has functions whose types include "Arrow a => a b c",
implying that instances of arrow have kind "* -> * -> *". In fact, GHC
will tell you as much even without trying to give a body to "instance
Arrow B":

    Kind mis-match
    Expected kind `* -> * -> *', but `B' has kind `*'
    In the instance declaration for `Arrow B'

-md


More information about the Haskell-Cafe mailing list