Skip to content

Commit c43b63b

Browse files
authored
Merge pull request #11 from anttih/psc-0.9-updates
Update deps for psc 0.9.1 and make changes for new class hierarchy
2 parents e44a802 + 2c95a65 commit c43b63b

File tree

4 files changed

+48
-29
lines changed

4 files changed

+48
-29
lines changed

bower.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
"output"
1414
],
1515
"dependencies": {
16-
"purescript-integers": "~0.2.1"
16+
"purescript-integers": "^1.0.0"
1717
},
1818
"devDependencies": {
19-
"purescript-console": "^0.1.0",
20-
"purescript-prelude": "^0.1.0",
21-
"purescript-strongcheck": "~0.14.7"
19+
"purescript-console": "^1.0.0",
20+
"purescript-prelude": "^1.0.0",
21+
"purescript-strongcheck": "^1.0.0"
2222
}
2323
}

src/Data/Ratio.purs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module Data.Ratio where
22

3-
import Prelude (class Num, class DivisionRing, class ModuloSemiring, class
4-
Ring, class Semiring, (*), zero, (-), (+), one)
3+
import Prelude
54

65
data Ratio a = Ratio a a
76

@@ -14,13 +13,14 @@ instance semiringRatio :: (Semiring a) => Semiring (Ratio a) where
1413
instance ringRatio :: (Ring a) => Ring (Ratio a) where
1514
sub (Ratio a b) (Ratio c d) = Ratio ((a * d) - (b * c)) (b * d)
1615

17-
instance moduloSemiringRatio :: (Ring a, ModuloSemiring a) => ModuloSemiring (Ratio a) where
18-
mod _ _ = zero
19-
div (Ratio a b) (Ratio c d) = Ratio (a * d) (b * c)
16+
instance commutativeRingRatio :: (CommutativeRing a) => CommutativeRing (Ratio a)
2017

21-
instance divisionRingRatio :: (DivisionRing a) => DivisionRing (Ratio a)
18+
instance euclideanRingRatio :: (CommutativeRing a, Semiring a) => EuclideanRing (Ratio a) where
19+
degree _ = 1
20+
div (Ratio a b) (Ratio c d) = Ratio (a * d) (b * c)
21+
mod _ _ = zero
2222

23-
instance numRatio :: (Num a) => Num (Ratio a)
23+
instance fieldRatio :: (Field a) => Field (Ratio a)
2424

2525
numerator :: forall a. Ratio a -> a
2626
numerator (Ratio a _) = a

src/Data/Rational.purs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Data.Ratio (Ratio(Ratio))
1313
newtype Rational = Rational (Ratio Int)
1414

1515
instance showRational :: Show Rational where
16-
show (Rational (Ratio a b)) = show a ++ " % " ++ show b
16+
show (Rational (Ratio a b)) = show a <> " % " <> show b
1717

1818
instance eqRational :: Eq Rational where
1919
eq x y = eq' (reduce x) (reduce y)
@@ -26,21 +26,22 @@ instance ordRational :: Ord Rational where
2626
compare _ _ = GT
2727

2828
instance semiringRational :: Semiring Rational where
29-
one = Rational $ one
29+
one = Rational one
3030
mul (Rational a) (Rational b) = reduce $ Rational $ a `mul` b
31-
zero = Rational $ zero
31+
zero = Rational zero
3232
add (Rational a) (Rational b) = reduce $ Rational $ a `add` b
3333

3434
instance ringRational :: Ring Rational where
3535
sub (Rational a) (Rational b) = reduce $ Rational $ a `sub` b
3636

37-
instance moduloSemiringRational :: ModuloSemiring Rational where
38-
mod _ _ = zero
39-
div (Rational a) (Rational b) = reduce $ Rational $ a `div` b
37+
instance commutativeRingRational :: CommutativeRing Rational
4038

41-
instance divisionRingRational :: DivisionRing Rational
39+
instance euclideanRingRational :: EuclideanRing Rational where
40+
degree (Rational a) = degree a
41+
div (Rational a) (Rational b) = Rational $ a `div` b
42+
mod _ _ = Rational zero
4243

43-
instance numRational :: Num Rational
44+
instance fieldRational :: Field Rational
4445

4546
infixl 7 rational as %
4647

test/Main.purs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import Control.Monad.Eff.Console (CONSOLE, log)
66
import Control.Monad.Eff.Random (RANDOM)
77
import Control.Monad.Eff.Exception (EXCEPTION)
88
import Data.Rational (Rational, (%))
9-
import Test.StrongCheck (class Arbitrary, Result, quickCheck, (===))
9+
import Test.StrongCheck (Result, quickCheck, (===))
10+
import Test.StrongCheck.Arbitrary (class Arbitrary)
1011
import Test.StrongCheck.Gen (chooseInt, suchThat)
1112

