A good friend has been trolling me recently with a CTF-style
treasure hunt, and some of the clues have been obscured behind classic
shift ciphers. This has given me a chance to play with some of my
favourite combinators from lens,
as well as mess around with finite-typelits,
which I’ve wanted to do for some time. The implementation of the ciphers
turned out to be quite elegant, so I thought I’d write them up.
I recently finished adding support for idempotency tokens to
amazonka (the unofficial Haskell AWS SDK), using the client
token support provided by AWS. Implementing this required making
some fairly substantial changes to the generator, the bespoke Haskell
program that turns the service definitions in botocore into
cabal packages. amazonka is over a decade old, and the
generator has evolved as AWS have added new styles of APIs for their
different services. Some services return XML, some return JSON, some
return raw bytes, some return additional information in HTTP response
headers, and so on. As a result, the generator’s logic had been extended
each time and had grown gnarly.
To untangle things, I resolved to make one of my favourite programming moves, “make the plan a value” or “decide what to do, and then do it”. This move decouples two parts of a program by introducing an intermediate data structure (a “plan”, or a value of a “plan type”) describing what is to be done before any action is taken.
I really like this pattern because the “deciding what to do” part of
a program is often more complicated than the “doing” part. Separating
the two often turns the “deciding” part into a pure function that
produces a plan, making test cases simpler to write. And then if the
“doing” phase is also complicated (because of remote API calls,
rollbacks, parallel execution, or whatever else), it’s more manageable
because those complications are not entangled with the planning logic.
You also get to add a --dry-run flag almost for free, which
is really
useful for testing. (Henrik observes that testing for a
dryRun variable everywhere pollutes the code somewhat. I
think if he split his code into a planning phase and consumed that plan
with either a “dry run” printer or a “real” executor, the inline checks
that he dislikes would disappear.)
This technique isn’t subtle or obscure. I’ve seen it called the plan-execute pattern, and the “routing slip” enterprise integration pattern is literally about centralising the decision logic before execution. I’ve previously called it a “tiny DSL”, and Matt Parsons sells its benefits in his Three Layer Haskell Cake article:
Often times, a simple non-recursive sum type “command” suffices as an interface between a pure function and an effectful one. A list of commands adds a lot of flexibility without dramatically complicating things.
Having resolved to introduce a plan type, how do we work it into a
venerable program like the Amazonka generator? For this part of the
generator, it means separating the analysis of the loaded service
definitions from the generation of the instance AWSRequest
declarations. I did this by introducing a Config record of
simple data and a function
instanceD :: Config -> Decl () that builds Haskell code
from that config record.