% mrh 19/1 2013
% $Header: /cvsroot/latex-beamer/latex-beamer/solutions/generic-talks/generic-ornate-15min-45min.en.tex,v 1.5 2007/01/28 20:48:23 tantau Exp $
\documentclass[9pt,xcolor=dvipsnames]{beamer}
%\documentclass[handout,9pt,xcolor=dvipsnames]{beamer}


\mode<presentation>
{
  \usetheme{DTU}
  % or ...


  \setbeamercovered{transparent}

  % or whatever (possibly just delete it)
}


\usepackage[english]{babel}
% or whatever

\usepackage[latin1]{inputenc}
% or whatever

\usepackage{palatino}
%\usepackage[T1]{fontenc}
\usepackage{mrhalltt}
\usepackage{itemlist}
%\usepackage{fix-cm}

\usepackage{epsf,epsfig}

\input{macros}

% Or whatever. Note that the encoding and the font should match. If T1
% does not look nice, try deleting the line with the fontenc.

%\usepackage{pgfpages}
%\pgfpagesuselayout{4 on 1}[letterpaper,landscape,bordershrink=5mm]

\title % (optional, use only with long paper titles)
{02157 Functional Programming}

\subtitle
{Lecture 9: Module System -- briefly} % (optional)

\author% (optional, use only with lots of authors)
{Michael~R.~Hansen}
% - Use the \inst{?} command only if the authors have different
%   affiliation.

\institute % (optional, but mostly needed)
{DTU Informatics, Technical University of Denmark}

\date {MRH 08/11/2012}% (optional)
%{Advanced Topics on Embedded Systems}

%\subject{Talks}
% This is only inserted into the PDF information catalog. Can be left
% out.



% If you have a file called "university-logo-filename.xxx", where xxx
% is a graphic format that can be processed by latex or pdflatex,
% resp., then you can add a logo as follows:

 \pgfdeclareimage[height=.75cm]{university-logo}{DTUlogo}
 \logo{\pgfuseimage{university-logo}}

 \pgfdeclareimage[height=3.3cm]{frise}{DTUfrise}
 \pgfdeclareimage[height=.8cm]{dept}{DTUInformatics}


% If you wish to uncover everything in a step-wise fashion, uncomment
% the following command:

%\beamerdefaultoverlayspecification{<+->}



\begin{document}

\begin{frame}
  \titlepage
%  \begin{tabular}{p{5cm}p{10cm}}
%    &\pgfuseimage{frise}\\
%    \pgfuseimage{dept} &
%  \end{tabular}

\end{frame}

%\begin{frame}{Outline}
%  \tableofcontents
  % You might wish to add the option [pausesections]
%\end{frame}


\begin{frame}{Overview}

\begin{itemize}

\item<1-> Supports modular program design including

\begin{itemize}
\item encapsulation

\item abstraction and

\item reuse of software components.

\end{itemize}


\item<2-> A module is characterized by:

\begin{itemize}
\item a {\em\redbrown signature} -- an interface specifications and

\item a matching {\em\redbrown implementation} -- containing declarations of the interface specifications.

\end{itemize}


\item<3-> Example based (incomplete) presentation to give the flavor.
\end{itemize}

\medskip
\pause[4]

Sources:
\begin{itemize} \item Chapter 7: Modules. (A fast reading suffices.)

\end{itemize}
\end{frame}


\begin{frame}[fragile]
\frametitle{An example: Search trees}

Consider the following implementation of search trees:
\begin{verbatim}
   type Tree = Lf
             | Br of Tree*int*Tree;;

let rec insert i = function
  | Lf                ->  Br(Lf,i,Lf)
  | Br(t1,j,t2) as tr ->
      match compare i j with
      | 0           -> tr
      | n when n<0  -> Br(insert i t1 , j, t2)
      | _           -> Br(t1,j, insert i t2);;


let rec memberOf  i = function
  | Lf          -> false
  | Br(t1,j,t2) -> match compare i j with
                   | 0   -> true
                   | n when n<0 -> memberOf i t1
                   | _          -> memberOf i t2;;
\end{verbatim}
\end{frame}


