Last active
January 5, 2025 14:01
-
-
Save ramirez7/4742eacdfae0588cd100bfb07e124131 to your computer and use it in GitHub Desktop.
ghci command to run top-level TH. Thanks /u/affinehyperplane for the trick! https://www.reddit.com/r/haskell/comments/18vkivp/comment/kh9byjd/?utm_source=reddit&utm_medium=web2x&context=3
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
:{ | |
let doTH code = do | |
u <- Data.Unique.newUnique | |
let decls = "ghciTH_" ++ show (Data.Unique.hashUnique u) | |
pure $ unlines | |
[ unwords ["let", decls, "=", code] | |
, "() = ();" ++ decls | |
] | |
:} | |
:def th doTH |
Note that if there's an error in the code you pass to :th
, it'll print the error and then a spurious error regarding ghciTH_XXX
not being in scope:
λ :th makeWorldAndComponents "World" []
<interactive>:3:16: error:
Variable not in scope:
makeWorldAndComponents :: String -> [a0] -> t
<interactive>:3:9: error:
Variable not in scope:
ghciTH_1 :: Language.Haskell.TH.Lib.Internal.DecsQ
(I forgot to import Apecs
in the above example)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage (to inspect an
apecs
world):