This repository has been archived by the owner on Oct 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathInferTypesFuzz.elm
341 lines (275 loc) · 9.25 KB
/
InferTypesFuzz.elm
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
module InferTypesFuzz exposing (typeInference)
import Dict exposing (Dict)
import Elm.AST.Canonical as Canonical
import Elm.AST.Canonical.Unwrapped as CanonicalU
import Elm.Data.Located as Located
import Elm.Data.Qualifiedness exposing (Qualified)
import Elm.Data.Type as Type
import Elm.Data.Type.Concrete as ConcreteType exposing (ConcreteType(..))
import Elm.Data.VarName exposing (VarName)
import Expect
import Fuzz exposing (Fuzzer)
import Stage.InferTypes
import Stage.InferTypes.Environment as Env
import Stage.InferTypes.SubstitutionMap as SubstitutionMap
import Test exposing (Test, describe, fuzz)
import TestHelpers exposing (dumpConcreteType)
typeInference : Test
typeInference =
let
fuzzExpr : ConcreteType Qualified -> Test
fuzzExpr typeWanted =
fuzz (exprOfType typeWanted) (dumpConcreteType typeWanted) <|
\input ->
input
|> Canonical.fromUnwrapped
|> Stage.InferTypes.inferExpr
Dict.empty
0
Env.empty
SubstitutionMap.empty
|> Result.map (Tuple.first >> Located.unwrap >> Tuple.second)
|> Expect.equal (Ok (ConcreteType.toTypeOrId typeWanted))
fuzzExpressions : String -> List (ConcreteType Qualified) -> Test
fuzzExpressions description types =
types
|> List.map fuzzExpr
|> describe description
in
describe "Stage.InferTypesFuzz"
[ describe "inferExpr"
[ fuzzExpressions "fuzz literals"
[ Int
, Float
, Bool
, Char
, String
, Unit
]
, fuzzExpressions "fuzz lists"
[ List Unit
, List Int
, List (List String)
]
, fuzzExpressions "fuzz functions"
[ Function { from = Int, to = Int }
]
, fuzzExpressions "fuzz tuples"
[ Tuple Int String
, Tuple Bool Char
, Tuple3 Int String Bool
, Tuple3 Unit Char Float
]
, fuzzExpressions "fuzz records"
[ Record Dict.empty
, Record (Dict.fromList [ ( "a", Int ) ])
, Record (Dict.fromList [ ( "a", Int ), ( "b", String ) ])
]
]
]
exprOfType : ConcreteType Qualified -> Fuzzer CanonicalU.Expr
exprOfType targetType =
exprOfTypeWithDepth 3 targetType
exprOfTypeWithDepth : Int -> ConcreteType Qualified -> Fuzzer CanonicalU.Expr
exprOfTypeWithDepth depthLeft targetType =
let
basicExpr =
basicExprOfType depthLeft targetType
pickAffordable ( cost, fuzzer ) =
if cost <= depthLeft then
targetType
|> fuzzer depthLeft
|> Just
else
Nothing
in
-- TODO: When generating Ints we should be able to use Plus here.
[ ( 1, ifExpr )
]
|> List.filterMap pickAffordable
|> (::) basicExpr
|> Fuzz.oneOf
basicExprOfType : Int -> ConcreteType Qualified -> Fuzzer CanonicalU.Expr
basicExprOfType depthLeft targetType =
let
cannotFuzz details =
let
prefix =
"Cannot fuzz `" ++ dumpConcreteType targetType ++ "` expressions."
message =
if String.isEmpty details then
prefix
else
prefix ++ " " ++ details
in
Debug.todo message
in
case targetType of
TypeVar _ ->
cannotFuzz "Vars, by definition, cannot be supported"
Int ->
intExpr
Float ->
floatExpr
Bool ->
boolExpr
Char ->
charExpr
String ->
stringExpr
Unit ->
unitExpr
List elementType ->
if Type.isParametric (ConcreteType.toTypeOrId elementType) then
{- Supporting parametric types might have weird interplay with recursive generation.
We are leaving it off, at least for now.
-}
cannotFuzz "Only lists with non-parametric element types are supported."
else
listExpr depthLeft elementType
Function { from, to } ->
case ( from, to ) of
( Int, Int ) ->
intToIntFunctionExpr
_ ->
cannotFuzz "Only `Int -> Int` functions are supported."
Tuple firstType secondType ->
tupleExpr depthLeft ( firstType, secondType )
Tuple3 firstType secondType thirdType ->
tuple3Expr depthLeft ( firstType, secondType, thirdType )
Record bindings ->
recordExpr depthLeft bindings
UserDefinedType _ ->
{- TODO after Exprs contain constructors / record constructors, we
can try to pick one specific subtype and generate it here
-}
cannotFuzz "TODO: Custom type constructors don't exist in Expr types yet"
ifExpr : Int -> ConcreteType Qualified -> Fuzzer CanonicalU.Expr
ifExpr depth targetType =
let
combine test then_ else_ =
CanonicalU.If
{ test = test
, then_ = then_
, else_ = else_
}
subexpr =
exprOfTypeWithDepth (depth - 1)
in
Fuzz.map3 combine
(subexpr Bool)
(subexpr targetType)
(subexpr targetType)
intExpr : Fuzzer CanonicalU.Expr
intExpr =
Fuzz.int
|> Fuzz.map CanonicalU.Int
floatExpr : Fuzzer CanonicalU.Expr
floatExpr =
-- Does not produce NaNs, but that should not be an issue for us.
Fuzz.float
|> Fuzz.map CanonicalU.Float
boolExpr : Fuzzer CanonicalU.Expr
boolExpr =
Fuzz.bool
|> Fuzz.map CanonicalU.Bool
charExpr : Fuzzer CanonicalU.Expr
charExpr =
Fuzz.char
|> Fuzz.map CanonicalU.Char
stringExpr : Fuzzer CanonicalU.Expr
stringExpr =
Fuzz.stringOfLengthBetween 1 5
|> Fuzz.map CanonicalU.String
unitExpr : Fuzzer CanonicalU.Expr
unitExpr =
CanonicalU.Unit
|> Fuzz.constant
listExpr : Int -> ConcreteType Qualified -> Fuzzer CanonicalU.Expr
listExpr depthLeft elementType =
elementType
|> exprOfTypeWithDepth depthLeft
|> Fuzz.listOfLengthBetween 1 5
|> Fuzz.map CanonicalU.List
intToIntFunctionExpr : Fuzzer CanonicalU.Expr
intToIntFunctionExpr =
let
combine argument intPart =
lambda argument <|
CanonicalU.Plus
(CanonicalU.Argument argument)
intPart
intSubExpr =
exprOfTypeWithDepth 0 Int
in
Fuzz.map2 combine
-- TODO: Later we will need something better to avoid shadowing.
randomVarName
intSubExpr
tupleExpr : Int -> ( ConcreteType Qualified, ConcreteType Qualified ) -> Fuzzer CanonicalU.Expr
tupleExpr depthLeft ( firstType, secondType ) =
Fuzz.map2 CanonicalU.Tuple
(firstType |> exprOfTypeWithDepth depthLeft)
(secondType |> exprOfTypeWithDepth depthLeft)
tuple3Expr : Int -> ( ConcreteType Qualified, ConcreteType Qualified, ConcreteType Qualified ) -> Fuzzer CanonicalU.Expr
tuple3Expr depthLeft ( firstType, secondType, thirdType ) =
Fuzz.map3 CanonicalU.Tuple3
(firstType |> exprOfTypeWithDepth depthLeft)
(secondType |> exprOfTypeWithDepth depthLeft)
(thirdType |> exprOfTypeWithDepth depthLeft)
recordExpr : Int -> Dict VarName (ConcreteType Qualified) -> Fuzzer CanonicalU.Expr
recordExpr depthLeft bindings =
let
typesFuzzer =
bindings
|> Dict.values
|> List.map (exprOfTypeWithDepth (depthLeft - 1))
|> Fuzz.sequence
namesFuzzer =
Dict.keys bindings |> Fuzz.constant
in
Fuzz.map2
(\names exprs ->
List.map2
(\name expr ->
( name, { name = name, body = expr } )
)
names
exprs
|> Dict.fromList
|> CanonicalU.Record
)
namesFuzzer
typesFuzzer
randomVarName : Fuzzer VarName
randomVarName =
let
starters =
"abcdefghijklnmoqprstuvwxyz"
others =
"ABCDEFGHIJKLMNOQPRSTUVQXYZ_0123456789"
all =
starters ++ others
charFrom string =
string
|> String.toList
|> Fuzz.oneOfValues
firstChar =
charFrom starters
rest =
Fuzz.listOfLengthBetween 1 5 <| charFrom all
in
Fuzz.map2 (::) firstChar rest
|> Fuzz.map String.fromList
lambda : VarName -> CanonicalU.Expr -> CanonicalU.Expr
lambda argument body =
CanonicalU.Lambda
{ argument = argument
, body = body
}
call : CanonicalU.Expr -> CanonicalU.Expr -> CanonicalU.Expr
call fn argument =
CanonicalU.Call
{ fn = fn
, argument = argument
}