The Idea Behind Types

From Mesham
Revision as of 11:35, 3 July 2010 by Polas (talk | contribs)
Jump to navigationJump to search

A Type

The concept of a type will be familar to many programmers. A large subset of languages follow the syntax [Type] [Variablename], such as "int a" or "float b", to allow the programmer to declare the variable. Such a statement affects both the compiler and runtime semantics - the compiler can perform analysis and optimisation (such as type checking) and in runtime the variable has a specific size and format. When we consider these sorts of languages, it can be thought of that the programmer provides information, to the compiler, via the type. However, there is only so much that one single type can reveal, and so languages often include numerous keywords in order to allow for the programmer to specify additional information. Taking C as an example, in order to declare a variable "m" to be a character in read only memory the programmer writes "const char m". In order to extend the language, and allow for extra variable attributes (such as where a variable is located in the parallel programming context) then new keywords would need to be introduced, which is less than ideal.

Type Oriented Programming

The approach adopted by Mesham is to allow the programmer to encode all variable information via the type system, by combining different types together to form a supertype (type chain.) In our language, "const char m" becomes "var m: Char :: const[]", where var m declares the variable, the operator ":" specifies the type and the operator "::" combines two types together. In this case, the supertype is that formed by combining the type Char with the type const. It should be noted that some type cohercions, such as "Int :: Char" are meaningless and so rules exist within each type to govern which combinations are allowed.

Type presidence is from right to left - in the example "Char :: const[]", it can be thought of that the read only attributes of const override the default read/write attributes of Char. Abstractly, the programmer can consider the supertype (type chain) formed to be a little bit like a linked list. For instance the supertype created by "A::B::C::D::E" is illustrated below.

Type Chain Illustration

Advantages

Using this approach many different attributes can be associated with a variable, the fact that types are loosely coupled means that the language designers can add attributes (types) with few problems, and by only changing the type of a variable the semantics can change considerably. Another advantage is that the rich information provided by the programmer allows for many optimisations to be performed during compilation that using a lower level language might not be obvious to the compiler.

Technically

On a more technical note, the type system implements a number of services. These are called by the core of the compiler and if the specific type does not honour that service, then the call is passed onto the next in the chain - until all are exhausted. For instance, using the types "A::B::C::D::E", if service "Q1" was called, then type "E" would be asked first, if it did not honour the service, "Q1" would be passed to type "D" - if that type did not honour it then it would be passed to type "C" and so forth.