[Haskell-cafe] The amount of CPP we have to use is getting out of hand

Nicola Gigante nicola.gigante at gmail.com
Fri Jan 9 17:59:18 UTC 2015

> Il giorno 09/gen/2015, alle ore 14:55, Johan Tibell <johan.tibell at gmail.com> ha scritto:
> Hi,
> (This was initially written as a Google+ post, but I'm reposting it here to raise awareness of the issue.)
> The amount of CPP we have to use in Haskell is getting a bit out of hand. Here are the number of modules, per library, that use CPP for some of the libraries I maintain:
> containers 18/18
> hashable 4/5
> unordered-containers 6/9
> network 3/7
> cassava 4/16
> cabal/cabal-install 13/75
> cabal/Cabal 7/78
> ekg 1/15
> If this doesn't look like a lot to you (I hope it does!) consider than some languages don't use CPP at all (e.g. Java).
> CPP really sucks from a maintenance perspective:
>  * It's not Haskell, but this bizarre string concatenation language.
>  * The code is harder to read, bitrots more easily, and is harder to test.
>  * The code can't be compiled without using Cabal (which generates some of the CPP macros for us.) This hurts e.g. ad-hoc testing/benchmarking.
> There are a couple of reasons we use CPP, but the main one is breaking changes in GHC and libraries we depend on. We need to reduce these kind of breakages in the future. Dealing with breakages and maintaining the resulting CPP-ed code is costing us time we could spend on other things, such as improving our libraries or writing new ones. I for one would like to get on with writing applications instead of spending time on run-of-the-mill libraries.
> Often these breaking changes are done in the name of "making things cleaner". Breaking changes, no matter how well-intended, doesn't make code cleaner, it makes it less clean*. Users end up having to use both the old "unclean" API and the new "clean" API.
> The right way to move to evolve an new API is to add new functions and data types, not modify old ones, whenever possible.
> * It takes about 3 major GHC releases (~3 years) before you can remove the CPP, but since new things keep breaking all the time you always have a considerable amount of CPP.


I’m an outsider so this could probably sound ingenuous but,
why not thinking about an in-language feature to solve the
problems addressed by CPP?

I think these all fall into:
Enable some top-level declaration only if the XYZ feature is

This feature should allow the user to specify different definitions
of the same symbol depending on the availability of compiler
features but also _modules_ features.

So if I can declare and export a newFunc function from my module 
only if DataKinds is supported, I can do it explicitly instead of
relying on GHC version X.Y.Z. On the other hand, the users of
my module can decide if they want to compile some code depending
on the fact that my module exports the function or not.

This should not be limited to “the module exports the function”. Other
types of “features” could be tested over, and modules should be
able to declare the new features added which deserve to be tested
in this way. For example, if in the 2.0 version of my module I’ve
increased the laziness of my data structure, I can export the feature
“MyModule.myFunc is lazy” (encoded in some way). Then the
user can decide which implementation of its algorithm to use depending
on this.

I think a system like this should solve the majority of maintenance burden
- Dependencies on libraries features are explicit and the version
  numbers needed to support them can be inferred by cabal.
  For example cabal could support a syntax like containers(with:foo_is_lazy)
  instead of containers >= x.y.z
- GHC can automatically warn about features that are supported by all the 
  currently supported versions of GHC so that the checks can be removed.
- Code is more testable because the test suite could run tests multiple times,
  each time “faking” the availability of certain features, with the GHC support 
  of a “fake old version mode” where it has simply to pretend to not know
  the existence of a certain extension (not at all a “compatibility mode”, to be
  clear). As for library features, integrating with cabal sandboxes one could 
  automatically switch library versions to run the test with.
- Other?

I repeat: I’m an outsider of the world of maintenance of the haskell packages,
so I could be missing something obvious. Hope this can be useful though.

> — Johan


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20150109/1a0c2192/attachment.html>

More information about the Haskell-Cafe mailing list