
On Thursday 10 June 2010 23:38:15, Martin Drautzburg wrote:
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".
Here, we define (<*>) for the type (<*>) :: (Named (a -> b)) -> (Named a) -> (Named b) (redundant parentheses against ambiguity errors). A 'Named' thing is a thing together with a name. So how do we apply a function with a name to an argument with a name? What we get is a value with a name. The value is of course the function applied to the argument ignoring names. The name of the result is the textual representation of the function application, e.g. Named "sin" sin <*> Named "pi" pi ~> Named "sin(pi)" 1.2246063538223773e-16 (<*>) is application of named functions to named values, or 'lifting function application to named things'.
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
Upgrade. We're at 6.12 now! Lots of improvements. permutations was added in 6.10, IIRC.
"base", whatever that is.
guard $ namedPure 42 == f' <*> g' <*> h' <*> namedPure 42
Ah, the 42 needs namedPure.
Simplest way, it could be Named "answer to Life, the Universe and Everything" 42
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
A list, one string for every permutation satisfying the condition.
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?
do x <- list guard (condition x) return (f x) is syntactic sugar for concat (map (\x -> if condition x then [f x] else []) list)