Skip to content

Commit 5f16b76

Browse files
authored
Add withRunInBase to help decompose InputT (#131)
1 parent 74b574f commit 5f16b76

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

System/Console/Haskeline.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ module System.Console.Haskeline(
6464
defaultPrefs,
6565
runInputTWithPrefs,
6666
runInputTBehaviorWithPrefs,
67+
withRunInBase,
6768
-- ** History
6869
-- $history
6970
getHistory,

System/Console/Haskeline/InputT.hs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,27 @@ instance ( Fail.MonadFail m ) => Fail.MonadFail (InputT m) where
6565
instance ( MonadFix m ) => MonadFix (InputT m) where
6666
mfix f = InputT (mfix (unInputT . f))
6767

68+
-- | Run an action in the underlying monad, as per 'lift', passing it a runner
69+
-- function which restores the current 'InputT' context. This can be used in
70+
-- the event that we have some function that takes an action in the underlying
71+
-- monad as an argument (such as 'lift', 'hoist', 'forkIO', etc) and we want
72+
-- to compose it with actions in 'InputT'.
73+
withRunInBase :: Monad m =>
74+
((forall a . InputT m a -> m a) -> m b) -> InputT m b
75+
withRunInBase inner = InputT $ do
76+
runTerm <- ask
77+
history <- ask
78+
killRing <- ask
79+
prefs <- ask
80+
settings <- ask
81+
lift $ lift $ lift $ lift $ lift $ inner $
82+
flip runReaderT settings .
83+
flip runReaderT prefs .
84+
flip runReaderT killRing .
85+
flip runReaderT history .
86+
flip runReaderT runTerm .
87+
unInputT
88+
6889
-- | Get the current line input history.
6990
getHistory :: MonadIO m => InputT m History
7091
getHistory = InputT get

0 commit comments

Comments
 (0)