%\documentclass[a4paper,titlepage]{slides}
\documentclass[a4paper,landscape,titlepage]{slides}

\usepackage[latin1]{inputenc}
%\usepackage[T1]{fontenc}
\usepackage[german]{babel}
%\usepackage{portland}
\usepackage{lscape}
%\usepackage[dvips]{color}
\usepackage{array}
\usepackage{dcolumn}
\usepackage{epsfig}
\input{epsf}
\usepackage{fancyvrb}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage[normalem]{ulem}
\usepackage{url}

\newcommand{\slidetitle}[1]{\vspace{4ex}{\large\centerline{#1}}\vspace{1ex}}
%\newcommand{\epsline}[1]{\vspace{1ex}\centerline{\epsffile{#1.eps}}}
\newcommand{\epsline}[2]{\vspace{1ex}\centerline{\epsfig{file=#1.eps}}}
%\wepsbox{file,width}
\newcommand{\wepsbox}[2]{\includegraphics[width=#2]{#1.eps}}
%\wepsline{file,width}
\newcommand{\wepsline}[2]{\vspace{1ex}\centerline{\epsfig{file=#1.eps,width=#2}}}
\newcommand{\white}[1]{\textcolor{white}{#1}}
\newcommand{\code}[1]{\texttt{#1}}
\newcommand{\rep}[1]{{\Large$\downarrow$}\makebox[0pt][l]{#1}}
\newcommand{\comment}[1]{}
%\pass{arrow}{components}
\newcommand{\pass}[2]{\(\left.\mbox{\fbox{\begin{tabular}{l}#2\end{tabular}}}\right#1\)}
\newenvironment{portrait}{}{}
\newenvironment{myitemize}{\begin{list}{$\bullet$}{\setlength{\itemsep}{0ex}\setlength{\topsep}{0ex}}}{\end{list}}

\newcolumntype{.}{D{.}{.}{2}}

\setlength\paperwidth{32cm} %16:9 aspect ratio
\setlength\paperheight{18cm}
\setlength\textheight{\paperheight}
\addtolength\textheight{-1cm}
  \setlength\topmargin{\paperheight}
  \addtolength\topmargin{-2in}
  \addtolength\topmargin{-\headheight}
  \addtolength\topmargin{-\headsep}
  \addtolength\topmargin{-\textheight}
  \addtolength\topmargin{-\footskip}     % this might be wrong!
  \addtolength\topmargin{-.5\topmargin}
  \addtolength\topmargin{-1.6ex}
%\addtolength\evensidemargin{-2cm}
\addtolength\oddsidemargin{-1cm}
\addtolength\textwidth{4cm}

%\setlength\textheight{\paperheight}
%\addtolength\textheight{-1cm}
%  \setlength\topmargin{\paperheight}
%  \addtolength\topmargin{-2in}
%  \addtolength\topmargin{-\headheight}
%  \addtolength\topmargin{-\headsep}
%  \addtolength\topmargin{-\textheight}
%  \addtolength\topmargin{-\footskip}     % this might be wrong!
%  \addtolength\topmargin{-.5\topmargin}
%%\addtolength\evensidemargin{-2cm}
%%\addtolength\oddsidemargin{-2cm}
%%\addtolength\textwidth{4cm}

\title{Special Properties of\\Forth and Postscript}
\author{
  M. Anton Ertl\\TU Wien
}
\date{}
\begin{document}
%\begin{landscape}
\maketitle
%\end{landscape}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
  \slidetitle{Debugging}

  \begin{itemize}
  \item Stepping Debugger\\
    \code{dbg \emph{word}}\\
    Only works with \code{gforth-itc}
  \item Tracer (\code{printf} debugging)\\
    \verb|~~|
  \item Backtrace
  \end{itemize}
\end{slide}
    

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
  \slidetitle{IDE features}

  \begin{myitemize}
  \item \code{locate \emph{word}}\\
    \code{n b g l}\\
    \code{edit \emph{word}}
  \item \code{help \emph{word}}\\
    \code{n b g l}
  \item \code{where \emph{word}}\\
    \code{( u ) ww}\\
    \code{nw bw}
  \item after a Backtrace\\
    \code{( u ) tt}
    \code{nt bt}
  \item Decompiler\\
    \code{see \emph{word}}\\
    \code{simple-see \emph{word}}\\
    \code{see-code \emph{word}}
  \end{myitemize}
\end{slide}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Interpretation, Compilation, and Execution}

\begin{verbatim}
\ Compilation
: hello ( -- )
  ." hello, world" ;

\ Interpretation
.( hello, world)

\ Interpretation and execution
hello
\end{verbatim}

Interpretation

\begin{itemize}
\item exists in Forth, Postscript, Lisp, Prolog, Python, ...

\item does not exist in Fortran, C, C++, Java, Rust, ...
\end{itemize}
\end{slide}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{No hard boundary between compile time and run time}

... in the languge; there may be one in the head of the programmer.\\
No executable file; many systems have an image file.

\begin{itemize}

\item Initialize data structures

\item Macros: Execution during compilation

\item Run-time code generation: optimization or simplification

\end{itemize}

C++ has a separate programming language for macros (template language)
\end{slide}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Initialization}

\begin{verbatim}
/* C */ int a[] = {235,1857};

/* C, alternative */
int a[2];  a[0]=foo(2); a[1]=bar(3);

\ Forth
create a 2 foo , 3 bar ,
\end{verbatim}

\end{slide}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Macros}
\begin{verbatim}
: endif POSTPONE then ; immediate
: foo ... if ... endif ... ;

: (map) ( a n -- a a')   cells over + swap ;
: map<   postpone (map)  postpone ?do postpone i postpone @ ; immediate
: >map   1 cells postpone literal  postpone +loop ; immediate
: step  0  array 1000 map< + >map drop ;
\end{verbatim}
\end{slide}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Code generation: \texttt{LITERAL COMPILE,}}
\begin{verbatim}
: foo [ 5 cells ] literal ;
: ]cells ] cells POSTPONE literal ;
: foo [ 5 ]cells ;

: twice ( xt -- )
  dup compile, compile, ;
: 2+ [ ' 1+ twice ] ;
: 4* [ ' 2* twice ] ;
\end{verbatim}
\end{slide}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Code generation: Gray}
\begin{verbatim}
: compile-test \ set -- )
 postpone literal
 test-vector @ compile, ;

: generate-alternative1 \ -- )
 operand1 get-first compile-test
 postpone if
 operand1 generate
 postpone else
 operand2 generate
 postpone endif ;
\end{verbatim}
\end{slide}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Quines}

Programs that print themselves; the shorter the better.

\begin{verbatim}
                 source type
\end{verbatim}

By avoiding the use of certain Forth features, we get a variety of quines:\\
\texttt{http://www.complang.tuwien.ac.at/forth/quines.html}
\end{slide}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Name Binding}

\begin{itemize}

\item What happens if a name is redefined?

\item Algol (C, Java, ...): Compilation error

\item Lisp, Postscript: Dynamic Name Binding\\
  The new definition replaces the old one completely

\item Forth: Static name binding\\
  The old definition still exists\\
  Old uses use the old definition\\
  New uses use the new definition
\end{itemize}

\end{slide}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Name binding: Collision}

\begin{myitemize}

\item Frequent case: programmer defines name $x$, library is extended by $x$:
\begin{verbatim}
( in main ) require lib1.fs
  ( in lib1.fs ) require lib2.fs
    ( in lib2.fs ) : x ." x1" ;
  ( in lib1.fs ) : y x ;
( in main ) : x ." x2" ;
( in main ) x \ outputs "x2"
( in main ) y \ outputs "x1"
\end{verbatim}

  
\item Algol: Compile-time error

\item Lisp: Library 1 executes the wrong function $x$; run-time error
  or worse

\item Forth: Warning at compile time, program works

\end{myitemize}

\end{slide}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Namensbindung: problems and solutions}

\begin{itemize}

\item Collisions: conventions (C), namespaces (C++)
\begin{verbatim}
#define _POSIX_C_SOURCE 199506L
#include <unistd.h>
\end{verbatim}

\item Collisions: dictionaries (Postscript)

\item Forward declarations: \code{defer} (Forth)
\begin{verbatim}
defer foo
: bar ... foo ... ;
:noname ... bar ... ; is foo
\end{verbatim}
\end{itemize}
\end{slide}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Stateless control structures}
\begin{verbatim}
... IF ... THEN
... IF ... ELSE ... THEN
BEGIN ... WHILE ... REPEAT
BEGIN ... UNTIL
CASE ... OF ... ENDOF ... OF ... ENDOF ... ENDCASE
?DO ... LOOP
\end{verbatim}
\end{slide}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Control structures: foundation}

\centerline{
\begin{tabular}{r|r|r}
		  & forwards     & backwards \\\hline
Unconditional branch& \code{AHEAD ( -- orig )} & \code{AGAIN ( dest -- )}\\
Conditional branch  & \code{IF ( -- orig )}    & \code{UNTIL ( dest -- )}\\
branch target       & \code{THEN ( orig -- )}  & \code{BEGIN ( -- dest )}\\
\end{tabular}}
\begin{verbatim}
            : foo 
              BEGIN ( C: dest )
                ... IF ( C: dest orig )
                  ... THEN ( C: dest )
                ... UNTIL ( C: ) ;
\end{verbatim}
\end{slide}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Control Structures: ground floor}
\begin{verbatim}
: ELSE ( compilation: orig1 -- orig2 ; run-time: -- ) \ core
    POSTPONE ahead
    1 cs-roll
    POSTPONE then ; immediate restrict

: WHILE ( compilation: dest -- orig dest ; run-time: f -- ) \ core
    POSTPONE if
    1 cs-roll ; immediate restrict

: REPEAT ( compilation: orig dest -- ; run-time: -- ) \ core
    POSTPONE again
    POSTPONE then ; immediate restrict
\end{verbatim}
\end{slide}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Control structures: Usage}
\begin{verbatim}
: foo
  ... if ( C: orig1 )
    ...
  else ( C: orig2 )
    ... begin ( C: orig2 dest )
      ... while ( C: orig2 orig3 dest )
      ... repeat ( C: orig2 )
  then ;
\end{verbatim}
\end{slide}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Control structures: unconventional}
\begin{verbatim}
: foo
  begin ( C: dest )
    ... while ( C: orig1 dest )
    ... while ( C: orig1 orig2 dest )
    ... repeat ( C: orig1 )
  ... else ( C: orig3 )
    ... then ;
\end{verbatim}

\begin{itemize}

\item Arbitrary stateless control structures with \code{cs-roll}

\item Readability?

\end{itemize}
\end{slide}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Gforth: generalized guarded commands}
\begin{minipage}{12cm}
\begin{verbatim}
case
    ... ?of ... endof
    ... ?of ... contof
    ...
next-case \ or "0 endcase"
\end{verbatim}
\end{minipage}
\begin{minipage}{15cm}
\begin{verbatim}
: gcd ( n1 n2 -- n )
    case
        2dup > ?of tuck - contof
        2dup < ?of over - contof
    ( n n' ) endcase ;

: collatz ( u -- )
    case
        dup .
        1 of endof
        dup 1 and ?of 3 * 1+ contof
        2/
    next-case ;
\end{verbatim}
\end{minipage}
\end{slide}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Types}

\centerline{Who knows the type of data?}
\vspace{1ex}
\centerline{
\begin{tabular}{rr|c|c}
  &&\multicolumn{2}{c}{run-time system}\\
  &&no&yes\\\hline
  \raisebox{-1ex}{compiler}&no&Forth&Postscript, Python, Lisp\\
  &yes&C, Pascal&C++, Java\\
\end{tabular}}

\begin{itemize}
\item Type knowledge of the programmer (e.g., sorted array)

\item uniformity (Lisp) vs.\ specialization (Java)
\end{itemize}
\end{slide}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Types: Checking}

\begin{verbatim}
                             'a' 5 *
\end{verbatim}

\begin{myitemize}

\item How do you find type errors in Forth?  Testing!

\item With a little experience type errors are easy to find.\\
  experience in interpreting program output\\
  experience in writing test cases\\
  experience in writing programs for easy testability

\item More intensive testing $\Rightarrow$ you also find other errors

\end{myitemize}
\end{slide}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Types: Checking}
\begin{quote}
As programmers learned C with Classes or C++, they lost the ability to
quickly find the ``silly errors'' that creep into C programs through
the lack of checking. Further, they failed to take the precautions
against such silly errors that good C programmers take as a matter of
course. After all, ``such errors don't happen in C with Classes.''
Thus, as the frequency of run-time errors caused by uncaught argument
type errors goes down, their seriousness and the time needed to find
them goes up.  \emph{Bjarne Stroustroup}
\end{quote}

\end{slide}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Types: Overloading}
\begin{verbatim}
int n;      n*3           n @ 3 *
float r;    r*3.0         r f@ 3.0e f*
int n;      n<3           n @ 3 <
unsigned u; u<3           u @ 3 u<
\end{verbatim}
\end{slide}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Types: Advantages and disadvantages of Forth}

\begin{itemize}
\item[$+$] More uniformity: Better reusability

\item[$+$] Extensions as libraries that would need type system
  extension in other languages (OOP)

\item[$+$] Less complexity, e.g., for containers

\item[$-$] No type knowledge for garbage collection, marshalling etc.

\item[$-$] For changes affecting many lines static type checking would be useful\\
  Poor man's checking: Use new names for changed interfaces\\
  New names allow gradual changes
  
\end{itemize}
\end{slide}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Storage management}

\begin{itemize}
\item Static allocation\\
  \code{create allot}\\
  Small systems ($<64$KB)

\item Dynamic allocation with explicit deallocation\\
  \code{allocate free}\\
  Keep track of allocations: Memory leaks and double \code{free}\\
  Medium systems
  

\item Dynamic allocation with automatic deallocation\\
  \code{allocate} and garbage collection\\
  Large systems ($>16$MB)

\end{itemize}
\end{slide}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Storage management and APIs}

\begin{verbatim}
read-line ( c-addr u1 wfileid -- u2 flag wior )
\end{verbatim}

Reads a line from wfileid into the buffer at c-addr u1.  Gforth
supports all three common line terminators: LF, CR and CRLF. A
non-zero wior indicates an error.  A false flag indicates that
'read-line' has been invoked at the end of the file.  u2 indicates
the line length (without terminator): u2$<$u1 indicates that the line
is u2 chars long; u2=u1 indicates that the line is at least u1 chars
long, the u1 chars of the buffer have been filled with chars from the
line, and the next slice of the line with be read with the next
'read-line'.  If the line is u1 chars long, the first 'read-line'
returns u2=u1 and the next read-line returns u2=0.

\begin{verbatim}
slurp-fid ( fid -- c-addr u )
\end{verbatim}
  
c-addr u is the content of the file fid
\end{slide}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Object-oriented extension: features}
\begin{itemize}
\item Dynamic Dispatch (virtual functions/methods)
\item Instance Variables
\item Single inheritance
\item Static binding (C++: \code{A::m})
\item Basic class \code{object}
\item \code{new}
\end{itemize}
\end{slide}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Object-oriented extension: Usage (1)}
\begin{verbatim}
object class
  cell var text
  cell var len
  cell var x
  cell var y
  method init
  method draw
end-class button

:noname ( o -- ) >r
 r@ x @ r@ y @ at-xy  r@ text @ r> len @ type ;
 button defines draw
:noname ( addr u o -- ) >r
 0 r@ x ! 0 r@ y ! r@ len ! r> text ! ;
 button defines init
\end{verbatim}
\end{slide}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Object-oriented extension: Usage (2)}
\begin{verbatim}
button class
end-class bold-button

