Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dispatches to ap method of monad if available #3

Closed
wants to merge 2 commits into from

Conversation

rjmk
Copy link

@rjmk rjmk commented Mar 31, 2016

I don't know if you would prefer to also have chain rewritten to dispatch when available?

@paparga
Copy link
Contributor

paparga commented Mar 31, 2016

I test it, but it doesn't seem to work (fixing a small typo).

return this.x.ap ? this.x.ap(a) : this.chain(f => a.map(f))

And changing package.json to:
"data.task": "safareli/data.task.git#patch-1"

My code:

const {Free, liftF} = require("./free")
const Task = require("data.task")
const daggy = require("daggy")

const Shout = daggy.tagged("text", "ms")

const shout = (text, ms) => liftF(Shout(text, ms))

const par3 = x => y => z => [x, y, z]

const app = shout("Last", 1000).map(par3).ap(shout("Second", 500)).ap(shout("First", 100))

const shoutToTask = ({text, ms}) => new Task((rej, res) => {
  setTimeout(()=>{
    console.log(text + "!!!!")
    res("ok")
  },ms)
})

console.log("----- Using Free -----");

app.foldMap(shoutToTask, Task.of).fork(console.log,console.log)

// Now using Task directly

const shout2 = (text, ms) => new Task((rej, res) => {
  setTimeout(()=>{
    console.log(text + "!!!!")
    res("ok")
  },ms)
})

const app2 = shout2("Last", 1000).map(par3).ap(shout2("Second", 500)).ap(shout2("First", 100))

setTimeout(()=>{
  console.log("")
  console.log("----- Using Task directly-----")
  app2.fork(console.log,console.log)
}, 2500)

And this is the output:
image

@DrBoolean
Copy link
Owner

Shoot yeah, I wasn't thinking earlier. this in the context of that method was Free itself - not the containing value.

I think the implementation is just:

Free.prototype.ap = function(a) {
  return this.cata({
    Impure: (x, g) => Impure(x, y => g(y).ap(a)),
    Pure: f => a.map(f)
  })
}

However, this doesn't quite work.

I think the order we're witnessing is due to the nature of Free Monads being run all the way through first, then interpreted later. We might have to use Free Applicatives here...I have to research a bit.

@DrBoolean
Copy link
Owner

I should say, it does "work", but the order is backwards as you've noticed.

@rjmk
Copy link
Author

rjmk commented Apr 1, 2016

I think this is the specific monad. But yeah, the issue (I think) is here, where when foldMapping chain is called, so in the interpreting. I don't know whether it would also be possible to expose an interface for interpreting with aps

@safareli
Copy link
Contributor

Here is Free Applicative from fantasy-free if that's helpfull.

@safareli
Copy link
Contributor

@DrBoolean if you have some good resources about this topic it will be nice if you add it to README

@rjmk
Copy link
Author

rjmk commented Apr 20, 2016

Superceded by #5

@rjmk rjmk closed this Apr 20, 2016
@safareli
Copy link
Contributor

safareli commented Jun 25, 2016

Hi guys I have ported free-concurrent written by @srijs which is combination of free applicative functor and free monad.
with this implementation test case provided by @paparga works properly.

@DrBoolean
Copy link
Owner

Perfect!

@paparga
Copy link
Contributor

paparga commented Jun 25, 2016

Great!

@safareli
Copy link
Contributor

safareli commented Jul 11, 2016

Could you guys check if my test case for checking concurrency is correct?

@rjmk
Copy link
Author

rjmk commented Jul 11, 2016

Looks good to me, @safareli

@safareli
Copy link
Contributor

Thanks!

@safareli
Copy link
Contributor

As it turns out when structure is monad applicative instance should behave as if it's derived from monad, so making free concurrent when ap is used is not lawful.

I was thinking on that issue and camte to this and I would love to hear your thoughts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants