1 {-# LANGUAGE UndecidableInstances #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3 module Typing.Test where
6 import Test.Tasty.HUnit
8 import Control.Applicative (Applicative(..))
9 import Control.Arrow (left)
10 import Data.Map.Strict (Map)
11 import Data.Maybe (isJust)
12 import Data.NonNull (NonNull)
14 import Data.Ratio (Ratio)
15 import Data.Text (Text)
16 import Data.List.NonEmpty (NonEmpty)
17 import GHC.Exts (Constraint)
18 import Prelude hiding (exp)
19 import qualified Data.Function as Fun
20 import qualified Data.Map.Strict as Map
21 import qualified Data.MonoTraversable as MT
22 import qualified Data.Sequences as Seqs
23 import qualified System.IO as IO
24 import qualified Text.Megaparsec as P
26 import Language.Symantic.Grammar
27 import Language.Symantic
28 import Language.Symantic.Lib hiding ((<$>), (<*), show)
30 import Grammar.Megaparsec
61 , Proxy MT.MonoFoldable
62 , Proxy MT.MonoFunctor
66 , Proxy Seqs.IsSequence
67 , Proxy Seqs.SemiSequence
71 type SRC = SrcTe (NonEmpty P.SourcePos) SS
74 , Gram_Source src (P.ParsecT e s m)
75 ) => Gram_Type src (P.ParsecT e s m)
77 cs :: Source src => Name2Type src
80 (Len2Type $ \len -> TypeT $
81 tyConstLen @(K []) @[] len `tyApp`
82 tyConstLen @(K Char) @Char len) $
83 inj_Name2Type (Proxy @SS)
86 tests = testGroup "Typing" $
87 [ testGroup "readTy" $
88 let run inp (TypeT exp :: TypeT SRC '[]) =
89 testCase inp $ got @?= Right (Right $ TypeVT exp)
91 got :: Either (P.ParseError Char P.Dec)
92 (Either (Error_Type SRC) (TypeVT SRC))
93 got = readTy cs <$> P.runParser (unCF g) "" inp
94 g :: Gram_Type SRC g => CF g (AST_Type SRC)
96 let (==>) = run; infixr 0 ==> in
97 [ "Bool" ==> TypeT $ tyBool
98 , "(->) Bool" ==> TypeT $ tyFun `tyApp` tyBool
99 , "[]" ==> TypeT $ tyConst @(K []) @[]
100 , "[Char]" ==> TypeT $ tyList tyChar
101 , "[Char -> [Char]]" ==> TypeT $ tyList (tyChar ~> tyList tyChar)
102 , "([])" ==> TypeT $ tyConst @(K []) @[]
103 , "[()]" ==> TypeT $ tyList tyUnit
104 , "()" ==> TypeT $ tyUnit
105 , "(())" ==> TypeT $ tyUnit
106 , "(,)" ==> TypeT $ tyConst @(K (,)) @(,)
107 , "((,))" ==> TypeT $ tyConst @(K (,)) @(,)
108 , "(,) Int" ==> TypeT $ tyConst @(K (,)) @(,) `tyApp` tyInt
109 , "(Bool)" ==> TypeT $ tyBool
110 , "((Bool))" ==> TypeT $ tyBool
111 , "(Bool, Int)" ==> TypeT $ tyBool `tyTuple2` tyInt
112 , "((Bool, Int))" ==> TypeT $ tyBool `tyTuple2` tyInt
113 , "((Bool, Int), Char)" ==> TypeT $ (tyBool `tyTuple2` tyInt) `tyTuple2` tyChar
114 , "(Bool, Int) -> Char" ==> TypeT $ (tyBool `tyTuple2` tyInt) ~> tyChar
115 , "(Bool -> Int)" ==> TypeT $ tyBool ~> tyInt
116 , "String" ==> TypeT $ tyList tyChar
117 , "[Char] -> String" ==> TypeT $ tyList tyChar ~> tyList tyChar
118 , "String -> [Char]" ==> TypeT $ tyList tyChar ~> tyList tyChar
119 , "Maybe Bool" ==> TypeT $ tyMaybe tyBool
120 , "Either Bool Int" ==> TypeT $ tyEither tyBool tyInt
121 , "Bool -> Int" ==> TypeT $ tyBool ~> tyInt
122 , "(Bool -> Int) -> Char" ==> TypeT $ (tyBool ~> tyInt) ~> tyChar
123 , "Bool -> (Int -> Char)" ==> TypeT $ tyBool ~> (tyInt ~> tyChar)
124 , "Bool -> Int -> Char" ==> TypeT $ tyBool ~> tyInt ~> tyChar
125 , "Bool -> (Int -> Char) -> ()" ==> TypeT $ tyBool ~> (tyInt ~> tyChar) ~> tyUnit
126 , "IO" ==> TypeT $ tyConst @(K IO) @IO
127 , "Traversable IO" ==> TypeT $ tyTraversable (tyConst @(K IO) @IO)
128 , "Monad IO" ==> TypeT $ tyMonad (tyConst @(K IO) @IO)
129 , "(->) (IO Bool)" ==> TypeT $ tyConst @(K (->)) @(->) `tyApp` (tyIO tyBool)
130 , "Monad IO" ==> TypeT $ tyMonad (tyConst @(K IO) @IO)
131 , "Eq" ==> TypeT $ tyConst @(K Eq) @Eq
132 , "Eq Bool" ==> TypeT $ tyEq tyBool
134 , testGroup "Parsing errors" $
135 let run inp = testCase inp $ got @?= Left ()
137 got :: Either () (AST_Type SRC)
138 got = left (\(_::P.ParseError (P.Token String) P.Dec) -> ()) $ P.runParser (unCF g) "" inp
139 g :: Gram_Type SRC g => CF g (AST_Type SRC)
143 , "(Bool -> Int) Char"
145 , testGroup "Compiling errors" $
146 let run inp = testCase inp $ got @?= Right (Left ())
148 got :: Either (P.ParseError Char P.Dec) (Either () (TypeVT SRC))
149 got = left (Fun.const ()) . readTy cs <$> P.runParser (unCF g) "" inp
150 g :: Gram_Type SRC g => CF g (AST_Type SRC)
157 , "(->) Bool Int Char"
160 , testGroup "proveConstraint" $
161 let (==>) (typ::Type SRC '[] (t::Constraint)) expected =
162 testCase (show typ) $
163 isJust (proveConstraint typ) @?= expected in
164 [ tyEq tyBool ==> True
165 , tyOrd tyBool ==> True
166 , tyFunctor (tyConst @(K Maybe) @Maybe) ==> True
167 , tyFunctor (tyConst @(K IO) @IO) ==> True
168 , tyMonad (tyConst @(K IO) @IO) ==> True
169 , tyTraversable (tyConst @(K IO) @IO) ==> False