@@ -15,384 +15,535 @@ import Data.List.NonEmpty as NonEmptyList
1515import Data.Set as Set
1616import Data.Tuple (Tuple(..))
1717import Test.Extra (assertRight)
18−import Test.Unit (TestSuite, suite, test)
19−import Test.Unit.Assert as Assert
18+import Test.Spec (Spec, describe, it)
19+import Test.Spec.Assertions (shouldEqual, shouldSatisfy)
2020import Data.Maybe (Maybe(..))
2121
22−
23−tests :: TestSuite
22+tests :: Spec Unit
2423tests = do
25− suite "TypeQuery parser" do
24+ describe "TypeQuery parser" do
2625
27− test "test #0" do
26+ it "test #0" do
2827 let input = "a"
2928 assertRight (parseTypeQuery input) (qVar "a")
3029
31− test "test #1" do
30+ it "test #1" do
3231 let input = "ab"
3332 assertRight (parseTypeQuery input) (qVar "ab")
3433
35− test "test #2" do
34+ it "test #2" do
3635 let input = "a b"
3736 assertRight (parseTypeQuery input) (QApp (qVar "a") (qVar "b"))
3837
39− test "test #3" do
38+ it "test #3" do
4039 let input = "a b c"
4140 assertRight (parseTypeQuery input) (QApp (QApp (qVar "a") (qVar "b")) (qVar "c"))
4241
43− test "test #4" do
42+ it "test #4" do
4443 let input = "a -> b"
4544 assertRight (parseTypeQuery input) (QFun (qVar "a") (qVar "b"))
4645
47− test "test #5" do
46+ it "test #5" do
4847 let input = "a -> b c"
4948 assertRight (parseTypeQuery input) (QFun (qVar "a") (QApp (qVar "b") (qVar "c")))
5049
51− test "test #6" do
50+ it "test #6" do
5251 let input = "a b -> c"
5352 assertRight (parseTypeQuery input) (QFun (QApp (qVar "a") (qVar "b")) (qVar "c"))
5453
55− test "test #7" do
54+ it "test #7" do
5655 let input = "a b"
5756 assertRight (parseTypeQuery input) (QApp (qVar "a") (qVar "b"))
5857
59− test "test #8" do
58+ it "test #8" do
6059 let input = "a (b c)"
6160 assertRight (parseTypeQuery input) (QApp (qVar "a") (QApp (qVar "b") (qVar "c")))
6261
63− test "test #9" do
62+ it "test #9" do
6463 let input = "(a b) (c d)"
6564 assertRight (parseTypeQuery input)
66− (QApp (QApp (qVar "a") (qVar "b"))
67− (QApp (qVar "c") (qVar "d")))
65+ ( QApp (QApp (qVar "a") (qVar "b"))
66+ (QApp (qVar "c") (qVar "d"))
67+ )
6868
69− test "test #10" do
69+ it "test #10" do
7070 let input = "a ( b c )"
7171 assertRight (parseTypeQuery input) (QApp (qVar "a") (QApp (qVar "b") (qVar "c")))
7272
73− test "test #11" do
73+ it "test #11" do
7474 let input = "aaa"
7575 assertRight (parseTypeQuery input) (qVar "aaa")
7676
77− test "test #12" do
77+ it "test #12" do
7878 let input = "aaa ( bbb ccc )"
7979 assertRight (parseTypeQuery input) (QApp (qVar "aaa") (QApp (qVar "bbb") (qVar "ccc")))
8080
81− test "test #13" do
81+ it "test #13" do
8282 let input = "(a -> b) -> (c -> d)"
83− assertRight (parseTypeQuery input) (QFun (QFun (qVar "a") (qVar "b"))
84− (QFun (qVar "c") (qVar "d")))
83+ assertRight (parseTypeQuery input)
84+ ( QFun (QFun (qVar "a") (qVar "b"))
85+ (QFun (qVar "c") (qVar "d"))
86+ )
8587
86− test "test #14" do
88+ it "test #14" do
8789 let input = "a -> b -> c -> d"
88− assertRight (parseTypeQuery input) (QFun (qVar "a")
89− (QFun (qVar "b")
90− (QFun (qVar "c") (qVar "d"))))
90+ assertRight (parseTypeQuery input)
91+ ( QFun (qVar "a")
92+ ( QFun (qVar "b")
93+ (QFun (qVar "c") (qVar "d"))
94+ )
95+ )
9196
92− test "test #15" do
97+ it "test #15" do
9398 let input = "a -> b -> c"
94− assertRight (parseTypeQuery input) (QFun (qVar "a")
95− (QFun (qVar "b")
96− (qVar "c")))
99+ assertRight (parseTypeQuery input)
100+ ( QFun (qVar "a")
101+ ( QFun (qVar "b")
102+ (qVar "c")
103+ )
104+ )
97105
98− test "test #16" do
106+ it "test #16" do
99107 let input = "forall a b c. c"
100− assertRight (parseTypeQuery input) (QForAll (nl "a" ["b", "c"]) (qVar "c"))
108+ assertRight (parseTypeQuery input) (QForAll (nl "a" [ "b", "c" ]) (qVar "c"))
101109
102− test "test #17" do
110+ it "test #17" do
103111 let input = "forall a. Maybe a"
104112 assertRight (parseTypeQuery input) (QForAll (nl "a" $ []) (QApp (qConst "Maybe") (qVar "a")))
105113
106− test "test #18" do
114+ it "test #18" do
107115 let input = "forall m a. Monad m => a -> m a"
108116 assertRight (parseTypeQuery input)
109− (QForAll (nl "m" ["a"])
110− (qConstraint "Monad" (l [qVar "m"])
111− (QFun (qVar "a")
112− (QApp (qVar "m") (qVar "a")))))
113−
114− test "test #19" do
117+ ( QForAll (nl "m" [ "a" ])
118+ ( qConstraint "Monad" (l [ qVar "m" ])
119+ ( QFun (qVar "a")
120+ (QApp (qVar "m") (qVar "a"))
121+ )
122+ )
123+ )
124+
125+ it "test #19" do
115126 let input = "{ a :: Int }"
116127 assertRight (parseTypeQuery input)
117128 (QApp (qConst "Record") (QRow (pure (Tuple (Identifier "a") (qConst "Int")))))
118129
119− test "test #20" do
130+ it "test #20" do
120131 let input = "{a::Int}"
121132 assertRight (parseTypeQuery input)
122133 (QApp (qConst "Record") (QRow (pure (Tuple (Identifier "a") (qConst "Int")))))
123134
124− test "test #21" do
135+ it "test #21" do
125136 let input = "Int"
126137 assertRight (parseTypeQuery input) (qConst "Int")
127138
128− test "test #22" do
139+ it "test #22" do
129140 let input = "a->b"
130141 assertRight (parseTypeQuery input) (QFun (qVar "a") (qVar "b"))
131142
132− test "test #23" do
143+ it "test #23" do
133144 let input = "forall m a. MonadRec m => Process m a -> m a"
134− assertRight (parseTypeQuery input) (QForAll (nl "m" ("a" : Nil))
135− (qConstraint "MonadRec" (l [qVar "m"])
136− (QFun (QApp (QApp (qConst "Process")
137− (qVar "m")) (qVar "a"))
138− (QApp (qVar "m") (qVar "a")))))
139−
140− test "test #24" do
145+ assertRight (parseTypeQuery input)
146+ ( QForAll (nl "m" ("a" : Nil))
147+ ( qConstraint "MonadRec" (l [ qVar "m" ])
148+ ( QFun
149+ ( QApp
150+ ( QApp (qConst "Process")
151+ (qVar "m")
152+ )
153+ (qVar "a")
154+ )
155+ (QApp (qVar "m") (qVar "a"))
156+ )
157+ )
158+ )
159+
160+ it "test #24" do
141161 let input = "forall t f a. Foldable1 t => Apply f => f"
142− assertRight (parseTypeQuery input) (QForAll (nl "t" ["f", "a"])
143− (qConstraint "Foldable1" (l [qVar "t"])
144− (qConstraint "Apply" (l [qVar "f"]) (qVar "f"))))
162+ assertRight (parseTypeQuery input)
163+ ( QForAll (nl "t" [ "f", "a" ])
164+ ( qConstraint "Foldable1" (l [ qVar "t" ])
165+ (qConstraint "Apply" (l [ qVar "f" ]) (qVar "f"))
166+ )
167+ )
145168
146− test "test #25" do
169+ it "test #25" do
147170 let input = "forall m a.MonadRec m=>Process m a->m a"
148− assertRight (parseTypeQuery input) ((QForAll (nl "m" ("a" : Nil))
149− (qConstraint "MonadRec" (l [qVar "m"])
150− (QFun (QApp (QApp (qConst "Process")
151− (qVar "m")) (qVar "a"))
152− (QApp (qVar "m") (qVar "a"))))))
153−
154− test "test #26" do
171+ assertRight (parseTypeQuery input)
172+ ( ( QForAll (nl "m" ("a" : Nil))
173+ ( qConstraint "MonadRec" (l [ qVar "m" ])
174+ ( QFun
175+ ( QApp
176+ ( QApp (qConst "Process")
177+ (qVar "m")
178+ )
179+ (qVar "a")
180+ )
181+ (QApp (qVar "m") (qVar "a"))
182+ )
183+ )
184+ )
185+ )
186+
187+ it "test #26" do
155188 let input = "m a -> (a -> m b) -> m b"
156189 assertRight (parseTypeQuery input) (QFun (QApp (qVar "m") (qVar "a")) (QFun (QFun (qVar "a") (QApp (qVar "m") (qVar "b"))) (QApp (qVar "m") (qVar "b"))))
157190
158− test "test #27" do
191+ it "test #27" do
159192 let input = "forall f a. Alternative f => Lazy (f (List a)) => f a -> f (List a)"
160− assertRight (parseTypeQuery input) ((QForAll (nl "f" ["a"]))
161− (qConstraint "Alternative" (l [qVar "f"])
162− (qConstraint "Lazy" (l [QApp (qVar "f")
163− (QApp (qConst "List") (qVar "a"))])
164− (QFun (QApp (qVar "f") (qVar "a"))
165− (QApp (qVar "f")
166− (QApp (qConst "List") (qVar "a")))))))
167−
168− test "test #28" do
193+ assertRight (parseTypeQuery input)
194+ ( (QForAll (nl "f" [ "a" ]))
195+ ( qConstraint "Alternative" (l [ qVar "f" ])
196+ ( qConstraint "Lazy"
197+ ( l
198+ [ QApp (qVar "f")
199+ (QApp (qConst "List") (qVar "a"))
200+ ]
201+ )
202+ ( QFun (QApp (qVar "f") (qVar "a"))
203+ ( QApp (qVar "f")
204+ (QApp (qConst "List") (qVar "a"))
205+ )
206+ )
207+ )
208+ )
209+ )
210+
211+ it "test #28" do
169212 let input = "forall f a. Alternative f => Lazy(f (List a))=>f a -> f (List a)"
170− assertRight (parseTypeQuery input) ((QForAll (nl "f" ["a"]))
171− (qConstraint "Alternative" (l [qVar "f"])
172− (qConstraint "Lazy" (l [QApp (qVar "f")
173− (QApp (qConst "List") (qVar "a"))])
174− (QFun (QApp (qVar "f") (qVar "a"))
175− (QApp (qVar "f")
176− (QApp (qConst "List") (qVar "a")))))))
177−
178− test "test #29" do
213+ assertRight (parseTypeQuery input)
214+ ( (QForAll (nl "f" [ "a" ]))
215+ ( qConstraint "Alternative" (l [ qVar "f" ])
216+ ( qConstraint "Lazy"
217+ ( l
218+ [ QApp (qVar "f")
219+ (QApp (qConst "List") (qVar "a"))
220+ ]
221+ )
222+ ( QFun (QApp (qVar "f") (qVar "a"))
223+ ( QApp (qVar "f")
224+ (QApp (qConst "List") (qVar "a"))
225+ )
226+ )
227+ )
228+ )
229+ )
230+
231+ it "test #29" do
179232 let input = "{a::Int,b::Int}"
180233 assertRight (parseTypeQuery input)
181− (QApp (qConst "Record") (QRow (List.fromFoldable
182− [ Tuple (Identifier "a") (qConst "Int")
183− , Tuple (Identifier "b") (qConst "Int")])))
184−
185− test "test #30" do
234+ ( QApp (qConst "Record")
235+ ( QRow
236+ ( List.fromFoldable
237+ [ Tuple (Identifier "a") (qConst "Int")
238+ , Tuple (Identifier "b") (qConst "Int")
239+ ]
240+ )
241+ )
242+ )
243+
244+ it "test #30" do
186245 let input = "{record''' :: Int}"
187246 assertRight (parseTypeQuery input)
188− (QApp (qConst "Record") (QRow (List.fromFoldable [ Tuple (Identifier "record'''") (qConst "Int")])))
247+ (QApp (qConst "Record") (QRow (List.fromFoldable [ Tuple (Identifier "record'''") (qConst "Int") ])))
189248
190− test "test #31" do
249+ it "test #31" do
191250 let input = "(row''' :: Int)"
192251 assertRight (parseTypeQuery input)
193− (QRow (List.fromFoldable [ Tuple (Identifier "row'''") (qConst "Int")]))
252+ (QRow (List.fromFoldable [ Tuple (Identifier "row'''") (qConst "Int") ]))
194253
195− test "test #32" do
254+ it "test #32" do
196255 let input = "(row1 :: Int, row2 :: (),row3::(row4::{}))"
197256 assertRight (parseTypeQuery input)
198− (QRow (l [ Tuple (Identifier "row1") (qConst "Int")
199− , Tuple (Identifier "row2") (QRow Nil)
200− , Tuple (Identifier "row3") (QRow (l [ Tuple (Identifier "row4") (QApp (qConst "Record") (QRow Nil)) ])) ]))
201−
202− test "test #33" do
257+ ( QRow
258+ ( l
259+ [ Tuple (Identifier "row1") (qConst "Int")
260+ , Tuple (Identifier "row2") (QRow Nil)
261+ , Tuple (Identifier "row3") (QRow (l [ Tuple (Identifier "row4") (QApp (qConst "Record") (QRow Nil)) ]))
262+ ]
263+ )
264+ )
265+
266+ it "test #33" do
203267 let input = "Foldable1 t => Apply f => t (f a) -> f Unit"
204268 assertRight (parseTypeQuery input)
205269 (qConstraint "Foldable1" ((qVar "t") : Nil) (qConstraint "Apply" ((qVar "f") : Nil) (QFun (QApp (qVar "t") (QApp (qVar "f") (qVar "a"))) (QApp (qVar "f") (qConst "Unit")))))
206270
207− test "test #34" do
271+ it "test #34" do
208272 let input = "Foldable1 t => Apply f => t (f a) -> f a"
209273 assertRight (parseTypeQuery input)
210274 (qConstraint "Foldable1" ((qVar "t") : Nil) (qConstraint "Apply" ((qVar "f") : Nil) (QFun (QApp (qVar "t") (QApp (qVar "f") (qVar "a"))) (QApp (qVar "f") (qVar "a")))))
211275
212− test "test #35" do
276+ it "test #35" do
213277 let input = "Generic a rep => GenericEq rep => a -> a -> Boolean"
214278 assertRight (parseTypeQuery input)
215− (qConstraint "Generic" ((qVar "a") : (qVar "rep") : Nil)
216− (qConstraint "GenericEq" ((qVar "rep") : Nil)
217− (QFun (qVar "a") (QFun (qVar "a") (qConst "Boolean")))))
279+ ( qConstraint "Generic" ((qVar "a") : (qVar "rep") : Nil)
280+ ( qConstraint "GenericEq" ((qVar "rep") : Nil)
281+ (QFun (qVar "a") (QFun (qVar "a") (qConst "Boolean")))
282+ )
283+ )
218284
219− suite "polish notation" do
285+ describe "polish notation" do
220286
221− test "test #1" do
287+ it "test #1" do
222288 let input = "(a -> b) -> (b -> ((a -> b) -> c))"
223289 assertRight (shapeOfTypeQuery <$> parseTypeQuery input)
224− (l [ PForAll 3, PFun, PFun, PVar, PVar, PFun, PVar, PFun, PFun
225− , PVar, PVar, PVar ])
226−
227− test "test #2" do
290+ ( l
291+ [ PForAll 3
292+ , PFun
293+ , PFun
294+ , PVar
295+ , PVar
296+ , PFun
297+ , PVar
298+ , PFun
299+ , PFun
300+ , PVar
301+ , PVar
302+ , PVar
303+ ]
304+ )
305+
306+ it "test #2" do
228307 let input = "forall a. (a -> b) -> (b -> ((a -> b) -> c))"
229308 assertRight (shapeOfTypeQuery <$> parseTypeQuery input)
230− (l [ PForAll 3, PFun, PFun, PVar, PVar, PFun, PVar, PFun, PFun
231− , PVar, PVar, PVar ])
232−
233− test "test #3" do
309+ ( l
310+ [ PForAll 3
311+ , PFun
312+ , PFun
313+ , PVar
314+ , PVar
315+ , PFun
316+ , PVar
317+ , PFun
318+ , PFun
319+ , PVar
320+ , PVar
321+ , PVar
322+ ]
323+ )
324+
325+ it "test #3" do
234326 let input = "forall a. (a -> b) -> (b -> ((a -> b) -> c))"
235327 assertRight (shapeOfTypeQuery <$> parseTypeQuery input)
236− (l [ PForAll 3, PFun, PFun, PVar, PVar, PFun, PVar, PFun, PFun
237− , PVar, PVar, PVar ])
238−
239− test "test #4" do
328+ ( l
329+ [ PForAll 3
330+ , PFun
331+ , PFun
332+ , PVar
333+ , PVar
334+ , PFun
335+ , PVar
336+ , PFun
337+ , PFun
338+ , PVar
339+ , PVar
340+ , PVar
341+ ]
342+ )
343+
344+ it "test #4" do
240345 let input = "forall a. (forall h. ST h (STArray h a)) -> Array a"
241346 assertRight (shapeOfTypeQuery <$> parseTypeQuery input)
242− (l [ PForAll 1, PFun, PForAll 1, PApp, PApp, PVar, PVar, PApp, PApp, PVar, PVar, PVar, PApp, PVar, PVar ])
243−
244− suite "type shapes" do
245− test "test #1" do
246− let query = "Generic a rep => GenericEq rep => a -> a -> Boolean"
247− c1 = constr (qname [""] "Generic") [TypeVar "a", TypeVar "rep"]
248− c2 = constr (qname [""] "GenericEq") [TypeVar "rep"]
249−
250− fun t1 t2 =
251− TypeApp (TypeApp (TypeConstructor (QualifiedName { moduleNameParts: ["Prim"]
252− , name: Identifier "Function" })) t1) t2
253− type_ =
254− ForAll "a" Nothing $
255− ForAll "rep" Nothing $
256− ConstrainedType c1
257− (ConstrainedType c2
258− (fun (TypeVar "a") (fun (TypeVar "b")
259− (TypeConstructor $ qname ["Prim", "Boolean"] "Boolean"))))
260− shape = shapeOfTypeQuery <$> parseTypeQuery query
261− Assert.equal (pure $ shapeOfType type_) shape
262−
263− suite "free variable counting" do
264−
265− test "test #1" do
347+ (l [ PForAll 1, PFun, PForAll 1, PApp, PApp, PVar, PVar, PApp, PApp, PVar, PVar, PVar, PApp, PVar, PVar ])
348+
349+ describe "type shapes" do
350+ it "test #1" do
351+ let
352+ query = "Generic a rep => GenericEq rep => a -> a -> Boolean"
353+ c1 = constr (qname [ "" ] "Generic") [ TypeVar "a", TypeVar "rep" ]
354+ c2 = constr (qname [ "" ] "GenericEq") [ TypeVar "rep" ]
355+
356+ fun t1 t2 =
357+ TypeApp
358+ ( TypeApp
359+ ( TypeConstructor
360+ ( QualifiedName
361+ { moduleNameParts: [ "Prim" ]
362+ , name: Identifier "Function"
363+ }
364+ )
365+ )
366+ t1
367+ )
368+ t2
369+ type_ =
370+ ForAll "a" Nothing
371+ $ ForAll "rep" Nothing
372+ $
373+ ConstrainedType c1
374+ ( ConstrainedType c2
375+ ( fun (TypeVar "a")
376+ ( fun (TypeVar "b")
377+ (TypeConstructor $ qname [ "Prim", "Boolean" ] "Boolean")
378+ )
379+ )
380+ )
381+ shape = shapeOfTypeQuery <$> parseTypeQuery query
382+ pure (shapeOfType type_) `shouldEqual` shape
383+
384+ describe "free variable counting" do
385+
386+ it "test #1" do
266387 let input = "forall a. (a -> b) -> (b -> ((a -> b) -> c))"
267388 assertRight (countFreeVars <$> parseTypeQuery input) 2
268389
269− test "test #2" do
390+ it "test #2" do
270391 -- `b` is not bound on the left, `a` is not bound on the right
271392 let input = "(forall a. (a -> b)) -> forall b. (b -> a)"
272393 assertRight (countFreeVars <$> parseTypeQuery input) 2
273394
274− test "test #3" do
395+ it "test #3" do
275396 let input = "a -> forall a. a"
276397 assertRight (countFreeVars <$> parseTypeQuery input) 1
277398
278− test "test #4" do
399+ it "test #4" do
279400 let input = "(forall a. a) -> a"
280401 assertRight (countFreeVars <$> parseTypeQuery input) 1
281402
282− test "test #5" do
403+ it "test #5" do
283404 let input = "forall a. a -> a"
284405 assertRight (countFreeVars <$> parseTypeQuery input) 0
285406
286− test "test #6" do
407+ it "test #6" do
287408 let input = "a -> b -> c"
288409 assertRight (countFreeVars <$> parseTypeQuery input) 3
289410
290− test "test #7" do
411+ it "test #7" do
291412 let input = "forall m a. Monad m => a -> m a"
292413 assertRight (countFreeVars <$> parseTypeQuery input) 0
293414
294− test "test #8" do
415+ it "test #8" do
295416 let input = "Monad m => a -> m a"
296417 assertRight (countFreeVars <$> parseTypeQuery input) 2
297418
298− test "test #9" do
419+ it "test #9" do
299420 let input = "Monad m => a -> a"
300421 assertRight (countFreeVars <$> parseTypeQuery input) 2
301422
302− test "test #10" do
423+ it "test #10" do
303424 let input = "forall a. (forall a. a) a"
304425 assertRight (countFreeVars <$> parseTypeQuery input) 0
305426
306− test "test #11" do
427+ it "test #11" do
307428 let input = "forall a. (forall b. a) a"
308429 assertRight (countFreeVars <$> parseTypeQuery input) 0
309430
310− test "test #12" do
431+ it "test #12" do
311432 let input = "forall a. (forall b. a) a b"
312433 assertRight (countFreeVars <$> parseTypeQuery input) 1
313434
314− suite "typeVarPenalty" do
315−
316− test "#0" do
317− Assert.equal 0 (typeVarPenalty mempty)
318−
319− test "#1" do
320− Assert.equal 0 (typeVarPenalty $ l [ substitute "a" "b"
321− , substitute "b" "a"
322− ])
323−
324− test "#2" do
325− Assert.equal 0 (typeVarPenalty $ l [ substitute "a" "b"
326− , substitute "a" "b"
327− , substitute "a" "b"
328− ])
329−
330− test "#3" do
331− Assert.equal 1 (typeVarPenalty $ l [ substitute "a" "b"
332− , substitute "a" "c"
333− ])
334−
335− test "#4" do
336− Assert.equal 1 (typeVarPenalty $ l [ substitute "a" "b"
337− , substitute "b" "a"
338− , substitute "b" "c"
339− ])
340−
341− test "#5" do
342− Assert.equal 0 (typeVarPenalty $ l [ substitute "a" "b"
343− , substitute "b" "c"
344− , substitute "c" "a"
345− ])
346−
347− test "#6" do
348− Assert.equal 2 (typeVarPenalty $ l [ substitute "a" "b"
349− , substitute "a" "c"
350− , substitute "a" "a"
351− ])
352−
353− test "#7" do
354− Assert.equal 2 (typeVarPenalty $ l [ substitute "a" "a"
355− , substitute "b" "a"
356− , substitute "c" "a"
357− ])
358−
359− test "#8" do
360− Assert.equal 4 (typeVarPenalty $ l [ substitute "a" "a"
361− , substitute "b" "a"
362− , substitute "c" "a"
363− , substitute "a" "b"
364− , substitute "a" "c"
365− , substitute "a" "a"
366− ])
367−
368− test "#9" do
369− Assert.equal 0 (typeVarPenalty $ l [ substitute "a" "e"
370− , substitute "b" "d"
371− , substitute "c" "f"
372− ])
373−
374− suite "unification" do
375− test "instantiation #0" do
376− let mVarQuery = qVar "m"
377− unitConstQuery = qConst "Unit"
378−
379− Assert.assert "instantiation #0" $
380− (penalty unitConstQuery unitType < penalty mVarQuery unitType)
381−
382− test "generalization #0" do
383− let query = qVar "m"
384− t1 = TypeVar "m"
385−
386− Assert.assert "qeneralization #0" $
387− (penalty query unitType > penalty query t1)
388−
435+ describe "typeVarPenalty" do
436+
437+ it "#0" do
438+ 0 `shouldEqual` (typeVarPenalty mempty)
439+
440+ it "#1" do
441+ 0 `shouldEqual`
442+ ( typeVarPenalty $ l
443+ [ substitute "a" "b"
444+ , substitute "b" "a"
445+ ]
446+ )
447+
448+ it "#2" do
449+ 0 `shouldEqual`
450+ ( typeVarPenalty $ l
451+ [ substitute "a" "b"
452+ , substitute "a" "b"
453+ , substitute "a" "b"
454+ ]
455+ )
456+
457+ it "#3" do
458+ 1 `shouldEqual`
459+ ( typeVarPenalty $ l
460+ [ substitute "a" "b"
461+ , substitute "a" "c"
462+ ]
463+ )
464+
465+ it "#4" do
466+ 1 `shouldEqual`
467+ ( typeVarPenalty $ l
468+ [ substitute "a" "b"
469+ , substitute "b" "a"
470+ , substitute "b" "c"
471+ ]
472+ )
473+
474+ it "#5" do
475+ 0 `shouldEqual`
476+ ( typeVarPenalty $ l
477+ [ substitute "a" "b"
478+ , substitute "b" "c"
479+ , substitute "c" "a"
480+ ]
481+ )
482+
483+ it "#6" do
484+ 2 `shouldEqual`
485+ ( typeVarPenalty $ l
486+ [ substitute "a" "b"
487+ , substitute "a" "c"
488+ , substitute "a" "a"
489+ ]
490+ )
491+
492+ it "#7" do
493+ 2 `shouldEqual`
494+ ( typeVarPenalty $ l
495+ [ substitute "a" "a"
496+ , substitute "b" "a"
497+ , substitute "c" "a"
498+ ]
499+ )
500+
501+ it "#8" do
502+ 4 `shouldEqual`
503+ ( typeVarPenalty $ l
504+ [ substitute "a" "a"
505+ , substitute "b" "a"
506+ , substitute "c" "a"
507+ , substitute "a" "b"
508+ , substitute "a" "c"
509+ , substitute "a" "a"
510+ ]
511+ )
512+
513+ it "#9" do
514+ 0 `shouldEqual`
515+ ( typeVarPenalty $ l
516+ [ substitute "a" "e"
517+ , substitute "b" "d"
518+ , substitute "c" "f"
519+ ]
520+ )
521+
522+ describe "unification" do
523+ it "instantiation #0" do
524+ let
525+ mVarQuery = qVar "m"
526+ unitConstQuery = qConst "Unit"
527+
528+ shouldSatisfy
529+ (penalty unitConstQuery unitType)
530+ (_ < penalty mVarQuery unitType)
531+
532+ it "generalization #0" do
533+ let
534+ query = qVar "m"
535+ t1 = TypeVar "m"
536+
537+ shouldSatisfy
538+ (penalty query unitType)
539+ (_ > penalty query t1)
389540
390541l :: forall f. Foldable f => (forall a. f a -> List a)
391542l = List.fromFoldable
392543
393544nl
394545 :: forall t
395− . Foldable t
546+ . Foldable t
396547 => Functor t
397548 => String
398549 -> t String
@@ -400,9 +551,12 @@ nl
400551nl x rst = NonEmptyList.cons' (Identifier x) $ List.fromFoldable (rst <#> Identifier)
401552
402553unitType :: Type
403−unitType = TypeConstructor (QualifiedName { moduleNameParts: []
404− , name: Identifier "Unit"
405− })
554+unitType = TypeConstructor
555+ ( QualifiedName
556+ { moduleNameParts: []
557+ , name: Identifier "Unit"
558+ }
559+ )
406560
407561countFreeVars :: TypeQuery -> Int
408562countFreeVars = getFreeVariables >>> Set.size