Skip to content

Instantly share code, notes, and snippets.

@ruicc
Last active December 19, 2015 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruicc/5869498 to your computer and use it in GitHub Desktop.
Save ruicc/5869498 to your computer and use it in GitHub Desktop.
{-# LANGUAGE GADTs, MultiParamTypeClasses, ScopedTypeVariables #-}
module Main where
import System.IO
import Control.Monad.Operational.Simple
data FileIO a where
Open :: FilePath -> FileIO Handle
Get :: Handle -> FileIO String
Echo :: String -> FileIO ()
Close :: Handle -> FileIO String
instance Operational FileIO IO where
-- singleton :: FileIO a -> IO a
singleton (Open path) = openFile path ReadMode
singleton (Get hdl) = hGetLine hdl
singleton (Close hdl) = hClose hdl >> return "closed"
singleton (Echo str) = putStrLn str
open :: FilePath -> Program FileIO Handle
open = singleton . Open
get :: Handle -> Program FileIO String
get = singleton . Get
close :: Handle -> Program FileIO String
close = singleton . Close
echo :: String -> Program FileIO ()
echo = singleton . Echo
main :: IO ()
main = do
val <- fromProgram $ do
hdl <- open "./hoho"
line <- get hdl
echo line
close hdl
return "FileIO計算結果"
putStrLn val
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment