-
Notifications
You must be signed in to change notification settings - Fork 2
/
mono-sequence.sml
475 lines (417 loc) · 14.9 KB
/
mono-sequence.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
(*
MonoSequence
Abstract Signatures for Integer-Addressable Monomorphic Collections
Copyright 2011 Christopher Cramer
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
*)
signature MONO_SEQUENCE = sig
type sequence
type element
type array
type arraySlice
type vector
type vectorSlice
(*
isEmpty t returns true if t is empty, and false if
t is not empty.
*)
val isEmpty: sequence -> bool
(*
length t returns the number of elements in t.
If t does not have a finite length, then it raises
InfiniteLength.
*)
val length: sequence -> int
(*
hd t returns the element at the head of the sequence.
It raises Empty if t is empty.
*)
val hd: sequence -> element
(*
sub (t, i) returns the ith element of t. It
raises Subscript if the end of the sequence is
reached before the ith element.
*)
val sub: sequence * int -> element
(*
appl f t applies f to every element of t, from
the first to the last element.
app is equivalent to appl.
*)
val appl: (element -> unit) -> sequence -> unit
val app: (element -> unit) -> sequence -> unit
(*
appli f t applies f to a pair of the index of each
element of t, along with the element itself, from the
first to the last.
appi is equivalent to appli.
*)
val appli: (int * element -> unit) -> sequence -> unit
val appi: (int * element -> unit) -> sequence -> unit
(*
appr f t applies f to every element of t, from
the last to the first element.
*)
val appr: (element -> unit) -> sequence -> unit
(*
appri f t applies f to a pair of the index of each
element of t, along with the element itself, from the
last to the first.
*)
val appri: (int * element -> unit) -> sequence -> unit
(*
foldl f b t returns f (xn, ... f (x1, f (x0, b)) ...),
where x0, x1, ... xn are the elements of t, or simply b
if the sequence is empty.
fold is equivalent to foldl.
*)
val foldl: (element * 'a -> 'a) -> 'a -> sequence -> 'a
val fold: (element * 'a -> 'a) -> 'a -> sequence -> 'a
(*
foldli f b t returns (f (n, xn, ... f (1, x1, f (0, x0, b)) ...),
where x0, x1, ... xn are the elements of t, or simply b
if the sequence is empty.
foldi is equivalent to foldli.
*)
val foldli: (int * element * 'a -> 'a) -> 'a -> sequence -> 'a
val foldi: (int * element * 'a -> 'a) -> 'a -> sequence -> 'a
val foldr: (element * 'a -> 'a) -> 'a -> sequence -> 'a
val foldri: (int * element * 'a -> 'a) -> 'a -> sequence -> 'a
(*
reducel f t returns f (xn, ... f (x2, f (x1, x0)) ...),
where x0, x1, x2, ... xn are the elements of t, or if t
contains a single element, simply returns that element.
If t is empty, it raises Empty.
reduce is equivalent to reducel.
*)
val reducel: (element * element -> element) -> sequence -> element
val reduce: (element * element -> element) -> sequence -> element
(*
reducer f t returns f (x0, f (x1, ... f (xn-1, xn) ...)),
where x0, x1, ... xn-1, xn are the elements of t, of if t
contains a single element, simply returns that element.
If t is empty it raises Empty.
*)
val reducer: (element * element -> element) -> sequence -> element
(*
findl f t returns SOME x, in which x is the
first element which, applied to f, results in true.
If no element results in true, it returns NONE.
find is equivalent to findl.
*)
val findl: (element -> bool) -> sequence -> element option
val find: (element -> bool) -> sequence -> element option
(*
findli f t returns SOME (i, x), in which i is the
index of the first element which, applied to f, results
in true, and x is the element itself. If no element
results in true, it returns NONE.
findi is equivalent to findli.
*)
val findli: (int * element -> bool) -> sequence -> (int * element) option
val findi: (int * element -> bool) -> sequence -> (int * element) option
(*
findr f t returns SOME x, in which x is the
last element which, applied to f, results in true.
If no element results in true, it returns NONE.
*)
val findr: (element -> bool) -> sequence -> element option
(*
findri f t returns SOME (i, x), in which i is the
index of the last element which, applied to f, results
in true, and x is the element itself. If no element
results in true, it returns NONE.
*)
val findri: (int * element -> bool) -> sequence -> (int * element) option
(*
exists f t returns true on the first element which, when
applied to f, yields true, and returns false if no such
element is found.
*)
val exists: (element -> bool) -> sequence -> bool
(*
all f t returns false on the first element which, when
applied to f, yields false, and returns true if no such
element is found.
*)
val all: (element -> bool) -> sequence -> bool
(*
collate f (t, u) compares t and u lexicographically,
using f to compare elements of each, and returns LESS if
t is less than u, EQUAL if t is equal to u, and GREATER
if t is greater than u.
*)
val collate: (element * element -> order) -> sequence * sequence -> order
(*
Convert to another sequence type. The functions with rev
reverse before conversion.
*)
val toShadow: sequence -> element Shadow.t
val revToShadow: sequence -> element Shadow.t
val toList: sequence -> element list
val revToList: sequence -> element list
val toArray: sequence -> array
val revToArray: sequence -> array
val toPolyArray: sequence -> element Array.array
val revToPolyArray: sequence -> element Array.array
val toVector: sequence -> vector
val revToVector: sequence -> vector
val toPolyVector: sequence -> element Vector.vector
val revToPolyVector: sequence -> element Vector.vector
end
signature CREATEABLE_MONO_SEQUENCE = sig
include MONO_SEQUENCE
val maxLen: int
(*
empty () returns an empty sequence.
*)
val empty: unit -> sequence
(*
single x returns a sequenge which contains a single
element of x.
*)
val single: element -> sequence
(*
unfold f a0 creates a sequence b0, b1, ... bn, through
a process where f a0 yields SOME (b0, a1), f a1 yields
SOME (b1, a2), etc. until f an yields SOME (bn, an+1)
and f an+1 yields NONE.
*)
val unfold: ('a -> (element * 'a) option) -> 'a -> sequence
(*
unfoldn f (n, a0) creates a sequence b0, b1, ... bn,
through a process where f a0 yields (b0, a1), f a1
yields (b1, a2), etc. until the sequence reaches length
n.
*)
val unfoldn: ('a -> element * 'a) -> int * 'a -> sequence
(*
tabulate (n, f) creates a sequence of length n, where
the elements are f 0, f 1, ... f (n - 1).
*)
val tabulate: int * (int -> element) -> sequence
(*
split f t returns two subsequences: the first consists
all the elements from at the beginning of t
that satisfy the predicate f, and the second consists of
the remaining elements.
*)
val split: (element -> bool) -> sequence -> sequence * sequence
(*
splitAt (t, n) returns two subsequences: the first consists
of the first n elements of t, and the second consists of
the remaining elements.
*)
val splitAt: sequence * int -> sequence * sequence
(*
drop f t returns the sequence remaining after dropping the
elements from the beginning of t that, when applied
to f, return true.
*)
val drop: (element -> bool) -> sequence -> sequence
(*
trim (t, n) returns the sequence remaining after trimming
n elements from the beginning of t.
*)
val trim: sequence * int -> sequence
(*
limit (t, n) returns a sequence limited to the first
n elements of t.
*)
val limit: sequence * int -> sequence
(*
take f t returns a sequence consisting of the elements
from the beginning of t that, when applied to f, yield
true.
*)
val take: (element -> bool) -> sequence -> sequence
(*
tokens f t returns a list of subsequences separated
by elements that, when applied to f, yield true. Multiple
adjacent separating elements are treated as a single separator,
thus there are no empty subsequences in the result.
*)
val tokens: (element -> bool) -> sequence -> sequence list
(*
fields f t returns a list of subsequences separated
by elements that, when applied to f, yield true. Multiple
adjacent separating elements are treated as separating
empty subsequences, contrary to tokens above.
*)
val fields: (element -> bool) -> sequence -> sequence list
(*
translate f t returns a sequence of the concatenation
of the results of f applied to each element of t.
*)
val translate: (element -> sequence) -> sequence -> sequence
(*
extract (t, i, NONE) returns a subsequence of t,
starting at the ith element and continuing to the end.
extract (t, i, SOME n) returns a subsequence of t,
starting at the ith element and continuing for n elements.
*)
val extract: sequence * int * int option -> sequence
(*
insert (t, i, x) returns a new sequence, with
x inserted at position i. The elements from
position i to the end of the sequence are
shifted to the right.
*)
val insert: sequence * int * element -> sequence
(*
delete (t, i) returns a new sequence, with the
the ith element in t removed.
*)
val delete: sequence * int -> sequence
(*
These convert from another data structure to this
type of sequence. The ones with Rev in the name
reverse while converting.
*)
val fromShadow: element Shadow.t -> sequence
val fromRevShadow: element Shadow.t -> sequence
val fromArray: array -> sequence
val fromRevArray: array -> sequence
val fromArraySlice: arraySlice -> sequence
val fromRevArraySlice: arraySlice -> sequence
val fromPolyArray: element Array.array -> sequence
val fromRevPolyArray: element Array.array -> sequence
val fromPolyArraySlice: element ArraySlice.slice -> sequence
val fromRevPolyArraySlice: element ArraySlice.slice -> sequence
val fromVector: vector -> sequence
val fromRevVector: vector -> sequence
val fromVectorSlice: vectorSlice -> sequence
val fromRevVectorSlice: vectorSlice -> sequence
val fromPolyVector: element Vector.vector -> sequence
val fromRevPolyVector: element Vector.vector -> sequence
val fromPolyVectorSlice: element VectorSlice.slice -> sequence
val fromRevPolyVectorSlice: element VectorSlice.slice -> sequence
val fromList: element list -> sequence
(*
map f t returns a sequence of the results of applying f to
each element of t.
*)
val map: (element -> element) -> sequence -> sequence
(*
mapi f t returns a sequence of the results of applying f
to a pair of the index of each element of t, along with the element
itself.
*)
val mapi: (int * element -> element) -> sequence -> sequence
(*
getItem t returns NONE if t is empty, or SOME (x, u)
in which x is the next element and u is a sequence
containing the remaining elements.
*)
val getItem: sequence -> (element * sequence) option
(*
ungetItem (t, x) returns a sequence in which x is
the first element, and t makes up the remaining
elements.
*)
val ungetItem: sequence * element -> sequence
(*
tl t returns the sequence consisting of all but the
first element of t. It raises Empty if t is empty.
*)
val tl: sequence -> sequence
(*
append (t, u) returns a sequence of the elements
of t, followed by the elements of u.
*)
val append: sequence * sequence -> sequence
(*
filter f t returns a sequence of only the elements
of t in which f returns true.
*)
val filter: (element -> bool) -> sequence -> sequence
(*
mapPartial f t returns a sequence of only the elements
where the application of f returns SOME, with the SOME
stripped out.
*)
val mapPartial: (element -> element option) -> sequence -> sequence
(*
partition f t returns a pair of sequences. The first
sequence in the pair consists of elements which, when
applied to f, yield true, and the second sequence
consists of the the elements which, when applied to f,
yield false.
*)
val partition: (element -> bool) -> sequence -> sequence * sequence
(*
rev t returns a sequence with the elements of t
in reverse order.
*)
val rev: sequence -> sequence
(*
concat l returns a sequence that is a concatentation of
all the sequences in l.
*)
val concat: sequence list -> sequence
(*
concatWith t l returns a sequence that is a concatentation
of all the sequences in l, with t inserted in between them.
*)
val concatWith: sequence -> sequence list -> sequence
end
signature PURE_MONO_SEQUENCE = sig
type pureSequence
type pureElement
(*
update (t, i, x) returns a new sequence, with
the ith element in t replaced by x.
*)
val update: pureSequence * int * pureElement -> pureSequence
end
signature IMPURE_MONO_SEQUENCE = sig
type impureSequence
type impureElement
type impureVector
type impureVectorSlice
type impureArray
type impureArraySlice
val update: impureSequence * int * impureElement -> unit
val modify: (impureElement -> impureElement) -> impureSequence -> unit
val modifyi: (int * impureElement -> impureElement) -> impureSequence -> unit
val copy: {src: impureSequence, dst: impureSequence, di: int} -> unit
val copyShadow: {src: impureElement Shadow.t, dst: impureSequence, di: int} -> unit
val copyRevShadow: {src: impureElement Shadow.t, dst: impureSequence, di: int} -> unit
val copyArray: {src: impureArray, dst: impureSequence, di: int} -> unit
val copyRevArray: {src: impureArray, dst: impureSequence, di: int} -> unit
val copyArraySlice: {src: impureArraySlice, dst: impureSequence, di: int} -> unit
val copyRevArraySlice: {src: impureArraySlice, dst: impureSequence, di: int} -> unit
val copyPolyArray: {src: impureElement Array.array, dst: impureSequence, di: int} -> unit
val copyRevPolyArray: {src: impureElement Array.array, dst: impureSequence, di: int} -> unit
val copyPolyArraySlice: {src: impureElement ArraySlice.slice, dst: impureSequence, di: int}
-> unit
val copyRevPolyArraySlice: {
src: impureElement ArraySlice.slice
, dst: impureSequence, di: int
} -> unit
val copyVector: {src: impureVector, dst: impureSequence, di: int} -> unit
val copyVec: {src: impureVector, dst: impureSequence, di: int} -> unit
val copyRevVector: {src: impureVector, dst: impureSequence, di: int} -> unit
val copyVectorSlice: {src: impureVectorSlice, dst: impureSequence, di: int} -> unit
val copyRevVectorSlice: {src: impureVectorSlice, dst: impureSequence, di: int} -> unit
val copyPolyVector: {src: impureElement Vector.vector, dst: impureSequence, di: int} -> unit
val copyRevPolyVector: {src: impureElement Vector.vector, dst: impureSequence, di: int}
-> unit
val copyPolyVectorSlice: {src: impureElement VectorSlice.slice, dst: impureSequence, di: int}
-> unit
val copyRevPolyVectorSlice: {
src: impureElement VectorSlice.slice
, dst: impureSequence
, di: int
} -> unit
val copyList: {src: impureElement list, dst: impureSequence, di: int} -> unit
end