Skip to content
Snippets Groups Projects
Commit 0eed2fa1 authored by JustinStaples Staples's avatar JustinStaples Staples
Browse files

complete version of T03 slides, adding slides on motivation, types, quantifiers

parent 470e6d18
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -105,6 +105,17 @@ McMaster University\\ }
}
\end{frame}
\section{Motivation}
\begin{frame}
\frametitle{Motivation}
\begin{itemize}
\item A software specification that is written in natural language is likely to be interpreted in multiple different ways.
\item A formal specification aims to remove these ambiguities.
\item Mathematics is the formal language that we use to specify what a program should do.
\end{itemize}
\end{frame}
\section{Operators}
\begin{frame}
......@@ -134,8 +145,9 @@ McMaster University\\ }
\item Binary operators take two operands
\item For this class, we will primarily use:
\begin{itemize}
\item $\vee$, $\wedge$, $\Rightarrow$, $\equiv$
\item $+$, $-$, $*$, $/$
\item $\leq$, $\geq$, $<$, $>$, $=$
\item $\vee$, $\wedge$, $\Rightarrow$, $\equiv$
\end{itemize}
\item With so many different operators, it is important to keep in mind the types of the operands and the types of the return values.
\end{itemize}
......@@ -217,16 +229,28 @@ McMaster University\\ }
\item A set is an unordered collection of elements of the same type.
\item Any given element is either in the set or it is not.
\item Sets do not contain duplicate elements.
\item The following are examples of sets:
$$ S = \{0, 1, 2\} $$
$$ C = \{green, blue, yellow\} $$
$$ P = \{ x : \mathbb{N} \mid 1 \leq x < 4 : x^2 \} $$
\item The following are not examples of sets:
$$ T = \{0, 0, 1, 2\} $$
$$ B = \{ 0, 5, yellow, c \}$$
\begin{center}
Exercise: Identify which of the following are sets
$$ A = \{green, blue, yellow\} $$
$$ B = \{0, 0, 1, 2\} $$
$$ C = \{-5, 5, 10\} $$
$$ D = \{\} $$
$$ E = \{ 0, 5, yellow, c \}$$
\end{center}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Sets}
Solution:
\begin{itemize}
\item $A$ and $C$ are sets because they have unique elements that are all of the same type.
\item $D$ is a special set called the empty set.
\item $B$ and $E$ are not sets because they either have duplicate elements or elements of different types.
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Set Operations}
\begin{itemize}
......@@ -263,7 +287,7 @@ McMaster University\\ }
\item The predicate is a boolean value that tests which elements to consider.
\item The expression is what should be added to the set in the event that the predicate was satisfied.
\begin{center}
Exercise: Consider the set defined a few slides back. $ P = \{ x : \mathbb{N} \mid 1 \leq x < 4 : x^2 \} $. Lets use the definition of set comprehension to enumerate the elements in this set.
Exercise: Consider the following set. $ P = \{ x : \mathbb{N} \mid 1 \leq x < 5 : x^2 \} $. Lets use the definition of set comprehension to enumerate the elements in this set.
\end{center}
\end{itemize}
\end{frame}
......@@ -271,10 +295,10 @@ McMaster University\\ }
\begin{frame}
\frametitle{Set Building (Comrehension)}
\begin{itemize}
\item The set $P$ contains the square of the all the natural numbers from 1 to 3.
\item The set $P$ contains the square of the all the natural numbers from 1 to 4.
\end{itemize}
\begin{center}
Solution: $$ P = \{1, 4, 9\} $$
Solution: $$ P = \{1, 4, 9, 16\} $$
\end{center}
\end{frame}
......@@ -288,15 +312,34 @@ McMaster University\\ }
\item Types are used to specify the type of a variable.
\item You can think of a type as a set of values. This gives us a nice way of defining types.
\item For example, a variable of type colour would be an element of the set $C = \{red, blue, green, yellow, orange, purple\}$.
\item An variable of type Integer would be an element of the set $\mathbb{Z} = \{...-2, 1, 0, 1, 2, ...\}$
\item A variable of type Integer would be an element of the set $\mathbb{Z} = \{...-2, 1, 0, 1, 2, ...\}$
\item The type of a variable can be specified with the following notation. $x : \mathbb{Z}$ (x is an integer) or $y : C$ (y is a colour).
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Primitive Types}
\begin{itemize}
\item You should already be familiar with a few standard primitive types. These are:
\begin{itemize}
\item Integer ($\mathbb{Z}$)
\item Real ($\mathbb{R}$)
\item Boolean (True, False)
\item Character (the set of ASCII characters)
\item String (a finite sequence of characters)
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Custom Types}
\begin{itemize}
\item We are not limited to the use of these primitives!
\item As stated previously, new types can be defined using sets. For example, the colour type mentioned before: $C = \{red, blue, green, yellow, orange, purple\}$
\item What if we wanted to define a point data type, called PointT? PointT = tuple of (x : $\mathbb{R}$, y : $\mathbb{R}$).
\item From your A1, the CurveT type might be defined as CurveT = tuple of (x : SeqT, y : SeqT).
\end{itemize}
\end{frame}
% ---------------------------------------------------
......@@ -304,19 +347,129 @@ McMaster University\\ }
\begin{frame}
\frametitle{Quantifiers}
\begin{itemize}
\item A quantifier is a shorthand notation for applying an operator many times over. The general form of a quantifier is
\begin{center}
$(*x : X \mid R : P)$
\end{center}
\begin{itemize}
\item x is an element of type X.
\item R, the range, determines which values of X should be considered.
\item P, the values that the operator $*$ will be applied to.
\end{itemize}
\item This notation looks a lot like the set building notation, and for good reason! Lets see why with an example.
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Quantifiers}
\begin{center}
Exercise: Evaluate p = $(+x : \mathbb{N} \mid 1 \leq x < 5: x^2)$
\end{center}
\end{frame}
\begin{frame}
\frametitle{Quantifiers}
\begin{center}
Solution:
\end{center}
\begin{itemize}
\item To evaluate, first build the set of elements defined by the range and predicate. For this example, we have already seen this set before (P = \{1, 4, 9, 16\}).
\item Then, apply the operator, in this case $+$, to all of the elements. $p = 1 + 4 + 9 + 16 = 30$
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Quantifiers}
\begin{itemize}
\item We have special names and symbols for quantifications that use $\wedge$ or $\vee$ as the operator.
\item For $\wedge$, the universal quantifier, $(\forall x : X \mid R : P)$, asserts that for all x in the range R, the predicate P is satisfied.
\item For $\vee$, the existential quantifier, $(\exists x : X \mid R : P)$, asserts that there exists an x in the range R where the predicate P is satisfied.
\end{itemize}
\end{frame}
\section{Practice Exercises}
\begin{frame}
\frametitle{Practice}
Exercise 1: Imagine someone has given you the following description of what it means for a set of integers A to be a subset of set B. 'Set A is a subset of set B if all the integers in A are also in B'. Write this out formally using a quantification. Start with: $$ A \subseteq B \equiv (your\ answer\ here) $$
\end{frame}
\begin{frame}
\frametitle{Practice}
\begin{center}
Solution: $$ A \subseteq B \equiv (\forall x : \mathbb{Z} \mid x \in A : x \in B) $$
\end{center}
\begin{itemize}
\item This can be written out by quantifying over all integers.
\item The range tells us to consider only integers that are in set A.
\item The predicate returns True when those elements are also in set B.
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Practice}
\begin{center}
Is A = \{1, 2, 3\} a subset of B = \{1, 2\}?
\end{center}
\begin{itemize}
\item Using the definition, $$ A \subseteq B \equiv (\forall x : \mathbb{Z} \mid x \in A : x \in B)$$
$$ \equiv (1 \in B \wedge 2 \in B \wedge 3 \in B) $$
$$ \equiv True \wedge True \wedge False $$
$$ \equiv False $$
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Practice}
Exercise 2: Given a set of integers S, what is the meaning of the following expression?
$$ (+x : \mathbb{N} \mid x \in S \wedge x \% 2 = 0 : 1) $$
\end{frame}
\begin{frame}
\frametitle{Practice}
\begin{center}
Solution: the count of all the even integers in S
\end{center}
\begin{itemize}
\item $x\%2 = 0$ means that the integer is divisible by 2.
\item 1 is contributed every time this occurs, which effectively counts the elements.
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Practice}
Exercise 3: Evaluate the following quantifications
$$ a = (\forall x : \mathbb{Z} \mid -3 <= x <= 3 : (\exists y : \mathbb{Z} \mid -3 <= y <= 3 : x + y = 0)) $$
$$ b = (\exists x : \mathbb{Z} \mid -3 <= x <= 3 : (\forall y : \mathbb{Z} \mid -3 <= y <= 3 : x + y = 0)) $$
\end{frame}
\begin{frame}
\frametitle{Practice}
\begin{center}
Solution: a = True, b = False
\end{center}
\begin{itemize}
\item a is True because for every x in the range $-3 <= x <= 3$, there is a y in the same range that can be added to x to give 0.
\item b is false because there is no such number in the range $-3 <= x <= 3$ that gives 0 when added to each other number in that range.
\item It is useful to break nested quantifiers up into smaller, simpler parts.
\item The order can change the entire meaning.
\end{itemize}
\end{frame}
\section{References}
\begin{frame}
\frametitle{References}
\begin{itemize}
\item A Logical Approach to Discrete Math (Gries and Schneider)
\item L08 (Lecture Notes 8 Mathematics for MIS)
\end{itemize}
\end{frame}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment