In other words, a Monad in the context of CS or PL is the solution to a problem which does not arise in Scala.
And, of course, an option type does not have to be a Maybe Monad, like any other Either Of type.
In other words, a Monad in the context of CS or PL is the solution to a problem which does not arise in Scala.
And, of course, an option type does not have to be a Maybe Monad, like any other Either Of type.
I thought the value of an option type was being able to “program the semicolon” to do automatic null checks for you.
datatype 'a option = None | Some of 'a 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/G...
[1] https://hackage.haskell.org/package/base-4.12.0.0/docs/src/G...
In the context of strict, statically typed language an algebraic Option type is good-enough.
Many algebraic data types are monads. I’m not sure why you think that is a contradiction.
https://stackoverflow.com/a/50173415 has some good examples.
It's close, though. Really close. And don't get me wrong - I still love promises and async / await. They're much better than what we had before.
More clearly, Promise indeed forms a functor, if the domain is the set of Javascript functions which do not return 'Promise<X>'.
The only point of a Monad is that in a lazy language it desugars into an implicit nested functions, which guarantees the order of evaluation.
The monadic code, then, could be generalized over >>=, >> and return, and given that, specialized instances of a Monad class could be made, like Maybe, ST and what not.
The only meaning in performing these steps is to establish a guaranteed order of evaluation (via implicit nesting of functions) for serialized abstractions.
But that is ok, most of Javascript coders never studied CS fundamentals or comparative PL.
Please, do tell.
> But that is ok, most of Javascript coders never studied CS fundamentals or comparative PL.
Can you explain what you mean by this? You seem to be under the impression that javascript the language did something extra to make Promises form a monad when in reality, the very idea of a Promise forms a monad to begin with, independent of any particular implementation in javascript.
That is not true at all. You seem to confuse side-effect-free with lazy and the state monad with any monad. Monad instances like options or either data types allow for the simple composition of functions that yield them. In any language. Promises form a useful monad because there is no other way to talk about values that have not been evaluated yet.
There is a way, and it is known since at least R5RS.
https://www.gnu.org/software/mit-scheme/documentation/mit-sc...
To recapitulate some basics: JS is a strongly typed dynamic language.
Strongly typed implies no implicit coersions at runtime. Python is strongly typed. JS or PHP are weakly typed.
What's about this Python code:
1.0 + 1
Or this Scala Code: "1".toFloat + "1".toInt
In both cases that's clearly implicit type coercion at runtime.So according to the above constraint there would hardly be any strongly typed languages… (But quite the opposite is true in fact).
Weakly typed means you can hose the type-system somehow and then perform some "illegal" or completely nonsensical operation. For example that's easy in C, but not possible in JS. In a strongly typed language any value has some precise type at any time. This is true for JS so it's strongly typed.