Monday, May 27, 2013

[ryvpijhs] Code reflection at compile time

Consider a Haskell extension providing this function:

compileTimeAnalyze :: (Code -> IO ()) -> a -> a

"compileTypeAnalyze f e" passes to user-defined function "f" the parse tree (and any other useful information such as types and symbol table) of the expression "e" and evaluates "f" at compile time.  At run time, it simply evaluates "e".

The likely use is to emit warnings or errors if there is something domain-specifically wrong with the code that the compiler might not detect.

Inspired by debugging 2D graphics code.  We often want the number of references to some X coordinate be the same as the number to the corresponding Y coordinate, or else there is likely a typo where we typed X instead of Y or vice versa.

We probably also want the user to be able to define

compileTimeAnalyzeModule :: Code -> IO()

which gets the entire contents of the module for static analysis.  What about static analysis across many modules?

Template Haskell can probably do most of this.

No comments :