summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/latex/memorygraphs/memorygraphs.tex
blob: d7f9c811b1bd59206180bd2366de4eece56d873b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
%% memorygraphs.tex
%% Copyright 2018-2019 C. Staps
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status “maintained”.
%
% The Current Maintainer of this work is C. Staps.
%
% This work consists of the files memorygraphs.sty and the documentation in
% memorygraphs.tex and example-fibonacci.tex.
\documentclass[a4paper]{article}

\usepackage{geometry}
\usepackage[hidelinks]{hyperref}
\usepackage{csquotes}
\usepackage{cleveref}

\usepackage[nonewpage]{imakeidx}
\makeindex[intoc]

\usepackage{latexgit}
\usepackage{fancyvrb}
\usepackage{listings}
\usepackage{color}
\definecolor{codebg}{rgb}{0.8,0.8,1}
\lstset{backgroundcolor=\color{codebg},
	frame=single,
	framerule=0pt,
	basicstyle=\ttfamily\footnotesize,
	columns=fullflexible}

\usepackage{memorygraphs}

\newcommand{\memorygraphs}{\textsf{memorygraphs}}
\newcommand{\TikZ}{Ti\textit{k}Z}

\title{The \memorygraphs{} package}
\author{Camil Staps\thanks{info@camilstaps.nl}}
\date{\gitcommitdate[formatDate]}

\newif\ifexamplehorizontal
\newenvironment{example}{%
		\medskip%
		\VerbatimOut{\jobname.tmp}}
	{\endVerbatimOut%
		\ifexamplehorizontal%
			\noindent\begin{minipage}{\linewidth}
				\centering
				\input{\jobname.tmp}
			\end{minipage}
			\par\bigskip
			\noindent\begin{minipage}{\linewidth}
				\lstinputlisting{\jobname.tmp}
			\end{minipage}
		\else%
			\hfill%
			\noindent\begin{minipage}{.4\linewidth}
				\centering
				\input{\jobname.tmp}
			\end{minipage}%
			\noindent\begin{minipage}{.6\linewidth}
				\lstinputlisting{\jobname.tmp}
			\end{minipage}
		\fi%
	}

