Headline

A data type that does not reveal representation

Illustration

An abstract data type is usually just defined through the list of operations on the type, possibly enirched (formaly or informaly) by properties (invariants, pre- and postconditions).

Consider the following concrete data type for points:

data Point = Point { getX :: Int, getY :: Int }
  deriving (Eq, Show, Read)

Now suppose we want to hide the precise representation of points. In particular, we want to rule out that programmers can match and apply the constructor Point. The existing getters are sufficient to observe points without matching, but we need to provide some "public" means of constructing points.

mkPoint :: Int -> Int -> Point
mkPoint = Point

The idea is now to export mkPoint, but not the constructor, thereby making possible representation changes without changing any code that uses points. This is, of course, a trivial example, as the existing representation of points is probably quite appropriate, but see a more advanced illustration for an abstract data type Stack.

A complete module for an abstract data type for points may then look like this:

module Point(
  Point, -- constructor is NOT exported
  mkPoint,
  getX,
  getY
) where

data Point = Point { getX :: Int, getY :: Int }
  deriving (Eq, Show, Read)

mkPoint :: Int -> Int -> Point
mkPoint = Point

When defining an abstract data type, we take indeed the point of view that the representation and thus the implementation as such is not known or not to be looked at. Hence, ideally, the intended functionality should be described in some other way. For instance, we may describe the functionality by properties. For instance, in Haskell we may declare testable Technology:QuickCheck properties like this:

prop_getX :: Int -> Int -> Bool
prop_getX x y = getX (mkPoint x y) == x

prop_getY :: Int -> Int -> Bool
prop_getY x y = getY (mkPoint x y) == y

These properties describe the (trivial) correspondence between construction with mkPoint and observation with getX and getY. Logically, the first property says that for all given x and y, we can construct a point and we can retrieve x again from that point with getX.

Relationships


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.