[GHC] #8220: Macros / functions for source location

#8220: Macros / functions for source location ------------------------------------+------------------------------------- Reporter: wojteknar | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.6.3 Keywords: | Operating System: Unknown/Multiple Architecture: Unknown/Multiple | Type of failure: None/Unknown Difficulty: Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | ------------------------------------+------------------------------------- In GNAT (Ada compiler) there is a special package with functions (evaluated at compile time) that return source location and enclosing entity name. This is very useful for logging and debugging. I'd love to have something similar in GHC. http://www.radford.edu/~nokie/classes/320/std_lib_html/gnat- source_info.html -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8220 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8220: Macros / functions for source location -------------------------------------+------------------------------------ Reporter: wojteknar | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.6.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Unknown/Multiple Type of failure: None/Unknown | Difficulty: Unknown Test Case: | Blocked By: Blocking: | Related Tickets: -------------------------------------+------------------------------------ Comment (by ezyang): You can do this using the C pre-processor: {{{ {-# LANGUAGE CPP #-} main = do print (__FILE__, __LINE__) }}} which is arguably the right place to do it. Is there any particular reason you want an ordinary function? A traditional function suffers from a severe modularity problem, because you can't use one of these functions in another function like "ASSERT"; it will report the line number of the assert definition and not the actual use-site of the assert; a pre- processor macro does not have this problem. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8220#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8220: Macros / functions for source location -------------------------------------+------------------------------------ Reporter: wojteknar | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.6.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Unknown/Multiple Type of failure: None/Unknown | Difficulty: Unknown Test Case: | Blocked By: Blocking: | Related Tickets: -------------------------------------+------------------------------------ Comment (by wojteknar): So I have been told on #haskell, that I can get the file and line from the preprocessor, but what I want primarily is the function name, with control over inlining. This is very useful in various situations like tracing, timing, running testcases. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8220#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8220: Macros / functions for source location -------------------------------------+------------------------------------ Reporter: wojteknar | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.6.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Unknown/Multiple Type of failure: None/Unknown | Difficulty: Unknown Test Case: | Blocked By: Blocking: | Related Tickets: -------------------------------------+------------------------------------ Comment (by simonpj): Also in Template Haskell you can get the source location of the splice. Thus {{{ ...$(do { loc <- location; ... }).... }}} But not (currently) the name of the enclosing function. It's not obvious exactly what you need here. The innermost enclosing function? Outermost? What if the splice is in a type declaration not a value declaration? In general, TH (rather than CPP) seems a nicer place to experiment. You could work on a design in collaboration with others, with worked-out use- cases. Simon -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8220#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8220: Macros / functions for source location -------------------------------------+------------------------------------ Reporter: wojteknar | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.6.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Unknown/Multiple Type of failure: None/Unknown | Difficulty: Unknown Test Case: | Blocked By: Blocking: | Related Tickets: -------------------------------------+------------------------------------ Comment (by wojteknar): My original use case is a Parsec parser, many functions like this: {{{ magic = word16 stream_MAGIC <?> "magic" version = word16 stream_VERSION <?> "version" nullMark = word8 tc_NULL*> pure () <?> "nullMark" }}} Being only human, I often forget to match the <?> "label" with the function name. I believe the outermost enclosing function would be best. Also, I noticed that some exceptions are able to print the function name, but this is probably the innermost. {{{ oneForOne 1 = 1 main = print $ oneForOne 2 -- Prints -- Exception: exception-test.hs:10:1-15: Non-exhaustive patterns in function oneForOne }}} Admittedly, this is not a feature one cannot live without, but it is very handy in some situations. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8220#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8220: Macros / functions for source location -------------------------------------+------------------------------------ Reporter: wojteknar | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.6.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Unknown/Multiple Type of failure: None/Unknown | Difficulty: Unknown Test Case: | Blocked By: Blocking: | Related Tickets: -------------------------------------+------------------------------------ Comment (by wojteknar): On second thought, looking at my Parsec code, innermost enclosing function would be valuable, too. {{{ innerFunName, outerFunName :: String }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8220#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8220: Macros / functions for source location -------------------------------------+------------------------------------ Reporter: wojteknar | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.6.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Unknown/Multiple Type of failure: None/Unknown | Difficulty: Unknown Test Case: | Blocked By: Blocking: | Related Tickets: -------------------------------------+------------------------------------ Comment (by simonpj): Sounds plausible. But to progress we need a worked-out design. (I suggest via the TH route.) What, precisely, does it look like from the programmer's point of view? The way lies open for you to develop one, with input from others. Simon -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8220#comment:6 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8220: Macros / functions for source location -------------------------------------+------------------------------------ Reporter: wojteknar | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.6.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Unknown/Multiple Type of failure: None/Unknown | Difficulty: Unknown Test Case: | Blocked By: Blocking: | Related Tickets: -------------------------------------+------------------------------------ Comment (by literon): The extension in the branch https://github.com/sol/rewrite-with-location/ can help. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8220#comment:7 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC