Chapter 2 6 Modula 2 Structured Types Types

  • Slides: 22
Download presentation
Chapter 2. 6 Modula 2 Structured Types

Chapter 2. 6 Modula 2 Structured Types

Types in Modula 2 • Simple Types: values can’t be decomposed – Ordinal Types:

Types in Modula 2 • Simple Types: values can’t be decomposed – Ordinal Types: bijection with natural numbers – Reals: approx. representation for real values – Pointers: addresses in data memory • Structured Types: Values have # components – Arrays: all components have same type – Records: components can have # types – Sets: small sets of ordinal values – Procedures: entire subprograms

Types in Modula 2 • Simple Types: values can’t be decomposed – Ordinal Types:

Types in Modula 2 • Simple Types: values can’t be decomposed – Ordinal Types: bijection with natural numbers – Reals: approx. representation for real values – Pointers: addresses in data memory • Structured Types: Values have # components – Arrays: all components have same type – Records: components can have # types – Sets: small sets of ordinal values – Procedures: entire subprograms

ARRAY type index type component type

ARRAY type index type component type

ARRAY element array variable designator index expression

ARRAY element array variable designator index expression

ARRAY applications Mon 8 9 10 11 12 13 14 15 16 17 18

ARRAY applications Mon 8 9 10 11 12 13 14 15 16 17 18 19 Tue Wed info. Thu Fri st. pr. info. tele. N tele. E Sat st. pr. Sun

ARRAY applications TYPE Day Hour Course Schedule = = (Mon, Tue, Wed, Thu, Fri,

ARRAY applications TYPE Day Hour Course Schedule = = (Mon, Tue, Wed, Thu, Fri, Sat, Sun); [8. . 19]; ARRAY[0. . 10] OF CHAR; ARRAY Day, Hour OF Course; Room. K 1 : Schedule; VAR BEGIN. . . Room. K 1[Wed, 8] : = "info. "; . . . END

Sieve of Eratosthenes Print all primes <= Max Make a set of cardinals 2

Sieve of Eratosthenes Print all primes <= Max Make a set of cardinals 2 <= x <= MAX FOR all cardinals present in set Print the first cardinal x remaining in the set Remove all multiples of x from the set

Sieve of Eratosthenes Make a set of cardinals 2 <= x <= MAX 2

Sieve of Eratosthenes Make a set of cardinals 2 <= x <= MAX 2 3 4 5 6 7 8 FALSE TRUE FALSE CONST Max = 1000; VAR Sieve : ARRAY[2. . Max] OF BOOLEAN; i : [2. . Max]; . . . FOR i: = 2 TO MAX DO Sieve[i] : = TRUE END; (* FOR *) . . .

Sieve of Eratosthenes FOR all cardinals present in the set Print the first cardinal

Sieve of Eratosthenes FOR all cardinals present in the set Print the first cardinal in the set Remove all multiples of it VAR p : CARDINAL; . . . FOR i : = 2 TO Max DO IF Sieve[i] THEN p : = i; Write. Card(p, 5); Write. Ln; WHILE p <= Max DO Sieve[p] : = FALSE; p : = p + i END (* WHILE *) END (* IF *) END (* FOR *)

Types in Modula 2 • Simple Types: values can’t be decomposed – Ordinal Types:

Types in Modula 2 • Simple Types: values can’t be decomposed – Ordinal Types: bijection with natural numbers – Reals: approx. representation for real values – Pointers: addresses in data memory • Structured Types: Values have # components – Arrays: all components have same type – Records: components can have # types – Sets: small sets of ordinal values – Procedures: entire subprograms

Repairing a flat tire Refinement of “tools” • Tools : – Jack : device

Repairing a flat tire Refinement of “tools” • Tools : – Jack : device to lift a car – Wrench : device to loose or fasten bolts

RECORD example TYPE Date = RECORD Day : [1. . 31]; Month : [1.

RECORD example TYPE Date = RECORD Day : [1. . 31]; Month : [1. . 12]; Year : [1900. . 2100] END; VAR Today : Date;

RECORD type END field list fixed fields field type fields

RECORD type END field list fixed fields field type fields

RECORD element record variable designator Today. Day : = 20; Today. Month : =

RECORD element record variable designator Today. Day : = 20; Today. Month : = 10; Today. Year : = 1998; field identifier WITH Today DO Day : = 20; Month : = 10; Year : = 1998; END

Nested RECORDs example (1) TYPE Date VAR = RECORD Day : [1. . 31];

Nested RECORDs example (1) TYPE Date VAR = RECORD Day : [1. . 31]; Month : [1. . 12]; Year : [1900. . 2100] END; String = ARRAY [0. . 29] OF Char; Identity = RECORD Name, First. Name : String; Birth. Place : String; Birth. Date : Date; Issue. Date : Date; END; Myself Today : Identity; : Date;

Nested RECORDs example (2) Today. Day Today. Month Today. Year : = 20; :

Nested RECORDs example (2) Today. Day Today. Month Today. Year : = 20; : = 1998 Myself. Name Myself. First. Name Myself. Birth. Place Myself. Birth. Date. Day Myself. Birth. Date. Month Myself. Birth. Date. Year Myself. Issue. Date : = : = "Tiberghien"; "Jacques"; "Berchem"; 1; 4; 1946; Today;

Nested RECORDs example (2) WITH Today DO Day : = 20; Month : =

Nested RECORDs example (2) WITH Today DO Day : = 20; Month : = 10; Year : = 1998 END; WITH Myself DO Name : = "Tiberghien"; First. Name : = "Jacques"; Birth. Place : = "Berchem"; WITH Birth. Date DO Day : = 1; Month : = 4; Year : = 1946; END; Issue. Date : = Today; END

Variant RECORDs variant fields variant list variant

Variant RECORDs variant fields variant list variant

Variant RECORD with explicit tag field Car = RECORD Weight : CARDINAL; Number. Doors

Variant RECORD with explicit tag field Car = RECORD Weight : CARDINAL; Number. Doors : CARDINAL; Frame. Id, Engine. Id : ARRAY[0. . 19] OF CHAR; Fuel : (Gasoline, Fuel. Oil, LPG, Electricity); CASE Engine : (Explosion, Electricity) OF Explosion : Nbr. Cylinders : CARDINAL; Vol. Cylinders : CARDINAL | Electricity : Supply : (AC, DC); Voltage : CARDINAL; END;

Variant RECORD with implicit tag field Car = RECORD Weight : CARDINAL; Number. Doors

Variant RECORD with implicit tag field Car = RECORD Weight : CARDINAL; Number. Doors : CARDINAL; Frame. Id, Engine. Id : ARRAY[0. . 19] OF CHAR; Fuel : (Gasoline, Fuel. Oil, LPG, Electricity); CASE (Explosion, Electricity) OF Explosion : Nbr. Cylinders : CARDINAL; Vol. Cylinders : CARDINAL | Electricity : Supply : (AC, DC); Voltage : CARDINAL; END;

Types in Modula 2 • Simple Types: values can’t be decomposed – Ordinal Types:

Types in Modula 2 • Simple Types: values can’t be decomposed – Ordinal Types: bijection with natural numbers – Reals: approx. representation for real values – Pointers: addresses in data memory • Structured Types: Values have # components – Arrays: all components have same type – Records: components can have # types – Sets: small sets of ordinal values – Procedures: entire subprograms