Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Why does Zip require Semialign
The Zip class from Data.Zip requires an implementation of Semialign:
class Semialign f => Zip f
In my mind:
-
Ziptakes the intersection of two shapes. -
Semialigntakes the union of two shapes.
For 2D matrices I can take the intersection of two matrices, but not the union. For example:
a :: Array (Int, Int) Int
a =
matrix
[[1,1,1]
,[1,1,1]
]
b :: Array (Int, Int) Int
b =
matrix
[[0,2]
,[2,0]
,[0,2]
]
I can zip these:
zipWith (+) a b =
[[1,3]
,[3,1]
]
but their union is not a rectangle, so I can't align them.
But implementing Zip on Array (Int, Int), requires first implementing Semialign.
Why is that? It seems like a totally extraneous requirement.
1 answer
There's good reason to believe this is simply historical accident. The Semialign class came first, and used to include zip and zipWith directly. When those members were separated out into their own class, the motivation was types that had align but not zip (one example is NEMap), so Zip got the superclass instead of Semialign. In retrospect, perhaps making the two classes independent and defining a laws-only subclass of both would have been better.

0 comment threads