Add tests for Compiling.
authorJulien Moutinho <julm+symantic@autogeree.net>
Thu, 8 Dec 2016 01:50:22 +0000 (02:50 +0100)
committerJulien Moutinho <julm+symantic@autogeree.net>
Thu, 8 Dec 2016 01:50:22 +0000 (02:50 +0100)
22 files changed:
Language/Symantic/Compiling/Applicative/Test.hs
Language/Symantic/Compiling/Bool/Test.hs
Language/Symantic/Compiling/Char.hs
Language/Symantic/Compiling/Foldable/Test.hs [new file with mode: 0644]
Language/Symantic/Compiling/Functor/Test.hs
Language/Symantic/Compiling/Int.hs
Language/Symantic/Compiling/Integer.hs
Language/Symantic/Compiling/Integral.hs
Language/Symantic/Compiling/List.hs
Language/Symantic/Compiling/Map.hs
Language/Symantic/Compiling/Map/Test.hs [new file with mode: 0644]
Language/Symantic/Compiling/Maybe.hs
Language/Symantic/Compiling/Num.hs
Language/Symantic/Compiling/Term.hs
Language/Symantic/Compiling/Term/Test.hs [new file with mode: 0644]
Language/Symantic/Compiling/Test.hs
Language/Symantic/Compiling/Text.hs
Language/Symantic/Transforming/Trans.hs
Language/Symantic/Typing/Kind.hs
Language/Symantic/Typing/Syntax.hs
Language/Symantic/Typing/Test.hs
symantic.cabal