: bold   27 emit ." [1m" ;
: normal 27 emit ." [0m" ;

:noname bold [ button :: draw ] normal ; bold-button defines draw

button new Constant foo
s" thin foo" foo init
page
foo draw
bold-button new Constant bar
s" fat bar" bar init
1 bar y !
bar draw
\end{verbatim}
\end{slide}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Object-oriented extension: source code}

\begin{verbatim}
: method ( m v -- m' v ) Create  over , swap cell+ swap
  DOES> ( ... o -- ... ) @ over @ + @ execute ;
: var ( m v size -- m v' ) Create  over , +
  DOES> ( o -- addr ) @ + ;
: class ( class -- class methods vars ) dup 2@ ;
: end-class  ( class methods vars -- )
  Create  here >r , dup , 2 cells ?DO ['] noop , 1 cells +LOOP
  cell+ dup cell+ r> rot @ 2 cells /string move ;
: defines ( xt class -- ) ' >body @ + ! ;
: new ( class -- o )  here over @ allot swap over ! ;
: :: ( class "name" -- ) ' >body @ + @ compile, ;
Create object  1 cells , 2 cells ,
\end{verbatim}
Explanation: \code{https://bernd-paysan.de/mini-oof.html}
\end{slide}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Object-oriented extension: explanation}
  \begin{minipage}[b]{12cm}
\begin{verbatim}
object class
  cell var text
  cell var len
  cell var x
  cell var y
  method init
  method draw
end-class button
button new Constant foo
  
button class
end-class bold-button
bold-button new Constant bar
\end{verbatim}
  \end{minipage}%
  \wepsbox{mini-oof}{14cm}\hfil
\end{slide}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
  \slidetitle{Forth-philosophy}

  \begin{itemize}
  \item actually Chuck Moore's philosophy
  \item \emph{Keep it simple!}
  \item \emph{Do not speculate!}\\
    do not generalize\\
    do not design for reuse\\
    only take steps when necessary
  \item \emph{Do it yourself!}\\
    no libraries
  \item \emph{Do not bury your tools!}
  \end{itemize}
\end{slide}
    

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
  \slidetitle{Implementation}
  \wepsbox{new-direct}{13cm}
  \begin{myitemize}
  \item \code{see \emph{word}}
  \item \code{simple-see \emph{word}}
  \item \code{see-code \emph{word}}
  \end{myitemize}
\end{slide}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Postscript}

\begin{itemize}
\item Syntax
  \begin{myitemize}
      \item \verb|( ) < > [ ] { } / %| are delimiters
      \item \verb|<< >>| are lexemes
      \item \verb|( )| surround strings; balance parentheses, or use \verb|\) \(|
  \end{myitemize}
\item Types
  \begin{myitemize}
  \item Types known at run-time
  \item Operators work on several types
  \item Type checking at run-time
  \item Limited non-static stack depth when building an array
  \end{myitemize}
\end{itemize}
\end{slide}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Postscript: types}
\centerline{
\begin{tabular}{c|>{\tt}c}
Typ		&{\rm Beispiel-Literal}             \\\hline
integer		&1				    \\
real		&1.0				    \\
boolean		&true				    \\
name		&/name				    \\
mark		&[				    \\
null		&null				    \\
operator	&/add load			    \\
fontID		&				    \\
save		&				    \\\hline
array		&[1 2]				    \\
string		&(string)			    \\
dictionary	&<< index1 wert1 index2 wert2 ... >>\\
file		&                                   \\
\end{tabular}}
\begin{myitemize}
  \item simple types: value semantics
  \item composite types: reference semantics (shallow copying)
\end{myitemize}
\end{slide}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Attributes: literal und executable objects}
\hspace{1ex}
\centerline{
\begin{tabular}{c|>{\tt}c|>{\tt}c}
Typ&{\rm executable example}&{\rm executable literal}\\\hline
Name		&name			&/name\\
Operator	&//add			&/add load\\
Array		&\{dup mul\}	&[ 1 2 ]\\
\end{tabular}
}
\begin{myitemize}
\item Attribute of object in addition to type

\item Executing a literal object pushes it on the stack

\item Executing an executable object has a type-specific effect

\item Difference between direct and indirect execution

\item Attributes are ignored when the object is treated as data (most operations)

\item Further attribute: access

\end{myitemize}

\end{slide}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Procedures and arrays}

