Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Can`t realize ap composition #46

Open
Ne4to777 opened this issue Dec 19, 2019 · 3 comments
Open

Can`t realize ap composition #46

Ne4to777 opened this issue Dec 19, 2019 · 3 comments

Comments

@Ne4to777
Copy link

Ne4to777 commented Dec 19, 2019

Fantasy Land spec:
v.ap(u.ap(a.map(f => g => x => f(g(x))))) is equivalent to v.ap(u).ap(a) (composition)

Implementation:

const v = Task.of(f => g => x => f(g(x)))
const u = Task.of(x => x * 3)
const a = Task.of(x => x + 3)

v
.ap(u
.ap(a
.map(f => g => x => f(g(x)))))
.fork(console.error, console.log)

Log: g => x => f(g(x))

v
.ap(u)
.ap(a)
.fork(console.error, console.log)

Log: x => f(g(x))

@robotlolita
Copy link
Member

The composition rule states:

v ap (u ap (a map \f. \g. \x. f (g x)))  <==>  (v ap u) ap a

But this was changed at some point. The old specification (which is what this library implements) instead stated:

((a map \f. \g. \x. f (g x)) ap u) ap v  <==>  a ap (u ap v)

So v is the value you want to apply (what goes into x), u and a are respectively g and f. Thus, you need:

const v = Task.of(3);
const u = Task.of(x => x * 3)
const a = Task.of(x => x + 3)

With that, you should have the equivalence:

a.ap(u.ap(v))  <==>  a.map(f => g => x => f(g(x))).ap(u).ap(v)

Here's the relevant discussion about the change: fantasyland/fantasy-land#50

Note that this library is not maintained anymore. The new version implements Fantasy Land 2.x specification (https://folktale.origamitower.com/api/v2.3.0/en/-unknown-module-.folktale.concurrency.future._future.fantasy-land-ap.html)

@Ne4to777
Copy link
Author

Hmm. Actual specification: https://github.com/fantasyland/fantasy-land#apply

@robotlolita
Copy link
Member

Yes, that's the version 4 of the Fantasy Land specification.

This library does not implement version 4. It implements version 1. In version 1, the arguments to ap were flipped.

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

No branches or pull requests

2 participants