transient-haskell / transient

A full stack, reactive architecture for general purpose programming. Algebraic and monadically composable primitives for concurrency, parallelism, event handling, transactions, multithreading, Web, and distributed computing with complete de-inversion of control (No callbacks, no blocking, pure state)
MIT License
630 stars 28 forks source link

`threads` management under exception handlers #82

Closed agocorona closed 4 years ago

agocorona commented 4 years ago

This program creates three tasks under the original thread (since threads 0 precludes the creation of new threads for choose). It uses the backtracking promitives:

main= keep' $ do
   x <- return 0 `onBack` \s ->  do
            forward ""
            case s of
                "a" -> threads 0 $ choose [1..3 :: Int]
                _   -> empty
   liftIO $ print x
   if x== 0 then back "a" else back "b"
   return ()

It produces: 0 -- generated by return 0 1 -- due to onBack and choose 2 -- due to onBack and choose 3 -- due to onBack and choose

Now, the same/equivalent program using exceptions only produces two results:

main= keep' $ do
   x <- return 0 `onException'` \(ErrorCall s) ->  do
            continue
            case s of
                "a" -> threads 0 $ choose [1..3 :: Int]
                _   -> empty
   liftIO $ print x
   if x== 0 then error "a"  else error "b"
   return ()

0
1

It seems that the first time that empty is executed, after choose 1 has been executed, which triggers "b" exception, stop the whole computation. It shouldn't, since exceptions are managed by the same mechanism.

agocorona commented 4 years ago

However if instead of error "a" I use throwt $ ErrorCall "a" it works well. error "a"= throw $ ErrorCall "a"

agocorona commented 4 years ago

However without exceptions the problem is the same:

main= keep' $ do
   x <- threads 0 $ choose [1..3 :: Int]
   liftIO $ print x
   when (x== 1) $ error "error"
   return ()

1 keep': error ...

agocorona commented 4 years ago

Solved. pending upload together with other changes