This repository was 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 pathInferTypesTest.elm
540 lines (523 loc) · 18.9 KB
/
InferTypesTest.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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
module InferTypesTest exposing (isParametric, niceVarName, typeInference, typeToString)
import Dict
import Elm.AST.Canonical as Canonical
import Elm.AST.Canonical.Unwrapped as CanonicalU
import Elm.AST.Typed as Typed
import Elm.Compiler.Error as Error exposing (Error(..), TypeError(..))
import Elm.Data.Qualifiedness exposing (PossiblyQualified(..), Qualified(..))
import Elm.Data.Type as Type exposing (Type(..), TypeOrId(..))
import Elm.Data.Type.ToString as TypeToString
import Expect
import Stage.InferTypes
import Stage.InferTypes.Environment as Env
import Stage.InferTypes.SubstitutionMap as SubstitutionMap
import Test exposing (Test, describe, test)
import TestHelpers exposing (dumpTypeOrId)
typeInference : Test
typeInference =
let
runSection : String -> List ( String, CanonicalU.Expr, Result TypeError (Type Qualified) ) -> Test
runSection description tests =
describe description
(List.map runTest tests)
runTest : ( String, CanonicalU.Expr, Result TypeError (Type Qualified) ) -> Test
runTest ( description, input, output ) =
test description <|
\() ->
input
|> Canonical.fromUnwrapped
|> Stage.InferTypes.inferExpr
Dict.empty
0
Env.empty
SubstitutionMap.empty
|> Result.map (Tuple.first >> Typed.getType)
|> Result.mapError Tuple.first
|> Expect.equal (Result.map Just output)
in
describe "Stage.InferTypesTest"
[ runSection "list"
[ ( "empty list"
, CanonicalU.List []
, Ok (List (Id 1))
)
, ( "one item"
, CanonicalU.List [ CanonicalU.Bool True ]
, Ok (List (Type Bool))
)
, ( "more items"
, CanonicalU.List
[ CanonicalU.Int 1
, CanonicalU.Int 2
, CanonicalU.Int 3
]
, Ok (List (Type Int))
)
, ( "different types"
, CanonicalU.List
[ CanonicalU.Int 1
, CanonicalU.String "two"
]
, Err (TypeMismatch (Type Int) (Type String))
)
, ( "more items with different types"
, CanonicalU.List
[ CanonicalU.Bool True
, CanonicalU.String "two"
, CanonicalU.Int 3
]
, Err (TypeMismatch (Type Bool) (Type String))
)
, ( "List of List of Int"
, CanonicalU.List
[ CanonicalU.List [ CanonicalU.Int 1 ]
, CanonicalU.List [ CanonicalU.Int 2 ]
]
, Ok (List (Type (List (Type Int))))
)
, ( "List of List of different types"
, CanonicalU.List
[ CanonicalU.List [ CanonicalU.Int 1 ]
, CanonicalU.List [ CanonicalU.Bool False ]
]
, Err (TypeMismatch (Type Int) (Type Bool))
)
]
, runSection "tuple"
[ ( "items with the same types"
, CanonicalU.Tuple
(CanonicalU.String "Hello")
(CanonicalU.String "Elm")
, Ok (Tuple (Type String) (Type String))
)
, ( "items of different types"
, CanonicalU.Tuple
(CanonicalU.Bool True)
(CanonicalU.Int 1)
, Ok (Tuple (Type Bool) (Type Int))
)
]
, runSection "tuple3"
[ ( "same types"
, CanonicalU.Tuple3
(CanonicalU.String "FP")
(CanonicalU.String "is")
(CanonicalU.String "good")
, Ok (Tuple3 (Type String) (Type String) (Type String))
)
, ( "different types"
, CanonicalU.Tuple3
(CanonicalU.Bool True)
(CanonicalU.Int 1)
(CanonicalU.Char 'h')
, Ok (Tuple3 (Type Bool) (Type Int) (Type Char))
)
]
, runSection "plus"
[ ( "same types"
, CanonicalU.Plus
(CanonicalU.Var { module_ = "Main", name = "age" })
(CanonicalU.Int 1)
, Ok Int
)
]
, runSection "cons"
[ ( "simple case"
, CanonicalU.Cons
(CanonicalU.Int 1)
(CanonicalU.List [])
, Ok (List (Type Int))
)
, ( "advanced case"
, CanonicalU.Cons
(CanonicalU.Int 1)
(CanonicalU.Cons
(CanonicalU.Int 2)
(CanonicalU.List
[ CanonicalU.Int 3
, CanonicalU.Int 4
]
)
)
, Ok (List (Type Int))
)
, ( "fail with wrong argument types"
, CanonicalU.Cons
(CanonicalU.List
[ CanonicalU.Int 1
, CanonicalU.Int 2
]
)
(CanonicalU.List
[ CanonicalU.Int 3
, CanonicalU.Int 4
]
)
, Err
(TypeMismatch
(Type (List (Type Int)))
(Type Int)
)
)
, ( "variable and list"
, CanonicalU.Cons
(CanonicalU.Var { module_ = "Main", name = "age" })
(CanonicalU.List [ CanonicalU.Int 1 ])
, Ok (List (Type Int))
)
]
, runSection "record"
[ ( "empty case"
, CanonicalU.Record Dict.empty
, Ok (Record Dict.empty)
)
, ( "one field"
, CanonicalU.Record (Dict.fromList [ ( "a", { name = "a", body = CanonicalU.Int 42 } ) ])
, Ok (Record <| Dict.fromList [ ( "a", Type Int ) ])
)
, ( "two fields"
, CanonicalU.Record
(Dict.fromList
[ ( "a", { name = "a", body = CanonicalU.Int 42 } )
, ( "b", { name = "b", body = CanonicalU.String "hello" } )
]
)
, Ok
(Record <|
Dict.fromList
[ ( "a", Type Int )
, ( "b", Type String )
]
)
)
]
]
typeToString : Test
typeToString =
let
toStringQualified : TypeOrId Qualified -> String
toStringQualified typeOrId =
typeOrId
|> TypeToString.toString (TypeToString.fromTypeOrId typeOrId)
|> Tuple.first
toStringPossiblyQualified : TypeOrId PossiblyQualified -> String
toStringPossiblyQualified typeOrId =
typeOrId
|> TypeToString.toStringPossiblyQualified (TypeToString.fromTypeOrId typeOrId)
|> Tuple.first
runTest : ( String, TypeOrId Qualified, String ) -> Test
runTest ( description, input, output ) =
test description <|
\() ->
toStringQualified input
|> Expect.equal output
runTest_ : ( String, TypeOrId PossiblyQualified, String ) -> Test
runTest_ ( description, input, output ) =
test description <|
\() ->
toStringPossiblyQualified input
|> Expect.equal output
runEqual ( description, input, output ) =
test description <|
\() ->
Expect.equal input output
in
describe "Type.toString"
{- TODO test that strings given to `Id _` are different from strings
given by `Type (TypeVar _)` (eg. if I have type annotation with `List a`
and somehow in the larger context there's an `Id 0`, it doesn't
collide with the `a` given by the `Var "a"` and instead is given `a0`
or something similar.
-}
[ describe "list"
[ runTest
( "empty list"
, Type (List (Id 0))
, "List a"
)
, runTest
( "one item in list"
, Type (List (Type Bool))
, "List Bool"
)
, runTest
( "list of list of String"
, Type (List (Type (List (Type String))))
, "List (List String)"
)
]
, describe "lambda"
[ runTest
( "function with one param"
, Type
(Function
{ from = Id 99
, to = Type Int
}
)
, "a -> Int"
)
, runTest
( "function with two params"
, Type
(Function
{ from = Id 0
, to =
Type
(Function
{ from = Id 1
, to = Id 1
}
)
}
)
, "a -> b -> b"
)
, runTest
( "function with two params, but they're vars"
, Type
(Function
{ from = Type (TypeVar "a")
, to =
Type
(Function
{ from = Type (TypeVar "b")
, to = Type (TypeVar "b")
}
)
}
)
, "a -> b -> b"
)
, runTest
( "function as param"
, Type
(Function
{ from =
Type
(Function
{ from = Id 9
, to = Id 9
}
)
, to = Id 0
}
)
, "(a -> a) -> b"
)
, runTest
( "list of functions"
, Type
(List
(Type
(Function
{ from = Id 0
, to = Id 0
}
)
)
)
, "List (a -> a)"
)
]
, describe "edge cases"
[ runEqual
( "Var number doesn't count"
, toStringQualified <| Type (List (Id 0))
, toStringQualified <| Type (List (Id 1))
)
, runEqual
( "TypeMismatch types share vars index"
, Error.toString
(TypeError
(TypeMismatch
(Type
(Function
{ from = Type (List (Id 0))
, to = Id 1
}
)
)
(Type
(Function
{ from = Type (List (Id 1))
, to = Id 0
}
)
)
)
)
, "The types `(List a) -> b` and `(List b) -> a` don't match."
)
]
, describe "tuples"
[ runTest
( "tuple with two literals"
, Type (Tuple (Type Int) (Type String))
, "( Int, String )"
)
, runTest
( "tuple with two params"
, Type (Tuple (Id 0) (Id 1))
, "( a, b )"
)
, runTest
( "3-tuple with three params"
, Type (Tuple3 (Id 0) (Id 1) (Id 2))
, "( a, b, c )"
)
]
, describe "user defined type"
[ runTest
( "type without a param"
, Type
(UserDefinedType
{ qualifiedness = Qualified "MyModule"
, name = "MyBool"
, args = []
}
)
, "MyModule.MyBool"
)
, runTest
( "type with a param"
, Type
(UserDefinedType
{ qualifiedness = Qualified "Maybe"
, name = "Maybe"
, args = [ Type Int ]
}
)
, "Maybe.Maybe Int"
)
, runTest
( "type with a param 2"
, Type
(UserDefinedType
{ qualifiedness = Qualified "Maybe"
, name = "Maybe"
, args = [ Id 0 ]
}
)
, "Maybe.Maybe a"
)
, runTest_
( "unqualified type"
, Type
(UserDefinedType
{ qualifiedness = PossiblyQualified Nothing
, name = "Maybe"
, args = [ Id 0 ]
}
)
, "Maybe a"
)
]
, describe "records"
[ runTest
( "empty record"
, Type (Record Dict.empty)
, "{}"
)
, runTest
( "one field record"
, Type <| Record <| Dict.fromList [ ( "a", Type Int ) ]
, "{ a : Int }"
)
, runTest
( "two fields record"
, Type <|
Record <|
Dict.fromList
[ ( "a", Type Int )
, ( "b", Type String )
]
, "{ a : Int, b : String }"
)
, runTest
( "parametric field in record"
, Type <| Record <| Dict.fromList [ ( "foo", Id 0 ) ]
, "{ foo : a }"
)
]
, runTest
( "unit"
, Type Unit
, "()"
)
]
niceVarName : Test
niceVarName =
let
runTest ( input, output ) =
test output <|
\() ->
TypeToString.niceVarName input
|> Expect.equal output
in
describe "Type.niceVarName" <|
List.map runTest
[ ( 0, "a" )
, ( 1, "b" )
, ( 2, "c" )
, ( 25, "z" )
--
, ( 26, "a1" )
, ( 49, "x1" )
, ( 50, "y1" )
, ( 51, "z1" )
--
, ( 52, "a2" )
, ( 77, "z2" )
--
, ( 259, "z9" )
, ( 260, "a10" )
]
isParametric : Test
isParametric =
let
runTest : Int -> ( TypeOrId Qualified, Bool ) -> Test
runTest i ( input, output ) =
test (String.fromInt i ++ ": " ++ dumpTypeOrId input) <|
\() ->
input
|> Type.isParametric
|> Expect.equal output
in
describe "Type.isParametric" <|
List.indexedMap runTest
[ ( Type Unit, False )
, ( Type Bool, False )
, ( Type Char, False )
, ( Type Int, False )
, ( Type String, False )
, ( Type <| TypeVar "a", True )
, ( Id 0, True )
, ( Type <| Function { from = Type Int, to = Type String }, False )
, ( Type <| Function { from = Type (TypeVar "bcd"), to = Type Int }, True )
, ( Type <| Function { from = Id 102, to = Type Int }, True )
, ( Type <| Function { from = Type String, to = Type (TypeVar "xx") }, True )
, ( Type <| Function { from = Type String, to = Id 99 }, True )
, ( Type <| Function { from = Type Int, to = Type (Function { from = Id 0, to = Id 0 }) }, True )
, ( Type <| List (Type Int), False )
, ( Type <| List (Id 0), True )
, ( Type <| List (Type (TypeVar "ab")), True )
, ( Type <| List (Type (List (Type Int))), False )
, ( Type <| List (Type (List (Id 0))), True )
, ( Type <| List (Type (List (Type (TypeVar "cd")))), True )
, ( Type <| Tuple (Type Int) (Type String), False )
, ( Type <| Tuple (Id 0) (Type Int), True )
, ( Type <| Tuple (Type String) (Id 0), True )
, ( Type <| Tuple (Type (List (Id 0))) (Type Int), True )
, ( Type <| Tuple (Type Char) (Type (Tuple (Type Int) (Id 0))), True )
, ( Type <| Tuple3 (Type Int) (Type String) (Type Bool), False )
, ( Type <| Tuple3 (Id 0) (Type Int) (Type Char), True )
, ( Type <| Tuple3 (Type String) (Id 0) (Type Unit), True )
, ( Type <| Tuple3 (Type Bool) (Type Unit) (Id 0), True )
, ( Type <| Tuple3 (Type (List (Id 0))) (Type Int) (Type Char), True )
, ( Type <| Tuple3 (Type String) (Type (Function { from = Id 0, to = Type Int })) (Type Unit), True )
, ( Type <| Tuple3 (Type Bool) (Type Unit) (Type (Tuple (Id 0) (Type Int))), True )
, ( Type <| Record Dict.empty, False )
, ( Type <| Record <| Dict.fromList [ ( "a", Id 0 ), ( "b", Type String ) ], True )
, ( Type <| Record <| Dict.fromList [ ( "a", Type Int ), ( "b", Type String ) ], False )
]