Futures in Haskell
Asked Answered
J

3

12

Does Haskell have an equivalent of Alice's ability to bind a variable to a future?

val a = spawn foo;

where foo is some function.

I know Haskell supports channels and threads; I'm hoping for syntax as natural as Alice's to bind a value to a future and spawn a thread to calculate it without having to deal with the details.

Jurat answered 21/8, 2010 at 12:47 Comment(2)
Unrelated, but Clojure actually has those as well.Monseigneur
I've never looked into this myself, but I suspect that lazy IO can be made to do this in a really neat way, although it may have unsafeInterleaveIO under the hood.Wiseacre
R
14

You can use par from Control.Parallel as in

a `par` f a b c
where
  a = foo

This is a hint to the runtime that a could be evaluated in another thread.

Rhinoscopy answered 21/8, 2010 at 13:20 Comment(0)
B
12

Funny, I was just reading a new post by Simon Marlow: Parallel programming in Haskell with explicit futures. Apparently he and others have been working on some new parallel programming abstractions that are intended to be more natural and explicit than the par and pseq APIs.

Belk answered 21/8, 2010 at 14:31 Comment(0)
S
7

Not in the standard library, but

http://ghcmutterings.wordpress.com/2010/08/20/parallel-programming-in-haskell-with-explicit-futures/

data Future a = Future a

fork :: Eval a -> Eval (Future a)
fork a = do a' <- rpar (runEval a); return (Future a')

join :: Future a -> Eval a
join (Future a) = a `pseq` return a
Sternick answered 21/8, 2010 at 14:41 Comment(2)
"parallel" is part of the Haskell Platform, and regular lazy futures (par ) are already part of it.Konyn
Why isn't this Future api in the standard library? This is much easier for me to reason about than par and co.Defection

© 2022 - 2024 — McMap. All rights reserved.