[Haskell-cafe] do nmergeIO or mergeIO preserve order?

Luke Palmer lrpalmer at gmail.com
Tue Mar 10 19:49:41 EDT 2009


Oh, you're right.  Here are some thoughts.

You want the list you get back to only contain values in WHNF.  This differs
from mergeIO & co., which are simply evaluating the spine of the list, and
don't even look at the values.

I would also consider it bad style to be fully polymorphic in this case, as
you require polymorphic seq, which is evil (though I don't have the space to
argue this right now :-).  Unamb would be bad style, also, since your
semantics are nondeterministic and so you wouldn't meet the precondition.
Surely your result would have to be in IO.  ("amb" would be okay)

Here is how I would do it:

chooseIO :: [IO a] -> IO [a]
chooseIO xs = do
    chan <- newChan
    let eval io = forkIO (io >>= writeChan chan)
    forkIO $ mapM_ eval xs
    getChanContents chan

(The list never ends in this case, even when xs is finite.  I think it's
possible to make the result finite with the argument is, and maintain good
GC properties, but it would take some care)

The reason I take a list of [IO a] is to avoid polymorphic seq; the elements
of the list have built in what it means to evaluate them.  This also buys
you more flexibility (but also fewer guarantees...).

For the working Haskeller, it is safe to avoid all this pedantic zealotry...
I'm just being a purist.

Luke



On Tue, Mar 10, 2009 at 5:31 PM, Anatoly Yakovenko <aeyakovenko at gmail.com>wrote:

> i think this would still force me to evailuate the whole list, right?
> i would want something that pipes the results into a channel that i
> can lazyly read as the results are available.
>
> On Tue, Mar 10, 2009 at 2:44 PM, Luke Palmer <lrpalmer at gmail.com> wrote:
> > I think nmergeIO . map (:[]) should do the trick.
> >
> > Luke
> >
> > On Tue, Mar 10, 2009 at 3:41 PM, Anatoly Yakovenko <
> aeyakovenko at gmail.com>
> > wrote:
> >>
> >> Hmm, yea, actually that makes sense.  What i am looking for is
> >> something that maps over a list and returns the list in order which
> >> the values are evaluated.  looks like i can implement that pretty
> >> easily with unamb.
> >>
> >> On Tue, Mar 10, 2009 at 2:33 PM, Luke Palmer <lrpalmer at gmail.com>
> wrote:
> >> > Although it is not formally specified, my intuition for the
> >> > specification is
> >> > that order is preserved within each of the lists.
> >> >
> >> > Luke
> >> >
> >> > On Tue, Mar 10, 2009 at 2:50 PM, Anatoly Yakovenko
> >> > <aeyakovenko at gmail.com>
> >> > wrote:
> >> >>
> >> >> do nmergeIO or mergeIO preserve order? or not preserve order?
> >> >> _______________________________________________
> >> >> Haskell-Cafe mailing list
> >> >> Haskell-Cafe at haskell.org
> >> >> http://www.haskell.org/mailman/listinfo/haskell-cafe
> >> >
> >> >
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090310/67c9edb1/attachment.htm


More information about the Haskell-Cafe mailing list