<div dir="ltr">I'm working on writing a Haskell wrapper for the GNU Lightning code generation library (<a href="http://www.gnu.org/software/lightning/">http://www.gnu.org/software/lightning/</a>), which in C consists of a series of macros that write to a certain global state in a pointer _jit, such as jit_add or jit_call. Now, I figure that can be wrapped fairly simply in a monad, which we'll call the Lightning monad:<div><br></div><div>-- syntax given here is just a sample.</div><div>incr = lightningEmit $ do</div><div>    prolog</div><div>    in <- arg</div><div>    getArg R0, in</div><div>    addi R0, R0, 1</div><div>    retr R0</div><div><br></div><div>However, I've come up with four possible designs for the Lightning monad. All but one would be reader monad transformers holding the pointer to the JIT state.</div><div><br></div><div>1) Lightning m a, where m is a MonadIO.</div><div>Pros: Users have the power of the monads they are using within.</div><div>Cons: Complexity, especially with having to handle monads with multiple returns like ListT.</div><div><br></div><div>2) Lightning a, which just wraps IO, and is a MonadIO.</div><div>Pros: Allows the use of other IO functions in the monad.</div><div>Cons: Allows the use of other IO functions in the monad.</div><div><br></div><div>3) Lightning a, which just wraps IO, and is not a MonadIO.</div><div>Pros: Simplicity; possibly permitting the code generation to be unwrapped with unsafePerformIO.</div><div>Cons: Referential integrity could still be violated.</div><div><br></div><div>4) Lightning a, which would be a free monad over the Lightning operations allowed.</div><div>Pros: Evaluation (in the IO monad) would be certain to be pure, which would permit or accommodate it being a monad transformer, or converted to a pure function on architectures where the Lightning library is not available.</div><div>Cons: Assembling, then disassembling to reassemble, a long list of instructions produces significant overhead, rendering it less suitable for JIT purposes. Interacting with the generated code is more difficult.</div><div><br></div><div>Are there any other options I am missing? Are there any possibly game-changing pros or cons I have failed to notice?</div><div><br></div><div>Thanks,</div><div><br>Paul Drees</div></div>