22317
|
1 |
module Classes where {
|
|
2 |
|
23956
|
3 |
|
25200
|
4 |
data Nat = Suc Nat | Zero_nat;
|
23956
|
5 |
|
24628
|
6 |
data Bit = B1 | B0;
|
23956
|
7 |
|
25200
|
8 |
nat_aux :: Integer -> Nat -> Nat;
|
|
9 |
nat_aux i n = (if i <= 0 then n else nat_aux (i - 1) (Suc n));
|
23956
|
10 |
|
25200
|
11 |
nat :: Integer -> Nat;
|
|
12 |
nat i = nat_aux i Zero_nat;
|
22317
|
13 |
|
|
14 |
class Semigroup a where {
|
|
15 |
mult :: a -> a -> a;
|
|
16 |
};
|
|
17 |
|
25200
|
18 |
class (Semigroup a) => Monoidl a where {
|
22317
|
19 |
neutral :: a;
|
|
20 |
};
|
|
21 |
|
25200
|
22 |
class (Monoidl a) => Monoid a where {
|
24991
|
23 |
};
|
|
24 |
|
25200
|
25 |
class (Monoid a) => Group a where {
|
22317
|
26 |
inverse :: a -> a;
|
|
27 |
};
|
|
28 |
|
23956
|
29 |
inverse_int :: Integer -> Integer;
|
|
30 |
inverse_int i = negate i;
|
22317
|
31 |
|
23956
|
32 |
neutral_int :: Integer;
|
|
33 |
neutral_int = 0;
|
22317
|
34 |
|
23956
|
35 |
mult_int :: Integer -> Integer -> Integer;
|
|
36 |
mult_int i j = i + j;
|
22317
|
37 |
|
25200
|
38 |
instance Semigroup Integer where {
|
|
39 |
mult = mult_int;
|
22317
|
40 |
};
|
|
41 |
|
25200
|
42 |
instance Monoidl Integer where {
|
|
43 |
neutral = neutral_int;
|
22317
|
44 |
};
|
|
45 |
|
25200
|
46 |
instance Monoid Integer where {
|
24991
|
47 |
};
|
|
48 |
|
25200
|
49 |
instance Group Integer where {
|
|
50 |
inverse = inverse_int;
|
23956
|
51 |
};
|
22317
|
52 |
|
25200
|
53 |
pow_nat :: (Monoid a) => Nat -> a -> a;
|
|
54 |
pow_nat (Suc n) x = mult x (pow_nat n x);
|
|
55 |
pow_nat Zero_nat x = neutral;
|
22317
|
56 |
|
25200
|
57 |
pow_int :: (Group a) => Integer -> a -> a;
|
23956
|
58 |
pow_int k x =
|
25200
|
59 |
(if 0 <= k then pow_nat (nat k) x
|
|
60 |
else inverse (pow_nat (nat (negate k)) x));
|
23956
|
61 |
|
|
62 |
example :: Integer;
|
25200
|
63 |
example = pow_int 10 (-2);
|
22317
|
64 |
|
|
65 |
}
|