Solving the Expression Problem with Elixir

protocols
Tags: #<Tag:0x00007f8ea6213a68>

#1

Hello,

I have been thinking about how Elixir might help us solve the expression problem. There are some really good articles on how clojure does it with protocols, so I thought I’d give it a go:

The solution I came to is very un-idiomatic elixir. I’d be interested in hearing how others would approach the problem!


#2

I’m not sure if you watched this talk already, but it’s very relevant to what you’re doing.


#3

No I haven’t, it looks good. I understand protocols well already though. Just interested to hear other’s thoughts


#4

Not heard of it referred to as the ‘Expression Problem’, but that is a good example as to why both pure functional and OOP are very bad at solving it, protocols are not really better than OOP here, what would work best is multiple polymorphic dispatch (rehashing a bit of the article to set up context here), like CLOS (Lisps’s OOP system) supports, where you can dynamically dispatch on more than one argument, which is easily solved via pattern matching in Elixir (but requires horrible dispatch trees and such for OOP). Here’s an example (typed in post so probably errors somewhere) using my ProtocolEx library (which builds matchers at compile-time, think of it as a significantly more powerful version of the built-in Protocols):

import ProtocolEx
defprotocol_ex Overlap, as: shapes do
  def overlap?(shape, other_shape), do: overlap?({shape, other_shape})
  def overlap?(shapes)
end


defimpl_ex SquareRect, {%schemal{}, %schemar{}} when schemal in [Square, Rect] and schemar in [Square, Rect], for: Overlaps do
  def overlap?({%Square{}, %Square{}}), do: :test_if_squares_overlap
  def overlap?({%Rect{}, %Rect{}}), do: :test_if_rects_overlap
  def overlap?({%Rect{}, %Square{}}), do: :test_if_rect_overlaps_square
  def overlap?({%Square{}, %Rect{}}), do: :test_if_square_overlaps_rect
end

defimpl_ex CircleRect, {%schemal{}, %schemar{}} when schemal in [Circle, Rect] and schemar in [Circle, Rect], for: Overlaps do
  def overlap?({%Circle{}, %Circle{}}), do: :test_if_circles_overlap
  def overlap?({%Rect{}, %Circle{}}), do: :test_if_rect_overlaps_circle
  def overlap?({%Circle{}, %Rect{}}), do: :test_if_circle_overlaps_rect
end

Or whatever, there’s many ways of doing it, and you can split each case of overlap into it’s own module, or put them all together, or define defaults, or control ordering, or let dependencies of this add in their own things for their own types, or use tagged tuples instead of structs, or whatever. :slight_smile:


#5

The example in the article I wrote is achieving polymorphic dispatch using protocols, so protocols are much better than OOP for solving this specific problem.

Pattern matching in elixir doesn’t get you everything you need to solve the expression problem. You also need to be able to extend the patterns that you can match on, dynamically. Meaning the patterns you match on need to be extensible. The only way you can achieve this is with protocols AFAIKT


#6

Yes, hence why I was speaking of my ProtocolEx library, which is like Elixir’s Protocols except it works based on matchers thus you can actually multi-dispatch instead of single-dispatch. :wink: