Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell compiler magic: what requires a special treatment from the compiler?

Tags:

haskell

ghc

When trying to learn Haskell, one of the difficulties that arise is the ability when something requires special magic from the compiler. One exemple that comes in mind is the seq function which can't be defined i.e. you can't make a seq2 function behaving exactly as the built-in seq. Consequently, when teaching someone about seq, you need to mention that seq is special because it's a special symbol for the compiler.

Another example would be the do-notation which only works with instances of the Monad class.

Sometimes, it's not always obvious. For instance, continuations. Does the compiler knows about Control.Monad.Cont or is it plain old Haskell that you could have invented yourself? In this case, I think nothing special is required from the compiler even if continuations are a very strange kind of beast.

Language extensions set aside, what other compiler magic Haskell learners should be aware of?

like image 988
gawi Avatar asked Mar 02 '16 00:03

gawi


1 Answers

Nearly all the ghc primitives that cannot be implemented in userland are in the ghc-prim package. (it even has a module called GHC.Magic there!)

So browsing it will give a good sense.

Note that you should not use this module in userland code unless you know exactly what you are doing. Most of the usable stuff from it is exported in downstream modules in base, sometimes in modified form. Those downstream locations and APIs are considered more stable, while ghc-prim makes no guarantees as to how it will act from version to version.

The GHC-specific stuff is reexported in GHC.Exts, but plenty of other things go into the Prelude (such as basic data types, as well as seq) or the concurrency libraries, etc.

like image 52
sclv Avatar answered Sep 23 '22 17:09

sclv