1 {-# LANGUAGE UndecidableInstances #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3 {-# OPTIONS_GHC -fconstraint-solver-iterations=11 #-}
4 -- | Symantic for @(,)@.
5 module Language.Symantic.Compiling.Tuple2 where
7 import Control.Monad (liftM, liftM2)
8 import Data.Monoid ((<>))
10 import Data.Text (Text)
11 import qualified Data.Tuple as Tuple
12 import Data.Type.Equality ((:~:)(Refl))
13 import Prelude hiding (fst, snd)
15 import Language.Symantic.Parsing
16 import Language.Symantic.Typing
17 import Language.Symantic.Compiling.Term
18 import Language.Symantic.Interpreting
19 import Language.Symantic.Transforming.Trans
21 -- * Class 'Sym_Tuple2'
22 class Sym_Tuple2 term where
23 tuple2 :: term a -> term b -> term (a, b)
24 fst :: term (a, b) -> term a
25 snd :: term (a, b) -> term b
27 default tuple2 :: Trans t term => t term a -> t term b -> t term (a, b)
28 default fst :: Trans t term => t term (a, b) -> t term a
29 default snd :: Trans t term => t term (a, b) -> t term b
31 tuple2 = trans_map2 tuple2
35 type instance Sym_of_Iface (Proxy (,)) = Sym_Tuple2
36 type instance Consts_of_Iface (Proxy (,)) = Proxy (,) ': Consts_imported_by (,)
37 type instance Consts_imported_by (,) =
50 instance Sym_Tuple2 HostI where
54 instance Sym_Tuple2 TextI where
55 tuple2 (TextI a) (TextI b) =
57 let p' = precedence_Toplevel in
58 "(" <> a p' v <> ", " <> b p' v <> ")"
61 instance (Sym_Tuple2 r1, Sym_Tuple2 r2) => Sym_Tuple2 (DupI r1 r2) where
62 tuple2 = dupI2 (Proxy @Sym_Tuple2) tuple2
63 fst = dupI1 (Proxy @Sym_Tuple2) fst
64 snd = dupI1 (Proxy @Sym_Tuple2) snd
67 ( Read_TypeNameR Text cs rs
69 ) => Read_TypeNameR Text cs (Proxy (,) ': rs) where
70 read_typenameR _cs "(,)" k = k (ty @(,))
71 read_typenameR _rs raw k = read_typenameR (Proxy @rs) raw k
72 instance Show_Const cs => Show_Const (Proxy (,) ': cs) where
73 show_const ConstZ{} = "(,)"
74 show_const (ConstS c) = show_const c
78 , Proj_Consts cs (Consts_imported_by (,))
81 ) => Proj_ConC cs (Proxy (,)) where
82 proj_conC _ (TyConst q :$ (TyConst c :$ a))
83 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType `SKiArrow` SKiType)
84 , Just Refl <- proj_const c (Proxy::Proxy (,))
86 _ | Just Refl <- proj_const q (Proxy::Proxy Applicative)
87 , Just Con <- proj_con (ty @Monoid :$ a) -> Just Con
88 | Just Refl <- proj_const q (Proxy::Proxy Functor) -> Just Con
89 | Just Refl <- proj_const q (Proxy::Proxy Foldable) -> Just Con
90 | Just Refl <- proj_const q (Proxy::Proxy Monad)
91 , Just Con <- proj_con (ty @Monoid :$ a) -> Just Con
92 | Just Refl <- proj_const q (Proxy::Proxy Traversable) -> Just Con
94 proj_conC _ (t@(TyConst q) :$ (TyConst c :$ a :$ b))
95 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType `SKiArrow` SKiType)
96 , Just Refl <- proj_const c (Proxy::Proxy (,))
98 _ | Just Refl <- proj_const q (Proxy::Proxy Bounded)
99 , Just Con <- proj_con (t :$ a)
100 , Just Con <- proj_con (t :$ b) -> Just Con
101 | Just Refl <- proj_const q (Proxy::Proxy Eq)
102 , Just Con <- proj_con (t :$ a)
103 , Just Con <- proj_con (t :$ b) -> Just Con
104 | Just Refl <- proj_const q (Proxy::Proxy Monoid)
105 , Just Con <- proj_con (t :$ a)
106 , Just Con <- proj_con (t :$ b) -> Just Con
107 | Just Refl <- proj_const q (Proxy::Proxy Ord)
108 , Just Con <- proj_con (t :$ a)
109 , Just Con <- proj_con (t :$ b) -> Just Con
110 | Just Refl <- proj_const q (Proxy::Proxy Show)
111 , Just Con <- proj_con (t :$ a)
112 , Just Con <- proj_con (t :$ b) -> Just Con
114 proj_conC _c _q = Nothing
115 data instance TokenT meta (ts::[*]) (Proxy (,))
116 = Token_Term_Tuple2 (EToken meta ts) (EToken meta ts)
117 | Token_Term_Tuple2_fst (EToken meta ts)
118 | Token_Term_Tuple2_snd (EToken meta ts)
119 deriving instance Eq_Token meta ts => Eq (TokenT meta ts (Proxy (,)))
120 deriving instance Show_Token meta ts => Show (TokenT meta ts (Proxy (,)))
122 ( Inj_Const (Consts_of_Ifaces is) (,)
124 ) => CompileI is (Proxy (,)) where
127 Token_Term_Tuple2 tok_a tok_b ->
128 -- (,) :: a -> b -> (a, b)
129 compileO tok_a ctx $ \ty_a (TermO a) ->
130 compileO tok_b ctx $ \ty_b (TermO b) ->
131 k (ty @(,) :$ ty_a :$ ty_b) $ TermO $
132 \c -> tuple2 (a c) (b c)
133 Token_Term_Tuple2_fst tok_ab ->
134 -- fst :: (a, b) -> a
135 compileO tok_ab ctx $ \ty_ab (TermO ab) ->
136 check_type2 (ty @(,)) (At (Just tok_ab) ty_ab) $ \Refl ty_a _ty_b ->
139 Token_Term_Tuple2_snd tok_ab ->
140 -- snd :: (a, b) -> b
141 compileO tok_ab ctx $ \ty_ab (TermO ab) ->
142 check_type2 (ty @(,)) (At (Just tok_ab) ty_ab) $ \Refl _ty_a ty_b ->