[Haskell-cafe] no sparks?

Daniel Fischer daniel.is.fischer at web.de
Sun Dec 20 19:46:57 EST 2009


Am Montag 21 Dezember 2009 01:00:10 schrieb Felipe Lessa:
> On Mon, Dec 21, 2009 at 12:39:06AM +0100, Daniel Fischer wrote:
> > Am Sonntag 20 Dezember 2009 23:25:02 schrieb Jamie Morgenstern:
> > > Also, I was wondering if something akin to a "parallel or" exists. By
> > > this, I mean I am looking for a function which, given x : a , y : a,
> > > returns either, whichever computation returns first.
> >
> > This wouldn't be easy to reconcile with referential transparency.
> > You can do that in IO, roughly
> >
> > m <- newEmptyMVar
> > t1 <- forkIO $ method1 >>= putMVar m
> > t2 <- forkIO $ method2 >>= putMVar m
> > rs <- takeMVar m
> > killThread t1
> > killThread t2
> > return rs
> >
> > But in pure code, I think not.
>
> There's 'unamb' in Hackage, however I think you should carefully
> understand its implementation details before using it.  Not that
> I use it myself.

Which is a (much) refined version of the above, wrapped in unsafePerformIO.
It is your obligation to call it only when legit, because it is *not* referentially 
transparent:

Prelude Data.Unamb> let go :: Int -> Bool -> Bool; go 0 b = b; go n b = let c = not b in c 
`seq` go (n-1) c
Prelude Data.Unamb> :set +s
Prelude Data.Unamb> let fun n = go n True
(0.00 secs, 0 bytes)
Prelude Data.Unamb> unamb (fun $ 10^6) (fun $ 10^6+1)
Loading package unamb-0.2.2 ... linking ... done.
True
(2.38 secs, 75324960 bytes)
Prelude Data.Unamb> unamb (fun $ 10^6) (fun $ 10^6+1)
False
(2.29 secs, 73940848 bytes)

>
> Link: http://hackage.haskell.org/package/unamb
>
> --
> Felipe.



More information about the Haskell-Cafe mailing list