Skip to content

Commit 6630cb3

Browse files
committed
fix #1 and #2
1 parent 93f6898 commit 6630cb3

File tree

2 files changed

+37
-30
lines changed

2 files changed

+37
-30
lines changed

transient/src/Transient/Base.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ TransIO, TransientIO
259259

260260
-- * Exceptions
261261

262-
,onException, onException', cutExceptions, continue, catcht, throwt,exceptionPoint, onExceptionPoint
262+
,onException, onException',whileException, cutExceptions, continue, catcht, throwt,exceptionPoint, onExceptionPoint
263263

264264
-- * Utilities
265265
,genId

transient/src/Transient/Internals.hs

+36-29
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ instance MonadPlus TransIO where
366366
mx <- runTrans x
367367

368368
was <- gets execMode -- getData `onNothing` return Serial
369+
369370
if was == Remote
370371

371372
then return Nothing
@@ -1309,14 +1310,7 @@ inputf remove ident message mv cond = do
13091310
when remove $ do removeChild; liftIO $ delConsoleAction ident
13101311
c <- liftIO $ readIORef rconsumed
13111312
if c then returnm mv else do
1312-
-- if null str
1313-
-- then
1314-
1315-
-- if retry then do
1316-
-- liftIO $ writeIORef rconsumed True;
1317-
-- loop
1318-
-- else do liftIO $ writeIORef rconsumed True; returnm mv
1319-
-- else do
1313+
13201314
let rr = read1 str
13211315

13221316
case (rr,str) of
@@ -1395,7 +1389,7 @@ inputLoop= do
13951389

13961390

13971391
inputLoop
1398-
`catch` \(SomeException _) -> myThreadId >>= killThread
1392+
`catch` \(SomeException _) -> inputLoop -- myThreadId >>= killThread
13991393

14001394

