blob: 376dd9192ff16841b45f9e9f47983036ea7fb8c7 (
plain)
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
|
(;module: {#;doc "Pseudo-random number generation (PRNG) algorithms."}
[lux #- list]
(lux (control functor
applicative
["M" monad #+ do Monad]
hash)
(data [bit]
[text "Text/" Monoid<Text>]
text/format
[product]
[number]
(number ["r" ratio]
["c" complex])
(coll [list "List/" Fold<List>]
["A" array]
["D" dict]
["Q" queue]
["S" set]
["ST" stack]
["V" vector]))
))
## [Exports]
(type: #export #rec PRNG
{#;doc "An abstract way to represent any PRNG."}
(-> Unit [PRNG Nat]))
(type: #export (Random a)
{#;doc "A producer of random values based on a PRNG."}
(-> PRNG [PRNG a]))
(struct: #export _ (Functor Random)
(def: (map f fa)
(function [state]
(let [[state' a] (fa state)]
[state' (f a)]))))
(struct: #export _ (Applicative Random)
(def: functor Functor<Random>)
(def: (wrap a)
(function [state]
[state a]))
(def: (apply ff fa)
(function [state]
(let [[state' f] (ff state)
[state'' a] (fa state')]
[state'' (f a)]))))
(struct: #export _ (Monad Random)
(def: applicative Applicative<Random>)
(def: (join ffa)
(function [state]
(let [[state' fa] (ffa state)]
(fa state')))))
(def: #export nat
(Random Nat)
(function [prng]
(let [[prng left] (prng [])
[prng right] (prng [])]
[prng (n.+ (bit;shift-left +32 left)
right)])))
(def: #export int
(Random Int)
(function [prng]
(let [[prng left] (prng [])
[prng right] (prng [])]
[prng (nat-to-int (n.+ (bit;shift-left +32 left)
right))])))
(def: #export bool
(Random Bool)
(function [prng]
(let [[prng output] (prng [])]
[prng (|> output (bit;and +1) (n.= +1))])))
(def: (bits n)
(-> Nat (Random Nat))
(function [prng]
(let [[prng output] (prng [])]
[prng (bit;unsigned-shift-right (n.- n +64) output)])))
(def: #export real
(Random Real)
(do Monad<Random>
[left (bits +26)
right (bits +27)]
(wrap (|> right
(n.+ (bit;shift-left +27 left))
nat-to-int
int-to-real
(r./ (|> +1 (bit;shift-left +53) nat-to-int int-to-real))))))
(def: #export deg
(Random Deg)
(:: Monad<Random> map real-to-deg real))
(def: #export (text' char-gen size)
(-> (Random Nat) Nat (Random Text))
(if (n.= +0 size)
(:: Monad<Random> wrap "")
(do Monad<Random>
[x char-gen
xs (text' char-gen (n.dec size))]
(wrap (Text/append (text;from-code x) xs)))))
(def: #export (text size)
(-> Nat (Random Text))
(text' nat size))
(do-template [<name> <type> <ctor> <gen>]
[(def: #export <name>
(Random <type>)
(do Monad<Random>
[left <gen>
right <gen>]
(wrap (<ctor> left right))))]
[ratio r;Ratio r;ratio nat]
[complex c;Complex c;complex real]
)
(def: #export (seq left right)
{#;doc "Sequencing combinator."}
(All [a b] (-> (Random a) (Random b) (Random [a b])))
(do Monad<Random>
[=left left
=right right]
(wrap [=left =right])))
(def: #export (alt left right)
{#;doc "Heterogeneous alternative combinator."}
(All [a b] (-> (Random a) (Random b) (Random (| a b))))
(do Monad<Random>
[? bool]
(if ?
(do @
[=left left]
(wrap (+0 =left)))
(do @
[=right right]
(wrap (+1 =right))))))
(def: #export (either left right)
{#;doc "Homogeneous alternative combinator."}
(All [a] (-> (Random a) (Random a) (Random a)))
(do Monad<Random>
[? bool]
(if ?
left
right)))
(def: #export (rec gen)
{#;doc "A combinator for producing recursive random generators."}
(All [a] (-> (-> (Random a) (Random a)) (Random a)))
(function [state]
(let [gen' (gen (rec gen))]
(gen' state))))
(def: #export (filter pred gen)
{#;doc "Retries the generator until the output satisfies a predicate."}
(All [a] (-> (-> a Bool) (Random a) (Random a)))
(do Monad<Random>
[sample gen]
(if (pred sample)
(wrap sample)
(filter pred gen))))
(def: #export (maybe value-gen)
(All [a] (-> (Random a) (Random (Maybe a))))
(do Monad<Random>
[some? bool]
(if some?
(do @
[value value-gen]
(wrap (#;Some value)))
(wrap #;None))))
(do-template [<name> <type> <zero> <plus>]
[(def: #export (<name> size value-gen)
(All [a] (-> Nat (Random a) (Random (<type> a))))
(if (n.> +0 size)
(do Monad<Random>
[x value-gen
xs (<name> (n.dec size) value-gen)]
(wrap (<plus> x xs)))
(:: Monad<Random> wrap <zero>)))]
[list List (;list) #;Cons]
[vector V;Vector V;empty V;add]
)
(do-template [<name> <type> <ctor>]
[(def: #export (<name> size value-gen)
(All [a] (-> Nat (Random a) (Random (<type> a))))
(do Monad<Random>
[values (list size value-gen)]
(wrap (|> values <ctor>))))]
[array A;Array A;from-list]
[queue Q;Queue Q;from-list]
[stack ST;Stack (List/fold ST;push ST;empty)]
)
(def: #export (set Hash<a> size value-gen)
(All [a] (-> (Hash a) Nat (Random a) (Random (S;Set a))))
(if (n.> +0 size)
(do Monad<Random>
[xs (set Hash<a> (n.dec size) value-gen)]
(loop [_ []]
(do @
[x value-gen
#let [xs+ (S;add x xs)]]
(if (n.= size (S;size xs+))
(wrap xs+)
(recur [])))))
(:: Monad<Random> wrap (S;new Hash<a>))))
(def: #export (dict Hash<a> size key-gen value-gen)
(All [k v] (-> (Hash k) Nat (Random k) (Random v) (Random (D;Dict k v))))
(if (n.> +0 size)
(do Monad<Random>
[kv (dict Hash<a> (n.dec size) key-gen value-gen)]
(loop [_ []]
(do @
[k key-gen
v value-gen
#let [kv+ (D;put k v kv)]]
(if (n.= size (D;size kv+))
(wrap kv+)
(recur [])))))
(:: Monad<Random> wrap (D;new Hash<a>))))
(def: #export (run prng calc)
(All [a] (-> PRNG (Random a) [PRNG a]))
(calc prng))
## [PRNGs]
## PCG32 http://www.pcg-random.org/
## Based on this Java implementation: https://github.com/alexeyr/pcg-java
(def: pcg-32-magic-mult Nat +6364136223846793005)
(def: #export (pcg-32 [inc seed])
{#;doc "An implementation of the PCG32 algorithm.
For more information, please see: http://www.pcg-random.org/"}
(-> [Nat Nat] PRNG)
(function [_]
(let [seed' (|> seed (n.* pcg-32-magic-mult) (n.+ inc))
xor-shifted (|> seed (bit;unsigned-shift-right +18) (bit;xor seed) (bit;unsigned-shift-right +27))
rot (|> seed (bit;unsigned-shift-right +59))]
[(pcg-32 [inc seed']) (bit;rotate-right rot xor-shifted)]
)))
## Xoroshiro128+ http://xoroshiro.di.unimi.it/
(def: #export (xoroshiro-128+ [s0 s1])
{#;doc "An implementation of the Xoroshiro128+ algorithm.
For more information, please see: http://xoroshiro.di.unimi.it/"}
(-> [Nat Nat] PRNG)
(function [_]
(let [result (n.+ s0 s1)
s01 (bit;xor s0 s1)
s0' (|> (bit;rotate-left +55 s0)
(bit;xor s01)
(bit;xor (bit;shift-left +14 s01)))
s1' (bit;rotate-left +36 s01)]
[(xoroshiro-128+ [s0' s1']) result])
))
## [Values]
(def: (swap from to vec)
(All [a] (-> Nat Nat (V;Vector a) (V;Vector a)))
(V;put to (default (undefined)
(V;nth from vec))
vec))
(def: #export (shuffle seed vector)
{#;doc "Shuffle a vector randomly based on a seed value."}
(All [a] (-> Nat (V;Vector a) (V;Vector a)))
(let [_size (V;size vector)
_shuffle (M;fold Monad<Random>
(function [idx vec]
(do Monad<Random>
[rand nat]
(wrap (swap idx (n.% _size rand) vec))))
vector
(list;n.range +0 (n.dec _size)))]
(|> _shuffle
(run (pcg-32 [+123 seed]))
product;right)))
|