-
Notifications
You must be signed in to change notification settings - Fork 4
/
Collections.fs
400 lines (372 loc) · 12.7 KB
/
Collections.fs
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
/// <summary>
/// Collections used in Starling.
/// </summary>
module Starling.Collections
open Chessie.ErrorHandling
open Starling.Utils
/// <summary>
/// A function-like construct.
/// </summary>
/// <remarks>
/// <para>
/// A Func is a combination of a string name and list of parameters.
/// It generically represents any pattern <c>Name(p1, p2, .., pn)</c>
/// in Starling.
/// </para>
/// <para>
/// Examples of Func uses in Starling include function signatures and
/// calls, components of <see cref="T:Starling.Model">views</see>, and
/// Horn clause predicates.
/// </para>
/// </remarks>
/// <typeparam name="param">The type of parameters in the Func.</typeparam>
type Func<'param> =
{ /// The name of a Func.
Name : string
/// The parameters of a Func.
Params : 'param list }
override this.ToString () = sprintf "Func: %s(%A)" this.Name this.Params
/// <summary>
/// Creates a new <c>Func</c>.
/// </summary>
/// <parameter name="name">
/// The name of the <c>Func</c>.
/// </parameter>
/// <parameter name="pars">
/// The parameters of the <c>Func</c>, as a sequence.
/// </parameter>
/// <returns>
/// A new <c>Func</c> with the given name and parameters.
/// </returns>
let func (name : string)
(pars : 'param seq)
: Func<'param> =
{ Name = name; Params = List.ofSeq pars }
module Func =
module Pretty =
open Starling.Core
/// Pretty-prints Funcs using pxs to print parameters.
let printFunc pxs { Name = f; Params = xs } = Pretty.func f (Seq.map pxs xs)
/// <summary>
/// A multiset, or ordered list.
/// </summary>
/// <typeparam name="item">
/// The type of items in the Multiset.
/// </typeparam>
type Multiset<'item> when 'item: comparison =
| MSet of Map<'item, int>
override this.ToString() = sprintf "%A" this
/// <summary>
/// Operations on multisets.
/// </summary>
/// <seealso cref="T:Starling.Collections.Multiset`1" />
module Multiset =
(*
* Construction
*)
/// <summary>
/// The empty multiset.
/// </summary>
/// <typeparam name="item">
/// The type of items in the multiset.
/// </typeparam>
let empty : Multiset<'item> = MSet (Map.empty)
/// <summary>
/// Adds an element n times to a multiset
/// </summary>
/// <typeparam name="item">
/// The type of items in the multiset.
/// </typeparam>
let addn (MSet ms : Multiset<'item>)
(k : 'item)
(m : int)
: Multiset<'item> =
let n = ms.TryFind k |> Option.fold (fun _ y -> y) 0
MSet (ms.Add (k, n+m))
/// <summary>
/// Adds an element to a multiset
/// </summary>
let add (ms : Multiset<'item>) (k : 'item) : Multiset<'item> = addn ms k 1
/// <summary>
/// Creates a multiset with the given flat sequence as its contents.
/// </summary>
/// <param name="xs">
/// The flat sequence to use to create the multiset.
/// </param>
/// <typeparam name="item">
/// The type of items in the multiset.
/// </typeparam>
/// <returns>
/// A multiset containing the given items.
/// </returns>
let ofFlatSeq (xs : 'item seq) : Multiset<'item> =
Seq.fold add empty xs
/// <summary>
/// Creates a multiset with the given flat list as its contents.
/// </summary>
/// <param name="xs">
/// The flat list to use to create the multiset.
/// </param>
/// <typeparam name="item">
/// The type of items in the multiset.
/// </typeparam>
/// <returns>
/// A multiset containing the given items.
/// </returns>
let ofFlatList (xs : 'item list) : Multiset<'item> =
xs |> ofFlatSeq
/// <summary>
/// Creates a multiset with one item.
/// </summary>
/// <param name="x">
/// The item to place in the multiset.
/// </param>
/// <typeparam name="item">
/// The type of items in the multiset.
/// </typeparam>
/// <returns>
/// A singleton multiset.
/// </returns>
let singleton (x : 'item) : Multiset<'item> = add empty x
(*
* Destruction
*)
/// <summary>
/// Converts a multiset to a sorted, flattened sequence.
/// </summary>
/// <param name="ms">
/// The multiset to convert.
/// </param>
/// <typeparam name="item">
/// The type of items in the multiset.
/// </typeparam>
/// <returns>
/// The sorted, flattened sequence.
/// </returns>
let toFlatSeq (MSet ms : Multiset<'item>) : 'item seq =
// TODO(CaptainHayashi): this will be removed when itviews land.
ms
|> Map.toSeq
|> Seq.map (fun (k, amount) -> Seq.replicate amount k)
|> Seq.concat
/// <summary>
/// Converts a multiset to a sorted, flattened list.
/// </summary>
/// <param name="ms">
/// The multiset to convert.
/// </param>
/// <typeparam name="item">
/// The type of items in the multiset.
/// </typeparam>
/// <returns>
/// The sorted, flattened list.
/// </returns>
let toFlatList (ms : Multiset<'item>) : 'item list =
// TODO(CaptainHayashi): this will be removed when itviews land.
ms
|> toFlatSeq
|> List.ofSeq
/// <summary>
/// Converts a multiset to a set, removing duplicates.
/// </summary>
/// <param name="ms">
/// The multiset to convert.
/// </param>
/// <typeparam name="item">
/// The type of items in the multiset.
/// </typeparam>
/// <returns>
/// The set of items in the multiset.
/// </returns>
let toSet (MSet ms : Multiset<'item>) : Set<'item> =
// TODO(CaptainHayashi): this _may_ be removed when itviews land,
// as it will be impossible to decide whether, if something is in
// the list an unknown amount of times, that amount > 0.
Map.fold (fun set k _ -> Set.add k set) Set.empty ms
(*
* Operations
*)
/// <summary>
/// Takes the length of a multiset.
/// </summary>
/// <param name="mset">
/// The multiset to measure.
/// </param>
/// <returns>
/// The number of elements in <paramref name="_arg1" />.
/// </returns>
let length (MSet mset : Multiset<_>) : int =
// TODO(CaptainHayashi): this will be removed when itviews land.
mset |> Map.fold (fun count _ n -> count + n) 0
/// <summary>
/// Appends two multisets.
/// </summary>
/// <remarks>
/// Since multisets are ordered, the resulting multiset may not
/// necessarily be <c>xs</c> followed by <c>ys</c>.
/// </remarks>
/// <param name="xs">The first multiset to append.</param>
/// <param name="ys">The second multiset to append.</param>
/// <typeparam name="item">
/// The type of items in the multiset.
/// </typeparam>
/// <returns>
/// The result of appending <c>xs</c> to <c>ys</c>.
/// </returns>
let append (xs : Multiset<'item>)
(MSet ymap : Multiset<'item>)
: Multiset<'item> =
Map.fold addn xs ymap
/// <summary>
/// Folds <c>f</c> over the unique items of a multiset in some
/// arbitrary order.
/// </summary>
/// <param name="f">
/// The function to fold over the multiset. This takes the current
/// state, the item, and the number of times that item
/// appears in the multiset. It should return the new state.
/// </param>
/// <param name="init">
/// The initial value of the state.
/// </param>
/// <typeparam name="State">
/// The type of the accumulator.
/// </typeparam>
/// <typeparam name="Item">
/// The type of items in the multiset.
/// </typeparam>
/// <returns>
/// The final value of the state.
/// </returns>
let fold (f : 'State -> 'Item -> int -> 'State)
(init : 'State)
(MSet ms : Multiset<'Item>)
: 'State =
ms
|> Map.toSeq
|> Seq.fold (fun state (item, num) -> f state item num) init
/// <summary>
/// Maps <c>f</c> over the unique items of a multiset, passing
/// an accumulator in some arbitrary order.
/// </summary>
/// <param name="f">
/// The function to map over the multiset. This takes the
/// accumulator, the item, and the number of times that item
/// appears in the multiset. It should return the new item. It
/// is assumed the number of appearances does not change.
/// </param>
/// <param name="init">
/// The initial value of the accumulator.
/// </param>
/// <typeparam name="acc">
/// The type of the accumulator.
/// </typeparam>
/// <typeparam name="src">
/// The type of variables in the list to map.
/// </typeparam>
/// <typeparam name="dst">
/// The type of variables in the list after mapping.
/// </typeparam>
/// <returns>
/// The pair of the final value of the accumulator, and the
/// result of mapping <c>f</c> over the multiset.
/// </returns>
/// <remarks>
/// Since multisets are ordered, mapping can change the position of
/// items.
/// </remarks>
let mapAccum
(f : 'acc -> 'src -> int -> ('acc * 'dst))
(init : 'acc)
(MSet ms : Multiset<'src>)
: ('acc * Multiset<'dst>) =
// TODO(CaptainHayashi): convert map to a similar abstraction.
ms
|> Map.toList
|> mapAccumL
(fun acc (src, num) ->
let acc', dst = f acc src num
(acc', (dst, num)))
init
|> pairMap id (Map.ofList >> MSet)
/// <summary>
/// Maps <c>f</c> over a multiset.
/// </summary>
/// <remarks>
/// Since multisets are ordered, mapping can change the position of
/// items.
/// </remarks>
/// <param name="f">The function to map over the multiset.</param>
/// <typeparam name="src">
/// The type of variables in the list to map.
/// </typeparam>
/// <typeparam name="dst">
/// The type of variables in the list after mapping.
/// </typeparam>
/// <returns>
/// The result of mapping <c>f</c> over the multiset.
/// </returns>
let map (f : 'src -> 'dst)
(MSet xs : Multiset<'src>)
: Multiset<'dst> =
let rec repeat_add map k n =
match n with
| 0 -> map
| n -> repeat_add (add map (f k)) k (n-1)
//Note that this is used with side-effecting f, so must be called n times for each addition.
Map.fold repeat_add empty xs
/// <summary>
/// Collapses a multiset of results to a result on a multiset.
/// </summary>
/// <param name="_arg1">
/// The multiset to collect.
/// </param>
/// <typeparam name="item">
/// Type of items in the multiset.
/// </typeparam>
/// <typeparam name="err">
/// Type of errors in the result.
/// </typeparam>
/// <returns>
/// A result, containing the collected form of
/// <paramref name="_arg1" />.
/// </returns>
let collect
(MSet ms : Multiset<Result<'item, 'err>>)
: Result<Multiset<'item>, 'err> =
// TODO(CaptainHayashi): unify with map?
let rec itr tos fros warns : Result<Multiset<'item>, 'err> =
match tos with
| [] -> ok (MSet (Map.ofList fros))
| ((Warn (x, ws), n)::xs) -> itr xs ((x, n)::fros) (ws@warns)
| ((Pass x, n)::xs) -> itr xs ((x, n)::fros) warns
| ((Fail e, n)::xs) -> Bad e
itr (Map.toList ms) [] []
/// <summary>
/// Pulls an arbitrary item out of the multiset.
/// </summary>
/// <param name="ms">
/// The multiset.
/// </param>
/// <typeparam name="Item">
/// The type of items in the multiset's lists.
/// </typeparam>
/// <returns>
/// An option: <c>None</c> if the multiset is empty; else
/// <c>Some (i, m, n)</c> where <c>i</c> is an item in the multiset,
/// <c>m</c> is the multiset less <c>i</c>, and
/// <c>n</c> is the number of times it occurs.
/// </returns>
let pop (MSet ms : Multiset<'Item>)
: ('Item * Multiset<'Item> * int) option =
// TODO(CaptainHayashi): tests.
match (Map.toList ms) with
| [] -> None
| (i, n)::xs -> Some (i, MSet (Map.ofList xs), n)
module Pretty =
open Starling.Core.Pretty
/// Pretty-prints a multiset given a printer for its contents.
let printMultiset pItem =
toFlatList
>> List.map pItem
>> semiSep