]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/Compiling/Monad.hs
Add Parsing.Grammar.
[haskell/symantic.git] / Language / Symantic / Compiling / Monad.hs
1 {-# LANGUAGE UndecidableInstances #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3 -- | Symantic for 'Monad'.
4 module Language.Symantic.Compiling.Monad where
5
6 import Control.Monad (Monad)
7 import qualified Control.Monad as Monad
8 import Data.Monoid ((<>))
9 import Data.Proxy
10 import Data.Text (Text)
11 import Data.Type.Equality ((:~:)(Refl))
12 import Prelude hiding (Monad(..))
13
14 import Language.Symantic.Parsing
15 import Language.Symantic.Typing
16 import Language.Symantic.Compiling.Term
17 import Language.Symantic.Compiling.Applicative (Sym_Applicative)
18 import Language.Symantic.Interpreting
19 import Language.Symantic.Transforming.Trans
20
21 -- * Class 'Sym_Monad'
22 class Sym_Applicative term => Sym_Monad term where
23 return :: Monad m => term a -> term (m a)
24 (>>=) :: Monad m => term (m a) -> term (a -> m b) -> term (m b)
25 when :: Applicative f => term Bool -> term (f ()) -> term (f ())
26
27 default return :: (Trans t term, Monad m)
28 => t term a -> t term (m a)
29 default (>>=) :: (Trans t term, Monad m)
30 => t term (m a) -> t term (a -> m b) -> t term (m b)
31 default when :: (Trans t term, Applicative f)
32 => t term Bool -> t term (f ()) -> t term (f ())
33
34 return = trans_map1 return
35 (>>=) = trans_map2 (>>=)
36 when = trans_map2 when
37
38 infixl 1 >>=
39
40 type instance Sym_of_Iface (Proxy Monad) = Sym_Monad
41 type instance Consts_of_Iface (Proxy Monad) = Proxy Monad ': Consts_imported_by Monad
42 type instance Consts_imported_by Monad =
43 [ Proxy ()
44 , Proxy Applicative
45 , Proxy Bool
46 ]
47
48 instance Sym_Monad HostI where
49 return = Monad.liftM Monad.return
50 (>>=) = Monad.liftM2 (Monad.>>=)
51 when = Monad.liftM2 Monad.when
52 instance Sym_Monad TextI where
53 return = textI1 "return"
54 (>>=) = textI_infix ">>=" (Precedence 1)
55 when (TextI cond) (TextI ok) =
56 TextI $ \p v ->
57 let p' = Precedence 2 in
58 paren p p' $
59 "when " <> cond p' v <>
60 " " <> ok p' v
61 instance (Sym_Monad r1, Sym_Monad r2) => Sym_Monad (DupI r1 r2) where
62 return = dupI1 (Proxy @Sym_Monad) return
63 (>>=) = dupI2 (Proxy @Sym_Monad) (>>=)
64 when = dupI2 (Proxy @Sym_Monad) when
65
66 instance
67 ( Read_TypeNameR Text cs rs
68 , Inj_Const cs Monad
69 ) => Read_TypeNameR Text cs (Proxy Monad ': rs) where
70 read_typenameR _cs "Monad" k = k (ty @Monad)
71 read_typenameR _rs raw k = read_typenameR (Proxy @rs) raw k
72 instance Show_Const cs => Show_Const (Proxy Monad ': cs) where
73 show_const ConstZ{} = "Monad"
74 show_const (ConstS c) = show_const c
75
76 instance Proj_ConC cs (Proxy Monad)
77 data instance TokenT meta (ts::[*]) (Proxy Monad)
78 = Token_Term_Monad_return (EToken meta '[Proxy Token_Type]) (EToken meta ts)
79 | Token_Term_Monad_bind (EToken meta ts) (EToken meta ts)
80 | Token_Term_Monad_when (EToken meta ts) (EToken meta ts)
81 deriving instance (Eq meta, Eq_Token meta ts) => Eq (TokenT meta ts (Proxy Monad))
82 deriving instance (Show meta, Show_Token meta ts) => Show (TokenT meta ts (Proxy Monad))
83 instance -- CompileI
84 ( Read_TypeName Name_LamVar (Consts_of_Ifaces is)
85 , Inj_Const (Consts_of_Ifaces is) Monad
86 , Inj_Const (Consts_of_Ifaces is) (->)
87 , Inj_Const (Consts_of_Ifaces is) ()
88 , Inj_Const (Consts_of_Ifaces is) Applicative
89 , Inj_Const (Consts_of_Ifaces is) Bool
90 , Proj_Con (Consts_of_Ifaces is)
91 , Compile is
92 ) => CompileI is (Proxy Monad) where
93 compileI tok ctx k =
94 case tok of
95 Token_Term_Monad_return tok_ty_m tok_a ->
96 -- return :: Monad m => a -> m a
97 compile_type tok_ty_m $ \(ty_m::Type (Consts_of_Ifaces is) m) ->
98 check_kind
99 (At Nothing (SKiType `SKiArrow` SKiType))
100 (At (Just tok_ty_m) $ kind_of ty_m) $ \Refl ->
101 check_con (At (Just tok_ty_m) (ty @Monad :$ ty_m)) $ \Con ->
102 compileO tok_a ctx $ \ty_a (TermO a) ->
103 k (ty_m :$ ty_a) $ TermO $
104 \c -> return (a c)
105 Token_Term_Monad_bind tok_ma tok_a2mb ->
106 -- (>>=) :: Monad m => m a -> (a -> m b) -> m b
107 compileO tok_ma ctx $ \ty_ma (TermO ma) ->
108 compileO tok_a2mb ctx $ \ty_a2mb (TermO a2mb) ->
109 check_con1 (ty @Monad) (At (Just tok_ma) ty_ma) $ \Refl Con ty_ma_m ty_ma_a ->
110 check_type2 (ty @(->)) (At (Just tok_a2mb) ty_a2mb) $ \Refl ty_a2mb_a ty_a2mb_mb ->
111 check_type1 ty_ma_m (At (Just tok_a2mb) ty_a2mb_mb) $ \Refl _ty_a2mb_mb_b ->
112 check_type
113 (At (Just tok_a2mb) ty_a2mb_a)
114 (At (Just tok_ma) ty_ma_a) $ \Refl ->
115 k ty_a2mb_mb $ TermO $
116 \c -> (>>=) (ma c) (a2mb c)
117 Token_Term_Monad_when tok_cond tok_ok ->
118 -- when :: Applicative f => Bool -> f () -> f ()
119 compileO tok_cond ctx $ \ty_cond (TermO cond) ->
120 compileO tok_ok ctx $ \ty_ok (TermO ok) ->
121 check_con1 (ty @Applicative) (At (Just tok_ok) ty_ok) $ \Refl Con _ty_ok_f ty_ok_u ->
122 check_type
123 (At Nothing (ty @Bool))
124 (At (Just tok_cond) ty_cond) $ \Refl ->
125 check_type
126 (At Nothing (ty @()))
127 (At (Just tok_ok) ty_ok_u) $ \Refl ->
128 k ty_ok $ TermO $
129 \c -> when (cond c) (ok c)