-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathZMod2.hs
More file actions
37 lines (29 loc) · 1.05 KB
/
Copy pathZMod2.hs
File metadata and controls
37 lines (29 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, GeneralizedNewtypeDeriving, NoImplicitPrelude, RebindableSyntax #-}
module ZMod2 where
import NumericPrelude
import qualified Algebra.Additive as Additive
import qualified Algebra.Ring as Ring
import qualified Algebra.Field as Field
import Utils
import Data.Array.Unboxed
import System.Random
newtype ZMod2 = ZMod2 Bool deriving (IArray UArray, Eq, Ord)
instance Show ZMod2 where
show (ZMod2 b) = if b then "1" else "0"
instance Read ZMod2 where
readsPrec _ ('0':rst) = [(0,rst)]
readsPrec _ ('1':rst) = [(1,rst)]
readsPrec _ _ = []
instance Additive.C ZMod2 where
(+) = ZMod2 .-. (xor `on` (==one))
negate = id
zero = ZMod2 False
instance Ring.C ZMod2 where
(*) = ZMod2 .-. ((&&) `on` (==one))
one = ZMod2 True
fromInteger n = ZMod2 $ odd n
instance Field.C ZMod2 where
x / y = if y == one then x else error "Z/2 Divide by Zero"
instance Random ZMod2 where
random g = let (r,g') = random g in (ZMod2 r,g')
randomR (ZMod2 x,ZMod2 y) g = let (r,g') = randomR (x,y) g in (ZMod2 r, g')