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