handouts/ho09.tex
author Christian Urban <urbanc@in.tum.de>
Tue, 23 Aug 2016 15:37:59 +0200
changeset 460 ab7acd13bef3
parent 404 4e3bc09748f7
child 496 cc4f49397abc
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}
276
d7109c6e721d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
d7109c6e721d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
\begin{document}
366
34a8f73b2c94 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
     9
\fnote{\copyright{} Christian Urban, 2014}
276
d7109c6e721d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
404
4e3bc09748f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 370
diff changeset
    11
%% why are shuttle flights so good with software
4e3bc09748f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 370
diff changeset
    12
%%http://www.fastcompany.com/28121/they-write-right-stuff
4e3bc09748f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 370
diff changeset
    13
335
06d5fc15594d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 276
diff changeset
    14
\section*{Handout 9 (Static Analysis)}
06d5fc15594d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 276
diff changeset
    15
06d5fc15594d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 276
diff changeset
    16
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
    17
we need a more principled approach to programming. Testing is
359
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
    18
good, but as Edsger Dijkstra famously wrote: 
346
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    19
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    20
\begin{quote}\it 
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    21
``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
    22
\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
    23
inadequate for showing their \underline{\smash{absence}}.''
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    24
\end{quote}
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    25
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    26
\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
    27
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
    28
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
    29
is the complete formalisation and (mathematical) verification
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    30
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
    31
92a718b88e14 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 335
diff changeset
    32
\begin{center}
92a718b88e14 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 335
diff changeset
    33
