]> 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_App lam
36 .|. Expr_Lambda_Val lam
37 .|. Expr_List lam
38 .|. Expr_Maybe lam
39 .|. Expr_Int
40 .|. Expr_Bool
41 .|. Expr_Functor lam
42 .|. Expr_Applicative lam
43 )
44 ex_from = root_expr_from (Proxy::Proxy (Ex lam)) (Proxy::Proxy lam)
45
46 (==>) ast expected =
47 testCase (show ast) $
48 case ex_from ast of
49 Left err -> Left err @?= snd `left` expected
50 Right (Exists_Type_and_Repr ty (Forall_Repr r)) ->
51 case expected of
52 Left (_, err) -> Right ("…"::String) @?= Left err
53 Right (ty_expected::Type_Root_of_Expr (Ex IO) h, _::h, _::Text) ->
54 (>>= (@?= (\(_::Proxy h, err) -> err) `left` expected)) $
55 case ty `eq_type` ty_expected of
56 Nothing -> return $ Left $
57 error_expr (Proxy::Proxy (Ex IO)) $
58 Error_Expr_Type_mismatch ast
59 (Exists_Type ty)
60 (Exists_Type ty_expected)
61 Just Refl -> do
62 h <- host_from_expr r
63 return $
64 Right
65 ( ty
66 , h
67 , text_from_expr r
68 -- , (text_from_expr :: Repr_String IO h -> Text) r
69 )
70
71 tests :: TestTree
72 tests = testGroup "Applicative"
73 [ AST "<*>"
74 [ AST "<$>"
75 [ AST "val"
76 [ AST "x" [], AST "Int" []
77 , AST "val"
78 [ AST "y" [], AST "Int" []
79 , AST "+" [ AST "var" [AST "x" []]
80 , AST "var" [AST "y" []] ]
81 ]
82 ]
83 , AST "just" [ AST "int" [AST "1" []] ]
84 ]
85 , AST "just" [ AST "int" [AST "2" []] ]
86 ] ==> Right
87 ( type_maybe type_int
88 , Just 3
89 , "fmap (\\x0 -> (\\x1 -> x0 + x1)) (just 1) <*> just 2" )
90 ]