do nmergeIO or mergeIO preserve order?

do nmergeIO or mergeIO preserve order? or not preserve order?

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
do nmergeIO or mergeIO preserve order? or not preserve order? _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

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
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
wrote: do nmergeIO or mergeIO preserve order? or not preserve order? _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

I think nmergeIO . map (:[]) should do the trick.
Luke
On Tue, Mar 10, 2009 at 3:41 PM, Anatoly Yakovenko
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
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@gmail.com> wrote:
do nmergeIO or mergeIO preserve order? or not preserve order? _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

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
I think nmergeIO . map (:[]) should do the trick.
Luke
On Tue, Mar 10, 2009 at 3:41 PM, Anatoly Yakovenko
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
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
wrote: do nmergeIO or mergeIO preserve order? or not preserve order? _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

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
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
wrote: I think nmergeIO . map (:[]) should do the trick.
Luke
On Tue, Mar 10, 2009 at 3:41 PM, Anatoly Yakovenko < aeyakovenko@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
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
wrote: do nmergeIO or mergeIO preserve order? or not preserve order? _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

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)
what do you mean by fully polymorphic?
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
Cool, thanks, thats basically what iw as thinking to. Anatoly

Anatoly Yakovenko wrote:
do nmergeIO or mergeIO preserve order? or not preserve order?
If you have a list of operations "[IO a]" then the future package at http://hackage.haskell.org/cgi-bin/hackage-scripts/package/future can do this. It's 'forkPromises' function returns a "Chan a" which can be used to get the non-order preserving results (actually "Either SomeExcption a"). If you are feeling lucky you can use "getChanContents" and "filter" to get a lazy "[a]" which is the results as they are completed. -- Chris
participants (3)
-
Anatoly Yakovenko
-
ChrisK
-
Luke Palmer