Programming language type system

Write freshman do not understand anything when something, mark it

1 Introduction

Type system is adopted as a means and type checking, from FORTRAN compiler in the 1950's began. Research programming language type system (type theory) point of view, in software engineering, programming language design, compiler and high-performance network security and other aspects of important applications using type theory. Check out this introductory courses and lessons by studying computer science at data, the authors deepened the understanding of the concept of programming language type system, and its role and problems have a certain understanding. The resulting study will now finishing this report.
S

2. type system (type system)

2.1 Definitions

Type, usually considered to be a set of possible set of values. Such as integers, which may be an integer value of the collection; the word has a connotation system go into some difficulties. We discuss the programming language in this field, reference to "type system", a reasonable explanation for: a set of basic configuration of the type "set primitive types"; and a series defined on the "basic set of types" composition, operation, conversion method.
We can get type system defined as follows:
type of programming language part, which consists of a set of rules setting (typing rule), the set of rules used to give various program constructs (variables, expressions, functions, etc.) is assigned
Example 1: "If M and N are the expression of type long, the long type is M + N expression" non-setting rule described in the form of

Example 2: If the function f is a parameter of a long type, the corresponding argument type should also be long. If the argument is the corresponding char, short, and int type, the system will automatically upgrade them as long. [1]

Type system definition See also:

A type system is a collection of rules that assign a property called type to various constructs a computer program consists of, such as variables, expressions, functions or modules.[2]

2.2 Role

We know that the computer is stored in a binary manner and in a continuous eight bits as a basic unit - "byte." This view, computer storage is generic, store text, images, sounds or other other media have no intrinsic nature of the difference.
The reality is that we artificially introduced a series of very different "basic types" in the programming language concepts, such as the C language type int and double. In a sense, int and double itself does not make any difference, are but a memory cell composed of only several bytes.
That why we want to set the "complex" type system? In fact, on the "Store" concept, we use binary mode, in bytes, to achieve the storage of information, is sufficient. However, we note that, further to say or computer programming language, its role mainly two, namely, information storage and processing of information. It turns out that the theoretical study type (type theory), partition type system, will bring great convenience for information processing.

A type system is a tractable syntactic method for proving the absence of certain program behaviors by classifying phrases according to the kinds of values they compute.[3]

The fundamental purpose of a type system is to prevent the occurrence of execution errors during the running of a program.[4]

In other words, the fundamental purpose of the system is the use of type design type examination (described below) way to ensure that due process is running at the right time behavior is good.
Typed programming language in addition to contribute to type checking, earlier errors are found also another program modules may be compiled independently of each other, and result in more efficient space arrangement and access. We also see different types of data due to differences in calculation rules, there is the type of system is necessary.

And, combined with the actual meaning of the type, we can see, "type theory to solve the basic problems of the program has significance, but raises the question, that is a meaningful program is not the appropriate corresponding type, which also led to a richer demand type system. "[5]

2.3 Related Concepts

We first introduce some concepts discussed below will appear
trapped errors lead to erroneous termination of the program execution, as in addition to 0, Java out of bounds array access in
untrapped errors continue after an error, but may be arbitrary behavior. C as in buffer overflow, Jump to the wrong address
Forbidden Behaviors language design, you can define a set of forbidden behaviors it must include all untrapped errors, but may contain errors Trapped..
Well / ILL behaved: If the program execution impossible forbidden behaviors, was well behaved, otherwise ill behaved.
Understanding of the above concept, we can distinguish between programming languages.
A language all the programs are well behaved-- that is impossible forbidden behaviors, the language is strongly typed (strongly typed). Otherwise, weak type (weakly typed), such as C language buffer overflow, is trapped errors, i.e. belongs forbidden behaviors.
2 static type (statically typed): ill behaved programs rejected at compile time.
Dynamic type (dynamiclly typed): Behaviors denied running ILL, 
3. if the type is a part of the syntax of the language, it is explicit type (explicitly typed); if the compile time type inference is implicit type (implicity typed), such as ML and Haskell4.

Thus, we can classify the common programming languages, are classified as follows

无类型: 汇编
弱类型、静态类型 : C/C++
弱类型、动态类型检查: Perl/PHP
强类型、静态类型检查 :Java/C#
强类型、动态类型检查 :Python, Scheme
静态显式类型 :Java/C
静态隐式类型 :Ocaml, Haskell

4. The system of formal type

Type expression, such as:
int, intint, pointer (int)
setting rules, such as:

x:int|-x+1:int

Where | - left side is referred to as stereotypes typing environment assertion:

 |– M : int    |– N : int
 |– M + N : int 

