CS450 ☽ Specifying λS ☽ Lecture 13 ☽ Tiago Cogumbreiro
CS450 ☽ Specifying λS ☽ Lecture 13 ☽ Tiago Cogumbreiro
CS450 ☽ Specifying λS ☽ Lecture 13 ☽ Tiago Cogumbreiro
CS450 ☽ Specifying λS ☽ Lecture 13 ☽ Tiago Cogumbreiro
For fundamental contributions to the theory and practice of distributed and concurrent systems, notably the invention of concepts such as causality and logical clocks, safety and liveness, replicated state machines, and sequential consistency.
Source: lamport.org
CS450 ☽ Specifying λS ☽ Lecture 13 ☽ Tiago Cogumbreiro
CS450 ☽ Specifying λS ☽ Lecture 13 ☽ Tiago Cogumbreiro
CS450 ☽ Specifying λS ☽ Lecture 13 ☽ Tiago Cogumbreiro
In this class, we will learn how to implement an interpreter. In CS451/651 you can learn how to implement a compiler.
The interpreter:
The compiler:
CS450 ☽ Specifying λS ☽ Lecture 13 ☽ Tiago Cogumbreiro
Python, for instance, is known as an interpreter, but it actually compiles Python into an assembly-like language that is then interpreted (executed). Racket works the same way as Python. Java is also executed in the same way, but compilation is performed by the user.
Additionally, for performance reasons, some interpreters perform just-in-time compilation, which dynamically translates (compiles) small parts of the language as machine code that is executed directly by the CPU.
CS450 ☽ Specifying λS ☽ Lecture 13 ☽ Tiago Cogumbreiro
((lambda (x) x) 3)'((lambda (x) x) 3)(r:apply (r:lambda (list (r:variable 'x))(list (r:variable 'x))) (list (r:number 3)))(r:number 3)CS450 ☽ Specifying λS ☽ Lecture 13 ☽ Tiago Cogumbreiro
CS450 ☽ Specifying λS ☽ Lecture 13 ☽ Tiago Cogumbreiro
The concrete syntax dictates the syntactic structure of a program (how do we represent a number, a variable, etc). This is not the focus of our course (refer to CS451/651).
We can easily sidestep the parsing issue by reminding ourselves that quote
can serialize Racket expressions into a data structure.
CS450 ☽ Specifying λS ☽ Lecture 13 ☽ Tiago Cogumbreiro
(define-type s:value (U s:number s:lambda))
(define-type s:expression (U s:value s:variable s:apply))
(struct s:number ([value : Number]) #:transparent)
(struct s:variable ([name : Symbol]) #:transparent)
(struct s:lambda ([param : s:variable] [body : s:expression]) #:transparent)
(struct s:apply ([func : s:expression] [arg : s:expression]) #:transparent)
CS450 ☽ Specifying λS ☽ Lecture 13 ☽ Tiago Cogumbreiro
CS450 ☽ Specifying λS ☽ Lecture 13 ☽ Tiago Cogumbreiro
CS450 ☽ Specifying λS ☽ Lecture 13 ☽ Tiago Cogumbreiro
Since λS is Turing complete, can you write a non-terminating program in λS?
CS450 ☽ Specifying λS ☽ Lecture 13 ☽ Tiago Cogumbreiro
We show that evaluating $({(\lambda x.x)}\ {10})$ returns $10$:
CS450 ☽ Specifying λS ☽ Lecture 13 ☽ Tiago Cogumbreiro
We will use a more familiar notation. We define the evaluation function $\mathtt{eval}(e) = v$ that takes an expression $e$ and returns a value $v$.
CS450 ☽ Specifying λS ☽ Lecture 13 ☽ Tiago Cogumbreiro
CS450 ☽ Specifying λS ☽ Lecture 13 ☽ Tiago Cogumbreiro
Our objective is to evaluate an expression.
If the expression $e$ is a value, then we are in the base case, and we return value $e$.
If the expression is a variable $x$, this is an error, report it as such.
Otherwise, we have a function application $(e_f\ e_a)$. Recursively evaluate function $e_f$ down to a value. Ensure that the result is a function declaration, say $(\texttt{lambda}(x)\ e_d)$.
Recursively evaluate the argument of the function $e_a$ down to a value $v_a$.
Substitute variable $x$ by value $v_a$ in $e_d$, say $e_d[x \mapsto v_a]$. Recursively evaluate $e_d[x \mapsto v_a]$.
CS450 ☽ Specifying λS ☽ Lecture 13 ☽ Tiago Cogumbreiro
CS450 ☽ Specifying λS ☽ Lecture 13 ☽ Tiago Cogumbreiro
CS450 ☽ Specifying λS ☽ Lecture 13 ☽ Tiago Cogumbreiro
Objective: substitute variable $x$ by value $v$ in expression $e$, notation $e[x \mapsto v]$.
CS450 ☽ Specifying λS ☽ Lecture 13 ☽ Tiago Cogumbreiro
Function (check-r:eval? exp val) is given in the template to help you test your code
effectively.
The use of
check-r:eval?is optional. You are encouraged to play around withr:evaldirectly.
CS450 ☽ Specifying λS ☽ Lecture 13 ☽ Tiago Cogumbreiro
; `a number is a value, so we just return it`
(check-r:eval? 1 1)
; `a lambda is a value, so we just return it`
(check-r:eval? '(lambda (x) x) '(lambda (x) x))
; `function application`
(check-r:eval? '((lambda (x) x) 10) 10)
; `function application that returns a lambda and replaces a variable`
(check-r:eval? '((lambda (y) (lambda (x) y)) 1) '(lambda (x) 1))