\begin{frame}{Example cont'd}

Is this implementation adequate?

\bigskip
\pause

No. Search tree property can be violated by a
programmer:

\begin{alltt}
toList(insert 2 (Br(Br(Lf,3,Lf), 1, Br(Lf,0,Lf))));;\\
> val it = [3;1;0;2]: int list
\end{alltt}

\pause
\bigskip

Solution: Hide the internal structure of search trees.

\end{frame}


\begin{frame}{Module}

A {\redbrown module} is a combination of a
\begin{itemize}
\item<1-> {\redbrown signature}, which is a specification of an
interface to the module (the user's view), and an

\item<2-> {\redbrown
implementation}, which provides declarations for the specifications in the signature.

\end{itemize}

\end{frame}

\begin{frame}[fragile]
\frametitle{Geometric vectors: Signature}

The signature specifies one type and eight values:

{\small
\begin{alltt}\blue
// Vector signature
module Vector
type vector
val ( ~-. ) : vector -> vector             // Vector sign change
val ( +. )  : vector -> vector -> vector   // Vector sum
val ( -. )  : vector -> vector -> vector   // Vector difference
val ( *. )  : float  -> vector -> vector   // Product with number
val ( &. )  : vector -> vector -> float    // Dot product
val norm    : vector -> float              // Length of vector
val make    : float * float -> vector      // Make vector
val coord   : vector -> float * float      // Get coordinates
\end{alltt}
}
\bigskip
\pause

\begin{center}\redbrown The specification 'vector' does not reveal the
implementation \end{center}

\medskip
\pause
\begin{itemize}
\item Why is {\blue make} and {\blue coord} introduced?
\end{itemize}


\end{frame}

\begin{frame}[fragile]
\frametitle{Geometric vectors (2): Simple implementation}

An implementation must declare each specification of the signature:

\begin{alltt}\blue
// Vector implementation
module Vector
type vector ={\redbrown V of} float * float
let (~-.) (V(x,y))             = V(-x,-y)
let (+.) (V(x1,y1)) (V(x2,y2)) = V(x1+x2,y1+y2)
let (-.)  v1         v2        = v1 +. -. v2
let ( *.) a         (V(x1,y1)) = V(a*x1,a*y1)
let (&.) (V(x1,y1)) (V(x2,y2)) = x1*x2 + y1*y2
let norm  (V(x1,y1))           = sqrt(x1*x1+y1*y1)
let make  (x,y)                = V(x,y)
let coord (V(x,y))             = (x,y)
\end{alltt}


\bigskip
\pause

\begin{itemize}
\item Since the representation of 'vector' is {\redbrown hidden in the signature}, the type must be {\redbrown implemented by either a tagged value or a record}.
\end{itemize}
\end{frame}

\begin{frame}[fragile]
\frametitle{Geometric vectors (3): Compilation}

Suppose
\begin{itemize}
\item the signature is in a file {\blue 'Vector.fsi'}
\item the implementation is in a file {\blue 'Vector.fs'}
\end{itemize}

\bigskip


A library file {\blue 'Vector.dll'} is constructed by the following command:
{\blue\begin{verbatim}
C:\mrh\Kurser\02157-11\Week 10\fsc -a Vector.fsi Vector.fs
\end{verbatim}
}

\bigskip

The library {\blue 'Vector'} can now be used just like other libraries, such as {\blue 'Set'} or {\blue 'Map'}.

\end{frame}


\begin{frame}[fragile]
\frametitle{Geometric vectors (4): Use of library}

A library must be referenced before it can be used.

{\small
\begin{alltt}\blue
{\redbrown #r @"c:{\ttbsl}mrh{\ttbsl}Kurser{\ttbsl}02157-11{\ttbsl}Week 10{\ttbsl}Vector.dll";;}
\ett{--> Referenced 'c:{\ttbsl}mrh{\ttbsl}Kurser{\ttbsl}02157-11{\ttbsl}Week 10{\ttbsl}Vector.dll'}
open Vector ;;

let a = make(1.0,-2.0);;
\ett{val a : vector}
let b = make(3.0,4.0);;
\ett{val b : vector}
let c = 2.0 *. a -. b;;
\ett{val c : vector}

coord c ;;
\ett{val it : float * float = (-1.0, -8.0)}

let d = c &. a;;
\ett{val d : float = 15.0}

let e = norm b;;
\ett{val e : float = 5.0}
\end{alltt}
}

\medskip

Notice: the implementation of \texttt{\blue vector} is not visible and it
cannot be exploited.

\end{frame}


\begin{frame}[fragile]
\frametitle{Type augmentation}

A {\em\blue type augmentation}
\begin{itemize}

\item adds declarations to the definition of a tagged type or a record type
\item allows declaration of (overloaded) operators.
\end{itemize}

\bigskip
\pause

In the 'Vector' module we would like to
\begin{itemize}

\item overload  \verb/+/, \verb/-/ \ and \ \verb/*/ to also denote {\blue vector} operations.

\item overload \verb/*/ \ is even overloaded to denote two different
operations on vectors.


\end{itemize}
\end{frame}


\begin{frame}[fragile]
\frametitle{Type augmentation -- signature}

{\blue
\begin{verbatim}
module Vector

[<Sealed>]
type vector =
  static member ( ~- ) : vector -> vector
  static member ( + )  : vector * vector -> vector
  static member ( - )  : vector * vector -> vector
  static member ( * )  : float  * vector -> vector
  static member ( * )  : vector * vector -> float
val make : float * float -> vector
val coord: vector -> float * float
val norm : vector -> float
\end{verbatim}
}
\pause
\bigskip

\begin{itemize}

\item<2->
The {\em attribute} \verb/[<Sealed>]/ is mandatory when
a type augmentation is used. %It protects the library against unintentional use in the %OO-world.


\item<3->
The ``\verb/member/'' specification and declaration of an infix operator (e.g. \verb/+/) correspond
to a type of form \ {\it type}$_1$\;\verb/*/\;{\it type$_2$}\;\verb/->/\;{\it type}$_3$ %while the earlier
%\verb/val/ specification and \verb/let/ declaration (of e.g. \verb/(+.)/) use a %higher-order  type of form \
%{\it type}$_1$\;\verb/->/\;{\it type$_2$}\;\verb/->/\;{\it type}$_3$. The indicated
%type is required to get the overloading.
%The resulting prefix function (e.g. \verb/(+)/) will, nevertheless, get the usual %higher-order type.

\item<4-> The operators can still be used on numbers.

\end{itemize}

\end{frame}

\begin{frame}[fragile]
\frametitle{Type augmentation -- implementation and use}

{\small\blue
\begin{alltt}
module Vector

type vector =
  | V of float * float
  static member (~-) (V(x,y))           = V(-x,-y)
  static member (+) (V(x1,y1),V(x2,y2)) = V(x1+x2,y1+y2)
  static member (-) (V(x1,y1),V(x2,y2)) = V(x1-x2,y1-y2)
  static member (*) (a, V(x,y))         = V(a*x,a*y)
  static member (*) (V(x1,y1),V(x2,y2)) = x1*x2 + y1*y2
let make (x,y)     = V(x,y)
let coord (V(x,y)) = (x,y)
let norm (V(x,y))  = sqrt(x*x + y*y)
\end{alltt}
}

\medskip
\pause

The operators \verb/+/, \verb/-/, \verb/*/ are available on vectors even without opening:

\begin{alltt}\blue
let a = Vector.make(1.0,-2.0);;
\ett{val a : Vector.vector}

let b = Vector.make(3.0,4.0);;
\ett{val b : Vector.vector}

let c = 2.0 * a - b;;
\ett{val c : Vector.vector}

Vector.coord c ;;
\ett{val it : float * float = (-1.0, -8.0)}
\end{alltt}

\end{frame}

\begin{frame}[fragile]
\frametitle{Customizing the string function}

{\small
\begin{alltt}\blue
module Vector
type vector =
   | V of float * float
  {\redbrown override v.ToString() =
           match v with | V(x,y) -> string(x,y) }

let make (x,y)     = V(x,y)
   ...
type vector with
  static member (~-) (V(x,y))           = V(-x,-y)
  ...
\end{alltt}
}

\begin{itemize}

\item The default ToString function that do not reveal a meaningful value is overridden to give a string for the pair of coordinates.

\item A type extension is used.
\end{itemize}

\pause
\bigskip

Example:
\begin{alltt}
let a = Vector.make(1.0,2.0);;
\ett{val a : Vector.vector = (1, 2)}

string(a+a);;
\ett{val it : string = "(2, 4)"}
\end{alltt}
\end{frame}


\begin{frame}{Summary}

Modular program development

\begin{itemize}

\item program libraries using signatures and structures

\item type augmentation, overloaded operators, customizing string (and other) functions

\item Encapsulation, abstraction, reuse of components, division of concerns, ...

\item ...

\end{itemize}

\end{frame}

\end{document}
