鬼一般找什么人压床| 什么水没有鱼| 什么海没有鱼| 籍贯是什么意思| ko是什么意思啊| 什么什么桑田| 拉肚子什么原因造成的| 什么人容易得骨肿瘤| 身强力壮是什么生肖| 十月十六号是什么星座| 扁桃体发炎可以吃什么水果| 鱼不能和什么一起吃| 栩是什么意思| 三个耳读什么| 做腋臭手术挂什么科室| 女性去泰国要注意什么| 不动明王是什么意思| poison是什么意思| 身上痣多是什么原因| 玉米淀粉可以用什么代替| 冰丝纤维是什么面料| 南明为什么打不过清朝| 什么是钓鱼网站| 湿气重喝什么茶| 梦见跟妈妈吵架是什么意思| 血压低什么症状| 手指肿胀什么原因| 保税仓是什么意思| 泰州有什么好玩的地方| 二十四节气分别是什么| 屁特别臭是什么原因| 硅胶是什么材质| prp是什么意思| 浆果是什么| 未属什么五行| 舌尖发麻是什么问题| 什么是痤疮| b是什么牌子的衣服| 导诊是干什么的| 人为什么会得阑尾炎| 女人脸肿是什么原因引起的| 三班倒是什么意思| 脚气挂什么科| 围绝经期吃什么药调理| 秋天穿什么衣服| 佳什么意思| 他达拉非是什么药| 什么麻料最好| 婚检查什么| 抗核抗体是什么| 护理专业是什么| 黄风怪是什么动物| 11.23是什么星座| 什么动物吃蚊子| 肌酸什么时候喝| 三黄鸡是什么鸡| 一热就头疼是什么原因| gg是什么品牌| 褪黑素有什么用| 白带多是什么情况| mcn是什么意思| 辗转是什么意思| 淀粉样变是什么病| 1.4什么星座| 芝五行属什么| 命大是什么意思| 挚肘是什么意思| 1975年是什么年| 长脸适合什么眼镜框| 什么食物含蛋白高| 什么水果降血糖| 太极贵人是什么意思| 琥珀酱是什么味| 2r是什么意思| 什么叫湿热| 生殖器是什么| 胆管结石用什么药能把它除掉| 心肌缺血有什么症状| 绿色配什么颜色| 四柱是什么意思| 十月7号是什么星座| 拉肚子能吃什么食物| 硬度不够吃什么好| 英姿的动物是什么生肖| 1996属鼠的是什么命| 升白细胞的针剂叫什么| 12388是什么电话| 呦西是什么意思| 黄连泡水喝有什么功效| 大忌什么意思| 男人血精是什么原因造成的| 孕妇血糖高吃什么| 经方是什么意思| 过敏是什么样的图片| 颞颌关节紊乱挂什么科| 鼻子毛白了是什么原因| 什么叫中出| 腔调是什么意思| 什么水果糖分最低| 散仙是什么意思| 蚊子不喜欢什么味道| 什么叫直系亲属| 鸡子是什么东西| 国外微信叫什么| 重阳节送老人什么礼物| median什么意思| pd是什么意思| 小别胜新婚是什么意思| mophie是什么牌子| 女性尿道出血是什么原因引起的| 团粉是什么| 中度贫血是什么原因造成的| 血小板为什么会高| 一千年前是什么朝代| 肺气虚吃什么食物| 盐冻虾是什么意思| 如何知道自己是什么星座| 生辰八字指的是什么| 校长是什么级别| 脚底麻是什么原因| 焦点是什么| 夏的五行属什么| 翘楚是什么意思| 晚上7点到9点是什么时辰| 双侧骶髂关节致密性骨炎是什么病| 精子是什么颜色的| 拔完牙吃什么药| 刑冲破害是什么意思| eft是什么意思| 饮料喝多了有什么危害| 水平是什么意思| 很轴是什么意思| 喝牛奶什么时候喝最好| 去湿气吃什么药| 阙是什么意思| 老过敏是缺什么维生素| 什么人不能吃火龙果| 浅黄色是什么颜色| 正缘是什么| 江团鱼是什么鱼| 五指毛桃是什么| 什么啊| 独美是什么意思| 尿崩症是什么意思| 眼睛近视缺什么维生素| 什么是红肉| 搪塞什么意思| 什么欢快| 比五行属什么| 藿香正气水什么人不能喝| 过敏性鼻炎吃什么药好| 肛门周围痒是什么病| 口牙是什么意思| 缺铁性贫血吃什么补得快| 脚底板黄是什么原因| 明年是什么年| 平起平坐代表什么生肖| 疱疹性咽峡炎是什么引起的| 血小板是干什么用的| 什么时候去西藏旅游最好| 寄生茶在什么树上最好| 紫外线过敏用什么药膏| 什么时候闰正月| 拉绿粑粑是什么原因| 生意盎然什么意思| 桑寄生是什么| 什么炒肉| 怀才不遇什么意思| 对宫星座是什么意思| 感冒吃什么水果比较好| 吃什么促进排便| 融合菜是什么意思| 什么花最大| 本科一批和本科二批有什么区别| 纹眉需要注意什么| 惜字如金什么意思| 与众不同是什么意思| 天蝎座女生配什么星座| 什么是湿疹| kangol是什么牌子| 自我感动是什么意思| 男人肾虚吃什么最补| 什么病不能吃松花粉| 北京的区长是什么级别| 黄鼻涕是什么类型的感冒| 痤疮用什么药膏最有效| 天津副市长什么级别| 色泽是什么意思| 做背有什么好处及作用| 牛蒡根泡水喝有什么好处| 荨麻疹忌口什么食物| 凌晨两点多是什么时辰| 脾肾阳虚吃什么药| 肾结石能吃什么水果| 鸡枞是什么| 眼睛长结石是什么原因引起的| 萨瓦迪卡什么意思| oa期刊是什么意思| hcg高代表什么| 酿酒用什么菌| 三是什么意思| 一个月来两次例假是什么原因| 春风得意是什么生肖| 一晚上尿五六次是什么原因| 吃什么助于睡眠| 奢靡是什么意思| 男人说冷静一段时间是什么意思| 什么样的孕妇容易翻盘| 喝菊花茶有什么好处| 生气对身体有什么危害| 酸中毒是什么意思| 蹲马步有什么好处| 肚子疼是什么病| 话说多了声音嘶哑是什么原因| 胆的作用及功能是什么| 怀孕建档是什么意思| 梦见捡到很多钱是什么意思| 怀孕什么时候吃鹅蛋最好| 萎靡是什么意思| 什么蔬菜| 淋巴细胞绝对值偏低是什么意思| 双氧水又叫什么名字| 墨绿色是什么颜色| 这个季节吃什么水果最好| 阿胶适合什么人吃| 什么四海| 什么是虚荣心| 柚子什么时候成熟| bigbang什么意思| 石榴花是什么颜色| 见红的血是什么颜色| 3月26日是什么节日| 立竿见影是什么意思| 红曲米是什么东西| 连锁反应是什么意思| 风疹吃什么药好得快| 刘邦是什么星座| 炒菜用什么油最健康| 产妇刚生完孩子适合吃什么| 胸痹是什么意思| 矿物质是什么| 焦糖色配什么颜色好看| 28.88红包代表什么意思| 信的拼音是什么| 脚心起水泡是什么病症| 7月23号是什么星座| 玉米热量高为什么还减肥| 省长是什么级别干部| horns是什么意思| 男人忽冷忽热说明什么| 胰腺炎恢复期吃什么好| 珠地棉是什么面料| 炖鸡肉放什么调料| 龋牙是什么意思| 汪峰是什么星座| 中国的国服是什么服装| 手癣用什么药膏| 金屋藏娇定富贵是什么生肖| 文曲星是什么神仙| 22点是什么时辰| 孕妇喉咙痛吃什么好得最快| 舌加氏念什么| 香蕉是什么季节的| 黄瓜是什么科| 百度Jump to content

