Skip to content

Instantly share code, notes, and snippets.

@arildm
Created March 2, 2013 16:16
Show Gist options
  • Save arildm/5071749 to your computer and use it in GitHub Desktop.
Save arildm/5071749 to your computer and use it in GitHub Desktop.
type IP a = State -> (State,a)
data State
andThen :: IP a -> (a -> IP b) -> IP b
f `andThen` g = \s0 -> let (s1,a) = f s0
(s2,b) = g a s1
in (s2,b)
instance Monad IP where
(>>=) = andThen
return x = \s -> (s,x)
safePrint line = do
noInk <- outOfInk
if noInk
then return -1
else do print line
return 0
outOfInk = \s -> (s,False)
@arildm
Copy link
Author

arildm commented Mar 2, 2013

Output:

Prelude> :r
[1 of 1] Compiling Main             ( ExplicitState.hs, interpreted )

ExplicitState.hs:9:10:
    Illegal instance declaration for `Monad IP'
      (All instance types must be of the form (T t1 ... tn)
       where T is not a synonym.
       Use -XTypeSynonymInstances if you want to disable this.)
    In the instance declaration for `Monad IP'
Failed, modules loaded: none.

And with {-# LANGUAGE TypeSynonymInstances #-}:

Prelude> :r
[1 of 1] Compiling Main             ( ExplicitState.hs, interpreted )

ExplicitState.hs:11:10:
    Type synonym `IP' should have 1 argument, but has been given none
    In the instance declaration for `Monad IP'
Failed, modules loaded: none.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment