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