Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does folktale have an IO monad?

I've been exploring the folktale library and found a wealth of useful constructs. After using Tasks via control.async and data.task, I wanted to use an IO monad, but can't seem to find it. Given how rich folktale is, I am surprised and wondering whether I just am not seeing it.

Is there an IO monad in folktale?

like image 247
foxdonut Avatar asked Mar 15 '23 05:03

foxdonut


1 Answers

In Haskell, the IO monad is provided by (and inherently bound to), the runtime. Folktale does not provide functional equivalents for runtime functions but otherwise Task and IO serve the same purpose. An IO action in Haskell can be asynchronous, so we can say that it is even more similar to Haskell's IO than, for example, the IO monad in monet.js.

One difference is that Task provides error handling, while the IO monad doesn't.

You can program using Tasks in JS in the same manner that you program in Haskell using IO actions. You just need to define all impure runtime functions that you use using Tasks.

For example, take the function print, (print :: Show a => a -> IO ()) provided by the Haskell runtime which just prints its input and returns nothing. We can write a similar function in JS, using tasks. It will probably look something like this.

// Definition
const print = (input) => Task.task(r => {
    console.log(String(input))
    r.resolve(undefined)
})

// Usage
const main = Task.of("Hello world").chain(print)
like image 90
Boris Marinov Avatar answered Mar 25 '23 08:03

Boris Marinov