2020-05-18 15:59:07 +03:00
|
|
|
module System.REPL
|
|
|
|
|
|
|
|
import System.File
|
|
|
|
|
|
|
|
||| A basic read-eval-print loop, maintaining a state
|
|
|
|
||| @ state the input state
|
|
|
|
||| @ prompt the prompt to show
|
|
|
|
||| @ onInput the function to run on reading input, returning a String to
|
|
|
|
||| output and a new state. Returns Nothing if the repl should exit
|
|
|
|
export
|
Back to HasIO, remove MonadIO
Following a fairly detailed discussion on slack, the feeling is
generally that it's better to have a single interface. While precision
is nice, it doesn't appear to buy us anything here. If that turns out to
be wrong, or limiting somehow, we can revisit it later. Also:
- it's easier for backend authors if the type of IO operations is
slightly less restrictive. For example, if it's in HasIO, that limits
alternative implementations, which might be awkward for some
alternative back ends.
- it's one less extra detail to learn. This is minor, but there needs to
be a clear advantage if there's more detail to learn.
- It is difficult to think of an underlying type that can't have a Monad
instance (I have personally never encountered one - if they turns out
to exist, again, we can revisit!)
2020-06-21 21:21:22 +03:00
|
|
|
replWith : HasIO io =>
|
2020-06-21 03:18:43 +03:00
|
|
|
(state : a) -> (prompt : String) ->
|
|
|
|
(onInput : a -> String -> Maybe (String, a)) -> io ()
|
2020-05-18 15:59:07 +03:00
|
|
|
replWith acc prompt fn
|
|
|
|
= do eof <- fEOF stdin
|
|
|
|
if eof
|
|
|
|
then pure ()
|
|
|
|
else do putStr prompt
|
|
|
|
fflush stdout
|
|
|
|
x <- getLine
|
|
|
|
case fn acc x of
|
|
|
|
Just (out, acc') =>
|
|
|
|
do putStr out
|
|
|
|
replWith acc' prompt fn
|
|
|
|
Nothing => pure ()
|
|
|
|
|
|
|
|
||| A basic read-eval-print loop
|
|
|
|
||| @ prompt the prompt to show
|
|
|
|
||| @ onInput the function to run on reading input, returning a String to
|
|
|
|
||| output
|
|
|
|
export
|
Back to HasIO, remove MonadIO
Following a fairly detailed discussion on slack, the feeling is
generally that it's better to have a single interface. While precision
is nice, it doesn't appear to buy us anything here. If that turns out to
be wrong, or limiting somehow, we can revisit it later. Also:
- it's easier for backend authors if the type of IO operations is
slightly less restrictive. For example, if it's in HasIO, that limits
alternative implementations, which might be awkward for some
alternative back ends.
- it's one less extra detail to learn. This is minor, but there needs to
be a clear advantage if there's more detail to learn.
- It is difficult to think of an underlying type that can't have a Monad
instance (I have personally never encountered one - if they turns out
to exist, again, we can revisit!)
2020-06-21 21:21:22 +03:00
|
|
|
repl : HasIO io =>
|
2020-06-21 03:18:43 +03:00
|
|
|
(prompt : String) -> (onInput : String -> String) -> io ()
|
2020-05-18 15:59:07 +03:00
|
|
|
repl prompt fn
|
|
|
|
= replWith () prompt (\x, s => Just (fn s, ()))
|
|
|
|
|
|
|
|
|