I'm using OpenUnion a type from world-peace library and I'd like to write a function which will transform an union into a narrower one if possible:
unionShrink :: Contains bs as => OpenUnion as -> Maybe (OpenUnion bs)
Contains bs as typeclass means bs ⊂ as. Union represents one value of the type in the list in the type parameter, hence Maybe, which means that the first union may contain a value which is of the type which is not in the second (narrower) union.
How to do that?
I feel like there should be an easier way and I'm not very proud of the names, but here's one way to do it:
It works fine for simple monomorphic examples:
But I doubt it will work very well when polymorphism is involved.