\begin{myitemize}
\item Procedures are executable arrays

\item Syntactically different\\
  \verb|{ 1 2 add }|\\
  \verb|[ 1 2 add ]|

\item Binding a procedure to a name\\ and executing the name executes the procedure indirectly\\
  Also with \code{exec}, \code{if} etc.

\item Indirect execution of a procedure:\\
  Elements of the procedure are executed \emph{directly}

\item Direct execution of a procedure:\\
  push it on the stack
\end{myitemize}
\begin{verbatim}
/x { { 1 2 add } } def
x exec
\end{verbatim}
\end{slide}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Control Structures}

Operators that take (usually) procedures as parameters
\begin{verbatim}
a 0 lt { 1 == } if
a 0 lt { 1 == } { 2 == } ifelse
5 1 10 { == } for
5 2 10 { == } for
[ 1 7 3 ] { == } forall
<< /c 1 /a 2 /b 3 >> { == == } forall
4 { (abc) = } repeat
5 { dup 0 lt { exit } if dup = 1 sub } loop pop
\end{verbatim}
\end{slide}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Names and dictionaries}

\begin{itemize}
\item \verb|/squared {dup mul} def|

\item By executing \code{squared}, the procedure is executed indirectly\\
      $\Rightarrow$ its elements are executed directly

