Call IO Monad Inside Arrow

I may have done the wrong thing, but I'm using HXT to read in some vertex data that I would like to use in an array in HOpenGL. Vertex arrays must be Ptr, which is created when newArray is called. Unfortunately, newArray returns IO Ptr, so I'm not sure how to use it inside Arrow. I think I need something with a type declaration similar to IO a → Arrow a?

+6
source share
1 answer

Type IO a -> Arrow a does not make sense; Arrow is a type of type, not a specific type, like Monad or Num . Specifically, an Arrow instance is a type constructor that takes two parameters that describe things that can be arranged as functions, matching types from end to end. Thus, the conversion of IO a into an arrow could be called a conceptual type error.

I'm not sure exactly what you are trying to do, but if you really want to use IO operations as part of Arrow , you will need your Arrow instance for this. The simplest form is to observe that functions with types such as a -> mb for any Monad instance can be constructed in an obvious way. The hxt package hxt be more complex:

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

This is some mixture of the monads IO , State and [] attached to the function, as described above, so that you can compose them through all three Monad at each step. I really haven't used hxt lot, but if it's the Arrow you work with, it's pretty simple to raise an arbitrary IO function to serve one - just pass the state value s unchanged and turn the function output into a singleton list. You may already have a function to do this for you, but I did not notice it with a brief glance.

Basically, you need something like this:

 liftArrIO :: (a -> IO b) -> IOSLA sab liftArrIO f = IOSLA $ \sx -> fmap (\y -> (s, [y])) (fx) 
+5
source

Source: https://habr.com/ru/post/896256/


All Articles