In Haskell, I have a module with a partial order type:
data PartialOrder a = PartialOrder [a] [(a, a)]
I don't export the value constructor because that's not how I want the type to be used, but I still want to be able to pattern match the PartialOrder type outside the module; is this possible? In particular, I want to be able to pattern match something that is not the type constructor, but instead, to pattern match something like the following:
f (PartialOrder xs le) = ...
Where le
is a function implicitly defining the explicit ordering defined in the value constructor. I know such a facility is available in Scala, is there a way to do the same in Haskell?
Thanks in advance.
It sounds like a use case for
ViewPatterns
. You could write a type like:Write a function like:
then use the
ViewPatterns
extension to write code like this:Of course, you should come up with better names for these things :P!
There's no way to have the same effect implicitly (ie without the
viewOrder
function), for better or worse. I think it's usually a good thing, making it clear that you're not matching the actual implementation of the type.