\url{http://sel4.systems}
92a718b88e14 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 335
diff changeset
    34
\end{center}
92a718b88e14 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 335
diff changeset
    35
350
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
    36
\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
    37
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
    38
emerging
337
92a718b88e14 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 335
diff changeset
    39
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
    40
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
    41
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
    42
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
    43
programs using \emph{static analysis}.
335
06d5fc15594d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 276
diff changeset
    44
350
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
    45
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
    46
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
    47
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
    48
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
    49
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
    50
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
    51
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
    52
this undecidability-problem by essentially allowing answers
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    53
\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
    54
this ``trick'' even the halting problem becomes
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    55
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
    56
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
    57
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
    58
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
    59
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
    60
\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
    61
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
    62
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    63
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    64
\subsubsection*{A Simple, Idealised Programming Language}
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    65
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    66
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
    67
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
    68
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
    69
contains, amongst other things, variables holding integers.
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
    70
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
    71
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
    72
runs. This sign-analysis seems like a very simple problem. But
355
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    73
even such simple problems, if approached naively, are in
354
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
    74
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
    75
you think why?
347
efad8155513f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 346
diff changeset
    76
efad8155513f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 346
diff changeset
    77
efad8155513f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 346
diff changeset
    78
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
    79
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
    80
will never be negative and this variable is used as an index
355
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    81
for an array, then the compiler does not need to generate code
357
5b91f5ad2772 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 356
diff changeset
    82
for an underflow-check. Remember some languages are immune to
351
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
    83
buffer-overflow attacks, but they need to add underflow and
359
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
    84
overflow checks everywhere. According to John Regher, an
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
    85
expert in the field of compilers, overflow checks can cause
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
    86
5-10\% slowdown, in some languages even 100\% for tight
357
5b91f5ad2772 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 356
diff changeset
    87
loops.\footnote{\url{http://blog.regehr.org/archives/1154}} If
5b91f5ad2772 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 356
diff changeset
    88
the compiler can omit the underflow check, for example, then
5b91f5ad2772 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 356
diff changeset
    89
this can potentially drastically speed up the generated code. 
346
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    90
354
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
    91
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
    92
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
    93
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    94
\begin{multicols}{2}
350
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
    95
\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
    96
: \meta{Stmt} ::= \meta{label} \texttt{:}
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    97
                | \meta{var} \texttt{:=} \meta{Exp}
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    98
                | \texttt{jmp?} \meta{Exp} \meta{label}
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
    99
                | \texttt{goto} \meta{label}\\
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   100
: \meta{Prog} ::= \meta{Stmt} \ldots{} \meta{Stmt}\\
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   101
\end{plstx}
350
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   102
\columnbreak
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   103
\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
   104
               | \meta{Exp} \texttt{*} \meta{Exp}
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   105
               | \meta{Exp} \texttt{=} \meta{Exp} 
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   106
               | \meta{num}
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   107
               | \meta{var}\\
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   108
\end{plstx}
346
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   109
\end{multicols}
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   110
350
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   111
\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
   112
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
   113
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
   114
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
   115
sequences of statements. Statements are either labels,
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   116
variable assignments, conditional jumps (\pcode{jmp?}) and
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   117
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
   118
which can be used as the target of a jump. We assume that in
355
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   119
every program the labels are unique---if there is a clash,
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   120
then we do not know where to jump to. The conditional jumps
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   121
and variable assignments involve (arithmetic) expressions.
352
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   122
Expressions are either numbers, variables or compound
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   123
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
   124
(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
   125
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
   126
\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
   127
\pcode{2}\ldots{} An example program that calculates the
359
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   128
factorial of 5 is in our programming language as follows:
350
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   129
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   130
\begin{lstlisting}[language={},xleftmargin=10mm]
346
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   131
      a := 1
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   132
      n := 5 
350
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   133
top:  
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   134
      jmp? n = 0 done 
346
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   135
      a := a * n 
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   136
      n := n + -1 
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   137
      goto top 
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   138
done:
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   139
\end{lstlisting}
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   140
354
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   141
\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
   142
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
   143
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
   144
\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
   145
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
   146
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
   147
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
   148
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
   149
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
   150
what it calculates.
350
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   151
 
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   152
\begin{figure}[t]
346
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   153
\begin{lstlisting}[numbers=none,
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   154
                   language={},xleftmargin=10mm]
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   155
      n := 6
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   156
      m1 := 0
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   157
      m2 := 1
350
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   158
loop: 
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   159
      jmp? n = 0 done
346
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   160
      tmp := m2
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   161
      m2 := m1 + m2
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   162
      m1 := tmp
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   163
      n := n + -1
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   164
      goto top
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   165
done:
5a6e8b7d20f7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 338
diff changeset
   166
\end{lstlisting}
351
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
   167
\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
   168
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
   169
\end{figure}
338
f1491e0d7be0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 337
diff changeset
   170
350
54d6fc856950 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
   171
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
   172
complete---meaning quite powerful. However, discussing this
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
   173
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
   174
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
   175
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
   176
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
   177
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
   178
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
   179
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
   180
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
   181
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
   182
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
   183
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
   184
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
   185
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
   186
or large, which is not the case with real machine code. In
368
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   187
real machine code, basic number formats have a range and might
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   188
over-flow or under-flow this range. Also the number of
352
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   189
variables in our programs is potentially unlimited, while
368
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   190
memory in an actual computer, of course, is always limited. To
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   191
sum up, our language might look ridiculously simple, but it is
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   192
not too far removed from practically relevant issues.
276
d7109c6e721d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   193
351
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
   194
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
   195
\subsubsection*{An Interpreter}
cd8d18f7b7ac updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 350
diff changeset
   196
352
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   197
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
   198
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
   199
most importantly you can decide what each part of the program
360
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   200
should mean and do. While our language is quite simple and the
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   201
meaning of statements, for example, is rather straightforward,
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   202
there are still places where we need to make real choices. For
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   203
example consider the conditional jumps, say the one in the
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   204
factorial program: 
352
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   205
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   206
\begin{center}
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   207
\code{jmp? n = 0 done}
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   208
\end{center}
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   209
da5713bcdbb0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 351
diff changeset
   210
\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
   211
(\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
   212
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
   213
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
   214
one, and \pcode{false} as zero? In this way we can dispense
355
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   215
with the additional concept of Booleans.
352
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
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
   218
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
   219
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
   220
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
   221
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
   222
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
   223
initialised, for example. Our interpreter will just crash if
359
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   224
it cannot find out the value for a variable when it is not
355
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   225
initialised. Also, we will assume that labels in good programs
354
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   226
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
   227
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   228
First we will pre-process our programs. This will simplify the
359
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   229
definition of our interpreter later on. By pre-processing our
360
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   230
programs we will transform programs into \emph{snippets}. A
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   231
snippet is a label and all the code that comes after the
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   232
label. This essentially means a snippet is a \emph{map} from
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   233
labels to code.\footnote{Be sure you know what maps are. In a
359
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   234
programming context they are often represented as association
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   235
list where some data is associated with a key.} 
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   236
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   237
Given that programs are sequences (or lists) of statements, we
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   238
can easily calculate the snippets by just traversing this
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   239
sequence and recursively generating the map. Suppose a program
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   240
is of the general form
354
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   241
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   242
\begin{center}
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   243
$stmt_1\;stmt_2\; \ldots\; stmt_n$
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   244
\end{center}
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   245
355
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   246
\noindent The idea is to go through this sequence of
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   247
statements one by one and check whether they are a label. If
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   248
yes, we add the label and the remaining statements to our map.
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   249
If no, we just continue with the next statement. To come up
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   250
with a recursive definition for generating snippets, let us
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   251
write $[]$ for the program that does not contain any
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   252
statement. Consider the following definition:
354
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
355
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   266
the program that does not contain any statement. In the second
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   267
clause, we have to distinguish the case where the first
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   268
statement is a label or not. As said before, if not, then we
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   269
just ``throw away'' the label and recursively calculate the
368
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   270
snippets for the rest of the program (the otherwise clause).
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   271
If yes, then we do the same, but also update the map so that
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   272
$label$ now points to the rest of the statements (the if
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   273
clause). This looks all realtively straightforward, but there
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   274
is one small problem we need to overcome: our two programs
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   275
shown so far have no label as \emph{entry point}---that is
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   276
where the execution is supposed to start. We usually assume
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   277
that the first statement will be run first. To make this the
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   278
default, it is convenient if we add to all our programs a
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   279
default label, say \pcode{""} (the empty string). With this we
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   280
can define our pre-processing of programs as follows
354
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   281
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   282
\begin{center}
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   283
$\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
   284
\end{center} 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   285
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   286
\noindent Let us see how this pans out in practice. If we
355
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   287
pre-process the factorial program shown earlier, we obtain the 
354
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   288
following map:
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   289
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   290
\begin{center}
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   291
\small
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   292
\lstset{numbers=none,
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   293
        language={},
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   294
        xleftmargin=0mm,
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   295
        aboveskip=0.5mm,
355
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   296
        belowskip=0.5mm,
354
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   297
        frame=single,
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   298
        framerule=0mm,
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   299
        framesep=0mm}
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   300
\begin{tikzpicture}[node distance=0mm]
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   301
\node (A1) [draw]{\pcode{""}};
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   302
\node (B1) [right=of A1] {$\mapsto$};
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   303
\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
   304
\begin{minipage}{3.5cm}
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   305
\begin{lstlisting}[language={},xleftmargin=0mm]
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   306
  a := 1
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   307
  n := 5 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   308
top:  
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   309
  jmp? n = 0 done 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   310
  a := a * n 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   311
  n := n + -1 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   312
  goto top 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   313
done:
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   314
\end{lstlisting}
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   315
\end{minipage}};
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   316
\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
   317
\node (B2) [right=of A2] {$\mapsto$};
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   318
\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
   319
\begin{minipage}{3.5cm}
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   320
\begin{lstlisting}[language={},xleftmargin=0mm]
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   321
  jmp? n = 0 done 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   322
  a := a * n 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   323
  n := n + -1 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   324
  goto top 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   325
done:
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   326
\end{lstlisting} 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   327
\end{minipage}}; 
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   328
\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
   329
\node (B3) [right=of A3] {$\mapsto$};
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   330
\node (C3) [right=of B3] {$[]$};
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   331
\end{tikzpicture}
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   332
\end{center}
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   333
8e5e84b14041 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   334
\noindent I highlighted the \emph{keys} in this map. Since
356
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   335
there are three labels in the factorial program (remember we
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   336
added \pcode{""}), there are three keys. When running the
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   337
factorial program and encountering a jump, then we only have
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   338
to consult this snippets-map in order to find out what the
359
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   339
next statements should be.
355
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   340
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   341
We should now be in the position to define how a program
356
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   342
should be run. In the context of interpreters, this
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   343
``running'' of programs is often called \emph{evaluation}. Let
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   344
us start with the definition of how expressions are evaluated.
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   345
A first attempt might be the following recursive function:
355
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   346
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   347
\begin{center}
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   348
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
356
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   349
$\textit{eval\_exp}(\texttt{n})$ & $\dn$ & $n$
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   350
  \qquad\text{if}\; \texttt{n}\; \text{is a number like} 
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   351
  \ldots \pcode{-2}, \pcode{-1}, \pcode{0}, \pcode{1},
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   352
\pcode{2}\ldots{}\\
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   353
$\textit{eval\_exp}(\texttt{e}_\texttt{1} \,\texttt{+}\, 
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   354
                    \texttt{e}_\texttt{2})$ & 
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   355
  $\dn$ & $\textit{eval\_exp}(\texttt{e}_\texttt{1}) + 
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   356
           \textit{eval\_exp}(\texttt{e}_\texttt{2})$\\
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   357
$\textit{eval\_exp}(\texttt{e}_\texttt{1} \,\texttt{*}\, 
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   358
                    \texttt{e}_\texttt{2})$ & 
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   359
  $\dn$ & $\textit{eval\_exp}(\texttt{e}_\texttt{1}) * 
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   360
           \textit{eval\_exp}(\texttt{e}_\texttt{2})$\\
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   361
$\textit{eval\_exp}(\texttt{e}_\texttt{1} \,\texttt{=}\, 
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   362
                    \texttt{e}_\texttt{2})$ & 
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   363
  $\dn$ & $\begin{cases}
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   364
  1 & \text{if}\;\textit{eval\_exp}(\texttt{e}_\texttt{1}) =
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   365
                 \textit{eval\_exp}(\texttt{e}_\texttt{2})\\
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   366
  0 & \text{otherwise}
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   367
  \end{cases}$          
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   368
\end{tabular}
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   369
\end{center}
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   370
368
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   371
\noindent While this should look all rather intuitive`, still
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   372
be very careful. There is a subtlety which can be easily
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   373
overlooked: The function \textit{eval\_exp} takes an
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   374
expression of our programming language as input and returns a
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   375
number as output. Therefore whenever we encounter a number in
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   376
our program, we just return this number---this is defined in
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   377
the first clause above. Whenever we encounter an addition,
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   378
well then we first evaluate the left-hand side
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   379
$\texttt{e}_\texttt{1}$ of the addition (this will give a
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   380
number), then evaluate the right-hand side
356
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   381
$\texttt{e}_\texttt{2}$ (this gives another number), and
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   382
finally add both numbers together. Here is the subtlety: on
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   383
the left-hand side of the $\dn$ we have a \texttt{+} (in the
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   384
teletype font) which is the symbol for addition in our
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   385
programming language. On the right-hand side we have $+$ which
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   386
stands for the arithmetic operation from ``mathematics'' of
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   387
adding two numbers. These are rather different concepts---one
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   388
is a symbol (which we made up), and the other a mathematical
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   389
operation. When we will have a look at an actual
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   390
implementation of our interpreter, the mathematical operation
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   391
will be the function for addition from the programming
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   392
language in which we \underline{\smash{implement}} our
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   393
interpreter. While the \texttt{+} is just a symbol that is
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   394
used in our programming language. Clearly we have to use a
368
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   395
symbol that is a good mnemonic for addition, otherwise we will
356
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   396
confuse the programmers working with our language. Therefore
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   397
we use $\texttt{+}$. A similar choice is made for times in the
368
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   398
third clause and equality in the fourth clause. 
356
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   399
368
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   400
Remember I wrote at the beginning of this section about being
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   401
god when designing a programming language. You can see this
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   402
here: we need to give meaning to symbols. At the moment
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   403
however, we are a poor fallible god. Look again at the grammar
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   404
of our programming language and our definition. Clearly, an
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   405
expression can contain variables. So far we have ignored them.
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   406
What should our interpreter do with variables? They might
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   407
change during the evaluation of a program. For example the
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   408
variable \pcode{n} in the factorial program counts down from 5
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   409
down to 0. How can we improve our definition above to give also
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   410
an answer whenever our interpreter encounters a variable in an
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   411
expression? The solution is to add an \emph{environment},
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   412
written $env$, as an additional input argument to our
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   413
\textit{eval\_exp} function.
356
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   414
 
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   415
\begin{center}
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   416
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   417
$\textit{eval\_exp}(\texttt{n}, env)$ & $\dn$ & $n$
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   418
  \qquad\text{if}\; \texttt{n}\; \text{is a number like} 
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   419
  \ldots \pcode{-2}, \pcode{-1}, \pcode{0}, \pcode{1},
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   420
\pcode{2}\ldots{}\\
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   421
$\textit{eval\_exp}(\texttt{e}_\texttt{1} \,\texttt{+}\, 
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   422
                    \texttt{e}_\texttt{2}, env)$ & 
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   423
  $\dn$ & $\textit{eval\_exp}(\texttt{e}_\texttt{1}, env) + 
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   424
           \textit{eval\_exp}(\texttt{e}_\texttt{2}, env)$\\
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   425
$\textit{eval\_exp}(\texttt{e}_\texttt{1} \,\texttt{*}\, 
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   426
                    \texttt{e}_\texttt{2}, env)$ & 
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   427
  $\dn$ & $\textit{eval\_exp}(\texttt{e}_\texttt{1}, env) * 
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   428
           \textit{eval\_exp}(\texttt{e}_\texttt{2}, env)$\\
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   429
$\textit{eval\_exp}(\texttt{e}_\texttt{1} \,\texttt{=}\, 
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   430
                    \texttt{e}_\texttt{2}, env)$ & 
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   431
  $\dn$ & $\begin{cases}
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   432
  1 & \text{if}\;\textit{eval\_exp}(\texttt{e}_\texttt{1}, env) =
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   433
                 \textit{eval\_exp}(\texttt{e}_\texttt{2}, env)\\
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   434
  0 & \text{otherwise}
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   435
  \end{cases}$\\
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   436
$\textit{eval\_exp}(\texttt{x}, env)$ & $\dn$ & $env(x)$      
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   437
\end{tabular}
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   438
\end{center}
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   439
 
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   440
\noindent This environment $env$ also acts like a map: it
368
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   441
associates variables with their current values. For example
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   442
after evaluating the first two lines in our factorial
359
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   443
program, such an environment might look as follows
357
5b91f5ad2772 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 356
diff changeset
   444
5b91f5ad2772 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 356
diff changeset
   445
\begin{center}
5b91f5ad2772 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 356
diff changeset
   446
\begin{tabular}{ll}
5b91f5ad2772 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 356
diff changeset
   447
$\fbox{\texttt{a}} \mapsto \texttt{1}$ &
5b91f5ad2772 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 356
diff changeset
   448
$\fbox{\texttt{n}} \mapsto \texttt{5}$
5b91f5ad2772 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 356
diff changeset
   449
\end{tabular}
5b91f5ad2772 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 356
diff changeset
   450
\end{center}
5b91f5ad2772 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 356
diff changeset
   451
359
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   452
\noindent Again I highlighted the keys. In the clause for
357
5b91f5ad2772 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 356
diff changeset
   453
variables, we can therefore consult this environment and
5b91f5ad2772 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 356
diff changeset
   454
return whatever value is currently stored for this variable.
368
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   455
This is written $env(x)$---meaning we query this map with $x$
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   456
we obtain the corresponding number. You might ask what happens
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   457
if an environment does not contain any value for, say, the
359
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   458
variable $x$? Well, then our interpreter just ``crashes'', or
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   459
more precisely will raise an exception. In this case we have a
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   460
``bad'' program that tried to use a variable before it was
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   461
initialised. The programmer should not have done this. In a
368
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   462
real programming language, we would of course try a bit harder
359
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   463
and for example give an error at compile time, or design our
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   464
language in such a way that this can never happen. With the
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   465
second version of \textit{eval\_exp} we completed our
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   466
definition for evaluating expressions.
356
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   467
 
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   468
Next comes the evaluation function for statements. We define
359
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   469
this function in such a way that we recursively evaluate a
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   470
whole sequence of statements. Assume a program $p$ (you want
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   471
to evaluate) and its pre-processed snippets $sn$. Then we can
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   472
define:
356
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   473
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   474
\begin{center}
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   475
\begin{tabular}{@{}l@{\hspace{1mm}}c@{\hspace{1mm}}l@{}}
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   476
$\textit{eval\_stmts}([], env)$ & $\dn$ & $env$\\
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   477
$\textit{eval\_stmts}(\texttt{label:}\;rest, env)$ &
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   478
  $\dn$ & $\textit{eval\_stmts}(rest, env)$ \\ 
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   479
$\textit{eval\_stmts}(\texttt{x\,:=\,e}\;rest, env)$ & 
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   480
  $\dn$ & $\textit{eval\_stmts}(rest, 
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   481
           env[x := \textit{eval\_exp}(\texttt{e}, env)])$\\  
359
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   482
$\textit{eval\_stmts}(\texttt{goto\,lbl}\;rest, env)$ 
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   483
 & $\dn$ & $\textit{eval\_stmts}(sn(\texttt{lbl}), env)$\\
356
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   484
$\textit{eval\_stmts}(\texttt{jmp?\,e\,lbl}\;rest, env)$ 
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   485
 & $\dn$ & $\begin{cases}\begin{array}{@{}l@{\hspace{-12mm}}r@{}}
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   486
 \textit{eval\_stmts}(sn(\texttt{lbl}), env)\\ 
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   487
 & \text{if}\;\textit{eval\_exp}(\texttt{e}, env) = 1\\
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   488
 \textit{eval\_stmts}(rest, env) & \text{otherwise}\\
359
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   489
 \end{array}\end{cases}$  
355
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   490
\end{tabular}
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   491
\end{center}
619073c37649 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   492
359
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   493
\noindent The first clause is for the empty program, or when
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   494
we arrived at the end of the program. In this case we just
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   495
return the environment. The second clause is for when the next
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   496
statement is a label. That means the program is of the form
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   497
$\texttt{label:}\;rest$ where the label is some string and
368
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   498
$rest$ stands for all following statements. This case is easy,
359
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   499
because our evaluation function just discards the label and
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   500
evaluates the rest of the statements (we already extracted all
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   501
important information about labels when we pre-processed our
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   502
programs and generated the snippets). The third clause is for
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   503
variable assignments. Again we just evaluate the rest for the
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   504
statements, but with a modified environment---since the
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   505
variable assignment is supposed to introduce a new variable or
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   506
change the current value of a variable. For this modification
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   507
of the environment we first evaluate the expression
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   508
$\texttt{e}$ using our evaluation function for expressions.
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   509
This gives us a number. Then we assign this number to the
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   510
variable $x$ in the environment. This modified environment
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   511
will be used to evaluate the rest of the program. The fourth
368
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   512
clause is for the unconditional jump to a label, called
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   513
\texttt{lbl}. That means we have to look up in our snippets
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   514
map $sn$ what are the next statements for this label.
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   515
Therefore we will continue with evaluating, not with the rest
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   516
of the program, but with the statements stored in the
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   517
snippets-map under the label $\texttt{lbl}$. The fifth clause
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   518
for conditional jumps is similar, but to decide whether to
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   519
make the jump we first need to evaluate the expression
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   520
$\texttt{e}$ in order to find out whether it is $1$. If yes,
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   521
we jump, otherwise we just continue with evaluating the rest
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   522
of the program.
356
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   523
359
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   524
Our interpreter works in two stages: First we pre-process our
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   525
program generating the snippets map $sn$, say. Second we call
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   526
the evaluation function with the default entry point and the
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   527
empty environment:
356
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   528
359
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   529
\begin{center}
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   530
$\textit{eval\_stmts}(sn(\pcode{""}), \varnothing)$
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   531
\end{center}
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   532
 
360
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   533
\noindent It is interesting to note that our interpreter when
359
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   534
it comes to the end of the program returns an environment. Our
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   535
programming language does not contain any constructs for input
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   536
and output. Therefore this environment is the only effect we
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   537
can observe when running the program (apart from that our
c90f803dc7ea updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 357
diff changeset
   538
interpreter might need some time before finishing the
360
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   539
evaluation of the program and the CPU getting hot). Evaluating 
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   540
the factorial program with our interpreter we receive as
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   541
``answer''-environment
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   542
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   543
\begin{center}
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   544
\begin{tabular}{ll}
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   545
$\fbox{\texttt{a}} \mapsto \texttt{120}$ &
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   546
$\fbox{\texttt{n}} \mapsto \texttt{0}$
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   547
\end{tabular}
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   548
\end{center}
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   549
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   550
\noindent While the discussion above should have illustrated
368
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   551
the ideas, in order to do some serious calculations, we clearly
360
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   552
need to implement the interpreter.
356
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   553
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   554
368
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   555
\subsubsection*{Scala Code for the Interpreter}
356
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   556
360
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   557
Functional programming languages are very convenient for
368
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   558
implementations of interpreters. A good choice for a
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   559
functional programming language is Scala, a programming
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   560
language that combines functional and object-oriented
369
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 368
diff changeset
   561
pro\-gramming-styles. It has received in the last five years or
368
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   562
so quite a bit of attention. One reason for this attention is
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   563
that, like the Java programming language, Scala compiles to
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   564
the Java Virtual Machine (JVM) and therefore Scala programs
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   565
can run under MacOSX, Linux and Windows.\footnote{There are
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   566
also experimental backends for Android and JavaScript.} Unlike
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   567
Java, however, Scala often allows programmers to write very
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   568
concise and elegant code. Some therefore say Scala is the much
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   569
better Java. A number of companies, The Guardian, Twitter,
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   570
Coursera, FourSquare, LinkedIn to name a few, either use Scala
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   571
exclusively in production code, or at least to some
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   572
substantial degree. If you want to try out Scala yourself, the
b46f86d95967 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   573
Scala compiler can be downloaded from
360
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   574
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   575
\begin{quote}
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   576
\url{http://www.scala-lang.org}
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   577
\end{quote}
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   578
370
ddac52c0014c updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 369
diff changeset
   579
Let us have a look at the Scala code shown in
ddac52c0014c updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 369
diff changeset
   580
Figure~\ref{code}. It shows the entire code for the
ddac52c0014c updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 369
diff changeset
   581
interpreter, though the implementation is admittedly no
ddac52c0014c updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 369
diff changeset
   582
frills.
360
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   583
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   584
\begin{figure}[t]
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   585
\small
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   586
\lstinputlisting[language=Scala]{../progs/inter.scala}
369
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 368
diff changeset
   587
\caption{The entire code of the interpreter for our
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 368
diff changeset
   588
idealised programming language.\label{code}}
360
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   589
\end{figure}
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   590
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   591
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   592
\subsubsection*{Static Analysis}
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   593
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   594
Finally we can come back to our original problem, namely 
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   595
finding out what the signs of variables are 
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   596
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   597
\begin{center}
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   598
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   599
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   600
\end{center}
356
e1e0f78baa70 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   601
 
276
d7109c6e721d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   602
\end{document}
d7109c6e721d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   603
370
ddac52c0014c updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 369
diff changeset
   604
%% list of static analysers for C
ddac52c0014c updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 369
diff changeset
   605
http://spinroot.com/static/index.html
ddac52c0014c updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 369
diff changeset
   606
ddac52c0014c updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 369
diff changeset
   607
%% NASA coding rules for C
ddac52c0014c updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 369
diff changeset
   608
http://pixelscommander.com/wp-content/uploads/2014/12/P10.pdf
ddac52c0014c updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 369
diff changeset
   609
276
d7109c6e721d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   610
%%% Local Variables: 
d7109c6e721d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   611
%%% mode: latex
d7109c6e721d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   612
%%% TeX-master: t
d7109c6e721d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   613
%%% End: