Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.
Newbie question. I can't believe I'm still struggling to grapple MTL stuff. The lucid library has HtmlT m a which has a MonadState instance. But how exactly do you use it? I tried flip runState mempty someFunc where someFunc is MonadState String m => HtmlT m () but that doesn't compile.
HtmlT m a
flip runState mempty someFunc
MonadState String m => HtmlT m ()
StateT String (HtmlT Identity) () seems one step forward, but then you have to lift all UI elements, thus losing access to state.
StateT String (HtmlT Identity) ()
Aha, flip runStateT mempty $ evalHtmlT someFunc works.
flip runStateT mempty $ evalHtmlT someFunc
Grr, but that doesn't render the Html at runtime
Okay, after reading the source ... unpack and pack it back: HtmlT $ fmap fst $ flip runStateT mempty $ runHtmlT someFunc
HtmlT $ fmap fst $ flip runStateT mempty $ runHtmlT someFunc
you're doing something cray here!
monad transformers should only need to be unpeeled in main
flip runState myState $ runHtmlT myHTMLFUNCTION
the State here is global
if you want local state semantic, you want to build a StateT s Html instead
StateT s Html
With StateT s Html you would have lift when rendering UI, but at that point - access to state is lost.
And it seems that the lucid library recommends putting State inside the HtmlT transformer: https://hackage.haskell.org/package/lucid-2.9.12/docs/Lucid.html#t:HtmlT
So to "inject" an inner monad, I had to peel the skin, apply the topical cream (runState), then put it back.
(well, that reads pretty gory)
Real-world code that lead to all this: https://funprog.zulipchat.com/#narrow/stream/201385-Haskell/topic/HTML.20in.20Haskell/near/188742480