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