Haskell — dual personality IO / ST monad?

It has type classes for "references to mutable data", separated for readable, writable, etc., with instances for IORef and STRef as well as things like TVar MVar ForeignPtr etc.

Up vote 12 down vote favorite 1 share g+ share fb share tw.

I have some code that currently uses a ST monad for evaluation. I like not putting IO everywhere because the runST method produces a pure result, and indicates that such result is safe to call (versus unsafePerformIO). However, as some of my code has gotten longer, I do want to put debugging print statements in.

Is there any class that provides a dual-personality monad or typeclass machinery, one which can be a ST or an IO (depending on its type or a "isDebug" flag)? I recall SPJ introduced a "Mutation" class in his "Fun with Type Functions" paper, which used associative types to relate IO to IORef and ST to STRef. Does such exist as a package somewhere?

Edit / solution Thanks very much the nth time, C.A. McCann! Using that solution, I was able to introduce an additional class for monads which support a pdebug function. The ST monad will ignore these calls, whereas IO will run putStrLn.

Class DebugMonad m where pdebug :: String -> m () instance DebugMonad (ST s) where pdebug _ = return () instance DebugMonad IO where pdebug = putStrLn test initV = do v.

It has type classes for "references to mutable data", separated for readable, writable, etc., with instances for IORef and STRef, as well as things like TVar, MVar, ForeignPtr, etc.

Have you considered Debug.Trace. Trace instead? haskell.org/haskellwiki/Debugging.

I am currently using that. However, it has its weaknesses, such as the trace results only being printed once: after a variable has a value (e.g. The result of the ST computation), it's not recomputed. Example: I type seq (somecomp somedata) () into ghci; the first time it prints debug and an error message, the second time just the error.

Of course, :r works, but I'd rather it have better semantics i.e. Debug. Trace is kind of a hack.

– gatoatigrado Jul 26 '11 at 22:30.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions