函数reify
允许我查找有关给定名称的信息。对于函数,返回值为VarI
:
data Info = ... | VarI Name Type (Maybe Dec) Fixity | ...
在这里我可以检查函数的类型,我也想检查它的声明。但是,在VarI
的第3个参数中,我总是看到Nothing
。有没有办法获得函数的声明?
答案 0 :(得分:7)
来自template haskell docs on the VarI Info contructor:
“值”变量(与类型变量相对,请参阅
TyVarI
)。Maybe Dec
字段包含Just
声明,该声明定义变量 - 包括声明的RHS - 或者Nothing
,在RHS不可用于编译器的情况下。目前,此值始终Nothing
:由于缺乏兴趣,返回RHS尚未实施。
查看实施reifyThing
功能的ghc source mirror on github中的string VarI
only appears twice,compiler/typecheck/TcSplice.lhs和两者:
reifyThing :: TcTyThing -> TcM TH.Info
-- The only reason this is monadic is for error reporting,
-- which in turn is mainly for the case when TH can't express
-- some random GHC extension
reifyThing (AGlobal (AnId id))
= do { ty <- reifyType (idType id)
; fix <- reifyFixity (idName id)
; let v = reifyName id
; case idDetails id of
ClassOpId cls -> return (TH.ClassOpI v ty (reifyName cls) fix)
_ -> return (TH.VarI v ty Nothing fix)
}
reifyThing (AGlobal (ATyCon tc)) = reifyTyCon tc
reifyThing (AGlobal (ADataCon dc))
= do { let name = dataConName dc
; ty <- reifyType (idType (dataConWrapId dc))
; fix <- reifyFixity name
; return (TH.DataConI (reifyName name) ty
(reifyName (dataConOrigTyCon dc)) fix)
}
reifyThing (ATcId {tct_id = id})
= do { ty1 <- zonkTcType (idType id) -- Make use of all the info we have, even
-- though it may be incomplete
; ty2 <- reifyType ty1
; fix <- reifyFixity (idName id)
; return (TH.VarI (reifyName id) ty2 Nothing fix) }
reifyThing (ATyVar tv tv1)
= do { ty1 <- zonkTcTyVar tv1
; ty2 <- reifyType ty1
; return (TH.TyVarI (reifyName tv) ty2) }
reifyThing thing = pprPanic "reifyThing" (pprTcTyThingCategory thing)
与haskell docs所说的模板一样,该字段使用的值始终为Nothing
。
挖掘deaper,this code was added in 2003,看起来像重写了reify系统。所以它似乎没有兴趣让它工作,因为已经超过10年,该领域总是具有值Nothing
。所以我猜你是否想要这个功能,你必须自己实现它(或者向ghc开发邮件列表提出一个很好的用例,鼓励别人去做)。