CSE 755, part2 Operational Semantics Operational Semantics (O.S.): Specify Semantics by specifying how each command is to be executed. One approach: Define an interpreter for the language. Meta-circular interpreter: Interpreter in the same language References: McCarthy and others: Lisp 1.5 programmer's manual 1
Operational Semantics. Operational Semantics (O.S.): Specify Semantics by specifying how each command is to be executed. One approach: Define an interpreter for the language. Meta-circular interpreter: Interpreter in the same language - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
CSE 755, part2
Operational Semantics
Operational Semantics (O.S.):Specify Semantics by specifying how each command is to be executed.
One approach: Define an interpreter for the language.
Meta-circular interpreter:Interpreter in the same language
References:McCarthy and others: Lisp 1.5 programmer's manual
1
CSE 755, part2
Lisp Basics
Data types: Atomic & non-atomic s-expressions
Atomic s-exp: Numbers [we will use only integers] Strings [we won't use them] Symbols [i.e., identifiers]:
function names [built-in and user-defined] parameter names constants: T [for true], NIL [for false; also other key uses]
Non-atomic s-exp:If s1, s2 are s-exps, so is (s1 . s2)[so non-atomic s-exps are binary trees]
Note: car[ s ] gives error if s is atomic cdr [ (s1 . s2) ]= s2
Note: cdr[ s ] gives error if s is atomic atom[ s ] = returns T/NIL if s is/isn't an atom null[ s ] = returns T/NIL if s is/isn't the atom NIL eq[ s1, s2]= ret. T/NIL if s1, s2 are/aren't the same atom
(null, eq may give errors if applied to non-atoms) plus[ s1, s2 ]: returns [s1 + s2] (must be numbers) minus[ s1, s2 ]: returns [s1 s2] (must be numbers) Etc.
Lisp Basics (contd.)
3
CSE 755, part2
Problem:Suppose we have four s-exps, s1, s2, s3, s4 that we want to keep together. Possible ways:
eq[ T, T] = T eq[ T, NIL] = NIL eq[ NIL, ()] = T eq[car[(2 . 3)], cdr[(3 . 2)]] = T eq[(2 . 3), (3 . 2)] : error; arguments to eq must be atoms eq[plus[2 , 3], minus[10, 5]]= T null[T] = NIL null[NIL]= T null[car[(2)]] = NIL null[cdr[(2)]] = T atom[car[(2)]] = T atom[cdr[(2)]] = T
6
CSE 755, part2
Lisp (the prog. lang.)
Lisp is not an imperative language Lisp is a functional programming language So:
No "internal state" that changes as program is "executed" In fact, no program variables whose values change over time! A program has no "side-effects"
Lisp runs in eval-print mode: Give the interpreter a Lisp Expression (not Lisp "program") It evaluates it, prints the value Waits for the next Lisp Expression
7
CSE 755, part2
Types of Lisp Expressions
1. Constants: 4, T, NIL, "xyz"
2. Function application:
(F a1 a2 a3 ... )
F is the function to be applied; a1, a2, ... the arguments
The interpreter first evaluates each argument, then applies F to the resulting values
Examples:
(plus 2 3) will output 5 // plus may be called "+"
(plus (plus 3 4) (plus 3 5)) will output 15
(car (3 . 4)) will output an error message!8
CSE 755, part2
Types of Lisp Exps. (contd.)
3. Quoted s-expressions:(quote s)
where s is any s-exp, not necessarily a Lisp expression;
The interpreter returns s as the value of (quote s)
where each of b1, e1, b2, e2, ..., bn, en is a Lisp exp.
To evaluate: first evaluate b1; if it evaluates to T [i.e., non-NIL], evaluate e1 and return the resulting value;
if b1 evaluates to NIL, evaluate b2 and if it evaluates to non-NIL, evaluate e2 and return the resulting value;
if b1 and b2 both evaluate to NIL, evaluate b3 and ...
If b1, b2, ..., bn evaluate to NIL, error!
10
CSE 755, part2
Types of Lisp Exps. (contd.)
4. Examples
(cond (T NIL) (NIL T)) : evaluates to ??
(cond (NIL T) (T NIL)) : evaluates to ??
(cond (NIL T) (NIL NIL)) : evaluates to ??
(cond (T NIL) (T NIL)) : evaluates to ??
(cond
( (eq (plus 2 3) (minus 4 5)) 10 )
( (eq (plus 2 3) (times 4 5))20 )
( (eq (plus 2 3) (plus 4 5)) 30 )
( T NIL ) ) : evaluates to ??
cond is also a special form; why?
11
CSE 755, part2
Types of Lisp Exps. (contd.)
5. Function definition:
(defun f (p1 p2 ...) fb)f is the name of the function being defined;p1, p2, ... are the formal parameters of f;fb is the body of the function -- itself a Lisp exp.
The interpreter saves the definition of f for use when f is applied
Example:(defun sum (x y) (plus x y) )defines a function named sum that has two parameters whose names are x, y [types?], and whose body is as given
(sum 3 4) evaluates to 7
defun is also a special form; why?
That's it! That is (essentially) all of Lisp!12
CSE 755, part2
Examples
Examples:(defun equal (x y) ; x, y need not be atoms
Atoms: Constants: simple; others: look up on a-list
Function application: (F a1 a2 a3 ... an)F is a function that expects n parameters; each ai is a Lisp expression;Evaluate each ai; bind that value aiv to pi, the
corr. parameter, by adding (pi . aiv) to the a-list;
then evaluate the body of FNote: If F is not a built-in function, get def. from d-
list
How Lisp expressions are eval'd
18
CSE 755, part2
(QUOTE S) : simply return S
(COND (b1 s1) ... (bn sn) ) :Eval. b1; if result is non-NIL, eval. s1 and return its
value;else, eval. b2, if result is non-NIL, eval. s2 & return its
val;...; eval. bn, if non-NIL, eval. sn & return its val;if b1, ..., bn all eval to NIL, print error, escape to top;
(DEFUN F (p1 ... pn) fb) : add (F . ((p1 ... pn) . fb)) to d-list.
How Lisp expressions are eval'd
19
Can be written in Lisp fairly easily ... one reason: Lisp functions and Lisp exps. are s-exps.
eval: evaluates a Lisp-exp. given current parameter bindings (a-list) and fn. definitions (d-list)
apply: applies a function to given set of argument values - gets the fn. definition from d-list, binds the formal pars to corr. arg.vals, then calls eval to evaluate the body of the fn.
evcon: evaluates a conditional Lisp-exp: Given ((b1 e1) (b2 e2) ...): uses eval to evaluate b1, b2, ...until one, bi, evaluates to non-NIL; uses eval to evaluate corr.ei and returns its value.