3 ms·
intersect :: Segment -> Segment -> Maybe Segment > What would a Nothing result mean? Would it mean that the 2 segments do not intersect, or would it mean that
by the_af 11y ago
intersect :: Segment -> Segment -> Maybe Segment
> What would a Nothing result mean? Would it mean that the 2 segments do not intersect, or would it mean that one of the segments was invalid?
The way I see it, it can only mean that they do not intersect, and never that one of the segments was invalid. This is because a Segment is guaranteed to be valid by construction; after all, that's precisely the property you bought by using mkSegment!
- bad_user 11y agoYes but by using mkSegment in the implementation of intersect, you have to treat its Nothing result, even though it can never happen and so you either end up with this reflected in the signature of intersect itself, or you cheat in the implementation of intersect by assuming that Nothing will never happen, which would defeat the purpose of mkSegment.
- the_af 11y agoIt's a bit cumbersome, yes, but I don't think it defeats the purpose of mkSegment. Let's see: because mkSegment is the only way to construct a Segment, you know the arguments to intersect are valid. So there cannot be any doubt about the meaning of Maybe in the result; it simply cannot refer to invalid arguments. There is only one other possible meaning: that the segments don't intersect, which is the idiomatic use of Maybe (another reason why I wouldn't use Maybe in this case to signal an invalid argument: "which argument?"). Yes, this means that even if there is an intersection, mkSegment forces you to handle the None case, even though you know by construction this will never happen. It's annoying but nothing too terrible. I don't see how this is cheating; you are simply using a property that the type system is unable to catch... but this property exists nonetheless.