10.07.2015 Views

An Introduction to Functional Programming Through Lambda Calculus

An Introduction to Functional Programming Through Lambda Calculus

An Introduction to Functional Programming Through Lambda Calculus

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

- 178 -<strong>to</strong> insert:- val iinsert = insert (fn (i1:int,i2:int) => i1 val iinsert = fn : int -> ((int list) -> (int list))Now, the comparison function is:(int * int) -> boolso ’a must be int in the rest of the function.Functions which are defined for generalised types are said <strong>to</strong> be polymorphic because they have many forms.Polymorphic typing gives substantial power <strong>to</strong> a programming language and a great deal of research and developmenthas gone in<strong>to</strong> its theory and practise. There are several forms of polymorphism. Strachey distinguishes ‘ad-hoc’polymorphism through opera<strong>to</strong>r overloading from ‘parameterised’ polymorphism through abstraction over types.Cardelli distinguishes ‘explicit’ parameterised polymorphism where the types are themselves objects from the weaker‘implicit’ polymorphism where type variables are used in type expressions but types are not themselves objects, as inSML. Milner first made type polymorphism in functional languages practical with his early ML for LCF. Thisintroduced polymorphic type checking where types are deduced from type expressions and variable use. Hope andMiranda also have implicit parameterised polymorphic type checking.9.23. New typesA new concrete type may be introduced by a datatype binding. This is used <strong>to</strong> define a new type’s constituentvalues recursively byi) listing base values explicitlyii)defining structured values in terms of base values and other structured values.The binding introduces new type construc<strong>to</strong>rs which are used <strong>to</strong> build new values of that datatype. They are also used<strong>to</strong> identify and manipulate such values.At simplest, a datatype binding takes the form:datatype = | |...which defines the base values of type , an identifier, <strong>to</strong> be the type construc<strong>to</strong>r identifiers or etc.For example, we could define the type bool with:- datatype bool = true | false;> datatype bool = true | falsecon true = true : boolcon false = false : boolThis defines the construc<strong>to</strong>rs true and false for the new type bool. In effect, this specifies that an object of typebool may have either the value true or the value false. <strong>An</strong> equality test for bool is also defined so that thevalues true and false may be tested explicitly.For example, a traffic light goes through the stages red, red and amber, green, amber and back <strong>to</strong> red:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!