HOME

TheInfoList



OR:

Gradual typing is a
type system 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). Usu ...
that lies in between
static typing 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). Usu ...
and dynamic typing. Some variables and expressions may be given types and the correctness of the typing is checked at
compile time In computer science, compile time (or compile-time) describes the time window during which a language's statements are converted into binary instructions for the processor to execute. The term is used as an adjective to describe concepts relat ...
(which is ''static typing'') and some expressions may be left untyped and eventual type errors are reported at runtime (which is ''dynamic typing''). Gradual typing allows software developers to choose either type paradigm as appropriate, from within a single language. In many cases gradual typing is added to an existing dynamic language, creating a derived language allowing but not requiring static typing to be used. In some cases a language uses gradual typing from the start.


History

The term was coined by Jeremy Siek, who developed gradual typing in 2006 with Walid Taha.


Implementation

In gradual typing, a special type named ''dynamic'' is used to represent statically-unknown types. The notion of type equality is replaced by a new relation called ''consistency'' that relates the dynamic type to every other type. The consistency relation is reflexive and symmetric but not transitive. Prior attempts at integrating static and dynamic typing tried to make the dynamic type be both the top and bottom of the subtype hierarchy. However, because
subtyping In programming language theory, subtyping (also called subtype polymorphism or inclusion polymorphism) is a form of type polymorphism. A ''subtype'' is a datatype that is related to another datatype (the ''supertype'') by some notion of substi ...
is transitive, that results in every type becoming related to every other type, and so subtyping would no longer rule out any static type errors. The addition of a second phase of plausibility checking to the type system did not completely solve this problem. Gradual typing can easily be integrated into the type system of an object-oriented language that already uses the subsumption rule to allow implicit upcasts with respect to subtyping. The main idea is that consistency and subtyping are orthogonal ideas that compose nicely. To add subtyping to a gradually-typed language, simply add the subsumption rule and add a subtyping rule that makes the dynamic type a subtype of itself, because subtyping is supposed to be reflexive. (But do not make the top of the subtyping order dynamic!)


Examples

Examples of gradually typed languages derived from existing dynamically typed languages include Closure Compiler,
TypeScript TypeScript (abbreviated as TS) is a high-level programming language that adds static typing with optional type annotations to JavaScript. It is designed for developing large applications and transpiles to JavaScript. It is developed by Micr ...
(both for
JavaScript JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior. Web browsers have ...
), Hack (for PHP), PHP (since 7.0), Typed Racket (for Racket), Typed Clojure (for
Clojure Clojure (, like ''closure'') is a dynamic programming language, dynamic and functional programming, functional dialect (computing), dialect of the programming language Lisp (programming language), Lisp on the Java (software platform), Java platfo ...
), Cython (a Python compiler), mypy (a static type checker for Python)
pyre
(alternative static type checker for Python), or cperl (a typed Perl 5).
ActionScript ActionScript is an object-oriented programming language originally developed by Macromedia Inc. (later acquired by Adobe). It is influenced by HyperTalk, the scripting language for HyperCard. It is now an implementation of ECMAScript (mean ...
is a gradually typed language that is now an implementation of
ECMAScript ECMAScript (; ES) is a standard for scripting languages, including JavaScript, JScript, and ActionScript. It is best known as a JavaScript standard intended to ensure the interoperability of web pages across different web browsers. It is stan ...
, though it originally arose separately as a sibling, both influenced by Apple's
HyperTalk HyperTalk is a discontinued high-level, procedural programming language created in 1987 by Dan Winkler and used in conjunction with Apple Computer's HyperCard hypermedia program by Bill Atkinson. Because the main target audience of HyperTalk ...
. A system for the J programming language has been developed, adding coercion, error propagation and filtering to the normal validation properties of the type system as well as applying type functions outside of function definitions, thereby the increasing flexibility of type definitions. Conversely, C# started as a statically typed language, but as of version 4.0 is gradually typed, allowing variables to be explicitly marked as dynamic by using the dynamic type. Gradually typed languages not derived from a dynamically typed language include Dart, Dylan, and Raku. Raku (formerly Perl6) has gradual typing implemented from the start. Type checks occur at all locations where values are assigned or bound. An "untyped" variable or parameter is typed as Any, which will match (almost) all values. The compiler flags type-checking conflicts at compile time if it can determine at compile time that they will never succeed.
Objective-C Objective-C is a high-level general-purpose, object-oriented programming language that adds Smalltalk-style message passing (messaging) to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was ...
has gradual typing for object pointers with respect to method calls. Static typing is used when a variable is typed as pointer to a certain class of object: when a method call is made to the variable, the compiler statically checks that the class is declared to support such a method, or it generates a warning or error. However, if a variable of the type id is used, the compiler will allow any method to be called on it. The
JS++ JS++ is a programming language for web development that extends JavaScript with a sound type system. It includes imperative, object-oriented, functional, and generic programming features. It is free and open-source software released under ...
programming language, released in 2011, is a superset of
JavaScript JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior. Web browsers have ...
(dynamically typed) with a gradual type system that is
sound In physics, sound is a vibration that propagates as an acoustic wave through a transmission medium such as a gas, liquid or solid. In human physiology and psychology, sound is the ''reception'' of such waves and their ''perception'' by the br ...
for
ECMAScript ECMAScript (; ES) is a standard for scripting languages, including JavaScript, JScript, and ActionScript. It is best known as a JavaScript standard intended to ensure the interoperability of web pages across different web browsers. It is stan ...
and DOM API corner cases.


References


Further reading

* {{Cite book, last1=Siek, first1=Jeremy G., last2=Vitousek, first2=Michael M., last3=Cimini, first3=Matteo, last4=Boyland, first4=John Tang, title=Refined Criteria for Gradual Typing , date=2015, editor-last=Ball, editor-first=Thomas, editor2-last=Bodik, editor2-first=Rastislav, editor3-last=Krishnamurthi, editor3-first=Shriram, editor4-last=Lerner, editor4-first=Benjamin S., editor5-last=Morrisett, editor5-first=Greg , series=Leibniz International Proceedings in Informatics , location=Dagstuhl, Germany, publisher=Schloss Dagstuhl–Leibniz-Zentrum fuer Informatik, volume=32, pages=274–293, doi=10.4230/lipics.snapl.2015.274, doi-access=free , isbn=9783939897804, s2cid=15383644 Type systems