{-# LANGUAGE UndecidableInstances #-} {-# OPTIONS_GHC -fno-warn-orphans #-} {-# OPTIONS_GHC -fconstraint-solver-iterations=5 #-} -- | Symantic for 'IO'. module Language.Symantic.Lib.IO where import Control.Monad (liftM, liftM2) import Data.Proxy import Data.Type.Equality ((:~:)(Refl)) import qualified System.IO as IO import Language.Symantic.Parsing import Language.Symantic.Typing import Language.Symantic.Compiling import Language.Symantic.Interpreting import Language.Symantic.Transforming import Language.Symantic.Lib.Lambda -- * Class 'Sym_IO' class Sym_IO term where io_hClose :: term IO.Handle -> term (IO ()) io_openFile :: term IO.FilePath -> term IO.IOMode -> term (IO IO.Handle) default io_hClose :: Trans t term => t term IO.Handle -> t term (IO ()) default io_openFile :: Trans t term => t term IO.FilePath -> t term IO.IOMode -> t term (IO IO.Handle) io_hClose = trans_map1 io_hClose io_openFile = trans_map2 io_openFile type instance Sym_of_Iface (Proxy IO) = Sym_IO type instance TyConsts_of_Iface (Proxy IO) = Proxy IO ': TyConsts_imported_by IO type instance TyConsts_imported_by IO = [ Proxy IO.Handle , Proxy IO.IOMode , Proxy Applicative , Proxy Functor , Proxy Monad ] type instance TyConsts_imported_by IO.Handle = '[ Proxy Eq ] type instance TyConsts_imported_by IO.IOMode = [ Proxy Enum , Proxy Eq , Proxy Ord ] instance Sym_IO HostI where io_hClose = liftM IO.hClose io_openFile = liftM2 IO.openFile instance Sym_IO TextI where io_hClose = textI1 "IO.hClose" io_openFile = textI2 "IO.openFile" instance (Sym_IO r1, Sym_IO r2) => Sym_IO (DupI r1 r2) where io_hClose = dupI1 @Sym_IO io_hClose io_openFile = dupI2 @Sym_IO io_openFile instance ( Read_TyNameR TyName cs rs , Inj_TyConst cs IO ) => Read_TyNameR TyName cs (Proxy IO ': rs) where read_TyNameR _cs (TyName "IO") k = k (ty @IO) read_TyNameR _rs raw k = read_TyNameR (Proxy @rs) raw k instance ( Read_TyNameR TyName cs rs , Inj_TyConst cs IO.Handle ) => Read_TyNameR TyName cs (Proxy IO.Handle ': rs) where read_TyNameR _cs (TyName "IO.Handle") k = k (ty @IO.Handle) read_TyNameR _rs raw k = read_TyNameR (Proxy @rs) raw k instance ( Read_TyNameR TyName cs rs , Inj_TyConst cs IO.IOMode ) => Read_TyNameR TyName cs (Proxy IO.IOMode ': rs) where read_TyNameR _cs (TyName "IO.Mode") k = k (ty @IO.IOMode) read_TyNameR _rs raw k = read_TyNameR (Proxy @rs) raw k instance Show_TyConst cs => Show_TyConst (Proxy IO ': cs) where show_TyConst TyConstZ{} = "IO" show_TyConst (TyConstS c) = show_TyConst c instance Show_TyConst cs => Show_TyConst (Proxy IO.Handle ': cs) where show_TyConst TyConstZ{} = "IO.Handle" show_TyConst (TyConstS c) = show_TyConst c instance Show_TyConst cs => Show_TyConst (Proxy IO.IOMode ': cs) where show_TyConst TyConstZ{} = "IO.IOMode" show_TyConst (TyConstS c) = show_TyConst c instance -- Proj_TyConC IO ( Proj_TyConst cs IO , Proj_TyConsts cs (TyConsts_imported_by IO) ) => Proj_TyConC cs (Proxy IO) where proj_TyConC _ (TyConst q :$ TyConst c) | Just Refl <- eq_skind (kind_of_TyConst c) (SKiType `SKiArrow` SKiType) , Just Refl <- proj_TyConst c (Proxy @IO) = case () of _ | Just Refl <- proj_TyConst q (Proxy @Applicative) -> Just TyCon | Just Refl <- proj_TyConst q (Proxy @Functor) -> Just TyCon | Just Refl <- proj_TyConst q (Proxy @Monad) -> Just TyCon _ -> Nothing proj_TyConC _c _q = Nothing instance -- Proj_TyConC IO.Handle ( Proj_TyConst cs IO.Handle , Proj_TyConsts cs (TyConsts_imported_by IO.Handle) ) => Proj_TyConC cs (Proxy IO.Handle) where proj_TyConC _ (TyConst q :$ TyConst c) | Just Refl <- eq_skind (kind_of_TyConst c) SKiType , Just Refl <- proj_TyConst c (Proxy @IO.Handle) = case () of _ | Just Refl <- proj_TyConst q (Proxy @Eq) -> Just TyCon _ -> Nothing proj_TyConC _c _q = Nothing instance -- Proj_TyConC IO.IOMode ( Proj_TyConst cs IO.IOMode , Proj_TyConsts cs (TyConsts_imported_by IO.IOMode) ) => Proj_TyConC cs (Proxy IO.IOMode) where proj_TyConC _ (TyConst q :$ TyConst c) | Just Refl <- eq_skind (kind_of_TyConst c) SKiType , Just Refl <- proj_TyConst c (Proxy @IO.IOMode) = case () of _ | Just Refl <- proj_TyConst q (Proxy @Enum) -> Just TyCon | Just Refl <- proj_TyConst q (Proxy @Eq) -> Just TyCon | Just Refl <- proj_TyConst q (Proxy @Ord) -> Just TyCon _ -> Nothing proj_TyConC _c _q = Nothing data instance TokenT meta (ts::[*]) (Proxy IO) = Token_Term_IO_hClose (EToken meta ts) | Token_Term_IO_openFile (EToken meta ts) deriving instance Eq_Token meta ts => Eq (TokenT meta ts (Proxy IO)) deriving instance Show_Token meta ts => Show (TokenT meta ts (Proxy IO)) instance -- CompileI ( Inj_TyConst (TyConsts_of_Ifaces is) IO , Inj_TyConst (TyConsts_of_Ifaces is) IO.Handle , Inj_TyConst (TyConsts_of_Ifaces is) [] , Inj_TyConst (TyConsts_of_Ifaces is) Char , Inj_TyConst (TyConsts_of_Ifaces is) IO.IOMode , Inj_TyConst (TyConsts_of_Ifaces is) (->) , Inj_TyConst (TyConsts_of_Ifaces is) () , Compile is ) => CompileI is (Proxy IO) where compileI tok ctx k = case tok of Token_Term_IO_hClose tok_h -> -- hClose :: Handle -> IO () compileO tok_h ctx $ \ty_h (TermO h) -> check_TyEq (At Nothing (ty @IO.Handle)) (At (Just tok_h) ty_h) $ \Refl -> k (ty @IO :$ ty @()) $ TermO $ io_hClose . h Token_Term_IO_openFile tok_fp -> -- openFile :: FilePath -> IOMode -> IO Handle compileO tok_fp ctx $ \ty_fp (TermO fp) -> check_TyEq (At Nothing tyFilePath) (At (Just tok_fp) ty_fp) $ \Refl -> k (ty @IO.IOMode ~> ty @IO :$ ty @IO.Handle) $ TermO $ \c -> lam $ io_openFile (fp c) where tyFilePath = ty @[] :$ ty @Char instance -- TokenizeT Inj_Token meta ts IO => TokenizeT meta ts (Proxy IO) where tokenizeT _t = mempty { tokenizers_infix = tokenizeTMod [Mod_Name "IO"] [ tokenize1 "hClose" infixN5 Token_Term_IO_hClose , tokenize1 "openFile" infixN5 Token_Term_IO_openFile ] } instance Gram_Term_AtomsT meta ts (Proxy IO) g