23956
|
1 |
structure Classes =
|
22317
|
2 |
struct
|
|
3 |
|
24628
|
4 |
datatype nat = Suc of nat | Zero_nat;
|
22317
|
5 |
|
24628
|
6 |
datatype bit = B1 | B0;
|
22813
|
7 |
|
23956
|
8 |
fun nat_aux i n =
|
|
9 |
(if IntInf.<= (i, (0 : IntInf.int)) then n
|
|
10 |
else nat_aux (IntInf.- (i, (1 : IntInf.int))) (Suc n));
|
22813
|
11 |
|
23956
|
12 |
fun nat i = nat_aux i Zero_nat;
|
22317
|
13 |
|
22479
|
14 |
type 'a semigroup = {mult : 'a -> 'a -> 'a};
|
|
15 |
fun mult (A_:'a semigroup) = #mult A_;
|
22317
|
16 |
|
|
17 |
type 'a monoidl =
|
23956
|
18 |
{Classes__semigroup_monoidl : 'a semigroup, neutral : 'a};
|
|
19 |
fun semigroup_monoidl (A_:'a monoidl) = #Classes__semigroup_monoidl A_;
|
22479
|
20 |
fun neutral (A_:'a monoidl) = #neutral A_;
|
22317
|
21 |
|
24991
|
22 |
type 'a monoid = {Classes__monoidl_monoid : 'a monoidl};
|
|
23 |
fun monoidl_monoid (A_:'a monoid) = #Classes__monoidl_monoid A_;
|
|
24 |
|
|
25 |
type 'a group = {Classes__monoid_group : 'a monoid, inverse : 'a -> 'a};
|
|
26 |
fun monoid_group (A_:'a group) = #Classes__monoid_group A_;
|
22479
|
27 |
fun inverse (A_:'a group) = #inverse A_;
|
22317
|
28 |
|
23956
|
29 |
fun inverse_int i = IntInf.~ i;
|
22317
|
30 |
|
23956
|
31 |
val neutral_int : IntInf.int = (0 : IntInf.int);
|
22317
|
32 |
|
23956
|
33 |
fun mult_int i j = IntInf.+ (i, j);
|
22317
|
34 |
|
23956
|
35 |
val semigroup_int = {mult = mult_int} : IntInf.int semigroup;
|
22317
|
36 |
|
|
37 |
val monoidl_int =
|
23956
|
38 |
{Classes__semigroup_monoidl = semigroup_int, neutral = neutral_int} :
|
|
39 |
IntInf.int monoidl;
|
22317
|
40 |
|
24991
|
41 |
val monoid_int = {Classes__monoidl_monoid = monoidl_int} :
|
|
42 |
IntInf.int monoid;
|
|
43 |
|
22317
|
44 |
val group_int =
|
24991
|
45 |
{Classes__monoid_group = monoid_int, inverse = inverse_int} :
|
23956
|
46 |
IntInf.int group;
|
22317
|
47 |
|
24991
|
48 |
fun pow_nat A_ (Suc n) x =
|
|
49 |
mult ((semigroup_monoidl o monoidl_monoid) A_) x (pow_nat A_ n x)
|
|
50 |
| pow_nat A_ Zero_nat x = neutral (monoidl_monoid A_);
|
22317
|
51 |
|
|
52 |
fun pow_int A_ k x =
|
24991
|
53 |
(if IntInf.<= ((0 : IntInf.int), k)
|
|
54 |
then pow_nat (monoid_group A_) (nat k) x
|
|
55 |
else inverse A_ (pow_nat (monoid_group A_) (nat (IntInf.~ k)) x));
|
22317
|
56 |
|
23956
|
57 |
val example : IntInf.int =
|
|
58 |
pow_int group_int (10 : IntInf.int) (~2 : IntInf.int);
|
22317
|
59 |
|
|
60 |
end; (*struct Classes*)
|