1 {-# LANGUAGE UndecidableInstances #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3 {-# OPTIONS_GHC -fconstraint-solver-iterations=5 #-}
4 -- | Symantic for 'IO'.
5 module Language.Symantic.Compiling.IO where
7 import Control.Monad (liftM, liftM2)
9 import Data.Text (Text)
10 import Data.Type.Equality ((:~:)(Refl))
11 import qualified System.IO as IO
13 import Language.Symantic.Parsing
14 import Language.Symantic.Typing
15 import Language.Symantic.Compiling.Term
16 import Language.Symantic.Interpreting
17 import Language.Symantic.Transforming.Trans
20 class Sym_IO term where
21 io_hClose :: term IO.Handle -> term (IO ())
22 io_openFile :: term IO.FilePath -> term IO.IOMode -> term (IO IO.Handle)
24 default io_hClose :: Trans t term => t term IO.Handle -> t term (IO ())
25 default io_openFile :: Trans t term => t term IO.FilePath -> t term IO.IOMode -> t term (IO IO.Handle)
27 io_hClose = trans_map1 io_hClose
28 io_openFile = trans_map2 io_openFile
30 type instance Sym_of_Iface (Proxy IO) = Sym_IO
31 type instance Consts_of_Iface (Proxy IO) = Proxy IO ': Consts_imported_by IO
32 type instance Consts_imported_by IO =
39 type instance Consts_imported_by IO.Handle =
42 type instance Consts_imported_by IO.IOMode =
48 instance Sym_IO HostI where
49 io_hClose = liftM IO.hClose
50 io_openFile = liftM2 IO.openFile
51 instance Sym_IO TextI where
52 io_hClose = textI1 "IO.hClose"
53 io_openFile = textI2 "IO.openFile"
54 instance (Sym_IO r1, Sym_IO r2) => Sym_IO (DupI r1 r2) where
55 io_hClose = dupI1 (Proxy @Sym_IO) io_hClose
56 io_openFile = dupI2 (Proxy @Sym_IO) io_openFile
59 ( Read_TypeNameR Text cs rs
61 ) => Read_TypeNameR Text cs (Proxy IO ': rs) where
62 read_typenameR _cs "IO" k = k (ty @IO)
63 read_typenameR _rs raw k = read_typenameR (Proxy @rs) raw k
65 ( Read_TypeNameR Text cs rs
66 , Inj_Const cs IO.Handle
67 ) => Read_TypeNameR Text cs (Proxy IO.Handle ': rs) where
68 read_typenameR _cs "IO.Handle" k = k (ty @IO.Handle)
69 read_typenameR _rs raw k = read_typenameR (Proxy @rs) raw k
71 ( Read_TypeNameR Text cs rs
72 , Inj_Const cs IO.IOMode
73 ) => Read_TypeNameR Text cs (Proxy IO.IOMode ': rs) where
74 read_typenameR _cs "IO.Mode" k = k (ty @IO.IOMode)
75 read_typenameR _rs raw k = read_typenameR (Proxy @rs) raw k
77 instance Show_Const cs => Show_Const (Proxy IO ': cs) where
78 show_const ConstZ{} = "IO"
79 show_const (ConstS c) = show_const c
80 instance Show_Const cs => Show_Const (Proxy IO.Handle ': cs) where
81 show_const ConstZ{} = "IO.Handle"
82 show_const (ConstS c) = show_const c
83 instance Show_Const cs => Show_Const (Proxy IO.IOMode ': cs) where
84 show_const ConstZ{} = "IO.IOMode"
85 show_const (ConstS c) = show_const c
87 instance -- Proj_ConC IO
89 , Proj_Consts cs (Consts_imported_by IO)
90 ) => Proj_ConC cs (Proxy IO) where
91 proj_conC _ (TyConst q :$ TyConst c)
92 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType)
93 , Just Refl <- proj_const c (Proxy::Proxy IO)
95 _ | Just Refl <- proj_const q (Proxy::Proxy Applicative) -> Just Con
96 | Just Refl <- proj_const q (Proxy::Proxy Functor) -> Just Con
97 | Just Refl <- proj_const q (Proxy::Proxy Monad) -> Just Con
99 proj_conC _c _q = Nothing
100 instance -- Proj_ConC IO.Handle
101 ( Proj_Const cs IO.Handle
102 , Proj_Consts cs (Consts_imported_by IO.Handle)
103 ) => Proj_ConC cs (Proxy IO.Handle) where
104 proj_conC _ (TyConst q :$ TyConst c)
105 | Just Refl <- eq_skind (kind_of_const c) SKiType
106 , Just Refl <- proj_const c (Proxy::Proxy IO.Handle)
108 _ | Just Refl <- proj_const q (Proxy::Proxy Eq) -> Just Con
110 proj_conC _c _q = Nothing
111 instance -- Proj_ConC IO.IOMode
112 ( Proj_Const cs IO.IOMode
113 , Proj_Consts cs (Consts_imported_by IO.IOMode)
114 ) => Proj_ConC cs (Proxy IO.IOMode) where
115 proj_conC _ (TyConst q :$ TyConst c)
116 | Just Refl <- eq_skind (kind_of_const c) SKiType
117 , Just Refl <- proj_const c (Proxy::Proxy IO.IOMode)
119 _ | Just Refl <- proj_const q (Proxy::Proxy Enum) -> Just Con
120 | Just Refl <- proj_const q (Proxy::Proxy Eq) -> Just Con
121 | Just Refl <- proj_const q (Proxy::Proxy Ord) -> Just Con
123 proj_conC _c _q = Nothing
124 data instance TokenT meta (ts::[*]) (Proxy IO)
125 = Token_Term_IO_hClose (EToken meta ts)
126 | Token_Term_IO_openFile (EToken meta ts)
127 deriving instance Eq_Token meta ts => Eq (TokenT meta ts (Proxy IO))
128 deriving instance Show_Token meta ts => Show (TokenT meta ts (Proxy IO))
130 ( Inj_Const (Consts_of_Ifaces is) IO
131 , Inj_Const (Consts_of_Ifaces is) IO.Handle
132 , Inj_Const (Consts_of_Ifaces is) []
133 , Inj_Const (Consts_of_Ifaces is) Char
134 , Inj_Const (Consts_of_Ifaces is) IO.IOMode
135 , Inj_Const (Consts_of_Ifaces is) (->)
136 , Inj_Const (Consts_of_Ifaces is) ()
138 ) => CompileI is (Proxy IO) where
141 Token_Term_IO_hClose tok_h ->
142 -- hClose :: Handle -> IO ()
143 compileO tok_h ctx $ \ty_h (TermO h) ->
145 (At Nothing (ty @IO.Handle))
146 (At (Just tok_h) ty_h) $ \Refl ->
147 k (ty @IO :$ ty @()) $ TermO $
149 Token_Term_IO_openFile tok_fp ->
150 -- openFile :: FilePath -> IOMode -> IO Handle
151 compileO tok_fp ctx $ \ty_fp (TermO fp) ->
153 (At Nothing tyFilePath)
154 (At (Just tok_fp) ty_fp) $ \Refl ->
155 k (ty @IO.IOMode ~> ty @IO :$ ty @IO.Handle) $ TermO $
156 \c -> lam $ io_openFile (fp c)
157 where tyFilePath = ty @[] :$ ty @Char