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