{-# 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 qualified Data.Tuple as Tuple import Data.Proxy (Proxy(..)) -- import Data.Type.Equality ((:~:)(Refl)) import Prelude hiding (maybe) import Language.Symantic.Type import Language.Symantic.Repr.Dup import Language.Symantic.Trans.Common import Language.Symantic.Expr.Common import Language.Symantic.Expr.Functor -- * 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 -- Sym_Tuple2 Dup ( Sym_Tuple2 r1 , Sym_Tuple2 r2 ) => Sym_Tuple2 (Dup r1 r2) where tuple2 (a1 `Dup` a2) (b1 `Dup` b2) = tuple2 a1 b1 `Dup` tuple2 a2 b2 instance Constraint1_Type Functor_with_Lambda (Type_Type1 ((,) fst) root) where constraint1_type _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 , Type_Lift Type_Tuple2 (Type_of_Expr root) , Type_Unlift Type_Tuple2 (Type_of_Expr root) , Error_Expr_Lift (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 type_unlift $ 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_var SZero) (type_var $ SSucc SZero) :: Type_Root_of_Expr ex (Zero, Succ Zero))) (Exists_Type ty) -- | Parse 'list_cons'. 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 , Type_Lift Type_Tuple2 (Type_of_Expr root) , Type_Unlift Type_Tuple2 (Type_of_Expr root) , Error_Expr_Lift (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)