implements instance EuclideanRing

This commit is contained in:
Guillaume Bagan 2022-12-29 00:15:12 +01:00
parent 29e8406b8b
commit 61cebd23fb
4 changed files with 29 additions and 2 deletions

1
.gitignore vendored
View File

@ -8,3 +8,4 @@
/.purs*
/.psa*
/.spago
/package-lock.json

View File

@ -25,7 +25,23 @@ export const biMul = (x) => (y) => x * y;
export const biSub = (x) => (y) => x - y;
export const biMod = (x) => (y) => x % y;
export const biMod = (x) => (y) => {
if (y === 0n)
return 0n;
const yy = y < 0n ? -y : y;
return ((x % yy) + yy) % yy;
}
export const biDiv = (x) => (y) => {
if (y === 0n) return 0n;
const xx = x - biMod(x)(y);
return y > 0n ? (xx / y) : -(xx / -y);
}
export const biDegree = (x) => {
const xx = x < 0n ? -x : x;
return BigInt.asIntN(32, xx > 2147483647n ? 2147483647n : xx);
}
export const biZero = 0n;

View File

@ -91,10 +91,18 @@ foreign import biSub :: BigInt -> BigInt -> BigInt
instance Ring BigInt where
sub = biSub
foreign import biMod :: BigInt -> BigInt -> BigInt
foreign import biDiv :: BigInt -> BigInt -> BigInt
foreign import biDegree :: BigInt -> Int
instance CommutativeRing BigInt
instance EuclideanRing BigInt where
degree = biDegree
div = biDiv
mod = biMod
-- Raise an BigInt to the power of another BigInt.
foreign import pow :: BigInt -> BigInt -> BigInt

View File

@ -7,7 +7,7 @@ import Debug (spy)
import Effect (Effect)
import Effect.Console (log)
import Js.BigInt.BigInt (BigInt, and, fromInt, fromString, fromTLInt, not, or, pow, shl, shr, toString, xor)
import Prelude (class CommutativeRing, class Eq, class Ord, class Ring, class Semiring, Unit, bind, compare, discard, identity, map, negate, one, pure, show, zero, ($), (*), (+), (-), (<$>), (<<<), (==))
import Prelude (class CommutativeRing, class Eq, class EuclideanRing, class Ord, class Ring, class Semiring, Unit, bind, compare, discard, identity, map, negate, one, pure, show, zero, ($), (*), (+), (-), (<$>), (<<<), (==))
import Test.Assert (assert)
import Test.QuickCheck (quickCheck)
import Test.QuickCheck.Arbitrary (class Arbitrary)
@ -32,6 +32,7 @@ derive newtype instance Ord TestBigInt
derive newtype instance Semiring TestBigInt
derive newtype instance Ring TestBigInt
derive newtype instance CommutativeRing TestBigInt
derive newtype instance EuclideanRing TestBigInt
instance Arbitrary TestBigInt where
arbitrary = do
@ -118,6 +119,7 @@ main = do
Data.checkSemiring prxBigInt
Data.checkRing prxBigInt
Data.checkCommutativeRing prxBigInt
-- Data.checkEuclideanRing prxBigInt
log "Converting BigInt to Int"
-- assert $ (fromString "0" <#> asIntN 64) == Just 0