Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is code introspection in haskell?

In the yesod book there is a paragraph:

Template Haskell is essentially Haskell which generates a Haskell Abstract Syntax Tree (AST).

There’s actually more power in TH than that, as it can actually introspect code. We don’t use these facilities in Yesod, however.

What does it mean to introspect code and what can you do with this feature?

like image 646
Qwertie Avatar asked Dec 25 '17 14:12

Qwertie


1 Answers

The Template Haskell code in Yesod is only used to generate code. In that sense, it's a strict replacement for boilerplate. Instead of using Template Haskell, we could write out manually a transformation from the route file syntax to code you should write by hand, and you could write the equivalent code yourself.

With introspection, you actually look at existing information the compiler has and make decisions. For example, you could search for all of the instances of the Show typeclass and create a String with that list. That kind of approach can be useful in some cases, such as auto-generating a set of tests. The comment in the book is just stating that Yesod never does this sort of thing.

like image 123
Michael Snoyman Avatar answered Sep 27 '22 21:09

Michael Snoyman