index aa7de80d753751f846b64ea9abad7db877904f01..fb62d241b0fd9e870470fe6f524d09d7bffca90c 100644 (file)
@@ -1,27 +1,14 @@
 {-# LANGUAGE DataKinds #-}
-{-# LANGUAGE FlexibleContexts #-}
-{-# LANGUAGE GADTs #-}
-{-# LANGUAGE NoMonomorphismRestriction #-}
-{-# LANGUAGE Rank2Types #-}
-{-# LANGUAGE ScopedTypeVariables #-}
-{-# LANGUAGE TypeOperators #-}
 {-# OPTIONS_GHC -fno-warn-missing-signatures #-}
 module Compiling.Applicative.Test where
 
 import Test.Tasty
-import Test.Tasty.HUnit
 
-import qualified Control.Arrow as Arrow
-import qualified Control.Monad as Monad
--- import Control.Monad.IO.Class (MonadIO(..))
 import Data.Proxy (Proxy(..))
-import Data.Text (Text)
-import Data.Type.Equality ((:~:)(Refl))
-import Prelude hiding ((&&), not, (||))
 
 import Language.Symantic.Typing
 import Language.Symantic.Compiling
-import Language.Symantic.Interpreting
+import Compiling.Term.Test
 
 -- * Tests
 type Ifaces =
@@ -30,31 +17,7 @@ type Ifaces =
  , Proxy Maybe
  , Proxy Applicative
  ]
-type Consts = Consts_of_Ifaces Ifaces
-
-(==>) (ast::Syntax Text) expected =
-       testCase (show ast) $
-       case root_term_from ast of
-        Left err -> Left err @?= Prelude.snd `Arrow.left` expected
-        Right (ETerm ty (Term te)::ETerm Ifaces) ->
-               case expected of
-                Left (_, err) -> Right ("…"::Text) @?= Left err
-                Right (ty_expected::Type Consts h, _::h, _::Text) ->
-                       (Monad.>>= (@?= (\(_::Type Consts h, err) -> err) `Arrow.left` expected)) $
-                       case ty `eq_type` ty_expected of
-                        Nothing -> Monad.return $ Left $
-                               Error_Term_Type_mismatch
-                                (At Nothing $ EType ty)
-                                (At Nothing $ EType ty_expected)
-                        Just Refl -> do
-                               let h = host_from_term te
-                               Monad.return $
-                                       Right
-                                        ( ty
-                                        , h
-                                        , text_from_term te
-                                        -- , (text_from_expr :: Repr_Text h -> Text) r
-                                        )
+(==>) = test_term_from (Proxy::Proxy Ifaces)
 
 tests :: TestTree
 tests = testGroup "Applicative"
index 306cdafe45f73c7a0c2413bf79c26322b9260f61..da5e853d4dba47df44100fcfcf8f16593fb4dddc 100644 (file)
@@ -1,27 +1,17 @@
 {-# LANGUAGE DataKinds #-}
-{-# LANGUAGE FlexibleContexts #-}
-{-# LANGUAGE GADTs #-}
 {-# LANGUAGE NoMonomorphismRestriction #-}
-{-# LANGUAGE Rank2Types #-}
-{-# LANGUAGE ScopedTypeVariables #-}
-{-# LANGUAGE TypeOperators #-}
 {-# OPTIONS_GHC -fno-warn-missing-signatures #-}
 module Compiling.Bool.Test where
 
 import Test.Tasty
-import Test.Tasty.HUnit
 
-import qualified Control.Arrow as Arrow
-import qualified Control.Monad as Monad
--- import Control.Monad.IO.Class (MonadIO(..))
 import Data.Proxy (Proxy(..))
-import Data.Text (Text)
-import Data.Type.Equality ((:~:)(Refl))
 import Prelude hiding ((&&), not, (||))
 
 import Language.Symantic.Typing
 import Language.Symantic.Compiling
 import Language.Symantic.Interpreting
+import Compiling.Term.Test
 
 -- * Class 'Sym_Bool_Vars'
 
@@ -59,31 +49,7 @@ type Ifaces =
  [ Proxy (->)
  , Proxy Bool
  ]
-type Consts = Consts_of_Ifaces Ifaces
-
-(==>) (ast::Syntax Text) expected =
-       testCase (show ast) $
-       case root_term_from ast of
-        Left err -> Left err @?= Prelude.snd `Arrow.left` expected
-        Right (ETerm ty (Term te)::ETerm Ifaces) ->
-               case expected of
-                Left (_, err) -> Right ("…"::Text) @?= Left err
-                Right (ty_expected::Type Consts h, _::h, _::Text) ->
-                       (Monad.>>= (@?= (\(_::Type Consts h, err) -> err) `Arrow.left` expected)) $
-                       case ty `eq_type` ty_expected of
-                        Nothing -> Monad.return $ Left $
-                               Error_Term_Type_mismatch
-                                (At Nothing $ EType ty)
-                                (At Nothing $ EType ty_expected)
-                        Just Refl -> do
-                               let h = host_from_term te
-                               Monad.return $
-                                       Right
-                                        ( ty
-                                        , h
-                                        , text_from_term te
-                                        -- , (text_from_expr :: Repr_Text h -> Text) r
-                                        )
+(==>) = test_term_from (Proxy::Proxy Ifaces)
 
 tests :: TestTree
 tests = testGroup "Bool" $
index a0202b5d1622b211c62a259a76d3ff506bd8a5dc..c0c2446e342876c1ffc8614f96143657fcec3c3a 100644 (file)
@@ -94,8 +94,9 @@ instance -- Term_fromI
                where
                char_from =
                        let ty = tyChar in
-                       from_lex (Text.pack $ show_type ty) ast $ \(i::Char) as ->
-                       k as ty $ TermLC $ Fun.const $ char i
+                       from_ast1 ast $ \ast_lit as ->
+                       from_lex (Text.pack $ show_type ty) ast_lit $ \(lit::Char) ->
+                       k as ty $ TermLC $ Fun.const $ char lit
                char_toUpper_from =
                        from_ast0 ast $ \_ as ->
                        k as (tyChar ~> tyChar) $ TermLC $
diff --git a/Language/Symantic/Compiling/Foldable/Test.hs b/Language/Symantic/Compiling/Foldable/Test.hs
new file mode 100644 (file)
index 0000000..abfbe2b
--- /dev/null
@@ -0,0 +1,42 @@
+{-# LANGUAGE DataKinds #-}
+{-# OPTIONS_GHC -fno-warn-missing-signatures #-}
+{-# OPTIONS_GHC -O0 #-} -- speedup compile-time…
+module Compiling.Foldable.Test where
+
+import Test.Tasty
+
+import Data.Proxy (Proxy(..))
+
+import Language.Symantic.Typing
+import Language.Symantic.Compiling
+import Compiling.Term.Test
+
+-- * Tests
+type Ifaces =
+ [ Proxy (->)
+ , Proxy []
+ , Proxy Int
+ , Proxy Foldable
+ ]
+(==>) = test_term_from (Proxy::Proxy Ifaces)
+
+tests :: TestTree
+tests = testGroup "Foldable"
+ [ Syntax "foldMap"
+        [ syLam (Syntax "x" []) syInt $
+                       Syntax "list"
+                        [ syInt
+                        , Syntax "x" []
+                        , Syntax "x" []
+                        ]
+        , Syntax "list"
+                [ syInt
+                , Syntax "int" [Syntax "1" []]
+                , Syntax "int" [Syntax "2" []]
+                , Syntax "int" [Syntax "3" []]
+                ]
+        ] ==> Right
+        ( tyList :$ tyInt
+        , [1, 1, 2, 2, 3, 3]
+        , "foldMap (\\x0 -> [x0, x0]) [1, 2, 3]" )
+ ]
index 416707bb8526c7a424b063c48228e6430b43f699..4a82effc1fd6081b2955700821e98830bfc6c8e9 100644 (file)
@@ -1,27 +1,15 @@
 {-# LANGUAGE DataKinds #-}
-{-# LANGUAGE FlexibleContexts #-}
-{-# LANGUAGE GADTs #-}
-{-# LANGUAGE NoMonomorphismRestriction #-}
-{-# LANGUAGE Rank2Types #-}
-{-# LANGUAGE ScopedTypeVariables #-}
-{-# LANGUAGE TypeOperators #-}
 {-# OPTIONS_GHC -fno-warn-missing-signatures #-}
 module Compiling.Functor.Test where
 
 import Test.Tasty
-import Test.Tasty.HUnit
 
-import qualified Control.Arrow as Arrow
-import qualified Control.Monad as Monad
--- import Control.Monad.IO.Class (MonadIO(..))
 import Data.Proxy (Proxy(..))
-import Data.Text (Text)
-import Data.Type.Equality ((:~:)(Refl))
 import Prelude hiding ((&&), not, (||))
 
 import Language.Symantic.Typing
 import Language.Symantic.Compiling
-import Language.Symantic.Interpreting
+import Compiling.Term.Test
 
 -- * Tests
 type Ifaces =
@@ -29,34 +17,8 @@ type Ifaces =
  , Proxy Bool
  , Proxy Maybe
  , Proxy Functor
- -- , Proxy Foldable
- -- , Proxy Traversable
  ]
-type Consts = Consts_of_Ifaces Ifaces
-
-(==>) (ast::Syntax Text) expected =
-       testCase (show ast) $
-       case root_term_from ast of
-        Left err -> Left err @?= Prelude.snd `Arrow.left` expected
-        Right (ETerm ty (Term te)::ETerm Ifaces) ->
-               case expected of
-                Left (_, err) -> Right ("…"::Text) @?= Left err
-                Right (ty_expected::Type Consts h, _::h, _::Text) ->
-                       (Monad.>>= (@?= (\(_::Type Consts h, err) -> err) `Arrow.left` expected)) $
-                       case ty `eq_type` ty_expected of
-                        Nothing -> Monad.return $ Left $
-                               Error_Term_Type_mismatch
-                                (At Nothing $ EType ty)
-                                (At Nothing $ EType ty_expected)
-                        Just Refl -> do
-                               let h = host_from_term te
-                               Monad.return $
-                                       Right
-                                        ( ty
-                                        , h
-                                        , text_from_term te
-                                        -- , (text_from_expr :: Repr_Text h -> Text) r
-                                        )
+(==>) = test_term_from (Proxy::Proxy Ifaces)
 
 tests :: TestTree
 tests = testGroup "Functor"
index c2607a3d3674418e4e8541629b61eafbe797de47..f769c72f6c5f8384723a19d4ccb672e5a2e2257a 100644 (file)
@@ -89,8 +89,9 @@ instance -- Term_fromI
                where
                int_from =
                        let ty = tyInt in
-                       from_lex (Text.pack $ show_type ty) ast $ \(i::Int) as ->
-                       k as ty $ TermLC $ Fun.const $ int i
+                       from_ast1 ast $ \ast_lit as ->
+                       from_lex (Text.pack $ show_type ty) ast_lit $ \(lit::Int) ->
+                       k as ty $ TermLC $ Fun.const $ int lit
 
 -- | The 'Int' 'Type'
 tyInt :: Inj_Const cs Int => Type cs Int
index 2af85c6894cf6b63bd5c172dc3bd61288cf585a7..d5a4189e04f03f93ef4bb84dc0a22e2ee6975128 100644 (file)
@@ -87,8 +87,9 @@ instance -- Term_fromI
                where
                integer_from =
                        let ty = tyInteger in
-                       from_lex (Text.pack $ show_type ty) ast $ \(i::Integer) as ->
-                       k as ty $ TermLC $ Fun.const $ integer i
+                       from_ast1 ast $ \ast_lit as ->
+                       from_lex (Text.pack $ show_type ty) ast_lit $ \(lit::Integer) ->
+                       k as ty $ TermLC $ Fun.const $ integer lit
 
 -- | The 'Integer' 'Type'
 tyInteger :: Inj_Const cs Integer => Type cs Integer
index d4b7cc1cd955eda4dc4c69163ea9acec957de800..1875cead10e90da2e08b053e665952dfa1c9ba3d 100644 (file)
@@ -126,10 +126,10 @@ instance -- Term_fromI
                 -- div  :: Integral i => i -> i -> i
                 -- mod  :: Integral i => i -> i -> i
                        from_ast1 ast $ \ast_a as ->
-                               term_from ast_a ctx $ \ty_a (TermLC x) ->
-                               check_constraint (At (Just ast_a) (tyIntegral :$ ty_a)) $ \Con ->
-                               k as (ty_a ~> ty_a) $ TermLC $
-                                \c -> lam $ \y -> op (x c) y
+                       term_from ast_a ctx $ \ty_a (TermLC x) ->
+                       check_constraint (At (Just ast_a) (tyIntegral :$ ty_a)) $ \Con ->
+                       k as (ty_a ~> ty_a) $ TermLC $
+                        \c -> lam $ \y -> op (x c) y
                integral_op2t2_from
                 (op::forall term a. (Sym_Integral term, Integral a)
                         => term a -> term a -> term (a, a)) =
index 3d46a4b190936bc18b7e31b1cdf9a4d02d5aa1fe..9808feb6497aff557178d2e7a753083f86652502 100644 (file)
@@ -11,7 +11,7 @@
 {-# LANGUAGE TypeOperators #-}
 {-# LANGUAGE UndecidableInstances #-}
 {-# OPTIONS_GHC -fno-warn-orphans #-}
-{-# OPTIONS_GHC -fconstraint-solver-iterations=7 #-}
+{-# OPTIONS_GHC -fconstraint-solver-iterations=8 #-}
 -- | Symantic for '[]'.
 module Language.Symantic.Compiling.List where
 
@@ -62,6 +62,7 @@ type instance Sym_of_Iface (Proxy []) = Sym_List
 type instance Consts_of_Iface (Proxy []) = Proxy [] ': Consts_imported_by []
 type instance Consts_imported_by [] =
  [ Proxy Applicative
+ , Proxy Bool
  , Proxy Eq
  , Proxy Foldable
  , Proxy Functor
@@ -130,8 +131,7 @@ instance -- Proj_ConC
         = Just $ case () of
                 _ | Just Refl <- proj_const q (Proxy::Proxy Eq)
                   , Just Con  <- proj_con (t :$ a) -> Just Con
-                  | Just Refl <- proj_const q (Proxy::Proxy Monoid)
-                  , Just Con  <- proj_con (t :$ a) -> Just Con
+                  | Just Refl <- proj_const q (Proxy::Proxy Monoid) -> Just Con
                   | Just Refl <- proj_const q (Proxy::Proxy Ord)
                   , Just Con  <- proj_con (t :$ a) -> Just Con
                 _ -> Nothing
index ca61f696096db84716028517ddb7c478f76e43d1..f9435602a1e75bb22369cc205ad9908a144ba56d 100644 (file)
@@ -36,7 +36,7 @@ import Language.Symantic.Transforming.Trans
 -- * Class 'Sym_Map'
 class Sym_Map term where
        map_fromList :: Ord k => term [(k, a)] -> term (Map k a)
-       mapWithKey :: term (k -> a -> b) -> term (Map k a) -> term (Map k b)
+       map_mapWithKey :: term (k -> a -> b) -> term (Map k a) -> term (Map k b)
        map_lookup :: Ord k => term k -> term (Map k a) -> term (Maybe a)
        map_keys :: term (Map k a) -> term [k]
        map_member :: Ord k => term k -> term (Map k a) -> term Bool
@@ -46,7 +46,7 @@ class Sym_Map term where
        map_foldrWithKey :: term (k -> a -> b -> b) -> term b -> term (Map k a) -> term b
        
        default map_fromList :: (Trans t term, Ord k) => t term [(k, a)] -> t term (Map k a)
-       default mapWithKey :: Trans t term => t term (k -> a -> b) -> t term (Map k a) -> t term (Map k b)
+       default map_mapWithKey :: Trans t term => t term (k -> a -> b) -> t term (Map k a) -> t term (Map k b)
        default map_lookup :: (Trans t term, Ord k) => t term k -> t term (Map k a) -> t term (Maybe a)
        default map_keys :: Trans t term => t term (Map k a) -> t term [k]
        default map_member :: (Trans t term, Ord k) => t term k -> t term (Map k a) -> t term Bool
@@ -56,7 +56,7 @@ class Sym_Map term where
        default map_foldrWithKey :: Trans t term => t term (k -> a -> b -> b) -> t term b -> t term (Map k a) -> t term b
        
        map_fromList     = trans_map1 map_fromList
-       mapWithKey       = trans_map2 mapWithKey
+       map_mapWithKey   = trans_map2 map_mapWithKey
        map_lookup       = trans_map2 map_lookup
        map_keys         = trans_map1 map_keys
        map_member       = trans_map2 map_member
@@ -84,7 +84,7 @@ type instance Consts_imported_by Map =
 
 instance Sym_Map HostI where
        map_fromList     = liftM  Map.fromList
-       mapWithKey       = liftM2 Map.mapWithKey
+       map_mapWithKey   = liftM2 Map.mapWithKey
        map_lookup       = liftM2 Map.lookup
        map_keys         = liftM  Map.keys
        map_member       = liftM2 Map.member
@@ -93,18 +93,18 @@ instance Sym_Map HostI where
        map_difference   = liftM2 Map.difference
        map_foldrWithKey = liftM3 Map.foldrWithKey
 instance Sym_Map TextI where
-       map_fromList     = textI_app1 "map_fromList"
-       mapWithKey       = textI_app2 "mapWithKey"
-       map_lookup       = textI_app2 "map_lookup"
-       map_keys         = textI_app1 "map_keys"
-       map_member       = textI_app2 "map_member"
-       map_insert       = textI_app3 "map_insert"
-       map_delete       = textI_app2 "map_delete"
-       map_difference   = textI_app2 "map_difference"
-       map_foldrWithKey = textI_app3 "map_foldrWithKey"
+       map_fromList     = textI_app1 "Map.fromList"
+       map_mapWithKey   = textI_app2 "Map.mapWithKey"
+       map_lookup       = textI_app2 "Map.lookup"
+       map_keys         = textI_app1 "Map.keys"
+       map_member       = textI_app2 "Map.member"
+       map_insert       = textI_app3 "Map.insert"
+       map_delete       = textI_app2 "Map.delete"
+       map_difference   = textI_app2 "Map.difference"
+       map_foldrWithKey = textI_app3 "Map.foldrWithKey"
 instance (Sym_Map r1, Sym_Map r2) => Sym_Map (DupI r1 r2) where
        map_fromList     = dupI1 sym_Map map_fromList
-       mapWithKey       = dupI2 sym_Map mapWithKey
+       map_mapWithKey   = dupI2 sym_Map map_mapWithKey
        map_lookup       = dupI2 sym_Map map_lookup
        map_keys         = dupI1 sym_Map map_keys
        map_member       = dupI2 sym_Map map_member
@@ -164,14 +164,14 @@ instance -- Term_fromI
        term_fromI ast ctx k =
                case ast_lexem ast of
                 "Map.fromList"     -> map_fromList_from
-                "mapWithKey"       -> mapWithKey_from
-                "map_lookup"       -> map_lookup_from
-                "map_keys"         -> map_keys_from
-                "map_member"       -> map_member_from
-                "map_insert"       -> map_insert_from
-                "map_delete"       -> map_delete_from
-                "map_difference"   -> map_difference_from
-                "map_foldrWithKey" -> map_foldrWithKey_from
+                "Map.mapWithKey"   -> mapWithKey_from
+                "Map.lookup"       -> map_lookup_from
+                "Map.keys"         -> map_keys_from
+                "Map.member"       -> map_member_from
+                "Map.insert"       -> map_insert_from
+                "Map.delete"       -> map_delete_from
+                "Map.difference"   -> map_difference_from
+                "Map.foldrWithKey" -> map_foldrWithKey_from
                 _ -> Left $ Error_Term_unsupported
                where
                map_fromList_from =
@@ -184,13 +184,13 @@ instance -- Term_fromI
                        k as ((tyMap :$ ty_k) :$ ty_a) $ TermLC $
                         \c -> map_fromList (l c)
                mapWithKey_from =
-                -- mapWithKey :: (k -> a -> b) -> Map k a -> Map k b
+                -- map_mapWithKey :: (k -> a -> b) -> Map k a -> Map k b
                        from_ast1 ast $ \ast_k2a2b as ->
                        term_from ast_k2a2b ctx $ \ty_k2a2b (TermLC k2a2b) ->
                        check_type2 tyFun ast_k2a2b ty_k2a2b $ \Refl ty_k ty_a2b ->
                        check_type2 tyFun ast_k2a2b ty_a2b $ \Refl ty_a ty_b ->
                        k as ((tyMap :$ ty_k) :$ ty_a ~> (tyMap :$ ty_k) :$ ty_b) $ TermLC $
-                        \c -> lam $ mapWithKey (k2a2b c)
+                        \c -> lam $ map_mapWithKey (k2a2b c)
                map_lookup_from =
                 -- lookup :: Ord k => k -> Map k a -> Maybe a
                        from_ast2 ast $ \ast_k ast_m as ->
diff --git a/Language/Symantic/Compiling/Map/Test.hs b/Language/Symantic/Compiling/Map/Test.hs
new file mode 100644 (file)
index 0000000..e0564b5
--- /dev/null
@@ -0,0 +1,117 @@
+{-# LANGUAGE DataKinds #-}
+{-# LANGUAGE NoMonomorphismRestriction #-}
+{-# OPTIONS_GHC -fno-warn-missing-signatures #-}
+{-# OPTIONS_GHC -O0 #-} -- speedup compile-time…
+module Compiling.Map.Test where
+
+import Test.Tasty
+
+import Data.Map.Strict (Map)
+import qualified Data.Map.Strict as Map
+import Data.Proxy (Proxy(..))
+import Data.Text (Text)
+import qualified Data.Text as Text
+import Prelude hiding (zipWith)
+
+import Language.Symantic.Typing
+import Language.Symantic.Compiling.Bool
+import Language.Symantic.Compiling.Map
+import Language.Symantic.Compiling.List
+import Language.Symantic.Compiling.Term
+import Language.Symantic.Compiling.Tuple2
+import Language.Symantic.Compiling.Int
+import Language.Symantic.Compiling.Text
+import Compiling.Term.Test
+
+-- * Terms
+t = bool True
+f = bool False
+e1 = map_fromList $ zipWith (lam (lam . tuple2))
+ (list $ int Prelude.<$> [1..5])
+ (list $ (text . Text.singleton) Prelude.<$> ['a'..'e'])
+
+-- * Tests
+type Ifaces =
+ [ Proxy (->)
+ , Proxy []
+ , Proxy Int
+ , Proxy Map
+ , Proxy Text
+ , Proxy (,)
+ , Proxy Num
+ , Proxy Monoid
+ ]
+(==>) = test_term_from (Proxy::Proxy Ifaces)
+
+tests :: TestTree
+tests = testGroup "Map"
+ [ Syntax "Map.fromList"
+        [ Syntax "zipWith"
+                [ syLam (Syntax "x" []) syInt $
+                               syLam (Syntax "y" []) syText $
+                                       Syntax "(,)"
+                                        [ Syntax "x" []
+                                        , Syntax "y" []
+                                        ]
+                , Syntax "list"
+                        [ syInt
+                        , Syntax "int" [Syntax "1" []]
+                        , Syntax "int" [Syntax "2" []]
+                        , Syntax "int" [Syntax "3" []]
+                        ]
+                , Syntax "list"
+                        [ syText
+                        , Syntax "text" [Syntax "\"a\"" []]
+                        , Syntax "text" [Syntax "\"b\"" []]
+                        , Syntax "text" [Syntax "\"c\"" []]
+                        ]
+                ]
+        ] ==> Right
+        ( (tyMap :$ tyInt) :$ tyText
+        , Map.fromList [(1, "a"), (2, "b"), (3, "c")]
+        , "Map.fromList (((\\x0 -> (\\x1 -> zipWith (\\x2 -> (\\x3 -> (x2, x3))) x0 x1)) [1, 2, 3]) [\"a\", \"b\", \"c\"])" )
+ , Syntax "Map.foldrWithKey"
+        [ syLam (Syntax "k" []) syInt $
+                       syLam (Syntax "v" []) syText $
+                               syLam (Syntax "a" []) (syTuple2 [syInt, syText]) $
+                                       syTuple2
+                                        [ Syntax "+"
+                                                [ Syntax "k" []
+                                                , Syntax "fst" [ Syntax "a" [] ]
+                                                ]
+                                        , Syntax "mappend"
+                                                [ Syntax "v" []
+                                                , Syntax "snd" [ Syntax "a" [] ]
+                                                ]
+                                        ]
+        , syTuple2
+                [ Syntax "int" [Syntax "0" []]
+                , Syntax "text" [Syntax "\"\"" []]
+                ]
+        , Syntax "Map.fromList"
+                [ Syntax "zipWith"
+                        [ syLam (Syntax "x" []) syInt $
+                                       syLam (Syntax "y" []) syText $
+                                               syTuple2
+                                                [ Syntax "x" []
+                                                , Syntax "y" []
+                                                ]
+                        , Syntax "list"
+                                [ syInt
+                                , Syntax "int" [Syntax "1" []]
+                                , Syntax "int" [Syntax "2" []]
+                                , Syntax "int" [Syntax "3" []]
+                                ]
+                        , Syntax "list"
+                                [ syText
+                                , Syntax "text" [Syntax "\"a\"" []]
+                                , Syntax "text" [Syntax "\"b\"" []]
+                                , Syntax "text" [Syntax "\"c\"" []]
+                                ]
+                        ]
+                ]
+        ] ==> Right
+        ( (tyTuple2 :$ tyInt) :$ tyText
+        , (6, "abc")
+        , "((\\x0 -> (\\x1 -> Map.foldrWithKey (\\x2 -> (\\x3 -> (\\x4 -> ((\\x5 -> x2 + x5) (fst x4), (\\x5 -> mappend x3 x5) (snd x4))))) x0 x1)) (0, \"\")) (Map.fromList (((\\x0 -> (\\x1 -> zipWith (\\x2 -> (\\x3 -> (x2, x3))) x0 x1)) [1, 2, 3]) [\"a\", \"b\", \"c\"]))" )
+ ]
index b48f7f55cb6b0d36a62a01b1a29e28b1a6c00f5f..8882f8baf681bf024dbb654a75d1df8a01dbdee6 100644 (file)
@@ -109,11 +109,25 @@ instance -- Term_fromI
  ) => Term_fromI is (Proxy Maybe) ast where
        term_fromI ast ctx k =
                case ast_lexem ast of
-                "maybe"   -> maybe_from
-                "Just"    -> _Just_from
                 "Nothing" -> _Nothing_from
+                "Just"    -> _Just_from
+                "maybe"   -> maybe_from
                 _ -> Left $ Error_Term_unsupported
                where
+               _Nothing_from =
+                -- Nothing :: Maybe a
+                       from_ast1 ast $ \ast_ty_a as ->
+                       either (Left . Error_Term_Typing . At (Just ast)) Fun.id $
+                       type_from ast_ty_a $ \ty_a -> Right $
+                       check_kind ast SKiType (At (Just ast_ty_a) ty_a) $ \Refl ->
+                       k as (tyMaybe :$ ty_a) $ TermLC $
+                       Fun.const _Nothing
+               _Just_from =
+                -- Just :: a -> Maybe a
+                       from_ast1 ast $ \ast_a as ->
+                       term_from ast_a ctx $ \ty_a (TermLC a) ->
+                       k as (tyMaybe :$ ty_a) $ TermLC $
+                        \c -> _Just (a c)
                maybe_from =
                 -- maybe :: b -> (a -> b) -> Maybe a -> b
                        from_ast2 ast $ \ast_b ast_a2b as ->
@@ -123,20 +137,6 @@ instance -- Term_fromI
                        check_type (At (Just ast_a2b) ty_a2b_b) (At (Just ast_b) ty_b) $ \Refl ->
                        k as (tyMaybe :$ ty_a2b_a ~> ty_b) $ TermLC $
                         \c -> lam $ maybe (b c) (a2b c)
-               _Just_from =
-                -- Just :: a -> Maybe a
-                       from_ast1 ast $ \ast_a as ->
-                       term_from ast_a ctx $ \ty_a (TermLC a) ->
-                       k as (tyMaybe :$ ty_a) $ TermLC $
-                        \c -> _Just (a c)
-               _Nothing_from =
-                -- Nothing :: Maybe a
-                       from_ast1 ast $ \ast_ty_a as ->
-                       either (Left . Error_Term_Typing . At (Just ast)) Fun.id $
-                       type_from ast_ty_a $ \ty_a -> Right $
-                       check_kind ast SKiType (At (Just ast_ty_a) ty_a) $ \Refl ->
-                       k as (tyMaybe :$ ty_a) $ TermLC $
-                       Fun.const _Nothing
 
 -- | The 'Maybe' 'Type'
 tyMaybe :: Inj_Const cs Maybe => Type cs Maybe
index 23f1e9d67c8ea90cce57cb23ae26ad240e859de2..6f8c74fd5f6ccc62da10df9b6de7b7acb723036c 100644 (file)
@@ -61,7 +61,9 @@ infixl 7 *
 
 type instance Sym_of_Iface (Proxy Num) = Sym_Num
 type instance Consts_of_Iface (Proxy Num) = Proxy Num ': Consts_imported_by Num
-type instance Consts_imported_by Num = '[]
+type instance Consts_imported_by Num =
+ '[ Proxy Integer
+ ]
 
 instance Sym_Num HostI where
        abs         = liftM Prelude.abs
index 1178b55cfa0743f76f05b28d271f7c66e1a28eed..777eeeac37b195f8aae10587ced09336c4b65d80 100644 (file)
@@ -139,7 +139,9 @@ type Term_fromT ast ctx ret is ls rs
  -> Either (Error_Term is ast) ret
 
 -- ** Type 'Term_fromIT'
--- | Convenient type synonym defining a 'term_fromI'.
+-- | Almost like 'Term_fromT', but with the remaining 'ast_nodes'
+-- given to the continuation so that 'term_apply' can be used within
+-- the instance 'Term_fromR'@ is ls (i ': rs) ast@.
 type Term_fromIT ast ctx ret is ls rs
  = ast
  -> LamCtx_Type is (Lexem ast) ctx
@@ -176,7 +178,7 @@ instance
        term_fromR ast ctx k =
                case term_fromI ast ctx $ \as ty (TermLC te
                 ::TermLC ctx h is ls (i ': rs)) ->
-                       apply ast as ctx ty (TermLC te) k of
+                       term_apply ast as ctx ty (TermLC te) k of
                 Left Error_Term_unsupported ->
                        term_fromR ast ctx $ \ty (TermLC te
                         ::TermLC ctx h is (i ': ls) rs) ->
@@ -186,7 +188,9 @@ instance
 -- | End the recursion.
 instance AST ast => Term_fromR is ls '[] ast
 
-apply ::
+-- | Apply the given @ast@s to the given 'TermLC',
+-- or return an 'Error_Term'.
+term_apply ::
  ( Term_from is ast
  , Inj_Const (Consts_of_Ifaces is) (->)
  ) => ast -> [ast]
@@ -198,14 +202,14 @@ apply ::
     -> TermLC ctx h' is ls rs
     -> Either (Error_Term is ast) ret )
  -> Either (Error_Term is ast) ret
-apply ast_f args ctx ty_f te_f@(TermLC f) k =
+term_apply ast_f args ctx ty_f te_f@(TermLC f) k =
        case args of
         [] -> k ty_f te_f
         ast_a:as ->
                term_from ast_a ctx $ \ty_a (TermLC a) ->
                check_type2 tyFun ast_f ty_f $ \Refl ty_arg ty_res ->
                check_type (At (Just ast_f) ty_arg) (At (Just ast_a) ty_a) $ \Refl ->
-               apply ast_f as ctx ty_res (TermLC $ \c -> f c .$ a c) k
+               term_apply ast_f as ctx ty_res (TermLC $ \c -> f c .$ a c) k
 
 -- ** Class 'Term_fromI'
 -- | Handle the work of 'Term_from' for a given /interface/ @i@,
diff --git a/Language/Symantic/Compiling/Term/Test.hs b/Language/Symantic/Compiling/Term/Test.hs
new file mode 100644 (file)
index 0000000..e54ff55
--- /dev/null
@@ -0,0 +1,63 @@
+{-# LANGUAGE DataKinds #-}
+{-# LANGUAGE FlexibleContexts #-}
+{-# LANGUAGE GADTs #-}
+{-# LANGUAGE NoMonomorphismRestriction #-}
+{-# LANGUAGE Rank2Types #-}
+{-# LANGUAGE ScopedTypeVariables #-}
+{-# LANGUAGE TypeOperators #-}
+{-# OPTIONS_GHC -fno-warn-missing-signatures #-}
+module Compiling.Term.Test where
+
+import Test.Tasty
+import Test.Tasty.HUnit
+
+import qualified Control.Arrow as Arrow
+import qualified Control.Monad as Monad
+-- import Control.Monad.IO.Class (MonadIO(..))
+import Data.Proxy (Proxy(..))
+import Data.Text (Text)
+import Data.Type.Equality ((:~:)(Refl))
+import Prelude as P
+
+import Language.Symantic.Typing
+import Language.Symantic.Compiling
+import Language.Symantic.Interpreting
+
+test_term_from
+ :: forall is h.
+ ( Sym_of_Ifaces is HostI
+ , Sym_of_Ifaces is TextI
+ , Term_from is (Syntax Text)
+ , Eq h
+ , Show h
+ , Show_Const (Consts_of_Ifaces is)
+ ) => Proxy is -> Syntax Text
+ -> Either (Type (Consts_of_Ifaces is) h, Error_Term is (Syntax Text))
+           (Type (Consts_of_Ifaces is) h, h, Text)
+ -> TestTree
+test_term_from _is ast expected =
+       testCase (elide $ show ast) $
+       case root_term_from ast of
+        Left err -> Left err @?= P.snd `Arrow.left` expected
+        Right (ETerm ty (Term te)::ETerm is) ->
+               case expected of
+                Left (_, err) -> Right ("…"::Text) @?= Left err
+                Right (ty_expected::Type (Consts_of_Ifaces is) h, _::h, _::Text) ->
+                       (Monad.>>= (@?= (\(_::Type (Consts_of_Ifaces is) h, err) -> err) `Arrow.left` expected)) $
+                       case ty `eq_type` ty_expected of
+                        Nothing -> Monad.return $ Left $
+                               Error_Term_Type_mismatch
+                                (At Nothing $ EType ty)
+                                (At Nothing $ EType ty_expected)
+                        Just Refl -> do
+                               let h = host_from_term te
+                               Monad.return $
+                                       Right
+                                        ( ty
+                                        , h
+                                        , text_from_term te
+                                        -- , (text_from_term :: Repr_Text h -> Text) r
+                                        )
+       where
+               elide s | P.length s P.> 42 = P.take 42 s P.++ ['…']
+               elide s = s
index 23555c3454617ee8bb76fe6cadf2025e8dddd616..e9aa2b52ba610e585ece92a8486599a7b884498e 100644 (file)
@@ -7,6 +7,8 @@ import Test.Tasty
 import qualified Compiling.Bool.Test as Bool
 import qualified Compiling.Functor.Test as Functor
 import qualified Compiling.Applicative.Test as Applicative
+import qualified Compiling.Foldable.Test as Foldable
+import qualified Compiling.Map.Test as Map
 
 import Prelude hiding ((&&), not, (||), (==), id)
 
@@ -28,4 +30,6 @@ tests = testGroup "Compiling" $
  [ Bool.tests
  , Functor.tests
  , Applicative.tests
+ , Foldable.tests
+ , Map.tests
  ]
index 7cb3b461a82b0e53a8156048d39546ef6ad7c806..656c9612825730a251a239536b9a9cd7e54e6580 100644 (file)
@@ -80,8 +80,9 @@ instance -- Term_fromI
                where
                text_from =
                        let ty = tyText in
-                       from_lex (Text.pack $ show_type ty) ast $ \(i::Text) as ->
-                       k as ty $ TermLC $ Fun.const $ text i
+                       from_ast1 ast $ \ast_lit as ->
+                       from_lex (Text.pack $ show_type ty) ast_lit $ \(lit::Text) ->
+                       k as ty $ TermLC $ Fun.const $ text lit
 
 -- | The 'Text' 'Type'
 tyText :: Inj_Const cs Text => Type cs Text
index d0e1fcdc4eb3752dbbb968cdf1c69841dbfc68e6..09034afcb8e011bc1760534c6cf1b065d20b4d3a 100644 (file)
@@ -31,7 +31,7 @@ class Trans t term where
         -> (t term a -> t term b -> t term c)
        trans_map2 f e1 e2 = trans_lift (trans_apply e1 `f` trans_apply e2)
        
-       -- | Convenient method to define the identity transformation for a terary symantic method.
+       -- | Convenient method to define the identity transformation for a ternary symantic method.
        trans_map3
         :: (term a -> term b -> term c -> term d)
         -> (t term a -> t term b -> t term c -> t term d)
index 4575eba61e181b2773c9b4ace87ca05def42bc28..ca70215f6d11819b898094a799f116339b6dc8d9 100644 (file)
@@ -98,8 +98,6 @@ type instance Kind_Of (x::Constraint) = Constraint
 type instance Kind_Of (x::Kind.Type)  = Kind.Type
 type instance Kind_Of (x::a -> b)     = Kind_Of (Any::a) -> Kind_Of (Any::b)
 
-data (a::k1) :~~: (b::k2) where
-       HRefl :: forall k (a::k) (b::k). a :~~: b
 {-
 instance Show (x :~~: y) where
        show _ = "HRefl"
index 15684ffda5e472ab369efdefedfe31c739c3b53b..92ad671cdcc3a2dba0b66d835ecbccb888be6724 100644 (file)
@@ -37,12 +37,12 @@ from_lex
  :: ( AST ast, Lexem ast ~ Text
     , Read ty, Lift_Error_Syntax err )
  => Text -> ast
- -> (ty -> [ast] -> Either (err ast) ret)
+ -> (ty -> Either (err ast) ret)
  -> Either (err ast) ret
 from_lex msg ast_x k =
        from_ast0 ast_x $ \lex_x as ->
        case reads $ Text.unpack lex_x of
-        [(x, "")] -> k x as
+        [(x, "")] | null as -> k x
         _ -> Left $ lift_error_syntax $
                Error_Syntax_read $
                At (Just ast_x) msg
index 2b3f985a77c0ccab6f0f9f7ff6327844c7c21d3f..6d38eb63eece064d0b1583ee6ff45df78f5d8a45 100644 (file)
@@ -1,10 +1,4 @@
-{-# LANGUAGE DataKinds #-}
-{-# LANGUAGE FlexibleContexts #-}
-{-# LANGUAGE KindSignatures #-}
-{-# LANGUAGE Rank2Types #-}
 {-# LANGUAGE ScopedTypeVariables #-}
-{-# LANGUAGE OverloadedStrings #-}
-{-# LANGUAGE TypeOperators #-}
 {-# LANGUAGE TypeInType #-}
 {-# OPTIONS_GHC -fno-warn-orphans #-}
 module Typing.Test where
index 00c74f9a566c057e7c87a05befb62f2e5b49ff81..9252a926fa2958ec876cef444a366b3d233da02e 100644 (file)
@@ -28,7 +28,7 @@ name: symantic
 stability: experimental
 synopsis: Library for Typed Tagless-Final Higher-Order Extensible DSL
 tested-with: GHC==8.0.1
-version: 3.20161205
+version: 3.20161208
 
 Source-Repository head
  location: git://git.autogeree.net/symantic
@@ -127,11 +127,14 @@ Test-Suite symantic-test
   hs-source-dirs: Language/Symantic
   main-is: Test.hs
   other-modules:
-    Typing.Test
-    Compiling.Test
+    Compiling.Applicative.Test
     Compiling.Bool.Test
+    Compiling.Foldable.Test
     Compiling.Functor.Test
-    Compiling.Applicative.Test
+    Compiling.Map.Test
+    Compiling.Term.Test
+    Compiling.Test
+    Typing.Test
   if flag(threaded)
     ghc-options: -threaded -rtsopts -with-rtsopts=-N
   if flag(dev)