% mrh 17/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[handout,9pt,xcolor=dvipsnames]{beamer}
\documentclass[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}

\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 1: Introduction and Getting Started} % (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 6/09/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}


% Since this a solution template for a generic talk, very little can
% be said about how it should be structured. However, the talk length
% of between 15min and 45min and the theme suggest that you stick to
% the following rules:

% - Exactly two or three sections (other than the summary).
% - At *most* three subsections per section.
% - Talk about 30s to 2min per frame. So there should be between about
%   15 and 30 frames, all told.



%\section{Introduction}
\begin{frame}
\begin{center}\LARGE \color{BlueViolet}  WELCOME to \\
02157 Functional Programming
\end{center}

\bigskip

\begin{itemlist}{Teaching assistant:}
\item[Teacher:] Michael R.\ Hansen\\
DTU Informatics,
 \textcolor{BrickRed}{mrh@imm.dtu.dk}
\bigskip

\item[Teaching assistant:]
 Phan Anh Dung, PhD Student.\\
 S{\o}ren Olofsson, master's student\\
 Both at DTU Informatics
\bigskip

\item[Homepage:]
\textcolor{BrickRed}{www.imm.dtu.dk/courses/02157}
\end{itemlist}

\end{frame}


\begin{frame}{Today: Friday, September 7.}

\begin{itemize}

\item Introduction to functional programming and F\#\\
(341.23 --- here)

\item about 9:15 -- lecture notes can be bought here.

\item Make your first programs in the databar\\
(341 Rooms: 015 and 019 --- E-databar)

\item Introduction to lists in F\#\\
(341.23 --- here)

\item Computations with polynomials in F\#\\
(341 Rooms: 015 and 019 --- E-databar)
\end{itemize}

\bigskip

