Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell and low-level IO actions

Tags:

linux

haskell

ghc

How is low level stuff like sockets, pipes and file IO implemented in Haskell? I guess these IO methods are not native in Haskell but Haskell quickly wraps some low level C library, is it right?

like image 855
Cartesius00 Avatar asked Oct 15 '12 12:10

Cartesius00


2 Answers

Sockets, pipes and files are implemented in the kernel; to use them from a user-space program, you need to call into the kernel. If you regard the kernel as a library, then yes, I/O is necessarily implemented by a low-level C/assembler library.

In practice, Haskell implementations such as GHC will use the C library's wrappers around system calls. See e.g. the GHC RTS commentary, which describes the bits of C that make up the core of any Haskell program compiled with GHC. When in doubt, consult the source code.

like image 85
Fred Foo Avatar answered Sep 28 '22 12:09

Fred Foo


"quickly wraps" isn't always the best description here.

  1. Sometimes it's wrapped in the sense that you wrap a parcel (eg wxcore wraps wx).
    This is closest to the "quickly wrapping" you mentioned, but I think it would be better called thinly wrapping because I don't think it's as straightforward as all that.
  2. Sometimes it's wrapped like upholstery wraps a chair (eg wxHaskell wraps wx).
    I would call this "built around".
  3. Sometimes it's wrapped like a car wraps an engine (eg reactive banana wraps wx).
    I would call this "uses", and if you look at it you can see there's an engine in it, but it doesn't look like an engine, and you use it very differently.
  4. Sometimes it's wrapped like lorries wrap a goods train (eg haskell's threads wrap OS threads). I would call this "reimplemented". (Haskell can use OS threads, but Haskell's 'native' threads are far more lightweight.)

You could argue that because GHC's runtime system is written in C and your OS was probably written in C, Haskell is a wrapper around C, but that's like saying a Spyker C8 car wraps an Audi V8 engine. Spyker might be upset if you called their lovely C8 a box with an Audi in it. When you're driving your car you're using an engine, but not directly. Some people like to tweak their car, just like some people like to overclock their CPU, but you don't have to unless you want to. Some people say you should know how the engine works if you want to understand your car.

If you can forgive the Formula 1 references, mainly Haskell "wraps" C like a Torro Rosso wraps a Ferrari, but occasionally it's like a Maclaren wraps a Mercedes. (Before you call a Torro Rosso slow, compare it to a Ford.)

like image 25
AndrewC Avatar answered Sep 28 '22 10:09

AndrewC