How to design a programming language (iv) what is a pit (operation template)

Source: Internet
Author: User
Tags goto

In fact, when I wrote the third article in this series, it was found that the farther away the machine, the higher the abstraction, the less the number of pits. But this is not to say that the closer the machine is to the concept, the stronger it is, or the closer it is to nature. This is a large number of programmers on the computational theory of a misunderstanding. Most people understand the structure of the knowledge of the programming, it is to use reductionism to understand, this method is not wrong in fact. But the problem is that the "restore" approach is not unique. A lot of people think, anyway your high level of language compiling finished is nothing but machine code. But there is another explanation, you no matter how low-level language compiled is nothing but a CPS transform (continuation passing style) of the Λ-calculus program. They are equivalent, not only in ability but also in "essence".

A λ-calculus program that has been completely processed with a CPS transformation is much like a bunch of instructions. and similar to C + + inline operation, here is completely natural, safe, easy to do. Why is it that our machines don't invent this? Obviously it doesn't matter how we want to write a program. It is this conflict that gives us the wrong intuition that "the farther the distance the machine runs, the slower it is." Even if you're using a functional language, say Haskell or F #, you're running a completely compiled machine code at the end of the run. These languages do not need "simulators" at all, although for a variety of historical reasons people first developed the simulator. Of course a well-designed C program must still be faster than Haskell, but I think there are not many people to do so, and most of the time it is a waste of the boss's money, because your program does not need to be so fast. It's the same thing as the idea of doing the internet for testing--is there a bug? Find out again, first release Rob Market.

If you know anything about this, CPS transforms-that is, lost in stupid Parentheses-er's favorite call-with-current-continuation, his other name is call/cc-- is an approach that is as powerful as a goto and a basic control flow. Goto and CPS can be converted to each other, and all other control flows can be converted to Goto and CPS. They are comparable in this respect. And since a program that is fully processed with the CPS transformation looks like a bunch of instructions, what's the difference? The difference is that CPS can be strongly typed, while Goto is never possible.

As a last piece of nonsense, I give a small example of what is called "a λ-calculus program that has been completely processed with a CPS transformation is a lot like a bunch of instructions." Let's use an expression such as a (b (x), C (x)) to say:
Before processing:

A (b x) (C x)

After processing:

b xλa0.
A a0λa1.
C XΛA2.
A1 A2

With our familiar to can not be familiar with the Haskell of the Monad of the method to translate is actually:

A0 <-b (x)
A1 <-A (A0)
A2 <-C (x)
Return (A1 (A2))

Well, as to how the above form (which looks like a SSA) is made into machine code, let's see how it works. So much nonsense above is to express a conclusion: Abstract does not mean that the burden. Of course, as for the programmer's IQ requirements, for some people is also a burden, I have no way, so I do not consider him.

=============== Nonsense End ================

Templates are also a kind of abstraction like this. Why I want to write the title "pit", just want to unify with the front, in fact, to the template so high-level abstraction, basically there is no pit. Of course, C + + practice is another matter, and I think those pits you probably will not touch for a lifetime. Let's start with a simple one.

Something simpler than a template is naturally generic. Why do you call him generics? Because generics are actually a way to copy code, it has no ability to derive, so it's definitely not a template. But in most cases, such a weak abstraction of generics is almost enough. There are about three methods associated with generics.

The first is to define a function that returns a class (where the parameter is T):

Class array<t>
{
public Array (int count);
public int count{get;}
Public T this[int index]{get; set;}
}

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.