Skip to content

Commit d85ddb5

Browse files
authored
Use BigInt instead of Int (#42)
1 parent b480bc4 commit d85ddb5

File tree

8 files changed

+58
-46
lines changed

8 files changed

+58
-46
lines changed

README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,8 @@ bower install purescript-rationals
4040

4141
## Other Ratios
4242

43-
`Rational` is just a type alias for `Ratio Int` and you might want to use
44-
`Ratio` with other than `Int`. The type you choose must however be an `EuclideanRing`.
45-
46-
For example, one limitation with `Rational` is that it can easily overflow
47-
the 32-bit PureScript `Int`. You can get around this problem by using
48-
[`BigInt`](https://pursuit.purescript.org/packages/purescript-bigints/3.1.0/docs/Data.BigInt#t:BigInt).
43+
`Rational` is just a newtype over `Ratio BigInt` and you might want to use
44+
`Ratio` with other than `BigInt`. The type you choose must however be an `EuclideanRing`.
4945

5046
```
5147
> import Data.Ratio ((%), reduce)

bower.json

Lines changed: 0 additions & 20 deletions
This file was deleted.

package.json

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
{
22
"private": true,
33
"scripts": {
4-
"postinstall": "bower install",
5-
"build": "pulp build",
6-
"test": "pulp test",
4+
"build": "spago build",
75
"spago-build": "spago build",
6+
"test": "spago -x ./test.dhall test",
87
"spago-test": "spago -x ./test.dhall test"
98
},
109
"devDependencies": {
11-
"big-integer": "^1.6.51",
12-
"bower": "^1.8.14",
13-
"pulp": "^16.0.1",
14-
"purescript": "^0.15.2",
15-
"purs-tidy": "^0.9.2",
10+
"purescript": "^0.15.8",
1611
"spago": "^0.20.9"
1712
}
1813
}

packages.dhall

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
let upstream =
2-
https://github.com/purescript/package-sets/releases/download/psc-0.15.2-20220706/packages.dhall
3-
sha256:7a24ebdbacb2bfa27b2fc6ce3da96f048093d64e54369965a2a7b5d9892b6031
2+
https://github.com/purescript/package-sets/releases/download/psc-0.15.8-20230503/packages.dhall
3+
sha256:b0ca02f2efb206465b499ee847f174212be0c8c2e031044b7bd192d5294f22cb
44

55
in upstream

spago.dhall

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{ name = "rationals"
2-
, dependencies = [ "integers", "prelude" ]
2+
, dependencies = [ "integers", "js-bigints", "prelude" ]
33
, license = "MIT"
44
, repository = "https://github.com/purescript-contrib/purescript-rationals.git"
55
, packages = ./packages.dhall

src/Data/Rational.purs

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,60 @@
11
module Data.Rational
22
( Rational
3+
, (%)
34
, toNumber
45
, fromInt
5-
, module Data.Ratio
6+
, fromBigInt
7+
, numerator
8+
, denominator
9+
, class ToRational
10+
, toRational
611
) where
712

813
import Prelude
914

1015
import Data.Int as Int
1116
import Data.Ratio (Ratio, denominator, numerator, (%))
17+
import Data.Ratio as Ratio
18+
import JS.BigInt (BigInt)
19+
import JS.BigInt as BigInt
1220

13-
type Rational = Ratio Int
21+
newtype Rational = Rational (Ratio.Ratio BigInt)
22+
23+
derive newtype instance Eq Rational
24+
derive newtype instance Ord Rational
25+
derive newtype instance Show Rational
26+
derive newtype instance Semiring Rational
27+
derive newtype instance Ring Rational
28+
derive newtype instance CommutativeRing Rational
29+
derive newtype instance EuclideanRing Rational
30+
derive newtype instance DivisionRing Rational
1431

1532
toNumber :: Rational -> Number
16-
toNumber x = Int.toNumber (numerator x) / Int.toNumber (denominator x)
33+
toNumber (Rational x) = BigInt.toNumber (Ratio.numerator x) / BigInt.toNumber (Ratio.denominator x)
1734

1835
fromInt :: Int -> Rational
19-
fromInt i = i % 1
36+
fromInt i = Rational $ Ratio.reduce (BigInt.fromInt i) (BigInt.fromInt 1)
37+
38+
fromBigInt :: BigInt -> Rational
39+
fromBigInt i = Rational $ Ratio.reduce i (BigInt.fromInt 1)
40+
41+
numerator :: Rational -> BigInt
42+
numerator (Rational x) = Ratio.numerator x
43+
44+
denominator :: Rational -> BigInt
45+
denominator (Rational x) = Ratio.denominator x
46+
47+
class
48+
ToRational a
49+
where
50+
toRational :: a -> a -> Rational
51+
52+
instance ToRational Int
53+
where
54+
toRational a b = Rational $ Ratio.reduce (BigInt.fromInt a) (BigInt.fromInt b)
55+
56+
instance ToRational BigInt
57+
where
58+
toRational a b = Rational $ Ratio.reduce a b
59+
60+
infixl 7 toRational as %

test.dhall

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ let conf = ./spago.dhall
22

33
in conf // {
44
sources = conf.sources # [ "test/**/*.purs" ],
5-
dependencies = conf.dependencies # [ "bigints", "console", "effect", "test-unit", "quickcheck", "quickcheck-laws" ]
5+
dependencies = conf.dependencies # [ "console", "effect", "integers", "quickcheck", "quickcheck-laws" ]
66
}
77

test/Main.purs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ module Test.Main where
22

33
import Prelude
44

5-
import Data.BigInt (BigInt, fromInt)
6-
import Data.Ratio (Ratio, (%))
5+
import Data.Rational (Rational, (%))
76
import Effect (Effect)
87
import Effect.Console (log)
8+
import JS.BigInt (BigInt, fromInt)
99
import Test.QuickCheck (Result, quickCheck', (===))
1010
import Test.QuickCheck.Arbitrary (class Arbitrary, arbitrary)
1111
import Test.QuickCheck.Gen (Gen, suchThat)
@@ -14,7 +14,7 @@ import Test.QuickCheck.Laws.Data as Data
1414
import Test.QuickCheck.Laws.Data.Field (checkField) as DataField
1515
import Type.Proxy (Proxy(..))
1616

17-
newtype TestRational = TestRational (Ratio BigInt)
17+
newtype TestRational = TestRational Rational
1818

1919
derive newtype instance commutativeRingTestRational :: CommutativeRing TestRational
2020
derive newtype instance eqTestRational :: Eq TestRational
@@ -41,7 +41,7 @@ instance arbitraryTestRational :: Arbitrary TestRational where
4141
testRational :: Proxy TestRational
4242
testRational = Proxy
4343

44-
newtype TestRatNonZero = TestRatNonZero (Ratio BigInt)
44+
newtype TestRatNonZero = TestRatNonZero Rational
4545

4646
derive newtype instance eqTestRatNonZero :: Eq TestRatNonZero
4747
derive newtype instance semiringTestRatNonZero :: Semiring TestRatNonZero

0 commit comments

Comments
 (0)