-
Notifications
You must be signed in to change notification settings - Fork 0
/
mler.10.sml
300 lines (256 loc) · 5.69 KB
/
mler.10.sml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
fun eq_int (n:int, m:int):bool = (n = m);
datatype num =
Zero
| One_more_than of num
exception Too_small
signature N =
sig
type number
exception Too_small
val succ:number -> number
val pred:number -> number
val is_zero:number -> bool
end
functor NumberAsNum ()
:>
N
=
struct
datatype num =
Zero
| One_more_than of num
type number = num
exception Too_small
fun succ (n)
= One_more_than (n)
fun pred (Zero)
= raise Too_small
| pred (One_more_than (n))
= n
fun is_zero (Zero)
= true
| is_zero (_)
= false
end
functor NumberAsInt ()
:>
N
=
struct
type number = int
exception Too_small
fun succ (n)
= n+1
fun pred (0)
= raise Too_small
| pred (n)
= n-1
fun is_zero (0)
= true
| is_zero (_)
= false
end;
structure IntStruct = NumberAsInt ();
structure NumStruct = NumberAsNum ();
signature P =
sig
type number
val plus: (number * number) -> number
end
functor PON (structure a_N: N)
:>
P
=
struct
type number = a_N.number
fun plus (n, m)
= if a_N.is_zero (n)
then m
else a_N.succ (plus (a_N.pred (n), m))
end;
structure IntArith = PON (structure a_N = IntStruct);
(* IntArith.plus (1,2) *)
signature N_C_R =
sig
type number
exception Too_small
val conceal:int -> number
val succ:number -> number
val pred:number -> number
val is_zero:number -> bool
val reveal:number -> int
end
functor NumberAsNum ()
:>
N_C_R
=
struct
datatype num =
Zero
| One_more_than of num
type number = num
exception Too_small
fun conceal (n)
= if eq_int (n, 0)
then Zero
else One_more_than (conceal (n-1))
fun succ (n)
= One_more_than (n)
fun pred (Zero)
= raise Too_small
| pred (One_more_than (n))
= n
fun is_zero (Zero)
= true
| is_zero (_)
= false
fun reveal (n)
= if is_zero (n)
then 0
else 1+reveal (pred (n))
end
functor NumberAsInt ()
:>
N_C_R
=
struct
type number = int
exception Too_small
fun conceal (n)
= n
fun succ (n)
= n+1
fun pred (0)
= raise Too_small
| pred (n)
= n-1
fun is_zero (0)
= true
| is_zero (_)
= false
fun reveal (n)
= n
end;
structure IntStruct = NumberAsInt ();
structure NumStruct = NumberAsNum ();
structure IntArith = PON (structure a_N = IntStruct);
structure NumArith = PON (structure a_N = NumStruct);
(* IntArith.plus (1,2) *)
NumStruct.conceal (0);
NumStruct.succ(NumStruct.conceal (0));
NumStruct.reveal(NumStruct.succ(NumStruct.conceal (0)));
NumStruct.reveal(NumStruct.succ(
NumStruct.succ(
NumStruct.succ(
NumStruct.succ(
NumStruct.conceal (0))))));
(* NumStruct.reveal(NumArith.plus(NumStruct.conceal(1), *)
(* NumStruct.conceal (0))); *)
functor PON (structure a_N: N)
:>
P where type number = a_N.number
=
struct
type number = a_N.number
fun plus (n, m)
= if a_N.is_zero (n)
then m
else a_N.succ (plus (a_N.pred (n), m))
end;
structure IntStruct = NumberAsInt ();
structure NumStruct = NumberAsNum ();
structure IntArith = PON (structure a_N = IntStruct);
structure NumArith = PON (structure a_N = NumStruct);
NumStruct.reveal(NumArith.plus(NumStruct.conceal(1),
NumStruct.conceal (2)));
IntStruct.reveal(IntArith.plus(IntStruct.conceal(1),
IntStruct.conceal (2)));
functor NumberAsInt2 ()
:>
N where type number = int
=
struct
type number = int
exception Too_small
fun succ (n)
= n+1
fun pred (0)
= raise Too_small
| pred (n)
= n-1
fun is_zero (0)
= true
| is_zero (_)
= false
end;
structure IntStruct2 = NumberAsInt2 ();
structure IntArith2 = PON (structure a_N = IntStruct2);
IntArith2.plus (1, 2);
signature S =
sig
type number1
type number2
val similar: (number1 * number2) -> bool
end
functor Same (structure a_N: N
structure b_N: N)
:>
S where type number1 = a_N.number
where type number2 = b_N.number
=
struct
type number1 = a_N.number
type number2 = b_N.number
fun sim (n, m)
= if a_N.is_zero (n)
then b_N.is_zero (m)
else sim (a_N.pred (n),
b_N.pred (m))
fun similar (n,m)
= ((sim (n,m)
handle
a_N.Too_small => false)
handle
b_N.Too_small => false)
end
structure SimIntNum = Same (structure a_N = IntStruct
structure b_N = NumStruct)
structure SimNumInt = Same (structure a_N = NumStruct
structure b_N = IntStruct);
SimNumInt.similar (NumStruct.conceal (0), IntStruct.conceal (0));
SimNumInt.similar (NumStruct.conceal (0), IntStruct.conceal (1));
SimIntNum.similar (IntStruct.conceal (0), NumStruct.conceal (0));
SimIntNum.similar (IntStruct.conceal (0), NumStruct.conceal (1));
structure SimNumNum = Same (structure a_N = NumStruct
structure b_N = NumStruct);
SimNumNum.similar (NumStruct.conceal (0), NumStruct.conceal (0));
SimNumNum.similar (NumStruct.conceal (0), NumStruct.conceal (1));
fun new_plus (x, y)
= NumStruct.reveal (
NumArith.plus (
NumStruct.conceal (x),
NumStruct.conceal (y)));
new_plus (1, 2);
signature J =
sig
val new_plus: (int * int) -> int
end;
functor NP (structure a_N: N_C_R
structure a_P: P
sharing type
a_N.number
=
a_P.number)
:>
J
=
struct
fun new_plus (x, y)
= a_N.reveal (
a_P.plus (
a_N.conceal (x),
a_N.conceal (y)))
end;
structure NPStruct = NP (
structure a_N = NumberAsNum ()
structure a_P = PON (structure a_N = a_N));