\item Definition is stored in current dictionary
\item dictionary corresponds to Forth's wordlist
\item Dictionary stack corresponds to Forth's search order
\end{itemize}
\end{slide}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Name binding}
\begin{myitemize}
\item name binding at run-time\\
      \verb|/foo {bar} def  /bar {1} def|

\item Usage for local variables\\
      \verb|/foo { << /a rot >> begin ... a ... end } def|

\item Dynamic scoping\\
  \verb|<< /a 5 >> begin { a } end exec | gives an error\\
  Most languages support static scoping only

\item Similar to environment-variables in Unix
  \begin{verbatim}
    /foo { ... conf ... } def
    << /conf { 5 } >> begin foo end
    << /conf { x } >> begin foo end
\end{verbatim}
\end{myitemize}
\end{slide}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Comparison}
\begin{verbatim}
/v [ 0 1 999 {} for ] def
/step {0 v { add } forall} def
100000 {step pop} repeat


: (map) ( a n - a a')   cells over + swap ;
: map[   postpone (map)  postpone ?do postpone I postpone @ ; immediate
: ]map   1 cells postpone literal  postpone +loop ; immediate

create array 1000 cells allot   
: init  1000 0 DO I  array I cells + !  LOOP ;
init
: step  0  array 1000 map[ + ]map drop ;
: bench  100000 0 DO step LOOP ;
bench
\end{verbatim}
\end{slide}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Document Structuring Conventions}

