@@ -1,136 +1,228 @@
11module Test.TypeQuery where
22
3−
4−import Data.Argonaut.Decode
5−import Data.Argonaut.Encode
6−import Data.Argonaut.Parser
7−import Data.Either
8−import Data.Maybe
9−import Effect (Effect)
10−import Effect.Aff
11−import Effect.Console (log)
12−import Partial.Unsafe
133import Prelude
144import Spago.Search.DocsJson
15−import Spago.Search.TypeDecoder
165import Spago.Search.TypeQuery
17−import Test.Unit (suite, test, timeout)
18−import Test.Unit.Assert as Assert
19−import Test.Unit.Main (runTest)
20−import Data.List as List
6+import Spago.Search.TypeShape
7+
8+import Data.Either (Either(..), isRight)
9+import Data.Foldable (class Foldable)
2110import Data.List (List(..), (:))
22−import Data.Tuple
23−import Data.Foldable
11+import Data.List as List
2412import Data.List.NonEmpty (NonEmptyList)
2513import Data.List.NonEmpty as NonEmptyList
14+import Data.Set as Set
15+import Data.Tuple (Tuple(..))
16+import Effect.Aff (Aff)
17+import Test.Unit (TestSuite, suite, test)
18+import Test.Unit.Assert as Assert
2619
27−tests =
20+tests :: TestSuite
21+tests = do
2822 suite "TypeQuery parser" do
2923
3024 test "test #0" do
3125 let input = "a"
32− assertRight (parseTypeQuery input) (QAny "a")
26+ assertRight (parseTypeQuery input) (QVar "a")
3327
34− test "test #0.1" do
28+ test "test #1" do
3529 let input = "ab"
36− assertRight (parseTypeQuery input) (QAny "ab")
30+ assertRight (parseTypeQuery input) (QVar "ab")
3731
38− test "test #1.0" do
32+ test "test #2" do
3933 let input = "a b"
40− assertRight (parseTypeQuery input) (QApp (QAny "a") (QAny "b"))
34+ assertRight (parseTypeQuery input) (QApp (QVar "a") (QVar "b"))
4135
42− test "test #1.1" do
36+ test "test #3" do
4337 let input = "a b c"
44− assertRight (parseTypeQuery input) (QApp (QApp (QAny "a") (QAny "b")) (QAny "c"))
38+ assertRight (parseTypeQuery input) (QApp (QApp (QVar "a") (QVar "b")) (QVar "c"))
4539
46− test "test #2" do
40+ test "test #4" do
4741 let input = "a -> b"
48− assertRight (parseTypeQuery input) (QFun (QAny "a") (QAny "b"))
42+ assertRight (parseTypeQuery input) (QFun (QVar "a") (QVar "b"))
4943
50− test "test #3" do
44+ test "test #5" do
5145 let input = "a -> b c"
52− assertRight (parseTypeQuery input) (QFun (QAny "a") (QApp (QAny "b") (QAny "c")))
46+ assertRight (parseTypeQuery input) (QFun (QVar "a") (QApp (QVar "b") (QVar "c")))
5347
54− test "test #4" do
48+ test "test #6" do
5549 let input = "a b -> c"
56− assertRight (parseTypeQuery input) (QFun (QApp (QAny "a") (QAny "b")) (QAny "c"))
50+ assertRight (parseTypeQuery input) (QFun (QApp (QVar "a") (QVar "b")) (QVar "c"))
5751
58− test "test #5" do
52+ test "test #7" do
5953 let input = "a b"
60− assertRight (parseTypeQuery input) (QApp (QAny "a") (QAny "b"))
54+ assertRight (parseTypeQuery input) (QApp (QVar "a") (QVar "b"))
6155
62− test "test #6" do
56+ test "test #8" do
6357 let input = "a (b c)"
64− assertRight (parseTypeQuery input) (QApp (QAny "a") (QApp (QAny "b") (QAny "c")))
58+ assertRight (parseTypeQuery input) (QApp (QVar "a") (QApp (QVar "b") (QVar "c")))
6559
66− test "test #6.1" do
60+ test "test #9" do
6761 let input = "(a b) (c d)"
6862 assertRight (parseTypeQuery input)
69− (QApp (QApp (QAny "a") (QAny "b"))
70− (QApp (QAny "c") (QAny "d")))
63+ (QApp (QApp (QVar "a") (QVar "b"))
64+ (QApp (QVar "c") (QVar "d")))
7165
72− test "test #7" do
66+ test "test #10" do
7367 let input = "a ( b c )"
74− assertRight (parseTypeQuery input) (QApp (QAny "a") (QApp (QAny "b") (QAny "c")))
68+ assertRight (parseTypeQuery input) (QApp (QVar "a") (QApp (QVar "b") (QVar "c")))
7569
76− test "test #8" do
70+ test "test #11" do
7771 let input = "aaa"
78− assertRight (parseTypeQuery input) (QAny "aaa")
72+ assertRight (parseTypeQuery input) (QVar "aaa")
7973
80− test "test #9" do
74+ test "test #12" do
8175 let input = "aaa ( bbb ccc )"
82− assertRight (parseTypeQuery input) (QApp (QAny "aaa") (QApp (QAny "bbb") (QAny "ccc")))
76+ assertRight (parseTypeQuery input) (QApp (QVar "aaa") (QApp (QVar "bbb") (QVar "ccc")))
8377
84− test "test #10" do
78+ test "test #13" do
8579 let input = "(a -> b) -> (c -> d)"
86− assertRight (parseTypeQuery input) (QFun (QFun (QAny "a") (QAny "b"))
87− (QFun (QAny "c") (QAny "d")))
80+ assertRight (parseTypeQuery input) (QFun (QFun (QVar "a") (QVar "b"))
81+ (QFun (QVar "c") (QVar "d")))
8882
89− test "test #11" do
83+ test "test #14" do
9084 let input = "a -> b -> c -> d"
91− assertRight (parseTypeQuery input) (QFun (QAny "a")
92− (QFun (QAny "b")
93− (QFun (QAny "c") (QAny "d"))))
85+ assertRight (parseTypeQuery input) (QFun (QVar "a")
86+ (QFun (QVar "b")
87+ (QFun (QVar "c") (QVar "d"))))
9488
95− test "test #11.1" do
89+ test "test #15" do
9690 let input = "a -> b -> c"
97− assertRight (parseTypeQuery input) (QFun (QAny "a")
98− (QFun (QAny "b")
99− (QAny "c")))
91+ assertRight (parseTypeQuery input) (QFun (QVar "a")
92+ (QFun (QVar "b")
93+ (QVar "c")))
10094
101− test "test #12" do
95+ test "test #16" do
10296 let input = "forall a b c. c"
103− assertRight (parseTypeQuery input) (QForAll (nl "a" ["b", "c"]) (QAny "c"))
97+ assertRight (parseTypeQuery input) (QForAll (nl "a" ["b", "c"]) (QVar "c"))
10498
105− test "test #13" do
99+ test "test #17" do
106100 let input = "forall a. Maybe a"
107− assertRight (parseTypeQuery input) (QForAll (nl "a" $ []) (QApp (QConcrete "Maybe") (QAny "a")))
101+ assertRight (parseTypeQuery input) (QForAll (nl "a" $ []) (QApp (QConst "Maybe") (QVar "a")))
108102
109− test "test #14" do
103+ test "test #18" do
110104 let input = "forall m a. Monad m => a -> m a"
111105 assertRight (parseTypeQuery input)
112106 (QForAll (nl "m" ["a"])
113107 (QConstraint "Monad" (l ["m"])
114− (QFun (QAny "a")
115− (QApp (QAny "m") (QAny "a")))))
108+ (QFun (QVar "a")
109+ (QApp (QVar "m") (QVar "a")))))
116110
117− test "test #15" do
111+ test "test #19" do
118112 let input = "{ a :: Int }"
119113 assertRight (parseTypeQuery input)
120− (QRow (pure (Tuple "a" (QConcrete "Int"))))
114+ (QRow (pure (Tuple "a" (QConst "Int"))))
121115
122− test "test #16" do
116+ test "test #20" do
123117 let input = "{a::Int}"
124118 assertRight (parseTypeQuery input)
125− (QRow (pure (Tuple "a" (QConcrete "Int"))))
119+ (QRow (pure (Tuple "a" (QConst "Int"))))
126120
127− test "test #17" do
121+ test "test #21" do
128122 let input = "Int"
129− assertRight (parseTypeQuery input) (QConcrete "Int")
123+ assertRight (parseTypeQuery input) (QConst "Int")
130124
131− test "test #18" do
125+ test "test #22" do
132126 let input = "a->b"
133− assertRight (parseTypeQuery input) (QFun (QAny "a") (QAny "b"))
127+ assertRight (parseTypeQuery input) (QFun (QVar "a") (QVar "b"))
128+
129+ test "test #23" do
130+ let input = "forall m a. MonadRec m => Process m a -> m a"
131+ assertRight (parseTypeQuery input) (QForAll (nl "m" ("a" : Nil))
132+ (QConstraint "MonadRec" ("m" : Nil)
133+ (QFun (QApp (QApp (QConst "Process")
134+ (QVar "m")) (QVar "a"))
135+ (QApp (QVar "m") (QVar "a")))))
136+
137+ test "test #24" do
138+ let input = "forall t f a. Foldable1 t => Apply f => f"
139+ assertRight (parseTypeQuery input) (QForAll (nl "t" ["f", "a"])
140+ (QConstraint "Foldable1" (l ["t"])
141+ (QConstraint "Apply" ("f" : Nil) (QVar "f"))))
142+
143+ test "test #25" do
144+ let input = "forall m a.MonadRec m=>Process m a->m a"
145+ assertRight (parseTypeQuery input) ((QForAll (nl "m" ("a" : Nil))
146+ (QConstraint "MonadRec" ("m" : Nil)
147+ (QFun (QApp (QApp (QConst "Process")
148+ (QVar "m")) (QVar "a"))
149+ (QApp (QVar "m") (QVar "a"))))))
150+
151+ suite "polish notation" do
152+
153+ test "test #1" do
154+ let input = "(a -> b) -> (b -> ((a -> b) -> c))"
155+ assertRight (shapeOfTypeQuery <$> parseTypeQuery input)
156+ (l [ PForAll 3, PFun, PFun, PVar, PVar, PFun, PVar, PFun, PFun
157+ , PVar, PVar, PVar ])
158+
159+ test "test #2" do
160+ let input = "forall a. (a -> b) -> (b -> ((a -> b) -> c))"
161+ assertRight (shapeOfTypeQuery <$> parseTypeQuery input)
162+ (l [ PForAll 3, PFun, PFun, PVar, PVar, PFun, PVar, PFun, PFun
163+ , PVar, PVar, PVar ])
164+
165+ test "test #3" do
166+ let input = "forall a. (a -> b) -> (b -> ((a -> b) -> c))"
167+ assertRight (shapeOfTypeQuery <$> parseTypeQuery input)
168+ (l [ PForAll 3, PFun, PFun, PVar, PVar, PFun, PVar, PFun, PFun
169+ , PVar, PVar, PVar ])
170+
171+ test "test #4" do
172+ let input = "forall a. (forall h. ST h (STArray h a)) -> Array a"
173+ assertRight (shapeOfTypeQuery <$> parseTypeQuery input)
174+ (l [ PForAll 1, PFun, PForAll 1, PApp, PApp, PVar, PVar, PApp, PApp, PVar, PVar, PVar, PApp, PVar, PVar ])
175+
176+ suite "free variable counting" do
177+
178+ test "test #1" do
179+ let input = "forall a. (a -> b) -> (b -> ((a -> b) -> c))"
180+ assertRight (countFreeVars <$> parseTypeQuery input) 2
181+
182+ test "test #2" do
183+ -- `b` is not bound on the left, `a` is not bound on the right
184+ let input = "(forall a. (a -> b)) -> forall b. (b -> a)"
185+ assertRight (countFreeVars <$> parseTypeQuery input) 2
186+
187+ test "test #3" do
188+ let input = "a -> forall a. a"
189+ assertRight (countFreeVars <$> parseTypeQuery input) 1
190+
191+ test "test #4" do
192+ let input = "(forall a. a) -> a"
193+ assertRight (countFreeVars <$> parseTypeQuery input) 1
194+
195+ test "test #5" do
196+ let input = "forall a. a -> a"
197+ assertRight (countFreeVars <$> parseTypeQuery input) 0
198+
199+ test "test #6" do
200+ let input = "a -> b -> c"
201+ assertRight (countFreeVars <$> parseTypeQuery input) 3
202+
203+ test "test #7" do
204+ let input = "forall m a. Monad m => a -> m a"
205+ assertRight (countFreeVars <$> parseTypeQuery input) 0
206+
207+ test "test #8" do
208+ let input = "Monad m => a -> m a"
209+ assertRight (countFreeVars <$> parseTypeQuery input) 2
210+
211+ test "test #9" do
212+ let input = "Monad m => a -> a"
213+ assertRight (countFreeVars <$> parseTypeQuery input) 2
214+
215+ test "test #10" do
216+ let input = "forall a. (forall a. a) a"
217+ assertRight (countFreeVars <$> parseTypeQuery input) 0
218+
219+ test "test #11" do
220+ let input = "forall a. (forall b. a) a"
221+ assertRight (countFreeVars <$> parseTypeQuery input) 0
222+
223+ test "test #12" do
224+ let input = "forall a. (forall b. a) a b"
225+ assertRight (countFreeVars <$> parseTypeQuery input) 1
134226
135227l :: forall f. Foldable f => (forall a. f a -> List a)
136228l = List.fromFoldable
@@ -153,3 +245,6 @@ assertRight eiActual expected =
153245 Assert.equal (Right expected) eiActual
154246 Right actual -> do
155247 Assert.equal (Right expected) eiActual
248+
249+countFreeVars :: TypeQuery -> Int
250+countFreeVars = getFreeVariables >>> Set.size