Q:

A MonadTransformer to typecheck for a completed action

Preamble
This is one of those questions where I think that someone has already solved my problem, but I don't know where to look.
Question
I am looking for a MonadTransformer that carries the idea of being complete or incomplete in such a way that an action of the monad stack will fail to typecheck unless it is complete.
My Situation
I have a Monad stack with a state which holds an MVar.  Failing to write to this MVar will cause a thread blocked indefinitely in an MVar exception. I could just check the MVar itself, but handing it off to another thread (which follows the same rules) is also a valid way to get the MVar filled (as is throwing an error).
I am seeking some way for the typechecker to throw an error if one of those conditions is not met by the time the monad is run.  I can check at run time but, I think there might be a way to use the type system.

A:

Sounds like some kind of indexed monad could help. Indexed monads allow or forbid some operations according to some type-level state.

We could also rely on polymorphism:
{-# language RankNTypes #-}
import Control.Monad
import Control.Monad.Trans.State

-- Receives an v and returns a "proof" token
newtype Gulp token v = Gulp (v -> IO token)

-- Computation polymorphic on the token
type EnsuredWrite v r = forall token. StateT (Gulp token v) IO (token,r)

The idea is that actions of type EnsuredWrite need to return a token value, but only know how to produce one by feeding the function in Gulp. If they return at all, they must have called that function.
The actual type of the token is not important, it can be a simple (). But EnsuredWrite actions shouldn't know that, hence the forall.
This solution doesn't forbid repeated writes, though.

