Calling an IO Monad inside an Arrow
Asked Answered
T

1

6

Perhaps I'm going about this the wrong way, but I'm using HXT to read in some vertex data that I'd like to use in an array in HOpenGL. Vertex arrays need to be a Ptr which is created by calling newArray. Unfortunately newArray returns an IO Ptr, so I'm not sure how to go about using it inside an Arrow. I think I need something with a type declaration similar to IO a -> Arrow a?

Toback answered 30/8, 2011 at 21:49 Comment(0)
V
5

The type IO a -> Arrow a doesn't make sense; Arrow is a type class, not a specific type, much like Monad or Num. Specifically, an instance of Arrow is a type constructor taking two parameters that describes things that can be composed like functions, matching types end-to-end. So, converting IO a to an arrow could perhaps be called a conceptual type error.

I'm not sure exactly what you're trying to do, but if you really want to be using IO operations as part of an Arrow, you need your Arrow instance to include that. The simplest form of that is to observe that functions with types like a -> m b for any Monad instance can be composed in the obvious way. The hxt package seems to provide a more complicated type:

newtype IOSLA s a b = IOSLA { runIOSLA :: s -> a -> IO (s, [b]) }

This is some mixture of the IO, State, and [] monads, attached to a function as above such that you can compose them going through all three Monads at each step. I haven't really used hxt much, but if these are the Arrows you're working with, it's pretty simple to lift an arbitrary IO function to serve as one--just pass the state value s through unchanged, and turn the output of the function into a singleton list. There may already be a function to do this for you, but I didn't see one at a brief glance.

Basically, you'd want something like this:

liftArrIO :: (a -> IO b) -> IOSLA s a b
liftArrIO f = IOSLA $ \s x -> fmap (\y -> (s, [y])) (f x)
Venue answered 30/8, 2011 at 22:5 Comment(3)
arrIO seems to be the one he's looking for. Its implementation is basically identical to yours.Violetteviolin
@hammar: Aha! Thanks, for some reason I assumed it would have "lift" in the name.Venue
Ahhh yes, that's it precisely! Every time I think I've got arrows and monads figured out I wind up figuring them out all over again...Toback

© 2022 - 2024 — McMap. All rights reserved.