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.

- 69 -within our intended interpretations.For example, consider the effect of:iszero true ==λn.(n select_first) true =>true select_first ==λfirst.λsecond.first select_first =>λsecond.select_first ==λsecond.λfirst.λsecond.firstThis produces a perfectly good function for selecting the second argument in an application with three nestedarguments but we expect iszero <strong>to</strong> return a boolean.Using these functions is analogous <strong>to</strong> programming in machine code. In most CPUs, the sole objects areundifferentiated bit patterns which have no intrinsic meanings but are interpreted in different ways by the machinecode operations applied <strong>to</strong> them. For example, different machine code instructions may treat a bit pattern as a signedor unsigned integer, decimal or floating point number or a data or instruction address. Thus, a machine code programmay twos-complement an address or jump <strong>to</strong> a floating point number.The single typed systems programming language BCPL, a precursor of C, is similarly free and easy. Althoughrepresentations are provided for a variety of objects, operations are used without type checks on the assumption thattheir operands are appropriate. Early versions of C provided type checking but were somewhat lax when operationswere carried out on almost appropriate types, allowing indirection on integers or arithmetic on pointers, for example.It is claimed that this ‘freedom’ from types makes languages more flexible. It does ease implementation dependentprogramming where advantage is taken of particular architectural features on particular CPUs through bit or wordlevel manipulation but this in turn leads <strong>to</strong> a loss of portability because of gross incompatibilities betweenarchitectures. For example, many computer memories are based on 8 bit bytes so 16 bit words require 2 bytes.However, computers differ in the order in which these bytes are used: some put the <strong>to</strong>p 8 bits in the first byte bu<strong>to</strong>thers put them in the second byte. Thus, programs using ‘clever’ address arithmetic which involves knowing the byteorder won’t work on some computers. ‘Type free’ programming also increases incomprehensible errors through dodgylow-level subterfuges. For example, ‘cunning’ address manipulations <strong>to</strong> access the fields of a data structure may causethe corruption of other fields or of a completely different data structure which is close <strong>to</strong> the requisite one in memory,through byte mis-alignments.5.3. Type as objects and operationsTypes are introduced in<strong>to</strong> languages <strong>to</strong> control the use of operations on objects so as <strong>to</strong> ensure that only meaningfulcombinations are used. As we saw in chapter 2, variables in programming languages are used as a primary abstractionmechanism. In ‘typeless’ languages there are no restrictions on object/operation combinations and any variable may beassociated with any object. Here, variables just abstract over objects in general. In weakly typed languages, like LISPand Prolog, objects are typed but variables are not. There are restrictions on object/operation combinations but not onvariable/object associations. Thus, variables do not abstract over specific types. In strongly typed languages like MLand Pascal variables are specified as being of a specific type and have the same restrictions on use as objects of thattype.More formally, a type specifies a class of objects and associated operations. Object classes may be defined by listingtheir values, for example for booleans:TRUE is a booleanFALSE is a boolean

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

Saved successfully!

Ooh no, something went wrong!