14. 型の力
●
JavaScriptの例と似たようなやつ
import Data.IORef
action :: IO ()
action = do
x <- newIORef 1
a <- f x 10 – 11
b <- f x 1 – 3
c <- f x 10 – 13
return ()
f :: IORef Int -> Int -> IO Int
f x y = do
current <- readIORef x
modifyIORef x (+1) – 副作用
return $ current + y
15. import Data.IORef
action :: IO ()
action = do
x <- newIORef 1
a <- f x 10 – 11
b <- f x 1 – 3
c <- f x 10 – 13
return ()
f :: IORef Int -> Int -> IO Int
f x y = do
current <- readIORef x
modifyIORef x (+1) – 副作用
return $ current + y
型の力
●
JavaScriptの例と似たようなやつf x 10
同じ呼び出しで違う戻り値
これはIO型の
副作用によるもの
16. import Data.IORef
action :: ()
action = do
x <- newIORef 1
a <- f x 10
b <- f x 1
c <- f x 10
return ()
f :: Int -> Int -> Int
f x y = do
current <- readIORef x
modifyIORef x (+1)
return $ current + y
型の力
●
JavaScriptの例と似たようなやつ
関数のIO …を消すと ?
19. 型の力
●
副作用がないはずの型の関数に
副作用が含まれているので
コンパイルエラー
Program.hs:4:1:
Couldn't match expected type ‘IO t3’ with actual type ‘()’
In the expression: main
When checking the type of the IO action ‘main’
Program.hs:5:3:
Couldn't match type ‘IO ()’ with ‘()’
Expected type: IO (IORef Integer) -> (IORef Integer -> IO
()) -> ()
Actual type: IO (IORef Integer)
-> (IORef Integer -> IO ()) -> IO ()
In a stmt of a 'do' block: x <- newIORef 1
In the expression:
do { x <- newIORef 1;
a <- f x 10;
b <- f x 1;
c <- f x 10;
20. 型の力
●
副作用がないはずの型の関数に
副作用が含まれているので
コンパイルエラー
Program.hs:4:1:
Couldn't match expected type ‘IO t3’ with actual type ‘()’
In the expression: main
When checking the type of the IO action ‘main’
Program.hs:5:3:
Couldn't match type ‘IO ()’ with ‘()’
Expected type: IO (IORef Integer) -> (IORef Integer -> IO
()) -> ()
Actual type: IO (IORef Integer)
-> (IORef Integer -> IO ()) -> IO ()
In a stmt of a 'do' block: x <- newIORef 1
In the expression:
do { x <- newIORef 1;
a <- f x 10;
b <- f x 1;
c <- f x 10;
Program.hs:6:8:
Couldn't match expected type ‘IO t0’ with actual type ‘In
In a stmt of a 'do' block: a <- f x 10
In the expression:
do { x <- newIORef 1;
a <- f x 10;
b <- f x 1;
c <- f x 10;
.... }
21. 型の力
●
副作用がないはずの型の関数に
副作用が含まれているので
コンパイルエラー
Program.hs:4:1:
Couldn't match expected type ‘IO t3’ with actual type ‘()’
In the expression: main
When checking the type of the IO action ‘main’
Program.hs:5:3:
Couldn't match type ‘IO ()’ with ‘()’
Expected type: IO (IORef Integer) -> (IORef Integer -> IO
()) -> ()
Actual type: IO (IORef Integer)
-> (IORef Integer -> IO ()) -> IO ()
In a stmt of a 'do' block: x <- newIORef 1
In the expression:
do { x <- newIORef 1;
a <- f x 10;
b <- f x 1;
c <- f x 10;
Program.hs:6:8:
Couldn't match expected type ‘IO t0’ with actual type ‘In
In a stmt of a 'do' block: a <- f x 10
In the expression:
do { x <- newIORef 1;
a <- f x 10;
b <- f x 1;
c <- f x 10;
.... }
Program.hs:7:10:
Couldn't match expected type ‘Int’ with actual type ‘IORef
Integer’
In the first argument of ‘f’, namely ‘x’
In a stmt of a 'do' block: b <- f x 1
Program.hs:8:8:
Couldn't match expected type ‘IO t2’ with actual type ‘Int’
In a stmt of a 'do' block: c <- f x 10
In the expression:
do { x <- newIORef 1;
a <- f x 10;
b <- f x 1;
c <- f x 10;
>> Too many compile error <<