There is a lot of hoopla and obfuscation in the functional programming world about monads. "A monad is a monoid in the category of endofunctors." Well, that certainly helps, doesn't it?
So, what actually is a monad? A monad is a data type, either a struct or a tuple, that combines some base data type with a boolean flag (okay, something, valid, etc.), along with functions/procedures that use this structure. At a minimum, you need a function to convert the raw data to the data+bool structure ("constructor"), a function that takes data+bool and extracts the raw data ("unit"), and a function that takes data+bool, applies some other monadic function to the raw data, and then returns data+bool ("bind").
So, if you're converting a string into a monadic string, you need the following:
type: (String, Bool)
constructor: String --> (String, Bool)
unit: (String, Bool) --> String
bind: ((String, Bool), Proc(String --> (String, Bool)) --> (String, Bool)
In actual practice, the data structure guard can be either a boolean or an enum. When an enum is used, the "true" value is usually called something like Okay or Just, and the "false" value is called Nothing. The data structure then becomes a typed variant (tagged union) with either the base type if Okay/Just, or null if Nothing. Or, in languages where every variable is a pointer to data, "Nothing" is a null pointer, and "Just" is a valid pointer. This is equivalent to the (data, bool) tuple where you have the two cases of ("some string value", true) and ("", false).
In Listack, every piece of data is monadic, because it comes with an "invalid" flag, which is false by default.
No comments:
Post a Comment
I reserve the right to remove egregiously profane or abusive comments, spam, and anything else that really annoys me. Feel free to agree or disagree, but let's keep this reasonably civil.