我正在编写一个模板 haskell 库供我自己使用,它会生成函数。我想做的是将这些生成的函数传递给酸态的 makeAcidic TH 函数。
但是,在我的 Q 上下文中简单地传递生成的名称的结果会导致此错误:
不在具体化范围内
如何将我新创建的函数置于 makeAcidic 工作的范围内?
我的代码:
gen_render :: Name -> Q [Dec]
gen_render typName =
do (TyConI d) <- reify typName -- Get all the information on the type
(type_name, vars, nars, constructors) <- typeInfo (return d) -- extract name and constructors
x <- mapM func_def constructors
let funcs = concat x
let setterNames = [setter | (setter, _) <- funcs]
acidic <- makeAcidic typName setterNames
return $ [newFuncs | (_, newFuncs) <- funcs] ++ acidic
where func_def data_con@(con_name, compontents)
= do
x <- mapM genFieldSetandGet compontents
return $ concat x
genFieldSetandGet (Just fieldName, fieldType) = do
uVar <- runQ $ varE $ mkName "u"
ixInsert <- appsE $ [[| IxSet.insert |], (varE $ mkName "u"), parensE $ appsE [(varE fieldName), (varE $ mkName "db")]]
{-instanceD (cxt [])
(conT (mkName "DatabaseAccess") `appT` (return fieldType) `appT` conT typName)-}
let setterName = (mkName $ "updateDb" ++ (capitalize $ nameBase fieldName))
func <- funD setterName $
[clause [(varP $ mkName "u"), (varP $ mkName "db")] (normalB $
[| modify |] `appE` [| IxSet.insert |] `appE` (varE $ mkName "u") ) []]
return [(setterName, func)]