Why does Maybe include Just?

前端 未结 4 1647
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-07 17:42

Thanks to some excellent answers here, I generally understand (clearly in a limited way) the purpose of Haskell\'s Maybe and that its definition is



        
4条回答
  •  既然无缘
    2021-01-07 18:24

    Maybe a is designed so to have one more value than the type a. In type theory, sometimes it is written as 1 + a (up to iso), which makes that fact even more evident.

    As an experiment, consider the type Maybe (Maybe Bool). Here we have 1 + 1 + 2 values, namely:

    Nothing
    Just Nothing
    Just (Just False)
    Just (Just True)
    

    If we were allowed to define

    data Maybe a = Nothing | a
    

    we would lose the distinction between the cases Just Nothing and Nothing, since there is no longer Just to make them apart. Indeed, Maybe (Maybe a) would collapse into Maybe a. This would be an inconvenient special case.

提交回复
热议问题