6 ms·
Option type is just an algebraic data type. datatype 'a option = None | Some of 'a
by throwaway487549 8y ago
Option type is just an algebraic data type.
datatype 'a option = None | Some of 'a
- gizmo686 8y agoWhich is awfully similar to Haskell's Maybe. Ignoring boilerplate and comments, the entire module is just [0]: data Maybe a = Nothing | Just a deriving ( Eq, Ord ) And the deriving line isn't really nessasary. The Monad module is larger and contains code specific to Maybe [1]: instance Monad Maybe where (Just x) >>= k = k x Nothing >>= _ = Nothing (>>) = (*>) fail _ = Nothing (Only the first 2 lines are needed. The 3rd is an optimization, the 4th is a historical wart that will be removed in future versions (moved to MonadFail) [0] https://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Maybe.html#Maybe https://hackage.haskell.org/package/base-4.12.0.0/docs/src/G... [1] https://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Base.html#line-854 https://hackage.haskell.org/package/base-4.12.0.0/docs/src/G...
- throwaway487549 8y agoAnd this is meaningful only in the context of a lazy language. In the context of strict, statically typed language an algebraic Option type is good-enough.
- gizmo686 8y agoMaybe is an algebraic Option type. It just happens to implement an interface that abstracts away the if(None) checks that you would be doing anyway. Lazyness has nothing to do with it.
- cultus 8y agoThe option monad is literally isomorphic to maybe. It 100% follows the definition of a monad and is an instance of the monad type class in Cats and Scalaz. Many algebraic data types are monads. I’m not sure why you think that is a contradiction.
- throwaway487549 8y agoThis isomorphism has no meaning. It is also isomorphic to logical OR, if you wish. Shall we wrap OR into a Monad?
- kreetx 8y agoI think you're overthinking it. The isomorphism simply exists. And you don't wrap things into a monad, you just write an instance for it -- it has no effect on the data type itself, it simply allows to use the data type in monadic contexts.
- AnaniasAnanas 8y agoI think you mean of Either. Either is isomorphic to logical OR in type theory.
- smadge 8y agoYes, and that algebraic data type you defined is a monad, whether you acknowledge and use that fact or not.