哈尔滨市16家开发区被纳入国家“目录” 黑龙江省纳入数量在东北居首

From Wikipedia, the free encyclopedia
(Redirected from Type equivalence)
百度 教育的事情从来没有等一等的说辞,错过了,就是错过了;错过了,就是一代人的代价。

In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a type (for example, integer, floating point, string) to every term (a word, phrase, or other set of symbols). Usually the terms are various language constructs of a computer program, such as variables, expressions, functions, or modules.[1] A type system dictates the operations that can be performed on a term. For variables, the type system determines the allowed values of that term.

Type systems formalize and enforce the otherwise implicit categories the programmer uses for algebraic data types, data structures, or other data types, such as "string", "array of float", "function returning boolean".

Type systems are often specified as part of programming languages and built into interpreters and compilers, although the type system of a language can be extended by optional tools that perform added checks using the language's original type syntax and grammar.

The main purpose of a type system in a programming language is to reduce possibilities for bugs in computer programs due to type errors.[2] The given type system in question determines what constitutes a type error, but in general, the aim is to prevent operations expecting a certain kind of value from being used with values of which that operation does not make sense (validity errors).

Type systems allow defining interfaces between different parts of a computer program, and then checking that the parts have been connected in a consistent way. This checking can happen statically (at compile time), dynamically (at run time), or as a combination of both.

Type systems have other purposes as well, such as expressing business rules, enabling certain compiler optimizations, allowing for multiple dispatch, and providing a form of documentation.

Usage overview

[edit]

An example of a simple type system is that of the C language. The portions of a C program are the function definitions. One function is invoked by another function.

The interface of a function states the name of the function and a list of parameters that are passed to the function's code. The code of an invoking function states the name of the invoked, along with the names of variables that hold values to pass to it.

During a computer program's execution, the values are placed into temporary storage, then execution jumps to the code of the invoked function. The invoked function's code accesses the values and makes use of them.

If the instructions inside the function are written with the assumption of receiving an integer value, but the calling code passed a floating-point value, then the wrong result will be computed by the invoked function.

The C compiler checks the types of the arguments passed to a function when it is called against the types of the parameters declared in the function's definition. If the types do not match, the compiler throws a compile-time error or warning.

A compiler may also use the static type of a value to optimize the storage it needs and the choice of algorithms for operations on the value. In many C compilers the float data type, for example, is represented in 32 bits, in accord with the IEEE specification for single-precision floating point numbers. They will thus use floating-point-specific microprocessor operations on those values (floating-point addition, multiplication, etc.).

The depth of type constraints and the manner of their evaluation affect the typing of the language. A programming language may further associate an operation with various resolutions for each type, in the case of type polymorphism. Type theory is the study of type systems. The concrete types of some programming languages, such as integers and strings, depend on practical issues of computer architecture, compiler implementation, and language design.

Fundamentals

[edit]

Formally, type theory studies type systems. A programming language must have the opportunity to type check using the type system whether at compile time or runtime, manually annotated or automatically inferred. As Mark Manasse concisely put it:[3]

