-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAlgebraicStructure.hs
More file actions
80 lines (62 loc) · 1.83 KB
/
Copy pathAlgebraicStructure.hs
File metadata and controls
80 lines (62 loc) · 1.83 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
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
{-
Algebraic Structures!!!! Yaaaay!!!
We don't enforce the algebraic laws, just enjoying
-}
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
module AlgebraicStructure where
import qualified Prelude as Prelude ((+),(-),(*),negate,fromInteger)
import Prelude hiding ((+),(-),(*),negate,fromInteger,sum,product,Num)
-- (+) is commutative and associative
-- a-a=zero
-- a + 0 = a
class AbelianGroup g where
(+) :: g -> g -> g
(-) :: g -> g -> g
a - b = a + (negate b)
negate :: g -> g
negate a = zero - a
zero :: g
sum :: [g] -> g
sum = foldr (+) zero
-- (*) is associative, not nessesarily commutative
-- 1 is a unit on both sides
class Monoid m where
(*) :: m -> m -> m
one :: m
product :: [m] -> m
product = foldr (*) one
-- monoid with inverse is group
class (Monoid g) => NonAbelianGroup g where
inv :: g -> g
-- if (*) distributes over (+), we have a ring
class (AbelianGroup r,Monoid r) => Ring r where
fromInteger :: Integer -> r
fromInteger 0 = zero
fromInteger 1 = one
fromInteger n
| n < 0 = negate $ fromInteger $ negate n
| n > 0 = let no2 = fromInteger $ n `div` 2
in no2 + no2 + (fromInteger $ n`mod`2)
-- A module over a ring
class (Monoid r,AbelianGroup m) => Module m r where
(*>) :: r -> m -> m
-- needs no extra structure, just is what it is
class (Monoid r,Ring m, Module m r) => Algebra m r
class (Ring f) => Field f where
(/) :: f -> f -> f
-- a Ring is a Module over itself
-- I can't believe this compiles!!!
instance (Ring r) => Module r r where
(*>) = (*)
------------------------
--Integers are things --
------------------------
instance AbelianGroup Integer where
(+) = (Prelude.+)
(-) = (Prelude.-)
zero = 0
instance Monoid Integer where
one = 1
(*) = (Prelude.*)
instance Ring Integer where
fromInteger = Prelude.fromInteger