This repository was archived by the owner on Nov 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAlgebra.hs
142 lines (121 loc) · 3.42 KB
/
Algebra.hs
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
module Algebra where
infixl 6 +. , -.
infixl 7 *. , /.
--
-- (x::A)->B is dependant functions
-- (x = y) A is equality in type A
--
-- For simplicity we may require decidable equality on the elements.
class {-(Eq a) =>-} SemiGroup a where
(+.) :: a->a->a
-- assocAdd :: (x::a)->(y::a)->(z::a)->
-- ((a+.b)+.c = a+.(b+.c)) a
class (SemiGroup a) => Monoid a where
zero :: a
-- leftZero :: (x::a) -> (zero +. x = x) a
class (Monoid a) => Group a where
neg :: a->a
(-.) :: a->a->a
x -. y = x +. neg y
-- leftNeg :: (x::a) -> (neg x +. x = zero) a
class (Group a) => AbelianGroup a
-- commAdd :: (x::a)->(y::a)-> (x+.y = y+.x) a
class (AbelianGroup a) => Ring a where
(*.) :: a->a->a
-- assocMul :: (x::a)->(y::a)->(z::a)->
-- ((a*.b)*.c = a*.(b*.c)) a
-- distrRingL :: (x::a)->(y::a)->(z::a)->
-- (x*.(y+.z) = x*.y +. x*.z)
-- distrRingR :: (x::a)->(y::a)->(z::a)->
-- ((y+.z)*.x = y*.x +. z*.x)
class (Ring a) => UnityRing a where
one :: a
-- leftOne :: (x::a)->(one *. x = x) a
-- rightOne :: (x::a)->(x *. one = x) a
class (Ring a) => CommutativeRing a
-- commMul :: (x::a)->(y::a)-> (x*.y = y*.x) a
class (CommutativeRing a, UnityRing a) => IntegralDomain a
-- noZeroDiv :: (x::a)->(y::a)-> ( (x*.y = zero) a -> Either ((x=zero) a) ((y=zero) a) )
class (UnityRing a) => DivisionRing a where
inv :: a->a
(/.) :: a->a->a
x /. y = x *. inv y
-- leftinv :: (x::a) -> (inv x *. x = one) a
class (DivisionRing a, CommutativeRing a) => Field a
-- Every finite integral domain is a field.
-- Unique Factorization Domain
class (IntegralDomain a) => UFD a
-- every non-zero element has a unique factorization
-- Principal Ideal Domain
class (IntegralDomain a) => PID a
-- every ideal is a principal ideal
---------------------------------------------------
-- [a] --
instance SemiGroup [a] where
(+.) = (++)
instance Monoid [a] where
zero = []
-- Bool --
instance SemiGroup Bool where
(+.) = (||)
instance Monoid Bool where
zero = False
instance Group Bool where
neg = not
instance AbelianGroup Bool
instance Ring Bool where
(*.) = (&&)
instance CommutativeRing Bool
instance UnityRing Bool where
one = True
instance DivisionRing Bool where
inv x = x
-- Int --
instance SemiGroup Int where
(+.) = (+)
instance Monoid Int where
zero = 0
instance Group Int where
neg = negate
instance AbelianGroup Int
instance Ring Int where
(*.) = (*)
instance CommutativeRing Int
instance UnityRing Int where
one = 1
-- Integer --
instance SemiGroup Integer where
(+.) = (+)
instance Monoid Integer where
zero = 0
instance Group Integer where
neg = negate
instance AbelianGroup Integer
instance Ring Integer where
(*.) = (*)
instance CommutativeRing Integer
instance UnityRing Integer where
one = 1
instance IntegralDomain Integer
-- Q --
-- A new data tupe is needed to do the instance declarations
data Q = Q Rational {-#STRICT#-} deriving (Eq, Ord)
instance Text Q where
showsType _ = showString "Q"
showsPrec n (Q p) = showsPrec n p
instance SemiGroup Q where
Q a +. Q b = Q (a+b)
instance Monoid Q where
zero = Q 0
instance Group Q where
neg (Q a) = Q (-a)
instance AbelianGroup Q
instance Ring Q where
Q a *. Q b = Q (a*b)
instance CommutativeRing Q
instance UnityRing Q where
one = Q 1
instance IntegralDomain Q
instance DivisionRing Q where
inv (Q x) = Q (recip x)
instance Field Q