\begin{verbatim}
%!PS-Adobe-2.0
%%Creator: dvips(k) 5.95a Copyright 2005 Radical Eye Software
%%Title: slides.dvi
%%Pages: 4 0
%%PageOrder: Ascend
%%Orientation: Landscape
%%BoundingBox: 0 0 595 842
%%DocumentFonts: LCMSS8 CMTT8 CMSY8 CMMI8 LCMSSI8 LCMSSB8 CMSY10
%%DocumentPaperSizes: a4
%%EndComments
... ProcSets ...
... Fonts ...
... Setup ...
%%Page: (0,1,2,3,4,5,6,7) 1
... Postscript Code ...
%%Page: (8,9,10,11,12,13,14,15) 2
... unabhängig von anderen Seiten
%%Trailer
...
%%EOF
\end{verbatim}
  Example:\\
  Source: \url{https://www.complang.tuwien.ac.at/anton/lvas/ertl%26pirker97.txt}\\
  Result: \url{https://www.complang.tuwien.ac.at/papers/ertl%26pirker97.ps.gz}
\end{slide}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\slidetitle{Encapsulated Postscript}

\begin{myitemize}
\item Usually for graphics
\item Is included in other documents
\item Examples: \url{https://www.complang.tuwien.ac.at/anton/eps-gallery}
\end{myitemize}

\begin{verbatim}
%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 90 651 387 709
... Prolog ...
%%Page: 1 1
... Nur eine Seite, meist ohne showpage ...
\end{verbatim}
\end{slide}
\end{document}
