-
Notifications
You must be signed in to change notification settings - Fork 37
/
Combinators.fs
394 lines (341 loc) · 17.3 KB
/
Combinators.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
namespace TypeShape
open System
open System.Collections.Generic
open System.Threading
open TypeShape.Core
open TypeShape.Core.Utils
open TypeShape.Core.SubtypeExtensions
module private GIterator =
type Iterator<'Element, 'Graph> = Context<'Element> -> 'Graph -> unit
and Context<'Element> =
{
Token : CancellationToken
Stack : ObjectStack
Action : 'Element -> unit
}
let cache = new TypeCache()
let rec mkIterator<'E, 'T> () : Iterator<'E,'T> =
let mutable f = Unchecked.defaultof<Iterator<'E,'T>>
if cache.TryGetValue<Iterator<'E,'T>>(&f) then f else
use ctx = cache.CreateGenerationContext()
mkIteratorCached<'E, 'T> ctx
and mkIteratorCached<'E, 'T> (ctx : TypeGenerationContext) : Iterator<'E,'T> =
match ctx.InitOrGetCachedValue<Iterator<'E,'T>> (fun c s t -> c.Value s t) with
| Cached(value = v) -> v
| NotCached t ->
let iterator = mkIteratorImpl<'E, 'T> ctx
ctx.Commit t iterator
and mkIteratorImpl<'E, 'T> (ctx : TypeGenerationContext) : Iterator<'E, 'T> =
let inline wrap (iterator:Iterator<'E,'a>) : Iterator<'E,'T> =
if typeof<'a>.IsValueType then
fun c t -> if not c.Token.IsCancellationRequested then iterator c t
else
fun c t ->
if not c.Token.IsCancellationRequested then
match t :> obj with
| null -> ()
| o ->
let s = c.Stack
s.Push o
if not s.IsCycle then
iterator c t
s.Pop()
|> unbox
let mkMemberIter (shape : IShapeReadOnlyMember<'T>) =
shape.Accept { new IReadOnlyMemberVisitor<'T, Iterator<'E,'T>> with
member _.Visit (shape : ReadOnlyMember<'T, 'F>) =
let fIter = mkIteratorCached<'E, 'F> ctx
fun c t -> shape.Get t |> fIter c }
match shapeof<'T> :> TypeShape with
| :? TypeShape<'E> -> wrap(fun c (t:'E) -> c.Action t)
| Shape.SubtypeOf (shapeof<'E>) s ->
s.Accept { new ISubtypeWitnessVisitor<'E, Iterator<'E,'T>> with
member _.Visit(witness) =
wrap(fun c t -> c.Action (witness.Upcast t))
}
| Shape.Enum s ->
s.Accept { new IEnumVisitor<Iterator<'E,'T>> with
member _.Visit<'t, 'u when 't : enum<'u>
and 't : struct
and 't :> ValueType
and 't : (new : unit -> 't)>() =
let ui = mkIteratorCached<'E,'u> ctx
fun c (t:'t) ->
let u = LanguagePrimitives.EnumToValue t
ui c u
|> wrap }
| Shape.Nullable s ->
s.Accept { new INullableVisitor<Iterator<'E,'T>> with
member _.Visit<'t when 't : struct
and 't :> ValueType
and 't : (new : unit -> 't)>() =
let ti = mkIteratorCached<'E,'t> ctx
wrap(fun c (n:Nullable<'t>) -> if n.HasValue then ti c n.Value) }
| Shape.FSharpOption s ->
s.Element.Accept {
new ITypeVisitor<Iterator<'E,'T>> with
member _.Visit<'t>() =
let ei = mkIteratorCached<'E, 't> ctx
fun c tOpt ->
match tOpt with
| None -> ()
| Some t -> ei c t
|> wrap }
| Shape.Array s ->
s.Element.Accept {
new ITypeVisitor<Iterator<'E,'T>> with
member _.Visit<'t> () =
let ei = mkIteratorCached<'E, 't> ctx
match s.Rank with
| 1 -> wrap(fun c (ts : 't[]) -> for t in ts do ei c t)
| 2 -> wrap(fun c (ts : 't[,]) -> ts |> Array2D.iter (fun t -> ei c t))
| 3 -> wrap(fun c (ts : 't[,,]) -> ts |> Array3D.iter (fun t -> ei c t))
| rk -> failwithf "Rank-%d arrays not supported" rk
}
| Shape.FSharpList s ->
s.Element.Accept {
new ITypeVisitor<Iterator<'E,'T>> with
member _.Visit<'t>() =
let ei = mkIteratorCached<'E, 't> ctx
wrap(fun c (ts : 't list) -> for t in ts do ei c t)
}
| Shape.FSharpSet s ->
s.Accept {
new IFSharpSetVisitor<Iterator<'E,'T>> with
member _.Visit<'t when 't : comparison> () =
let ei = mkIteratorCached<'E, 't> ctx
wrap(fun c (ts:Set<'t>) -> for t in ts do ei c t)
}
| Shape.Tuple (:? ShapeTuple<'T> as shape) ->
let eIters = shape.Elements |> Array.map mkMemberIter
fun c (t:'T) -> for ei in eIters do ei c t
| Shape.FSharpRecord (:? ShapeFSharpRecord<'T> as shape) ->
let fIters = shape.Fields |> Array.map mkMemberIter
fun c (r:'T) -> for ei in fIters do ei c r
| Shape.FSharpUnion (:? ShapeFSharpUnion<'T> as shape) ->
let fIterss = shape.UnionCases |> Array.map (fun uc -> uc.Fields |> Array.map mkMemberIter)
fun c (u:'T) ->
let tag = shape.GetTag u
for fI in fIterss.[tag] do fI c u
| Shape.Collection s ->
s.Accept { new ICollectionVisitor<Iterator<'E,'T>> with
member _.Visit<'Collection, 't when 'Collection :> ICollection<'t>>() =
let tIter = mkIteratorCached<'E,'t> ctx
wrap(fun c (ts:'Collection) -> for t in ts do tIter c t) }
| Shape.Poco (:? ShapePoco<'T> as shape) ->
let pIters =
shape.Properties
|> Array.filter (fun p -> p.IsPublic)
|> Array.map mkMemberIter
fun c (p:'T) -> for pI in pIters do pI c p
| _ -> (fun _ _ -> ())
module private GMapper =
type Mapper<'Element, 'Graph> = ObjectStack -> ObjectCache -> ('Element -> 'Element) -> 'Graph -> 'Graph
let cache = new TypeCache()
let rec mkMapper<'E, 'T> () : Mapper<'E,'T> =
let mutable f = Unchecked.defaultof<Mapper<'E,'T>>
if cache.TryGetValue<Mapper<'E,'T>>(&f) then f else
use ctx = cache.CreateGenerationContext()
mkMapperCached<'E, 'T> ctx
and mkMapperCached<'E, 'T> (ctx : TypeGenerationContext) : Mapper<'E,'T> =
match ctx.InitOrGetCachedValue<Mapper<'E,'T>> (fun c s t -> c.Value s t) with
| Cached(value = v) -> v
| NotCached t ->
let mapper = mkMapperImpl<'E, 'T> ctx
ctx.Commit t mapper
and mkMapperImpl<'E, 'T> (ctx : TypeGenerationContext) : Mapper<'E, 'T> =
let inline wrap (mapper:Mapper<'E,'a>) : Mapper<'E,'T> =
if typeof<'a>.IsValueType then mapper
else
fun s (c:ObjectCache) f t ->
match t :> obj with
| null -> t
| o ->
s.Push o
let t' =
let id = s.ObjectId
let mutable o' : obj = null
if c.TryGetValue(id, &o') then o'
elif s.IsCycle then
c.CreateUninitializedInstance<obj>(id, o.GetType())
else
let t' = mapper s c f t
c.AddValue(id, t' :> obj)
s.Pop()
t' :?> 'a
|> unbox
let mkMemberMapper (shape : IShapeMember<'T>) =
shape.Accept { new IMemberVisitor<'T, ObjectStack -> ObjectCache -> ('E -> 'E) -> 'T -> 'T -> 'T> with
member _.Visit (shape : ShapeMember<'T, 'F>) =
let fMapper = mkMapperCached<'E, 'F> ctx
fun s c f src tgt ->
let srcField = shape.Get src
let tgtField = fMapper s c f srcField
shape.Set tgt tgtField }
match shapeof<'T> :> TypeShape with
| :? TypeShape<'E> -> wrap(fun _ _ f (t:'E) -> f t)
| Shape.Enum s ->
s.Accept { new IEnumVisitor<Mapper<'E,'T>> with
member _.Visit<'t, 'u when 't : enum<'u>
and 't : struct
and 't :> ValueType
and 't : (new : unit -> 't)>() =
let um = mkMapperCached<'E,'u> ctx
fun s c f (t:'t) ->
let u = LanguagePrimitives.EnumToValue t
let u' = um s c f u
LanguagePrimitives.EnumOfValue u'
|> wrap }
| Shape.Nullable s ->
s.Accept { new INullableVisitor<Mapper<'E,'T>> with
member _.Visit<'t when 't : struct
and 't :> ValueType
and 't : (new : unit -> 't)>() =
let tm = mkMapperCached<'E,'t> ctx
wrap(fun s c f (n:Nullable<'t>) -> if n.HasValue then Nullable(tm s c f n.Value) else n) }
| Shape.FSharpOption s ->
s.Element.Accept {
new ITypeVisitor<Mapper<'E,'T>> with
member _.Visit<'t>() =
let em = mkMapperCached<'E, 't> ctx
fun s c f tOpt ->
match tOpt with
| None -> None
| Some t -> Some(em s c f t)
|> wrap }
| Shape.Array s ->
s.Element.Accept {
new ITypeVisitor<Mapper<'E,'T>> with
member _.Visit<'t> () =
let em = mkMapperCached<'E, 't> ctx
match s.Rank with
| 1 -> wrap(fun s c f (ts : 't[]) -> ts |> Array.map (em s c f))
| 2 -> wrap(fun s c f (ts : 't[,]) -> ts |> Array2D.map (em s c f))
| 3 -> wrap(fun s c f (ts : 't[,,]) -> ts |> Array3D.map (em s c f))
| rk -> failwithf "Rank-%d arrays not supported" rk
}
| Shape.FSharpList s ->
s.Element.Accept {
new ITypeVisitor<Mapper<'E,'T>> with
member _.Visit<'t>() =
let em = mkMapperCached<'E, 't> ctx
wrap(fun s c f (ts : 't list) -> ts |> List.map (em s c f))
}
| Shape.FSharpSet s ->
s.Accept {
new IFSharpSetVisitor<Mapper<'E,'T>> with
member _.Visit<'t when 't : comparison> () =
let em = mkMapperCached<'E, 't> ctx
wrap(fun s c f ts -> ts |> Set.map (em s c f))
}
| Shape.Tuple (:? ShapeTuple<'T> as shape) ->
let eMappers = shape.Elements |> Array.map mkMemberMapper
fun s c f src ->
let mutable tgt = shape.CreateUninitialized()
for em in eMappers do tgt <- em s c f src tgt
tgt
| Shape.FSharpRecord (:? ShapeFSharpRecord<'T> as shape) ->
let fMappers = shape.Fields |> Array.map mkMemberMapper
fun s c f src ->
let mutable tgt = shape.CreateUninitialized()
for fm in fMappers do tgt <- fm s c f src tgt
tgt
| Shape.FSharpUnion (:? ShapeFSharpUnion<'T> as shape) ->
let fMapperss = shape.UnionCases |> Array.map (fun uc -> uc.Fields |> Array.map mkMemberMapper)
fun s c f src ->
let tag = shape.GetTag src
let case = shape.UnionCases.[tag]
let mutable tgt = case.CreateUninitialized()
for fm in fMapperss.[tag] do tgt <- fm s c f src tgt
tgt
| Shape.CliMutable (:? ShapeCliMutable<'T> as shape) ->
let pMappers = shape.Properties |> Array.map mkMemberMapper
fun s c f src ->
let mutable tgt = shape.CreateUninitialized()
for pm in pMappers do tgt <- pm s c f src tgt
tgt
| _ -> (fun _ _ _ t -> t)
[<RequireQualifiedAccess>]
module Generic =
/// <summary>
/// Generic map combinator. Calls the mapper function on every instance
/// of type 'a structurally within the source type 'T using depth-first
/// traversal. Cyclic objects are supported.
/// </summary>
/// <param name="mapper">Mapping operation to perform on every instance of type 'a.</param>
/// <param name="source">Source type to be traversed for instances of type 'a.</param>
let map (mapper : 'a -> 'a) (source : 'T) : 'T =
GMapper.mkMapper<'a,'T>() (new ObjectStack()) (new ObjectCache()) mapper source
/// <summary>
/// Generic iter combinator. Calls the action function on every instance
/// of type 'a structurally wihtin the source type 'T using depth-first
/// traversal. Cyclic objects are supported.
/// </summary>
/// <param name="action">Action to perform on every instance of type 'a.</param>
/// <param name="source">Source type to be traversed for instances of type 'a.</param>
let iter (action : 'a -> unit) (source : 'T) : unit =
let ctx : GIterator.Context<'a> = { Token = CancellationToken() ; Stack = new ObjectStack() ; Action = action }
GIterator.mkIterator<'a,'T>() ctx source
/// <summary>
/// Generic iter combinator with cancellation semantics. Calls the action function on every instance
/// of type 'a structurally within the source type 'T using depth-first
/// traversal. Cyclic objects are supported.
/// </summary>
/// <param name="action">Action to perform on every instance of type 'a.</param>
/// <param name="source">Source type to be traversed for instances of type 'a.</param>
let iterCancellation (action : CancellationTokenSource -> 'a -> unit) (source : 'T) : unit =
let cts = new CancellationTokenSource()
let ctx : GIterator.Context<'a> = { Token = cts.Token ; Stack = new ObjectStack() ; Action = action cts }
GIterator.mkIterator<'a, 'T>() ctx source
/// <summary>
/// Generic fold combinator. Folds over instances of type 'a structurally within
/// the source type 'T following depth-first traversal. Cyclic objects are supported.
/// </summary>
/// <param name="folder">Folding function for instances of type 'a.</param>
/// <param name="state">Initial state for the fold operation.</param>
/// <param name="source">Source type to traverse.</param>
let fold (folder : 'State -> 'a -> 'State) (state : 'State) (source : 'T) : 'State =
let state = ref state
iter (fun a -> state := folder !state a) source
!state
/// <summary>
/// Generic sumBy combinator. Sums instances of type 'a structurally within the source
/// type 'T following depth-first traversal. Cyclic objects are supported.
/// </summary>
/// <param name="projection">Projection function to sum by.</param>
/// <param name="source">Source type to traverse.</param>
let inline sumBy (projection : 'a -> 'Num) (source : 'T) : 'Num =
let count = ref LanguagePrimitives.GenericZero<'Num>
iter (fun a -> count := !count + projection a) source
!count
/// <summary>
/// Generic forall combinator. Returns true iff all instances of type 'a
/// structurally within the source 'T satisfy the user-provided predicate.
/// </summary>
/// <param name="predicate">Forall predicate.</param>
/// <param name="source">Source type to traverse.</param>
let forall (predicate : 'a -> bool) (source : 'T) : bool =
let result = ref true
iterCancellation (fun cts t -> if not(predicate t) then result := false; cts.Cancel()) source
!result
/// <summary>
/// Generic exists combinator. Returns true iff at least one instance of type 'a
/// structurally within the source 'T satisfies the user-provided predicate.
/// </summary>
/// <param name="predicate">Exists predicate.</param>
/// <param name="source">Source type to traverse.</param>
let exists (predicate : 'a -> bool) (source : 'T) : bool =
let result = ref false
iterCancellation (fun cts t -> if predicate t then result := true; cts.Cancel()) source
!result
/// <summary>
/// Generic collect combinator. Accumulates a collection of values based on mappings
/// of instances of type 'a structurally within the source of type 'T following
/// depth-first traversal. Cyclic objects are supported.
/// </summary>
/// <param name="mapping">Mapping function for collectable values.</param>
/// <param name="source">Source type to traverse.</param>
let collect (mapping : 'a -> #seq<'r>) (source : 'T) : 'r[] =
let aggregator = new ResizeArray<'r>()
iter (mapping >> aggregator.AddRange) source
aggregator.ToArray()