Skip to content

Commit

Permalink
last changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei.winitzki committed Dec 29, 2021
1 parent 9720c48 commit 7c01365
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 85 deletions.
2 changes: 1 addition & 1 deletion sofp-src/book_cover/sofp-cover-parameters.tex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
% -- Ingram: page count MUST be divisible by 2.
% -- Blurb : page count MUST be divisible by 6.
% Add blank pages as needed in final PDF generations!
\pgfmathsetmacro\TotalPageCount{831}% Must be manually entered
\pgfmathsetmacro\TotalPageCount{830}% Must be manually entered
\pgfmathsetmacro\PaperWidthPt{7.444in}%
\pgfmathsetmacro\PaperHeightPt{9.68in}%

Expand Down
4 changes: 2 additions & 2 deletions sofp-src/check_and_make_draft.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ name="sofp"
# When changing the title of any chapter, make sure it is correctly updated in `draft_title_*` and in the `chapters` array below.

# Expected total number of pages in the book draft:
draft_pages=799
draft_pages=798

# The number of pages in each chapter:
pagecounts=(3 17 42 34 24 49 43 12 63 54 63 62 54 27 148 5 5 6 3 6 6 3 6 48 21 1 2 1 1 7)
pagecounts=(3 17 42 34 24 49 43 12 63 54 63 62 53 27 148 5 5 6 3 6 6 3 6 48 21 1 2 1 1 7)

# To create the draft version that contains only proofread chapters:
# cut out from here, including:
Expand Down
82 changes: 27 additions & 55 deletions sofp-src/sofp-traversable.lyx
Original file line number Diff line number Diff line change
Expand Up @@ -21431,11 +21431,11 @@ width "26col%"
status open

\begin_layout Plain Layout
\begin_inset VSpace -100baselineskip%
\begin_inset VSpace -130baselineskip%
\end_inset


\begin_inset Formula $\xymatrix{\xyScaleY{2.0pc}\xyScaleX{5.0pc}L^{A}\ar[rd]\sb(0.4){\text{trav}_{L}^{G,A,B}(g\bef f)~~}\ar[r]\sp(0.5){\text{trav}_{L}^{F,A,B}(g)} & F^{L^{B}}\ar[d]\sp(0.45){f}\\
\begin_inset Formula $\xymatrix{\xyScaleY{1.7pc}\xyScaleX{5.0pc}L^{A}\ar[rd]\sb(0.4){\text{trav}_{L}^{G,A,B}(g\bef f)~~}\ar[r]\sp(0.5){\text{trav}_{L}^{F,A,B}(g)} & F^{L^{B}}\ar[d]\sp(0.45){f}\\
& G^{L^{B}}
}
$
Expand Down Expand Up @@ -22390,10 +22390,9 @@ noprefix "false"
).
To verify the identity law:
\begin_inset Formula
\begin{align*}
& \text{pu}_{F}(\text{wu}_{\text{Id}})\overset{?}{=}\text{wu}_{F}\\
\text{definition of }\text{wu}_{F}:\quad & =\text{pu}_{F}(1)=\text{pu}_{F}(\text{wu}_{\text{Id}})\quad.
\end{align*}
\[
\text{pu}_{F}(\text{wu}_{\text{Id}})\overset{?}{=}\text{wu}_{F}=\text{pu}_{F}(1)=\text{pu}_{F}(\text{wu}_{\text{Id}})\quad.
\]

\end_inset

Expand Down Expand Up @@ -22522,19 +22521,7 @@ p.traverse(f)
\end_inset

-effect.
If
\begin_inset listings
inline true
status open

\begin_layout Plain Layout

p.traverse(f)
\end_layout

\end_inset

does not skip any data stored in
We expect that the data structure of
\begin_inset listings
inline true
status open
Expand All @@ -22546,19 +22533,19 @@ p

\end_inset

, the full data structure of
should be preserved in the final result of
\begin_inset listings
inline true
status open

\begin_layout Plain Layout

p
p.traverse(f)
\end_layout

\end_inset

should be preserved in the final result.
.

\end_layout

