

% add the PATH to retrieve cls and sty beamer styles files
\makeatletter
\providecommand*{\input@path}{}
\edef\input@path{{../../support/beamer/}\input@path}% prepend
\makeatother

% \documentclass{pharoslides}
% \documentclass[aspectratio=169]{pharoslides} % format 16/9
\documentclass[withvideo]{pharoslides} % format 16/9 + incrustation video

\graphicspath{{./figures/}{../}{../../support/beamer/imgs/}}

%Information to be included in the title page:
\title{Essence of Dispatch}
\subtitle{Taking Pharo Booleans as Example}
\author{Damien Cassou, Stéphane Ducasse and Luc Fabresse}
\institute{http://stephane.ducasse.free.fr}
\date{}
\slidesid{W3S01}

\begin{document}

\frame[plain]{\titlepage}



\begin{frame}[fragile]

\frametitle{Objectives}

\begin{itemize}
\item Understanding of message passing (late binding)
\begin{itemize}
\item the heart of OOP
\item more an OOP lecture than a Pharo one
\end{itemize}

\item Insight at how beautiful Pharo's implementation is
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Context: Booleans}

In Pharo, Booleans have a superb implementation!

\begin{itemize}
\item \code{\&}, \code{\textbar{}}, \code{not} (eager)
\item \code{or:}, \code{and:} (lazy)
\item \code{ifTrue:ifFalse:}, \code{ifFalse:ifTrue:}
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Three Exercises}

\begin{enumerate}
\item Implement \code{not} (Not)
\item Implement \code{\textbar{}} (Or)
\item What is the goal of these exercises?
\end{enumerate}
\end{frame}



\begin{frame}[fragile]

\frametitle{Exercise 1: Implement Not}

Propose an implementation of Not in a world where:

\begin{itemize}
\item You have: \code{true}, \code{false}
\item You only have objects and messages
\item How would you implement the message \code{not}?
\end{itemize}

\begin{listing}{}
   false not
   -> true

   true not
   -> false
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Hint 1: No conditionals}

The solution does not use conditionals (i.e., no \code{if})
\end{frame}



\begin{frame}[fragile]

\frametitle{Hint 2: With Three Classes}

\begin{itemize}
\item The solution uses three classes:
\begin{itemize}
\item \code{Boolean} (abstract), \code{True} and \code{False}
\end{itemize}

\item \code{true} is the singleton instance of \code{True}
\item \code{false} is the singleton instance of \code{False}
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Hint 2: Three Classes}


\begin{center}
\includegraphics[width=0.7\textwidth]{figures/BooleanHiearchyAndInstances.png}\end{center}

\end{frame}



\begin{frame}[fragile]

\frametitle{Hint 3: How do We Express Choice in OOP?}

In OOP, choice is expressed 

\begin{itemize}
\item By defining classes with compatible methods
\item By sending a message to an instance of such class
\end{itemize}

Example 

\begin{listing}{}
   x open
\end{listing}

\begin{itemize}
\item \code{x} can be a file, a window, a tool,... 
\item The method is \textbf{selected} based on \code{x}'s class
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Implementation of Not in Two Methods}

\begin{listing}{}
False >> not
   "Negation -- answer true since the receiver is false."
   ^ true
\end{listing}

\begin{listing}{}
True >> not
   "Negation -- answer false since the receiver is true."
   ^ false
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Implementation Hierarchy}


\begin{center}
\includegraphics[width=1.1\textwidth]{figures/BooleanHiearchyAndInstancesWithNotMethods.png}\end{center}

\end{frame}



\begin{frame}[fragile]

\frametitle{Message Lookup is Choosing the Right Method}


\begin{center}
\includegraphics[width=1.05\textwidth]{figures/BooleanHiearchyAndInstancesWithNotMethodsLookup.png}\end{center}

\end{frame}



\begin{frame}[fragile]

\frametitle{Boolean Implementation}