\makeatletter
\definecolor{keyheading}{rgb}{0.75,0,0}
\newcommand{\keyheading}[2][\pgfutil@empty]{%
	\index{#2@\texttt{#2} key}%
	\edef\pgf@test{#1}%
	\item\texttt{{\color{keyheading}{/tikz/#2}}\ifx\pgf@test\pgfutil@empty\else\,=\,#1\fi}\par}
\newenvironment{keydescription}[2][\pgfutil@empty]{%
		\list{}{\leftmargin=2em\itemindent-\leftmargin}
		\itemsep=0pt
		\parskip=0pt
		\topsep=0pt
		\keyheading[#1]{#2}
	}{\endlist}
\newenvironment{macrodescription}[2][\pgfutil@empty]{%
		\index{#2@\texttt{\textbackslash{}#2}}%
		\list{}{\leftmargin=2em\itemindent-\leftmargin}
		\itemsep=0pt
		\parskip=0pt
		\topsep=0pt
		\item\texttt{{\color{keyheading}{\textbackslash#2}}\,#1}
		\par\parskip3pt
	}{\endlist}
\newcommand{\marg}[1]{\texttt{\textbraceleft}$#1$\texttt{\textbraceright}}
\makeatother

\newcommand{\opt}[1]{$\langle#1\rangle$}

\begin{document}

\maketitle

\begin{center}
\input{example-fibonacci}
\end{center}

\tableofcontents

\section{Introduction}
This is the documentation of the \LaTeX{} package \memorygraphs{}.
It defines some \TikZ{} styles and adds anchors to existing styles that ease the declaration of \enquote{memory graphs}.
It is intended for graphs that represent the memory of a computer program during its execution.

\section{Functionality}

\begin{keydescription}{memory graph}
The \verb$memory graph$ style is to be used on \verb$tikzpicture$.
It sets a different node distance that the author finds suitable for this kind of graphs.

\begin{example}
\begin{tikzpicture}[memory graph]
  \node[draw] (x) {37};
  \node[draw,right=of x] {42};
\end{tikzpicture}
\end{example}
\end{keydescription}

\subsection{Nodes}
The following styles can be used to typeset memory blocks:

\begin{keydescription}{block}
This is the most basic style to define a memory block.
By default, this shape is a rectangle with borders:

\begin{example}
\begin{tikzpicture}[memory graph]
  \node[block] {37};
\end{tikzpicture}
\end{example}
\end{keydescription}

\begin{keydescription}[\opt{n}]{arity}
The \verb$arity$ style can be used to create a node with arguments.
This implies \verb$block$:

\begin{example}
\begin{tikzpicture}[memory graph]
  \node[arity=2] {Cons};
\end{tikzpicture}
\end{example}
\end{keydescription}

\begin{macrodescription}[\marg{i}]{arg}
Because blocks with \verb$arity$ are multipart rectangles,
	one can use \TikZ{}'s \verb$\nodepart$ to put contents in the arguments.
However, it can be confusing that \verb$\nodepart{two}$ refers to the \emph{first} argument,
	so we redefine \verb$\arg$ in \verb$block$s to identify arguments of the memory block:

\begin{example}
\begin{tikzpicture}[memory graph]
	\node[arity=2] {Cons \arg{1} 37 \arg{2} \dots};
\end{tikzpicture}
\end{example}

Should one want to use math mode's \verb$\arg$ in a memory block, they can first rename it:

\begin{example}
\let\matharg\arg
\begin{tikzpicture}[memory graph]
  \node[block] {$\matharg(1)$};
\end{tikzpicture}
\end{example}
\end{macrodescription}

\subsection{Markings}
It is possible to mark the head of memory blocks using triangles in the north east and south east corners.

\begin{keydescription}[\opt{style}]{block mark north east}
\keyheading[\opt{style}]{block mark north west}
\keyheading[\opt{style}]{block mark south east}
\keyheading[\opt{style}]{block mark south west}
With this key, triangular marks can be added to the corners of the head of a node:

\begin{example}
\begin{tikzpicture}[memory graph]
  \node[arity=2,block mark north east] {Cons};
\end{tikzpicture}
\end{example}

It is optional to add a style:

\begin{example}
\begin{tikzpicture}
    [memory graph,every node/.style={block}]
  \node[block mark north east={fill,red}] at (1,1) {1};
  \node[block mark north west={blue}] at (0,1) {2};
  \node[block mark south west={fill,green}] at (0,0) {3};
  \node[block mark south east={brown}] at (1,0) {4};
\end{tikzpicture}
\end{example}

The key is long to avoid clashes with other packages,
	and because it depends on the context what nodes should be marked for.
It is of course possible to define a shorthand in your own document.
One application is to mark nodes that are in head normal form (HNF),
	for which one may define the key \verb$hnf$:

\begin{example}
\tikzset{every block/.style={block mark south east}}
\tikzset{hnf/.style={block mark south east=fill}}
\begin{tikzpicture}[memory graph]
  \node[arity=2,hnf]
    {Cons \arg{1}$\dots$ \arg{2}$\dots$};
  \node[arity=2] at (0,-1)
    {map \arg{1}$\dots$ \arg{2}$\dots$};
\end{tikzpicture}
\end{example}

The size of the rectangles is defined by \verb$\memorygraphs@marklength$,
	which can of course be changed.
The default is \makeatletter\the\memorygraphs@marklength\makeatother.

\begin{example}
\makeatletter
\memorygraphs@marklength=7pt
\makeatother
\begin{tikzpicture}[memory graph]
  \node[arity=2,block mark north west=fill]
    {Cons \arg{1}$\dots$ \arg{2}$\dots$};
\end{tikzpicture}
\end{example}
\end{keydescription}

\subsection{Anchors}
\index{anchors}
Because blocks with \verb$arity$ are multipart rectangles,
	one can use anchors like \verb$two south$ to refer to the south of the second part of a node.
These are aliased as \texttt{arg $i$ south} (and similar for other anchors on multipart nodes),
	where \verb$arg 1$ stands for \verb$two$.
The first block of a node is aliased as \verb$head$ instead of \verb$arg 0$,
	so one can use \verb$head south$.
For \verb$head$, anchors for the corners (\verb$head north east$, etc.) are defined as well.

The parts of multipart rectangles do not normally have a \verb$center$ anchor, but \memorygraphs{} defines these.
One can use both \verb$two center$ and \verb$arg 1 center$ to refer to the center of the first argument of a node.

The additional anchors are shown below.
See the \TikZ{} manual for the predefined anchors.

{\examplehorizontaltrue
\begin{example}
\Large
\begin{tikzpicture}
  \node
    [arity=2,
     line width=.25cm,inner xsep=1.25cm,inner ysep=.75cm,
     color=black!30,fill=yellow!30]
    (v) {Head \arg{1} 1 \arg{2} 2};
  \foreach \anchor/\placement in
    {head north/above,head south/below,head center/below,
     head north east/below,head south east/above,head north west/left,head south west/left,
     arg 1 north/above,arg 1 south/below,arg 1 center/below,
     arg 2 north/above,arg 2 south/below,arg 2 center/below}
    \draw[shift=(v.\anchor)] plot[mark=x] coordinates{(0,0)}
       node[\placement] {\scriptsize\texttt{v.\anchor}};
\end{tikzpicture}
\end{example}
}

\subsection{References}

\begin{keydescription}{ref}
This is a simple style for arrows with a circle at the start and slightly rounded corners:

\begin{example}
\begin{tikzpicture}[memory graph]
  \node[arity=2] (hd) {Cons};
  \node
    [arity=2,below=of hd.arg 2 center,anchor=head north]
    (tl) {Cons};
  \draw[ref] (hd.arg 2 center)
    -- (tl.head north);
  \draw[ref] (tl.arg 2 center)
    |- ($(hd.head north)+(0,.4)$)
    -- (hd.head north);
\end{tikzpicture}
\end{example}
\end{keydescription}

\section{Examples}
\examplehorizontaltrue

\begin{itemize}
\item
The linked list of Fibonacci numbers on the title page was generated with:

\noindent\begin{minipage}{\linewidth}
	\centering
	\input{example-fibonacci.tex}
\end{minipage}
\par\bigskip
\noindent\begin{minipage}{\linewidth}
	\lstinputlisting{example-fibonacci.tex}
\end{minipage}

\item
A cyclical linked list, with unboxed integers:

\begin{example}
\begin{tikzpicture}[memory graph]
  \node[block,arity=2] (xs) {Cons \arg{1} 1};

  \node[block,arity=2,right=of xs.arg 2 east] (xsb) {Cons \arg{1} 2};
  \draw[ref] (xs.arg 2 center) -- (xsb);

  \node[block,arity=2,right=of xsb.arg 2 east] (xsc) {Cons \arg{1} 3};
  \draw[ref] (xsb.arg 2 center) -- (xsc);

  \draw[ref] (xsc.arg 2 center) -- +(0,.6) -| (xs.head north);
\end{tikzpicture}
\end{example}
\end{itemize}

\printindex

\end{document}