Type System - Explicit or Implicit Declaration and Inference

Explicit or Implicit Declaration and Inference

For more details on this topic, see Type inference.

Many static type systems, such as those of C and Java, require type declarations: The programmer must explicitly associate each variable with a particular 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. Therefore, 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 might imply a list of integers – typically an array.

Type inference is in general possible, if it is decidable in the type theory in question. Moreover, even if inference is undecidable in general for a given type theory, 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 decidable. Most Haskell compilers allow arbitrary-rank polymorphism as an extension, but this makes type inference undecidable. (Type checking is decidable, however, and rank-1 programs still have type inference; higher rank polymorphic programs are rejected unless given explicit type annotations.)

Read more about this topic:  Type System

Famous quotes containing the words explicit, implicit, declaration and/or inference:

    ... the Ovarian Theory of Literature, or, rather, its complement, the Testicular Theory. A recent camp follower ... of this explicit theory is ... Norman Mailer, who has attributed his own gift, and the literary gift in general, solely and directly to the possession of a specific pair of organs. One writes with these organs, Mailer has said ... and I have always wondered with what shade of ink he manages to do it.
    Cynthia Ozick (b. 1928)

    The true colour of life is the colour of the body, the colour of the covered red, the implicit and not explicit red of the living heart and the pulses. It is the modest colour of the unpublished blood.
    Alice Meynell (1847–1922)

    Ignorance, forgetfulness, or contempt of the rights of man are the only causes of public misfortunes and of the corruption of governments.
    —French National Assembly. Declaration of the Rights of Man (drafted and discussed Aug. 1789, published Sept. 1791)

    The inference is, that God has restated the superiority of the West. God always does like that when a thousand white people surround one dark one. Dark people are always “bad” when they do not admit the Divine Plan like that. A certain Javanese man who sticks up for Indonesian Independence is very lowdown by the papers, and suspected of being a Japanese puppet.
    Zora Neale Hurston (1891–1960)