{-# LANGUAGE DefaultSignatures #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeFamilies #-} {-# OPTIONS_GHC -fno-warn-orphans #-} -- | Expression for 'Tuple'. module Language.Symantic.Expr.Tuple where import Data.Proxy (Proxy(..)) import Prelude hiding (maybe) import Language.Symantic.Type import Language.Symantic.Trans.Common import Language.Symantic.Expr.Root import Language.Symantic.Expr.Error import Language.Symantic.Expr.From -- * Class 'Sym_Tuple_Lam' -- | Symantic. class Sym_Tuple2 repr where tuple2 :: repr a -> repr b -> repr (a, b) default tuple2 :: Trans t repr => t repr a -> t repr b -> t repr (a, b) tuple2 = trans_map2 tuple2 instance Constraint_Type1 Functor (Type_Type1 (Proxy ((,) fst)) root) where constraint_type1 _c Type_Type1{} = Just Dict -- * Type 'Expr_Tuple2' -- | Expression. data Expr_Tuple2 (lam:: * -> *) (root:: *) type instance Root_of_Expr (Expr_Tuple2 lam root) = root type instance Type_of_Expr (Expr_Tuple2 lam root) = Type_Tuple2 type instance Sym_of_Expr (Expr_Tuple2 lam root) repr = (Sym_Tuple2 repr) type instance Error_of_Expr ast (Expr_Tuple2 lam root) = No_Error_Expr -- | Parsing utility to check that the given type is a 'Type_Tuple2' -- or raise 'Error_Expr_Type_mismatch'. check_type_tuple2 :: forall ast ex root ty h ret. ( root ~ Root_of_Expr ex , ty ~ Type_Root_of_Expr ex , Lift_Type Type_Tuple2 (Type_of_Expr root) , Unlift_Type Type_Tuple2 (Type_of_Expr root) , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast) (Error_of_Expr ast root) ) => Proxy ex -> ast -> ty h -> (Type_Tuple2 ty h -> Either (Error_of_Expr ast root) ret) -> Either (Error_of_Expr ast root) ret check_type_tuple2 ex ast ty k = case unlift_type $ unType_Root ty of Just ty_l -> k ty_l Nothing -> Left $ error_expr ex $ Error_Expr_Type_mismatch ast (Exists_Type (type_tuple2 (type_var0 SZero) (type_var0 $ SSucc SZero) :: Type_Root_of_Expr ex (Var0, Var0))) (Exists_Type ty) -- | Parse 'tuple2'. tuple2_from :: forall root lam ty ast hs ret. ( ty ~ Type_Root_of_Expr (Expr_Tuple2 lam root) , Eq_Type (Type_Root_of_Expr root) , Expr_from ast root , Lift_Type Type_Tuple2 (Type_of_Expr root) , Unlift_Type Type_Tuple2 (Type_of_Expr root) , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast) (Error_of_Expr ast root) , Root_of_Expr root ~ root ) => ast -> ast -> Expr_From ast (Expr_Tuple2 lam root) hs ret tuple2_from ast_a ast_b _ex _ast ctx k = expr_from (Proxy::Proxy root) ast_a ctx $ \(ty_a::Type_Root_of_Expr root h_a) (Forall_Repr_with_Context a) -> expr_from (Proxy::Proxy root) ast_b ctx $ \(ty_b::Type_Root_of_Expr root h_b) (Forall_Repr_with_Context b) -> k (type_tuple2 ty_a ty_b) $ Forall_Repr_with_Context $ \c -> tuple2 (a c) (b c)