1213

@@ -16,15 +17,15 @@ instance arbitraryTestRational :: Arbitrary TestRational where
1617
arbitrary = do
1718
a <- chooseInt (-99.0) 99.0
1819
b <- suchThat (chooseInt (-99.0) 99.0) (_ /= 0)
19-
return $ TestRational $ a % b
20+
pure $ TestRational $ a % b
2021

2122
newtype TestRatNonZero = TestRatNonZero Rational
2223

2324
instance arbitraryTestRatNonZero :: Arbitrary TestRatNonZero where
2425
arbitrary = do
2526
a <- suchThat (chooseInt (-99.0) 99.0) (_ /= 0)
2627
b <- suchThat (chooseInt (-99.0) 99.0) (_ /= 0)
27-
return $ TestRatNonZero $ a % b
28+
pure $ TestRatNonZero $ a % b
2829

2930
main :: forall eff. Eff (console :: CONSOLE, random :: RANDOM, err :: EXCEPTION | eff) Unit
3031
main = do
@@ -49,13 +50,13 @@ main = do
4950
log "Checking 'Annihilation' law for Semiring"
5051
quickCheck annihilation
5152

53+
log "Checking 'Additive inverse' law for Ring"
54+
quickCheck additiveInverse
55+
5256
log "Checking 'Remainder' law for MuduloSemiring"
5357
quickCheck remainder
5458

55-
log "Checking 'Multiplicative inverse' law for DivisionRing"
56-
quickCheck multiplicativeInverse
57-
58-
log "Checking 'Commutative multiplication' law for Num"
59+
log "Checking 'Commutative multiplication' law for CommutativeRing"
5960
quickCheck commutativeMultiplication
6061

6162
log "Checking 'Reflexivity' law for Ord"
@@ -65,6 +66,15 @@ main = do
6566
log "Checking 'Transitivity' law for Ord"
6667
quickCheck ordTransitivity
6768

69+
log "Checking 'Integral domain' law for EuclideanRing"
70+
quickCheck integralDomain
71+
72+
log "Checking 'Multiplicative Euclidean function' law for EuclideanRing"
73+
quickCheck multiplicativeEuclideanFunction
74+
75+
log "Checking 'Non-zero multiplicative inverse' law for Field"
76+
quickCheck multiplicativeInverse
77+
6878
where
6979

7080
identity :: TestRational -> Result
@@ -94,6 +104,9 @@ main = do
94104
annihilation :: TestRational -> Boolean
95105
annihilation (TestRational a) = zero * a == a * zero && zero == a * zero
96106

107+
additiveInverse :: TestRational -> Boolean
108+
additiveInverse (TestRational a) = a - a == (zero - a) + a && a - a == zero
109+
97110
ordReflexivity :: TestRational -> Boolean
98111
ordReflexivity (TestRational a) = a <= a
99112

@@ -103,11 +116,16 @@ main = do
103116
remainder :: TestRatNonZero -> TestRatNonZero -> Result
104117
remainder (TestRatNonZero a) (TestRatNonZero b) = a / b * b + (a `mod` b) === a
105118

106-
multiplicativeInverse :: TestRatNonZero -> Result
107-
multiplicativeInverse (TestRatNonZero x) = (one / x) * x === one
108-
109119
commutativeMultiplication :: TestRational -> TestRational -> Result
110120
commutativeMultiplication (TestRational a) (TestRational b) = a * b === b * a
111121

112122
ordTransitivity :: TestRational -> TestRational -> TestRational -> Boolean
113123
ordTransitivity (TestRational a) (TestRational b) (TestRational c) = if a <= b && b <= c then a <= c else true
124+
125+
integralDomain :: TestRatNonZero -> TestRatNonZero -> Boolean
126+
integralDomain (TestRatNonZero a) (TestRatNonZero b) = a * b /= zero
127+
128+
multiplicativeEuclideanFunction :: TestRatNonZero -> TestRatNonZero -> Boolean
129+
multiplicativeEuclideanFunction (TestRatNonZero a) (TestRatNonZero b) = a == (a / b) * b + (a `mod` b)
130+
multiplicativeInverse :: TestRational -> TestRational -> Boolean
131+
multiplicativeInverse (TestRational a) (TestRational b) = a `mod` b == zero

0 commit comments

Comments
 (0)