• Nenhum resultado encontrado

Schemas as declarations

No documento University of Oxford (páginas 168-172)

Free Types

11.3 Schemas as declarations

A schema may be used whenever a declaration is expected: in a set comprehen-sion, in a lambda exprescomprehen-sion, or following a logical quantifier. The effect is to introduce the variables mentioned in the declaration part of the schema, under the constraint of the predicate part.

To illustrate this, we introduce a second schema with the same components asSchemaOne, but under some constraint:

SchemaTwo a:Z

c:P Z

a∈c∧c

This describes a subset of the bindings described by SchemaOne: number a must be an element of setc.

The following set consists of those sets of integerscthat contain the integer 0:

{SchemaTwo|a=0•c}

The same effect could be achieved by replacingSchemaTwo with a list of dec-larations and a constraint:

{a:Z; c:P Z|a∈c∧c∅ ∧a=0•c}

or by declaring an object of subrange typeSchemaTwo and selecting the two components:

{s :SchemaTwo|s.a=0•s.c}

The first expression, in whichSchemaTwois used as a declaration, is both more concise and more readable.

Example 11.12 IfDateis the schema named inExample 11.9, then the set com-prehension

{Date|day=31•month}

describes the set of all months that have31days:

{jan,mar,may,jul,aug,oct,dec}

11.3/Schemas as declarations 155 If a set comprehension has no term part, then the type of objects in the set depends upon the characteristic tuple of the declaration. For example, in the set

{a:Z; c:P Z|a∈c∧a=0}

the characteristic tuple is the pair (a,c), and the type of objects in the set is Z×P Z. The set consists of every pair(a,c)that meets the stated constraint.

In a schema, the order in which components are declared is unimportant, so the characteristic tuple of

{SchemaTwo|a=0}

is quite different from(a,c). A typical element of this set is a binding associat-ingawith0andc with some set containing0; the characteristic tuple has one component: the bindingh|aŽa,cŽc|i.

This is a binding in which componentais bound to the value of variablea, and componentcis bound to the value of variablec:

h|a Ž a, |{z}c

component name

Ž

variable valuez}|{

c |i

Such a binding, in which each component of a schema is bound to a value of the same name, is called acharacteristic binding.

IfSis the name of a schema, then we writeθSto denote the characteristic binding of components fromS. For example,

θSchemaTwo = h|aŽa,cŽc|i

Whenever this expression is used, variables a andc must already have been declared. Furthermore, the types of these variables must match those given in the declaration part ofSchemaTwo, although any predicate information in the schema is ignored.

Example 11.13 If Dateis the schema named inExample 11.9, thenθDate de-notes the characteristic binding

h|monthŽmonth,dayŽday|i

Whenever this expression is used, variablesmonthandday must be in scope, and their types must match those given in the declaration part ofDate.

There is a close relationship between our use of schemas as types and our use of theθnotation. As an illustration of this, consider the schema

SchemaThree a:Z

c:P Z

c∅ ∧a∈c c⊆ {0,1}

This introduces the same variables asSchemaOneandSchemaTwo, but under a more restrictive set of constraints.

If we useSchemaThreeto describe a set of bindings, then this set will have exactly four elements:

SchemaThree = {h|aŽ0,cŽ{0}|i,h|aŽ0,cŽ{0,1}|i, h|aŽ1,cŽ{1}|i,h|aŽ1,cŽ{0,1}|i}

The same set of bindings is described by the set comprehension {a:Z; c:P Z|c∅ ∧a∈c∧c⊆ {0,1} • h|aŽa,cŽc|i } which is equal to{SchemaThree•θSchemaThree}.

When a schema name is used where a set or type would be expected, we take it to represent the corresponding set of bindings. For any schemaS, the declarationa:S is an abbreviated form of

a:{S •θS}

The variablea is declared to be a binding of appropriate type that meets the constraint part of schemaS. Thus we see that bindings, not schema types, are the primitive notion.

Example 11.14 When used in a variable declaration, the schema nameDate rep-resents the set of all valid dates: bindings in which there are at leastday days in month month. This set of bindings could also be written as a set compre-hension:

{Date•θDate}

Notice that, althoughθDateis the default term of this set comprehension, we include it here to avoid confusion. It is not immediately obvious that the ex-pression{Date}denotes a set comprehension.

11.3/Schemas as declarations 157 A schema may be used as the declaration part of a lambda expression. If SchemaOneis as defined above, then

FunctionOne == ( λSchemaOne•a2)

introduces a function defined upon objects of schema type, mapping any bind-ing ofaandc to the square of the value ofa. For example,

FunctionOneh|aŽ2,cŽ{1,2,3}|i = 4

As inSection 8.2, the source type of a lambda expression is given by the char-acteristic tuple of the declaration. In this case, the charchar-acteristic tuple has a single component: θSchemaOne.

Example 11.15 An object of schema typeBoxOfficehas two components: a set of seats allocated for the performance, and a record of which seats have been sold, and to whom. Given such an object, we may be interested in the set of seats that have yet to be sold. The function

free == ( λBoxOffice•#(seating\domsold) )

maps an object of typeBoxOfficeto the number of unsold seats, calculated as the size of the set difference betweenseatinganddomsold.

A schema may be used as the declaration part of a quantified expression;

this has the effect of introducing the components of the schema and then con-straining them. For example,

∃SchemaTwo•a=0 a ∃a:Z; c:P Z|c∅ ∧a∈c•a=0

In such expressions, the order in which the components are declared is unim-portant: there are no characteristic tuples to consider.

These expressions may be used to make statements about objects of the corresponding schema type. For example, the following predicate states that, for any object of typeSchemaOne, theacomponent must be an element of the c component:

∀SchemaOne•a∈c

This predicate is false, as the bindingh|aŽ1,c Ž{2,3}|iis an object of type SchemaOne. On the other hand, the predicate

∀SchemaTwo•a∈c

is true, as any binding of subrange typeSchemaTwomust satisfy precisely this constraint.

Example 11.16 The following predicate states that there is an object of schema typeDatesuch that the value ofmonthisfeb and the value ofdayis29:

∃Date•month=feb∧day =29

This is true: there can be29days in February.

Example 11.17 The following predicate states that, for any object of schema typeDate, the value ofday must be less than or equal to30:

∀Date•day 30

This is false, as the bindingh|monthŽmar,day Ž31|isatisfies the predicate part ofDate. March is a month with31days.

Whenever a schema appears as the declaration part of a quantified expres-sion, the same result could be achieved by declaring an object of schema type.

For example, the quantified predicate

∀SchemaTwo•a∈c

is logically equivalent to

∀s:SchemaTwo•s.a∈s.c

Both predicates insist that there is a binding of typeSchemaTwosuch thatais an element ofc.

No documento University of Oxford (páginas 168-172)

Documentos relacionados