Actions as data by Alejandro Serrano

November 9, 2022

In the Actions as Data presentation by Alejandro Serrano, he discusses the concept of modeling actions as algebraic data types in functional programming using the Pokemon Trading Card Game as an example. Alejandro introduces the idea of encoding data from the cards, such as name, HP, and attacks, as algebraic data types or case classes. He also explores the concept of sealed hierarchies and algebraic data types to represent closed lists of variants. Alejandro then introduces the concept of interpreting actions as data and controlling their outcomes for testing purposes using Haskell’s higher-kind types and generalized algebraic data types (GADTs). Alejandro also discusses the use of functions with no parameters, or thunks, to delay evaluation and the concept of the operational Monad in Haskell to abstract and recursively define actions as programs. Throughout the presentation, Alejandro emphasizes the benefits of using functional programming concepts to model complex systems, such as the Pokemon Trading Card Game.

Actions as data: A comprehensive overview

Setting the stage: The basics of Functional Programming

Alejandro Serrano began by laying the groundwork, ensuring that the audience was familiar with fundamental concepts such as data classes and algebraic data types (ADTs). These constructs form the backbone of many functional programming languages, allowing developers to model data and behavior concisely and effectively. By introducing these concepts early on, Alejandro prepared the audience for a deeper exploration into the abstraction of actions within a functional programming context.

 

Modeling actions with data: A dive into the Pokemon TCG

To make his concepts more relatable, Alejandro used the popular Pokemon Trading Card Game (TCG) as a domain for modeling actions. He explained how the complex rules of the game could be translated into algebraic data types or case classes in languages like Kotlin. For example, he showed how a Pokemon card’s attributes, such as its name, HP (Health Points), and attacks, can be encoded using these data types, making the rules of the game programmable and testable.

Alejandro then moved on to more abstract concepts, such as modeling actions like “damage” or “coin flips” as algebraic data types. This approach allows developers to represent game mechanics — such as performing an attack or flipping a coin — with clear, type-safe code that can be easily understood and tested.

 

Syntax, algebra, and interpretations: Making sense of actions

One of the key ideas Alejandro introduced was the distinction between syntax and algebra in the context of actions. The “syntax” refers to the rules governing how actions are defined and structured, while the “algebra” represents the underlying logic that interprets these actions. This separation allows for multiple interpretations of the same action syntax, enabling different behaviors depending on the context.

For instance, flipping a coin in the game could be modeled as a “flipCoin” action with two possible outcomes — heads or tails. Each outcome would lead to a different subsequent action, showcasing how the same syntax can yield varied results based on the interpretation.

 

Testing and recursion: Handling complexity with Functional Programming

As the presentation progressed, Alejandro addressed the challenge of testing actions that involve randomness, such as coin flips. He introduced the idea of controlling outcomes for testing purposes, allowing developers to write unit tests that specify the expected result of an action, even when randomness is involved.

Alejandro also discussed how to handle more complex actions using recursion. For example, he explored how to model a sequence of actions that depend on the outcome of previous actions, such as accumulating damage from multiple coin flips. However, he cautioned against the potential pitfalls of infinite loops in recursive functions, suggesting techniques like delayed evaluation to mitigate such risks.

 

Expanding the pattern: From damage to smart contracts

Moving beyond the Pokemon TCG, Serrano demonstrated how the patterns he discussed could be generalized to other domains, such as smart contracts in blockchain technology. By abstracting the concept of “actions” to include different types of outcomes — not just damage values — developers can apply these ideas to a wide range of applications, from games to financial systems.

 

The Power of Haskell: Leveraging Advanced Type Systems

Alejandro’s presentation culminated in a discussion of Haskell’s powerful type system, which supports higher-kind types and generalized algebraic data types (GADTs). These features allow for even greater abstraction and flexibility in modeling actions, while maintaining strong type safety. He highlighted how Haskell’s lazy evaluation and support for data records with functions inside enable developers to write concise, imperative-style code that remains purely functional.

 

Conclusion: A deep dive into Functional Programming

Alejandro Serrano’s presentation on Actions as Data offered a thought-provoking exploration of how functional programming techniques can be applied to model complex behaviors in a type-safe, testable manner. By using the familiar domain of the Pokemon Trading Card Game, Serrano made these abstract concepts accessible and demonstrated their practical applications in both games and real-world systems like blockchain.

For those interested in exploring these ideas further, Alejandro’s work, including his book Functional Programming Ideas for the Curious Kotliner, provides a deeper dive into the world of functional programming and its potential to revolutionize how we think about and write code.

 

Additional resources

Check out more from the MeetUp Func Prog Sweden. Func Prog Sweden is the community for anyone interested in functional programming. At the MeetUps the community explore different functional languages like Erlang, Elixir, Haskell, Scala, Clojure, OCaml, F# and more.