Monad simplest Introduction

Source: Internet
Author: User
Monad simplest Introduction

Haskell is a very unique programming language, even in functional languages. It is famous for its pure functions and powerful type systems. Monad is the first introduced to the programming world by Haskell. It can be counted as the most difficult concept in programming so far. Almost all those who have tried their best to understand monad have a sudden epiphany. They can't help but write an article to share with you what monad he understands, I am no exception.

  • 1. Why monad?
  • 2 monad Principle
  • 3. What is monad?
1. Why monad?

Why does Haskell introduce monad? The biggest reason is to introduce side effects in Pure Functional Languages.

Pure functions are safe and reliable. Function output depends entirely on the input, and there is no implicit dependency. Its existence is as perfect as a mathematical formula. However, the more perfect the thing is, the less useful it is, and the same is true for pure functions. Because it cannot depend on the external environment, the pure function cannot connect to the basic input and output. A simple hello world can be a pure function. In order to introduce Io operations, various function languages are different. Monad is the solution provided by Haskell. Furthermore, monad is not only an abstraction of Io operations, but also a common abstraction between a variety of similar operations. So
The problems solved by monad are not limited to I/O. For example, maybe and [] in Haskell are both monad. In Haskell, the beautiful error handling method and the flexible list deduction formula are also contributions of monad.

Monad is not the only method to introduce Io. Even monad does not introduce the side effects to pure functions. Pure functions do not have side effects. They are not pure functions, even if they use mysterious and hard-to-solve monad. So what exactly does monad do?

Let's look back: Pure functions are safe and reliable, but useless. They are a boring good man. Common functions are powerful but have many bugs, which are essential to programs. It's like a dangerous and attractive bad boy. How can we have the two at the same time, so that they can work together and make full use of their own characteristics without fighting? This is the role of monad. It introduces Io operations with side effects into Haskell in a controllable way, so that pure functions and IO operations can get along with each other and work together to organize secure and useful programs.

2 monad Principle

To collaborate between functions, they must interact in various forms. Haskell adopts a static and strong type system, which limits the connection between functions to the input and return value types. This greatly enhances the program security and brings the following problems: how can we fully isolate pure functions and side-effects functions and make them reusable?

We use Io operations for example analysis. Haskell has a dedicated type class (similar to the concept proposal in C ++). "Io" is used to indicate that a type has an additional external Io action.

To fully isolate pure functions and IO functions, Haskell cannot implement Io char-> Char. The input is an IO type, but the return value is a common type function. Otherwise, the side-effect function can use this function to "unpack" the IO and convert it into a pure function.

Char -> Char = (Char-> IO Char) . (IO Char -> Char)

In fact, once there is Io in the parameter, the return value must be Io, which ensures full isolation.

So how can I reuse pure functions and I/O functions? This depends on the return and> = functions defined in Io monad. Return (not a keyword in Haskell, but a common function name) is used to increase (or bind) the value A of A type as a in the most natural way) the value of Io a is Char-> Io char. The so-called "most natural" method is strictly defined and will be seen later. With this function, the pure function can become the return value by combining with return.
I/O functions with side effects. Of course, the actual I/O actions are built in Haskell. The main significance of return is to increase the type.

With the improvement, there is no descent operation. How can we combine putchar: Char-> io () and getchar: Io Char. Getchar reads one character from Io, and putchar writes the character to Io. However, getchar returns the IO char type, while putchar requires the normal char type. What should I do if they do not match? > = Function is required to connect these two functions.> = Type is

IO a -> (a -> IO b) -> IO b

In this way, >=can connect getchar and putchar, and re-write the input to the output.

echo = >>= getChar . putChar

As you can see,> = the operation is actually a restricted type drop (or unboxing) operation. The operation is performed only when the return value of the function is also the IO type. In this way, pure functions and side-effects functions are fully isolated, and functions can be reused.

There is a controllable channel through return and> = two parallel "worlds" (categories. The following figure intuitively reflects the role of monad. A and Io a belong to two different worlds, A is pure, and Io a is a type with external Io operations. In order to ensure the quality of the entire program, communication between the two worlds can only be performed in the form of an image.

  • Return is the most natural Type Lift function, which promotes a to'
  • A-> B 'is a common lifting function.
  • >>= Is a controlled type drop. You can only reduce B 'to B when B-> C' exists for function combination. In this way, a-> B 'and B-> C' can be combined into a-> C'.
  • Monad does not define C-> C', and there is no such uncontrolled descent method.

Here, the IO type class can be any type class that meets the monad definition. A' can be Io a, maybe a, or [a]. At the same time, Haskell's do syntax sugar further simplifies> = Composite syntax, making it a common solution for many similar problems, which will not be expanded here.

3. What is monad?

Monad is hard to understand because of its abstraction. This is different from the abstract of object-oriented concepts. The analogy of e is a bird. It is enough to understand the inheritance relationship between child class parent classes. Monad abstraction is a metaphysical high abstraction. It is a special operator in abstract algebra. To truly digest it, we must first understand the abstract concepts of objects, types, categories, and correspondence. Without these concepts, understanding monad can be a treasure in the air. If you need to understand monad separately, it is a good simplified image description.

As mentioned above, return is the most natural way to improve. Here the "most natural" is clear. The return and> = functions must satisfy the following principle.

  • Return and> = reciprocal operations
(return x) >>= f == f xm >>= return == m
  • >>= Meet the combination Law
(m >>= f) >>= g == m >>= (\x -> (f x >>= g))

These principles ensure that monad works normally as expected. Unfortunately, they cannot be checked by type systems in Haskell, which is a potential rule. Just as dynamic languages require a large number of tests to ensure code quality, static language also requires external tests to ensure that these hidden rules are not violated.

Monad is introduced into Haskell to solve Io operations, just as the concept of a face object can be simulated in a non-face object language, monad can also be implemented in other languages (such as Java, C ++, and Python ). However, the concept of monad can be used to its maximum effect only when Haskell is a strong type language with a powerful type system. Haskell may not be in the mainstream language for a day, but I believe monad will be like lambda and other concepts, from the function language's Wang XIE Family to the ordinary mainstream language.

The three steps for improving programming languages are abstraction, abstraction, and higher abstraction!


Reprinted from: Dao ke Dao | monad
Brief Introduction to http://zhuoqiang.me/a/what-is-monad

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.