2013-11-21 38 views
10

函数reify允许我查找有关给定名称的信息。对于一个函数的返回值是VarI如何使用`reify`获取函数声明?

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

在这里,我可以检查函数的类型,我也想检验其声明。但是,在VarI的第三个参数中,我总是看到Nothing。有没有办法获得函数的声明?

+0

这是一个很好的问题。我试过标记'INLINE'或'INLINEABLE'的定义,但都没有什么区别。我怀疑它根本没有实施,但我不知道。 – Carl

+0

[如何使用模板Haskell获取函数的主体?]的可能重复(http://stackoverflow.com/questions/13983391/how-to-use-template-haskell-to-get-the-body-of -功能) – jberryman

回答

7

template haskell docs on the VarI Info contructor

A “值” 变量(而不是一种类型的变量,参见TyVarI)。 Maybe Dec字段包含Just定义变量的声明 - 包括声明的RHS - 或Nothing,其中RHS对编译器不可用。目前,此值为总是Nothing:由于缺乏兴趣,返回RHS尚未实施。

望着ghc source mirror on github,在string VarI only appears twice,并且无论是在compiler/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) 

像模板哈斯克尔说的文档,使用该字段的值始终Nothing

挖掘便宜,this code was added in 2003,看起来像reify系统的重写。所以它似乎对获得它的工作没有兴趣,因为它已经超过10年了,现场总是有价值Nothing。所以我猜你是否想要自己实现这个功能(或者向ghc开发邮件列表提出一个很好的用例来鼓励别人去做)。