Skip to content

ghc-bignum package

Sylvain Henry requested to merge hsyl20/ghc:hsyl20-bignum into master

Introduce ghc-bignum package:

  • add a new ghc-bignum package providing Integer and Natural implementations
    • support several backends
  • remove integer-simple package.
    • ghc-bignum provides a new pure Haskell backend ("native")
    • faster than integer-simple
    • a lot more code sharing with other backends
  • remove integer-gmp package
    • merged as ghc-bignum "gmp" backend

Motivations

  • make GHC agnostic of the bignum backend used
    • faster: Integer/Natural are wired-in and we don't need to load interfaces to get their type
    • simpler: numeric literals don't have to carry their type
    • stronger: single code path (e.g. to produce final Core expression, to match datacons)
  • make it easier to test/add new bignum backends (e.g. bsdnt, libcrypto)
    • most of the code is shared between backends
    • the backend interface to implement is simple and well-documented (in the FFI backend module)
    • backends may fall back to "native" for some operations if necessary
  • use an efficient representation
    • always support small/big constructors (e.g. data Natural = NS Word# | NB ByteArray#)
    • was only present in integer-gmp. Now every backend supports it for free
  • make boot packages more consistent
    • avoid conditional package dependencies (e.g. in base)
    • backend selection is only a Cabal flag for ghc-bignum, not for its dependencies
  • easier to test
    • supports testing selected backend results against results of "native" backend

Side-effects

  • fix #15286 (closed) by defining Natural in ghc-bignum instead of base
    • remove Hadrian's ugly hack (i.e. passing -fno-omit-interface-pragmas)
  • fix #15262 (closed) by representing Integer/Natural in only one way
  • we provide a compatibility integer-gmp package built on top of ghc-bignum
    • it exposes function aliases and pattern synonyms (with deprecation warnings)
    • useful for packages directly depending on integer-gmp
  • a few GMP specifics functions are gone (e.g. prime number test). They would be better in another package (hgmp)

How to review

  • Commits are better reviewed one by one.
  • Reviewing the code of the backends may be difficult (especially numeric code) because it uses low-level unlifted stuff
    • I've added a lot of comments but do ask if there is something unclear
  • If you have some numeric code to test, please check that "gmp" and "native" backends work for you. Backend selection:
    • Hadrian:hadrian/build.sh --bignum=[gmp|native|check-gmp]
    • Make: set BIGNUM_BACKEND=[gmp|native] in your mk/build.mk file
    • check-gmp computes both with "gmp" and "native" and compare the results before returning them (Hadrian only).
Edited by Sylvain Henry

Merge request reports