Chapter 2.6 Modula 2 Structured 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: 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 typecomponent type ARRAY type
ARRAY element array variable designatorindex expression array element
ARRAY applications MonSunSatFriThuWedTue info. teleN st.pr. teleEst.pr. teleEst.pr. info. st.pr.info. st.pr.info.
ARRAY applications TYPE Day = (Mon,Tue,Wed,Thu,Fri,Sat,Sun); Hour = [8..19]; Course = ARRAY[0..10] OF CHAR; Schedule = ARRAY Day, Hour OF Course; VAR RoomK1 : Schedule; BEGIN... RoomK1[Wed,8] := "info.";... END
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 CONST Max = 1000; VAR Sieve : ARRAY[2..Max] OF BOOLEAN; i : [2..Max];... FOR i:= 2 TO MAX DO Sieve[i] := TRUE END; (* FOR *) FALSE TRUE
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; WriteCard(p,5); WriteLn; 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: 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 to lift a car –Wrench : device to loose or fasten bolts
RECORD example TYPE Date = RECORD Day : [1.. 31]; Month : [1.. 12]; Year : [ ] END; VAR Today : Date;
RECORD type END field listfields fixed fields field type
RECORD element record variable designator field identifier Today.Day := 20; Today.Month := 10; Today.Year := 1998; WITH Today DO Day := 20; Month := 10; Year := 1998; END
Nested RECORDs example (1) TYPE Date = RECORD Day : [1.. 31]; Month : [1.. 12]; Year : [ ] END; String = ARRAY [0..29] OF Char; Identity = RECORD Name, FirstName : String; BirthPlace : String; BirthDate : Date; IssueDate : Date; END; VAR Myself : Identity; Today : Date;
Nested RECORDs example (2) Today.Day := 20; Today.Month := 10; Today.Year := 1998 Myself.Name := "Tiberghien"; Myself.FirstName := "Jacques"; Myself.BirthPlace := "Berchem"; Myself.BirthDate.Day := 1; Myself.BirthDate.Month := 4; Myself.BirthDate.Year := 1946; Myself.IssueDate := Today;
Nested RECORDs example (2) WITH Today DO Day := 20; Month := 10; Year := 1998 END; WITH Myself DO Name := "Tiberghien"; FirstName := "Jacques"; BirthPlace := "Berchem"; WITH BirthDate DO Day := 1; Month := 4; Year := 1946; END; IssueDate := Today; END
Variant RECORDs variant variant list variant fields
Variant RECORD with explicit tag field Car =RECORD Weight : CARDINAL; NumberDoors : CARDINAL; FrameId,EngineId : ARRAY[0..19] OF CHAR; Fuel :(Gasoline,FuelOil,LPG,Electricity); CASE Engine :(Explosion,Electricity) OF Explosion : NbrCylinders : CARDINAL; VolCylinders : CARDINAL | Electricity : Supply : (AC,DC); Voltage : CARDINAL; END;
Variant RECORD with implicit tag field Car =RECORD Weight : CARDINAL; NumberDoors : CARDINAL; FrameId,EngineId : ARRAY[0..19] OF CHAR; Fuel :(Gasoline,FuelOil,LPG,Electricity); CASE (Explosion,Electricity) OF Explosion : NbrCylinders : CARDINAL; VolCylinders : CARDINAL | Electricity : Supply : (AC,DC); Voltage : CARDINAL; END;
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