\PassOptionsToPackage{unicode=true}{hyperref} % options for packages loaded elsewhere
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{textcomp} % provides euro and other symbols
\else % if luatex or xelatex
  \usepackage{unicode-math}
  \defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\fi
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\usepackage[]{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
}
\usepackage{hyperref}
\hypersetup{
            pdftitle={CSci 658-01: Software Language Engineering Abstraction},
            pdfauthor={H. Conrad Cunningham},
            pdfborder={0 0 0},
            breaklinks=true}
\urlstyle{same}  % don't use monospace font for urls
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\newenvironment{Shaded}{}{}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{#1}}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{#1}}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.49,0.56,0.16}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{#1}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textit{#1}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{#1}}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.53,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.56,0.13,0.00}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.73,0.13,0.13}{\textit{#1}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{#1}}}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.02,0.16,0.49}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{#1}}}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.74,0.48,0.00}{#1}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.73,0.40,0.53}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.10,0.09,0.49}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{#1}}}}
\usepackage{graphicx,grffile}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
\setlength{\emergencystretch}{3em}  % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{0}
% Redefines (sub)paragraphs to behave more like sections
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi

% set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother

\usepackage{caption}
\DeclareCaptionLabelFormat{nolabel}{}
\captionsetup{labelformat=nolabel}

\title{CSci 658-01: Software Language Engineering\\
Abstraction}
\author{\textbf{H. Conrad Cunningham}}
\date{\textbf{24 February 2018}}

\begin{document}
\maketitle

{
\setcounter{tocdepth}{4}
\tableofcontents
}
Copyright (C) 2017, 2018, \href{http://www.cs.olemiss.edu/~hcc}{H.
Conrad Cunningham}\\
Professor of \href{https://www.cs.olemiss.edu}{Computer and Information
Science}\\
\href{http://www.olemiss.edu}{University of Mississippi}\\
211 Weir Hall\\
P.O. Box 1848\\
University, MS 38677\\
(662) 915-5358

\textbf{Advisory}: The HTML version of this document requires use of a
browser that supports the display of MathML. A good choice as of
February 2018 is a recent version of Firefox from Mozilla.

TODO: - Combine or coordinate with other Data Abstraction and Modular
Design material - Expand to discuss the new Backpack module system

\hypertarget{abstraction}{%
\section{Abstraction}\label{abstraction}}

\hypertarget{introduction}{%
\subsection{Introduction}\label{introduction}}

As computing scientists and computer programmers, we should remember
\emph{Simplicity is good; complexity is bad.}

The most effective weapon that we have in the fight against complexity
is \emph{abstraction}. What is abstraction?

Abstraction is \emph{concentrating on the essentials and ignoring the
details.}

Sometimes abstraction is described as \emph{remembering the ``what'' and
ignoring the ``how''}.

\hypertarget{kinds-of-abstraction}{%
\subsection{Kinds of Abstraction}\label{kinds-of-abstraction}}

Large complex systems can only be made understandable by decomposing
them into modules. When viewed from the outside, from the standpoints of
users, each \emph{module} should be simple, with the complexity hidden
inside.

We strive for modules that have simple interfaces that can be used
without knowing the implementations. Here we use \emph{interface} to
mean any information about the module that other modules must assume to
be able to do their work correctly.

Two kinds of abstraction are of interest to computing scientists:
\emph{procedural abstraction} and \emph{data abstraction}.

\begin{description}
\tightlist
\item[Procedural abstraction:]
the separation of the logical properties of an \emph{action} from the
details of how the action is implemented.
\item[Data abstraction:]
the separation of the logical properties of \emph{data} from the details
of how the data are represented.
\end{description}

When we develop an algorithm following the top-down approach, we are
practicing procedural abstraction. At a high level, we break the problem
up into several tasks. We give each task a name and state its
requirements, but we do not worry about how the task is to be
accomplished until we expand it at a lower level of our design.

When we code a task in a programming language, we will typically make
each task a subprogram (procedure, function, subroutine, method, etc.).
Any other program component that calls the subprogram needs to know its
interface (name, parameters, return value, assumptions, etc.) but does
not need to know the subprogram's internal implementation details. The
internal implementation can be changed without affecting the caller.

In data abstraction, the focus is on the problem's data rather than the
tasks to be carried out.

\hypertarget{procedures-and-functions}{%
\subsection{Procedures and Functions}\label{procedures-and-functions}}

Generally we make the following distinctions among subprograms:

\begin{itemize}
\item
  A \emph{procedure} is (in its pure form) a subprogram that takes zero
  or more arguments but does not return a value. It is executed for its
  effects, such as changing values in a data structure within the
  program, modifying its reference or value-result arguments, or causing
  some effect outside the program (e.g., displaying text on the screen
  or reading from a file).
\item
  A \emph{function} is (in its pure form) a subprogram that takes zero
  or more arguments and returns a value but that does not have other
  effects.
\item
  A \emph{method} is a procedure or function often associated with an
  object or class in an object-oriented program. Some object-oriented
  languages use the metaphor of message-passing. A method is the feature
  of an object that receives a message. In an implementation, a method
  is typically a procedure or function associated with the (receiver)
  object; the object may be an \emph{implicit parameter} of the method.
\end{itemize}

Of course, the features of various programming languages and usual
practices for their use may not follow the above pure distinctions. For
example, a language may not distinguish between procedures and
functions. One term or another may be used for all subprograms.
Procedures may return values. Functions may have side effects. Functions
may return multiple values. The same subprogram can sometimes be called
either as a function or procedure.

Nevertheless, it is good practice to maintain the distinction between
functions and procedures for most cases in software design and
programming.

In Haskell, the primary unit of procedural abstraction is the pure
function. Haskell also groups functions and other declarations into a
program unit called a \texttt{module}. A \texttt{module} explicitly
exports selected functions and keep others hidden.

\hypertarget{using-top-down-stepwise-refinement}{%
\subsection{Using Top-Down Stepwise
Refinement}\label{using-top-down-stepwise-refinement}}

This section focuses on procedural abstraction. Later sections focus on
data abstraction.

A useful and intuitive design process for a small program is to begin
with a high-level solution and incrementally fill in the details. We
call this process top-down stepwise refinement. Here we introduce it
with an example.

\hypertarget{developing-a-square-root-package}{%
\subsubsection{Developing a square root
package}\label{developing-a-square-root-package}}

Consider the problem of computing the nonnegative square root of a
nonnegative number \(x\). Mathematically, we want to find the number
\(y\) such that

\begin{quote}
\(y \geq 0\) and \(y^{2} = x\).
\end{quote}

A common algorithm in mathematics for computing the above \(y\) is to
use Newton's method of successive approximations, which has the
following steps for square root:

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\tightlist
\item
  Guess at the value of \(y\).
\item
  If the current approximation (guess) is sufficiently close (i.e.~good
  enough), return it and stop; otherwise, continue.
\item
  Compute an improved guess by averaging the value of the guess \(y\)
  and \(x/y\), then go back to step 2.
\end{enumerate}

To encode this algorithm in Haskell, we work top down to decompose the
problem into smaller parts until each part can be solved easily. We
begin this \emph{top-down stepwise refinement} by defining a function
with the type signature:

\begin{Shaded}
\begin{Highlighting}[]
\OtherTok{    sqrtIter ::} \DataTypeTok{Double} \OtherTok{->} \DataTypeTok{Double} \OtherTok{->} \DataTypeTok{Double} 
\end{Highlighting}
\end{Shaded}

We choose type \texttt{Double} (double precision floating point) to
approximate the real numbers. Thus we can encode step 2 of the above
algorithm in Haskell as follows:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{    sqrtIter guess x }
        \FunctionTok{|}\NormalTok{ goodEnough guess x }\FunctionTok{=}\NormalTok{ guess }
        \FunctionTok{|}\NormalTok{ otherwise          }\FunctionTok{=}\NormalTok{ sqrtIter (improve guess x) x }
\end{Highlighting}
\end{Shaded}

We define \texttt{sqrtIter} to take two arguments--the current
approximation \texttt{guess} and number \texttt{x} for which we need the
square root. We have two cases:

\begin{itemize}
\item
  When the current approximation \texttt{guess} is sufficiently close to
  \texttt{x}, we return \texttt{guess}.

  We abstract this decision into a separate function \texttt{goodEnough}
  with type signature:

\begin{Shaded}
\begin{Highlighting}[]
\OtherTok{    goodEnough ::} \DataTypeTok{Double} \OtherTok{->} \DataTypeTok{Double} \OtherTok{->} \DataTypeTok{Bool} 
\end{Highlighting}
\end{Shaded}
\item
  When the approximation is not yet close enough, we reduce the problem
  to another application of \texttt{sqrtIter} itself to an improved
  approximation.

  We abstract the improvement process into a separate function
  \texttt{improve} with type signature:

\begin{Shaded}
\begin{Highlighting}[]
\OtherTok{    improve ::} \DataTypeTok{Double} \OtherTok{->} \DataTypeTok{Double} \OtherTok{->} \DataTypeTok{Double}
\end{Highlighting}
\end{Shaded}

  To ensure termination of \texttt{sqrtIter}, the argument
  \texttt{(improve\ guess\ x)} on the recursive call must get closer to
  a value that satisfies its base case.
\end{itemize}

The function \texttt{improve} takes the current \texttt{guess} and
\texttt{x} and carries out step 3 of the algorithm, thus averaging
\texttt{guess} and \texttt{x/guess}, as follows:

\begin{Shaded}
\begin{Highlighting}[]
\OtherTok{    improve ::} \DataTypeTok{Double} \OtherTok{->} \DataTypeTok{Double} \OtherTok{->} \DataTypeTok{Double} 
\NormalTok{    improve guess x }\FunctionTok{=}\NormalTok{ average guess (x}\FunctionTok{/}\NormalTok{guess)}
\end{Highlighting}
\end{Shaded}

Here we abstract \texttt{average} into a separate function as follows:

\begin{Shaded}
\begin{Highlighting}[]
\OtherTok{    average ::} \DataTypeTok{Double} \OtherTok{->} \DataTypeTok{Double} \OtherTok{->} \DataTypeTok{Double}
\NormalTok{    average x y }\FunctionTok{=}\NormalTok{ (x }\FunctionTok{+}\NormalTok{ y) }\FunctionTok{/} \DecValTok{2}
\end{Highlighting}
\end{Shaded}

The new guess is closer to the square root than the previous guess. Thus
the algorithm will terminate assuming a good choice for function
\texttt{goodEnough}, which guards the base case of the \texttt{sqrtIter}
recursion.

How should we define \texttt{goodEnough}? Given that we are working with
the limited precision of computer floating point arithmetic, it is not
easy to choose an appropriate test for all situations. Here we simplify
this and use a tolerance of 0.001.

We thus postulate the following definition for \texttt{goodEnough}:

\begin{Shaded}
\begin{Highlighting}[]
\OtherTok{    goodEnough ::} \DataTypeTok{Double} \OtherTok{->} \DataTypeTok{Double} \OtherTok{->} \DataTypeTok{Bool} 
\NormalTok{    goodEnough guess x }\FunctionTok{=}\NormalTok{ abs (square guess }\FunctionTok{-}\NormalTok{ x) }\FunctionTok{<} \FloatTok{0.001} 
\end{Highlighting}
\end{Shaded}

In the above, \texttt{abs} is the built-in absolute value function
defined in the standard Prelude library. We define square as the
following simple function (but could replace it by just
\texttt{guess\ *\ guess}):

\begin{Shaded}
\begin{Highlighting}[]
\OtherTok{    square ::} \DataTypeTok{Double} \OtherTok{->} \DataTypeTok{Double} 
\NormalTok{    square x }\FunctionTok{=}\NormalTok{ x }\FunctionTok{*}\NormalTok{ x }
\end{Highlighting}
\end{Shaded}

What is a good initial guess? It is sufficient to just use 1. So we can
define an overall square root function \texttt{sqrt\textquotesingle{}}
as follows:

\begin{Shaded}
\begin{Highlighting}[]
\OtherTok{    sqrt' ::} \DataTypeTok{Double} \OtherTok{->} \DataTypeTok{Double}
\NormalTok{    sqrt' x }\FunctionTok{|}\NormalTok{ x }\FunctionTok{>=} \DecValTok{0} \FunctionTok{=}\NormalTok{ sqrtIter }\DecValTok{1}\NormalTok{ x}
\end{Highlighting}
\end{Shaded}

(A square root function \texttt{sqrt} is defined in the Prelude library,
so a different name is needed to avoid the name clash.)

\hypertarget{making-the-package-a-haskell-module}{%
\subsubsection{Making the package a Haskell
module}\label{making-the-package-a-haskell-module}}

We can make this package into a Haskell module by putting the
definitions in a file (e.g., named \texttt{Sqrt.hs}) and adding a module
header at the beginning as follows:

\begin{Shaded}
\begin{Highlighting}[]
    \KeywordTok{module} \DataTypeTok{Sqrt}
\NormalTok{        (sqrt')}
    \KeywordTok{where}
        \CommentTok{-- give the definitions above for functions sqrt',}
        \CommentTok{--   sqrtIter, improve, average, and goodEnough, }
\end{Highlighting}
\end{Shaded}

The header gives the module the name \texttt{Sqrt} and defines the names
in parenthesis as being \emph{exported} to other modules that
\emph{import} this module. The other symbols (e.g., \texttt{sqrtIter},
\texttt{goodEnough}) are local to (i.e., hidden inside) the module.

In the above Haskell code, the symbol ``\texttt{-\/-}'' denotes the
beginning of an end-of-line comment. All text after that symbol is text
ignored by the Haskell compiler.

The Haskell module for the Square root case study is in file
\href{AbstractionCode/Sqrt.hs}{\texttt{Sqrt.hs}}. Limited testing code
is in module \href{AbstractionCode/TestSqrt.hs}{\texttt{TestSqrt.hs}}.

\hypertarget{top-down-stepwise-refinement}{%
\subsubsection{Top-down stepwise
refinement}\label{top-down-stepwise-refinement}}

The program design strategy known as \emph{top-down stepwise refinement}
is a relatively intuitive design process that has long been applied in
the design of structured programs in imperative procedural languages. It
is also useful in the functional setting.

In Haskell, we can apply top-down stepwise refinement as follows.

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
  Start with a high-level solution to the problem consisting of one or
  more functions. For each function, identify its type signature and
  functional requirements (i.e., its inputs, outputs, and termination
  condition).

  Some parts of each function are abstracted as ``pseudocode''
  expressions or as-yet-undefined function calls.
\item
  Choose one of the incomplete parts. Consider its type signature and
  functional requirements. Refine the incomplete part by breaking it
  into subparts or, if simple, defining it directly in terms of Haskell
  expressions (including calls to the Prelude or other available library
  functions).

  When refining an incomplete part, consider the various options
  according to the relevant design criteria (e.g., time, space,
  generality, understandability, elegance, etc.)

  The refinement of the function may require a refinement of the data
  being passed. If so, back up in the refinement process and readdress
  previous design decisions as needed.

  If it not possible to design an appropriate refinement, back up in the
  refinement process and readdress previous design decisions.
\item
  Continue step 2 until all parts are fully defined in terms of Haskell
  code and data and the resulting set of functions meets all required
  criteria.
\end{enumerate}

For as long as possible, we should stay with terminology and notation
that is close to the problem being solved. We can do this by choosing
appropriate function names and signatures and data types. (In later
chapters, we examine Haskell's rich set of builtin and user-defined
types.)

For stepwise refinement to work well, we must be willing to back up to
earlier design decisions when appropriate. We should keep good
documentation of the intermediate design steps.

The stepwise refinement method can work well for small programs, but it
may not scale well to large, long-lived, general purpose programs. In
particular, stepwise refinement may lead to a module structure in which
modules are tightly coupled and not robust with respect to changes in
requirements. A combination of techniques may be needed to develop
larger software systems.

\hypertarget{using-data-abstraction}{%
\subsection{Using Data Abstraction}\label{using-data-abstraction}}

A design technique that can help make a program robust with respect to
change in the data is to use data abstraction. As in the previous
subsection, let's begin with an example.

\hypertarget{rational-number-arithmetic}{%
\subsubsection{Rational number
arithmetic}\label{rational-number-arithmetic}}

For this example, let's implement a group of Haskell functions to
perform rational number arithmetic, assuming that the Haskell library
does not contain such a data type.

In mathematics we usually write rational numbers in the form
\(\frac{\texttt{x}}{\texttt{y}}\) where \texttt{x} and \texttt{y} are
integers and \texttt{y} \(\neq\) \texttt{0}.

For now, let's assume we have a special type \texttt{Rat} to represent
rational numbers and a constructor function

\begin{Shaded}
\begin{Highlighting}[]
\OtherTok{    makeRat ::} \DataTypeTok{Int} \OtherTok{->} \DataTypeTok{Int} \OtherTok{->} \DataTypeTok{Rat}
\end{Highlighting}
\end{Shaded}

to create a rational number instance from its numerator \texttt{x} and
denominator \texttt{y}. That is, \texttt{makeRat\ x\ y} constructs
rational number \(\frac{\texttt{x}}{\texttt{y}}\).

Further, let us assume we have selector functions \texttt{numer} and
\texttt{denom} with signatures

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{    numer,}\OtherTok{ denom ::} \DataTypeTok{Rat} \OtherTok{->} \DataTypeTok{Int}
\end{Highlighting}
\end{Shaded}

that each take a \texttt{Rat} argument and return the numerator and
denominator, respectively. That is, they satisfy the equalities:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{    numer (makeRat x y) }\FunctionTok{==}\NormalTok{ x}
\NormalTok{    denom (makeRat x y) }\FunctionTok{==}\NormalTok{ y}
\end{Highlighting}
\end{Shaded}

We consider how to implement rational numbers in Haskell later, but for
now let's look at rational arithmetic using the constructor and selector
functions above.

Given the knowledge of rational arithmetic from mathematics, we can
define the operations for unary negation, addition, subtraction,
multiplication, division, and equality.

\begin{Shaded}
\begin{Highlighting}[]
\OtherTok{    negRat ::} \DataTypeTok{Rat} \OtherTok{->} \DataTypeTok{Rat}
\NormalTok{    negRat x }\FunctionTok{=}\NormalTok{ makeRat (}\FunctionTok{-}\NormalTok{ numer x) (denom x)}

\NormalTok{    addRat, subRat, mulRat,}\OtherTok{ divRat ::} \DataTypeTok{Rat} \OtherTok{->} \DataTypeTok{Rat} \OtherTok{->} \DataTypeTok{Rat} 
\NormalTok{    addRat x y }\FunctionTok{=}\NormalTok{ makeRat (numer x }\FunctionTok{*}\NormalTok{ denom y }\FunctionTok{+}\NormalTok{ numer y }\FunctionTok{*}\NormalTok{ denom x) }
\NormalTok{                         (denom x }\FunctionTok{*}\NormalTok{ denom y)   }\CommentTok{-- x + y}
\NormalTok{    subRat x y }\FunctionTok{=}\NormalTok{ makeRat (numer x }\FunctionTok{*}\NormalTok{ denom y }\FunctionTok{-}\NormalTok{ numer y }\FunctionTok{*}\NormalTok{ denom x) }
\NormalTok{                         (denom x }\FunctionTok{*}\NormalTok{ denom y)   }\CommentTok{-- x - y}
\NormalTok{    mulRat x y }\FunctionTok{=}\NormalTok{ makeRat (numer x }\FunctionTok{*}\NormalTok{ numer y)}
\NormalTok{                         (denom x }\FunctionTok{*}\NormalTok{ denom y)   }\CommentTok{-- x * y}
\NormalTok{    divRat x y }\FunctionTok{=}\NormalTok{ makeRat (numer x }\FunctionTok{*}\NormalTok{ denom y)}
\NormalTok{                         (denom x }\FunctionTok{*}\NormalTok{ numer y)   }\CommentTok{-- x / y}

\OtherTok{    eqRat ::} \DataTypeTok{Rat} \OtherTok{->} \DataTypeTok{Rat} \OtherTok{->} \DataTypeTok{Bool} 
\NormalTok{    eqRat x y }\FunctionTok{=}\NormalTok{ (numer x) }\FunctionTok{*}\NormalTok{ (denom y) }\FunctionTok{==}\NormalTok{ (numer y) }\FunctionTok{*}\NormalTok{ (denom x)}
\end{Highlighting}
\end{Shaded}

Above we give the type signatures for all four functions in the same
type declaration by listing them separated by commas.

These functions all use the type \texttt{Rat}, constructor function
\texttt{makeRat}, and selector functions \texttt{numer} and
\texttt{denom} assumed above. They do not depend upon any specific
representation for rational numbers.

The above six functions work on rational numbers as a \emph{data
abstraction} defined by the type \texttt{Rat}, constructor function
\texttt{makeRat}, and selector functions \texttt{numer} and
\texttt{denom}.

The goal of a data abstraction is to separate the logical properties of
\emph{data} from the details of how the data are represented.

\hypertarget{rational-number-data-representation}{%
\subsubsection{Rational number data
representation}\label{rational-number-data-representation}}

Now, how can we represent rational numbers?

For this package, we define a type synonym \texttt{Rat} to denote this
type:

\begin{Shaded}
\begin{Highlighting}[]
    \KeywordTok{type} \DataTypeTok{Rat} \FunctionTok{=}\NormalTok{ (}\DataTypeTok{Int}\NormalTok{, }\DataTypeTok{Int}\NormalTok{) }
\end{Highlighting}
\end{Shaded}

For example, \texttt{(1,7)}, \texttt{(-1,-7)}, \texttt{(3,21)}, and
\texttt{(168,1176)} all represent \(\frac{\texttt{1}}{\texttt{7}}\).

As with any value that can be expressed in many different ways, it is
useful to define a single \emph{canonical} (or \emph{normal}) form for
representing values in the rational number type \texttt{Rat}.

It is convenient for us to choose a rational number representation
\texttt{(x,y)} that satisfies the following property, which we call an
\emph{invariant}:

\begin{quote}
\texttt{y\ \textgreater{}\ 0}, \texttt{x} and \texttt{y} are relatively
prime, and zero is denoted uniquely by \texttt{(0,1)}.
\end{quote}

By \emph{relatively prime}, we mean that the two integers have no common
divisors except 1.

By \emph{invariant}, we mean that the logical assertion always holds for
every rational number created by \texttt{makeRat} and manipulated only
by the operations in the \texttt{RationalCore} and \texttt{Rational}
modules.

This representation has the advantage that the magnitudes of the
numerator \texttt{x} and denominator \texttt{y} are kept small, thus
reducing problems with overflow arising during arithmetic operations.

We thus provide a function for constructing rational numbers in this
canonical form. We define constructor \texttt{makeRat} as follows.

\begin{Shaded}
\begin{Highlighting}[]
\OtherTok{    makeRat ::} \DataTypeTok{Int} \OtherTok{->} \DataTypeTok{Int} \OtherTok{->} \DataTypeTok{Rat}
\NormalTok{    makeRat x }\DecValTok{0} \FunctionTok{=}\NormalTok{ error ( }\StringTok{"Cannot construct a rational number "}
                               \FunctionTok{++}\NormalTok{ showRat (x,}\DecValTok{0}\NormalTok{) }\FunctionTok{++} \StringTok{"\textbackslash{}n"}\NormalTok{ ) }
\NormalTok{    makeRat }\DecValTok{0}\NormalTok{ _ }\FunctionTok{=}\NormalTok{ (}\DecValTok{0}\NormalTok{,}\DecValTok{1}\NormalTok{) }
\NormalTok{    makeRat x y }\FunctionTok{=}\NormalTok{ (x' }\OtherTok{`div`}\NormalTok{ d, y' }\OtherTok{`div`}\NormalTok{ d) }
        \KeywordTok{where}\NormalTok{ x' }\FunctionTok{=}\NormalTok{ (signum' y) }\FunctionTok{*}\NormalTok{ x }
\NormalTok{              y' }\FunctionTok{=}\NormalTok{ abs' y }
\NormalTok{              d  }\FunctionTok{=}\NormalTok{ gcd' x' y'}
\end{Highlighting}
\end{Shaded}

Above we use features of Haskell we have not used in the previous
examples:

\begin{itemize}
\item
  Instead of leaving the \texttt{(x,0)} case undefined, we define an
  explicit \texttt{error} call that returns a custom error message as a
  \texttt{String}.
\item
  To concatenate two strings, we use the infix \texttt{++} (read
  ``append'') operator. (We discuss \texttt{++} in the chapter on
  lists.)
\item
  Putting backticks (\texttt{\textasciigrave{}}) around an alphanumeric
  function name enables us to use that function as an infix operator.
  The function \texttt{div} denotes integer division. Above the
  \texttt{\textasciigrave{}div\textasciigrave{}} operator denotes the
  integer division function used in an infix manner.
\item
  The \texttt{where} clause introduces \texttt{x\textquotesingle{}},
  \texttt{y\textquotesingle{}}, and \texttt{d} as a local definitions
  within the body of \texttt{makeRat}. It can be called from within
  \texttt{makeRat} but not from outside the function. In contrast,
  \texttt{sqrtIter} in the Square Root example is at the same level as
  \texttt{sqrt\textquotesingle{}}, so it can be called by other
  functions (in the same Haskell module at least).

  The \texttt{where} feature allows us to introduce new definitions in a
  top-down manner--first using a symbol and then defining it.
\item
  Instead of defining the types of the local definitions
  \texttt{x\textquotesingle{}}, \texttt{y\textquotesingle{}}, and
  \texttt{d}, we use \emph{type inference}.

  These parameterless functions could be declared

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{    x', y',}\OtherTok{ d ::} \DataTypeTok{Int}
\end{Highlighting}
\end{Shaded}

  but it was not necessary because Haskell can infer the types from the
  types involved in their defining expressions.

  Type inference can be used more broadly in Haskell, but explicit type
  declarations should be used for any function called from outside.
\end{itemize}

The function \texttt{signum\textquotesingle{}} (similar to the more
general function \texttt{signum} in the Prelude) takes an integer and
returns the integer \texttt{-1}, \texttt{0}, or \texttt{1} when the
number is negative, zero, or positive, respectively.

\begin{Shaded}
\begin{Highlighting}[]
\OtherTok{    signum' ::} \DataTypeTok{Int} \OtherTok{->} \DataTypeTok{Int} 
\NormalTok{    signum' n }\FunctionTok{|}\NormalTok{ n }\FunctionTok{==} \DecValTok{0} \FunctionTok{=}  \DecValTok{0} 
              \FunctionTok{|}\NormalTok{ n }\FunctionTok{>} \DecValTok{0}  \FunctionTok{=}  \DecValTok{1} 
              \FunctionTok{|}\NormalTok{ n }\FunctionTok{<} \DecValTok{0}  \FunctionTok{=} \FunctionTok{-}\DecValTok{1} 
\end{Highlighting}
\end{Shaded}

The function \texttt{abs\textquotesingle{}} (similar to the more general
function \texttt{abs} in the Prelude) takes an integer and returns its
absolute value.

\begin{Shaded}
\begin{Highlighting}[]
\OtherTok{    abs' ::} \DataTypeTok{Int} \OtherTok{->} \DataTypeTok{Int}
\NormalTok{    abs' n }\FunctionTok{|}\NormalTok{ n }\FunctionTok{>=} \DecValTok{0} \FunctionTok{=}\NormalTok{  n}
           \FunctionTok{|}\NormalTok{ n }\FunctionTok{<}  \DecValTok{0} \FunctionTok{=} \FunctionTok{-}\NormalTok{n}
\end{Highlighting}
\end{Shaded}

The function \texttt{gcd\textquotesingle{}} (similar to the more general
function \texttt{gcd} in the Prelude) takes two integers and returns
their greatest common divisor.

\begin{Shaded}
\begin{Highlighting}[]
\OtherTok{    gcd' ::} \DataTypeTok{Int} \OtherTok{->} \DataTypeTok{Int} \OtherTok{->} \DataTypeTok{Int} 
\NormalTok{    gcd' x y }\FunctionTok{=}\NormalTok{ gcd'' (abs' x) (abs' y) }
        \KeywordTok{where}\NormalTok{ gcd'' x }\DecValTok{0} \FunctionTok{=}\NormalTok{ x }
\NormalTok{              gcd'' x y }\FunctionTok{=}\NormalTok{ gcd'' y (x }\OtherTok{`rem`}\NormalTok{ y) }
\end{Highlighting}
\end{Shaded}

Prelude operation \texttt{rem} returns the remainder from dividing its
first operand by its second.

Given \texttt{makeRat} defined as above, we can define \texttt{numer}
and \texttt{denom} as follows:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{    numer,}\OtherTok{ denom ::} \DataTypeTok{Rat} \OtherTok{->} \DataTypeTok{Int}
\NormalTok{    numer (x,_) }\FunctionTok{=}\NormalTok{ x}
\NormalTok{    denom (_,y) }\FunctionTok{=}\NormalTok{ y}
\end{Highlighting}
\end{Shaded}

Finally, to allow rational numbers to be displayed in the normal
fractional representation, we include function \texttt{showRat} in the
package. We use function \texttt{show}, found in the Prelude, here to
convert an integer to the usual string format and use the list operator
\texttt{++} to concatenate the two strings into one.

\begin{Shaded}
\begin{Highlighting}[]
\OtherTok{    showRat ::} \DataTypeTok{Rat} \OtherTok{->} \DataTypeTok{String}
\NormalTok{    showRat x }\FunctionTok{=}\NormalTok{ show (numer x) }\FunctionTok{++} \StringTok{"/"} \FunctionTok{++}\NormalTok{ show (denom x)}
\end{Highlighting}
\end{Shaded}

Unlike \texttt{Rat}, \texttt{makeRat}, \texttt{numer}, and
\texttt{denom}, function \texttt{showRat} (as implemented) does not use
knowledge of the data representation, but it is used by
\texttt{makeRat}. We could optimize it slightly by allowing it to access
the structure of the tuple directly.

\hypertarget{modularization}{%
\subsubsection{Modularization}\label{modularization}}

There are three groups of functions in this package:

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
  the six public rational arithmetic functions \texttt{negRat},
  \texttt{addRat}, \texttt{subRat}, \texttt{mulRat}, \texttt{divRat},
  and \texttt{eqRat}
\item
  the public type \texttt{Rat}, public constructor function
  \texttt{makeRat}, public selector functions \texttt{numer} and
  \texttt{denom}, and string conversion function \texttt{showRat}
\item
  the private utility functions called only by the second group, but
  just reimplementations of Prelude functions anyway
\end{enumerate}

As we have seen, \texttt{Rat}, \texttt{makeRat}, \texttt{numer},
\texttt{denom}, and \texttt{showRat} are the \emph{interface} to the
\emph{data abstraction} that hides the information about the
representation of the data. We can \emph{encapsulate} this group of
functions in a Haskell module as follows. This source code must also be
in a file named
\href{AbstractionCode/RationalCore.hs}{\texttt{RationalCore.hs}}.

\begin{Shaded}
\begin{Highlighting}[]
    \KeywordTok{module} \DataTypeTok{RationalCore}
\NormalTok{        (}\DataTypeTok{Rat}\NormalTok{, makeRat, numer, denom, showRat)}
    \KeywordTok{where}
        \CommentTok{-- Rat, makeRat, numer, denom, showRat definitions}
\end{Highlighting}
\end{Shaded}

We can encapsulate the utility functions in a separate module, which
would enable them to be used by several other modules.

However, given that the only use of the utility functions is within the
data representation module, we choose not to separate them at this time.
We leave them in the data abstraction module. Of course, we could also
eliminate them and use the corresponding Prelude functions directly.

Similarly, \texttt{negRat}, \texttt{addRat}, \texttt{subRat},
\texttt{mulRat}, \texttt{divRat}, and \texttt{eqRat} use the core data
abstraction and, in turn, extend the interface to include rational
number arithmetic operations. We can encapsulate these in another
Haskell module that imports the module giving the data representation.
This module must be in a file named
\href{AbstractionCode/Rational.hs}{\texttt{Rational.hs}}.

\begin{Shaded}
\begin{Highlighting}[]
    \KeywordTok{module} \DataTypeTok{Rational} 
\NormalTok{        ( }\DataTypeTok{Rat}\NormalTok{, makeRat, numer, denom, showRat, }\CommentTok{-- from RatioalCore}
\NormalTok{        negRat, addRat, subRat, mulRat, divRat, eqRat )}
    \KeywordTok{where}
        \KeywordTok{import} \DataTypeTok{RationalCore}
        \CommentTok{-- negRat, addRat, subRat, mulRat, divRat, eqRat definitions }
\end{Highlighting}
\end{Shaded}

Other modules that use the rational number package can import module
\texttt{Rational}.

This modular approach to program design and implementation offers the
potential of scalability and robustness with respect to change.

The key to this \emph{information-hiding} approach to design is to
identify the aspects of a software system that are most likely to change
from one version to another and make each a design \emph{secret} of some
module.

The secret of the \texttt{RationalCore} module is the rational number
data representation used. The secret of the \texttt{Rational} module
itself is the methods used for rational number arithmetic.

\hypertarget{alternative-rational-number-data-representation}{%
\subsubsection{Alternative rational number data
representation}\label{alternative-rational-number-data-representation}}

In the rational number data representation above, constructor
\texttt{makeRat} creates pairs in which the two integers are relatively
prime and the sign is on the numerator. Selector functions
\texttt{numer} and \texttt{denom} just return these stored values.

An alternative representation is to reverse this approach, as shown in
the following module (in file
\href{AbstractionCode/RationalDeferGCD.hs}{\texttt{RationalDeferGCD.hs}}.)

\begin{Shaded}
\begin{Highlighting}[]
    \KeywordTok{module} \DataTypeTok{RationalDeferGCD}
\NormalTok{        (}\DataTypeTok{Rat}\NormalTok{, makeRat, numer, denom, showRat)}
    \KeywordTok{where}

    \KeywordTok{type} \DataTypeTok{Rat} \FunctionTok{=}\NormalTok{ (}\DataTypeTok{Int}\NormalTok{,}\DataTypeTok{Int}\NormalTok{)}

\OtherTok{    makeRat ::} \DataTypeTok{Int} \OtherTok{->} \DataTypeTok{Int} \OtherTok{->} \DataTypeTok{Rat}
\NormalTok{    makeRat x }\DecValTok{0} \FunctionTok{=}\NormalTok{ error ( }\StringTok{"Cannot construct a rational number "}
                          \FunctionTok{++}\NormalTok{ showRat (x,}\DecValTok{0}\NormalTok{) }\FunctionTok{++} \StringTok{"\textbackslash{}n"}\NormalTok{ ) }
\NormalTok{    makeRat }\DecValTok{0}\NormalTok{ y }\FunctionTok{=}\NormalTok{ (}\DecValTok{0}\NormalTok{,}\DecValTok{1}\NormalTok{) }
\NormalTok{    makeRat x y }\FunctionTok{=}\NormalTok{ (x,y)}

\OtherTok{    numer ::} \DataTypeTok{Rat} \OtherTok{->} \DataTypeTok{Int}
\NormalTok{    numer (x,y) }\FunctionTok{=}\NormalTok{ x' }\OtherTok{`div`}\NormalTok{ d}
        \KeywordTok{where}\NormalTok{ x' }\FunctionTok{=}\NormalTok{ (signum' y) }\FunctionTok{*}\NormalTok{ x }
\NormalTok{              y' }\FunctionTok{=}\NormalTok{ abs' y }
\NormalTok{              d  }\FunctionTok{=}\NormalTok{ gcd' x' y'}

\OtherTok{    denom ::} \DataTypeTok{Rat} \OtherTok{->} \DataTypeTok{Int}
\NormalTok{    denom (x,y) }\FunctionTok{=}\NormalTok{ y' }\OtherTok{`div`}\NormalTok{ d}
        \KeywordTok{where}\NormalTok{ x' }\FunctionTok{=}\NormalTok{ (signum' y) }\FunctionTok{*}\NormalTok{ x }
\NormalTok{              y' }\FunctionTok{=}\NormalTok{ abs' y }
\NormalTok{              d  }\FunctionTok{=}\NormalTok{ gcd' x' y'}

\OtherTok{    showRat ::} \DataTypeTok{Rat} \OtherTok{->} \DataTypeTok{String}
\NormalTok{    showRat x }\FunctionTok{=}\NormalTok{ show (numer x) }\FunctionTok{++} \StringTok{"/"} \FunctionTok{++}\NormalTok{ show (denom x)}
\end{Highlighting}
\end{Shaded}

This approach defers the calculation of the greatest common divisor
until a selector is called.

The invariant for this rational number representation requires that, for
\texttt{(x,y)},

\begin{quote}
\texttt{y} \(\neq\) \texttt{0} and zero is represented uniquely by
\texttt{(0,1)}.
\end{quote}

Furthermore, function \texttt{numer} and \texttt{denom} satisfy the
equalities

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{    numer (makeRat x y) }\FunctionTok{==}\NormalTok{ x'}
\NormalTok{    denom (makeRat x y) }\FunctionTok{==}\NormalTok{ y'}
\end{Highlighting}
\end{Shaded}

where \texttt{y\textquotesingle{}\ \textgreater{}\ 0},
\texttt{x\textquotesingle{}} and \texttt{y\textquotesingle{}} are
relatively prime, and
\(\frac{\texttt{x}}{\texttt{y}} = \frac{\texttt{x'}}{\texttt{y'}}\).

Question:

\begin{quote}
What are the advantages and disadvantages of the two data
representations?
\end{quote}

Like module \texttt{RationalCore}, the design secret for module
\texttt{RationalDeferGCD} is the rational number data representation.

Regardless of which approach is used, the definitions of the arithmetic
and comparison functions do not change. Thus the \texttt{Rational}
module can import data representation module \texttt{RationalCore} or
\texttt{RationalDeferGCD}.

Figure 1 shows the dependencies among the modules we have examined in
the rational arithmetic case study.

\begin{figure}
\centering
\includegraphics{RationalModDep.png}
\caption{\textbf{Figure 1. Module Dependencies for Rational Arithmetic
Case Study}}
\end{figure}

We can consider the \texttt{RationalCore} and \texttt{RationalDeferGCD}
modules as two concrete instances (Haskell \texttt{module}s) of a more
abstract module we call \texttt{RationalRep} in the diagram.

The module \texttt{Rational} relies on the abstract module
\texttt{RationalRep} for an implementation of rational numbers. In the
Haskell code above, there are really two versions of the Haskell module
\texttt{Rational} that differ only in whether they import
\texttt{RationalCore} or \texttt{RationalDeferGCD}.

We could also replace alias \texttt{Rat} by a user-defined type to get
another alternative definition of \texttt{RationalRep}, as long as the
interface functions do not have to work with types other than
\texttt{Int}.

\hypertarget{modular-design-and-programming}{%
\subsection{Modular Design and
Programming}\label{modular-design-and-programming}}

Now let's step back from the rational arithmetic case study and consider
the general issues of data abstraction and modular design and
programming.

Software engineering pioneer David Parnas defines a \emph{module} as ``a
work assignment given to a programmer or group of programmers''
{[}Parnas 1978{]}. This is a \emph{software engineering} view of a
module.

In a programming language like Haskell, a \texttt{module} is also a
program unit defined with a construct or convention. This is a
\emph{programming language} view of a module.

Ideally, a language's module features should support the software
engineering module methods.

\hypertarget{information-hiding-modules}{%
\subsubsection{Information-hiding
modules}\label{information-hiding-modules}}

According to Parnas, the goals of \emph{modular design} are to {[}Parnas
1972{]}:

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
  enable programmers to understand the system by focusing on one module
  at a time (i.e., \emph{comprehensibility}).
\item
  shorten development time by minimizing required communication among
  groups (i.e., \emph{independent development}).
\item
  make the software system flexible by limiting the `number of modules
  affected by significant changes (i.e., \emph{changeability}).
\end{enumerate}

Parnas advocates the use of a principle called \emph{information hiding}
to guide decomposition of a system into appropriate modules (i.e.~work
assignments). He points out that the connections among the modules
should have as few information requirements as possible {[}Parnas
1972{]}.

In the Parnas approach, an information-hiding module:

\begin{itemize}
\item
  forms a \emph{cohesive} unit of functionality \emph{separate} from
  other modules
\item
  \emph{hides} a design decision (its \emph{secret}) from other modules
\item
  \emph{encapsulates} an aspect of system likely to change (its secret)
\end{itemize}

Aspects likely to change independently of each other become secrets of
separate modules. Aspects unlikely to change can become interactions
(connections) among modules.

This approach supports the goal of changeability (goal 2). When care is
taken to design the modules as clean abstractions with well-defined and
documented interfaces, the approach also supports the goals of
independent development (goal 1) and comprehensibility (goal 3).

Information hiding has been absorbed into the dogma of contemporary
object-oriented programming. However, information hiding is often
oversimplified as merely hiding the data and their representations
{[}Weiss 2001{]}.

The secret of a well-designed module may be much more than that. It may
include such knowledge as a specific functional requirement stated in
the requirements document, the processing algorithm used, the nature of
external devices accessed, or even the presence or absence of other
modules or programs in the system {[}Parnas 1972, 1979, 1985{]}. These
are important aspects that may change as the system evolves.

\hypertarget{interfaces}{%
\subsubsection{Interfaces}\label{interfaces}}

It is important for information-hiding modules to have well-defined and
stable interfaces.

According to Britton et al, an \emph{interface} is a ``set of
assumptions \ldots{} each programmer needs to make about the other
program \ldots{} to demonstrate the correctness of his own program''
{[}Britton 1981{]}.

An interface includes the type signature of each function (i.e., name,
arguments, and return value) and the constraints on the environment and
argument values (e.g., the invariants).

An \emph{abstract interface} is an interface that does not change when
one module implementation is substituted for another {[}Britton 1981;
Parnas 1978{]}. It concentrates on module's essential aspects and
obscures incidental aspects that vary among implementations.

Information-hiding modules and abstract interfaces enable us to design
and build software systems with multiple versions. The
information-hiding approach seeks to identify aspects of a software
design that might change from one version to another and to hide them
within independent modules behind well-defined abstract interfaces.

We can reuse the software design across several similar systems. We can
reuse an existing module implementation when appropriate. When we need a
new implementation, we can create one by following the specification of
the module's abstract interface.

\hypertarget{haskell-information-hiding-modules}{%
\subsubsection{Haskell information-hiding
modules}\label{haskell-information-hiding-modules}}

As we have seen, in Haskell the \texttt{module} construct can be used to
encapsulate an information-hiding module. The features exported form
part of the interface to the module. One module can \texttt{import}
another module, specifying its dependence on the interface of the other
module.

We define each Haskell module in a separate file. The Haskell compiler
can compile a module independently of others except that the modules it
depends on must be previously compiled. The Haskell build and package
management tools \texttt{cabal-install} and \texttt{stack} support
Haskell modules as their primary units.

The interface of a Haskell module consists of the names and type
signatures of its exported types and functions plus the constraints on
the functions and the expected properties of the ``objects''
manipulated.

In the Rational Arithmetic case study, we defined two information-hiding
modules:

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
  ``RationalRep'', whose secret is how to represent the rational number
  data and whose interface consists of the data type \texttt{Rat},
  operations (functions) \texttt{makeRat}, \texttt{numer},
  \texttt{denom}, and \texttt{showRat}, and the constraints on these
  types and functions
\item
  ``Rational'', whose secret is how to implement the rational number
  arithmetic and whose interface consists of operations (functions)
  \texttt{negRat}, \texttt{addRat}, \texttt{subRat}, \texttt{mulRat},
  \texttt{divRat}, and \texttt{eqRat}, the other module's interface, and
  the constraints on these types and functions
\end{enumerate}

We developed two distinct Haskell modules, \texttt{RationalCore} and
\texttt{RationalDeferGCD}, to implement the ``RationalRep''
information-hiding module. We developed one distinct Haskell module,
\texttt{Rational}, to implement the ``Rational'' information-hiding
module. Haskell module \texttt{Rational} can be paired (i.e., by chaning
the \texttt{import} statement) either of the other two variants of
``RationalRep''.

Unfortunately, Haskell 2010 has a relatively weak module system that
does not support multiple implementations as well as we might like.
There is no way to declare that multiple Haskell modules have the same
interface other than copying the common code into each module and
documenting the interface carefully. We must also have multiple versions
of \texttt{Rational} that differ only in which other module is imported.

Together the Glasgow Haskell Compiler (GHC) release 8.2 (July 2017) and
the Cabal-Install package manager release 2.0 (August 2017) support a
new extension, the Backpack mixin package system. This new system
remedies the above shortcoming. In this new approach, we would define
the abstract module ``RationalRep'' as a signature file and require that
\texttt{RationalCore} and \texttt{RationalDeferGCD} conform to it.

Further discussion of this new module system is beyond the scope of this
chapter.

\hypertarget{invariants}{%
\subsubsection{Invariants}\label{invariants}}

As we saw in the Rational Arithmetic case study, a module that provides
a data abstraction must ensure that the objects it creates and
manipulates maintain their integrity--always have a valid structure and
state. An invariant for the data abstraction can help us design and
implement such objects.

\begin{description}
\item[\textbf{Invariant}:]
A logical assertion that must always true for every ``object'' created
by the public constructors and manipulated only by the public operations
of the data abstraction.
\end{description}

Often, we separate an invariant into two parts.

\begin{description}
\item[\textbf{Interface invariant}:]
An invariant stated in terms of the public features and abstract
properties of the ``object''.
\item[\textbf{Implementation (representation) invariant}:]
A detailed invariant giving the required relationships among the
internal features of the implementation of an ``object''
\end{description}

An interface invariant is a key aspect of the abstract interface of a
module. It is useful to the users of the module, as well to the
developers.

In the Rational Arithmetic case study, the interface invariant for the
``RationalRep'' abstract module is the following.

\begin{quote}
For all integers \texttt{x} and nonzero integers \texttt{y},
\end{quote}

\begin{verbatim}
~~~{.haskell}
    numer (makeRat x y) == x' 
    denom (makeRat x y) == y' 
~~~
\end{verbatim}

\begin{quote}
where \texttt{y\textquotesingle{}\ \textgreater{}\ 0},
\texttt{x\textquotesingle{}} and \texttt{y\textquotesingle{}} are
relatively prime,
\(\frac{\texttt{x}}{\texttt{y}} = \frac{\texttt{x'}}{\texttt{y'}}\) and
if \texttt{x\textquotesingle{}} = 0, then \texttt{y\textquotesingle{}} =
1.
\end{quote}

An implementation invariant guides the developers in the design and
implementation of the internal details of a module. It relates the
internal details to the interface invariant.

We can state an implementation invariant for the \texttt{RationalCore}
module as follows.

\begin{quote}
For all integers \texttt{x} and nonzero integers \texttt{y},
\end{quote}

\begin{verbatim}
~~~{.haskell}
    makeRat x y == (x',y')
~~~
\end{verbatim}

\begin{quote}
where \texttt{y\textquotesingle{}\ \textgreater{}\ 0},
\texttt{x\textquotesingle{}} and \texttt{y\textquotesingle{}} are
relatively prime,
\(\frac{\texttt{x}}{\texttt{y}} = \frac{\texttt{x'}}{\texttt{y'}}\) and
if \texttt{x\textquotesingle{}} = 0, then \texttt{y\textquotesingle{}} =
1.
\end{quote}

The implementation invariant implies the interface invariant. Although
\texttt{makeRat} does quite a bit of work, \texttt{numer} and
\texttt{denom} are simple.

We can state an implementation invariant for the
\texttt{RationalDeferGCD} module as follows.

\begin{quote}
For all integers \texttt{x} and nonzero integers \texttt{y},
\end{quote}

\begin{verbatim}
~~~{.haskell}
    makeRat x y == (x,y)
~~~
\end{verbatim}

In this module implementation, \texttt{makeRat} is trivial, thus
\texttt{numer} and \texttt{denom} must do most of the work to establish
the interface invariant.

We will return to the invariant concepts in later chapters.

\hypertarget{design-criteria-for-interfaces}{%
\subsubsection{Design criteria for
interfaces}\label{design-criteria-for-interfaces}}

What makes a good interface for an information-hiding module?

In designing an interface for a module, we should also consider the
following criteria. Of course, some of these criteria conflict with one
another; a designer must carefully balance the criteria to achieve a
good interface design.

Note: These are general principles; they are not limited to Haskell or
functional programming. In object-oriented languages, these criteria
apply to class interfaces.

\begin{itemize}
\item
  \textbf{Cohesion:} All operations must logically fit together to
  support a single, coherent purpose. The module should describe a
  single abstraction.
\item
  \textbf{Simplicity:} Avoid needless features. The smaller the
  interface the easier it is to use the module.
\item
  \textbf{No redundancy:} Avoid offering the same service in more than
  one way; eliminate redundant features.
\item
  \textbf{Atomicity:} Do not combine several operations if they are
  needed individually. Keep independent features separate. All
  operations should be \emph{primitive}, that is, not be decomposable
  into other operations also in the public interface.
\item
  \textbf{Completeness:} All primitive operations that make sense for
  the abstraction should be supported by the module.
\item
  \textbf{Consistency:} Provide a set of operations that are internally
  consistent in

  \begin{itemize}
  \tightlist
  \item
    naming convention (e.g., in use of prefixes like ``set'' or ``get'',
    in capitalization, in use of verbs/nouns/adjectives),
  \item
    use of arguments and return values (e.g., order and type of
    arguments),
  \item
    behavior (i.e., make operations work similarly).
  \end{itemize}

  Avoid surprises and misunderstandings. Consistent interfaces make it
  easier to understand the rest of a system if part of it is already
  known.

  The operations should be consistent with good practices for the
  specific language being used.
\item
  \textbf{Reusability:} Do not customize modules to specific clients,
  but make them general enough to be reusable in other contexts.
\item
  \textbf{Robustness with respect to modifications:} Design the
  interface of an module so that it remains stable even if the
  implementation of the module changes. (That is, it should be an
  abstract interface for an information-hiding module as we discussed
  above.)
\item
  \textbf{Convenience:} Where appropriate, provide additional operations
  (e.g., beyond the complete primitive set) for the convenience of users
  of the module. Add convenience operations only for frequently used
  combinations after careful study.
\end{itemize}

We must trade off conflicts among the criteria. For example, we must
balance:

\begin{itemize}
\tightlist
\item
  completeness versus simplicity
\item
  reusability versus simplicity
\item
  convenience versus consistency, simplicity, no redundancy, and
  atomicity
\end{itemize}

We must also balance these design criteria against efficiency and
functionality.

\hypertarget{exercises}{%
\subsection{Exercises}\label{exercises}}

TODO: Add more exercises for the techniques and features introduced in
this section. Make sure what is here still make sense.

For each of the following exercises, develop and test a Haskell function
or set of functions.

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
  Develop a Haskell module (or modules) for line segments on the
  two-dimensional coordinate plane using the \emph{rectangular
  coordinate} system.

  We can represent a line segment with two points--the starting point
  and the ending point. Develop the following Haskell functions:

  \begin{itemize}
  \item
    constructor \texttt{newSeg} that takes two points and returns a new
    line segment
  \item
    selectors \texttt{startPt} and \texttt{endPt} that each take a
    segment and return its starting and ending points, respectively
  \end{itemize}

  We normally represent the plane with a \emph{rectangular coordinate}
  system. That is, we use two axes--an \texttt{x} \emph{axis} and a
  \texttt{y} \emph{axis}--intersecting at a right angle. We call the
  intersection point the \emph{origin} and label it with 0 on both axes.
  We normally draw the \texttt{x} axis horizontally and label it with
  increasing numbers to the right and decreasing numbers to the left. We
  also draw the \texttt{y} axis vertically with increasing numbers
  upward and decreasing numbers downward. Any point in the plane is
  uniquely identified by its \texttt{x}-coordinate and
  \texttt{y}-coordinate.

  Define a data representation for points in the rectangular coordinate
  system and develop the following Haskell functions:

  \begin{itemize}
  \item
    constructor \texttt{newPtFromRect} that takes the \texttt{x} and
    \texttt{y} coordinates of a point and returns a new point
  \item
    selectors \texttt{getx} and \texttt{gety} that takes a point and
    returns the \texttt{x} and \texttt{y} coordinates, respectively
  \item
    display function \texttt{showPt} that takes a point and returns an
    appropriate \texttt{String} representation for the point
  \end{itemize}

  Now, using the various constructors and selectors, also develop the
  Haskell functions for line segments:

  \begin{itemize}
  \item
    \texttt{midPt} that takes a line segment and returns the point at
    the middle of the segment
  \item
    display function \texttt{showSeg} that takes a line segment and
    returns an appropriate \texttt{String} representation
  \end{itemize}

  Note that \texttt{newSeg}, \texttt{startPt}, \texttt{endPt},
  \texttt{midPt}, and \texttt{showSeg} can be implemented independently
  from how the points are represented.
\item
  Develop a Haskell module (or modules) for line segments that
  represents points using the \emph{polar coordinate} system instead of
  the rectangular coordinate system used in the previous exercise.

  A polar coordinate system represents a point in the plane by its
  \emph{radial coordinate} \texttt{r} (i.e., the distance from the
  \emph{pole}) and its \emph{angular coordinate} \texttt{t} (i.e., the
  angle from the \emph{polar axis} in the reference direction). We
  sometimes call \texttt{r} the \emph{magnitude} and \texttt{t} the
  \emph{angle}.

  By convention, we align the rectangular and polar coordinate systems
  by making the origin the pole, the positive portion of the \texttt{x}
  axis the polar axis, and let the first quadrant (where both \texttt{x}
  and \texttt{y} are positive) be the smallest positive angles in the
  reference direction. That is, with a traditional drawing of the
  coordinate systems, we measure and the radial coordinate \texttt{r} as
  the distance from the origin measure the angular coordinate \texttt{t}
  counterclockwise from the positive \texttt{x} axis.

  Using knowledge of trigonometry, we can convert among rectangular
  coordinates \texttt{(x,y)} and polar coordinates \texttt{(r,t)} using
  the equations:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{    x }\FunctionTok{=}\NormalTok{ r }\FunctionTok{*}\NormalTok{ cos(t)}
\NormalTok{    y }\FunctionTok{=}\NormalTok{ r }\FunctionTok{*}\NormalTok{ sin(t)}
\NormalTok{    r }\FunctionTok{=}\NormalTok{ sqrt(x}\FunctionTok{^}\DecValTok{2} \FunctionTok{+}\NormalTok{ y}\FunctionTok{^}\DecValTok{2}\NormalTok{)}
\NormalTok{    t }\FunctionTok{=}\NormalTok{ arctan2(y,x)}
\end{Highlighting}
\end{Shaded}

  Define a data representation for points in the polar coordinate system
  and develop the following Haskell functions:

  \begin{itemize}
  \item
    constructor \texttt{newPtFromPolar} that takes the magnitude
    \texttt{r} and angle \texttt{t} as the polar coordinates of a point
    and returns a new point
  \item
    selectors \texttt{getMag} and \texttt{getAng} that each take a point
    and return the magnitude \texttt{r} and angle \texttt{t}
    coordinates, respectively
  \item
    selectors \texttt{getx} and \texttt{gety} that return the \texttt{x}
    and \texttt{y} components of the points (represented here in polar
    coordinates)
  \item
    display functions \texttt{showPtAsRect} and \texttt{showPtAsPolar}
    to convert the points to strings using rectangular and polar
    coordinates, respectively,
  \end{itemize}

  Functions \texttt{newSeg}, \texttt{startPt}, \texttt{endPt},
  \texttt{midPt}, and \texttt{showSeg} should work as in the previous
  exercise.
\item
  Modify the solutions to the previous two line-segment module exercises
  to enable the line segment functions to be in one module that works
  properly if composed with either of the two data representation
  modules. (The solutions may have already done this.)
\item
  Modify the solution to the previous line-segment exercise to use the
  Backpack module system.
\item
  Modify the modules in the previous exercise to enable the line segment
  module to work with both data representations in the same program.
\item
  Modify the solution to the Rational Arithmetic case study to use the
  Backpack module system.
\end{enumerate}

\hypertarget{acknowledgements}{%
\subsection{Acknowledgements}\label{acknowledgements}}

I adapted and revised much of this work in Summer and Fall 2016 from my
previous materials.

\begin{itemize}
\item
  Using Top-Down Stepwise Refinement (square root module) from my
  earlier implementations of this algorithm in Scala, Elixir, and Lua
  and from section 1.1.7 of Abelson and Sussman's
  \href{http://mitpress.mit.edu/sicp/}{\emph{Structure and
  Interpretation of Computer Programs}}
\item
  Using Data Abstraction (rational arithmetic module) mostly from
  chapter 5 of my \emph{Notes on Functional Programming with Haskell},
  from my Lua-based implementations, and from section 2.1 of Abelson and
  Sussman's \href{http://mitpress.mit.edu/sicp/}{\emph{Structure and
  Interpretation of Computer Programs}}
\item
  Modular Design and Programming from my Data Abstraction and Modular
  Design notes, which draw from the ideas of several of the references
  listed below
\end{itemize}

In 2017, I continued to develop this work as parts of chapters 1 and 2
of a draft textbook. In Spring 2018, I separated this material back into
a more focused new chapter on abstraction.

I maintain these notes as text in Pandoc's dialect of Markdown using
embedded LaTeX markup for the mathematical formulas and then translate
the notes to HTML, PDF, and other forms as needed.

\hypertarget{references}{%
\subsection{References}\label{references}}

\begin{description}
\tightlist
\item[{[}Abelson-Sussman 1996{]}]
Harold Abelson and Gerald Jay Sussman. \emph{Structure and
Interpretation of Computer Programs}
(\href{http://mitpress.mit.edu/sicp/}{SICP}), Second Edition, MIT Press,
1996.
\item[{[}Bird-Wadler 1998{]}]
Richard Bird and Philip Wadler. \emph{Introduction to Functional
Programming}, Second Edition, Addison Wesley, 1998.
{[}\href{https://usi-pl.github.io/lc/sp-2015/doc/Bird_Wadler.\%20Introduction\%20to\%20Functional\%20Programming.1ed.pdf}{First
Edition, 1988}{]}
\item[{[}Britton 1981{]}]
K. H. Britton, R. A. Parker, and D. L. Parnas. ``A Procedure for
Designing Abstract Interfaces for Device Interface Modules,'' In
\emph{Proceedings of the 5th International Conference on Software
Engineering}, pp.~195-204, March 1981.
\item[{[}Chiusano-Bjarnason 2015{]}{]}]
Paul Chiusano and Runar Bjarnason, \emph{Functional Programming in
Scala}, Manning, 2015.
\item[{[}Cunningham 2001{]}]
H. Conrad Cunningham and Jingyi Wang. Building a Layered Framework for
the Table Abstraction, In \emph{Proceedings of the ACM Symposium on
Applied Computing}, pp.~668-674, March 2001.
\item[{[}Cunningham 2004{]}]
H. Conrad Cunningham, Cuihua Zhang, and Yi Liu. Keeping Secrets within a
Family: Rediscovering Parnas, In \emph{Proceedings of the International
Conference on Software Engineering Research and Practice (SERP)},
pp.~712-718, CSREA Press, June 2004.
\item[{[}\href{localcopy/Cunningham_Table_Abstraction.pdf}{Cunningham
2010}{]}]
H. Conrad Cunningham, Yi Liu, and Jingyi Wang. Designing a Flexible
Framework for a Table Abstraction, In Y. Chan, J. Talburt, and T.
Talley, editors, Data Engineering: Mining, Information, and
Intelligence, pp.~279-314, Springer, 2010.
\item[{[}Cunningham 2014{]}]
H. Conrad Cunningham.
\href{https://john.cs.olemiss.edu/~hcc/csci450/notes/haskell_notes.pdf}{\emph{Notes
on Functional Programming with Haskell}}, 1994-2014.
\item[{[}Dale-Walker 1996{]}]
Nell Dale and Henry M. Walker. \emph{Abstract Data Types:
Specifications, Implementations, and Applications}, D. C. Heath, 1996.
(Especially chapter 1 on ``Abstract Specification Techniques'')
\item[{[}Horstmann 1995{]}]
Cay S. Horstmann. \emph{Mastering Object-Oriented Design in C++}, Wiley,
1995. (Especially chapters 3-6 on ``Implementing Classes'',
``Interfaces'', ``Object-Oriented Design'', and ``Invariants'')
\item[{[}Meyer 1997{]}]
Bertrand Meyer. \emph{Object-Oriented Program Construction}, Second
Edition, Prentice Hall, 1997. (Especially chapter 6 on ``Abstract Data
Types'' and chapter 11 on ``Design by Contract'')
\item[{[}Mossenbock 1995{]}]
Hanspeter Mossenbock. \emph{Object-Oriented Programming in Oberon-2},
Springer-Verlag, 1995. (Especially chapter 3 on ``Data Abstraction'')
\item[{[}Parnas 1972{]}]
D. L. Parnas. ``On the Criteria to Be Used in Decomposing Systems into
Modules,'' \emph{Communications of the ACM}, Vol. 15, No.~12,
pp.1053-1058, 1972.
\item[{[}Parnas 1976{]}]
D. L. Parnas. ``On the Design and Development of Program Families,''
\emph{IEEE Transactions on Software Engineering}, Vol. SE-2, No.~1,
pp.~1-9, March 1976.
\item[{[}Parnas 1978{]}]
D. L. Parnas. ``Some Software Engineering Principles,'' \emph{Infotech
State of the Art Report on Structured Analysis and Design}, Infotech
International, 10 pages, 1978. Reprinted in \emph{Software Fundamentals:
Collected Papers by David L. Parnas}, D. M. Hoffman and D. M. Weiss,
editors, Addison-Wesley, 2001.
\item[{[}Parnas 1979{]}]
D. L. Parnas. ``Designing Software for Ease of Extension and
Contraction,'' \emph{IEEE Transactions on Software Engineering}, Vol.
SE-5, No.~1, pp.~128-138, March 1979.
\item[{[}Parnas 1985{]}]
D. L. Parnas, P. C. Clements, and D. M. Weiss. ``The Modular Structure
of Complex Systems,'' \emph{IEEE Transactions on Software Engineering},
Vol. SE-11, No.~3, pp.~259-266, March 1985.
\item[{[}Thompson 2011{]}]
Simon Thompson. \emph{Haskell: The Craft of Programming, Third Edition},
Pearson, 2011.
\item[{[}Weiss 2001{]}]
D. M. Weiss. ``Introduction: On the Criteria to Be Used in Decomposing
Systems into Modules,'' In \emph{Software Fundamentals: Collected Papers
by David L. Parnas}, D. M. Hoffman and D. M. Weiss, editors,
Addison-Wesley, 2001.
\end{description}

\hypertarget{terms-and-concepts}{%
\subsection{Terms and Concepts}\label{terms-and-concepts}}

Procedural abstraction, top-down stepwise refinement, abstract code,
termination condition for recursion, Newton's method, Haskell
\texttt{module}, module exports and imports, rational number arithmetic,
data abstraction, properties of data, data representation, invariant,
interface invariant, implementation or representation invariant,
canonical or normal forms, information hiding, module secret,
encapsulation, interface, abstract interface, design criteria for
interfaces, software reuse, Haskell \texttt{where} local definition,
type inference, use of Haskell modules to implement information-hiding
modules rational number arithmetic, data abstraction, abstract
properties of data, data representation, invariant, interface invariant,
implementation or representation invariant, canonical or normal forms,
information hiding, module secret, encapsulation, interface, abstract
interface, design criteria for interfaces, software reuse, Haskell
\texttt{where} local definition, type inference, use of Haskell modules
to implement information-hiding modules

\end{document}
