Use of cpp

Ross Paterson ross at
Tue Jul 13 09:24:03 EDT 2004

On Tue, Jul 13, 2004 at 12:02:13PM +0100, Adrian Hey wrote:
> I've just modified some Haskell code from the AVL library to explicitly
> use ghc unboxed Ints in some routines.
> Unfortunately, I've broken portability by doing this and was considering some
> cpp magic to restore portability. But IIRC, the use of cpp is discouraged for
> Haskell library infrastructure.
> Is that so? If so, what should I use instead?

The initial version of the infrastructure may not support cpp, but it
should appear later.  However:

Using any equivalent of #ifdef makes your code hard to maintain: you're
not compiling all of it at any given time.  Sometimes it's unavoidable,
but for unboxing there is often a portable alternative:
- to get fields unboxed, add strictness annotations to them and use the
  UNPACK pragma.
- to get return values unboxed (i.e. components -- you can't unbox
  the whole thing), define a data type with strict fields (as above)
  and return that.
- to get arguments unboxed, add the minimum number of seq's to make GHC
  believe that the function is strict in those arguments.  Unfortunately
  this involves learning to read Core (or at least interface files),
  but after a while you'll get a feel for strictness.  A more serious
  problem is that this doesn't work well with higher-order functions.

More information about the Libraries mailing list