How can I make my stack monad faster? I made a stack monad that lets one manipulate and control values on a stack.

I want to know if it's correct (it seems correct), how to make it faster, and how I could detect stack overflows without carrying around the stack size and checking against it (I've heard about guard pages but I have no idea how I'd use them with Haskell).

{-# LANGUAGE RankNTypes #-}
module Main where

import Data.IORef

import Foreign.Ptr
import Foreign.Storable
import Foreign.Marshal.Alloc

import Control.Exception ( bracket )

import System.IO.Unsafe


newtype StackRef s a = StackRef (Ptr a)

data Stack = Stack (Ptr ())
newtype StackMonad s a = StackMonad (Stack -&gt; IO (Stack, a))

instance Monad (StackMonad s) where
 return x = ioToStackMonad (return x)
 (StackMonad m) &gt;&gt;= f = StackMonad $ \stack -&gt; do
   (newStack, x) &lt;- m stack
   let (StackMonad g) = f x
   g newStack

runStackWithSize :: Int -&gt; (forall s. StackMonad s a) -&gt; a
runStackWithSize stackSize (StackMonad f) = unsafePerformIO $
 bracket (mallocBytes stackSize) free $ \theStack -&gt; do
  (_, a) &lt;- f (Stack theStack)
  return a

-- | 1024 bytes is the usual size for a stack
runStack :: (forall s. StackMonad s a) -&gt; a
runStack = runStackWithSize 1024


newStackRef :: Storable a =&gt; a -&gt; StackMonad s (StackRef s a)
newStackRef value = StackMonad $ \(Stack topOfStack) -&gt; do
 let ptr = castPtr (alignPtr topOfStack (alignment value))
 poke ptr value
 return (Stack (plusPtr ptr (sizeOf value)), StackRef ptr)

ioToStackMonad :: IO a -&gt; StackMonad s a
ioToStackMonad action = StackMonad $ \ptr -&gt; do
 a &lt;- action
 return (ptr, a)

writeStackRef :: Storable a =&gt; StackRef s a -&gt; a -&gt; StackMonad s ()
writeStackRef (StackRef p) val = ioToStackMonad (poke p val)

readStackRef :: Storable a =&gt; StackRef s a -&gt; StackMonad s a
readStackRef (StackRef p) = ioToStackMonad (peek p)

{- |
 The type hackery means that pointers to the old stuff isn't reachable,
 therefore it's okay to pop the stack pointer.
-}
stack :: (forall s. (forall b. StackRef t b -&gt; StackRef s b) -&gt; StackMonad s a) -&gt; StackMonad t a
stack f = let (StackMonad s) = f (\(StackRef p) -&gt; StackRef p) in StackMonad $ \old_ptr -&gt; do
 (_, state) &lt;- s old_ptr
 return (old_ptr, state)

stack_ :: (forall s. (forall b. StackRef t b -&gt; StackRef s b) -&gt; StackMonad s ()) -&gt; StackMonad t ()
stack_ f = let (StackMonad s) = f (\(StackRef p) -&gt; StackRef p) in StackMonad $ \old_ptr -&gt; do
 _ &lt;- s old_ptr
 return (old_ptr, ())

x :: Int
x = runStack $ do
 a &lt;- newStackRef 5

 c &lt;- stack $ \lift1 -&gt; do
  b &lt;- newStackRef 1

  let liftedA = lift1 a
  aValue &lt;- readStackRef liftedA
  writeStackRef liftedA 6
  bValue &lt;- readStackRef b
  return (aValue + bValue)
 newAValue &lt;- readStackRef a
 return (c + newAValue)

main = print x
This code is simply incorrect. It is even not a stack (because it lacks
pop operation and it is impossible to implement it in a type-safe way).
Here is an example:

runStack $ do
  newStackRef (5:: Int)
  newStackRef False
  newStackRef 'x'


according to the definition of the newStackRef it just pokes Storable
representation of the value into the memory block and advances the pointer.
Any information about the type of that value is lost.

To pop a value from such a stack you need to specify correct type and
compiler (or RTS) is not able to warn you if the type is not correct.

To the monad part of the question. From the instance Monad
definition it is easy to see that it almost the same as the State monad (here is how it defined in 
Control.Monad.State.Trans.State.Lazy)

instance (Monad m) =&gt; Monad (StateT s m) where
    return a = state $ \s -&gt; (a, s)
    m &gt;&gt;= k  = StateT $ \s -&gt; do
        ~(a, s') &lt;- runStateT m s
        runStateT (k a) s'


The only difference is that it operates on top of IO an has fixed type of state. So it is actually StateT (Ptr ()) IO.

It is even not clear what it is for a stack to be an instance of Monad.
E.g. list monad allows nondeterministic computations (in prolog-like
style), Maybe monad allows computations that fail.

To enable computation to access and operate stack it is easier to use existing State monad with some stack implementation as a state. I see no reason to implement your own Stack monad.