mirror of
https://github.com/janishutz/eth-summaries.git
synced 2026-07-28 03:39:08 +02:00
[FMFP] Restructure summary
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
\newpage
|
||||
\subsection{Interpreters}
|
||||
Interpreters are prevalent in programming languages, database systems, text processors, HDLs, search engines, etc.
|
||||
|
||||
They are in concept very simple, as they perform three steps read, evaluate, print.
|
||||
|
||||
The implementation of one however is not trivial by any stretch of the imagination.
|
||||
@@ -0,0 +1,52 @@
|
||||
\subsubsection{Read step}
|
||||
During this step, text is turned from text into a more easily handlable format
|
||||
|
||||
|
||||
\paragraph{Lexical Analysis}
|
||||
During lexical analysis, the input is turned into tokens.
|
||||
For example: The source code is \texttt{position := initial + rate + 60}
|
||||
|
||||
The translation is:
|
||||
\begin{multicols}{2}
|
||||
\begin{enumerate}
|
||||
\item Identifier \texttt{position}
|
||||
\item Assignment symbol \texttt{:=}
|
||||
\item Identifier \texttt{initial}
|
||||
\item Addition symbol \texttt{+}
|
||||
\item Identifier \texttt{rate}
|
||||
\item Addition symbol \texttt{+}
|
||||
\item Number \texttt{60}
|
||||
\end{enumerate}
|
||||
\end{multicols}
|
||||
|
||||
It also removes whitespaces and comments
|
||||
|
||||
|
||||
\paragraph{Parsing}
|
||||
The tokens are then turned into an abstract syntax tree.
|
||||
|
||||
The syntax is specified by a grammar such as:
|
||||
\begin{align*}
|
||||
Expr & ::= Identifier \divider Number \divider Expr\; \texttt{`+`}\; Expr \\
|
||||
Assign & ::= Identifier\; \texttt{`:=`}\; Expr
|
||||
\end{align*}
|
||||
|
||||
This can also be represented as a haskell type:
|
||||
\begin{code}{haskell}
|
||||
data Expr = Identifier Ident | Number Num | Plus Expr Expr
|
||||
data Assign = Assignment Ident Expr
|
||||
type Ident = String
|
||||
type Num = Int
|
||||
\end{code}
|
||||
|
||||
Since some to be parsed statements are more complex to parse, we may do combinatory parsing.
|
||||
|
||||
This is much more powerful as it can handle ambiguous grammars typically found in real programming languages.
|
||||
|
||||
We can use for example these parser combinators:
|
||||
|
||||
\newpage
|
||||
\inputcode{haskell}{code/10_parser.hs}
|
||||
|
||||
These are just some of the functions defined.
|
||||
You can find a full mini-haskell and lambda-calculus parser on CodeExpert (at the time of writing this that was the case at least)
|
||||
@@ -0,0 +1,50 @@
|
||||
\newpage
|
||||
\subsection{Evaluation}
|
||||
Evaluation is then done using tree traversal as we have already seen in the Haskell section
|
||||
|
||||
|
||||
\subsubsection{Lazy Evaluation}
|
||||
Expressions are substituted before evaluation recursively until there are no more expressions to substitute, at which point the expression is evaluated.
|
||||
|
||||
This can obviously lead to duplicated evaluation, i.e. a computation reoccurring.
|
||||
|
||||
|
||||
\paragraph{In Haskell}
|
||||
In Haskell, this is solved using sharing where the terms are represented in a directed graph.
|
||||
|
||||
In pattern matching, the arguments are evaluated only as much as is needed to determine a pattern match.
|
||||
|
||||
For guards, the execution proceeds sequentially until success occurs.
|
||||
For instance in an \texttt{OR} statement, only the first statement is evaluated if it evaluates to true
|
||||
|
||||
Local definitions are also lazily evaluated (i.e. bound with \texttt{where} clauses)
|
||||
|
||||
|
||||
\paragraph{Applications}
|
||||
This concept can be used for data-driven programming.
|
||||
For instance, to determine the minimum value of a list, we could use insertion sort and take the head.
|
||||
Due to lazy evaluation, we have way fewer evaluations that need to happen.
|
||||
|
||||
Additionally for infinite lists or other infinite data structures, lazy evaluation allows creating a finite representation for the infinite data.
|
||||
It also allows operating on the infinite data given the operation only operates on a finite subset of the data structure.
|
||||
|
||||
An application of that is the prime number algorithm Sieve of Eratosthenes:
|
||||
\begin{enumerate}
|
||||
\item Generate list: \texttt{[2 ..]} (list of all natural numbers)
|
||||
\item Mark the first unmarked number: \texttt{head :: [a] -> a} from prelude determines first element
|
||||
\item Cross out all multiples: \verb|dropMults x ys = filter (\y -> y `mod` x /= 0) ys|
|
||||
\item Repeat with recursions: \texttt{sieve xs = head xs : sieve (dropMults (head xs) (tail xs))}
|
||||
\end{enumerate}
|
||||
|
||||
Another example is Newton's Algorithm to find roots.
|
||||
|
||||
|
||||
\paragraph{Correctness}
|
||||
Lazy evaluation makes reasoning about complexity and correctness harder, as types like \texttt{[Int]} include
|
||||
\begin{enumerate}
|
||||
\item finite, everywhere defined lists (e.g. \texttt{[1, 3, 5]})
|
||||
\item finite lists with undefined elements like \texttt{[1, 2, undef]}
|
||||
\item infinite lists with defined or undefined elements such as \texttt{[1..]}
|
||||
\end{enumerate}
|
||||
|
||||
However, induction is only sound for the first kind. More on this later on
|
||||
Reference in New Issue
Block a user