Expand Down Expand Up @@ -23045,20 +23032,16 @@ noprefix "false"
\end_layout

\begin_layout Standard
The identity law of
\begin_inset listings
inline true
status open

\begin_layout Plain Layout

traverse
\end_layout
Apart from visiting every stored value exactly once, we expect that all

\begin_inset Formula $F$
\end_inset

-effects returned by
\begin_inset Formula $f$
\end_inset

guarantees that all stored values are visited exactly once.
Another expected property of
are collected by
\begin_inset listings
inline true
status open
Expand All @@ -23070,15 +23053,7 @@ traverse

\end_inset

is that all
\begin_inset Formula $F$
\end_inset

-effects returned by
\begin_inset Formula $f$
\end_inset

are collected exactly once.
exactly once.
To see an example where that expectation fails, consider a simple traversable
functor
\begin_inset Formula $L^{A}=A\times A$
Expand Down Expand Up @@ -23254,6 +23229,10 @@ traverse
\end_inset

, and compute the following composition:
\begin_inset VSpace -15baselineskip%
\end_inset


\begin_inset Formula
\[
p\triangleright\text{trav}_{L}^{F,A,B}(f)\triangleright\big(\text{trav}_{L}^{G,B,C}(g)\big)^{\uparrow F}:F^{G^{L^{C}}}\quad.
Expand All @@ -23262,7 +23241,7 @@ p\triangleright\text{trav}_{L}^{F,A,B}(f)\triangleright\big(\text{trav}_{L}^{G,B
\end_inset


\begin_inset VSpace -50baselineskip%
\begin_inset VSpace -65baselineskip%
\end_inset


Expand Down Expand Up @@ -23347,11 +23326,7 @@ traverse

\end_inset

operation using that functor and obtain a value of the same type
\begin_inset Formula $F^{G^{L^{C}}}$
\end_inset

:
operation using that functor and obtain:
\begin_inset VSpace -50baselineskip%
\end_inset

Expand All @@ -23375,7 +23350,7 @@ width "38col%"
status open

\begin_layout Plain Layout
\begin_inset VSpace -150baselineskip%
\begin_inset VSpace -160baselineskip%
\end_inset


Expand Down Expand Up @@ -30216,12 +30191,9 @@ def access[N: Finite, A](s: SqSize[N, A], i: Int, j: Int): A = s match {
}
\end_layout

\end_inset
\begin_layout Plain Layout

Let us test this code:
\begin_inset listings
inline false
status open
\end_layout

\begin_layout Plain Layout

Expand Down Expand Up @@ -30278,7 +30250,7 @@ Sq
\end_inset

.
To test this code (Figure
For testing (Figure
\begin_inset space ~
\end_inset

Expand Down
40 changes: 17 additions & 23 deletions sofp-src/sofp-traversable.tex
Original file line number Diff line number Diff line change
Expand Up @@ -2880,8 +2880,8 @@ \subsection{Laws of \texttt{traverse}}
$F$ to another arbitrary applicative functor $G$:
\begin{wrapfigure}{l}{0.26\columnwidth}%
\vspace{-1\baselineskip}
$\xymatrix{\xyScaleY{2.0pc}\xyScaleX{5.0pc}L^{A}\ar[rd]\sb(0.4){\text{trav}_{L}^{G,A,B}(g\bef f)~~}\ar[r]\sp(0.5){\text{trav}_{L}^{F,A,B}(g)} & F^{L^{B}}\ar[d]\sp(0.45){f}\\
\vspace{-1.3\baselineskip}
$\xymatrix{\xyScaleY{1.7pc}\xyScaleX{5.0pc}L^{A}\ar[rd]\sb(0.4){\text{trav}_{L}^{G,A,B}(g\bef f)~~}\ar[r]\sp(0.5){\text{trav}_{L}^{F,A,B}(g)} & F^{L^{B}}\ar[d]\sp(0.45){f}\\
& G^{L^{B}}
}
$\vspace{0.5\baselineskip}
Expand Down Expand Up @@ -3118,10 +3118,9 @@ \subsubsection{Example \label{subsec:Example-pure-is-applicative-morphism}\ref{s
\]
We need to show that $\text{pu}_{F}$ obeys the laws~(\ref{eq:identity-law-of-applicative-morphism})\textendash (\ref{eq:composition-law-of-applicative-morphism}).
To verify the identity law:
\begin{align*}
& \text{pu}_{F}(\text{wu}_{\text{Id}})\overset{?}{=}\text{wu}_{F}\\
{\color{greenunder}\text{definition of }\text{wu}_{F}:}\quad & =\text{pu}_{F}(1)=\text{pu}_{F}(\text{wu}_{\text{Id}})\quad.
\end{align*}
\[
\text{pu}_{F}(\text{wu}_{\text{Id}})\overset{?}{=}\text{wu}_{F}=\text{pu}_{F}(1)=\text{pu}_{F}(\text{wu}_{\text{Id}})\quad.
\]
To verify the composition law:
\begin{align*}
& \text{zip}_{F}\big(\text{pu}_{F}(p^{:A})\times\text{pu}_{F}(q^{:B})\big)\overset{?}{=}\text{pu}_{F}(\text{zip}_{\text{Id}}(p\times q))=\text{pu}_{F}(p\times q)\\
Expand All @@ -3138,9 +3137,8 @@ \subsubsection{Example \label{subsec:Example-pure-is-applicative-morphism}\ref{s
as a law, consider for simplicity a function $f$ of type $A\rightarrow F^{A}$.
Then the final result of \lstinline!p.traverse(f)! has type $F^{L^{A}}$,
which can be visualized as a value of type $L^{A}$ wrapped under
an $F$-effect. If \lstinline!p.traverse(f)! does not skip any data
stored in \lstinline!p!, the full data structure of \lstinline!p!
should be preserved in the final result.
an $F$-effect. We expect that the data structure of \lstinline!p!
should be preserved in the final result of \lstinline!p.traverse(f)!.
To formulate this condition in a simple way, we may choose functions
$f$ that always return an \emph{empty} $F$-effect (that is, a result
Expand Down Expand Up @@ -3193,11 +3191,10 @@ \subsubsection{Statement \label{subsec:Statement-identity-law-traverse-simplifie
At the same time, we have: $\text{trav}_{L}(\text{id}\bef f)=\text{trav}_{L}(f)=\text{trav}_{L}(\text{pu}_{F})$.
So, Eq.~(\ref{eq:traverse-identity-law-with-pure}) holds. $\square$
The identity law of \lstinline!traverse! guarantees that all stored
values are visited exactly once. Another expected property of \lstinline!traverse!
is that all $F$-effects returned by $f$ are collected exactly once.
To see an example where that expectation fails, consider a simple
traversable functor $L^{A}=A\times A$ and write the \lstinline!traverse!
Apart from visiting every stored value exactly once, we expect that
all $F$-effects returned by $f$ are collected by \lstinline!traverse!
exactly once. To see an example where that expectation fails, consider
a simple traversable functor $L^{A}=A\times A$ and write the \lstinline!traverse!
method like this:
\begin{lstlisting}
def trav2[A, B, F[_]: WuZip : Functor](f: A => F[B])(la: (A, A)): F[(B, B)] = {
Expand All @@ -3216,25 +3213,24 @@ \subsubsection{Statement \label{subsec:Statement-identity-law-traverse-simplifie
found by composing two \lstinline!traverse! operations. Consider
two different applicative functors ($F$ and $G$), two functions
$f:A\rightarrow F^{B}$ and $g:B\rightarrow G^{C}$, and compute the
following composition:
following composition:\vspace{-0.15\baselineskip}
\[
p\triangleright\text{trav}_{L}^{F,A,B}(f)\triangleright\big(\text{trav}_{L}^{G,B,C}(g)\big)^{\uparrow F}:F^{G^{L^{C}}}\quad.
\]
\vspace{-0.5\baselineskip}
\vspace{-0.65\baselineskip}
\noindent The second \lstinline!traverse! operation needs to be lifted
to \lstinline!F! for the types to match. The result (of type $F^{G^{L^{C}}}$)
looks like a \lstinline!traverse! operation with respect to the functor
$F\circ G$. By Statement~\ref{subsec:Statement-applicative-composition},
the functor $F\circ G$ is applicative. So, we may apply a single
\lstinline!traverse! operation using that functor and obtain a value
of the same type $F^{G^{L^{C}}}$:\vspace{-0.5\baselineskip}
\lstinline!traverse! operation using that functor and obtain:\vspace{-0.5\baselineskip}
\[
p\triangleright\text{trav}_{L}^{F\circ G,A,C}(f\bef g^{\uparrow F}):F^{G^{L^{C}}}\quad.
\]
\begin{wrapfigure}{l}{0.38\columnwidth}%
\vspace{-1.5\baselineskip}
\vspace{-1.6\baselineskip}
$\xymatrix{\xyScaleY{1.5pc}\xyScaleX{3.4pc}L^{A}\ar[dr]\sb(0.35){\text{trav}_{L}^{F\circ G,A,C}(f\bef g^{\uparrow F})~~}\ar[r]\sp(0.5){\text{trav}_{L}^{F,A,B}(f)} & F^{L^{B}}\ar[d]\sp(0.4){\big(\text{trav}_{L}^{G,B,C}(g)\big)^{\uparrow F}}\\
& F^{G^{L^{C}}}
}
Expand Down Expand Up @@ -4477,16 +4473,14 @@ \subsubsection{Statement \label{subsec:Statement-nested-recursive-type-traversab
case Matrix(byIndex) => byIndex((Finite[N].apply(i), Finite[N].apply(j)))
case Next(next) => access[Option[N], A](next, i, j)
}
\end{lstlisting}
Let us test this code:
\begin{lstlisting}
scala> access(matrix2x2, 0, 1)
res0: Int = 12
\end{lstlisting}
Figure~\ref{fig:Full-code-implementing-traverse-for-square-matrix}
shows the complete code of \lstinline!sequence! for \lstinline!Sq!.
To test this code (Figure~\ref{fig:Full-code-implementing-traverse-for-square-matrix-tests}),
For testing (Figure~\ref{fig:Full-code-implementing-traverse-for-square-matrix-tests}),
we define a value \lstinline!matrix2x2List! that represents a square
matrix of lists:
\[
Expand Down
8 changes: 4 additions & 4 deletions sofp-src/sofp.tex
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,9 @@
~\\
ISBN: 978-0-359-76877-6\\
\\
{\scriptsize{}Source hash (sha256): b0cb97196cd4d30add1b48365c08df1401bf4a23b96981957b509b93e72d71d8}\\
{\scriptsize{}Git commit: 803a909f36526cbd50cd1bd0a1ed7c7820241a52}\\
{\scriptsize{}PDF file built by pdfTeX 3.14159265-2.6-1.40.20 (TeX Live 2019) on Wed, 29 Dec 2021 22:06:18 +0100 by Darwin 20.6.0}\\
{\scriptsize{}Source hash (sha256): f32d019833d71d9040b4b392919610a144a44b825477124ef0ce4e2d40a0c14f}\\
{\scriptsize{}Git commit: 9720c487a495e72da004be190a940eb9e41ae867}\\
{\scriptsize{}PDF file built by pdfTeX 3.14159265-2.6-1.40.20 (TeX Live 2019) on Wed, 29 Dec 2021 23:00:15 +0100 by Darwin 20.6.0}\\
~\\
{\scriptsize{}Permission is granted to copy, distribute and/or modify
this document under the terms of the GNU Free Documentation License,
Expand Down Expand Up @@ -320,7 +320,7 @@
type constructions.}\\
{\small{}}\\
{\small{}Long and difficult, yet boring explanations are logically
developed in excruciating detail through 1732 Scala
developed in excruciating detail through 1731 Scala
code snippets, 185 statements with step-by-step derivations,
101 diagrams, 212 solved examples with tested
Scala code, and 266 exercises. Discussions further build
Expand Down

0 comments on commit 7c01365

Please sign in to comment.