Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why compiling this Haskell program with -fllvm produces a different result?

Tags:

Compiling the following program:

main = print (sqrt (-7)) 

On OSX Yosemite, LLVM version 3.4.2, target x86_64-apple-darwin14.0.0, GHC 7.8.4 produces two different results, depending on llvm use:

apple1$ ghc -fforce-recomp a.hs -o a; ./a [1 of 1] Compiling Main             ( a.hs, a.o ) Linking a ... NaN  apple1$ ghc -fforce-recomp -fllvm a.hs -o a; ./a [1 of 1] Compiling Main             ( a.hs, a.o ) Linking a ... 0.0 

Why this happens?

like image 483
MaiaVictor Avatar asked Jan 20 '15 19:01

MaiaVictor


People also ask

Why is Haskell different?

Haskell is different in two ways: Values are immutable by default, and mutability must be explicitly indicated with a variable type. Mutating a mutable variable is considered a side effect, and that mutable is tracked by the type system.

What does Haskell compile to?

The compiler (written in Haskell), translates Haskell to C, assembly, LLVM bitcode and other formats. The strategy it uses is described best here: Implementing lazy functional languages on stock hardware:the Spineless Tagless G-machine.

How do I run a compiled Haskell file?

Open a command window and navigate to the directory where you want to keep your Haskell source files. Run Haskell by typing ghci or ghci MyFile. hs. (The "i" in "GHCi" stands for "interactive", as opposed to compiling and producing an executable file.)

Is Haskell a compiled or interpreted language?

Unlike Python, Ruby, JavaScript, Lua, and other interpreted languages, Haskell is compiled ahead-of-time, directly to native machine code. The compiler (GHC) is remarkably good at optimization and generating efficient executables.

What are the advantages of Haskell over C?

Haskell has a very good type system. Algebraic data types allow for types like Maybe and Either and we can use values of those types to represent results that may be there or not. In C, returning, say, -1 on failure is completely a matter of convention. It only has special meaning to humans.

Do you have side-effects in Haskell?

Do not despair, all is not lost. It turns out that Haskell actually has a really clever system for dealing with functions that have side-effects that neatly separates the part of our program that is pure and the part of our program that is impure, which does all the dirty work like talking to the keyboard and the screen.

Why does the same program produce different output with different compilers?

Edit: I see the question comments have the source, and others have also commented on Eclipse and Putty not being C compilers. Generally speaking, the same program producing different output with different compilers is a sign that you’ve run into undefined behavior — typically this means that you’ve done something wrong.

Why do we need exceptions in Haskell?

The type is different from just plain a -> b and if we try to use those two functions interchangeably, the compiler will complain at us. Despite having expressive types that support failed computations, Haskell still has support for exceptions, because they make more sense in I/O contexts.


1 Answers

(Question answered in the comments. Converted to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )

@leftaroundabout wrote:

I would consider it a bug, though it might well have been acceptable according to some specification. LLVM evidently fired some optimisation here which assumes non-negative arguments, making √(-7) undefined behaviour.

@Ed'ka wrote:

Probably related: LLVM bug 21048

like image 130
2 revs Avatar answered Jan 02 '23 14:01

2 revs