Haskell interface to GMP https://code.mathr.co.uk/hgmp

Latest on Hackage:

This package is not currently in any snapshots. If you're interested in using it, we recommend adding it to Stackage Nightly. Doing so will make builds more reliable, and allow stackage.org to host generated Haddocks.

BSD3 licensed by Claude Heiland-Allen
Maintained by claude@mathr.co.uk


Haskell interface to GMP. Contains type definitions and marshalling functions, to be able to write FFI bindings using Haskell's Integer and Rational types. Function bindings may come in a future version.

A simple example illustrating binding to GMP's next probable-prime function:

{-# LANGUAGE ForeignFunctionInterface #-}

import Foreign.Ptr (Ptr(..))
import Numeric.GMP.Types (MPZ)
import Numeric.GMP.Utils (withInInteger, withOutInteger_)
import System.IO.Unsafe (unsafePerformIO)

foreign import ccall safe "__gmpz_nextprime"
  mpz_nextprime :: Ptr MPZ -> Ptr MPZ -> IO ()

nextPrime :: Integer -> Integer
nextPrime n =
  unsafePerformIO $
    withOutInteger_ $ \rop ->
      withInInteger n $ \op ->
        mpz_nextprime rop op


  • use hsc2hs's #alignment macro in Storable instances

  • initial release
comments powered byDisqus