Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is Haskell a managed language?

Tags:

haskell

ghc

I'm a complete newbie in Haskell. One thing that always bugs me is the ambiguity in whether Haskell is a managed(term borrowed from MS) language like Java or a compile-to-native code like C?

The GHC page says this "GHC compiles Haskell code either directly to native code or using LLVM as a back-end".

In the case of "compiled to native code", how can features like garbage collection be possible without something like a JVM?

/Update/

Thanks so much for your answer. Conceptually, can you please help point out which one of my following understandings of garbage collection in Haskell is correct:

GHC compiles Haskell code to native code. In the processing of compiling, garbage collection routines will be added to the original program code?

OR

There is a program that runs along side a Haskell program to perform garbage collection?

like image 702
TommyQ Avatar asked May 30 '12 03:05

TommyQ


People also ask

Does Haskell compile to native code?

Unlike Python, Ruby, JavaScript, Lua, and other interpreted languages, Haskell is compiled ahead-of-time, directly to native machine code.

Is Haskell faster than C?

Then, you have something like Haskell that is often faster than C in the benchmarks. It's very close to C when it's not faster. It's not like, "Oh, it's twice as slow." No, it's right there, it's within a few percentage of C, and very often it's on the other side, it's faster than C.

Is Haskell faster than Java?

The programming language benchmark game suggests that Haskell is roughly as fast as Java and C#, but this is complicated by the extensive use of foreign libraries in those languages.


1 Answers

As far as I am aware the term "managed language" specifically means a language that targets .NET/the Common Language Runtime. So no, Haskell is not a managed language and neither is Java.

Regarding what Haskell is compiled to: As the documentation you quoted says, GHC compiles Haskell to native code. It can do so by either directly emitting native code or by first emitting LLVM code and then letting LLVM compile that to native code. Either way the end result of running GHC is a native executable.

Besides GHC there are also other implementations of Haskell - most notably Hugs, which is a pure interpreter that never produces an executable (native or otherwise).

how can features like garbage collection be possible without something like a JVM?

The same way that they're possible with the JVM: Every time memory is allocated, it is registered with the garbage collector. Then from time to time the garbage collector runs, following the steps of the given garbage collection algorithm. GHC-compiled code uses generational garbage collection.


In response to your edit:

GHC compiles Haskell code to native code. In the processing of compiling, garbage collection routines will be added to the original program code?

Basically. Except that saying "garbage collection routines will be added to the original program code" might paint the wrong picture. The GC routines are just part of the library that every Haskell program is linked against. The compiled code simply contains calls to those routines at the appropriate places.

Basically all there is to it is to call the GC's alloc function every time you would otherwise call malloc.

Just look at any GC library for C and how it's used: All you need to do is to #include the library's header and link against the library, and replace each occurence of malloc with the GC library's alloc function (and remove all calls to free) and bam, your code is garbage collected.

There is a program that runs along side a Haskell program to perform garbage collection?

No.

like image 129
sepp2k Avatar answered Sep 20 '22 09:09

sepp2k