]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/Expr/Traversable/Test.hs
init
[haskell/symantic.git] / Language / Symantic / Expr / Traversable / Test.hs
1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE GADTs #-}
4 {-# LANGUAGE NoMonomorphismRestriction #-}
5 {-# LANGUAGE ScopedTypeVariables #-}
6 {-# LANGUAGE TypeOperators #-}
7 {-# OPTIONS_GHC -fno-warn-missing-signatures #-}
8
9 module Expr.Applicative.Test where
10
11 import Test.Tasty
12 import Test.Tasty.HUnit
13
14 import qualified Control.Arrow as Arrow
15 import qualified Control.Monad as Monad
16 import Data.Functor.Identity
17 import Data.Proxy (Proxy(..))
18 import Data.Text (Text)
19 import Data.Type.Equality ((:~:)(Refl))
20 import Prelude hiding ((&&), not, (||), (==), (<$>), (+), Applicative(..), (>>=))
21
22 import Language.Symantic.Type
23 import Language.Symantic.Expr as Expr
24 import Language.Symantic.Repr
25
26 import AST.Test
27
28 -- * Expressions
29 t = bool True
30 f = bool False
31 e1 = val (\x -> val $ \y -> x + y)
32 <$> just (int 1)
33 <*> just (int 2)
34
35 -- * Tests
36 type Ex lam = Expr_Root
37 ( Expr_Lambda_App lam
38 .|. Expr_Lambda_Val lam
39 .|. Expr_List lam
40 .|. Expr_Maybe lam
41 .|. Expr_Int
42 .|. Expr_Bool
43 .|. Expr_Functor lam
44 .|. Expr_Applicative lam
45 .|. Expr_Traversable lam
46 )
47 ex_from = root_expr_from (Proxy::Proxy (Ex lam)) (Proxy::Proxy lam)
48
49 (==>) ast expected =
50 testCase (show ast) $
51 case ex_from ast of
52 Left err -> Left err @?= snd `Arrow.left` expected
53 Right (Exists_Type_and_Repr ty (Forall_Repr r)) ->
54 case expected of
55 Left (_, err) -> Right ("…"::String) @?= Left err
56 Right (ty_expected::Type_Root_of_Expr (Ex Identity) h, _::h, _::Text) ->
57 (Monad.>>= (@?= (\(_::Proxy h, err) -> err) `Arrow.left` expected)) $
58 case ty `eq_type` ty_expected of
59 Nothing -> Monad.return $ Left $
60 error_expr (Proxy::Proxy (Ex Identity)) $
61 Error_Expr_Type_mismatch ast
62 (Exists_Type ty)
63 (Exists_Type ty_expected)
64 Just Refl -> do
65 let h = runIdentity $ host_from_expr r
66 Monad.return $
67 Right
68 ( ty
69 , h
70 , text_from_expr r
71 -- , (text_from_expr :: Repr_Text Identity h -> Text) r
72 )
73
74 tests :: TestTree
75 tests = testGroup "Traversable"
76 [ AST "<*>"
77 [ AST "<$>"
78 [ AST "val"
79 [ AST "x" [], AST "Int" []
80 , AST "val"
81 [ AST "y" [], AST "Int" []
82 , AST "+" [ AST "var" [AST "x" []]
83 , AST "var" [AST "y" []] ]
84 ]
85 ]
86 , AST "just" [ AST "int" [AST "1" []] ]
87 ]
88 , AST "just" [ AST "int" [AST "2" []] ]
89 ] ==> Right
90 ( type_maybe type_int
91 , Just 3
92 , "fmap (\\x0 -> (\\x1 -> x0 + x1)) (just 1) <*> just 2" )
93 ]