14011395
{-# NOINLINE rconsumed #-}
@@ -1420,7 +1414,7 @@ processLine r = do
14201414
mbs <- readIORef rcb
14211415
mapM_ (\cb -> cb x) $ map (\(_,_,p)-> p) mbs
14221416

1423-
mapM' f []= return ()
1417+
mapM' _ []= return ()
14241418
mapM' f (xss@(x:xs)) =do
14251419
f x
14261420
r <- readIORef rconsumed
@@ -1434,7 +1428,7 @@ processLine r = do
14341428
else do
14351429
threadDelay 1000
14361430
n <- atomicModifyIORef riterloop $ \n -> (n+1,n)
1437-
if n==100
1431+
if n==1
14381432
then do
14391433
when (not $ null x) $ hPutStr stderr x >> hPutStrLn stderr ": can't read, skip"
14401434
writeIORef riterloop 0
@@ -1530,8 +1524,8 @@ keep mx = do
15301524

15311525
st <- get
15321526
setData $ Exit rexit
1533-
(abduce >> labelState (fromString "input") >> liftIO inputLoop >> empty)
1534-
<|> do
1527+
1528+
do
15351529
option "options" "show all options"
15361530
mbs <- liftIO $ readIORef rcb
15371531

@@ -1569,21 +1563,26 @@ keep mx = do
15691563
empty
15701564

15711565
<|> mx
1566+
<|> do
1567+
abduce
1568+
liftIO $ execCommandLine
1569+
labelState (fromString "input")
1570+
liftIO inputLoop
1571+
empty
15721572
return ()
1573-
threadDelay 10000
1574-
execCommandLine
1573+
15751574
stay rexit
15761575

15771576
where
15781577
type1 :: TransIO a -> Either String (Maybe a)
15791578
type1= undefined
15801579

15811580
-- | Same as `keep` but does not read from the standard input, and therefore
1582-
-- the async input APIs ('option' and 'input') cannot be used in the monad.
1583-
-- However, keyboard input can still be passed via command line arguments as
1581+
-- the async input APIs ('option' and 'input') cannot respond interactively.
1582+
-- However, input can still be passed via command line arguments as
15841583
-- described in 'keep'. Useful for debugging or for creating background tasks,
15851584
-- as well as to embed the Transient monad inside another computation. It
1586-
-- returns either the value returned by `exit`. or Nothing, when there are no
1585+
-- returns either the value returned by `exit` or Nothing, when there are no
15871586
-- more threads running
15881587
--
15891588

@@ -1687,10 +1686,7 @@ onBack ac bac = registerBack (typeof bac) $ Transient $ do
16871686
Backtrack mreason stack <- getData `onNothing` (return $ backStateOf (typeof bac))
16881687
runTrans $ case mreason of
16891688
Nothing -> ac -- !> "ONBACK NOTHING"
1690-
Just reason -> do
1691-
1692-
-- setState $ Backtrack mreason $ tail stack -- to avoid recursive call to the same handler
1693-
bac reason -- !> ("ONBACK JUST",reason)
1689+
Just reason -> bac reason -- !> ("ONBACK JUST",reason)
16941690
where
16951691
typeof :: (b -> TransIO a) -> b
16961692
typeof = undefined
@@ -1708,6 +1704,7 @@ onUndo x y= onBack x (\() -> y)
17081704
{-# NOINLINE registerUndo #-}
17091705
registerBack :: (Typeable b, Show b) => b -> TransientIO a -> TransientIO a
17101706
registerBack witness f = Transient $ do
1707+
tr "registerBack"
17111708
cont@(EventF _ x _ _ _ _ _ _ _ _ _ _ _) <- get
17121709
-- if isJust (event cont) then return Nothing else do
17131710
md <- getData `asTypeOf` (Just <$> return (backStateOf witness))
@@ -1720,7 +1717,7 @@ registerBack witness f = Transient $ do
17201717

17211718
Nothing -> setData $ Backtrack mwit [cont]
17221719

1723-
runTrans f
1720+
runTrans $ return () >> f
17241721
where
17251722
mwit= Nothing `asTypeOf` (Just witness)
17261723
--addr x = liftIO $ return . hashStableName =<< (makeStableName $! x)
@@ -1731,6 +1728,7 @@ registerUndo f= registerBack () f
17311728

17321729
-- XXX Should we enforce retry of the same track which is being undone? If the
17331730
-- user specifies a different track would it make sense?
1731+
-- see https://gitter.im/Transient-Transient-Universe-HPlay/Lobby?at=5ef46626e0e5673398d33afb
17341732
--
17351733
-- | For a given undo track type, stop executing more backtracking actions and
17361734
-- resume normal execution in the forward direction. Used inside an undo
@@ -1761,6 +1759,7 @@ noFinish= continue
17611759
--
17621760
back :: (Typeable b, Show b) => b -> TransIO a
17631761
back reason = do
1762+
tr "back"
17641763
bs <- getData `onNothing` return (backStateOf reason)
17651764
goBackt bs -- !>"GOBACK"
17661765

@@ -1773,15 +1772,17 @@ back reason = do
17731772

17741773
goBackt (Backtrack _ [] )= empty
17751774
goBackt (Backtrack b (stack@(first : bs)) )= do
1776-
setData $ Backtrack (Just reason) stack
1777-
x <- runClosure first -- !> ("RUNCLOSURE",length stack)
1775+
setData $ Backtrack (Just reason) bs --stack
1776+
x <- runClosure first !> ("RUNCLOSURE",length stack)
17781777
Backtrack back bs' <- getData `onNothing` return (backStateOf reason)
17791778

17801779
case back of
1781-
Nothing -> runContinuation first x -- !> "FORWARD EXEC"
1780+
Nothing -> do
1781+
setData $ Backtrack (Just reason) stack
1782+
runContinuation first x !> "FORWARD EXEC"
17821783
justreason -> do
1783-
setData $ Backtrack justreason bs
1784-
goBackt $ Backtrack justreason bs -- !> ("BACK AGAIN",back)
1784+
--setData $ Backtrack justreason bs
1785+
goBackt $ Backtrack justreason bs !> ("BACK AGAIN",back)
17851786
empty
17861787

17871788
backStateOf :: (Show a, Typeable a) => a -> Backtrack a
@@ -1936,8 +1937,14 @@ onException' mx f= onAnyException mx $ \e -> do
19361937

19371938
exceptBack st = \(e ::SomeException) -> do -- recursive catch itself
19381939
runStateT ( runTrans $ back e ) st -- !> "EXCEPTBACK"
1939-
`catch` exceptBack st
1940+
-- `catch` exceptBack st -- removed
1941+
1942+
-- re execute the first argument as long as the exception is produced within the argument.
1943+
-- The second argument is executed before every re-execution
1944+
-- if the second argument executes `empty` the execution is aborted.
19401945

1946+
whileException :: Exception e => TransIO b -> (e -> TransIO()) -> TransIO b
1947+
whileException mx fixexc = mx `catcht` \e -> do fixexc e; whileException mx fixexc
19411948

19421949
-- | Delete all the exception handlers registered till now.
19431950
cutExceptions :: TransIO ()

0 commit comments

Comments
 (0)