\onslide<2->{\begin{center}\redbrown By noon you have solved a
non-trivial problem using F\#\end{center}}

\end{frame}


\begin{frame}
\frametitle{Practical Matters}

\begin{itemize}

\item Textbook: {\blue Functional Programming using F\#}, Chapters 1-7, 9.\\
 by Michael R.~Hansen and Hans Rischel.\\[2mm]
Can be bought at the reception of DTU Informatics. Price 100 kr.\\[2mm]
Published by Cambridge University Press the coming winter.

\bigskip

\item<2->
\textmd{F\#} is an {\blue open-source} functional language integrated in the
\textmd{Visual Studio} development platform and with access to all features
in the \textmd{.NET} program library. The language is also supported on \textmd{Linux} and \textmd{MAC}
systems using the \textmd{Mono} platform.

\bigskip

\item<3-> We use F\# on the Windows platform in the E-databar.

\bigskip


\item<4-> Look at homepage concerning installations for your own PC (Windows, Linux or Mac).

\end{itemize}


\end{frame}




\begin{frame}{Imperative models}
\begin{itemize}
\item {\darkgreen Imperative models of computations} are expressed
in terms of {\redbrown states} and sequences of {\redbrown
state-changing operations}
\end{itemize}
\bigskip

Example:

{\blue
\begin{alltt}
   i := 0;\\
   s := 0;\\
   while i < length(A)\\
   \ \    do s := s+A[i];\\
   \ \ \ \ \ i := i+1\\
   \ \   od
\end{alltt}
}

\bigskip

\onslide<2->{
%\begin{itemize}
%\item
\hfill {\darkgreen An imperative model describes {\redbrown\em
how} a solution is obtained}
%\end{itemize}
}
\end{frame}



%\subsection{Object-oriented models}
\begin{frame}{Object-oriented models}

\begin{itemize}
\item An {\darkgreen object} is characterized by a {\redbrown
state} and an {\darkgreen interface} specifying a collection of
{\redbrown state-changing operations}.

\item {\darkgreen Object-oriented models of computations} are
expressed in terms of a collection of objects which exchange
messages by using interface operations.
\end{itemize}

\bigskip


\onslide<2->{
%\begin{itemize}
%\item
\hfill {\darkgreen Object-oriented models add structure to
imperative models}

\medskip

\hfill {\darkgreen An object-oriented model describes
{\redbrown\em how} a solution is obtained}
%\end{itemize}
}
\end{frame}



%\subsection{Declarative models}

\begin{frame}{Declarative models}

In declarative models focus is on \textcolor{BrickRed}{\em what} a
solution is.

\bigskip

\begin{itemize} \item Logical programming (02156
Logical Systems and Logical Programming)
\begin{itemize}
\item Programs are (typically) expressed in a fragment of
first-order logic. The formulas have a standard meaning, as well
as a \textcolor{BlueViolet}{procedural interpretation based on
logical inferences}.
\end{itemize}

\bigskip

\item<2-> Functional programming
\begin{itemize}
\item A program is expressed as a mathematical function \[\blue f:
A \to B\] and \textcolor{BlueViolet}{function
applications guide computations}.
\end{itemize}

\end{itemize}

\bigskip

\onslide<3->{Some advantages
\begin{itemize}
\item fast prototyping based on abstract concepts

\item more advanced applications are within reach

\item Supplement modelling and problem solving techniques

\item Execute in parallel on multi-core platforms

\end{itemize}
\ \hfill {\darkgreen F\# is as efficient as C\#}
}
\end{frame}





%\subsection{Functional programming background}

\begin{frame}{Some functional programming background}
In \textcolor{Green}{functional programming}, the model of
computation is the application of functions to arguments. \hfill
\textcolor{BrickRed}{no side-effects}

\bigskip

\begin{itemize}
\item Introduction of \textcolor{BrickRed}{$\lambda$-calculus}
around 1930 by Church and Kleene when investigating function
definition, function application, recursion and computable
functions. For example, $f(x) = x+2$ is represented by
${\color{BrickRed}\lambda} x. x+2$.

\medskip

\item<2-> Introduction of the type-less functional-like
programming language LISP was developed by McCarthy in the late
1950s.

\medskip

\item<3-> Introduction of the "variable-free" programming language
FP (Backus 1977), by providing a rich collection of functionals
(combining forms for functions).

\medskip

\item<4-> Introduction of functional languages with a strong type
system like ML (by Milner) and Miranda (by Turner) in the 1970s.

\end{itemize}
\end{frame}


\begin{frame}{Some background of the ``SML-family''}

\begin{itemize}
\item \textcolor{BlueViolet}{Standard Meta Language} (SML)  was
originally designed for theorem proving
\begin{itemlist}{xx}
\item[] Logic for Computable Functions (Edinburgh LCF)\\
\hfill  {\color{Green} Gordon,
    Milner, Wadsworth (1977)}
 \end{itemlist}


\medskip
\item<2-> High quality compilers, e.g. {\redbrown Standard ML of
New Jersey} and {\color{BrickRed} Moscow ML},
based on a {\em formal semantics}\\
\hfill  {\color{Green} Milner, Tofte, Harper,
  MacQueen 1990 \& 1997}


\medskip

\item<3-> SML-like systems (SML, OCAML, {\blue F\#}, $\ldots$)  have now
applications far away from its origins
\begin{itemlist}{xx}
\item[] Compilers, Artificial Intelligence,  Web-applications,
Financial sector, $\ldots$
\end{itemlist}


\medskip
\item<4-> {\blue F\#} is now integrated in the .net environment

\medskip
\item<5-> Declarative aspects are sneaking into more "main stream
languages"

\medskip
\item<6-> Often used to teach high-level programming concepts

\end{itemize}
\end{frame}



%\subsection{Course overview}
\begin{frame}{Overview of the course}
\begin{itemize}
\item Functional programming concepts and techniques

\item A model-based programming approach using a functional
language with a strong type system.

\item Program correctness, including structural induction and
well-founded induction
\end{itemize}

\bigskip

\onslide<2->{Fun with a variety of applications, such as
\begin{itemize}

\item a library for piecewise linear curves -- with applications

\item a Sudoko solver

\item an interpreter for a simple programming language

\item a lambda-calculus interpreter

\item a model checker for CTL -- a temporal logic



\item ...

\end{itemize}
}

\bigskip

\onslide<3->{Homepage for the course: \texttt{\blue
www.imm.dtu.dk/courses/02157}}

\end{frame}


%\subsection{Goals}
\begin{frame}{A major goal}

Teach {\redbrown abstraction} (not a concrete programming
language)
  \begin{itemize}
  \item Modelling
  \item Design
  \item Programming
  \end{itemize}

\pause

\medskip
Why?
\begin{center} More complex problems can be solved in
an succinct, elegant and understandable manner
\end{center}

\pause
\medskip

How?
\begin{center} Solving a broad class of problems showing the
applicability of the theory, concepts, techniques and tools.
\end{center}

\pause
\medskip

\begin{center} \textcolor{BrickRed}{Functional programming techniques once
mastered are useful for the design of programs in other
programming paradigms as well.}
\end{center}

\end{frame}

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

%\section{Getting started with F\#}

\begin{frame}{F\# supports}
\begin{itemize}
\item Functions as first class citizens

\medskip
\item<2-> Structured values like lists, trees, $\ldots$

\medskip

\item<3-> Strong and flexible type discipline, including
\begin{itemlist}{xx}
\item[] {\redbrown type inference} and
  {\redbrown polymorphism}
\end{itemlist}
\medskip


\item<4-> Imperative and object-oriented programming
\begin{itemlist}{xx}
\item[] assignments, loops, arrays, objects, Input/Output, etc.
\end{itemlist}

\end{itemize}

\bigskip
\onslide<5->{
Programming as a modelling discipline
\begin{itemize}
\item High-level programming, declarative programming, executable
  declarative specifications \hfill {\darkgreen B, Z, VDM, RAISE}

\item Fast time-to-market
\end{itemize}
}

\end{frame}


\begin{frame}{Course context}

{\blue Prerequisites for 02157:}
Programming in an imperative/object-oriented language, discrete mathematics, algorithms and data structure, as obtained, for example, from the bachelor programme in Software Technology.


\bigskip\pause

Successor course of 02157:
\begin{center}\blue 02257 Applied functional programming\\
3-weeks period January
\end{center}

An extension of 02157 that aims at an effective use of functional programming in connection with courses and projects at the {\darkgreen M.Sc. programme} in computer science and engineering, and in {\darkgreen industrial applications}.

\begin{itemize}

\item Computer science applications. Interpreter for a programming language, for example.

\item ``Practical applications''. Involving a database, for example.

\item Functional pearls. Monadic parsing, for example.
\end{itemize}

\end{frame}

\begin{frame}{Overview of Getting Started }

Main functional ingredients of F\#:
\begin{itemize}
\item The interactive environment

\item Values, expressions, types, patterns

\item Declarations of
  values and recursive functions

\item Binding, environment and evaluation

\item Type inference
\end{itemize}

\bigskip

\onslide<2->{\textcolor{BrickRed}{GOAL}: By the end of this first
part you have constructed \textcolor{Blue}{succinct},
\textcolor{Blue}{elegant} and \textcolor{Blue}{understandable} F\#
programs, e.g.\ for
\begin{itemize}
\item ${\rm sum}(m,n) = \sum_{i=m}^n i$
\medskip
\item Fibonacci numbers ($F_0 = 0, F_1 = 1, F_n = F_{n-1} +
F_{n-2}$)
\medskip
\item Binomial coefficients $\left(\begin{array}{c} n\\
k
\end{array}\right)$
\end{itemize}
%in a {\yellow succinct}, {\yellow elegant} and {\yellow
%understandable} manner.
}
\end{frame}


%\input{interactiveEnv}
%\subsection{The interactive environment}
\begin{frame}{The Interactive Environment}
\begin{tabular}{lc}
\begin{minipage}{5cm}
\begin{alltt}2*3 + 4;;\\
    \ett{val it : int = 10}
\end{alltt}
\end{minipage}
& \onslide<2->{
\begin{minipage}{7cm}
\begin{itemize}
\item [$\redbrown \Leftarrow$] Input to the F\# system \item
[$\redbrown \Leftarrow$] Answer from the F\# system
\end{itemize}
\end{minipage}}
\end{tabular}

\bigskip

\onslide<3-> {
\begin{itemize}
\item The {\em\darkgreen keyword} \texttt{val} indicates a value
is computed \item The {\em\darkgreen integer} 10 is the computed
value \item \texttt{int} is the {\em\darkgreen type} of the
computed value \item The {\em\darkgreen identifier} \texttt{it}
names the (last) computed value
\end{itemize}
}

\bigskip

\onslide<4->{The notion {\em\redbrown binding} explains which
entities are named by
  identifiers.
\[ {\darkgreen \mathtt{it} \mapsto 10} \qquad  \mbox{reads: ``\texttt{it} is bound to 10''} \]
}

\end{frame}


%\subsection{Declarations}
\begin{frame}{Value Declarations}
A value declaration has the form:\ {\blue $\mathtt{let}\
\mathit{identifier}\ = \mathit{expression}$}

\bigskip

\begin{tabular}{lc}
\begin{minipage}{6cm}
\begin{alltt}
let price = 25 * 5;;\\[2mm]
\ett{val price : int = 125}
\end{alltt}
\end{minipage}
&
\begin{minipage}{7cm}
\begin{itemize}
\item [$\redbrown \Leftarrow$] A declaration as input \item
[$\redbrown \Leftarrow$] Answer from the F\# system
\end{itemize}
\end{minipage}
\end{tabular}

\bigskip

The effect of a declaration is a binding: \  ${\darkgreen
\mathtt{price}
    \mapsto 125}$

\bigskip
\onslide<2->{Bound identifiers can be used in expressions and
declarations, e.g.}

\begin{tabular}{lr}
\begin{minipage}{6.3cm}
\onslide<2->{
\begin{alltt}
let newPrice = 2*price;;\\
\ett{val newPrice : int = 250}


newPrice > 500;;\\
\ett{val it : bool = false}
\end{alltt}}
\end{minipage}
&
\begin{minipage}{5.1cm}
\onslide<3->{
\ \\[2mm]
{\darkgreen A collection of bindings {\blue
\[\left[
   \begin{array}{lcl} \mathtt{price}& \mapsto& 125\\
                      \mathtt{newPrice} & \mapsto& 250\\
                      \mathtt{it}& \mapsto& \mathrm{false}
   \end{array}
\right]
\]}
is called an {\redbrown environment}}}
\end{minipage}

\end{tabular}

\end{frame}



\begin{frame}{Function Declarations 1:\ \ \color{Blue}$\mathtt{let}\ f\, x\ =\ e$}

Declaration of the circle area function:
\begin{alltt}\color{Blue}
\ \    let circleArea r = System.Math.PI * r * r;;
\end{alltt}

\medskip

\begin{itemize}
\item \texttt{\color{Blue}System.Math} is a program library \item
\texttt{\color{Blue}PI} is an identifier (with type
\texttt{\color{Blue}float}) for $\color{Blue}\pi$
  in \texttt{\color{Blue}System.Math}
\end{itemize}

\medskip

\onslide<2->{The type is {\redbrown automatically inferred} in the
answer:
\begin{alltt}\color{Blue}
\ \    \ett{val circleArea : {\redbrown float -> float}}
\end{alltt}
}

\bigskip
\onslide<3->{ Applications of the function:
\begin{alltt}\color{Blue}
\ \   circleArea 1.0;;     {\blue (*} {\darkgreen this is a comment} {\blue *)}\\
\ \  \ett{val it : float = 3.141592654}\\
\                                       \\
\ \   circleArea(3.2);;   {\blue //} {\darkgreen A comment: optional brackets}\\
\ \  \ett{val it : float = 32.16990877}
\end{alltt}
}
\end{frame}


\begin{frame}{Anonymous functions: by example \qquad (1)}

An {\blue anonymous function} computing the number of days in a month:
\begin{alltt}
function\\
 | 1  -> 31  // January\\
 | 2  -> 28  // February // not a leap year\\
 | 3  -> 31  // March\\
 | 4  -> 30  // April\\
 | 5  -> 31  // May\\
 | 6  -> 30  // June\\
 | 7  -> 31  // July\\
 | 8  -> 31  // August\\
 | 9  -> 30  // September\\
 | 10 -> 31  // October\\
 | 11 -> 30  // November\\
 | 12 -> 31;;// December\\
\ett{{\darkgreen  ... warning ... Incomplete pattern matches ... }}\\
\ett{{\blue val it : int -> int = <fun:clo@17-2>}}
\end{alltt}
\pause
\begin{alltt}
it 2;;\\
\ett{val it : int = 28}
\end{alltt}
\pause
\begin{center}\redbrown
A {\blue function expression} with a {\blue pattern} for every month
\end{center}

\end{frame}



\begin{frame}[fragile]
\frametitle{Anonymous functions: by example \qquad (2)}

One {\em\darkgreen wildcard pattern} {\darkgreen \_} can cover many similar cases:

\begin{alltt}
function
 | 2  -> 28  // February
 | 4  -> 30  // April
 | 6  -> 30  // June
 | 9  -> 30  // September
 | 11 -> 30  // November
 | {\darkgreen _}  -> 31;;// All other months
\end{alltt}

\pause

An even more succinct definition can be given using an {\blue {\em or}-pattern}:
\begin{alltt}
function
 | 2        -> 28  // February
 |{\blue 4|6|9|11} -> 30  // April, June, September, November
 | _        -> 31  // All other months
;;
\end{alltt}
\end{frame}


%\subsection{Recursion}
\begin{frame}{Recursion. Example  $\color{Blue}n! = 1 \cdot 2 \cdot \ldots \cdot n$, $\color{Blue}n\geq
  0$}

Mathematical definition: \hfill {\darkgreen recursion formula}
\[\color{Blue}
\begin{array}{lclrr}
0 ! & = & 1 & &\ \ \ \ \ \ \ \ \ \ \ \ \ (i)\\
n ! & = & n \cdot (n-1)!, & \mbox{for $n > 0$} & (ii)
\end{array}
\]

\bigskip
\pause

Computation:
\[\color{Blue}
\begin{array}{llr}
  & 3! & \\
= & 3 \cdot (3-1)! & (ii)\\
= & 3 \cdot 2 \cdot (2-1)! & (ii)\\
= & 3 \cdot 2 \cdot  1 \cdot (1-1)! & (ii)\\
= & 3 \cdot 2 \cdot 1 \cdot 1 & (i)\\
= & 6 &
\end{array}
\]

\end{frame}


\begin{frame}{Recursive declaration. Example $\color{Blue}n!$}
Function declaration:
\begin{alltt}\color{Blue}
let {\darkgreen rec} fact = function\\
\ \ | 0 -> 1     \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  (*\  i *)\\
\ \ | n -> n * fact(n-1);; \        (* ii *)\\
\ett{val fact : int -> int}
\end{alltt}

\pause

{\blue Evaluation}:
\[
\begin{array}{llrr}
  &  \mathtt{fact(3)} & & \\
\leadsto & \mathtt{3 * fact(3-1)} & (ii)& \onslide<3->{\darkgreen
  [\mathtt{n} \mapsto 3]} \\
\leadsto & \mathtt{3 * 2 * fact(2-1)} & (ii)
&\onslide<4->{\darkgreen
  [\mathtt{n} \mapsto 2]}\\
\leadsto & \mathtt{3 * 2 * 1 * fact(1-1)} &
(ii)&\onslide<5->{\darkgreen
  [\mathtt{n} \mapsto 1]} \\
\leadsto & \mathtt{3 * 2 * 1 * 1} & (i)&\onslide<6->{\darkgreen
  [\mathtt{n} \mapsto 0]} \\
\leadsto & \mathtt{6} & &
\end{array}
\]
\hfill %\lilacbox{
\begin{minipage}{7cm}
\[ {\blue e_1\ \leadsto e_2} \qquad \mbox{reads: $e_1$ {\redbrown evaluates to}
  $e_2$}
\]
\end{minipage}
%}
\end{frame}





\begin{frame}{Recursion. Example $\color{Blue}x^n = x \cdot \ldots \cdot x$, $\color{Blue}n$
  occurrences of $\color{Blue}x$}

\textcolor{BlueViolet}{Mathematical definition}: \hfill
{\darkgreen recursion formula}
\[\begin{array}{lclrr}
x^0  & = & 1 & &\ \ \ \ \ \ \ \ \ \ \ \ \ (1)\\
x^n  & = & x \cdot x^{n-1}, & \mbox{for $n > 0$} & (2)
\end{array}
\]

\pause
\bigskip

\textcolor{BlueViolet}{Function declaration:}
\begin{alltt}
let rec power = function\\
 \ \ | (\_,0) -> 1.0 \ \ \ \ \ \ \ \ \ \ \ \ \ \ (* 1 *)\\
 \ \ | (x,n) -> x * power(x,n-1) \ (* 2 *)
\end{alltt}

\pause
\bigskip

\textcolor{BlueViolet}{Patterns}:
\begin{description}
\item[${\blue \mathtt{(\_,0)}}$] matches any {\darkgreen pair} of
the form $(x,0)$.\\ The {\darkgreen wildcard} pattern \_ matches
any value. \item[${\blue \mathtt{(x,n)}}$] matches any pair
$(u,i)$ {\darkgreen yielding} the bindings
\[\redbrown \mathtt{x} \mapsto u, \mathtt{n} \mapsto i \]
\end{description}



\end{frame}



\begin{frame}{Evaluation. Example: \color{BlueViolet}\texttt{power(4.0, 2)}}

{\blue Function declaration}:
\begin{alltt}
let rec power = function\\
 \ \ | (\_,0) -> 1.0  \ \ \ \ \ \ \ \ \ \ \ \ \ \   (* 1 *)\\
 \ \ | (x,n) -> x * power(x,n-1)  \   (* 2 *)
\end{alltt}

\medskip

{\blue Evaluation}:
\[\begin{array}{lll}
  & \mathtt{power(4.0, 2)} & \\
\leadsto &  \mathtt{4.0 * power(4.0, 2-1)} & \mbox{Clause 2,
$[\mathtt{x} \mapsto 4.0, \mathtt{n}
\mapsto 2]$} \\
\leadsto &  \mathtt{4.0 * power(4.0, 1)} & \\
\leadsto &  \mathtt{4.0 * (4.0 * power(4.0,1-1))} & \mbox{Clause
2, $[\mathtt{x} \mapsto 4.0, \mathtt{n}
\mapsto 1]$}\\
\leadsto &  \mathtt{4.0 * (4.0 * power(4.0,0))} & \\
\leadsto &  \mathtt{4.0 * (4.0 * 1)} & \mbox{Clause 1}\\
\leadsto & 16.0 &
\end{array}
\]
\end{frame}

\begin{frame}{If-then-else expressions}

{\blue Form}:
\[ \mathtt{if}\ \color{BlueViolet}{b}\ \mathtt{then}\ \color{BlueViolet}{e_1}\ \mathtt{else}\ \color{BlueViolet}{e_2} \]

\medskip

{\blue Evaluation rules}:
\[
\begin{array}{lcl}
 \mathtt{if}\ \mathrm{\color{BlueViolet}true}\ \mathtt{then}\ e_1\ \mathtt{else}\ e_2 &
 \leadsto & e_1\\
 \mathtt{if}\ \mathrm{\color{BlueViolet}false}\ \mathtt{then}\ e_1\ \mathtt{else}\ e_2 &
 \leadsto & e_2
\end{array}
\]

\pause \bigskip

\textcolor{BlueViolet}{Alternative declarations}:

\medskip

\begin{tabular}{lcl}
\texttt{let rec fact n} & \texttt{=} &  \texttt{if n=0 then 1} \\
&    &          \texttt{else n * fact(n-1);}\\[3mm]
\texttt{let rec power(x,n)} & \texttt{=}& \texttt{if n=0 then 1.0} \\
  &    & \texttt{else x * power(x,n-1);}
\end{tabular}

\pause \bigskip

\begin{center}
\redbrown Use of patterns usually gives more
understandable programs
\end{center}

\end{frame}

\begin{frame}{Booleans}

{\blue Type name} \texttt{bool}

\medskip

{\blue Values} \texttt{false},\ \ \texttt{true}

\medskip

\begin{tabular}{lr}
\begin{tabular}{l|l|l}
{\blue Operator} & {\blue Type} & \\ \hline \texttt{not} &
\texttt{bool -> bool} & negation
\end{tabular} %
\hspace{.7cm}\framebox{\redbrown
\begin{tabular}{l}
\texttt{not true = false}\\
\texttt{not false = true}
\end{tabular}
}
\end{tabular}

\pause
\bigskip



{\blue Expressions}
\[ \begin{array}{lll}
e_1\ \mbox{\color{OliveGreen} \&\&}\
e_2 & \quad
& \mbox{\color{OliveGreen} ``conjunction $e_1
  \wedge e_2$''}\\
e_1\ \mbox{\color{OliveGreen} \texttt{||}}\
e_2 & \quad
&\mbox{\color{OliveGreen} ``disjunction $e_1
  \vee e_2$''}
\end{array}
 \]

\pause \medskip

{\blue --- are {\redbrown lazily} evaluated, e.g.}\hspace{1cm}
\framebox{\redbrown \begin{tabular}{l}\texttt{1<2 || 5/0 = 1}\\
$\lto$\ \texttt{true}\end{tabular}}

\bigskip
{\blue Precedence}:\ \  \texttt{\color{OliveGreen} \&\&} has
{\redbrown higher} than \texttt{\color{OliveGreen} ||}
\end{frame}




\begin{frame}[fragile]
\frametitle{Strings}

{\blue Type name} \texttt{string}

\medskip

{\blue Values} \texttt{"abcd"},\ \ \texttt{" "},\ \ \texttt{""},\
\ \verb-"123-{\redbrown \verb-\"-}\verb-321"-\ \ ({\redbrown
escape sequence} for {\darkgreen \texttt{"}})

\bigskip
\pause

\begin{tabular}{l|l|l}
{\blue Operator} & {\blue Type} & \\ \hline
\texttt{String.length} & \texttt{string -> int} & {\redbrown length} of string\\
\verb-+- & \texttt{string*string -> string} & {\redbrown concatenation}\\
\texttt{= < <= ...} & \texttt{string*string -> bool} & {\redbrown comparisons}\\
\texttt{string} & \texttt{obj -> string} & {\redbrown
conversions}
\end{tabular}

\bigskip

\pause \textcolor{BlueViolet}{Examples}

\bigskip

\begin{tabular}{l|@{\hspace{.5cm}}l}
\begin{minipage}{5cm}
%\onslide<3->{
\begin{verbatim}
- "auto" < "car";
> val it = true : bool

- "abc"+"de";
> val it = "abcde": string
\end{verbatim}%}
\end{minipage}
&
\begin{minipage}{6cm}
%\onslide<4->{
\begin{verbatim}
- String.length("abc"^"def");
> val it = 6 : int

- string(6+18);
> val it = "24": string
\end{verbatim}%}
\end{minipage}
\end{tabular}
\end{frame}



%\subsection{Types}
\begin{frame}{Types --- every expression has a type \color{BlueViolet}$e : \tau$}

Basic types: \ \ \

\begin{tabular}{l|l|l}
   & type name & example of values \\ \hline
Integers & \texttt{int}  & \tttilde 27,\ 0,\ 15,\ 21000\\
Floats    & \texttt{float} & \tttilde 27.3,\ 0.0,\ 48.21\\
Booleans & \texttt{bool} & true,\ false
\end{tabular}

\bigskip \pause

Pairs:\ \ \

\begin{tabular}{lr}
If $e_1: \tau_1$ and $e_2: \tau_2$ & \\
then $(e_1, e_2) : \tau_1 {\blue *} \tau_2$ & \ \ \ \ \ \ \ \ \
{\blue pair (tuple) type constructor}
\end{tabular}

\pause
\bigskip

Functions:\ \

\begin{tabular}{lr} if $f: \mbox{$\tau_1$
\texttt{\blue ->} $\tau_2$}$ and $a: \tau_1$ &
\ \ \ \ {\blue function type constructor}\\
then $f(a) : \tau_2$ &
\end{tabular}

\pause
\bigskip

Examples:

\medskip

\begin{tabular}{lr}
\begin{minipage}{6.2cm}
\texttt{\color{OliveGreen} (4.0, 2): float*int}

\texttt{\color{OliveGreen} power: float*int -> float}

\texttt{\color{OliveGreen} power(4.0, 2): float}
\end{minipage}
& \begin{minipage}{6cm} \redbrown \texttt{*} has higher precedence
that \texttt{->}
\end{minipage}
\end{tabular}
\end{frame}




\begin{frame}%[fragile]
  \frametitle{Type inference: \color{BlueViolet}\texttt{power}}

\begin{alltt}\color{BlueViolet}
let rec power = function\\
\ \ | (\ttunsc,0) -> 1.0   \ \   \ \ \ \ \ \ \ \ \ \ \  (* 1 *)\\
\ \ | (x,n) -> x * power(x,n-1)     (* 2 *)
\end{alltt}

\medskip

\begin{itemize}
\item<2-> The type of the function must have the form: $\tau_1$
\texttt{*} $\tau_2$ \texttt{->} $\tau_3$, because argument is a
pair.

\item<3-> $\tau_3$ = \texttt{float} because \texttt{1.0:float}\hfill
(Clause 1, function value.)

\item<4-> $\tau_2$ = \texttt{int} because \texttt{0:int}.

\item<5-> \texttt{x*power(x,n-1):float},
  because $\tau_3$ = \texttt{float}.

\item<6-> multiplication can have
\begin{center}
\texttt{int*int -> int}\ \  or\ \  \texttt{float*float -> float}
\end{center}
as types, but no ``mixture'' of \texttt{int} and \texttt{float}

\item<7-> Therefore \texttt{x:float} and $\tau_1$=\texttt{float}.
\end{itemize}

\medskip

\onslide<8->{\redbrown The F\# system determines the type
\texttt{\blue float*int -> float}}

\end{frame}


\begin{frame}{Summary}
\begin{itemize}
\item The interactive environment

\item Values, expressions, types, patterns

\item Declarations of
  values and recursive functions

\item Binding, environment and evaluation

\item Type inference
\end{itemize}

\bigskip
\pause

Breath first round through many concepts aiming at program
construction from the first day.

\bigskip

We will go deeper into each of the concepts later in the course.

\end{frame}



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


%\section{Brief introduction to lists}
\begin{frame}{Overview}

\begin{itemize}

\item Lists: values and constructors

\item Recursions following the structure of lists

\end{itemize}

\bigskip

The purpose of this lecture is to give you an (as short as
possible) introduction to lists, so that you can solve a problem
which can illustrate some of F\#'s high-level features.

\bigskip
\pause

This part is {\redbrown\em not} intended as a comprehensive
presentation on lists, and we will return to the topic again
later.

\end{frame}

%\subsection{Values}
\begin{frame}{Lists}

A list is a finite sequence of elements having the same type:
\[
{\blue [v_1; \ldots; v_n]} \qquad \mbox{({\blue $[\, ]$} is called
the empty list)}
\]

\bigskip
\pause

\begin{alltt}
[2;3;6];;\\
\ett{val it : int list = [2; 3; 6]}
\end{alltt}

\pause

\begin{alltt}
["a"; "ab"; "abc"; ""];;\\
\ett{val it : string list = ["a"; "ab"; "abc"; ""]}
\end{alltt}

\pause

\begin{alltt}
[sin; cos];;\\
\ett{val it : (float->float) list = [<fun:...>; <fun:...>]}
\end{alltt}

\pause


\begin{alltt}
[(1,true); (3,true)];;\\
\ett{val it : (int * bool) list = [(1, true); (3, true)]}
\end{alltt}

\pause

\begin{alltt}
[[]; [1]; [1;2]];;\\
\ett{val it : int list list = [[]; [1]; [1; 2]]}
\end{alltt}

\end{frame}



\begin{frame}{Trees for lists}

A non-empty list $\blue [x_1, x_2, \ldots, x_n]$, $n\geq 1$,
consists of
\begin{itemize}
\item a {\em\redbrown head} $\blue x_1$ and \item a {\em\redbrown
tail} $\blue [x_2, \ldots, x_n]$
\end{itemize}

\bigskip
\pause

\begin{center}
\setlength{\unitlength}{1mm}
\begin{picture}(95,55)(-10,0)

\put(3,57.5){\makebox(0,0){\tt ::}} \put(3,55){\line(-1,-1){10}}
\put(3,55){\line(1,-1) {10}} \put(-10,42.5){\makebox(0,0){2}}

\put(15,42.5){\makebox(0,0){\tt ::}} \put(15,40){\line(-1,-1){10}}
\put(15,40){\line(1,-1) {10}} \put(2.5,27.5){\makebox(0,0){3}}

\put(27.5,27.5){\makebox(0,0){\tt ::}}
\put(27.5,25){\line(-1,-1){10}} \put(27.5,25){\line(1,-1) {10}}
\put(15,12.5){\makebox(0,0){2}} \put(40,12.5){\makebox(0,0){\tt
[]}}

\put(12,2.5){\makebox(0,0){Graph for {\tt [2,3,2]}}}

\put(77.5,27.5){\makebox(0,0){\tt ::}}
\put(77.5,25){\line(-1,-1){10}} \put(77.5,25){\line(1,-1) {10}}
\put(65,12.5){\makebox(0,0){2}} \put(90,12.5){\makebox(0,0){\tt
[]}}

\put(77.5,2.5){\makebox(0,0){Graph for {\tt [2]}}}
\end{picture}
\end{center}
\end{frame}




%\subsection{Types}
\begin{frame}{List constructors: \texttt{[]} and \texttt{::}}

Lists are generated as follows:
\begin{itemize}
\item the empty list is a list, designated \texttt{\blue []}
\item if {\blue $x$} is an element and {\blue $\mathit{xs}$} is a list, \\
      then so is {\blue $x\ \mathtt{::}\ \mathit{xs}$} \hfill (type
      consistency)
\end{itemize}

\medskip

\texttt{::} associate to the {\redbrown right}, i.e. {\blue $x_1
\mathtt{::} x_2 \mathtt{::} xs \qquad$} \onslide<2->{
$\mbox{means} \qquad {\blue x_1 \mathtt{::} (x_2 \mathtt{::}
xs)}$}


\bigskip
\pause
\begin{center}
\setlength{\unitlength}{1mm}
\begin{picture}(42.5,41)

\put(15,40.5){\makebox(0,0){\tt ::}} \put(15,38){\line(-1,-1){10}}
\put(15,38){\line(1,-1) {10}} \put(2.5,25.5){\makebox(0,0){$x_1$}}

\put(27.5,25.5){\makebox(0,0){\tt ::}}
\put(27.5,23){\line(-1,-1){10}} \put(27.5,23){\line(1,-1) {10}}
\put(15,10.5){\makebox(0,0){$x_2$}}
\put(40,10.5){\makebox(0,0){$xs$}}

\put(21.25,2.5){\makebox(0,0){Graph for $x_1${\tt ::}$x_2${\tt
::}$xs$}}
\end{picture}
\end{center}
\end{frame}


%\subsection{Recursion on lists}
\begin{frame}[fragile]
\frametitle{Recursion on lists  -- a simple example}
\vspace{-.8cm}
\[
{\tt suml}\, \verb?[?x_1\verb?,? x_2\verb?,? \ldots
\verb?,?x_{n}\verb?]? = \sum_{i=1}^{n} x_i = x_1 + x_2 + \cdots +
x_{n} = x_1 + \sum_{i=2}^{n} x_i
\]

\pause

\ \hfill {\darkgreen Constructors are used in list patterns}
\begin{alltt}
let rec suml = function
  |{\darkgreen []}    -> 0
  |{\darkgreen x::xs} -> x + suml xs;;
> val suml : int list -> int
\end{alltt}

\pause

\[
\begin{array}{lll}
      & \verb/suml [1;2]/ &   \\
 \lto & \verb/1 + suml [2]/ &
{\rm (\verb/x/ \mapsto \verb/1/\
and\ \verb/xs/ \mapsto \verb/[2]/)}\\
\lto & \verb/1 + (2 + suml [])/ &
   {\rm (\verb/x/ \mapsto \verb/2/\
 and\ \verb/xs/ \mapsto \verb/[]/)}\\
 \lto & \verb/1 + (2 + 0)/ &
   {\rm (the\ pattern\ {\darkgreen \verb/[]/}\ matches\ the\ value\ \verb/[]/)}\\
 \lto & \verb/1 + 2/ & \\
 \lto  & \verb/3/  &
\end{array}
\]

\hfill {\redbrown Recursion follows the structure of lists}

\end{frame}



\begin{frame}[fragile]
\frametitle{Infix functions}

It is possible to declare {\redbrown infix functions} in F\#,
i.e.\ the function symbol is {\darkgreen between} the arguments.

\medskip
\pause

The {\blue\em prefix function} on lists is declared as follows:

\begin{alltt}\blue
let rec (<=.) xs ys =
   match (xs,ys) with
   | ([],_) -> true
   | (_,[]) -> false
   | (x::xs',y::ys') -> x=y && {\darkgreen xs' <=. ys'};;

{\darkgreen [1;2;3] <=. [1;2]};;
\ett{val it : bool = false}
\end{alltt}

\begin{itemize}

\item<3-> The special way of declaring the function \verb!(<=.) xs ys! makes
\verb!<=.! an infix operator

\item<3-> The \verb-match (xs,ys)- construct allows for branching out on patterns for \verb-(xs,ys)-

\end{itemize}

\bigskip
\pause[4]

\textcolor{BrickRed}{Suitable use of infix functions can increase
readability significantly}

\end{frame}

\begin{frame}[fragile]{Exercises}

\begin{itemize}

\item \verb-length- $xs$ : the length of the list
  $\mathit{xs}$ (is a predefined function).

\item {\tt remove}$(x, ys)$ : removes all occurrences of $x$ in
the list $\mathit{ys}$


\end{itemize}

\pause
\bigskip

\begin{center}
\color{BrickRed}Have fun with your first non-trivial functional
program:\\ polynomials represented as lists
\end{center}

\end{frame}
\end{document}
