List Comprehensions?
My first thought was something with list comprehensions... (Note; some of this is pseudo-code-y... those parts are italicized)
pixelTransform img transformFunction = do
(width,height) <- imageSize img
[ do
get pixel x y
transformFunction pixel
set pixel to new value
| x <- [ 0..(imageWidth-1) ], y <- [ 0..(imageHeight-1) ]]
Makes sense, right? For every combination of x and y, do the series of IO actions required to transform it. Turns out Haskell isn't a fan of IO inside of list comprehensions; you might get it to compile okay (if you spend time wrapping "return" calls around things), but nothing will happen.
'do' Style?
After some research, I found that Haskell supports a
do
x <- [ 0..10 ]
y <- [ 0..10 ]
return (x+y)
Cool Right? that will give us back a list of the sums of all combinations of x and y. If that works, then I should be able to do this, right?
pixelTransform img transformFunction = do
(width,height) <- imageSize img
x <- [ 0..(width-1) ]
y <- [ 0..(height-1) ]
get pixel x y
transformFunction pixel
set pixel to new value
return ()
Well, no. This doesn't fly either. And when we think about it, it makes sense. 'do' notation is syntactic sugar for constructing monadic actions together with the >>= (bind) operator. The above is the same as doing:
pixelTransform img transformFunction = return () >>=
imageSize img >>=
(\(width,height) -> [0..(width-1)] >>=
\x -> [0..(height-1) >>=
\y -> get pixel x y >>=
\pixel -> transformFunction pixel >>=
\new value -> set pixel to new value
That IS kind of ugly looking, but it doesn't show what the problem *really* is until we look at the type signature for >>=
ghci>:t ( >>= )
( >>= ) :: Monad m => m a -> (a -> m b) -> m b
Ah. So >>= takes a monad with some type a, a function which takes regular type a and returns a monad with some type b, and then applies that function to the monad with type a, which ends up returning a monad with type b.
Remember: Just because the input is m a, and the output is m b, it does not mean that they are different monads; the m is still there in both of them... it is the same monadic type, but it *may* contain a different type inside of it (if you think of the box analogy - a monad is a box which contains other values).
If you think about what our previous example is doing, you'll realize that the big issue is that we're trying to use completely different monads together. Applying an IO monad's result to a List monad doesn't make any sense (and vice versa)! So, in order to use multiple Monadic actions together within a do, they either need to be the same type, or they need to be wrapped in calls to return to put them into the right monadic context, but that gets sort of messy. So, what's the easiest way to do what I'm trying to do?
mapM_ saves the day
mapM_ is a function that lives inside the Control.Monad. It's kind of like the regular map that we all know and love, but it's meant for use with monads. About the name of the function - the 'M' stands for the fact that it takes a regular old type value and returns a monad, and the '_' just means that instead of returning m something, it is going to return m (). In our case, that means that the return type of mapM_ is going to be IO ().
Using map makes sense; we know the dimensions of the image (by grabbing the image size), so we can build a list of all combinations of x and y values between 0 and their respective sizes, and then map our transform function over each pixel
So now, the per-pixel transform function becomes this:
import Control.Monad (mapM_)
pixelTransform img transformFunction = do
(width,height) <- imageSize img
mapM_ (\(x,y) -> do
get pixel x y
transformFunction pixel
set pixel to new value
)
[ (x,y) | x <- [0..(width-1)], y <- [0..(height-1)] ]
Easy enough; the list comprehension at the end builds a list of tuples representing every possible x,y coordinate in the image and that is the list that we mapM_ over. The function we pass to mapM_ pattern matches the tuple, and then performs our pixel transformation there, then moves on.
As you can see, we used a list comprehension to build the list of x,y pairs. If you are a fan of Applicative Functors (like I am!), you can use the <$> and <*> operators to build the same list to pass to mapM_
import Control.Monad (mapM_)
import Control.Applicative
pixelTransform img transformFunction = do
(width,height) <- imageSize img
mapM_ (\(x,y) -> do
get pixel x y
transformFunction pixel
set pixel to new value
)
$ (,) <$> [0..(width-1)] <*> [0..(height-1)]
Wrap up
So far, mapM_ has been the best solution that I've found for looping over multidimensional list values with IO () actions, but if anybody else has found other/better/clearer ways of achieving the same results, I'd love to hear them
Update: I've found out that the reason why list comprehensions don't work is the exact same reason why do notation doesn't work; list comprehensions are syntactic sugar for using >>=, so it has the same requirements as a do block, in terms of Monad consistency
No comments:
Post a Comment