]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/Expr/Monad/Test.hs
polish code, Foldable
[haskell/symantic.git] / Language / Symantic / Expr / Monad / 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.Monad.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 qualified Data.Functor as Functor
17 import Data.Proxy (Proxy(..))
18 import Data.Text (Text)
19 import Data.Type.Equality ((:~:)(Refl))
20 import Prelude hiding ((&&), not, (||), (==), (<$>), (+), Applicative(..), Monad(..), Monad(..))
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 = (>>=) (list $ int Functor.<$> [1..3])
32 (lam $ \i -> list [i, i])
33
34 -- * Tests
35 type Ex = Expr_Root
36 ( Expr_Lambda
37 .|. Expr_List
38 .|. Expr_Maybe
39 .|. Expr_Int
40 .|. Expr_Bool
41 .|. Expr_Functor
42 .|. Expr_Applicative
43 .|. Expr_Monad
44 .|. Expr_Either
45 )
46 ex_from = root_expr_from (Proxy::Proxy Ex)
47
48 (==>) ast expected =
49 testCase (show ast) $
50 case ex_from ast of
51 Left err -> Left err @?= Prelude.snd `Arrow.left` expected
52 Right (Exists_Type0_and_Repr ty (Forall_Repr r)) ->
53 case expected of
54 Left (_, err) -> Right ("…"::String) @?= Left err
55 Right (ty_expected::Type_Root_of_Expr Ex h, _::h, _::Text) ->
56 (Monad.>>= (@?= (\(_::Proxy h, err) -> err) `Arrow.left` expected)) $
57 case ty `type0_eq` ty_expected of
58 Nothing -> Monad.return $ Left $
59 error_expr (Proxy::Proxy Ex) $
60 Error_Expr_Type_mismatch ast
61 (Exists_Type0 ty)
62 (Exists_Type0 ty_expected)
63 Just Refl -> do
64 let h = host_from_expr r
65 Monad.return $
66 Right
67 ( ty
68 , h
69 , text_from_expr r
70 -- , (text_from_expr :: Repr_Text h -> Text) r
71 )
72
73 tests :: TestTree
74 tests = testGroup "Monad"
75 [ AST ">>="
76 [ AST "list"
77 [ AST "Int" []
78 , AST "int" [AST "1" []]
79 , AST "int" [AST "2" []]
80 , AST "int" [AST "3" []]
81 ]
82 , AST "\\"
83 [ AST "x" [], AST "Int" []
84 , AST "list"
85 [ AST "Int" []
86 , AST "var" [ AST "x" [] ]
87 , AST "var" [ AST "x" [] ]
88 ]
89 ]
90 ] ==> Right
91 ( type_list type_int
92 , [1, 1, 2, 2, 3, 3]
93 , "[1, 2, 3] >>= (\\x0 -> [x0, x0])" )
94 , AST ">>="
95 [ AST "just" [ AST "int" [AST "1" []] ]
96 , AST "\\"
97 [ AST "x" [], AST "Int" []
98 , AST "nothing"
99 [ AST "Int" []
100 ]
101 ]
102 ] ==> Right
103 ( type_maybe type_int
104 , Nothing
105 , "just 1 >>= (\\x0 -> nothing)" )
106 , AST ">>="
107 [ AST "right"
108 [ AST "Bool" []
109 , AST "int" [AST "1" []]
110 ]
111 , AST "\\"
112 [ AST "x" [], AST "Int" []
113 , AST "left"
114 [ AST "Int" []
115 , AST "bool" [AST "True" []]
116 ]
117 ]
118 ] ==> Right
119 ( type_either type_bool type_int
120 , Left True
121 , "right 1 >>= (\\x0 -> left True)" )
122 ]