Last active
December 19, 2015 00:29
-
-
Save ruicc/5869498 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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