Professional Documents
Culture Documents
Parameter Passing
Metacircular
Macros
Outline
Data and Program Structure
Metalinguistic Abstraction
Metalinguistic Abstraction
(SICP 4.1 - 4.2)
Lecture IV
Ahmed Rezine
Linkopings Universitet
TDDA69, VT 2014
Metalinguistic
Metalinguistic
Metalinguistic Abstraction
Metacircular
Macros
Parameter Passing
Metalinguistic
Metacircular
Macros
Parameter Passing
Metalinguistic
Metacircular
Evaluators
Parameter Passing
Evaluators (cont.)
Macros
expression
Method Assume computer D can evaluate expressions in language P1
Define a procedure in P1 that computes how one can evaluate
expressions in language P2
Computer D can now evaluate expressions in language P2
P1 is called the host language
P2 is called the embedded language
All languages can be regarded as evaluators of some particular
languages
Metalinguistic
Passing
Metacircular
Macros
Parameter
Universal Machines
Metacircular
Macros
Parameter Passing
Evaluators (cont.)
computability
Alan Turing, in his 1936 paper, introduced a simple model, now called
Turing machines, and argued that any effective process can be
formulated as a program of such a machine (Church-Turing thesis).
We will discuss decisions and choices that are common and well
Metacircular
A Metacircular Evaluator
Metalinguistic
Macros
Parameter Passing
Metalinguistic
Metacircular
Outline
Macros
Parameter Passing
Metacircular evaluators
An evaluator that is written in the same language that it evaluates is said to be metacircular evaluator.
John McCarthy (the father of Lisp) already described in 1960 the eval-function of Lisp in Lisp
Metalinguistic
Metacircular
Macros
Parameter Passing
Metalinguistic
Metacircular
Macros
Parameter Passing
Expression,
evaluation:
environment
A procedure to arguments
An expression in an environment
Procedure,
arguments
evaluation.
Metalinguistic
Metacircular
Macros
Parameter Passing
eval (cont.)
eval
Primitive expressions:
Special forms:
is structured as a case analysis for the types of expressions the language can
evaluate
Metalinguistic
apply
Metacircular
Macros
Parameter Passing
Metalinguistic
Metacircular
Macros
Parameter Passing
be applied
Depending on the type of the procedure, applies it on the arguments:
Primitive procedure: Apply the procedure directly (this is a frontier with
the host language).
Compound procedure: Sequentially evaluate the expressions in the
body of the procedure in an extended environment.
Metacircular
Macros
Syntax
Sicp defines eval and apply. In the labs, these are renamed to
eval-%scheme and apply-%scheme
Sicp defines an evaluator for Scheme in Scheme. It defines the eval and apply
procedures to unroll the evaluation-application cycle.
To clarify the difference between the host (Scheme) and the embedded
language (%Scheme), and to avoid name collisions, the labs prefix the
special forms of %Scheme with % and define eval-%scheme and apply%scheme to unroll the evaluation-application cycle.
Metalinguistic
Passing
Metacircular
Macros
Parameter
<numb>
<var>
(%quote <datum>)
(%begin <expr>...<expr>
eval-%scheme
(%lambda (<par>...<par>)
<expr>...<expr>)
(%cond
(<expr> <expr> ... <expr>)
(<expr> <expr> ... <expr>)
(... )
(%else <expr> ... <expr>))
Metalinguistic
Metacircular
Macros
Parameter Passing
Defining variables:
Metacircular
Macros
apply-%scheme
Metalinguistic
Passing
Metacircular
Macros
Parameter
Metalinguistic
Passing
Metacircular
Macros
Parameter
Metalinguistic
( de primitive-procedures
f i (lis
( lis
%car car)
Parameter Passing
Metacircular
t (lis
t
%cdr cdr)
t
(lis
%cons cons)
t
(lis
%null? null?)
t
(lis
%list list)
t
(lis
% eq? eq?)
t
(lis
%+ +)
t
(lis
%> >)
t
(lis
%newline newline)
t
(lis
%display display)))
t
Macros
Parameter Passing
Metalinguistic
(define
(lookup-variable-value
env)environment
Finding
variable valuesvar
in the
(define (env-loop
env)
Metacircular
Macros
(define (scan vars vals)
(cond ((null? vars)
Defining
a variable in an environment
(env-loop (enclosing-environment env)))
((eq? var (mcar vars))
(mcar vals))
(else (scan (mcdr vars) (mcdr vals)))))
(if (eq? env the-empty-environment)
(error lookup-variable-value
"Unbound variable: ~s" var)
(define (define-variable!
var val env)
(let ((frame (first-frame
env)))
(let ((frame
env)))frame)
(scan(first-frame
(frame-variables
(define (scan(frame-values
vars vals) frame)))))
(cond env))
((null? vars)
(env-loop
(add-binding-to-frame! var val frame))
((eq? var (mcar vars))
(set-mcar! vals val))
(else (scan (mcdr vars) (mcdr vals)))))
(scan (frame-variables frame)
(frame-values frame))))
Metalinguistic
(define
(set-variable-value!
var in
val
Modifying
variable value
anenv)
environment
(define (env-loop env)
(define (scan vars vals)
(cond ((null? vars)
(env-loop (enclosing-environment env)))
((eq? var (mcar vars))
(set-mcar! vals val))
(else (scan (mcdr vars) (mcdr vals)))))
(if (eq? env the-empty-environment)
(error set-variable-value!
"Unbound variable: ~s" var)
(let ((frame (first-frame env)))
(scan (frame-variables frame)
(frame-values frame)))))
(env-loop env))
Parameter Passing
Metalinguistic
Passing
Metacircular
Macros
Parameter
Add new primitive-procedures by extending the list of procedures that are mapped to procedures in the underlying Scheme, or written in %Scheme and evaluated with
eval-%scheme in Scheme.
For special forms, add a case in eval-%scheme with corresponding syntactic predicates and selection functions and evaluation procedures.
Allow the usage of macros in order to define new expressions using previously defined ones. These are then expended by the evaluator before they get also evaluated
Metalinguistic
Metacircular
Metalinguistic
Metacircular
Macros
Metalinguistic
Parameter Passing
Macros
Parameter Passing
Macros
Parameter Passing
Outline
Metalinguistic
Metacircular
Macros
Parameter Passing
Metalinguistic
Metacircular
Common Lisp allows macro procedures that give the possibility to write
procedures that transform a Lisp expression into another before it is evaluated.
(
c
(
i
2
0
1.
2.
n
d
m
a(
e
v
(
l 2
l0
(
)
)
r
expansi
o l 2 onl
20
) 0 giv
)
luatio
n
es
(else 20))
gives
In lab 3,4
Metacircular
Macros
Parameter Passing
Metalinguistic
Metacircular
Different ways to
call
Outline
Macros
Parameter Passing
procedures
Read thoroughly the code (sections 4.1.1-4.1.7). You will use the code in
almost all the remaining labs. Pay attention to how the environment is
managed.
load %Scheme, test and experiment
implement different mechanisms to augument the language
operation?
More to be looked at in the labs.
Call-by-name: Arguments are sent, not-evaluated, to the procedure and only evaluated when needed
expressions
Metalinguistic
Parameter Passing
Metacircular
Metalinguistic
Metacircular
Macros
Macros
Parameter Passing
Parameter Passing
Metalinguistic
Metalinguistic
Metacircular
In lab 5
Macros
In lab 5 (cont.)
(
(
f
g
(
h
x
x
)
y
)
z
)
1
)
(g x) )
(h y))
(%+ z 3))
(%define y
2)
1.
The new model will allow us to define new functions that used to require
2.
For next lecture: Check whether your favorite language allows for
You will then change %Scheme so that one can specify how
constructions that we can describe in this model. Code blocks in Ruby for
example ?
3.
4.
... )
Parameter Passing