Wherein the form

a < b    b < c
a < c

The formula we call inference rules.
According to understand, it can be seen as a stereotyped assertion inference rules built on stereotypes rules.

3. Check the type (Type checking)

Check the compiler program after including syntax checking, type the name of the variable name and function names to declare a reference check, type checks and other checks.
The type of inspection we are discussing here, its implementation dependent on the type of system. For example, if a type long array, m is the long type, the compiler will find m + "123" and the type of error has a + 3.5.
About the type of system, here defined to borrow from Wikipedia follows [6]:

The process of verifying and enforcing the constraints of types—type checking—may occur either at compile-time (a static check) or at run-time.

Check mode where the two mentioned are:
the Static type IS Checking The Process of Verifying The type of A Safety Analysis Program based ON apos text of Program A (Source code).
Specifically, Static type checking is not running the program under test itself, only by grammatical correctness, structure, process, interfaces and the like check or test program source code analysis. Static method by a program of the static characteristic analysis to identify gaps and suspicious, for example, the parameters do not match, inappropriate and branch nested loop nest, recursion is not allowed, the variable is not used, and a null pointer references suspicious calculation
dynamic type checking is the process of verifying the type safety of a program at runtime.
most security type (type-safe) language with some form of dynamic type is detected, even if they also have a static type checker, the reason is that many useful kinetic energy is very difficult to put into practice for static checking. For example, consider a program has defined two types A and B, and B is a subtype of A. At this point if we want to convert from a variable of type B type A, that this variable downcast, this only this variable is legitimate when the original is the B type. The contents of the variable names that refer to the address stored only when the program is running can be determined, therefore, the dynamic type checking in determining whether such an operation when security is a must.
By extension of the third type --Combining static and dynamic type checking. I.e., some programming languages while allowing two or more types of inspections.

4.c language type of defect

It was the only C language is more familiar with the language, so as an example.
C language static type having a relatively complete system, a new type can be produced by combining type. Type C including basic types and configurations of two types (pointer function can be viewed as constructed and type). But the C language, there are some defects can not be ignored on the type of system. According to the definition above, we introduce the type of system, when given, C language is a weakly typed language, which can lead to checks during compilation is a certain defective, it may cause security problems when running . The more common operation is unsafe pointer arithmetic and casts.
Examples include when we use in class again here [7]:

typedef struct{int m;float a[];} record;
	record p={2,{1.0,2.0}},q={3,{1.0,2.0,3.0}};
	int main(){
		p=q;
		printf(“%d %f %f %f\n”,p.n,p.a[0],p.a[1],p.a[2]);
		printf(“%d %f %f %f\n”,q.n,q.a[0],q.a[1],q.a[2]);
      return 0;
	}

For such a procedure, the GCC: (Ubuntu / Linaro 4.6.3-1 ubuntu5 ) occurs following results compiled 4.6.3:
3 1.000000 2.000000 3.000000
3432433534 1.000000 2.000000 3.000000
reason is that the destruction qn compiler allocates contiguous memory to p and q structures stored data, resulting in pa [1] and the address of the adjacent qn, performed after p = q, pa [2] occupied address qn, qn values stored is destroyed.
And, following is an example of a type conversion
/ C encounter a beginner Celsius conversion algorithm
f for the input temperature, c is the temperature of the conversion
/

main()
{
     float c,f;
     scanf("%f",&f);
     c=5*(f-32)/9;
}
//根据优先级问题,先算括号里面的,即f-32,由于f是float型,则f-32也是float型
//5和9都是int型,5*(f-32)就是int型与float的乘积,此时的int会自动转换为
//float型
//同理,再除9,最终c为float型。

However, in practice often occur a problem that some students will equation c = 5 * (f-32) / 9 is rewritten as 

c=5/9*(f-32)

Which is to use 5/9 and then take the stuff in brackets, so the results will be directly output to 0, 5 and 9 because both int type, because the int / int = in, so 5/9 = 0, 0 multiplied by any number of all 0. This results in an error.
Weakly typed language and strongly typed language and can not determine the merits. In practice, what language to use depending on demand. Many times the weakening of the type improves the flexibility of the language, when writing simple little application, use a weakly typed language can save a lot of code volume, higher development efficiency. For example, C language type system defects, such defects caused by the leak in the C security, but for efficiency considerations we allow this deficiency, so the C language has remained one of the most popular languages. As for the construction of large projects, using a strongly typed language specification may be more reliable than using a weak type.

Guess you like

Origin blog.csdn.net/saber_jk/article/details/92699816