This Is Not a Monad Tutorial by John Azariah

December 7, 2022

In the presentation This Is Not a Monad Tutorial, John Azariah shares his experiences learning functional programming (FP) and the challenges of communicating FP concepts to other programmers. He criticizes the common practice of using tuples for error handling in Go and proposes using a choice type instead. John plans to approach FP by discussing a common problem and finding a solution, then introducing advanced concepts. He also discusses techniques for handling values and errors in code, the importance of context in programming, and the impact of language familiarity on the use of certain features.

This Is Not a Monad Tutorial: A comprehensive overview

Functional programming (FP) can often seem like a labyrinth of unfamiliar concepts and terminologies, even to seasoned developers. John Azariah, a principal engineer at Microsoft, delves into this topic in his presentation, This Is Not a Monad Tutorial. Here, he shares his journey of understanding FP, the challenges he faced, and practical insights for fellow programmers.

 

The challenges of learning FP

John begins by recounting his own experiences with FP, where despite decades of programming experience, he felt like a novice due to the steep learning curve. This resonates with many developers who find FP intimidating due to its unique terminology and paradigms.

Currently writing Go code at Microsoft, John criticizes the common practice in Go of using tuples for returning values, arguing that a choice type, which allows returning either a value or an error, would be more appropriate. This critique sets the stage for his discussion on FP principles and error handling.

 

Addressing error handling in FP

One major issue John highlights is error checking in functional programming. Using a code snippet as an example, he explains how error checks can be bypassed, leading to runtime issues. He proposes making error checks mandatory and reducing code clutter by using a choice type, which forces compliance and improves error handling.

 

Simplifying code with lambdas and syntactic sugar

John introduces a technique to handle values and errors in Go by wrapping values and adding a method that executes a Lambda function with the unwrapped value. This approach reduces the need for pattern matching and declutters code. However, it requires modifying function calls and converting them into Lambdas, which can be mechanically intensive but ensures error checking and function composition with constant stack space.

He acknowledges that while this approach is effective, the resulting code can be complex and unreadable. To address this, he suggests using syntactic sugar. In F#, for example, computational expressions with bind and return methods improve readability. Similarly, C#’s link feature offers a way to simplify call chains and unwrap values.

 

Enforcing consistent error checking

John’s approach involves wrapping all return values and preventing direct access to them, ensuring clean and consistent error checking. This method abstracts away error handling clutter and is implemented using the SelectMany method and extension methods. While not the only solution, it effectively removes boilerplate code and streamlines error handling.

 

Contextual execution and universal FP concepts

John explains how enriching the context in which a Lambda function runs can add functionalities like logging without altering the executed code. This concept of context management is universal in FP, where continuations form a sequence of instructions without passing context, making it applicable across different FP languages.

 

Perception and usage of language features

The speaker touches on how programmers’ familiarity with a language can shape their perception and use of certain features, such as syntactic sugar. For example, Go programmers might view features like the Elvis operator differently due to Go’s lack of syntactic sugar capabilities. Understanding these perspectives is crucial for effectively communicating the importance and utility of such features.

 

Transforming code with functional concepts

John discusses transforming Go’s null context into a more functional approach using lambdas and composition. This transformation, while not a monad tutorial, emphasizes composition within a context and simplifies boilerplate code, making the codebase less cluttered.

 

John’s journey with FP and monads

John shares his background, starting with C# and J# on the .NET CLR, and his introduction to FP in college. He later worked on Scala and delved into applicators, functors, and monads. At Microsoft, he applied FP concepts to quantum programming, encoding instructions using a free monad and trampoline.

Despite his deep understanding, John found it challenging to explain monads to colleagues lacking FP knowledge. He aims to introduce FP concepts more gently by focusing on composition in context before tackling monads, acknowledging the complexities involved.

 

Conclusion

John’s presentation offers valuable insights for developers navigating the complexities of FP, emphasizing practical approaches and gradual learning to make FP concepts more accessible.

John Azariah’s journey through FP, his critique of current practices, and his practical solutions offer a relatable and insightful perspective for developers looking to embrace functional programming.

 

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.