[Haskell-cafe] Parallel interruptible computations

Chris Wong lambda.fairy at gmail.com
Sun Sep 7 00:13:39 UTC 2014


Hi Corentin,

On Sat, Sep 6, 2014 at 10:35 PM, Corentin Dupont
<corentin.dupont at gmail.com> wrote:
> Nobody on those parallel interruptible computations/events?
> I believe this is related to FRP: we register on several events but only a
> part of them is enough to compute the result.
>  But I don't see such operator in FRP frameworks such as reactive-banana...

FRP libraries don't expose such a combinator, because ultimately it
can be decomposed into three problems:

1. Listening to multiple events

2. Maintaining internal state

3. Filtering out events that fit specific criteria

---

For (1), we use something like:

    merge :: Event a -> Event a -> Event a

For (2), we can use a Mealy machine:

    collect :: (s -> a -> (b, s)) -> s -> Event a -> Event b

For (3), we expose a filtering combinator

    filterJust :: Event (Maybe a) -> Event a

which discards Nothing events.

---

Now plug it all together:

    listenN :: Int -> [Event a] -> Event a
    listenN n = filterJust . collect f [] . merge
      where
        f xs x
          | length xs >= n = (Just xs, xs)  -- ignore
          | otherwise = (Nothing, x : xs)  -- accumulate

Hope this helps.

Chris

> On Fri, Sep 5, 2014 at 12:59 PM, Corentin Dupont <corentin.dupont at gmail.com>
> wrote:
>>
>> Hi guys!
>> I'm wondering if there is an abstraction (for example a typeclass) for
>> parallel interruptible computations.
>> I want several things to be computed in parallel. Those computations will
>> not finish at the same time, but as soon as enough result is gathered, the
>> remaining computations can be cancelled and the final result computed.
>> Clearly a Monad is not suited here: Monad are by definition for sequential
>> computation (since the result of the first computation is fed to the next).
>>
>> -> So the question is: is there an abstraction for parallel computations?
>>
>> Note that I'm not interrested with real implementations (such as with
>> threads) but with the abstraction.
>> For now I came up with:
>>
>> ShortcutEvents :: [Event a] -> ([a] -> Maybe b) -> Event b
>>
>> This takes a list of events and a function. The events can fire at
>> whatever moment, and as soon as one of the events fires, the function is
>> called with the results available so far. If the function returns Just, the
>> remaining events are cancelled and the final result is returned.
>> This is working fine, but I'd like a way to generalize this, especially to
>> generalize the list to any structure...
>>
>> The use case is a voting system: people can vote at whatever moment, but
>> sometime the result of the vote is known even if not everybody voted yet.
>>
>> Thanks!
>> Corentin
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list