blob: 8954aafb10584371672cac25b1b6a66d43e10a5b (
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
|
(.module:
[lux #*
[io]
[control
[monad (#+ do)]
pipe]
[data
[product]
[maybe]
[error]
[number]
[text format]
[collection
[list ("list/" Functor<List> Fold<List>)]
["dict" dictionary (#+ Dictionary)]
[set]]]
[language
[reference (#+ Variable) ("variable/" Equivalence<Variable>)]
["." compiler
[analysis (#+ Arity Analysis)]
["//" synthesis (#+ Synthesis)
[expression]]
[extension
[bundle]]]]
[math ["r" random]]
test]
[//primitive])
(def: constant-function
(r.Random [Arity Analysis Analysis])
(r.rec
(function (_ constant-function)
(do r.Monad<Random>
[function? r.bit]
(if function?
(do @
[[arity bodyA predictionA] constant-function]
(wrap [(inc arity)
(#analysis.Function (list) bodyA)
predictionA]))
(do @
[predictionA //primitive.primitive]
(wrap [+0 predictionA predictionA])))))))
(def: (pick scope-size)
(-> Nat (r.Random Nat))
(|> r.nat (:: r.Monad<Random> map (n/% scope-size))))
(def: function-with-environment
(r.Random [Arity Analysis Variable])
(do r.Monad<Random>
[num-locals (|> r.nat (:: @ map (|>> (n/% +100) (n/max +10))))
#let [indices (list.n/range +0 (dec num-locals))
local-env (list/map (|>> #reference.Local) indices)
foreign-env (list/map (|>> #reference.Foreign) indices)]
[arity bodyA predictionA] (: (r.Random [Arity Analysis Variable])
(loop [arity +1
current-env foreign-env]
(let [current-env/size (list.size current-env)
resolver (list/fold (function (_ [idx var] resolver)
(dict.put idx var resolver))
(: (Dictionary Nat Variable)
(dict.new number.Hash<Nat>))
(list.enumerate current-env))]
(do @
[nest? r.bit]
(if nest?
(do @
[num-picks (:: @ map (n/max +1) (pick (inc current-env/size)))
picks (|> (r.set number.Hash<Nat> num-picks (pick current-env/size))
(:: @ map set.to-list))
[arity bodyA predictionA] (recur (inc arity)
(list/map (function (_ pick)
(maybe.assume (list.nth pick current-env)))
picks))
#let [picked-env (list/map (|>> #reference.Foreign) picks)]]
(wrap [arity
(#analysis.Function picked-env bodyA)
predictionA]))
(do @
[chosen (pick (list.size current-env))]
(wrap [arity
(#analysis.Reference (reference.foreign chosen))
(maybe.assume (dict.get chosen resolver))])))))))]
(wrap [arity
(#analysis.Function local-env bodyA)
predictionA])))
(def: local-function
(r.Random [Arity Analysis Variable])
(loop [arity +0
nest? #1]
(if nest?
(do r.Monad<Random>
[nest?' r.bit
[arity' bodyA predictionA] (recur (inc arity) nest?')]
(wrap [arity'
(#analysis.Function (list) bodyA)
predictionA]))
(do r.Monad<Random>
[chosen (|> r.nat (:: @ map (|>> (n/% +100) (n/max +2))))]
(wrap [arity
(#analysis.Reference (reference.local chosen))
(|> chosen (n/+ (dec arity)) #reference.Local)])))))
(context: "Function definition."
(<| (seed +13007429814532219492)
## (times +100)
(do @
[[arity//constant function//constant prediction//constant] constant-function
[arity//environment function//environment prediction//environment] function-with-environment
[arity//local function//local prediction//local] local-function]
($_ seq
(test "Nested functions will get folded together."
(|> function//constant
expression.synthesize
(compiler.run [bundle.empty //.init])
(case> (^ (#error.Success (//.function/abstraction [environment arity output])))
(and (n/= arity//constant arity)
(//primitive.corresponds? prediction//constant output))
_
(n/= +0 arity//constant))))
(test "Folded functions provide direct access to environment variables."
(|> function//environment
expression.synthesize
(compiler.run [bundle.empty //.init])
(case> (^ (#error.Success (//.function/abstraction [environment arity (#//.Reference (reference.variable output))])))
(and (n/= arity//environment arity)
(variable/= prediction//environment output))
_
#0)))
(test "Folded functions properly offset local variables."
(|> function//local
expression.synthesize
(compiler.run [bundle.empty //.init])
(case> (^ (#error.Success (//.function/abstraction [environment arity (#//.Reference (reference.variable output))])))
(and (n/= arity//local arity)
(variable/= prediction//local output))
_
#0)))
))))
(context: "Function application."
(<| (times +100)
(do @
[arity (|> r.nat (:: @ map (|>> (n/% +10) (n/max +1))))
funcA //primitive.primitive
argsA (r.list arity //primitive.primitive)]
($_ seq
(test "Can synthesize function application."
(|> (analysis.apply [funcA argsA])
expression.synthesize
(compiler.run [bundle.empty //.init])
(case> (^ (#error.Success (//.function/apply [funcS argsS])))
(and (//primitive.corresponds? funcA funcS)
(list.every? (product.uncurry //primitive.corresponds?)
(list.zip2 argsA argsS)))
_
#0)))
(test "Function application on no arguments just synthesizes to the function itself."
(|> (analysis.apply [funcA (list)])
expression.synthesize
(compiler.run [bundle.empty //.init])
(case> (#error.Success funcS)
(//primitive.corresponds? funcA funcS)
_
#0)))
))))
|