\documentclass{beamer}
%
%
\makeatletter
\@ifundefined{lhs2tex.lhs2tex.sty.read}%
  {\@namedef{lhs2tex.lhs2tex.sty.read}{}%
   \newcommand\SkipToFmtEnd{}%
   \newcommand\EndFmtInput{}%
   \long\def\SkipToFmtEnd#1\EndFmtInput{}%
  }\SkipToFmtEnd

\newcommand\ReadOnlyOnce[1]{\@ifundefined{#1}{\@namedef{#1}{}}\SkipToFmtEnd}
\usepackage{amstext}
\usepackage{amssymb}
\usepackage{stmaryrd}
\DeclareFontFamily{OT1}{cmtex}{}
\DeclareFontShape{OT1}{cmtex}{m}{n}
  {<5><6><7><8>cmtex8
   <9>cmtex9
   <10><10.95><12><14.4><17.28><20.74><24.88>cmtex10}{}
\DeclareFontShape{OT1}{cmtex}{m}{it}
  {<-> ssub * cmtt/m/it}{}
\newcommand{\texfamily}{\fontfamily{cmtex}\selectfont}
\DeclareFontShape{OT1}{cmtt}{bx}{n}
  {<5><6><7><8>cmtt8
   <9>cmbtt9
   <10><10.95><12><14.4><17.28><20.74><24.88>cmbtt10}{}
\DeclareFontShape{OT1}{cmtex}{bx}{n}
  {<-> ssub * cmtt/bx/n}{}
\newcommand{\tex}[1]{\text{\texfamily#1}}	% NEU

\newcommand{\Sp}{\hskip.33334em\relax}
\newcommand{\NB}{\textbf{NB}}
\newcommand{\Todo}[1]{$\langle$\textbf{To do:}~#1$\rangle$}

\EndFmtInput
\makeatother
%
\author{Piyush P Kurur\\
  Office no: 224\\
  Dept. of Comp. Sci. and Engg.\\
  IIT Kanpur}


\newcommand{\Token}[2][]{\node(#2#1)[token]{#2};}
\newcommand{\Symbol}[2][]{\node(#2#1)[symbol]{#2};}
\newcommand{\Point}[1]{\node(#1)[point]{#1};}

\usepackage{tikz}
\usetikzlibrary{positioning,shapes,chains,fit}
\usetikzlibrary{shapes.symbols}
\usetikzlibrary{matrix}
\usetikzlibrary{backgrounds}
\usetikzlibrary{shapes.geometric}

\usepackage{algorithm2e}
\usepackage{pgfkeys}
\usepackage{multicol}
\pgfkeys %
    {%
      /tikz/coin/.style={
        rectangle,
        minimum height=2.5mm,
        minimum width=1cm,
        draw=black,
        fill=black!20,
        rounded corners
      },
      /tikz/towercolor/.style={
        fill=black!90
      },
      /tikz/towerbase/.style={
        trapezium,
        trapezium angle=45,
        trapezium stretches=true,
        towercolor,
        minimum width=7.5mm,
        minimum height=2.5mm,
      },
      /tikz/tower/.style={
        rectangle,
        rounded corners,
        towercolor,
        minimum width=2.5mm,
        minimum height=17.5mm,
      }
    }

\title{Fundamentals of Computing: Lecture 12}
\date{August 24, 2009}
\begin{document}
\begin{frame}
  \maketitle
\end{frame}

\begin{frame}
  \frametitle{Summary of the last class}
  \begin{tabbing}\tt
~~~~~while\char40{}~C~\char41{}\\
\tt ~~~~~\char123{}~\char47{}\char42{}~invariant~is~a~condition~that~is~true~here~\char42{}\char47{}\\
\tt ~~~~~~~S\\
\tt ~~~~~\char125{}
\end{tabbing}
  
  \begin{block}{How to design loop?}
    \begin{itemize}
    \item Write down the desired outcome $\varphi$.
      \pause
    \item Choose a loop invariant $I$ and a loop condition $C$ 
      such that $I \land \lnot C \Rightarrow \varphi$.
      \pause
    \item $I$ is obtained by parameterisation (i.e. replacing
      constants of $\varphi$ by variables)
      \pause
    \item The condition $C$ is the negation of parameterisation.
      \pause
    \item Initialise variables so that $I$ is true in the base case
      \pause
    \item Design the body $S$ to preserve the validity of $I$.
    \end{itemize}
      
  \end{block}
\end{frame}

\begin{frame}
  \frametitle{Finding the smallest in a sequence of $n$ numbers}

  The desired condition is 
  \[ \varphi \equiv  s = \mathrm{min}\{ a[0],\ldots, a[n-1]\}
  \textrm{ (n is a constant here).} \] 
  \pause
  Parameterising we get the invariant
    \[ I_i \equiv s = \mathrm{min}\{ a[0],\ldots, a[i-1]\}. \]
  \pause
  Note that 
  \[ I_i \land (i = {n}) \Rightarrow \varphi. \]
  So the condition C is $i \neq n$.

  Hence the loop.

  \begin{tabbing}\tt
~~~~~s~\char61{}~a\char91{}0\char93{}\char59{}\\
\tt ~~~~~i~\char61{}~1\char59{}~~\\
\tt ~~~~~while\char40{}~i~\char33{}\char61{}~n~\char41{}\\
\tt ~~~~~\char123{}\\
\tt ~~~~~~~if\char40{}~s~\char62{}~a\char91{}i\char93{}~\char41{}~s~\char61{}~a\char91{}i\char93{}\char59{}\\
\tt ~~~~~~~i\char43{}\char43{}\char59{}\\
\tt ~~~~~\char125{}
\end{tabbing}
\end{frame}



\newcommand{\Length}[1]{\mathrm{length}\left(#1\right)}
\newcommand{\SortedArray}[1]{\mathrm{SortedArray}\left(#1\right)}
\newcommand{\Sorted}[2]{\mathrm{Sorted}\left(#1,#2\right)}
\begin{frame}
  \frametitle{Sorting}
  Let us define what is sorted array 
  \[ 
  \SortedArray{a} \equiv \forall i\ 0 \leq i < \Length{a} - 1 \Rightarrow a[i] \leq a[i+1].
  \]
  \pause
    \[
  \Sorted a s \equiv \forall i \  0 \leq i < s \Rightarrow a[i] \leq a[i+1]
  \]
\end{frame}

\newcommand{\SortedP}[3]{\mathrm{Sorted}'(#1,#2,#3)}

\begin{frame}
  Choose the invariant $\Sorted{a}{i}$ for a parameter $i$.
  
  \begin{tabbing}\tt
~~~~~i~\char61{}~0\char59{}\\
\tt ~~~~~while\char40{}~i~\char33{}\char61{}~n\char41{}\\
\tt ~~~~~\char123{}\\
\tt ~~~~~~~S\char59{}~\char47{}\char42{}~Do~something~to~restore~invariant~\char42{}\char47{}\\
\tt ~~~~~~~i\char43{}\char43{}\char59{}\\
\tt ~~~~~\char125{}
\end{tabbing}

  \pause
  $S$ itself is a loop statement.

  \[ 
  \SortedP a  r k \equiv \forall j\  0 \leq j < r  \ a[j] \leq a[j+1] )
  \lor  j = k - 1
  \]
  
  \begin{tabbing}\tt
~~~~~k~\char61{}~i\char43{}1\char59{}\\
\tt ~~~~~while\char40{}~k~\char33{}\char61{}~0~\char41{}\\
\tt ~~~~~\char123{}\\
\tt ~~~~~~~if\char40{}~a\char91{}k\char45{}1\char93{}~\char62{}~a\char91{}k\char93{}~\char41{}~\char47{}\char42{}~swap~a\char91{}k\char45{}1\char93{}~and~a\char91{}k\char93{}~\char42{}\char47{}\\
\tt ~~~~~~~k\char45{}\char45{}\char59{}\\
\tt ~~~~~\char125{}
\end{tabbing}


\end{frame}
\end{document}
