-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tidy up documentation, make style consistent, add `nthPrimeMaxBound`, add ChangeLog.md, and improve `README.md`.
- Loading branch information
Showing
10 changed files
with
94 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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`: | ||
|
@@ -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]). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -26,6 +27,9 @@ ghc-options: | |
- -Wredundant-constraints | ||
- -Wmissing-export-lists | ||
|
||
default-extensions: | ||
- ScopedTypeVariables | ||
|
||
dependencies: | ||
- base >= 4.7 && < 5.0 | ||
|
||
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
-- | ||
|
@@ -14,10 +12,11 @@ module Math.NumberTheory.Prime.Count | |
( primePi, | ||
primePiMaxBound, | ||
nthPrime, | ||
nthPrimeMaxBound, | ||
primePhi, | ||
getNumPrimecountThreads, | ||
setNumPrimecountThreads, | ||
primecountVersion | ||
primecountVersion, | ||
) | ||
where | ||
|
||
|
@@ -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@ | ||
|
@@ -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" | ||
|
@@ -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'@. | ||
|
@@ -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 #-} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
-- | ||
|
@@ -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 | ||
|
||
|
@@ -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 | ||
|
||
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
resolver: lts-18.19 | ||
resolver: lts-18.21 | ||
|
||
packages: | ||
- '.' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
Oops, something went wrong.