handouts/ho09.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Wed, 17 Dec 2014 17:50:34 +0000
changeset 354 8e5e84b14041
parent 352 da5713bcdbb0
child 355 619073c37649
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
276
d7109c6e721d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
\documentclass{article}
d7109c6e721d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
\usepackage{../style}
d7109c6e721d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
\usepackage{../langs}
346
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
     4
\usepackage{../graphics}
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
     5
\usepackage{../grammar}
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
     6
\usepackage{multicol}
354
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
     7
\usepackage{array}
276
d7109c6e721d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
d7109c6e721d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
\begin{document}
d7109c6e721d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
335
06d5fc15594d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 276
diff changeset
    11
\section*{Handout 9 (Static Analysis)}
06d5fc15594d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 276
diff changeset
    12
06d5fc15594d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 276
diff changeset
    13
If we want to improve the safety and security of our programs,
337
92a718b88e14 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 335
diff changeset
    14
we need a more principled approach to programming. Testing is
351
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
    15
good, but as Dijkstra famously wrote: 
346
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    16
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    17
\begin{quote}\it 
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    18
``Program testing can be a very effective way to show the
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    19
\underline{\smash{presence}} of bugs, but it is hopelessly
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    20
inadequate for showing their \underline{\smash{absence}}.''
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    21
\end{quote}
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    22
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    23
\noindent While such a more principled approach has been the
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    24
subject of intense study for a long, long time, only in the
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    25
past few years some impressive results have been achieved. One
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    26
is the complete formalisation and (mathematical) verification
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    27
of a microkernel operating system called seL4.
337
92a718b88e14 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 335
diff changeset
    28
92a718b88e14 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 335
diff changeset
    29
\begin{center}
92a718b88e14 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 335
diff changeset
    30