\begin{itemize}
\item \code{Boolean} is abstract
\item Subclasses are \code{True} and \code{False} and implement
\begin{itemize}
\item logical operations \code{\&}, \code{not}
\item control structures \code{and:}, \code{or:}, \code{ifTrue:}, \code{ifFalse:}, \code{ifTrue:ifFalse:}, \code{ifFalse:ifTrue:}
\end{itemize}

\end{itemize}

\begin{listing}{}
Boolean>>not
   "Abstract method. Negation: Answer true if the receiver is false, answer false if the receiver is true."
   self subclassResponsibility
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Behavior of Or}

\begin{listing}{}
true | true -> true
true | false -> true
true | anything -> true
\end{listing}

\begin{listing}{}
false | true -> true
false | false -> false
false | anything -> anything
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Implementation of Or in Boolean}

\begin{listing}{}
Boolean >> | aBoolean
   "Abstract method. Evaluating Or: Evaluate the argument.
   Answer true if either the receiver or the argument is true."
   self subclassResponsibility
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Implementation of Or in Class False}

\begin{listing}{}
false | true -> true
false | false -> false
false | anything -> anything
\end{listing}

\begin{listing}{}
False >> | aBoolean
   "Evaluating Or -- answer with the argument, aBoolean."
   ^ aBoolean
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Implementation of Or in Class True}

\begin{listing}{}
true | true -> true
true | false -> true
true | anything -> true
\end{listing}

\begin{listing}{}
True >> | aBoolean
   "Evaluating Or -- answer true since the receiver is true."
   ^ true
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Real Implementation of Or in Class True}

The object \code{true} is the receiver of the message!

\begin{listing}{}
True>> | aBoolean
   "Evaluating disjunction (Or) -- answer true since the receiver is true."
   ^ true
\end{listing}

So we can write it like the following:

\begin{listing}{}
True >> | aBoolean
   "Evaluating disjunction (Or) -- answer true since the receiver is true."
   ^ self
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Or Implementation in Two Methods}


\begin{center}
\includegraphics[width=1.1\textwidth]{figures/BooleanHiearchyAndInstancesWithOrMethods.png}\end{center}

\end{frame}



\begin{frame}[fragile]

\frametitle{Summary}

\begin{itemize}
\item The solution to implement booleans' operations:
\begin{itemize}
\item does NOT use conditionals (if)
\item lets the receiver decide
\end{itemize}

\item Do not ask, tell
\end{itemize}
\end{frame}




% \setlength{\textwidth}{\originaltextwidth}
\setbeamertemplate{background canvas}{\insertBorderForHandout}

\begin{frame}[plain]

\begin{minipage}{\originaltextwidth}
\centering
\large


\vspace{2em}

A course by

\vspace{1em}

\hspace{-20px}\includegraphics[height=32px]{inriaLogo}\hspace{3em}\raisebox{7px}{and}\hspace{3em}\raisebox{-6px}{\includegraphics[height=30px]{logoutop}}

\vspace{2em}

in collaboration with

\vspace{1em}

\includegraphics[height=40px]{unisciellogo}\hskip 1.5em%
\raisebox{5px}{\includegraphics[height=30px]{unitlogo}}\hskip 2em%
\raisebox{8px}{\includegraphics[height=24px]{univLilleLogo}}\hskip 2em%
\raisebox{3px}{\includegraphics[height=29px]{logo-IMT}}\hskip 2em%
\raisebox{1px}{\includegraphics[height=32px]{minesDouaiLogo}}

\vspace{2.5em}

\includegraphics[width=50px]{by-nc-nd} \tiny \hskip 0.5em \raisebox{5px}{Inria 2016}

\medskip

\tiny
Except where otherwise noted, this work is licensed under CC BY-NC-ND 3.0 France

\url{https://creativecommons.org/licenses/by-nc-nd/3.0/fr/} 

\end{minipage}
\end{frame}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
