Programming Language
Programming Language
2-2
Mappings (2)
2-3
Arrays (1)
▪ Type declarations:
type Color is (red, green, blue);
type Pixel is array (Color) of Boolean;
▪ Application code:
p: Pixel := (true, false, true);
c: Color;
array construction
…
p(c) := not p(c);
indexing indexing
2-6
Example: Ada arrays (2)
▪ Set of values:
Pixel = Color → Boolean = {red, green, blue} → {false, true}
viz: {red → false, green → false, blue → false}
{red → false, green → false, blue → true}
{red → false, green → true, blue → false}
{red → false, green → true, blue → true}
{red → true, green → false, blue → false}
{red → true, green → false, blue → true}
{red → true, green → true, blue → false}
{red → true, green → true, blue → true}
▪ Cardinality:
#Pixel = (#Boolean)#Color = 23 = 8
2-7
Example: Ada 2-dimensional arrays
▪ Type declarations:
type Xrange is range 0 .. 511;
type Yrange is range 0 .. 255;
type Window is
array (YRange, XRange) of Pixel;
▪ Set of values:
Window = Yrange Xrange → Pixel
= {0, 1, …, 255} {0, 1, …, 511} → Pixel
▪ Cardinality:
#Window = (#Pixel)#Yrange #Xrange = 8256 512
2-8
Functions as mappings
▪ Definition:
function is_even (n: Integer)
return Boolean is
begin or any other code
return (n mod 2 = 0); that achieves the
end; same effect
▪ Type:
Integer → Boolean
▪ Value:
{…, 0 → true, 1 → false, 2 → true, 3 → false, …}
2-11
Disjoint unions (2)
▪ Cardinality:
#(S + T) = #S + #T hence the “+” notation
▪ Type declarations:
class Point {
private float x, y;
… // methods
}
class Circle extends Point {
private float r; inherits x and y
… // methods from Point
}
class Rectangle extends Point {
private float w, h; inherits x and y
… // methods from Point
}
2-13
Example: Java objects (2)
2-14
Example: Java objects (3)
▪ Methods:
class Point {
…
public float area()
{ return 0.0; }
}
class Circle extends Point {
…
public float area() overrides Point’s
{ return 3.1416 * r * r; } area() method
}
class Rectangle extends Point {
… overrides Point’s
public float area() area() method
{ return w * h; } 2-15
}
Example: Java objects (4)
▪ Application code:
Rectangle box =
new Rectangle(1.5, 2.0, 3.0, 4.0);
float a1 = box.area();
it can refer to a
Point it = …; Point, Circle, or
float a2 = it.area(); Rectangle object
calls the appropriate
area() method
2-16
Example: Haskell algebraic data types (1)
▪ Type declaration:
data Number = Exact Int | Inexact Float
Each Number value consists of a tag, together
with either an Integer variant (if the tag is
Exact) or a Float variant (if the tag is Inexact).
▪ Set of values:
Number = Exact Integer + Inexact Float
viz: … Exact(–2) Exact(–1) Exact 0 Exact 1 Exact 2 …
… Inexact(–1.0) … Inexact 0.0 … Inexact 1.0 …
▪ Cardinality:
#Number = #Integer + #Float
2-17
Example: Haskell algebraic data types (2)
▪ Application code:
pi = Inexact 3.1416 disjoint-union
construction
rounded :: Number -> Integer
rounded num =
case num of
Exact i -> i
projection
Inexact r -> round r tag test
(by pattern
matching)
2-18
Example: Ada discriminated records (1)
▪ Type declarations:
type Accuracy is (exact, inexact);
type Number (acc: Accuracy := exact) is
record
case acc of
when exact => ival: Integer;
when inexact => rval: Float;
end case;
end record;
2-19
Example: Ada discriminated records (2)
▪ Set of values:
Number = exact Integer + inexact Float
viz: … exact(–2) exact(–1) exact 0 exact 1 exact 2 …
… inexact(–1.0) … inexact 0.0 … inexact 1.0 …
▪ Cardinality:
#Number = #Integer + #Float
2-20
Example: Ada discriminated records (3)
▪ Type declarations:
type Form is
(pointy, circular, rectangular);
type Figure (f: Form := pointy) is record
x, y: Float;
case f is
when pointy => null;
when circular => r: Float;
when rectangular => w, h: Float;
end case;
end record;
Each Figure value consists of a tag field named f, together with a pair
of Float fields named x and y, together with either an empty variant or a
Float variant field named r or a pair of Float variant fields named w and h.
2-21
Example: Ada discriminated records (4)
▪ Set of values:
Figure = pointy(Float Float)
+ circular(Float Float Float)
+ rectangular(Float Float Float Float)
e.g.: pointy(1.0, 2.0) represents the point (1, 2)
circular(0.0, 0.0, 5.0)
rectangular(1.5, 2.0, 3.0, 4.0) represents a circle of
… radius 5 centered at (0, 0)
represents a 34 rectang-
le centered at (1.5, 2)
2-22
Example: Ada discriminated records (5)