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

Fix dot documentation #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions Control/Parallel/Strategies.hs
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,22 @@ withStrategy = flip using
-- | Compose two strategies sequentially.
-- This is the analogue to function composition on strategies.
--
-- For any strategies @strat1@, @strat2@, and @strat3@,
-- > strat2 `dot` strat1 == strat2 . withStrategy strat1
--
-- 'dot' is associative:
--
-- > (strat1 `dot` strat2) `dot` strat3 == strat1 `dot` (strat2 `dot` strat3)
-- > strat1 `dot` strat1 = strat1
-- > strat1 `dot` r0 == strat1
--
-- > strat2 `dot` strat1 == strat2 . withStrategy strat1
-- 'r0', 'rseq', and 'rpar' are all one-sided identities of 'dot':
--
-- > strat `dot` r0 == strat
-- > strat `dot` rseq == strat
-- > strat `dot` rpar == strat
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that true?

Copy link
Contributor Author

@treeowl treeowl Jun 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe so, yes. runEval m is a lifted value; it doesn't do anything until it's forced. Forcing runEval (r0 x) runs the usual return x IO computation, so that gives you x. Forcing runEval (rseq x) runs an IO computation that forces x to WHNF and returns the result (exactly the same as forcing x directly). Forcing runEval (rpar x) runs an IO computation that sparks a thread to evaluate x to WHNF, but immediately forces the result so the thread (almost certainly) fizzles.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r0 `dot` rseq == r0
r0 `dot` rpar == r0

Would be counterintuitive to my expectations, of the first description of dot, namely: "Compose two strategies sequentially". Or more explicitely:

"Composing the 'do nothing'-strategy with the 'evaluate to WHNF'-strategy sequentially, creates the 'do nothing'-strategy"

Am I the only, who thinks this sounds weird?

Copy link
Contributor Author

@treeowl treeowl Jun 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r0 `dot` rseq is not r0. But rseq `dot` r0 = rseq.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last comment corrected.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dot is a composition operator in a semigroupoids sense: namely, it is associative. But it doesn't form a category, which makes intuition around it a little tricky. @Borgvall, by definition,

f `dot` g = \x -> f (runEval (g x))

Think about what runEval (r0 x), runEval (rpar x), and runEval (rseq x) do when forced. Ultimately the same thing (if you ignore sparks that fizzle). If you don't force them, they all do ... nothing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I care about sparking, this is the main reason to use this library!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still concerned about strat dot rpar == strat

We can construct contrived examples in which this doesn't apply, at least operationally (and operationally is after all the point of the library, so the equalities should be operational ones). e.g.

let strat n x = rseq (fib n) >> return x  in strat 99999 `dot` rpar

If strat is very slow then the spark doesn't fizzle immediately and we get some useful parallelism. Of course it's hard to reliably define a strategy that's very slow (the example above could easily be optimised), but the fact that it's possible at all implies that we can't assume the spark fizzles immediately.

--
-- Furthermore, since strategies should only force and sparks computations
-- (that is, they don't actually change any values),
--
-- > strat `dot` strat == strat
--
dot :: Strategy a -> Strategy a -> Strategy a
strat2 `dot` strat1 = strat2 . runEval . strat1
Expand Down