Example: Haskell algebraic data types (1)

[Pages:36]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-1

Example: Haskell algebraic data types (2)

Application code:

pi = Inexact 3.1416

rounded :: Number -> Integer

rounded num =

case num of

projection (by pattern

Exact i -> i Inexact r -> round r

matching)

disjoint-union construction

tag test

2-2

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;

Each Number value consists of a tag field named acc, together with either an Integer variant field named ival (if the tag is exact) or a Float variant field named rval (if the tag is inexact).

2-3

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-4

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-5

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) circular(0.0, 0.0, 5.0) rectangular(1.5, 2.0, 3.0, 4.0) ...

represents the point (1, 2)

represents a circle of radius 5 centered at (0, 0)

represents a 3?4 rectangle centered at (1.5, 2)

2-6

Example: Ada discriminated records (5)

Application code:

discriminated-record

box: Figure :=

construction

(rectangular, 1.5, 2.0, 3.0, 4.0);

function area (fig: Figure) return Float is

begin

case fig.f is

when pointy =>

return 0.0; when circular =>

tag test

return 3.1416 * fig.r**2;

when rectangular =>

return fig.w * fig.h;

end case;

end;

projection

2-7

Example: Java objects (1)

Type declarations:

class Point { private float x, y; ... // methods

}

class Circle extends Point { private float r; ... // methods

}

class Rectangle extends Point { private float w, h; ... // methods

}

inherits x and y from Point

inherits x and y from Point

2-8

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download