Monthly Downloads: 13
Programming language: Haskell
License: BSD 3-clause "New" or "Revised" License
Tags: Compiler     Compiler Plugin    
Latest version: v1.0.0.0

const-math-ghc-plugin alternatives and similar packages

Based on the "Compiler Plugin" category.
Alternatively, view const-math-ghc-plugin alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of const-math-ghc-plugin or a related project?

Add another 'Compiler Plugin' Package


Build Status

GHC plugin for Constant Math

This plugin eliminates constant math expressions from Haskell source.

Using this plugin adds a compiler pass which replaces mathematical expressions on constant numeric values with the result of that expression. Supported expressions include:

  • unary RealFloat functions (exp, log, sqrt, sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh)
  • unary Num functions (negate, abs, signum).
  • fromRational
  • (**)

Basic arithmetic expressions (addition, subtraction, multiplication, division) are already eliminated by existing GHC passes. This plugin is run multiple times during compilation, so the results of intermediate arithmetic eliminations are exposed to the const-math plugin, allowing chains of mathematical expressions to be completely eliminated.


Install the latest released version of the plugin from Hackage (requires GHC >= 7.4.1):

$ cabal install const-math-ghc-plugin

Alternatively, git clone this repo and:

$ cabal install


To use this plugin when building a Haskell source file, pass the following argument to GHC:

$ ghc -fplugin ConstMath.Plugin foo.hs

To build a cabal package packagename:

$ cabal install --ghc-options="-package const-math-ghc-plugin -fplugin ConstMath.Plugin" packagename

Note that you need to expose the const-math-ghc-plugin package to ghc (using the -package argument) as cabal will otherwise hide all packages not specified in packagename.cabal.


Option arguments can be passed to the plugin with -fplugin-opt:

$ ghc -fplugin ConstMath.Plugin -fplugin-opt=ConstMath.Plugin:ARG foo.hs

Currently available arguments are:

--dry, --dry-run
    Don't make any substitutions, just report on matches

    verbose output (default)

    very verbose output (useful for seeing the AST used for matching)

-q, --quiet
    no output

Phase control

    run a ConstMath pass after early simplifier passes

    run a ConstMath pass after every simplifier pass

    run a ConstMath pass after every compiler pass (expensive)

Inspecting the result

To see the resulting changes, compare the core produced. An excellent way to view core is to cabal install ghc-core, a tool which runs ghc with options to dump the core and the disassembled output in a pager with syntax highlighting:

$ ghc-core -- -O2 -fplugin ConstMath.Plugin foo.hs

Alternatively, just run ghc directly and ask it to show you the core:

$ ghc -fforce-recomp -ddump-simpl -O2 -fplugin ConstMath.Plugin foo.hs

For example, we generate a short file with a single math expression:

$ echo "module Main where main = print (1.0 / sqrt (2 * pi))" > foo.hs

Dumping the core produced by a normal optimized GHC build shows the sqrt will be evaluated at run-time:

$ ghc -ddump-simpl -O2 foo.hs | grep sqrt
  case GHC.Prim./## 1.0 (GHC.Prim.sqrtDouble# 6.283185307179586)

Note that GHC has already calculated the result of (2 * pi).

However, dumping the core produced by GHC with the additional const math pass shows that the call to sqrt has been removed:

$ ghc -ddump-simpl -O2 -fplugin ConstMath.Plugin foo.hs | grep -c sqrt

Inspecting the core further show that the division has also been removed, and the result of the entire math expression will be directly written into the executable:

  (Data.Maybe.Nothing @ GHC.Types.Int)


The source tree contains a set of tests, including the full numeric test suite imported from the GHC sources. To run the test suite:

$ cd tests
$ make

Alternatively, you can use the cabal test framework:

$ cabal install --enable-tests

Join in

File bugs in the GitHub issue tracker.

Master git repository:

  • git clone https://github.com/kfish/const-math-ghc-plugin.git


BSD3. See LICENSE for terms of copyright and redistribution.

*Note that all licence references and agreements mentioned in the const-math-ghc-plugin README section above are relevant to that project's source code only.