The fundamental problem addressed by a type theory is to ensure that programs have meaning. The fundamental problem caused by a type theory is that meaningful programs may not have meanings ascribed to them. The quest for richer type systems results from this tension.

Assigning a data type, termed typing, gives meaning to a sequence of bits such as a value in memory or some object such as a variable. The hardware of a general purpose computer is unable to discriminate between for example a memory address and an instruction code, or between a character, an integer, or a floating-point number, because it makes no intrinsic distinction between any of the possible values that a sequence of bits might mean.[note 1] Associating a sequence of bits with a type conveys that meaning to the programmable hardware to form a symbolic system composed of that hardware and some program.

A program associates each value with at least one specific type, but it also can occur that one value is associated with many subtypes. Other entities, such as objects, modules, communication channels, and dependencies can become associated with a type. Even a type can become associated with a type. An implementation of a type system could in theory associate identifications called data type (a type of a value), class (a type of an object), and kind (a type of a type, or metatype). These are the abstractions that typing can go through, on a hierarchy of levels contained in a system.

When a programming language evolves a more elaborate type system, it gains a more finely grained rule set than basic type checking, but this comes at a price when the type inferences (and other properties) become undecidable, and when more attention must be paid by the programmer to annotate code or to consider computer-related operations and functioning. It is challenging to find a sufficiently expressive type system that satisfies all programming practices in a type safe manner.

A programming language compiler can also implement a dependent type or an effect system, which enables even more program specifications to be verified by a type checker. Beyond simple value-type pairs, a virtual "region" of code is associated with an "effect" component describing what is being done with what, and enabling for example to "throw" an error report. Thus the symbolic system may be a type and effect system, which endows it with more safety checking than type checking alone.

Whether automated by the compiler or specified by a programmer, a type system renders program behavior illegal if it falls outside the type-system rules. Advantages provided by programmer-specified type systems include:

  • Abstraction (or modularity) – Types enable programmers to think at a higher level than the bit or byte, not bothering with low-level implementation. For example, programmers can begin to think of a string as a set of character values instead of as an array of bytes. Higher still, types enable programmers to think about and express interfaces between two of any-sized subsystems. This enables more levels of localization so that the definitions required for interoperability of the subsystems remain consistent when those two subsystems communicate.
  • Documentation – In more expressive type systems, types can serve as a form of documentation clarifying the intent of the programmer. For example, if a programmer declares a function as returning a timestamp type, this documents the function when the timestamp type can be explicitly declared deeper in the code to be an integer type.

Advantages provided by compiler-specified type systems include:

  • Optimization – Static type-checking may provide useful compile-time information. For example, if a type requires that a value must align in memory at a multiple of four bytes, the compiler may be able to use more efficient machine instructions.
  • Safety – A type system enables the compiler to detect meaningless or invalid code. For example, we can identify an expression 3 / "Hello, World" as invalid, when the rules do not specify how to divide an integer by a string. Strong typing offers more safety, but cannot guarantee complete type safety.

Type errors

[edit]

A type error occurs when an operation receives a different type of data than it expected.[4] For example, a type error would happen if a line of code divides two integers, and is passed a string of letters instead of an integer.[4] It is an unintended condition[note 2] which might manifest in multiple stages of a program's development. Thus a facility for detection of the error is needed in the type system. In some languages, such as Haskell, for which type inference is automated, lint might be available to its compiler to aid in the detection of error.

Type safety contributes to program correctness, but might only guarantee correctness at the cost of making the type checking itself an undecidable problem (as in the halting problem). In a type system with automated type checking, a program may prove to run incorrectly yet produce no compiler errors. Division by zero is an unsafe and incorrect operation, but a type checker which only runs at compile time does not scan for division by zero in most languages; that division would surface as a runtime error. To prove the absence of these defects, other kinds of formal methods, collectively known as program analyses, are in common use. Alternatively, a sufficiently expressive type system, such as in dependently typed languages, can prevent these kinds of errors (for example, expressing the type of non-zero numbers). In addition, software testing is an empirical method for finding errors that such a type checker would not detect.

Type checking

[edit]

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

If a language specification requires its typing rules strongly, more or less allowing only those automatic type conversions that do not lose information, one can refer to the process as strongly typed; if not, as weakly typed.

The terms are not usually used in a strict sense.

Static type checking

[edit]

Static type checking is the process of verifying the type safety of a program based on analysis of a program's text (source code). If a program passes a static type checker, then the program is guaranteed to satisfy some set of type safety properties for all possible inputs.

Static type checking can be considered a limited form of program verification (see type safety), and in a type-safe language, can also be considered an optimization. If a compiler can prove that a program is well-typed, then it does not need to emit dynamic safety checks, allowing the resulting compiled binary to run faster and to be smaller.

Static type checking for Turing-complete languages is inherently conservative. That is, if a type system is both sound (meaning that it rejects all incorrect programs) and decidable (meaning that it is possible to write an algorithm that determines whether a program is well-typed), then it must be incomplete (meaning there are correct programs, which are also rejected, even though they do not encounter runtime errors).[7] For example, consider a program containing the code:

if <complex test> then <do something> else <signal that there is a type error>

Even if the expression <complex test> always evaluates to true at run-time, most type checkers will reject the program as ill-typed, because it is difficult (if not impossible) for a static analyzer to determine that the else branch will not be taken.[8] Consequently, a static type checker will quickly detect type errors in rarely used code paths. Without static type checking, even code coverage tests with 100% coverage may be unable to find such type errors. The tests may fail to detect such type errors, because the combination of all places where values are created and all places where a certain value is used must be taken into account.

