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:

    I think “taste” is a social concept and not an artistic one. I’m willing to show good taste, if I can, in somebody else’s living room, but our reading life is too short for a writer to be in any way polite. Since his words enter into another’s brain in silence and intimacy, he should be as honest and explicit as we are with ourselves.
    John Updike (b. 1932)

    The vanity of men, a constant insult to women, is also the ground for the implicit feminine claim of superior sensitivity and morality.
    Patricia Meyer Spacks (b. 1929)

    I believe the declaration that “all men are created equal” is the great fundamental principle upon which our free institutions rest.
    Abraham Lincoln (1809–1865)

    I shouldn’t want you to be surprised, or to draw any particular inference from my making speeches, or not making speeches, out there. I don’t recall any candidate for President that ever injured himself very much by not talking.
    Calvin Coolidge (1872–1933)