在 TH QuasiQuote 中使用 DataKinds 生成类型注释

Generating a type annotation using DataKinds within a TH QuasiQuote

在使用模板 haskell 的 haskell 项目中,我试图生成一个表达式,该表达式将类型注释作为虚拟类型。

一个简单的例子是 DataKindsKindSignatures 的情况,例如:

{-# LANGUAGE DataKinds, KindSignatures #-}
data Foo = A | B
data GenMe (w :: Foo) = GenMe Int

[| $(generate some code) :: GenMe $(genType someCompileTimeData) |]

我怎样才能写一个函数,比如 genType 这样

genType :: Foo -> Q Type

提升只是提升保存编译时Foo值的变量?我不知道要使用 Type Data Constructors 中的哪个构造函数 制作数据种类。

有什么想法吗?谢谢!

解决这个问题的另一种方法是定义一个 promote :: Exp -> Maybe Type 函数,然后在 Foo 上使用 lift

-- | Takes the AST for an expression and tries to produce the corresponding
-- promoted type AST.
promote :: Exp -> Q Type
promote (VarE n) = fail ("Cannot promote variable " ++ show n)
promote (ConE n) = pure (PromotedT n)
promote (LitE l) = LitT <$> promoteLit l
promote (TupE es) = foldl AppT (PromotedTupleT (length es)) <$> (traverse promote es)
promote (ListE es) = foldr (\x xs -> AppT (AppT PromotedConsT x) xs) PromotedNilT <$> (traverse promote es)
promote (ParensE e) = ParensT <$> promote e
promote (AppE e1 e2) = AppT <$> promote e1 <*> promote e2
promote (InfixE (Just e1) e2 (Just e3)) = AppT <$> (AppT <$> promote e2 <*> promote e1) <*> promote e3
promote _ = fail "Either impossible to promote or unimplemented"

-- | Promote an expression literal to a type one
promoteLit :: Lit -> Q TyLit
promoteLit (StringL s) = pure (StrTyLit s)
promoteLit (IntegerL i) = pure (NumTyLit i)
promoteLit _ = fail "Expression literal cannot be promoted"

然后,我认为以下内容应该可行

ghci> :set -XDeriveLift -XDataKinds -XKindSignatures -XTemplateHaskell -XQuasiQuotes
ghci> data Foo = A | B deriving (Lift)
ghci> foo1 = A
ghci> data GenMe (w :: Foo) = GenMe Int
ghci> runQ [| GenMe 1 :: GenMe $(promote =<< lift foo1) |]
SigE (AppE (ConE Ghci5.GenMe) (LitE (IntegerL 1))) (AppT (ConT Ghci5.GenMe) (PromotedT Ghci3.A))