Created
September 2, 2016 00:31
-
-
Save amosr/abf3331c2743575990f5863fdb691055 to your computer and use it in GitHub Desktop.
Template Haskell reify does not return declaration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- $ ghc --version | |
-- The Glorious Glasgow Haskell Compilation System, version 8.0.1 | |
-- $ runghc main.hs | |
-- "Nothing" | |
{-# LANGUAGE TemplateHaskell #-} | |
import THStuff | |
-- This definition has a known value. | |
-- If we reify it, it should have a declaration of "Just 5" (with some extra junk) | |
value :: Int | |
value = 5 | |
-- flush so that "value" is available below | |
return [] | |
main = do | |
-- What declaration do we get when we reify? | |
-- "Nothing". | |
print $(get 'value) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Template haskell parts need to be in separate module, because of staging restriction. | |
-- Use GHC 8, since the VarI constructor changed between 7 and 8. | |
{-# LANGUAGE TemplateHaskell #-} | |
module THStuff where | |
import Language.Haskell.TH | |
-- Try to convert a name into its definition. | |
-- This would be really useful for forcing definitions to be inlined into a particular callsite. | |
get :: Name -> Q Exp | |
get nm = do | |
-- Reify here returns an Info. https://hackage.haskell.org/package/template-haskell-2.11.0.0/docs/Language-Haskell-TH.html#t:Info | |
-- We only care about the VarI case, which would include names with known values such as function definitions, as well as unknown values like function arguments. | |
VarI _ _ dec <- reify nm | |
-- The third argument (dec) is a Maybe Dec. | |
-- From the docs: | |
-- 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. At present, this value is _always_ Nothing: returning the RHS has not yet been implemented because of lack of interest. | |
-- Convert the dec into a string just so we can return it. | |
let d = show dec | |
[|d|] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment