Download Free Types-Formal Specification Methods in Software Development-Lecture Slides and more Slides Software Development Methodologies in PDF only on Docsity!
Chapter No. 10
Free Types
Introduction
- In the course of a specification we may define a variety of data structures: lists, arrays, or trees of elements drawn from one or more basic types.
- These structures could be modeled using a combination of sets and relations, but resulting definitions would be verbose.
- A more elegant, concise alternative is offered by free types: sets with explicit structuring information.
The Natural Numbers
- The set of all natural numbers, N, is already part of our mathematical language
- It has been defined as a subset of the built- in type Z.
- Here, the construction of a set similar to N will prove a useful illustration of free type.
- Thus we attempt to define such a set, beginning with a basic type nat, a zero element, and a partial function called succ
First Attempt Defining Natural No.
- Every element n is either constant zero or result of applying the succ to an element m.
zero : nat succ : nat © nat
Õ n : nat × n = zerp Ú Ö m : nat × n = succ m
- Unfortunately, this is not enough to capture the essence of N. docsity.com
3 rd^ Attempt Defining Natural No.
- Having realized that the function used to construct naturals must be total, try again:
zero : nat succ : nat ª nat
Õ n : nat × n = zerp Ú Ö m : nat × n = succ m {zero} » ran succ = ¸
- We failed to exclude possibility that some element is successor of two or more othersdocsity.com
Last Attempt Defining Natural No.
- We must add a fourth requirement that the function used must be injective.
zero : nat succ : nat ¬ nat
{zero} » ran succ = ¸ {zero} ¼ ran succ = nat
- The final requirement is that nat should be
- the smallest set that meets the conditions
Free Type Definition
- The above abbreviation not only fails to introduce names, also fails making distinct
- No guarantee, red is different from green.
- Consider free type definition
Colors :: = red | orange | yellow | green | blue | indigo | violet
- It introduces a set Colors, and seven distinct constants
Free Type Definition
- In this definition, we may infer that Colors is the smallest set containing the seven distinct elements red, orange, yellow, green, blue, indigo, and violet.
- The order is unimportant i.e. the definition
Colors :: = indigo | red | orange | yellow | green | blue | violet, same as
Colors :: = red | orange | yellow | green | blue | indigo | violet
Example 10.
- Suppose The University of Oxford awards BA, MSc., D. Phil, MA. For ceremonial purposes, degrees are ordered as follows:
- MA is the highest ranking
- D. Phil takes second place,
- MSc and BA third and fourth respectively.
- Suppose that we wish to represent this ordered collection of degrees as a free type.
- Ordering is similar to first four naturals
Degree ::= status Ý 0..3 Þ
First Attempt Defining Natural No.
- Every element n is either constant zero or result of applying the succ to an element m.
ba, msc, dphil, ma : Degree
ba = status 0 msc = status 1 dphil = status 2 ma = status 3
Partial Functions
Example 8.1 An organization has a system for keeping track of its employees while they are on the premises. Each employee is issued with an active badge which reports their current position to a central database.
- If set of all people is Person , and set of all locations is Location , then information may be described by a relation where_is of type Person Locations
- This relation is a partial function, i.e., where_is Person © Location
Total Functions
Definition: if each element of X is related to unique element of Y then partial function is a total function denoted by X ª Y.
X ª Y == { f : X©Y | dom f = X × f }
b c
d
1
2 3
X f Y
Example : Total Functions
Example 8.3 If Rachel is an employee,
then we may write where_is rachel to
denote her current location. This makes
sense only if the database has a unique
record of Rachel’s whereabouts. If
where_is is the function
{otto § lobby, peter § meeting,
quentin § meeting, rachel § meeting}
Then we may observe that
where_is rachel = meeting
Lambda Functions
- Suppose that f is a function whose domain is precisely those elements of X that satisfy a constraint p. If the result of applying f to an arbitrary element x can be written as the expression e, then f can be described as
f = {x :X | p x § e}
- Lambda notation offers more conciseness ( declaration | constraint result)
- Using lambda notation, our function f is f = (x :X | p e)