-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFieldGF3.icl
49 lines (35 loc) · 1.2 KB
/
FieldGF3.icl
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
38
39
40
41
42
43
44
45
46
47
48
49
implementation module FieldGF3
import StdBool
import StdInt
import StdList
import StdMisc
import StdOverloaded
import StdGeneric
import StdEnv
import Gast
:: FieldGF3 = FieldGF3 Int
normalize :: FieldGF3 -> FieldGF3
normalize (FieldGF3 a) = FieldGF3 (((a rem 3) + 3) rem 3)
instance == FieldGF3 where
(==) (FieldGF3 a) (FieldGF3 b) = ((a + 3) rem 3) == ((b + 3) rem 3)
instance + FieldGF3 where
(+) (FieldGF3 a) (FieldGF3 b) = FieldGF3 ((a + b + 3) rem 3)
instance - FieldGF3 where
(-) (FieldGF3 a) (FieldGF3 b) = FieldGF3 ((a - b + 3) rem 3)
instance * FieldGF3 where
(*) (FieldGF3 a) (FieldGF3 b) = FieldGF3 ((a * b) rem 3)
instance / FieldGF3 where
(/) _ (FieldGF3 0) = abort "division by 0"
(/) a b = FieldGF3 (3 - ((2*a`/b`) rem 3))
where (FieldGF3 a`) = normalize a
(FieldGF3 b`) = normalize b
instance ~ FieldGF3 where
~ (FieldGF3 a) = FieldGF3 ((3 - a) rem 3)
instance fromInt FieldGF3 where
fromInt i = FieldGF3 (((i rem 3) + 3) rem 3)
instance toReal FieldGF3 where
toReal (FieldGF3 a) = toReal a
instance toString FieldGF3 where
toString a = toString a`
where (FieldGF3 a`) = normalize a
ggen{|FieldGF3|} state = map fromInt [0, 1, 2]