[Haskell-cafe] Re: How to "Show" an Operation?

Martin Drautzburg Martin.Drautzburg at web.de
Thu Jun 10 17:38:15 EDT 2010


On Thursday, 10. June 2010 22:10:08 Maciej Piechotka wrote:

Wow!

this is somewhat above my level. I guess I need to go back to the books. I'll 
document my ignorance nontheless.

> data Named a = Named String a
>
> instance Functor Named where
>     f `fmap` (Named s v) = Named s (f v)
>
okay so far

> instance Applicative Named where
>     pure x = Named "" x
>     (Named s f) <*> (Named t v) = Named (s ++ "(" ++ t ++ ")") (f v)

Applicative. Need to study that
Control.Applicative	(<*>) :: Applicative f => f (a -> b) -> f a -> f b

So in our case the Applicative is a "Named". When I apply a Named to a 
function, then I get a function between the corresponding Named types. When I 
pass it an Int->Char function, I get a Named Int -> Named Char function.

But here it is applied to another Named ... is that the (a->b)? Puzzeled.

> instance Eq a => Eq (Named a) where
>     (Named _ x) == (Named _ y) = x == y
>
> instance Show (Named a) where
>     show (Named s _) = s
>

Understood.

> namedPure :: Show a => a -> Named a
> namedPure x = Named (show x) x

When I can show something I can always name it so its name is what 'show' 
would return. Okay I guess I got it. This turns a "showable" into a Named.

>
> test :: Num a
>      => (a -> a) -> (a -> a) -> (a -> a) -> [String]
> test f g h = do
>     [f', g', h'] <- permutations [Named "f" f, Named "g" g, Named "h" h]

According to Hoogle permutations should be in Data.List. Mine (GHCI 6.8.2) 
does not seem to have it. Seems to have something to do with "base", whatever 
that is.

>     guard $ namedPure 42 == f' <*> g' <*> h' <*> namedPure 42

Ah, the 42 needs namedPure.
Again this <*> operator... 
I believe the whole thing is using a List Monad. 

>     return $ show f' ++ " . " ++ show g' ++ " . " ++ show h'

I wonder if the thing returns just one string or a list of strings. I 
guess "return" cannot return anything more unwrapped than a List, so it must 
be a List. But does it contain just the first match or all of them? All of 
them! And how many brackets are around them?

-- 
Martin


More information about the Haskell-Cafe mailing list