Skip to content

Instantly share code, notes, and snippets.

@dfithian
Last active January 25, 2019 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfithian/7d2b208fbc808a44379ceb0ebab09db6 to your computer and use it in GitHub Desktop.
Save dfithian/7d2b208fbc808a44379ceb0ebab09db6 to your computer and use it in GitHub Desktop.

Title

Template Haskell is for Haters - Of Boilerplate

Format

Hop Workshop (2h)

Audience

Intermediate

Elevator Pitch

We all hate boilerplate code. Having to restate slightly different incantations of similar code over and over again is not only mind numbing, it's prone to error. Use Template Haskell to kill boilerplate with fire.

Description

Everyone can agree that writing boilerplate code sucks. The options available in the Haskell ecosystem to reduce boilerplate are billed as "advanced" features - Scrap Your Boilerplate, Generic programming, Template Haskell metaprogramming. However, a simple application of any of these solutions can go a long way to improving code quality, reducing bugs, and increasing developer happiness. So why do they have to be advanced features?

Template Haskell is more approachable than it appears, and this workshop will empower attendees to use it to their advantage. In this workshop, we'll cover the "why" and "how" of basic applications of TH. Over the course of two hours, attendees will get familiar with TH functions, identify some boilerplate code to refactor, rewrite it using vanilla TH and QuasiQuoters, and write tests. By the end, attendees will have written something they can be proud of and be armed with tools for diving deeper into the world of Template Haskell.

NOTE This talk expects the attendee to have used Haskell before, be used to working in the type system, and understand concepts like monads.

Notes

Format

This talk will be divided into lecture sessions and exercises.

Lecture topics

The introduction will center around the tools we'll need for the workshop, what exactly Template Haskell is, what the Q monad is, and how can we use it? Exercises will include a simple "Hello World" written in TH, failing in Q, and printing the output of a TH splice.

After getting familiar with the base Template Haskell tools, we'll jump into the larger exercise. Lecture will cover identifying some boilerplate code, and introduce some basic combinators for TH needed for the exercise. The exercises will cover rewriting the boilerplate code identified in TH.

After rewriting the boilerplate code, the next section will introduce QuasiQuoters. QuasiQuoters are a way to "lift" regular Haskell code into Template Haskell's Q monad, so their purpose is to simplify the TH we have already written. The exercises will simplify the TH just written in the previous exercise to use QuasiQuoters.

The final section will cover testing. I want to leave a lot of time for this because it's the most important. Nothing's worse than writing an untestable macro! Lecture will cover the elements of successful tests and writing some tests. Exercises will be to wire in the TH splices, iterate, and fix bugs.

Motivation

Simply put, writing boilerplate code makes me very grumpy. For years I went about writing boring instances because I was afraid of using Template Haskell. Since I decided to start learning to use Template Haskell, my team's use of Template Haskell has empowered us to focus more on the real code that drives our company and industry forward. I want to share that knowledge with others, because the more efficient a team or company is in Haskell the better it is for the ecosystem as a whole.

Template Haskell is a means to greater productivity, because it takes care of mindless, error-prone work, and lets developers focus on writing code crucial to the business. It shouldn't be scary.

Tags

  • Haskell
  • Template Haskell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment