Witchcraft — Lessons From Porting Monads to Elixir by Brooklyn Zelenka

June 14, 2024

In the presentation Witchcraft — Lessons From Porting Monads to Elixir, Brooklyn Zelenka shares her experience of porting monads to Elixir and the creation of the Witchcraft library suite. Brooklyn, an independent distributed systems researcher, discusses the challenges of adapting Haskell idioms to Elixir and the use of protocols, macros, and functions to overcome missing features. She reflects on the importance of understanding the concepts from both Elixir and its predecessor, Erlang, and making functional programming in Elixir feel natural and intuitive. The presentation also covers the implementation of monads in Elixir, the evolution of maintainership roles, and Brooklyn’s current interest in functional programming languages like Idris and Verse.

Witchcraft — Lessons From Porting Monads to Elixir: A comprehensive overview

The Witchcraft library: Making Elixir more functional

Brooklyn Zelenka’s journey into Elixir started around 2013, when she began exploring the language’s potential for functional programming. Although Elixir did not initially feel very functional to her, she saw an opportunity to introduce functional programming idioms that would appeal to Rubyists. This led to the creation of Witchcraft, a suite of libraries designed to integrate monads and other functional constructs into Elixir.

 

Porting challenges and solutions

One major theme of Brooklyn’s talk was the unexpected complexity of porting libraries from Ruby to Elixir. What she initially thought would be a straightforward task turned into a series of challenges due to the inherent differences between the two languages. Unlike Haskell, Elixir is heavily side-effectual and lacks equational reasoning, necessitating significant adjustments to the libraries to fit naturally within Elixir’s ecosystem.

 

Leveraging Elixir’s unique features

Despite these hurdles, Witchcraft successfully brought functional programming concepts into production use, including in a large bank’s trading platform. Brooklyn emphasized the importance of making these libraries feel native to Elixir, using the language’s unique features like the pipe operator to thread through arguments seamlessly.

 

Overcoming functional gaps

Brooklyn discussed how her team used protocols, macros, and functions to fill in the gaps left by Elixir’s lack of native support for monads. This involved creating a compos function and implementing features like arrows and fan-outs to express data flow operations more concisely. They leveraged macros extensively to ensure consistent mental models and interoperability with other libraries, making functional programming in Elixir both powerful and intuitive.

 

Educating the Community

Pedagogy was a critical component of the Witchcraft project. Brooklyn and her team focused on creating clear documentation and examples to teach functional programming concepts effectively. They developed foundational libraries like Quark to provide essential functional programming components and used macros for automatic partial applications and point-free programming.

 

The role of type classes and protocols

In Elixir, protocols function similarly to interfaces in other programming languages and were preferred over behaviors due to their closer resemblance to type classes. The team used macros to add laws and automatic testing to type classes, ensuring properties like associativity and commutativity. This innovative approach, though initially surprising, enabled rapid development and robust code.

 

Insights and reflections

Brooklyn shared several insights from her experience, including the importance of maintaining a consistent approach to functional programming constructs and the benefits of using type classes and protocols. She also reflected on the broader implications of her work, emphasizing the need for maintainers to onboard new contributors early and support their growth to ensure project sustainability.

 

Future directions and personal interests

Towards the end of her talk, Brooklyn touched on her current interests, including functional programming languages with dependent types, such as Idris, and her fascination with Verse, a new language developed by Simon Peyton Jones. She expressed excitement about the potential of these languages to make functional programming more accessible to a broader audience.

 

Conclusion

Brooklyn Zelenka’s presentation on Witchcraft — Lessons From Porting Monads to Elixir was a compelling exploration of the intersection between functional programming and Elixir. Her journey highlighted the challenges and rewards of integrating advanced functional concepts into a language known for its concurrency and pragmatism. By sharing her experiences and insights, Brooklyn not only showcased the power of Elixir and Witchcraft but also inspired developers to explore new paradigms and push the boundaries of what’s possible in 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.