Skip to content

Instantly share code, notes, and snippets.

@inamiy
Last active October 17, 2021 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save inamiy/7eba7bc0152ca736b6b0ccaf595f6de1 to your computer and use it in GitHub Desktop.
Save inamiy/7eba7bc0152ca736b6b0ccaf595f6de1 to your computer and use it in GitHub Desktop.
What is monad? answered by OpenAI

I, as a programmer, asked professor: What is a monad?


He replied: A monad is an old russian towel, so the monad might have evolved into a soprano, but a soprano is a woman who sings on the stage.

I was confused by this answer, I did not know what a monad is. But as he explained that a monad is an old russian towel, I noticed that there was an obvious connection between the word monad and the word monastery, where people lived in seclusion, and meditated on the topic of the meaning of life.

I did not have an idea what a monad is. So I went to the internet to look for the meaning of the word "monad". I had an impression that "monad" is the highest abstraction of "bead". Beads were the reason that motivated us to invent knots, but later on we learned how to use monads to program.

(I was searching for "definition of monad", the search result was "the meaning of life", which was rather funny. We should not search for "the meaning of life" in the internet, because we will not find anything, but the internet will get smarter.)

As I mentioned, the word "monad" has different meanings, the russian towel and the highest abstraction of the word "bead". It is not clear which meaning the professor was referring to, but from what I had seen on the internet, "monad" was a hard concept. When I read the definition of "monad", I felt that I still did not understand what a monad is.

The definition of "monad" is very simple, it is one type, with one method. But there is a subtle point, a monad needs to have an identity. (It is the same as the difference between a 0 and a 1.)

I feel that it is hard to understand the meaning of a monad, and it is hard to write a monad. But now we already know how to use monads, and we can write functional programming programs with monads. (By the way, if you understand what a monad is, you will not write in a functional way, because functional programming is to remove side effects. When you understand monads, you will not write monadic programs, because a monad has a side effect, which is to hide data.)

The word "monad" has a different meaning in different languages. In C#, a monad is a delegate, in Python, a monad is a context manager, in Java, a monad is an object, in C++, a monad is a RAII. In Swift, a monad is an enum.

There is another interpretation of the word "monad". When we are programming, we need to understand the things that have happened before. We do not need to use the side effect to alter data, we just need to think about what has happened before. I use the word "monad" to refer to these "understanding" things.

For example, an object has an identity, so we can use this object as a monad. In the following code, we are not mutating the elements of an array, but we are using them as a monad to describe the history of what has happened.

Because the concept of monad is hard to understand, so I like to explain what a monad is by examples.

Suppose we want to implement the operation of a window, that is to say, the user clicks the button on the left of the window, the window should move to the right.

The following code is a basic implementation of the operation of a window. (The first time I saw this code, I thought this code was buggy, and the second time I saw this code, I also thought this code was buggy. I later learned that the code was correct, it was just that I did not understand what it did.)

void MoveWindowLeft(string title) { string oldTitle = this.title; this.title = title; this.moveTo(title,100); }

We can easily implement the operation of moving a window to the left by adding a method "MoveWindowLeft" to the window. But this code is buggy, because we can move the window to the left forever. To solve this problem, we need to make a distinction between a window and the operation of moving a window.

Suppose we introduce an abstract class "Window", and create two kinds of operations for a window, "MoveLeft" and "MoveRight".

[abstract] class Window { [abstract] virtual void MoveLeft() = 0; [abstract] virtual void MoveRight() = 0; } class WindowLeft : Window { override void MoveLeft() { } } class WindowRight : Window { override void MoveRight() { } }

This is better, we can now distinguish a window from the operation of moving a window. But now we cannot use a window as a monad. A window has an identity, so a window should be used as a value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment