[Haskell-cafe] Loop with the StateT monad

Brandon Allbery allbery.b at gmail.com
Sat Mar 18 23:08:50 UTC 2017


On Sat, Mar 18, 2017 at 6:51 PM, Ben Franksen <ben.franksen at online.de>
wrote:

> What exactly is meant here with "conflate"?
>

Confusing two distinct mechanisms that appear on the surface to be similar.
See below for why this is an inappropriate conflation.


>
> When you turn off buffering for stdin, then of course backspace cannot
> work. That should be clear.
>

Except that is not true anywhere but recent versions of ghc's runtime, and
(specifically because they use readline or editline) things like Python's
REPL. Try it in a simple C program.

Buffering means the program reads in chunks instead of character at a time.
This is an attribute of a program, specifically of whatever I/O library it
is using. It does not include line editing (you don't generally want to
interpret a backspace in a text file you are reading. You *certainly* don't
want to interpret control-C or control-D).

Things like backspace *do not live in the program*, unless you are using a
library like readline or editline. For most programs, they rely on the tty
driver to do this. This is why stty works from the shell, not as
instrumentation for whatever program. The default on Unixlikes is line mode
(stty icanon), which implements basic line editing and only sends a line to
the program when you press Enter or the EOF character (in the middle of a
line, this will send the incomplete line; press it a second time, or on an
empty line, and the program gets a zero-character read response, which is
the standard Unix EOF indication).

In ghc this is complicated by two things:

- ghci uses haskeline, a readline alternative, so the tty is always in
-icanon. Compiled ghc programs do not use haskeline unless specifically
written to do so.

- At some point, the ghc runtime started tweaking termios settings in
hSetBuffering. This is incomplete (per the IRC log I mentioned, apparently
nothing at all is done with LineBuffering), and causes confusion when
multiple ghc-compiled programs are involved (the program starts out in
LineBuffering and assumes stty icanon, but whether the tty is in icanon or
-icanon depends on what the invoking program did; this is the stack bug I
linked).

On Windows, this works differently; IIRC, whether you see "character mode"
or "line mode" depends on which API you use for reads, and line editing
indeed lives within the program.

-- 
brandon s allbery kf8nh                               sine nomine associates
allbery.b at gmail.com                                  ballbery at sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20170318/df5e502e/attachment.html>


More information about the Haskell-Cafe mailing list