@@ -3,6 +3,7 @@ module Test.TypeQuery where
33import Docs.Search.TypeDecoder (Constraint(..), QualifiedName(..), Type(..))
44import Docs.Search.TypeQuery (Substitution(..), TypeQuery(..), getFreeVariables, parseTypeQuery, penalty, typeVarPenalty)
55import Docs.Search.TypeShape (ShapeChunk(..), shapeOfType, shapeOfTypeQuery)
6+import Docs.Search.Types (Identifier(..))
67
78import Prelude
89
@@ -25,193 +26,195 @@ tests = do
2526
2627 test "test #0" do
2728 let input = "a"
28− assertRight (parseTypeQuery input) (QVar "a")
29+ assertRight (parseTypeQuery input) (qVar "a")
2930
3031 test "test #1" do
3132 let input = "ab"
32− assertRight (parseTypeQuery input) (QVar "ab")
33+ assertRight (parseTypeQuery input) (qVar "ab")
3334
3435 test "test #2" do
3536 let input = "a b"
36− assertRight (parseTypeQuery input) (QApp (QVar "a") (QVar "b"))
37+ assertRight (parseTypeQuery input) (QApp (qVar "a") (qVar "b"))
3738
3839 test "test #3" do
3940 let input = "a b c"
40− assertRight (parseTypeQuery input) (QApp (QApp (QVar "a") (QVar "b")) (QVar "c"))
41+ assertRight (parseTypeQuery input) (QApp (QApp (qVar "a") (qVar "b")) (qVar "c"))
4142
4243 test "test #4" do
4344 let input = "a -> b"
44− assertRight (parseTypeQuery input) (QFun (QVar "a") (QVar "b"))
45+ assertRight (parseTypeQuery input) (QFun (qVar "a") (qVar "b"))
4546
4647 test "test #5" do
4748 let input = "a -> b c"
48− assertRight (parseTypeQuery input) (QFun (QVar "a") (QApp (QVar "b") (QVar "c")))
49+ assertRight (parseTypeQuery input) (QFun (qVar "a") (QApp (qVar "b") (qVar "c")))
4950
5051 test "test #6" do
5152 let input = "a b -> c"
52− assertRight (parseTypeQuery input) (QFun (QApp (QVar "a") (QVar "b")) (QVar "c"))
53+ assertRight (parseTypeQuery input) (QFun (QApp (qVar "a") (qVar "b")) (qVar "c"))
5354
5455 test "test #7" do
5556 let input = "a b"
56− assertRight (parseTypeQuery input) (QApp (QVar "a") (QVar "b"))
57+ assertRight (parseTypeQuery input) (QApp (qVar "a") (qVar "b"))
5758
5859 test "test #8" do
5960 let input = "a (b c)"
60− assertRight (parseTypeQuery input) (QApp (QVar "a") (QApp (QVar "b") (QVar "c")))
61+ assertRight (parseTypeQuery input) (QApp (qVar "a") (QApp (qVar "b") (qVar "c")))
6162
6263 test "test #9" do
6364 let input = "(a b) (c d)"
6465 assertRight (parseTypeQuery input)
65− (QApp (QApp (QVar "a") (QVar "b"))
66− (QApp (QVar "c") (QVar "d")))
66+ (QApp (QApp (qVar "a") (qVar "b"))
67+ (QApp (qVar "c") (qVar "d")))
6768
6869 test "test #10" do
6970 let input = "a ( b c )"
70− assertRight (parseTypeQuery input) (QApp (QVar "a") (QApp (QVar "b") (QVar "c")))
71+ assertRight (parseTypeQuery input) (QApp (qVar "a") (QApp (qVar "b") (qVar "c")))
7172
7273 test "test #11" do
7374 let input = "aaa"
74− assertRight (parseTypeQuery input) (QVar "aaa")
75+ assertRight (parseTypeQuery input) (qVar "aaa")
7576
7677 test "test #12" do
7778 let input = "aaa ( bbb ccc )"
78− assertRight (parseTypeQuery input) (QApp (QVar "aaa") (QApp (QVar "bbb") (QVar "ccc")))
79+ assertRight (parseTypeQuery input) (QApp (qVar "aaa") (QApp (qVar "bbb") (qVar "ccc")))
7980
8081 test "test #13" do
8182 let input = "(a -> b) -> (c -> d)"
82− assertRight (parseTypeQuery input) (QFun (QFun (QVar "a") (QVar "b"))
83− (QFun (QVar "c") (QVar "d")))
83+ assertRight (parseTypeQuery input) (QFun (QFun (qVar "a") (qVar "b"))
84+ (QFun (qVar "c") (qVar "d")))
8485
8586 test "test #14" do
8687 let input = "a -> b -> c -> d"
87− assertRight (parseTypeQuery input) (QFun (QVar "a")
88− (QFun (QVar "b")
89− (QFun (QVar "c") (QVar "d"))))
88+ assertRight (parseTypeQuery input) (QFun (qVar "a")
89+ (QFun (qVar "b")
90+ (QFun (qVar "c") (qVar "d"))))
9091
9192 test "test #15" do
9293 let input = "a -> b -> c"
93− assertRight (parseTypeQuery input) (QFun (QVar "a")
94− (QFun (QVar "b")
95− (QVar "c")))
94+ assertRight (parseTypeQuery input) (QFun (qVar "a")
95+ (QFun (qVar "b")
96+ (qVar "c")))
9697
9798 test "test #16" do
9899 let input = "forall a b c. c"
99− assertRight (parseTypeQuery input) (QForAll (nl "a" ["b", "c"]) (QVar "c"))
100+ assertRight (parseTypeQuery input) (QForAll (nl "a" ["b", "c"]) (qVar "c"))
100101
101102 test "test #17" do
102103 let input = "forall a. Maybe a"
103− assertRight (parseTypeQuery input) (QForAll (nl "a" $ []) (QApp (QConst "Maybe") (QVar "a")))
104+ assertRight (parseTypeQuery input) (QForAll (nl "a" $ []) (QApp (qConst "Maybe") (qVar "a")))
104105
105106 test "test #18" do
106107 let input = "forall m a. Monad m => a -> m a"
107108 assertRight (parseTypeQuery input)
108109 (QForAll (nl "m" ["a"])
109− (QConstraint "Monad" (l [QVar "m"])
110− (QFun (QVar "a")
111− (QApp (QVar "m") (QVar "a")))))
110+ (qConstraint "Monad" (l [qVar "m"])
111+ (QFun (qVar "a")
112+ (QApp (qVar "m") (qVar "a")))))
112113
113114 test "test #19" do
114115 let input = "{ a :: Int }"
115116 assertRight (parseTypeQuery input)
116− (QApp (QConst "Record") (QRow (pure (Tuple "a" (QConst "Int")))))
117+ (QApp (qConst "Record") (QRow (pure (Tuple (Identifier "a") (qConst "Int")))))
117118
118119 test "test #20" do
119120 let input = "{a::Int}"
120121 assertRight (parseTypeQuery input)
121− (QApp (QConst "Record") (QRow (pure (Tuple "a" (QConst "Int")))))
122+ (QApp (qConst "Record") (QRow (pure (Tuple (Identifier "a") (qConst "Int")))))
122123
123124 test "test #21" do
124125 let input = "Int"
125− assertRight (parseTypeQuery input) (QConst "Int")
126+ assertRight (parseTypeQuery input) (qConst "Int")
126127
127128 test "test #22" do
128129 let input = "a->b"
129− assertRight (parseTypeQuery input) (QFun (QVar "a") (QVar "b"))
130+ assertRight (parseTypeQuery input) (QFun (qVar "a") (qVar "b"))
130131
131132 test "test #23" do
132133 let input = "forall m a. MonadRec m => Process m a -> m a"
133134 assertRight (parseTypeQuery input) (QForAll (nl "m" ("a" : Nil))
134− (QConstraint "MonadRec" (l [QVar "m"])
135− (QFun (QApp (QApp (QConst "Process")
136− (QVar "m")) (QVar "a"))
137− (QApp (QVar "m") (QVar "a")))))
135+ (qConstraint "MonadRec" (l [qVar "m"])
136+ (QFun (QApp (QApp (qConst "Process")
137+ (qVar "m")) (qVar "a"))
138+ (QApp (qVar "m") (qVar "a")))))
138139
139140 test "test #24" do
140141 let input = "forall t f a. Foldable1 t => Apply f => f"
141142 assertRight (parseTypeQuery input) (QForAll (nl "t" ["f", "a"])
142− (QConstraint "Foldable1" (l [QVar "t"])
143− (QConstraint "Apply" (l [QVar "f"]) (QVar "f"))))
143+ (qConstraint "Foldable1" (l [qVar "t"])
144+ (qConstraint "Apply" (l [qVar "f"]) (qVar "f"))))
144145
145146 test "test #25" do
146147 let input = "forall m a.MonadRec m=>Process m a->m a"
147148 assertRight (parseTypeQuery input) ((QForAll (nl "m" ("a" : Nil))
148− (QConstraint "MonadRec" (l [QVar "m"])
149− (QFun (QApp (QApp (QConst "Process")
150− (QVar "m")) (QVar "a"))
151− (QApp (QVar "m") (QVar "a"))))))
149+ (qConstraint "MonadRec" (l [qVar "m"])
150+ (QFun (QApp (QApp (qConst "Process")
151+ (qVar "m")) (qVar "a"))
152+ (QApp (qVar "m") (qVar "a"))))))
152153
153154 test "test #26" do
154155 let input = "m a -> (a -> m b) -> m b"
155− assertRight (parseTypeQuery input) (QFun (QApp (QVar "m") (QVar "a")) (QFun (QFun (QVar "a") (QApp (QVar "m") (QVar "b"))) (QApp (QVar "m") (QVar "b"))))
156+ assertRight (parseTypeQuery input) (QFun (QApp (qVar "m") (qVar "a")) (QFun (QFun (qVar "a") (QApp (qVar "m") (qVar "b"))) (QApp (qVar "m") (qVar "b"))))
156157
157158 test "test #27" do
158159 let input = "forall f a. Alternative f => Lazy (f (List a)) => f a -> f (List a)"
159160 assertRight (parseTypeQuery input) ((QForAll (nl "f" ["a"]))
160− (QConstraint "Alternative" (l [QVar "f"])
161− (QConstraint "Lazy" (l [QApp (QVar "f")
162− (QApp (QConst "List") (QVar "a"))])
163− (QFun (QApp (QVar "f") (QVar "a"))
164− (QApp (QVar "f")
165− (QApp (QConst "List") (QVar "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")))))))
166167
167168 test "test #28" do
168169 let input = "forall f a. Alternative f => Lazy(f (List a))=>f a -> f (List a)"
169170 assertRight (parseTypeQuery input) ((QForAll (nl "f" ["a"]))
170− (QConstraint "Alternative" (l [QVar "f"])
171− (QConstraint "Lazy" (l [QApp (QVar "f")
172− (QApp (QConst "List") (QVar "a"))])
173− (QFun (QApp (QVar "f") (QVar "a"))
174− (QApp (QVar "f")
175− (QApp (QConst "List") (QVar "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")))))))
176177
177178 test "test #29" do
178179 let input = "{a::Int,b::Int}"
179180 assertRight (parseTypeQuery input)
180− (QApp (QConst "Record") (QRow (List.fromFoldable [ Tuple "a" (QConst "Int"), Tuple "b" (QConst "Int")])))
181+ (QApp (qConst "Record") (QRow (List.fromFoldable
182+ [ Tuple (Identifier "a") (qConst "Int")
183+ , Tuple (Identifier "b") (qConst "Int")])))
181184
182185 test "test #30" do
183186 let input = "{record''' :: Int}"
184187 assertRight (parseTypeQuery input)
185− (QApp (QConst "Record") (QRow (List.fromFoldable [ Tuple "record'''" (QConst "Int")])))
188+ (QApp (qConst "Record") (QRow (List.fromFoldable [ Tuple (Identifier "record'''") (qConst "Int")])))
186189
187190 test "test #31" do
188191 let input = "(row''' :: Int)"
189192 assertRight (parseTypeQuery input)
190− (QRow (List.fromFoldable [ Tuple "row'''" (QConst "Int")]))
193+ (QRow (List.fromFoldable [ Tuple (Identifier "row'''") (qConst "Int")]))
191194
192195 test "test #32" do
193196 let input = "(row1 :: Int, row2 :: (),row3::(row4::{}))"
194197 assertRight (parseTypeQuery input)
195− (QRow (l [ Tuple "row1" (QConst "Int")
196− , Tuple "row2" (QRow Nil)
197− , Tuple "row3" (QRow (l [ Tuple "row4" (QApp (QConst "Record") (QRow Nil)) ])) ]))
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)) ])) ]))
198201
199202 test "test #33" do
200203 let input = "Foldable1 t => Apply f => t (f a) -> f Unit"
201204 assertRight (parseTypeQuery input)
202− (QConstraint "Foldable1" ((QVar "t") : Nil) (QConstraint "Apply" ((QVar "f") : Nil) (QFun (QApp (QVar "t") (QApp (QVar "f") (QVar "a"))) (QApp (QVar "f") (QConst "Unit")))))
205+ (qConstraint "Foldable1" ((qVar "t") : Nil) (qConstraint "Apply" ((qVar "f") : Nil) (QFun (QApp (qVar "t") (QApp (qVar "f") (qVar "a"))) (QApp (qVar "f") (qConst "Unit")))))
203206
204207 test "test #34" do
205208 let input = "Foldable1 t => Apply f => t (f a) -> f a"
206209 assertRight (parseTypeQuery input)
207− (QConstraint "Foldable1" ((QVar "t") : Nil) (QConstraint "Apply" ((QVar "f") : Nil) (QFun (QApp (QVar "t") (QApp (QVar "f") (QVar "a"))) (QApp (QVar "f") (QVar "a")))))
210+ (qConstraint "Foldable1" ((qVar "t") : Nil) (qConstraint "Apply" ((qVar "f") : Nil) (QFun (QApp (qVar "t") (QApp (qVar "f") (qVar "a"))) (QApp (qVar "f") (qVar "a")))))
208211
209212 test "test #35" do
210213 let input = "Generic a rep => GenericEq rep => a -> a -> Boolean"
211214 assertRight (parseTypeQuery input)
212− (QConstraint "Generic" ((QVar "a") : (QVar "rep") : Nil)
213− (QConstraint "GenericEq" ((QVar "rep") : Nil)
214− (QFun (QVar "a") (QFun (QVar "a") (QConst "Boolean")))))
215+ (qConstraint "Generic" ((qVar "a") : (qVar "rep") : Nil)
216+ (qConstraint "GenericEq" ((qVar "rep") : Nil)
217+ (QFun (qVar "a") (QFun (qVar "a") (qConst "Boolean")))))
215218
216219 suite "polish notation" do
217220
@@ -246,7 +249,7 @@ tests = do
246249
247250 fun t1 t2 =
248251 TypeApp (TypeApp (TypeConstructor (QualifiedName { moduleNameParts: ["Prim"]
249− , name: "Function" })) t1) t2
252+ , name: Identifier "Function" })) t1) t2
250253 type_ =
251254 ForAll "a" Nothing $
252255 ForAll "rep" Nothing $
@@ -314,70 +317,70 @@ tests = do
314317 Assert.equal 0 (typeVarPenalty mempty)
315318
316319 test "#1" do
317− Assert.equal 0 (typeVarPenalty $ l [ Substitute "a" "b"
318− , Substitute "b" "a"
320+ Assert.equal 0 (typeVarPenalty $ l [ substitute "a" "b"
321+ , substitute "b" "a"
319322 ])
320323
321324 test "#2" do
322− Assert.equal 0 (typeVarPenalty $ l [ Substitute "a" "b"
323− , Substitute "a" "b"
324− , Substitute "a" "b"
325+ Assert.equal 0 (typeVarPenalty $ l [ substitute "a" "b"
326+ , substitute "a" "b"
327+ , substitute "a" "b"
325328 ])
326329
327330 test "#3" do
328− Assert.equal 1 (typeVarPenalty $ l [ Substitute "a" "b"
329− , Substitute "a" "c"
331+ Assert.equal 1 (typeVarPenalty $ l [ substitute "a" "b"
332+ , substitute "a" "c"
330333 ])
331334
332335 test "#4" do
333− Assert.equal 1 (typeVarPenalty $ l [ Substitute "a" "b"
334− , Substitute "b" "a"
335− , Substitute "b" "c"
336+ Assert.equal 1 (typeVarPenalty $ l [ substitute "a" "b"
337+ , substitute "b" "a"
338+ , substitute "b" "c"
336339 ])
337340
338341 test "#5" do
339− Assert.equal 0 (typeVarPenalty $ l [ Substitute "a" "b"
340− , Substitute "b" "c"
341− , Substitute "c" "a"
342+ Assert.equal 0 (typeVarPenalty $ l [ substitute "a" "b"
343+ , substitute "b" "c"
344+ , substitute "c" "a"
342345 ])
343346
344347 test "#6" do
345− Assert.equal 2 (typeVarPenalty $ l [ Substitute "a" "b"
346− , Substitute "a" "c"
347− , Substitute "a" "a"
348+ Assert.equal 2 (typeVarPenalty $ l [ substitute "a" "b"
349+ , substitute "a" "c"
350+ , substitute "a" "a"
348351 ])
349352
350353 test "#7" do
351− Assert.equal 2 (typeVarPenalty $ l [ Substitute "a" "a"
352− , Substitute "b" "a"
353− , Substitute "c" "a"
354+ Assert.equal 2 (typeVarPenalty $ l [ substitute "a" "a"
355+ , substitute "b" "a"
356+ , substitute "c" "a"
354357 ])
355358
356359 test "#8" do
357− Assert.equal 4 (typeVarPenalty $ l [ Substitute "a" "a"
358− , Substitute "b" "a"
359− , Substitute "c" "a"
360− , Substitute "a" "b"
361− , Substitute "a" "c"
362− , Substitute "a" "a"
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"
363366 ])
364367
365368 test "#9" do
366− Assert.equal 0 (typeVarPenalty $ l [ Substitute "a" "e"
367− , Substitute "b" "d"
368− , Substitute "c" "f"
369+ Assert.equal 0 (typeVarPenalty $ l [ substitute "a" "e"
370+ , substitute "b" "d"
371+ , substitute "c" "f"
369372 ])
370373
371374 suite "unification" do
372375 test "instantiation #0" do
373− let mVarQuery = QVar "m"
374− unitConstQuery = QConst "Unit"
376+ let mVarQuery = qVar "m"
377+ unitConstQuery = qConst "Unit"
375378
376379 Assert.assert "instantiation #0" $
377380 (penalty unitConstQuery unitType < penalty mVarQuery unitType)
378381
379382 test "generalization #0" do
380− let query = QVar "m"
383+ let query = qVar "m"
381384 t1 = TypeVar "m"
382385
383386 Assert.assert "qeneralization #0" $
@@ -387,19 +390,37 @@ tests = do
387390l :: forall f. Foldable f => (forall a. f a -> List a)
388391l = List.fromFoldable
389392
390−nl :: forall t5 t6. Foldable t6 => t5 -> t6 t5 -> NonEmptyList t5
391−nl x rst = NonEmptyList.cons' x $ List.fromFoldable rst
393+nl
394+ :: forall t
395+ . Foldable t
396+ => Functor t
397+ => String
398+ -> t String
399+ -> NonEmptyList Identifier
400+nl x rst = NonEmptyList.cons' (Identifier x) $ List.fromFoldable (rst <#> Identifier)
392401
393402unitType :: Type
394403unitType = TypeConstructor (QualifiedName { moduleNameParts: []
395− , name: "Unit"
404+ , name: Identifier "Unit"
396405 })
397406
398407countFreeVars :: TypeQuery -> Int
399408countFreeVars = getFreeVariables >>> Set.size
400409
401410qname :: Array String -> String -> QualifiedName
402−qname m n = QualifiedName { moduleNameParts: m, name: n }
411+qname m n = QualifiedName { moduleNameParts: m, name: Identifier n }
403412
404413constr :: QualifiedName -> Array Type -> Constraint
405414constr c a = Constraint { constraintClass: c, constraintArgs: a }
415+
416+qVar :: String -> TypeQuery
417+qVar = QVar <<< Identifier
418+
419+qConst :: String -> TypeQuery
420+qConst = QConst <<< Identifier
421+
422+qConstraint :: String -> List TypeQuery -> TypeQuery -> TypeQuery
423+qConstraint = QConstraint <<< Identifier
424+
425+substitute :: String -> String -> Substitution
426+substitute a b = Substitute (Identifier a) (Identifier b)