A number of useful and common programming language features cannot be checked statically, such as downcasting. Thus, many languages will have both static and dynamic type checking; the static type checker verifies what it can, and dynamic checks verify the rest.

Many languages with static type checking provide a way to bypass the type checker. Some languages allow programmers to choose between static and dynamic type safety. For example, historically C# declares variables statically,[9]:?77,?Section 3.2? but C# 4.0 introduces the dynamic keyword, which is used to declare variables to be checked dynamically at runtime.[9]:?117,?Section 4.1? Other languages allow writing code that is not type-safe; for example, in C, programmers can freely cast a value between any two types that have the same size, effectively subverting the type concept.

Dynamic type checking and runtime type information

[edit]

Dynamic type checking is the process of verifying the type safety of a program at runtime. Implementations of dynamically type-checked languages generally associate each runtime object with a type tag (i.e., a reference to a type) containing its type information. This runtime type information (RTTI) can also be used to implement dynamic dispatch, late binding, downcasting, reflective programming (reflection), and similar features.

Most type-safe languages include some form of dynamic type checking, even if they also have a static type checker.[10] The reason for this is that many useful features or properties are difficult or impossible to verify statically. For example, suppose that a program defines two types, A and B, where B is a subtype of A. If the program tries to convert a value of type A to type B, which is known as downcasting, then the operation is legal only if the value being converted is actually a value of type B. Thus, a dynamic check is needed to verify that the operation is safe. This requirement is one of the criticisms of downcasting.

By definition, dynamic type checking may cause a program to fail at runtime. In some programming languages, it is possible to anticipate and recover from these failures. In others, type-checking errors are considered fatal.

Programming languages that include dynamic type checking but not static type checking are often called "dynamically typed programming languages".

Combining static and dynamic type checking

[edit]

Certain languages allow both static and dynamic typing. For example, Java and some other ostensibly statically typed languages support downcasting types to their subtypes, querying an object to discover its dynamic type and other type operations that depend on runtime type information. Another example is C++ RTTI. More generally, most programming languages include mechanisms for dispatching over different 'kinds' of data, such as disjoint unions, runtime polymorphism, and variant types. Even when not interacting with type annotations or type checking, such mechanisms are materially similar to dynamic typing implementations. See programming language for more discussion of the interactions between static and dynamic typing.

Objects in object-oriented languages are usually accessed by a reference whose static target type (or manifest type) is equal to either the object's run-time type (its latent type) or a supertype thereof. This is conformant with the Liskov substitution principle, which states that all operations performed on an instance of a given type can also be performed on an instance of a subtype. This concept is also known as subsumption or subtype polymorphism. In some languages subtypes may also possess covariant or contravariant return types and argument types respectively.

Certain languages, for example Clojure, Common Lisp, or Cython are dynamically type checked by default, but allow programs to opt into static type checking by providing optional annotations. One reason to use such hints would be to optimize the performance of critical sections of a program. This is formalized by gradual typing. The programming environment DrRacket, a pedagogic environment based on Lisp, and a precursor of the language Racket is also soft-typed.[11]

Conversely, as of version 4.0, the C# language provides a way to indicate that a variable should not be statically type checked. A variable whose type is dynamic will not be subject to static type checking. Instead, the program relies on runtime type information to determine how the variable may be used.[12][9]:?113–119?

In Rust, the dyn std::any::Any type provides dynamic typing of 'static types.[13]

Static and dynamic type checking in practice

[edit]

The choice between static and dynamic typing requires certain trade-offs.

Static typing can find type errors reliably at compile time, which increases the reliability of the delivered program. However, programmers disagree over how commonly type errors occur, resulting in further disagreements over the proportion of those bugs that are coded that would be caught by appropriately representing the designed types in code.[14][15] Static typing advocates[who?] believe programs are more reliable when they have been well type-checked, whereas dynamic-typing advocates[who?] point to distributed code that has proven reliable and to small bug databases.[citation needed] The value of static typing increases as the strength of the type system is increased. Advocates of dependent typing,[who?] implemented in languages such as Dependent ML and Epigram, have suggested that almost all bugs can be considered type errors, if the types used in a program are properly declared by the programmer or correctly inferred by the compiler.[16]

Static typing usually results in compiled code that executes faster. When the compiler knows the exact data types that are in use (which is necessary for static verification, either through declaration or inference) it can produce optimized machine code. Some dynamically typed languages such as Common Lisp allow optional type declarations for optimization for this reason.

By contrast, dynamic typing may allow compilers to run faster and interpreters to dynamically load new code, because changes to source code in dynamically typed languages may result in less checking to perform and less code to revisit.[clarification needed] This too may reduce the edit-compile-test-debug cycle.

