@@ -1,12 +1,16 @@
1module Main where
1module Main where
3import Prelude
3import Prelude
4+import Control.Monad.Eff.Console
5data Tuple a b = Tuple a b
6data Tuple a b = Tuple a b
8+instance showTuple :: (Show a, Show b) => Show (Tuple a b) where
9+ show (Tuple a b) = "(" <> show a <> ", " <> show b <> ")"
7−class MonadState s m where
11+class Monad m <= MonadState s m where
8 get :: m s
12 get :: m s
9− put :: s -> m {}
13+ put :: s -> m Unit
11data State s a = State (s -> Tuple s a)
15data State s a = State (s -> Tuple s a)
@@ -29,20 +33,29 @@ instance monadState :: Monad (State s)
30instance monadStateState :: MonadState s (State s) where
34instance monadStateState :: MonadState s (State s) where
31 get = State (\s -> Tuple s s)
35 get = State (\s -> Tuple s s)
32− put s = State (\_ -> Tuple s {})
36+ put s = State (\_ -> Tuple s unit)
38+-- Without the call to same, the following strange (but correct, in the absence of
39+-- functional dependencies) type:
45+-- ) => (t1 -> t2) -> m Unit
47+-- With the type hint, the inferred type is more sensible:
34−modify :: forall m s. (Prelude.Monad m, MonadState s m) => (s -> s) -> m {}
51+-- , MonadState t m
52+-- ) => (t -> t) -> m Unit
35−modify f = do
53+modify f =
37− put (f s)
56+ put (same f s)
39−test :: Tuple String String
58+ same :: forall a. (a -> a) -> (a -> a)
40−test = runState "" $ do
59+ same = id
41− modify $ (++) "World!"
60+
42− modify $ (++) "Hello, "
61+main = print $ runState 0 (modify (+ 1))
47− Control.Monad.Eff.Console.log "Done"