Industry observers note that 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.
Recent findings suggest that 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. (Related: Introducing 🤗 Accelerate...)
In a recent development, 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.). (Related: Fine tuning CLIP with Remote Sensing (Satellite) images...)
This technique isnât subtle or obscure. Iâve seen it called the strategy-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:.
Sources indicate that 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.
Industry observers note that I found the generatorâs logic too difficult to understand and replace in a single change, so I replaced it incrementally using the strangler fig pattern. The most complicated part of this change was response parsing, so I added to the Config record an enumeration of ported response parsers, and a fall back to âdo things the old wayâ for everything else:.
One thing that makes Amazonkaâs generator rewarding to hack on is that the service bindings act as an extensive and automatic golden test suite. Any changes to the generated bindings show up in diffs, and so if there are no diffs and the generator doesnât crash, the change probably didnât break anything. This makes the process of replacing the entangled code relatively simple (not easy):.
The end result worked well and kept the individual commits readable (example: factoring out status code parsing). Adding the idempotency machinery was then straightforward.
Recent findings suggest that the data types in the Amazonka example represent parameters to a Haskell code template, but this refactoring also works for more expressive proposal types, such as âtiny DSLsâ, âlists of commandsâ, or âfree structuresâ. The trick is to keep a âdo it the old wayâ constructor in your plan type until the âold wayâ has been refactored away.
A well-chosen strategy type introduces a narrow waist that decouples producers and consumers of your plan type, and the most obvious wins come from introducing alternate consumers of the plan: things like testability, --dry-run, and simpler executors all follow from that. A less obvious benefit is that you can also analyse and transform the plan itself. This is a great place to add pre-execution sanity checks or access controls. I regret that my tiny DSLs post inverted a transformation by interpreting it as matrix multiplications and inverting the result; it would have been simpler to provide an invert :: [Transform] -> [Transform] function.
Introducing alternate producers of the initiative type can also pay off, but needs a bit more imagination:.
Sources indicate that âMaking the proposal a valueâ works well in any language, but Haskellâs laziness makes it especially nice. Lazy evaluation can avoid materialising the whole plan in memory if it is consumed right away. Even in strict languages, the decoupling benefits are great enough that the extra memory use is often worth it.