Skip to content

Commit

Permalink
Prepare for release of v0.1.0.0
Browse files Browse the repository at this point in the history
Tidy up documentation, make style consistent, add `nthPrimeMaxBound`,
add ChangeLog.md, and improve `README.md`.
  • Loading branch information
pgujjula committed Apr 9, 2024
1 parent 89baf4b commit 8f5488d
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 63 deletions.
15 changes: 15 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Changelog

## 0.1.0.0

### Added
* `Math.NumberTheory.Prime.Count.FFI`, with FFI bindings to all functions from the
`primecount` library.
* `Math.NumberTheory.Prime.Count`, with high-level wrappers (`primePi`,
`nthPrime`, and `primePhi`) around the functions in
`Math.NumberTheory.Prime.Count.FFI`.
* Test coverage of `primePi`, `nthPrime`, and `primePhi` with `tasty-hunit`.
* Benchmarks of `primePi`, `nthPrime`, and `primePhi` with `tasty-bench`.
* Full Haddock documentation coverage.
* Support for GHC 9.2.1, 9.0.1, 8.10.7, 8.8.4, 8.6.5, and 8.4.4, and
libprimecount v7.x, verified by GitHub Actions.
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,33 @@ This library provides Haskell bindings to Kim Walisch's

## Build instructions
First follow the
[installation directions](https://github.com/kimwalisch/primecount#installation)
for the original `primecount` library. As stated in the original directions, if
you are installing through your system's package manager, make sure to get the
development package, which might have a name like `primecount-devel`.
[directions](https://github.com/kimwalisch/primecount#installation)
for installing `libprimecount`. As stated in the directions, if you are
installing through your system's package manager, make sure to get the
development version of the primecount package, which might have a name like
`primecount-devel`. The current version of the Haskell bindings supports any
version of `libprimecount >= 7.0`.

Then you can build the Haskell bindings with Stack or Cabal, and read the
documentation.
```
# stack
# Stack
stack build
stack test
stack haddock primecount --open
# cabal
# Cabal
cabal update
cabal build
cabal test
cabal haddock # and then open the documentation manually
```

### Building `libprimecount.so` from source
### Building `libprimecount` from source
If you build and install the original `primecount` library from source, instead
of through a package manager, then you need to make sure that your Haskell build
system knows where to find it. For example, on Linux it might be installed to
`/usr/local/lib64`.
system knows where to find it. For example, on Linux `libprimecount` might be
installed to `/usr/local/lib64`.

Then if using Stack, add the following lines to your `stack.yaml` or your global
`~/.stack/config.yaml`:
Expand All @@ -53,4 +55,4 @@ as an argument to Cabal.

## Bugs
Report any bugs on the Github issue tracker, or by emailing
[email protected]
[[email protected]](mailto:[email protected]).
7 changes: 5 additions & 2 deletions bench/Main.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{-# OPTIONS_GHC -Wno-all #-}

-- |
-- Copyright : 2022 Preetham Gujjula
-- License : BSD-3-Clause
-- Maintainer : [email protected]
-- Stability : experimental
module Main (main) where

import Math.NumberTheory.Prime.Count
Expand Down
10 changes: 7 additions & 3 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ license: BSD-3-Clause
author: "Preetham Gujjula"
maintainer: "[email protected]"
copyright: "2021 Preetham Gujjula"
category: Math
category: Math, Number Theory
description: Please see the README on Github at
<https://github.com/pgujjula/primecount-haskell#readme>

tested-with: GHC ==8.4.4 || ==8.6.5 || ==8.8.4 || ==8.10.7 || ==9.0.1 || ==9.2.1
extra-source-files:
- README.md
- ChangeLog.md

ghc-options:
- -Wall
Expand All @@ -26,6 +27,9 @@ ghc-options:
- -Wredundant-constraints
- -Wmissing-export-lists

default-extensions:
- ScopedTypeVariables

dependencies:
- base >= 4.7 && < 5.0

Expand All @@ -43,22 +47,22 @@ library:
tests:
primecount-tests:
source-dirs: test
main: Main.hs
dependencies:
- primecount
- silently
- tasty
- tasty-hunit
main: Main.hs
other-modules:
- Test.Math.NumberTheory.Prime.Count

benchmarks:
primecount-bench:
source-dirs: bench
main: Main.hs
dependencies:
- primecount
- tasty-bench
main: Main.hs
when:
- condition: false
other-modules: Paths_primecount
9 changes: 8 additions & 1 deletion primecount.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ name: primecount
version: 0.1.0.0
synopsis: Bindings to the primecount library
description: Please see the README on Github at <https://github.com/pgujjula/primecount-haskell#readme>
category: Math
category: Math, Number Theory
homepage: https://github.com/pgujjula/primecount-haskell#readme
bug-reports: https://github.com/pgujjula/primecount-haskell/issues
author: Preetham Gujjula
Expand All @@ -21,6 +21,7 @@ tested-with:
GHC ==8.4.4 || ==8.6.5 || ==8.8.4 || ==8.10.7 || ==9.0.1 || ==9.2.1
extra-source-files:
README.md
ChangeLog.md

source-repository head
type: git
Expand All @@ -32,6 +33,8 @@ library
Math.NumberTheory.Prime.Count.FFI
hs-source-dirs:
src
default-extensions:
ScopedTypeVariables
ghc-options: -Wall -Wcompat -Wmissing-export-lists -Wincomplete-record-updates -Wpartial-fields -Wmissing-home-modules -Werror=missing-home-modules -Widentities -Wredundant-constraints -Wmissing-export-lists
extra-libraries:
primecount
Expand All @@ -46,6 +49,8 @@ test-suite primecount-tests
Test.Math.NumberTheory.Prime.Count
hs-source-dirs:
test
default-extensions:
ScopedTypeVariables
ghc-options: -Wall -Wcompat -Wmissing-export-lists -Wincomplete-record-updates -Wpartial-fields -Wmissing-home-modules -Werror=missing-home-modules -Widentities -Wredundant-constraints -Wmissing-export-lists
build-depends:
base >=4.7 && <5.0
Expand All @@ -60,6 +65,8 @@ benchmark primecount-bench
main-is: Main.hs
hs-source-dirs:
bench
default-extensions:
ScopedTypeVariables
ghc-options: -Wall -Wcompat -Wmissing-export-lists -Wincomplete-record-updates -Wpartial-fields -Wmissing-home-modules -Werror=missing-home-modules -Widentities -Wredundant-constraints -Wmissing-export-lists
build-depends:
base >=4.7 && <5.0
Expand Down
29 changes: 17 additions & 12 deletions src/Math/NumberTheory/Prime/Count.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{-# LANGUAGE ScopedTypeVariables #-}

-- |
-- Module : Math.NumberTheory.Prime.Count
-- Copyright : 2021 Preetham Gujjula
-- License : BSD3
-- License : BSD-Clause
-- Maintainer : [email protected]
-- Stability : experimental
--
Expand All @@ -14,10 +12,11 @@ module Math.NumberTheory.Prime.Count
( primePi,
primePiMaxBound,
nthPrime,
nthPrimeMaxBound,
primePhi,
getNumPrimecountThreads,
setNumPrimecountThreads,
primecountVersion
primecountVersion,
)
where

Expand All @@ -36,16 +35,17 @@ import Text.Read (readMaybe)
primePi :: Integral a => a -> a
primePi n
| n < 0 = 0
| n' > bound = fromInteger (primePiStr n')
| otherwise = fromIntegral (primecount_pi (fromInteger n'))
| n' <= bound = fromIntegral (primecount_pi (fromInteger n'))
| n' <= primePiMaxBound = fromInteger (primePiStr n')
| otherwise = error "primePi: input larger than primePiMaxBound"
where
bound :: Integer
bound = toInteger (maxBound :: Int64)

n' :: Integer
n' = toInteger n

-- | The largest input supported by @primePi@.
-- | The largest input supported by 'primePi'.
--
-- * 64-bit CPUs: @10^31@
-- * 32-bit CPUs: @2^63 - 1@
Expand All @@ -67,12 +67,12 @@ primePiStr n = unsafePerformIO $ do
(error "primePi: couldn't parse result of primecount_pi_str")
pure
(readMaybe answer)
{-# NOINLINE primePiStr #-}

-- | The nth prime, starting at @nthPrime 1 == 2@.
--
-- * Throws an error if the input is less than 1.
-- * Throws an error if the input is larger than
-- @primePi ('maxBound' :: 'Int64') == 216289611853439384@.
-- * Throws an error if the input is larger than 'nthPrimeMaxBound`.
nthPrime :: Integral a => a -> a
nthPrime n
| n < 1 = error "nthPrime: n must be >= 1"
Expand All @@ -85,6 +85,11 @@ nthPrime n
n' :: Integer
n' = toInteger n

-- | The largest input supported by 'nthPrime', equal to
-- @primePi ('maxBound' :: 'Int64') == 216289611853439384@.
nthPrimeMaxBound :: Integer
nthPrimeMaxBound = 216289611853439384

-- | @primePhi n a@ counts the number of positive integers @<= n@ that are not
-- divisible by any of the first @a@ primes. Throws an error if @n@ is larger
-- than @'maxBound' :: 'Int64'@.
Expand All @@ -104,18 +109,18 @@ primePhi n a
a' :: Integer
a' = min bound (toInteger a)

-- | Get the currently set number of threads used by libprimecount.
-- | Get the currently set number of threads used by @libprimecount@.
getNumPrimecountThreads :: IO Int
getNumPrimecountThreads = primecount_get_num_threads

-- | Set the number of threads used by libprimecount. If the input is not
-- | Set the number of threads used by @libprimecount@. If the input is not
-- positive, the thread count is set to 1. If the input is greater than the
-- number of CPUs available, the thread count is set to the number of CPUs
-- available.
setNumPrimecountThreads :: Int -> IO ()
setNumPrimecountThreads = primecount_set_num_threads

-- | Get the libprimecount version number, in the form @"i.j"@
-- | Get the @libprimecount@ version number, in the form @"i.j"@
primecountVersion :: String
primecountVersion = unsafePerformIO (peekCString primecount_version)
{-# NOINLINE primecountVersion #-}
15 changes: 6 additions & 9 deletions src/Math/NumberTheory/Prime/Count/FFI.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- |
-- Module : Math.NumberTheory.Prime.Count.FFI
-- Copyright : 2021 Preetham Gujjula
-- License : BSD3
-- License : BSD-3-Clause
-- Maintainer : [email protected]
-- Stability : experimental
--
Expand All @@ -21,7 +21,7 @@ module Math.NumberTheory.Prime.Count.FFI
primecount_get_max_x,
primecount_get_num_threads,
primecount_set_num_threads,
primecount_version
primecount_version,
)
where

Expand All @@ -33,7 +33,7 @@ import Foreign.C.Types (CSize (..))
foreign import ccall unsafe "primecount_pi"
primecount_pi :: Int64 -> Int64

-- | Count the number of primes <= x (supports 128-bit)
-- | Count the number of primes <= x (supports 128-bit).
foreign import ccall unsafe "primecount_pi_str"
primecount_pi_str :: CString -> CString -> CSize -> IO Int

Expand All @@ -51,20 +51,17 @@ foreign import ccall unsafe "primecount_phi"
--
-- * 64-bit CPUs: @10^31@
-- * 32-bit CPUs: @2^63 - 1@
--
-- The return type is a 'CString' as @primecount_get_max_x@ may be a
-- 128-bit integer which is not supported by some compilers.
foreign import ccall unsafe "primecount_get_max_x"
primecount_get_max_x :: CString

-- | Get the currently set number of threads
-- | Get the currently set number of threads used by @libprimecount@.
foreign import ccall unsafe "primecount_get_num_threads"
primecount_get_num_threads :: IO Int

-- | Set the number of threads
-- | Set the number of threads used by @libprimecount@.
foreign import ccall unsafe "primecount_set_num_threads"
primecount_set_num_threads :: Int -> IO ()

-- | Get the primecount version number, in the form i.j
-- | Get the @libprimecount@ version number, in the form @"i.j"@.
foreign import ccall unsafe "primecount_version"
primecount_version :: CString
2 changes: 1 addition & 1 deletion stack.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resolver: lts-18.19
resolver: lts-18.21

packages:
- '.'
2 changes: 1 addition & 1 deletion test/Main.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- |
-- Copyright : 2021 Preetham Gujjula
-- License : BSD3
-- License : BSD-3-Clause
-- Maintainer : [email protected]
-- Stability : experimental
module Main (main) where
Expand Down
Loading

0 comments on commit 8f5488d

Please sign in to comment.