5 ms·
The friendship between Haskell and C
- DeathArrow 4y agoCalling a C API isn't producing side effects?
- Laaas 4y agoYou pretend that they don't for morally pure functions like the ones from math.h.
- eesmith 4y agoI remember my surprise on a DEC Alpha in late 2001 when I did something like acos(3) and the floating-point exception triggered a core dump. As I recall, on the other OSes I used, SIGFPE was by default ignored.
- josefx 4y agoIsn't everything in math.h spamming errno?
- Joker_vD 4y agoIt's not an observable side effect because nobody ever observes the value of errno :)
- k0k0r0 4y agoWell, this is not true. However, to be fair, most important use case of errno in my work is debugging in horrible code bases, trying to understand what has happened.
- cryptonector 4y agoThe FFI wrappers can restore the previous value of errno after reading the newer value after calling whatever foreign function, thus undoing that side-effect, and nothing other than a signal handler could observe this side-effect in the middle of the FFI wrapper's execution. Oh, note to self: signal handlers need to restore errno values if they call async-signal-safe functions that can set errno.
- ParetoOptimal 4y agoAnd "Fast and loose reasoning is morally correct": http://www.cs.ox.ac.uk/people/jeremy.gibbons/publications/fast+loose.pdf http://www.cs.ox.ac.uk/people/jeremy.gibbons/publications/fa...
- WJW 4y agoNot all C api calls produce (observable) side effects. As a very simple example, the sin() function from math.h is a pure function of type `Double -> Double`. No need to wrap that in IO.
- eesmith 4y agoThe documentation for sin on a Mac says: sin(±infinity) returns a NaN and raises the "invalid" floating-point exception. However, I can't figure out how to make that happen, and my clumsy attempts to figure out <fenv.h> and feraiseexcept() failed to make sin(INFINITY) do something other than return a NaN.
- jacobmartin 4y agoYou should be able to see the floating point exception by testing fetestexcept(FE_INVALID) (if it returns nonzero/true then you have a domain error). errno is also set as a side effect. (edit), for instance this gives me output of "1": #include <math.h> #include <fenv.h> #include <stdio.h> int main(void) { double a = sin(INFINITY); printf("%d\n", fetestexcept(FE_INVALID)); return 0; }
- eesmith 4y ago"1" on my FreeBSD/Intel box. "0" on my Mac/M1 box with the vendor cc but "1" with gcc-12. I think I have an outdated idea that there should be a way to have the exception trigger a SIGFPE, and that modern processors don't do that.
- jacobmartin 4y agofascinating! Does vendor cc link to a different libc? I wonder if that is the problem and the docs are for glibc. And testing just now, I can't even trigger SIGFPE with division by zero (integer or float). I guess all of these exceptions are "caught" now.
- 4y ago
- chongli 4y agoHaskell doesn’t have side effects, but it does have effects. This is a subtle distinction that is a bit hard to explain but I’ll give it my best shot: One way to think of Haskell is like a pure functional C preprocessor that also happens to be lazy. When Haskell code is running, pure functional expressions are being evaluated and reduced to their simplest form. This simplest form is not a plain value but actually a program that when run has effects (reading input, printing to the screen) just like a C program. The last piece of the puzzle is that, unlike the C preprocessor, Haskell doesn’t do all of the evaluation ahead of time. The evaluation of these pure functional expressions is interwoven with the execution of the program producing the desired effects. Haskell’s type system is used to express the dependencies between inputs and outputs so that the effects are performed in the correct sequence.
- aidenn0 4y agoThere was a somewhat tongue-in-cheek article a while back saying "C has no side effects" with the premise being That "when people say C, they really mean CPP, because nobody writes C with the preprocessor disabled" and since CPP is side-effect free, colloquially we can say that C is side-effect free...
- ackfoobar 4y agohttp://conal.net/blog/posts/the-c-language-is-purely-functional http://conal.net/blog/posts/the-c-language-is-purely-functio... "The C language is purely functional" and "Haskell is the world's finest imperative programming language" can blow some minds.
- aidenn0 4y agoThat's the one. There's also: > “Haskell is a dynamically-typed, interpreted language.” From https://aphyr.com/posts/342-typing-the-technical-interview https://aphyr.com/posts/342-typing-the-technical-interview
- deleted 4y ago[deleted]
- avgcorrection 4y agoThis question returns no value (does not expect an honest answer) but has the Snark effect on the person being asked.
- Jeff_Brown 4y agoxmonad (a tiling window manager) and pandoc (a command-line tool for document conversion) are both written in Haskell.
- cryptonector 4y agoThat's allowed in the IO monad, and, really, it's allowed. The idea in Haskell is to have many contexts where side effects are not allowed so that in those contexts your code must be pure, and pure code is easier to write tests for and reason about.
- epgui 4y agoDid anyone claim the contrary, or do you just have prejudiced ideas about Haskell?
- anon291 4y agoHaskell is two languages. There's the expression language which is pure. Then there's the runtime system which takes descriptions of side effects generated by the expression language (the IO type) and executes them in the real world. The expression language is absolutely pure.
- sshine 4y agoA recent article posted on HN that didn't get a lot of attention: Calling Rust functions from Haskell FFI: https://engineering.iog.io/2023-01-26-hs-bindgen-introduction/ https://engineering.iog.io/2023-01-26-hs-bindgen-introductio...
- eggy 4y agoCalling Zig from Haskell is a nice variant, and adds something over C. https://luctielen.com/posts/calling_zig_from_haskell/ https://luctielen.com/posts/calling_zig_from_haskell/