How to use getQ and putQ in a haskell template?

I would like to handle states in a Q monad with Template Haskell. According to some stack overflow answers, there is a solution that uses unsafePerformIO , but I want to avoid using it as much as possible.

I found getQ and putQ in the Language.Haskell.TH.Syntax module. These functions process the states in the Q monad represented by template-haskell 2.10. I tried using this API, but getQ unable to get the state.

The following code is an example of my problem. I expect x be limited to (Just B) , but x always Nothing .

 -- X.hs {-# LANGUAGE DeriveDataTypeable #-} module X where import Data.Typeable data T = A | B | C deriving (Typeable,Show) -- Y.hs {-# LANGUAGE TemplateHaskell #-} module Y where import X import Language.Haskell.TH import Language.Haskell.TH.Syntax -- splice for testing getQ and putQ do putQ B x <- getQ :: Q (Maybe T) runIO $ print x -- prints Nothing return [] 

As a result, I received the following compilation message.

 $ ghc -fforce-recomp Y.hs [1 of 2] Compiling X ( X.hs, Xo ) [2 of 2] Compiling Y ( Y.hs, Yo ) Nothing 

How to use getQ and putQ ?

+6
source share
1 answer

There is currently a bug in getQ: it has recently been fixed and should work in the next version of GHC.

+1
source

Source: https://habr.com/ru/post/990029/


All Articles