The Expression Problem & Lenses • Tony Morris • YOW! 2016

Learn how to solve the Expression Problem using lenses and type classes in Haskell. See real examples of parsing formats, chess patterns & GPS data using this approach.

Key takeaways
  • The Expression Problem deals with the challenge of adding new data constructors and functions to types without having to update all existing code

  • Lenses provide a solution by allowing abstraction over data types and their views/modifications while maintaining type safety

  • Using type classes with parametric polymorphism (P and F parameters) enables writing functions that work across multiple data types without mentioning concrete types

  • The approach trades some complexity (harder type errors, more complex signatures) for greater flexibility and code reuse

  • Prisms are specialized lenses for dealing with data types that may or may not contain a value (like Either or Maybe types)

  • The technique allows parsing different versions of data formats (like Java class files 1.5/1.7) without changing reference functions

  • Coming up with good identifier names remains one of the hardest programming challenges

  • Type classes help reduce boilerplate by allowing a single implementation to work across multiple data types

  • The solution isn’t perfect - there are still tradeoffs between adding constructors and modifying functions

  • Real-world applications include parsing complex data formats, detecting chess cheating patterns, and working with GPS/geodetic data