1 {-# LANGUAGE DefaultSignatures #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE MultiParamTypeClasses #-}
5 {-# LANGUAGE ScopedTypeVariables #-}
6 {-# LANGUAGE TypeFamilies #-}
7 {-# OPTIONS_GHC -fno-warn-orphans #-}
8 -- | Expression for tuples.
9 module Language.Symantic.Expr.Tuple where
11 import Data.Proxy (Proxy(..))
12 import Prelude hiding (maybe, fst, snd)
14 import Language.Symantic.Type
15 import Language.Symantic.Trans.Common
16 import Language.Symantic.Expr.Root
17 import Language.Symantic.Expr.Error
18 import Language.Symantic.Expr.From
20 -- * Class 'Sym_Tuple_Lam'
22 class Sym_Tuple2 repr where
23 tuple2 :: repr a -> repr b -> repr (a, b)
24 fst :: repr (a, b) -> repr a
25 snd :: repr (a, b) -> repr b
27 default tuple2 :: Trans t repr => t repr a -> t repr b -> t repr (a, b)
28 default fst :: Trans t repr => t repr (a, b) -> t repr a
29 default snd :: Trans t repr => t repr (a, b) -> t repr b
31 tuple2 = trans_map2 tuple2
35 -- * Type 'Expr_Tuple2'
37 data Expr_Tuple2 (root:: *)
38 type instance Root_of_Expr (Expr_Tuple2 root) = root
39 type instance Type_of_Expr (Expr_Tuple2 root) = Type_Tuple2
40 type instance Sym_of_Expr (Expr_Tuple2 root) repr = (Sym_Tuple2 repr)
41 type instance Error_of_Expr ast (Expr_Tuple2 root) = No_Error_Expr
43 -- | Parsing utility to check that the given type is a 'Type_Tuple2'
44 -- or raise 'Error_Expr_Type_mismatch'.
46 :: forall ast ex root ty h ret.
47 ( root ~ Root_of_Expr ex
48 , ty ~ Type_Root_of_Expr ex
49 , Lift_Type Type_Tuple2 (Type_of_Expr root)
50 , Unlift_Type Type_Tuple2 (Type_of_Expr root)
51 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
52 (Error_of_Expr ast root)
54 => Proxy ex -> ast -> ty h
55 -> (Type_Tuple2 ty h -> Either (Error_of_Expr ast root) ret)
56 -> Either (Error_of_Expr ast root) ret
57 check_type_tuple2 ex ast ty k =
58 case unlift_type $ unType_Root ty of
62 Error_Expr_Type_mismatch ast
63 (Exists_Type (type_tuple2 (type_var0 SZero) (type_var0 $ SSucc SZero)
69 :: forall root ty ast hs ret.
70 ( ty ~ Type_Root_of_Expr (Expr_Tuple2 root)
73 , Lift_Type Type_Tuple2 (Type_of_Expr root)
74 , Unlift_Type Type_Tuple2 (Type_of_Expr root)
75 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
76 (Error_of_Expr ast root)
77 , Root_of_Expr root ~ root
79 -> Expr_From ast (Expr_Tuple2 root) hs ret
80 tuple2_from ast_a ast_b _ex _ast ctx k =
81 expr_from (Proxy::Proxy root) ast_a ctx $
82 \(ty_a::ty h_a) (Forall_Repr_with_Context a) ->
83 expr_from (Proxy::Proxy root) ast_b ctx $
84 \(ty_b::ty h_b) (Forall_Repr_with_Context b) ->
85 k (type_tuple2 ty_a ty_b) $ Forall_Repr_with_Context $
86 \c -> tuple2 (a c) (b c)
90 :: forall root ty ast hs ret.
91 ( ty ~ Type_Root_of_Expr (Expr_Tuple2 root)
94 , Lift_Type Type_Tuple2 (Type_of_Expr root)
95 , Unlift_Type Type_Tuple2 (Type_of_Expr root)
96 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
97 (Error_of_Expr ast root)
98 , Root_of_Expr root ~ root
100 -> Expr_From ast (Expr_Tuple2 root) hs ret
101 fst_from ast_t ex ast ctx k =
102 expr_from (Proxy::Proxy root) ast_t ctx $
103 \(ty_t::ty h_t) (Forall_Repr_with_Context t) ->
104 check_type_tuple2 ex ast ty_t $ \(Type_Type2 _ ty_a _ty_b) ->
105 k ty_a $ Forall_Repr_with_Context $
110 :: forall root ty ast hs ret.
111 ( ty ~ Type_Root_of_Expr (Expr_Tuple2 root)
114 , Lift_Type Type_Tuple2 (Type_of_Expr root)
115 , Unlift_Type Type_Tuple2 (Type_of_Expr root)
116 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
117 (Error_of_Expr ast root)
118 , Root_of_Expr root ~ root
120 -> Expr_From ast (Expr_Tuple2 root) hs ret
121 snd_from ast_t ex ast ctx k =
122 expr_from (Proxy::Proxy root) ast_t ctx $
123 \(ty_t::ty h_t) (Forall_Repr_with_Context t) ->
124 check_type_tuple2 ex ast ty_t $ \(Type_Type2 _ _ty_a ty_b) ->
125 k ty_b $ Forall_Repr_with_Context $