What you're doing when you code: talking about the nature of programming (a) state machine

Source: Internet
Author: User

What you're doing when you code: talking about the nature of programming (a) state machine

This semester studied two interesting lessons, theory of computation and distributed System, a low a high total of two levels of the branch, but unexpectedly found the two theoretical overlap-that is the state machine. In theory of computation, the DFA, NFA, Turing machine are very classic state machine, and in the distributed system, the Global state is determined, The consistency agreement of the replicated state machine and other places, again see its figure.

So I found the Turing award Daniel Lamport's paper, "Computation and state Machines", tells the Almighty state machine. Here is an excerpt from a few places in this note that touch on the nature of programming, the classic sentence:

1. Think Better, Code Better

"I believe that's the best-of-the-better programs is-teach programmers how-to think better. thinking is isn't the ability to manipulate language; it's the ability to manipulate concepts. Computer science should is about concepts, not languages . But what does one teach concepts without getting distracted by the language in which those concepts is expressed? My answer is to use the same language as every other branch of science and Engineering-namely, Mathematics. "

The obsession with language are a strong obstacle to any attempt at unifying different parts of computer. When the one thinks only in terms of language, linguistic differences obscure fundamental similarities. Simple ideas can become complicated when they must is expressed in a particular language. "

Lamport thinks that to get programmers to write better code is not to teach them how to use the language every day, but how to think better, and that thinking is not the ability to use programming languages, but the ability to manipulate concepts . Computer science should only be about concepts, not specific languages. But how can we focus on the concept rather than the language used to express the concept? Lamport The answer is: Use the science and engineering disciplines of the common language-mathematics !

the difference of specific language hinders the similarity of our thinking essence , and hinders the unification of various branches of computer science, and the simple idea becomes complicated. This point of view I very much agree! In the past, personal love of concrete algorithm, but this semester after learning Distributed system, began to learn to appreciate the beauty of abstraction, such as learning programming is the most common pseudo-code. Here is a small subset of pseudo-code for the distributed election bully algorithm:






CLRs also has a lot of very beautiful pseudo-code can be referenced.

Pseudocode:show your idea

About learning how to write or appreciate pseudocode, individuals have a hands-on approach: find a less complex implementation code, like Java, and try to translate the code into beautiful pseudo-code , in contrast to the textbook examples. Here's a brief look at the difference between pseudo-code and implementation code, and where a person feels pseudo-code is beautiful:

    • omit "minutiae": Pseudo-code omits assert or parameter preconditions check, log print, error capture and processing, and implementation details like List/set/array format and conversion. The word "minutiae" is inaccurate, because these are all important aspects of engineering implementation, so right vote add a quote.
    • Simple and elegant unity : not previously found, pseudo-code in some things very close to mathematics, so inherited the simple elegance of mathematics and unified characteristics. such as collection elements add Delete, intersection set, empty, and so on. In addition, the pseudo-code to the global data enumeration and description is very clear, it is worth learning!
2. Everyday Math

"Much of computer science was about the state machines. This was as obvious a remark as saying that much of physics was about equations. "
"State machines provide a framework for much of computer science. They can described and manipulated with ordinary, everyday mathematics-that are, with sets, functions, and simple Logi C. State machines therefore provide a uniform-to-describe computation with simple mathematics. "

Most of the computer science is about state machine, which is just as obvious as we say that physics is mostly mathematical equations. But why can a state machine be the basis of such a vast computer science? That's because state machine can describe--sets, functions, and logic--in the simplest and most common mathematics . Where the collection represents alphabet (both input domain), the function represents the transition table, the logic is the basis of its correctness, for example, the following is a classic Turing machine (a kind of state machines) Definition:



3. state machine = Computing Object

"Computing objects-objects that compute. A computation is a
Sequence of steps, which I call a behavior. When a computation was a state behavior (

Ask youself:what is computation? What is you doing?

What exactly is a computation? In fact, "Introduction to Theory of computation" in the more clear: "A Turing machine computes a function by starting with the input to the F Unction on the tape and halting with the output of the function on the tape. A function is computable function if some Turing
Machine M, on every input w, halts with just F (w) in its tape. " If a Turing machine always accepts the input w and produces the output F (w), we say that the Turing machines are calculating F, and the process of running (a sequence of steps, actions, or state transitions + actions) is calculated . and F (w) is the result of the calculation that it produces each time according to the different input W. So it can be seen that, in fact, this is a more formal definition of the calculation with Lamport said is one thing.

Examples:state/state-action Machine

Lamport lists some of the classic examples of state machine:

    • automata: The first half of theory of computation in this semester is all learning automata, from DFA, NFA, Regular Expression, CFL, to the most famous Turing machine. Some have an internal state, and some have an extra action plus a stack. Among them, the state of Turing is the most complex, with internal state, head position, tape content and so on.
    • von Neumann Computer: Von Neumann is closer to our current computer, and its state is also an extension of the Turing machine, with all registers and memory contents, instruction register PC pointing position, etc.
    • algorithm: The usual definition of an algorithm is: Like a recipe, a series of steps to produce a behavior. Now, does this definition look much like the definition of computating Object? The most surprising is that distributed algorithms can also be described using state machine.

"It would be a absurd trivialization to say that Turing machines and distributed algorithms is the same because they are State machines, just as it would is absurd to say that relativity and quantum mechanics is the same because they use equ ations. "

Lamport also reminds readers that while many computer science problems can be represented by state machines, this does not mean that they have much in common or even identical. This is like, because both relativity and quantum physics use mathematical equations, we say that they are a thing of the same absurdity . This is a wonderful metaphor! Don't hold a hammer and look like a nail!

4. state Tree = computation = Operational semantics

The

is preceded by the concepts and relationships of Computing object and computation, and Computing object generates computation through a series of steps/behaviors if the behavior is stateful that Computing Object can be represented by state machine. What is the visual representation of that computation? The answer is another pillar of programming,--tree.

"One alternate definition of a computation is a state tree, which is a tree whose nodes be labeled by S Tates. A state tree describes the tree of possible executions from the root's state. A state machine generates every state tree for which (i) the root node was in the set of initial states and (ii) a node lab Eled with all state s have a child labeled with State T iff HS, TI are in the next-state relation (transition function). "

For a C program, the state would describe what program variables is currently defined, their values, the Conten TS of the heap and of the program's call stack, the current control point, and so on. Specifying how to translate any legal C program into a state machine essentially means giving an operational semantics to The language. Writing an operational semantics are the only practical method of formally specifying the meaning of a arbitrary program W Ritten in a language as complicated as C. "

Each node of the tree represents a state, and the root node is the initial status. Each edge is a transition in transition table, depending on whether the state or state-action machine, side can have an additional Action. The process of translating the program into a bit state machine gives the program operational semantics, which is the most practical way to give semantics to any complex program. for C, Java programs, the state is a local variable, heap, call stack, execution location, and so on . So, the recursion tree that we'll see later is the stepwise recursive decomposition that uses the arguments to represent the problem, which is actually the computation process of the state machine that calculates the problem.

At this point, we introduce the first important concept of programming nature--state machine, and take it as the center of the circle, which outlines almost all the concepts listed in this article: Set, Function, Logic, tree, etc. The following is an introduction to each one, thus giving readers a broad picture of the nature of programming.

What you're doing when you code: talking about the nature of programming (a) state machine

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.