Commit | Line | Data |
---|---|---|
7f918cf1 CE |
1 | (* Copyright (C) 1999-2006 Henry Cejtin, Matthew Fluet, Suresh |
2 | * Jagannathan, and Stephen Weeks. | |
3 | * | |
4 | * MLton is released under a BSD-style license. | |
5 | * See the file MLton-LICENSE for details. | |
6 | *) | |
7 | ||
8 | functor RingWithIdentity (S: RING_WITH_IDENTITY_STRUCTS) | |
9 | :> RING_WITH_IDENTITY where type t = S.t = | |
10 | struct | |
11 | ||
12 | open S | |
13 | ||
14 | structure IntInf = Pervasive.IntInf | |
15 | ||
16 | val base = {one = one, layout = layout, times = op *} | |
17 | val pow = Power.power base | |
18 | val powInf = Power.powerInf base | |
19 | val pows = Power.simultaneous base | |
20 | val powsInf = Power.simultaneousInf base | |
21 | ||
22 | local | |
23 | fun 'a | |
24 | make {zero: 'a, < : 'a * 'a -> bool, ~ : 'a -> 'a, | |
25 | power: {one: t, | |
26 | layout: t -> Layout.t, | |
27 | times: t * t -> t | |
28 | } -> (t * 'a) -> t} | |
29 | (i: 'a) : t = | |
30 | let | |
31 | val (i, fix) = | |
32 | if i < zero | |
33 | then (~ i, S.~) | |
34 | else (i, fn x => x) | |
35 | val i = power{one = S.zero, layout = layout, times = op +} (one, i) | |
36 | in fix i | |
37 | end | |
38 | in | |
39 | val fromInt = make{zero = 0, | |
40 | < = op <, | |
41 | ~ = Pervasive.Int.~, | |
42 | power = Power.power} | |
43 | val fromIntInf = make{zero = 0, | |
44 | < = IntInf.<, | |
45 | ~ = IntInf.~, | |
46 | power = Power.powerInf} | |
47 | end | |
48 | ||
49 | (* val fromIntInf = | |
50 | * Trace.trace("fromIntInf", Layout.str o IntInf.toString, layout) fromIntInf | |
51 | *) | |
52 | ||
53 | fun add1 i = i + one | |
54 | ||
55 | fun sub1 i = i - one | |
56 | ||
57 | fun inc r = r := add1(!r) | |
58 | ||
59 | fun dec r = r := sub1(!r) | |
60 | ||
61 | fun prod l = List.fold(l, one, op * ) | |
62 | ||
63 | val negOne = sub1 zero | |
64 | ||
65 | val two = add1 one | |
66 | ||
67 | val three = add1 two | |
68 | ||
69 | val pows = | |
70 | Trace.traceAssert | |
71 | ("RingWithIdentity.pows", | |
72 | List.layout (Layout.tuple2 (layout, Layout.str o Pervasive.Int.toString)), | |
73 | layout, | |
74 | fn l => (true, fn r => equals (r, List.fold (l, one, fn ((b, e), ac) => | |
75 | ac * pow (b, e))))) | |
76 | pows | |
77 | ||
78 | val powsInf = | |
79 | Trace.traceAssert | |
80 | ("RingWithIdentity.powsInf", | |
81 | List.layout (Layout.tuple2 (layout, Layout.str o Pervasive.IntInf.toString)), | |
82 | layout, | |
83 | fn l => (true, fn r => equals (r, List.fold (l, one, fn ((b, e), ac) => | |
84 | ac * powInf (b, e))))) | |
85 | powsInf | |
86 | ||
87 | end |