title: Using the state monad to hide explicit state
body: <p>I'm trying to write a little game in Haskell, and there's a fair amount of state necessary to pass around. I want to try hiding the state with the State monad</p>
  
  <p>Now I've run into a problem: functions which take the state and an argument were easy to write to work in the state monad. But there are also functions that just take the state as argument (and return a modified state, or possibly something else).</p>
  
  <p>In one part of my code, I have this line:</p>
  
  <pre><code>let player = getCurrentPlayer state
  </code></pre>
  
  <p>I would like it to not take state, and instead write</p>
  
  <pre><code>player &lt;- getCurrentPlayerM
  </code></pre>
  
  <p>currently, its implementation looks like this</p>
  
  <pre><code>getCurrentPlayer gameState = 
    (players gameState) ! (on_turn gameState)
  </code></pre>
  
  <p>and it seemed simple enough to make it work in the State monad by writing it like this: </p>
  
  <pre><code>getCurrentPlayerM = do state &lt;- get
                         return (players state ! on_turn state)
  </code></pre>
  
  <p>However, that provokes complaints from ghc! No instance for (MonadState GameState m0) arising from a use of `get', it says. I had already rewritten a very similar function, except that wasn't nullary in its State monad form, so on a hunch, I rewrote it like this:</p>
  
  <pre><code>getCurrentPlayerM _ = do state &lt;- get
                           return (players state ! on_turn state)
  </code></pre>
  
  <p>And sure enough, it works! But of course I have to call it as getCurrentPlayerM (), and I feel a little silly doing that. Passing in an argument was what I wanted to avoid in the first place!</p>
  
  <p>An additional surprise: looking at its type in ghci I get </p>
  
  <pre><code>getCurrentPlayerM :: MonadState GameState m =&gt; t -&gt; m P.Player
  </code></pre>
  
  <p>but if I try to set that explicitly in my code, I get another error: "Non type-variable argument in the constraint MonadState GameState m" and an offer of a language extension to permit it. I suppose it's because my GameState is a type and not a typeclass, but why it's accepted in practice but not when I try to be explicit about it I'm more confused about.</p>
  
  <p>So to sum up: </p>
  
  <ol>
  <li>Why can't I write nullary functions in the State monad?</li>
  <li>Why can't I declare the type my workaround function actually has?</li>
  </ol>