简体   繁体   English

如何使用`reify`获取函数的声明?

[英]How to get the declaration of a function using `reify`?

Function reify allows me to look up information about a given name. 函数reify允许我查找有关给定名称的信息。 For a function the returned value is VarI : 对于函数,返回值为VarI

data Info = ... |  VarI Name Type (Maybe Dec) Fixity  | ...

Here I can examine the function's type, and I'd also like to examine its declaration. 在这里,我可以检查函数的类型,也想检查其声明。 However, in the 3rd argument to VarI I always see Nothing . 但是,在VarI的第三个参数中,我始终看不到Nothing Is there a way to get the function's declaration? 有没有办法获取函数的声明?

From the template haskell docs on the VarI Info contructor : VarI信息构造器上模板haskell文档中

A "value" variable (as opposed to a type variable, see TyVarI ). 一个“值”变量(与类型变量相反,请参见TyVarI )。 The Maybe Dec field contains Just the declaration which defined the variable -- including the RHS of the declaration -- or else Nothing , in the case where the RHS is unavailable to the compiler. 如果RHS对编译器不可用,则Maybe Dec字段Just包含定义变量的声明-包括声明的RHS-否则为Nothing At present, this value is always Nothing : returning the RHS has not yet been implemented because of lack of interest. 目前,此值始终为 Nothing :由于缺乏兴趣,尚未执行返回RHS的操作。

Looking at the ghc source mirror on github , the string VarI only appears twice , and both in the compiler/typecheck/TcSplice.lhs implementing the reifyThing function: 查看github上的ghc源镜像字符串VarI仅出现两次 ,并且都在实现reifyThing函数的编译器 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)

Like the template haskell docs said, the value used for that field is always Nothing . 就像模板haskell docs所说的那样,用于该字段的值始终为Nothing

Digging deaper, this code was added in 2003 , in what looks like a rewrite of the reify system. 挖掘死神, 此代码在2003年添加,看起来像是对reify系统的重写。 So it does appear to be little interest in getting it working since it has been more than 10 years that field has always had the value Nothing . 因此,它似乎是在得到它的工作,因为它已经超过10年的那场一直有值一点兴趣Nothing So I'm guessing if you want the feature you will have to implement it yourself (or propose a good use case to the ghc development mailing list that would encourage someone else to do it). 因此,我猜测如果您需要该功能,则必须自己实现(或向ghc开发邮件列表提出一个很好的用例,以鼓励其他人使用它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM