1 {-# LANGUAGE AllowAmbiguousTypes #-}
2 {-# LANGUAGE PolyKinds #-}
3 {-# LANGUAGE TypeInType #-}
4 {-# LANGUAGE UndecidableInstances #-}
5 {-# OPTIONS_GHC -fno-warn-orphans #-}
6 module Testing.Compiling where
9 import Test.Tasty.HUnit
10 import Debug.Trace (trace)
12 import Control.Applicative (Applicative(..))
13 import Control.Arrow (left)
14 import Control.Monad (Monad(..))
15 import Data.Either (Either(..), either)
16 import Data.Eq (Eq(..))
17 import Data.Function (($), (.), id)
18 import Data.Functor ((<$>))
19 import Data.Functor.Identity (Identity(..))
20 import Data.List ((++))
21 import Data.Maybe (Maybe(..))
22 import Data.Ord (Ord(..))
23 import Data.String (String)
24 import Data.Text (Text)
25 import Data.Tuple (snd)
26 import Data.Type.Equality
27 import Data.Void (Void)
28 import Prelude (error)
29 import Text.Show (Show)
30 import qualified Control.Monad.Classes.Run as MC
31 import qualified Control.Monad.Trans.State.Strict as S
32 import qualified Data.List as List
33 import qualified Data.Text as Text
34 import qualified Text.Megaparsec as P
35 import qualified Text.Show as Show
37 import Language.Symantic.Grammar
38 import Language.Symantic
39 import Language.Symantic.Lib ()
41 import Testing.Megaparsec ()
45 (S.StateT (Imports NameTe, Modules src ss)
46 ((S.StateT (Imports NameTy, ModulesTy src))
54 Gram_Term src ss (G src ss) =>
55 Text -> Either (P.ParseErrorBundle Text Void) (AST_Term src ss)
57 let modsTe :: Modules src ss = either (error . Show.show) id modulesInj in
58 let impsTe = [] `importModules` modsTe in
59 let modsTy = modulesTyInj @ss @src in
60 let impsTy = importTypes @ss [] in
62 MC.evalStateStrict (impsTy, modsTy) $
63 MC.evalStateStrict (impsTe, modsTe) $
65 where g = unCF $ g_term <* eoi
71 , Gram_Term src ss (G src ss)
77 , SourceInj (AST_Type src) src
78 , SourceInj (KindK src) src
79 , SourceInj (TypeT src '[]) src
80 , SourceInj (TypeVT src) src
81 , Syms ss (BetaT View)
86 Either ( Type src '[] t
87 , Either (P.ParseErrorBundle Text Void)
89 (Type src '[] t, t, Text) ->
92 testCase (elide inp) $
93 case reduceTeApp <$> parseTe @ss inp of
94 Left err -> Left (Left err) @?= snd `left` expected
96 case readTerm CtxTyZ ast of
97 Left err -> Left (Right err) @?= snd `left` expected
100 TermVT (Term q t (TeSym te)) ->
102 Left (_, err) -> Right ("…"::Text) @?= Left err
103 Right (ty_expected::Type src '[] t, _::t, _::Text) ->
104 (>>= (@?= (\(_::Type src '[] t, err) -> err) `left` expected)) $
106 LenS{} -> return $ Left $ Right $ Error_Term_polymorphic (TypeVT t)
108 case proveConstraint q of
109 Nothing -> return $ Left $ Right $ Error_Term_proofless $ TypeVT t
111 case t `eqType` ty_expected of
112 Nothing -> return $ Left $ Right $
113 Error_Term_Beta $ Error_Beta_Unify $
114 Error_Unify_mismatch (TypeVT t) (TypeVT ty_expected)
116 return $ Right (t, eval $ te CtxTeZ, view $ betaT $ te CtxTeZ)
118 maybeRight :: Either l r -> Maybe r
119 maybeRight (Right r) = Just r
120 maybeRight Left{} = Nothing
122 elide :: Text -> String
123 elide s | Text.length s > 42 = List.take 42 (Text.unpack s) ++ ['…']
124 elide s = Text.unpack s
126 dbg :: Show a => String -> a -> a
127 dbg msg x = trace (msg ++ " = " ++ Show.show x) x