Statically typed languages that lack type inference (such as C and Java prior to version 10) require that programmers declare the types that a method or function must use. This can serve as added program documentation, that is active and dynamic, instead of static. This allows a compiler to prevent it from drifting out of synchrony, and from being ignored by programmers. However, a language can be statically typed without requiring type declarations (examples include Haskell, Scala, OCaml, F#, Swift, and to a lesser extent C# and C++), so explicit type declaration is not a necessary requirement for static typing in all languages.

Dynamic typing allows constructs that some (simple) static type checking would reject as illegal. For example, eval functions, which execute arbitrary data as code, become possible. An eval function is possible with static typing, but requires advanced uses of algebraic data types. Further, dynamic typing better accommodates transitional code and prototyping, such as allowing a placeholder data structure (mock object) to be transparently used in place of a full data structure (usually for the purposes of experimentation and testing).

Dynamic typing typically allows duck typing (which enables easier code reuse). Many[specify] languages with static typing also feature duck typing or other mechanisms like generic programming that also enable easier code reuse.

Dynamic typing typically makes metaprogramming easier to use. For example, C++ templates are typically more cumbersome to write than the equivalent Ruby or Python code since C++ has stronger rules regarding type definitions (for both functions and variables). This forces a developer to write more boilerplate code for a template than a Python developer would need to. More advanced run-time constructs such as metaclasses and introspection are often harder to use in statically typed languages. In some languages, such features may also be used e.g. to generate new types and behaviors on the fly, based on run-time data. Such advanced constructs are often provided by dynamic programming languages; many of these are dynamically typed, although dynamic typing need not be related to dynamic programming languages.

Strong and weak type systems

[edit]

Languages are often colloquially referred to as strongly typed or weakly typed. In fact, there is no universally accepted definition of what these terms mean. In general, there are more precise terms to represent the differences between type systems that lead people to call them "strong" or "weak".

Type safety and memory safety

[edit]

A third way of categorizing the type system of a programming language is by the safety of typed operations and conversions. Computer scientists use the term type-safe language to describe languages that do not allow operations or conversions that violate the rules of the type system.

Computer scientists use the term memory-safe language (or just safe language) to describe languages that do not allow programs to access memory that has not been assigned for their use. For example, a memory-safe language will check array bounds, or else statically guarantee (i.e., at compile time before execution) that array accesses out of the array boundaries will cause compile-time and perhaps runtime errors.

Consider the following program of a language that is both type-safe and memory-safe:[17]

var x := 5;   
var y := "37"; 
var z := x + y;

In this example, the variable z will have the value 42. Although this may not be what the programmer anticipated, it is a well-defined result. If y were a different string, one that could not be converted to a number (e.g. "Hello World"), the result would be well-defined as well. Note that a program can be type-safe or memory-safe and still crash on an invalid operation. This is for languages where the type system is not sufficiently advanced to precisely specify the validity of operations on all possible operands. But if a program encounters an operation that is not type-safe, terminating the program is often the only option.

Now consider a similar example in C:

int x = 5;
char y[] = "37";
char* z = x + y;
printf("%c\n", *z);

In this example z will point to a memory address five characters beyond y, equivalent to three characters after the terminating zero character of the string pointed to by y. This is memory that the program is not expected to access. In C terms this is simply undefined behaviour and the program may do anything; with a simple compiler it might actually print whatever byte is stored after the string "37". As this example shows, C is not memory-safe. As arbitrary data was assumed to be a character, it is also not a type-safe language.

In general, type-safety and memory-safety go hand in hand. For example, a language that supports pointer arithmetic and number-to-pointer conversions (like C) is neither memory-safe nor type-safe, because it allows arbitrary memory to be accessed as if it were valid memory of any type.

Variable levels of type checking

[edit]

Some languages allow different levels of checking to apply to different regions of code. Examples include:

  • The use strict directive in JavaScript[18][19][20] and Perl applies stronger checking.
  • The declare(strict_types=1) in PHP[21] on a per-file basis allows only a variable of exact type of the type declaration will be accepted, or a TypeError will be thrown.
  • The Option Strict On in VB.NET allows the compiler to require a conversion between objects.

Additional tools such as lint and IBM Rational Purify can also be used to achieve a higher level of strictness.

Optional type systems

[edit]

It has been proposed, chiefly by Gilad Bracha, that the choice of type system be made independent of choice of language; that a type system should be a module that can be plugged into a language as needed. He believes this is advantageous, because what he calls mandatory type systems make languages less expressive and code more fragile.[22] The requirement that the type system does not affect the semantics of the language is difficult to fulfill.

Optional typing is related to, but distinct from, gradual typing. While both typing disciplines can be used to perform static analysis of code (static typing), optional type systems do not enforce type safety at runtime (dynamic typing).[22][23]

Polymorphism and types

[edit]

The term polymorphism refers to the ability of code (especially, functions or classes) to act on values of multiple types, or to the ability of different instances of the same data structure to contain elements of different types. Type systems that allow polymorphism generally do so in order to improve the potential for code re-use: in a language with polymorphism, programmers need only implement a data structure such as a list or an associative array once, rather than once for each type of element with which they plan to use it. For this reason computer scientists sometimes call the use of certain forms of polymorphism generic programming. The type-theoretic foundations of polymorphism are closely related to those of abstraction, modularity and (in some cases) subtyping.

Specialized type systems

[edit]

Many type systems have been created that are specialized for use in certain environments with certain types of data, or for out-of-band static program analysis. Frequently, these are based on ideas from formal type theory and are only available as part of prototype research systems.

The following table gives an overview over type theoretic concepts that are used in specialized type systems. The names M, N, O range over terms and the names range over types. The following notation will be used:

  • means that has type ;
  • is that application of on ;
  • (resp. ) describes the type which results from replacing all occurrences of the type variable α (resp. term variable x) in by the type σ (resp. term N).
Type notion Notation Meaning
Function If and , then .
Product If , then is a pair s.t. and .
Sum If , then is the first injection s.t. , or is the second injection s.t. .
Intersection If , then and .
Union If , then or .
Record If , then M has a member .
Polymorphic If , then for any type σ.
Existential If , then for some type σ.
Recursive If , then .
Dependent function[a] If and , then .
Dependent pair[b] If , then is a pair s.t. and .
Dependent intersection[24] If , then and .
Familial intersection[24] If , then for any term .
Familial union[24] If , then for some term .
  1. ^ Also referred to as dependent product type, since .
  2. ^ Also referred to as dependent sum type, since .

Dependent types

[edit]

Dependent types are based on the idea of using scalars or values to more precisely describe the type of some other value. For example, might be the type of a matrix. We can then define typing rules such as the following rule for matrix multiplication:

where k, m, n are arbitrary positive integer values. A variant of ML called Dependent ML has been created based on this type system, but because type checking for conventional dependent types is undecidable, not all programs using them can be type-checked without some kind of limits. Dependent ML limits the sort of equality it can decide to Presburger arithmetic.

Other languages such as Epigram make the value of all expressions in the language decidable so that type checking can be decidable. However, in general proof of decidability is undecidable, so many programs require hand-written annotations that may be very non-trivial. As this impedes the development process, many language implementations provide an easy way out in the form of an option to disable this condition. This, however, comes at the cost of making the type-checker run in an infinite loop when fed programs that do not type-check, causing the compilation to fail.

Linear types

[edit]

Linear types, based on the theory of linear logic, and closely related to uniqueness types, are types assigned to values having the property that they have one and only one reference to them at all times. These are valuable for describing large immutable values such as files, strings, and so on, because any operation that simultaneously destroys a linear object and creates a similar object (such as str = str + "a") can be optimized "under the hood" into an in-place mutation. Normally this is not possible, as such mutations could cause side effects on parts of the program holding other references to the object, violating referential transparency. They are also used in the prototype operating system Singularity for interprocess communication, statically ensuring that processes cannot share objects in shared memory in order to prevent race conditions. The Clean language (a Haskell-like language) uses this type system in order to gain a lot of speed (compared to performing a deep copy) while remaining safe.

Intersection types

[edit]

Intersection types are types describing values that belong to both of two other given types with overlapping value sets. For example, in most implementations of C the signed char has range -128 to 127 and the unsigned char has range 0 to 255, so the intersection type of these two types would have range 0 to 127. Such an intersection type could be safely passed into functions expecting either signed or unsigned chars, because it is compatible with both types.

Intersection types are useful for describing overloaded function types: for example, if "intint" is the type of functions taking an integer argument and returning an integer, and "floatfloat" is the type of functions taking a float argument and returning a float, then the intersection of these two types can be used to describe functions that do one or the other, based on what type of input they are given. Such a function could be passed into another function expecting an "intint" function safely; it simply would not use the "floatfloat" functionality.

In a subclassing hierarchy, the intersection of a type and an ancestor type (such as its parent) is the most derived type. The intersection of sibling types is empty.

The Forsythe language includes a general implementation of intersection types. A restricted form is refinement types.

Union types

[edit]

Union types are types describing values that belong to either of two types. For example, in C, the signed char has a -128 to 127 range, and the unsigned char has a 0 to 255 range, so the union of these two types would have an overall "virtual" range of -128 to 255 that may be used partially depending on which union member is accessed. Any function handling this union type would have to deal with integers in this complete range. More generally, the only valid operations on a union type are operations that are valid on both types being unioned. C's "union" concept is similar to union types, but is not typesafe, as it permits operations that are valid on either type, rather than both. Union types are important in program analysis, where they are used to represent symbolic values whose exact nature (e.g., value or type) is not known.

In a subclassing hierarchy, the union of a type and an ancestor type (such as its parent) is the ancestor type. The union of sibling types is a subtype of their common ancestor (that is, all operations permitted on their common ancestor are permitted on the union type, but they may also have other valid operations in common).

Existential types

[edit]

Existential types are frequently used in connection with record types to represent modules and abstract data types, due to their ability to separate implementation from interface. For example, the type "T = ?X { a: X; f: (X → int); }" describes a module interface that has a data member named a of type X and a function named f that takes a parameter of the same type X and returns an integer. This could be implemented in different ways; for example:

  • intT = { a: int; f: (int → int); }
  • floatT = { a: float; f: (float → int); }

These types are both subtypes of the more general existential type T and correspond to concrete implementation types, so any value of one of these types is a value of type T. Given a value "t" of type "T", we know that "t.f(t.a)" is well-typed, regardless of what the abstract type X is. This gives flexibility for choosing types suited to a particular implementation, while clients that use only values of the interface type—the existential type—are isolated from these choices.

In general it's impossible for the typechecker to infer which existential type a given module belongs to. In the above example intT { a: int; f: (int → int); } could also have the type ?X { a: X; f: (int → int); }. The simplest solution is to annotate every module with its intended type, e.g.:

  • intT = { a: int; f: (int → int); } as ?X { a: X; f: (X → int); }

Although abstract data types and modules had been implemented in programming languages for quite some time, it wasn't until 1988 that John C. Mitchell and Gordon Plotkin established the formal theory under the slogan: "Abstract [data] types have existential type".[25] The theory is a second-order typed lambda calculus similar to System F, but with existential instead of universal quantification.

Gradual typing

[edit]

In a type system with Gradual typing, variables may be assigned a type either at compile-time (which is static typing), or at run-time (which is dynamic typing).[26] This allows software developers to choose either type paradigm as appropriate, from within a single language.[26] Gradual typing uses a special type named dynamic to represent statically unknown types; gradual typing replaces the notion of type equality with a new relation called consistency that relates the dynamic type to every other type. The consistency relation is symmetric but not transitive.[27]

Explicit or implicit declaration and inference

[edit]

Many static type systems, such as those of C and Java, require type declarations: the programmer must explicitly associate each variable with a specific type. Others, such as Haskell's, perform type inference: the compiler draws conclusions about the types of variables based on how programmers use those variables. For example, given a function f(x, y) that adds x and y together, the compiler can infer that x and y must be numbers—since addition is only defined for numbers. Thus, any call to f elsewhere in the program that specifies a non-numeric type (such as a string or list) as an argument would signal an error.

Numerical and string constants and expressions in code can and often do imply type in a particular context. For example, an expression 3.14 might imply a type of floating-point, while [1, 2, 3] might imply a list of integers—typically an array.

Type inference is in general possible, if it is computable in the type system in question. Moreover, even if inference is not computable in general for a given type system, inference is often possible for a large subset of real-world programs. Haskell's type system, a version of Hindley–Milner, is a restriction of System Fω to so-called rank-1 polymorphic types, in which type inference is computable. Most Haskell compilers allow arbitrary-rank polymorphism as an extension, but this makes type inference not computable. (Type checking is decidable, however, and rank-1 programs still have type inference; higher rank polymorphic programs are rejected unless given explicit type annotations.)

Decision problems

[edit]

A type system that assigns types to terms in type environments using typing rules is naturally associated with the decision problems of type checking, typability, and type inhabitation.[28]

  • Given a type environment , a term , and a type , decide whether the term can be assigned the type in the type environment.
  • Given a term , decide whether there exists a type environment and a type such that the term can be assigned the type in the type environment .
  • Given a type environment and a type , decide whether there exists a term that can be assigned the type in the type environment.

Unified type system

[edit]

Some languages like C# or Scala have a unified type system.[29] This means that all C# types including primitive types inherit from a single root object. Every type in C# inherits from the Object class. Some languages, like Java and Raku, have a root type but also have primitive types that are not objects.[30] Java provides wrapper object types that exist together with the primitive types so developers can use either the wrapper object types or the simpler non-object primitive types. Raku automatically converts primitive types to objects when their methods are accessed.[31]

Compatibility: equivalence and subtyping

[edit]

A type checker for a statically typed language must verify that the type of any expression is consistent with the type expected by the context in which that expression appears. For example, in an assignment statement of the form x := e, the inferred type of the expression e must be consistent with the declared or inferred type of the variable x. This notion of consistency, called compatibility, is specific to each programming language.

If the type of e and the type of x are the same, and assignment is allowed for that type, then this is a valid expression. Thus, in the simplest type systems, the question of whether two types are compatible reduces to that of whether they are equal (or equivalent). Different languages, however, have different criteria for when two type expressions are understood to denote the same type. These different equational theories of types vary widely, two extreme cases being structural type systems, in which any two types that describe values with the same structure are equivalent, and nominative type systems, in which no two syntactically distinct type expressions denote the same type (i.e., types must have the same "name" in order to be equal).

In languages with subtyping, the compatibility relation is more complex: If B is a subtype of A, then a value of type B can be used in a context where one of type A is expected (covariant), even if the reverse is not true. Like equivalence, the subtype relation is defined differently for each programming language, with many variations possible. The presence of parametric or ad hoc polymorphism in a language may also have implications for type compatibility.

See also

[edit]

Notes

[edit]
  1. ^ The Burroughs ALGOL computer line determined a memory location's contents by its flag bits. Flag bits specify the contents of a memory location. Instruction, data type, and functions are specified by a 3 bit code in addition to its 48 bit contents. Only the MCP (Master Control Program) could write to the flag code bits.
  2. ^ For example, a leaky abstraction might surface during development, which may show that more type development is needed. —"The evaluation of a well-typed program always terminates".—B. Nordstr?m, K. Petersson, and J. M. Smith[5] A systematic change in variables to avoid capture of a free variable can introduce error, in a functional programming language where functions are first class citizens.[6] —From the lambda calculus article.

References

[edit]
  1. ^ Pierce 2002, p. 1: "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."
  2. ^ Cardelli 2004, p. 1: "The fundamental purpose of a type system is to prevent the occurrence of execution errors during the running of a program."
  3. ^ Pierce 2002, p. 208.
  4. ^ a b Sethi, R. (1996). Programming languages: Concepts and constructs (2nd ed.). Addison-Wesley. p. 142. ISBN 978-0-201-59065-4. OCLC 604732680.
  5. ^ Nordstr?m, B.; Petersson, K.; Smith, J.M. (2001). "Martin-L?f's Type Theory". Algebraic and Logical Structures. Handbook of Logic in Computer Science. Vol. 5. Oxford University Press. p. 2. ISBN 978-0-19-154627-3.
  6. ^ Turner, D.A. (12 June 2012). "Some History of Functional Programming Languages" (PDF). invited lecture at TFP12, at St Andrews University. See the section on Algol 60.
  7. ^ "... any sound, decidable type system must be incomplete" —D. Remy (2017). p. 29, Remy, Didier. "Type systems for programming languages" (PDF). Archived from the original (PDF) on 14 November 2017. Retrieved 26 May 2013.
  8. ^ Pierce 2002.
  9. ^ a b c Skeet, Jon (2019). C# in Depth (4 ed.). Manning. ISBN 978-1617294532.
  10. ^ Miglani, Gaurav (2018). "Dynamic Method Dispatch or Runtime Polymorphism in Java". Archived from the original on 2025-08-06. Retrieved 2025-08-06.
  11. ^ Wright, Andrew K. (1995). Practical Soft Typing (PhD). Rice University. hdl:1911/16900.
  12. ^ "dynamic (C# Reference)". MSDN Library. Microsoft. Retrieved 14 January 2014.
  13. ^ "std::any — Rust". doc.rust-lang.org. Retrieved 2025-08-06.
  14. ^ Meijer, Erik; Drayton, Peter. "Static Typing Where Possible, Dynamic Typing When Needed: The End of the Cold War Between Programming Languages" (PDF). Microsoft Corporation.
  15. ^ Laucher, Amanda; Snively, Paul (2012). "Types vs Tests". InfoQ.
  16. ^ Xi, Hongwei (1998). Dependent Types in Practical Programming (PhD). Department of Mathematical Sciences, Carnegie Mellon University. CiteSeerX 10.1.1.41.548.
    Xi, Hongwei; Pfenning, Frank (1999). "Dependent Types in Practical Programming". Proceedings of the 26th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages. ACM. pp. 214–227. CiteSeerX 10.1.1.69.2042. doi:10.1145/292540.292560. ISBN 1581130953. S2CID 245490.
  17. ^ Visual Basic is an example of a language that is both type-safe and memory-safe.
  18. ^ "4.2.2 The Strict Variant of ECMAScript". ECMAScript? 2020 Language Specification (11th ed.). ECMA. June 2020. ECMA-262.
  19. ^ "Strict mode – JavaScript". MDN. Developer.mozilla.org. 2025-08-06. Retrieved 2025-08-06.
  20. ^ "Strict Mode (JavaScript)". MSDN. Microsoft. Retrieved 2025-08-06.
  21. ^ "Strict typing". PHP Manual: Language Reference: Functions.
  22. ^ a b Bracha, G. "Pluggable Types" (PDF).
  23. ^ "Sure. It's called "gradual typing", and I would qualify it as trendy. ..." Is there a language that allows both static and dynamic typing?. stackoverflow. 2012.
  24. ^ a b c Kopylov, Alexei (2003). "Dependent intersection: A new way of defining records in type theory". 18th IEEE Symposium on Logic in Computer Science. LICS 2003. IEEE Computer Society. pp. 86–95. CiteSeerX 10.1.1.89.4223. doi:10.1109/LICS.2003.1210048.
  25. ^ Mitchell, John C.; Plotkin, Gordon D. (July 1988). "Abstract Types Have Existential Type" (PDF). ACM Trans. Program. Lang. Syst. 10 (3): 470–502. doi:10.1145/44501.45065. S2CID 1222153.
  26. ^ a b Siek, Jeremy (24 March 2014). "What is gradual typing?".
  27. ^ Siek, Jeremy; Taha, Walid (September 2006). Gradual Typing for Functional Languages (PDF). Scheme and Functional Programming 2006. University of Chicago. pp. 81–92.
  28. ^ Barendregt, Henk; Dekkers, Wil; Statman, Richard (20 June 2013). Lambda Calculus with Types. Cambridge University Press. p. 66. ISBN 978-0-521-76614-2.
  29. ^ "8.2.4 Type system unification". C# Language Specification (5th ed.). ECMA. December 2017. ECMA-334.
  30. ^ "Native Types". Perl 6 Documentation.
  31. ^ "Numerics, § Auto-boxing". Perl 6 Documentation.

Further reading

[edit]
[edit]
规培是什么 肠胃不好吃什么药效果好 烤冷面的面皮是什么面 梦见陌生人死了是什么意思 营卫不和吃什么中成药
庆幸是什么意思 司令员是什么军衔 说话口臭是什么原因引起的 义齿是什么 黑豆加红枣有什么功效
角瓜念什么 南京有什么特产 皮革胃是什么意思 小孩子不吃饭是什么原因引起的 产复欣颗粒什么时候吃
盆腔积液吃什么药效果好 白色鼻毛是什么原因 下肢动脉闭塞吃什么药 凝血酸是什么 梦见锁门是什么意思
头昏是什么原因hcv7jop6ns6r.cn 英雄本色是什么意思hcv8jop8ns8r.cn 3岁小孩不会说话是什么原因hcv8jop5ns1r.cn 什么药膏能让疣体脱落hcv8jop2ns8r.cn 贫血吃什么药好hcv8jop8ns6r.cn
未来是什么意思hcv7jop9ns9r.cn 7.2什么星座hcv8jop7ns8r.cn smt是什么hcv9jop2ns3r.cn 臆想是什么意思wzqsfys.com 诺诺是什么意思hcv9jop5ns8r.cn
狐假虎威是什么意思hcv9jop5ns5r.cn 什么的跳hcv8jop8ns1r.cn 万圣节为什么要送糖果hcv9jop6ns6r.cn 晚上喝蜂蜜水有什么好处hcv7jop4ns8r.cn 铺天盖地的意思是什么hcv9jop4ns8r.cn
安眠穴在什么位置hcv8jop2ns5r.cn 牙龈萎缩吃什么药见效快hcv7jop6ns7r.cn 床垫什么样的好hcv7jop6ns0r.cn 高血压为什么不能献血hcv8jop1ns9r.cn 女生白带是什么jiuxinfghf.com
百度