How to specialize mapM for IO in Haskell

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How to specialize mapM for IO in Haskell



Say I have a task that represents some computation from k to v where some inputs have to be fetched externally.


k


v


newtype Task k v = Task { run ∷ ∀ f. Monad f ⇒ (k → f v) → f v }



For some tasks mapM will be used, e.g. to fetch multiple keys. I want to specialize mapM for some monads. Specifically for the IO monad I want to use Control.Concurrent.Async.mapConcurrently to perform actions concurrently.


mapM


mapM


IO


Control.Concurrent.Async.mapConcurrently



My first instinct is to introduce a wrapper type


newtype AsyncIO a = AsyncIO { io :: IO a }



and then introduce


instance Monad AsyncIO



However this doesn't work because in the current GHC implementation mapM is defined in term of traverse, which is in Traversable.


mapM


traverse


Traversable



Is there an elegant solution for this?





I don't quite understand what you mean by "for some tasks mapM will be used". Could you show an example of such a Task?
– Asad Saeeduddin
25 mins ago




mapM


Task




1 Answer
1



Have run take mapM or mapConcurrently as an additional argument, or for possibly less passing as an implicit parameter, though I'm not sure whether the latter will clash with f's quantification.


mapM


mapConcurrently





Can you post some code for the signature?
– Chris
1 min ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

C# - How to create a semi transparent or blurred backcolor on windows form

Will Oldham

Makefile test if variable is not empty