\url{http://sel4.systems}
92a718b88e14 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 335
diff changeset
    31
\end{center}
92a718b88e14 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 335
diff changeset
    32
350
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
    33
\noindent In 2011 this work was included in the MIT Technology
346
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    34
Review in the annual list of the world’s ten most important
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    35
emerging
337
92a718b88e14 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 335
diff changeset
    36
technologies.\footnote{\url{http://www2.technologyreview.com/tr10/?year=2011}}
338
f1491e0d7be0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 337
diff changeset
    37
While this work is impressive, its technical details are too
346
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    38
enormous for an explanation here. Therefore let us look at
337
92a718b88e14 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 335
diff changeset
    39
something much simpler, namely finding out properties about
338
f1491e0d7be0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 337
diff changeset
    40
programs using \emph{static analysis}.
335
06d5fc15594d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 276
diff changeset
    41
350
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
    42
Static analysis is a technique that checks properties of a
346
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    43
program without actually running the program. This should
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    44
raise alarm bells with you---because almost all interesting
351
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
    45
properties about programs  are equivalent to the halting
346
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    46
problem, which we know is undecidable. For example estimating
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    47
the memory consumption of programs is in general undecidable,
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    48
just like the halting problem. Static analysis circumvents
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    49
this undecidability-problem by essentially allowing answers
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    50
\emph{yes} and \emph{no}, but also \emph{don't know}. With
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    51
this ``trick'' even the halting problem becomes
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    52
decidable\ldots{}for example we could always say \emph{don't
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    53
know}. Of course this would be silly. The point is that we
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    54
should be striving for a method that answers as often as
347
efad8155513f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 346
diff changeset
    55
possible either \emph{yes} or \emph{no}---just in cases when
efad8155513f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 346
diff changeset
    56
it is too difficult we fall back on the
efad8155513f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 346
diff changeset
    57
\emph{don't-know}-answer. This might sound all like abstract
efad8155513f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 346
diff changeset
    58
nonsense. Therefore let us look at a concrete example.
346
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    59
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    60
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    61
\subsubsection*{A Simple, Idealised Programming Language}
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    62
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    63
Our starting point is a small, idealised programming language.
351
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
    64
It is idealised because we cut several corners in comparison
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
    65
with real programming languages. The language we will study
352
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
    66
contains, amongst other things, variables holding integers.
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
    67
Using static analysis, we want to find out what the sign of
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
    68
these integers (positive or negative) will be when the program
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
    69
runs. This sign-analysis seems like a very simple problem. But
354
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
    70
teven such simple problems, if approached naively, are in
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
    71
general undecidable, just like Turing's halting problem. I let
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
    72
you think why?
347
efad8155513f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 346
diff changeset
    73
efad8155513f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 346
diff changeset
    74
efad8155513f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 346
diff changeset
    75
Is sign-analysis of variables an interesting problem? Well,
efad8155513f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 346
diff changeset
    76
yes---if a compiler can find out that for example a variable
efad8155513f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 346
diff changeset
    77
will never be negative and this variable is used as an index
354
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
    78
for an array, then the comopiler does not need to generate code
347
efad8155513f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 346
diff changeset
    79
for an underflow-test. Remember some languages are immune to
351
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
    80
buffer-overflow attacks, but they need to add underflow and
350
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
    81
overflow checks everywhere. If the compiler can omit the
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
    82
underflow test, for example, then this can potentially
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
    83
drastically speed up the generated code.
346
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    84
354
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
    85
What do programs in our simple programming language look like?
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
    86
The following grammar gives a first specification:
346
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    87
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    88
\begin{multicols}{2}
350
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
    89
\begin{plstx}[rhs style=,one per line,left margin=9mm]
346
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    90
: \meta{Stmt} ::= \meta{label} \texttt{:}
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    91
                | \meta{var} \texttt{:=} \meta{Exp}
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    92
                | \texttt{jmp?} \meta{Exp} \meta{label}
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    93
                | \texttt{goto} \meta{label}\\
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    94
: \meta{Prog} ::= \meta{Stmt} \ldots{} \meta{Stmt}\\
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    95
\end{plstx}
350
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
    96
\columnbreak
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
    97
\begin{plstx}[rhs style=,one per line]
: \meta{Exp} ::= \meta{Exp} \texttt{+} \meta{Exp}
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
    98
               | \meta{Exp} \texttt{*} \meta{Exp}
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
    99
               | \meta{Exp} \texttt{=} \meta{Exp} 
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   100
               | \meta{num}
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   101
               | \meta{var}\\
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   102
\end{plstx}
346
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   103
\end{multicols}
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   104
350
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   105
\noindent I assume you are familiar with such
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   106
grammars.\footnote{\url{http://en.wikipedia.org/wiki/Backus–Naur_Form}}
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   107
There are three main syntactic categories: \emph{statments}
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   108
and \emph{expressions} as well as \emph{programs}, which are
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   109
sequences of statements. Statements are either labels,
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   110
variable assignments, conditional jumps (\pcode{jmp?}) and
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   111
unconditional jumps (\pcode{goto}). Labels are just strings,
352
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   112
which can be used as the target of a jump. We assume that in
354
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   113
every program the labels are unique---if there is a clash we
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   114
do not know where to jump to. The conditional jumps and
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   115
variable assignments involve (arithmetic) expressions.
352
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   116
Expressions are either numbers, variables or compound
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   117
expressions built up from \pcode{+}, \pcode{*} and \emph{=}
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   118
(for simplicity reasons we do not consider any other
350
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   119
operations). We assume we have negative and positive numbers,
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   120
\ldots \pcode{-2}, \pcode{-1}, \pcode{0}, \pcode{1},
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   121
\pcode{2}\ldots{} An example program that calculates the
354
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   122
factorial of 5 is in oure programming language as follows:
350
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   123
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   124
\begin{lstlisting}[language={},xleftmargin=10mm]
346
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   125
      a := 1
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   126
      n := 5 
350
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   127
top:  
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   128
      jmp? n = 0 done 
346
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   129
      a := a * n 
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   130
      n := n + -1 
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   131
      goto top 
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   132
done:
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   133
\end{lstlisting}
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   134
354
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   135
\noindent As can be seen each line of the program contains a
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   136
statement. In the first two lines we assign values to the
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   137
variables \pcode{a} and \pcode{n}. In line 4 we test whether
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   138
\pcode{n} is zero, in which case we jump to the end of the
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   139
program marked with the label \pcode{done}. If \pcode{n} is
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   140
not zero, we multiply the content of \pcode{a} by \pcode{n},
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   141
decrease \pcode{n} by one and jump back to the beginning of
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   142
the loop, marked with the label \pcode{top}. Another program
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   143
in our language is shown in Figure~\ref{fib}. I let you think
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   144
what it calculates.
350
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   145
 
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   146
\begin{figure}[t]
346
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   147
\begin{lstlisting}[numbers=none,
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   148
                   language={},xleftmargin=10mm]
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   149
      n := 6
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   150
      m1 := 0
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   151
      m2 := 1
350
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   152
loop: 
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   153
      jmp? n = 0 done
346
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   154
      tmp := m2
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   155
      m2 := m1 + m2
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   156
      m1 := tmp
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   157
      n := n + -1
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   158
      goto top
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   159
done:
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   160
\end{lstlisting}
351
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
   161
\caption{A mystery program in our idealised programming language.
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
   162
Try to find out what it calculates! \label{fib}}
350
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   163
\end{figure}
338
f1491e0d7be0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 337
diff changeset
   164
350
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   165
Even if our language is rather small, it is still Turing
351
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
   166
complete---meaning quite powerful. However, discussing this
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
   167
fact in more detail would lead us too far astray. Clearly, our
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
   168
programming is rather low-level and not very comfortable for
354
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   169
writing programs. It is inspired by real machine code, which
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   170
is the code that is executed by a CPU. So a more interesting
351
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
   171
question is what is missing in comparison with real machine
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
   172
code? Well, not much\ldots{}in principle. Real machine code,
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
   173
of course, contains many more arithmetic instructions (not
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
   174
just addition and multiplication) and many more conditional
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
   175
jumps. We could add these to our language if we wanted, but
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
   176
complexity is really beside the point here. Furthermore, real
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
   177
machine code has many instructions for manipulating memory. We
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
   178
do not have this at all. This is actually a more serious
352
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   179
simplification because we assume numbers to be arbitrary small
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   180
or large, which is not the case with real machine code. In
351
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
   181
real code basic number formats have a range and might
352
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   182
over-flow or under-flow from this range. Also the number of
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   183
variables in our programs is potentially unlimited, while
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   184
memory in an actual computer, of course, is always limited
354
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   185
somehow on any actual. To sum up, our language might look
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   186
ridiculously simple, but it is not much removed from
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   187
practically relevant issues.
276
d7109c6e721d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   188
351
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
   189
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
   190
\subsubsection*{An Interpreter}
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
   191
352
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   192
Designing a language is like playing god: you can say what
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   193
names for variables you allow; what programs should look like;
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   194
most importantly you can decide what each part of the program
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   195
should mean and do. While our language is rather simple and
354
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   196
the meaning of statements, for example, is rather
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   197
straightforward, there are still places where we need to make
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   198
real choices. For example consider the conditional jumps, say
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   199
the one in the factorial program: 
352
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   200
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   201
\begin{center}
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   202
\code{jmp? n = 0 done}
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   203
\end{center}
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   204
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   205
\noindent How should they work? We could introduce Booleans
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   206
(\pcode{true} and \pcode{false}) and then jump only when the
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   207
condition is \pcode{true}. However, since we have numbers in
354
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   208
our language anyway, why not just encoding \pcode{true} as
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   209
one, and \pcode{false} as zero? In this way we can dispense
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   210
with the additional concept of Booleans, {\bf but also we could
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   211
replace the jump above by
352
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   212
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   213
\begin{center}
354
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   214
\code{jmp? 1 + (n + -n) done}
352
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   215
\end{center}
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   216
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   217
\noindent which behaves exactly the same. But what does it
354
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   218
actually mean that two jumps behave the same? Or two programs
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   219
for that matter?}
352
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   220
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   221
I hope the above discussion makes it already clear we need to
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   222
be a bit more careful with our programs. Below we shall
354
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   223
describe an interpreter for our programming language, which
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   224
specifies exactly how programs are supposed to be
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   225
run\ldots{}at least we will specify this for all \emph{good}
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   226
programs. By good programs I mean where all variables are
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   227
initialised, for example. Our interpreter will just crash if
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   228
it cannot find out the value for a variable, in case it is not
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   229
initialised. Also we will assume that labels in good programs
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   230
are unique, otherwise our programs will calculate ``garbage''.
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   231
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   232
First we will pre-process our programs. This will simplify the
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   233
definition of our interpreter later on. We will transform
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   234
programs into \emph{snippets}. A snippet is a label and all
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   235
code that comes after the label. This essentially means a
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   236
snippet is a \emph{map} from labels to code. Given that
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   237
programs are sequences (or lists) of statements, we can easily
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   238
calculate the snippets by just recursively generating the map.
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   239
Suppose a program is of the general form
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   240
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   241
\begin{center}
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   242
$stmt_1\;stmt_2\; \ldots\; stmt_n$
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   243
\end{center}
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   244
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   245
\noindent In order to calculate the snippets of such a
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   246
program, we have to go through the statements one by one and
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   247
check whether they are a label. If yes, we add the label and
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   248
the remaining statements to our map. If no we just continue
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   249
with the next statement. To come up with a recursive
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   250
definition for generating snippets, let us write $[]$ for the
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   251
program that does not contain any statement. Consider the
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   252
following definition:
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   253
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   254
\begin{center}
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   255
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   256
$\textit{snippets}([])$ & $\dn$ & $\varnothing$\\
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   257
$\textit{snippets}(stmt\;\; rest)$ & $\dn$ &
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   258
$\begin{cases}
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   259
   \textit{snippets}(rest)[label := rest] & \text{if}\;stmt = \textit{label:}\\
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   260
   \textit{snippets}(rest)                & \text{otherwise}
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   261
\end{cases}$   
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   262
\end{tabular}
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   263
\end{center}
352
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   264
354
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   265
\noindent In the first clause we just return the empty map for
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   266
the program that does not contain any statements. In the
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   267
second clause, we have to distinguish the case where the first
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   268
statement is a label or not. If not, then we just ``throw
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   269
away'' the label and recursively calculate the snippets for
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   270
the rest of the program. If yes, then we do the same, but also
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   271
update the map so that $label$ now points to the rest of the
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   272
statements. There is one small problem we need to overcome:
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   273
our two programs have no label as entry point---that is where
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   274
the execution starts. We always assume that the first
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   275
statement will be run first. For us it seems convenient if
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   276
we add to all our programs a default label, say 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   277
\pcode{""} (the empty string). With this we can define
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   278
our preprocessing of programs as follows
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   279
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   280
\begin{center}
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   281
$\textit{preproc}(prog) \dn \textit{snippets}(\pcode{"":}\;\; prog)$ 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   282
\end{center} 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   283
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   284
\noindent Let us see how this pans out in practice. If we
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   285
preprocess the factorial program shown earlier we obtain the 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   286
following map:
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   287
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   288
\begin{center}
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   289
\small
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   290
\lstset{numbers=none,
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   291
        language={},
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   292
        xleftmargin=0mm,
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   293
        aboveskip=0.5mm,
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   294
        frame=single,
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   295
        framerule=0mm,
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   296
        framesep=0mm}
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   297
\begin{tikzpicture}[node distance=0mm]
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   298
\node (A1) [draw]{\pcode{""}};
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   299
\node (B1) [right=of A1] {$\mapsto$};
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   300
\node (C1) [right=of B1,anchor=north west] {
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   301
\begin{minipage}{3.5cm}
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   302
\begin{lstlisting}[language={},xleftmargin=0mm]
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   303
  a := 1
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   304
  n := 5 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   305
top:  
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   306
  jmp? n = 0 done 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   307
  a := a * n 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   308
  n := n + -1 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   309
  goto top 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   310
done:
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   311
\end{lstlisting}
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   312
\end{minipage}};
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   313
\node (A2) [right=of C1.north east,draw] {\pcode{top}};
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   314
\node (B2) [right=of A2] {$\mapsto$};
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   315
\node (C2) [right=of B2, anchor=north west] {
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   316
\begin{minipage}{3.5cm}
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   317
\begin{lstlisting}[language={},xleftmargin=0mm]
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   318
  jmp? n = 0 done 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   319
  a := a * n 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   320
  n := n + -1 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   321
  goto top 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   322
done:
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   323
\end{lstlisting} 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   324
\end{minipage}}; 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   325
\node (A3) [right=of C2.north east,draw] {\pcode{done}};
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   326
\node (B3) [right=of A3] {$\mapsto$};
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   327
\node (C3) [right=of B3] {$[]$};
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   328
\end{tikzpicture}
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   329
\end{center}
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   330
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   331
\noindent I highlighted the \emph{keys} in this map. Since
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   332
there are three labels in the factorial program, there are 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   333
three keys.
276
d7109c6e721d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   334
d7109c6e721d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   335
\end{document}
d7109c6e721d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   336
d7109c6e721d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   337
%%% Local Variables: 
d7109c6e721d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   338
%%% mode: latex
d7109c6e721d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   339
%%% TeX-master: t
d7109c6e721d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   340
%%% End: