Pet*_*lák 10 haskell reify template-haskell
函数reify允许我查找有关给定名称的信息.对于函数,返回的值是VarI:
data Info = ... | VarI Name Type (Maybe Dec) Fixity | ...
Run Code Online (Sandbox Code Playgroud)
在这里,我可以检查函数的类型,我也想检查它的声明.但是,在VarI我总是看到的第三个论点中Nothing.有没有办法获得函数的声明?
"值"变量(与类型变量相对,请参阅参考资料
TyVarI).该Maybe Dec字段包含Just定义变量的声明 - 包括声明的RHS - 否则Nothing,在RHS不可用于编译器的情况下.目前,这个价值总是如此Nothing:由于缺乏兴趣,返回RHS尚未实施.
查看github上的ghc源镜像,该字符串VarI只出现两次,并且在编译器/ typecheck/TcSplice.lhs中都实现了该reifyThing函数:
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)
Run Code Online (Sandbox Code Playgroud)
就像模板haskell docs所说的那样,用于该字段的值总是如此Nothing.
挖掘更便宜,这个代码是在2003年添加的,看起来像是重写了reify系统.所以它似乎没有兴趣让它工作,因为它已经超过10年,该领域一直有价值Nothing.所以我猜你是否想要你自己必须实现的功能(或者为ghc开发邮件列表提出一个好的用例,鼓励别人去做).