I need a monad which will report error data types (not strings) over the course of a computation. I’ve explored a couple different implementations:
- A
State [Error] amonad where errors are added with cons (:) and at the very end I callreverseon the error list to get the actual order. - A
Writer (Endo [Error]) amonad where I can add errors withEndo (e :). I’m worried about all the useless concatenation of identity functions, though. If I never add any errors then myEndowill still be a large data structure comprised of many concatenatedid . idcompositions. - A
Reader (MVector s Error) (ST s a)monad where I grow the error vector when adding a new error. There isn’t a pre-defined “push” function in the vector package so I’d have to write my own. Also, it would require me to add anSTmonad to some of my computations.
In an imperative language, I’d use a vector and call the “push” method or equivalent which would give me amortized O(1) appends and the resulting list would be in the correct order.
What is the most efficient implementation of a Haskell monad (relative to the efficiency of an imperative language) for this task?
Some of my code is in an ST monad and some of my code is pure. I can use different monads for these different use cases. Should I use something different for my ST code than for my pure code?