问题
Function reify allows me to look up information about a given name. For a function the returned value is 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
. Is there a way to get the function's declaration?
回答1:
From the template haskell docs on the VarI Info contructor:
A "value" variable (as opposed to a type variable, see
TyVarI
). TheMaybe Dec
field containsJust
the declaration which defined the variable -- including the RHS of the declaration -- or elseNothing
, in the case where the RHS is unavailable to the compiler. At present, this value is alwaysNothing
: returning the RHS has not yet been implemented because of lack of interest.
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:
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
.
Digging deaper, this code was added in 2003, in what looks like a rewrite of the reify system. 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
. 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).
来源:https://stackoverflow.com/questions/20118008/how-to-get-the-declaration-of-a-function-using-reify