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

monad law (<*>) = ap is ignored? #58

Closed
safareli opened this issue Oct 4, 2016 · 5 comments
Closed

monad law (<*>) = ap is ignored? #58

safareli opened this issue Oct 4, 2016 · 5 comments

Comments

@safareli
Copy link

safareli commented Oct 4, 2016

I have taken look at some talks of @simonmar about haxl:

  1. Keynote from Simon Marlow - Fun with Haxl
  2. The Haskell Cast #4 - Simon Marlow on Parallelism and Concurrency

And looks like Haxl is not lawful as it's ignoring law (<*>) = ap.
also as (>>) = (*>) is implied by that law it causes issues like one in first link (1.) about sequencing monadic actions in do even when applicative do was off:

run $ do
  cmd "touch" ["foo"]
  cmd "rm" ["foo"]

And there was need to add _ <- in order that to be sequential:

run $ do
  _ <- cmd "touch" ["foo"]
  cmd "rm" ["foo"]

My question are:

  • Is it intentional to ignore that law?
  • Can Haxl be lawful and also produce same benefits?
  • How often is it ignored in haskell libraries?
  • What are benefits of that law?

Also I have taken look at the original paper about Applicative and did get reasons why this law was intorduced, so if you know any discussion on that topic you can link would be great!

Also in js community some implementations of Futures also Ignore that law (fantasy-land#179), looks like this law is not that widely known.

@simonmar
Copy link
Contributor

simonmar commented Oct 4, 2016

The law holds provided (a) you only look at the results of runHaxl and (b) your data sources satisfy the property that fetching in parallel gives the same result as fetching sequentially.

So Haxl stretches the law a bit, but doesn't break it.

@simonmar simonmar closed this as completed Oct 4, 2016
@safareli
Copy link
Author

safareli commented Oct 4, 2016

I have updated code snippets to match ones from your slides and in that example (when performing side effective actions) it ignores that law. Is it common to obey laws for partial input like this in haskell?

@simonmar
Copy link
Contributor

simonmar commented Oct 5, 2016

Is it common to obey laws for partial input like this in haskell?

I don't fully understand the question. Perhaps if you give me a specific example of what you mean by partial input?

@safareli
Copy link
Author

safareli commented Oct 5, 2016

You said that Haxl is lawful if "your data sources satisfy the property that fetching in parallel gives the same result" so it's not lawful for all other possible use cases of Haxl(is partially lawful), for example for this fragment of Haxl it's not lawful as 1 and 2 have different results:

run $ do -- 1
  cmd "touch" ["foo"]
  cmd "rm" ["foo"]

run $ do -- 2
  _ <- cmd "touch" ["foo"]
  cmd "rm" ["foo"]

@simonmar
Copy link
Contributor

simonmar commented Oct 5, 2016

Right - the cmd data source doesn't satisfy the property, so when used with that data source Haxl doesn't obey the monad laws.

It's still possible to use Haxl under these conditions, but you have to know what you're doing. Be aware of when <*> is being used, and don't use ApplicativeDo.

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

No branches or pull requests

2 participants