]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/Expr/Tuple.hs
init
[haskell/symantic.git] / Language / Symantic / Expr / Tuple.hs
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 'Tuple'.
9 module Language.Symantic.Expr.Tuple where
10
11 -- import qualified Data.Tuple as Tuple
12 import Data.Proxy (Proxy(..))
13 -- import Data.Type.Equality ((:~:)(Refl))
14 import Prelude hiding (maybe)
15
16 import Language.Symantic.Type
17 import Language.Symantic.Repr.Dup
18 import Language.Symantic.Trans.Common
19 import Language.Symantic.Expr.Common
20 import Language.Symantic.Expr.Functor
21
22 -- * Class 'Sym_Tuple_Lam'
23 -- | Symantic.
24 class Sym_Tuple2 repr where
25 tuple2 :: repr a -> repr b -> repr (a, b)
26 default tuple2 :: Trans t repr => t repr a -> t repr b -> t repr (a, b)
27 tuple2 = trans_map2 tuple2
28
29 instance -- Sym_Tuple2 Dup
30 ( Sym_Tuple2 r1
31 , Sym_Tuple2 r2
32 ) => Sym_Tuple2 (Dup r1 r2) where
33 tuple2 (a1 `Dup` a2) (b1 `Dup` b2) = tuple2 a1 b1 `Dup` tuple2 a2 b2
34
35 instance Constraint1_Type Functor_with_Lambda (Type_Type1 ((,) fst) root) where
36 constraint1_type _c (Type_Type1 _ _) = Just Dict
37
38 -- * Type 'Expr_Tuple2'
39 -- | Expression.
40 data Expr_Tuple2 (lam:: * -> *) (root:: *)
41 type instance Root_of_Expr (Expr_Tuple2 lam root) = root
42 type instance Type_of_Expr (Expr_Tuple2 lam root) = Type_Tuple2
43 type instance Sym_of_Expr (Expr_Tuple2 lam root) repr = (Sym_Tuple2 repr)
44 type instance Error_of_Expr ast (Expr_Tuple2 lam root) = No_Error_Expr
45
46 -- | Parsing utility to check that the given type is a 'Type_Tuple2'
47 -- or raise 'Error_Expr_Type_mismatch'.
48 check_type_tuple2
49 :: forall ast ex root ty h ret.
50 ( root ~ Root_of_Expr ex
51 , ty ~ Type_Root_of_Expr ex
52 , Type_Lift Type_Tuple2 (Type_of_Expr root)
53 , Type_Unlift Type_Tuple2 (Type_of_Expr root)
54 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
55 (Error_of_Expr ast root)
56 )
57 => Proxy ex -> ast -> ty h
58 -> (Type_Tuple2 ty h -> Either (Error_of_Expr ast root) ret)
59 -> Either (Error_of_Expr ast root) ret
60 check_type_tuple2 ex ast ty k =
61 case type_unlift $ unType_Root ty of
62 Just ty_l -> k ty_l
63 Nothing -> Left $
64 error_expr ex $
65 Error_Expr_Type_mismatch ast
66 (Exists_Type (type_tuple2 (type_var SZero) (type_var $ SSucc SZero)
67 :: Type_Root_of_Expr ex (Zero, Succ Zero)))
68 (Exists_Type ty)
69
70 -- | Parse 'list_cons'.
71 tuple2_from
72 :: forall root lam ty ast hs ret.
73 ( ty ~ Type_Root_of_Expr (Expr_Tuple2 lam root)
74 , Eq_Type (Type_Root_of_Expr root)
75 , Expr_from ast root
76 , Type_Lift Type_Tuple2 (Type_of_Expr root)
77 , Type_Unlift Type_Tuple2 (Type_of_Expr root)
78 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
79 (Error_of_Expr ast root)
80 , Root_of_Expr root ~ root
81 ) => ast -> ast
82 -> Expr_From ast (Expr_Tuple2 lam root) hs ret
83 tuple2_from ast_a ast_b _ex _ast ctx k =
84 expr_from (Proxy::Proxy root) ast_a ctx $
85 \(ty_a::Type_Root_of_Expr root h_a) (Forall_Repr_with_Context a) ->
86 expr_from (Proxy::Proxy root) ast_b ctx $
87 \(ty_b::Type_Root_of_Expr root h_b) (Forall_Repr_with_Context b) ->
88 k (type_tuple2 ty_a ty_b) $ Forall_Repr_with_Context $
89 \c -> tuple2 (a c) (b c)