Operational Semantics - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Operational Semantics

Description:

Title: Computer and Information Science Engineering Subject: Dept Vist Author: Jeremy Loomis Last modified by: neelam Created Date: 10/20/1996 4:17:05 PM – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 25
Provided by: Jeremy239
Learn more at: https://cse.osu.edu
Category:

less

Transcript and Presenter's Notes

Title: Operational Semantics


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
  • References McCarthy and others Lisp 1.5
    programmer's manual

2
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

3
Lisp Basics (contd.)
  • Basic functions
  • cons s1, s2 (s1 . s2)
  • car (s1 . s2) s1Note car s gives error
    if s is atomic
  • cdr (s1 . s2) s2Note 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.

4
  • Problem Suppose we have four s-exps, s1, s2,
    s3, s4 that we want to keep together. Possible
    ways
  • ((s1 . s2) . (s3 . s4))
  • (s1 . (s2 . (s3 . s4)))
  • (((s1 . s2) . s3) . s4) what about (s1 . s2 .
    s3 . s4) ?
  • Standard solution
  • (s1 . (s2 . (s3 . (s4 . NIL))))
  • Notation
  • (s1 s2 s3 s4) denotes (s1 . (s2 . (s3 . (s4
    . NIL))))
  • (s1 s2 s3) denotes (s1 . (s2 . (s3 . NIL)))
  • Important
  • (s1 s2 s3) etc. is only a notation not a new
    type of s-exp
  • ( ) denotes the atom NIL

5
Lisp Basics (contd.)
  • car (2 3) 2
  • cdr(2 3) ( 3 ) Note not 3
  • cons 2 , 3 (2 . 3) Note not (2 3)
  • cons 2 , ( 3 . NIL) (2 . (3 . NIL))same
    cons 2 , ( 3 ) (2 . (3 . NIL))same cons 2
    , ( 3 ) (2 3)
  • cons (s1 s2), (s3 s4) ((s1 s2) . (s3
    s4)) ! (s1 s2 s3 s4)
  • cons (s1 . s2), (s3 s4) ((s1 . s2) . (s3
    s4)) ((s1 . s2) s3 s4) why?
  • cons (s1 s2), (s3 . s4) ?cons (s1 . s2), (s3
    . s4) ?

6
Lisp Basics (contd.)
  • eq T, T T
  • eq T, NIL NIL
  • eq NIL, () T
  • eqcar(2 . 3), cdr(3 . 2) T
  • eq(2 . 3), (3 . 2) error arguments to eq
    must be atoms
  • eqplus2 , 3, minus10, 5 T
  • nullT NIL
  • nullNIL T
  • nullcar(2) NIL
  • nullcdr(2) T
  • atomcar(2) T
  • atomcdr(2) T

7
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

8
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!

9
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)
  • Examples
  • (car (3 . 4)) will output error
  • (car (quote (3 . 4))) will output 3
  • (quote (3 . 4)) will output (3 . 4)
  • (cons (quote (1 . 2)) (quote (3 .4))) will
    output ((1 . 2) . (3 . 4))
  • (quote (cons (1 . 2) (3 . 4))) will output
    ??
  • (plus (car (2 . 3) ) (cdr (2 . 3)) ) will
    output ??
  • (plus (quote (2 . 3) ) (quote 3) ) will
    output ??

10
  • 3. Quoted s-expressions (quote s) looks like
    a function call with quote being the function
    called but it is not quote is a special form
  • 4. Conditional (cond (b1 e1) (b2 e2) ...
    (bn en) )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!

11
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?

12
Types of Lisp Exps. (contd.)
  • 5. Function definition
  • (defun f (p1 p2 ...) fb)f is the name of
    the function being definedp1, p2, ... are the
    formal parameters of ffb 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!

13
Examples
  • Examples(defun equal (x y) x, y need not
    be atoms
  • (cond
  • ( (atom x) (cond ( (atom y)
    (eq x y) )
  • ( T NIL) )
  • ( (atom y) NIL)
  • ((equal (car x) (car y)) (equal (cdr
    x) (cdr y)) )
  • (T NIL) ) )
  • Consider (equal 4 4) (equal 4 (plus 2
    2))
  • (equal (car (2 . 3)) (plus 1 1) ) watch
    out!

14
  • Examples(defun equal (x y) (cond
    ( (atom x) (cond ( (atom y) (eq x y) ) ( T
    NIL) )
  • ( (atom y) NIL)
  • ( (equal (car x) (car y)) (equal (cdr
    x) (cdr y)) )
  • (T NIL) ) )
  • "Design" notation
  • equalx, y atomx ? atomy ? eqx,
    y
  • T ? NIL
  • atomy ? NIL
  • equalcarx, cary ? equalcdrx,
    cdry
  • T ? NIL

15
Examples (contd.)
  • xmembx, list is x a member of list?
  • xmembx, list
  • nulllist ? NIL
  • eqx, carlist ? T
  • T ? xmembx, cdrlist
  • (defun xmemb (x list)
  • (cond
  • ( (null list) NIL)
  • ( (eq x (car list)) T)
  • ( T (xmemb x (cdr list)) ) ) )

16
Examples (contd.)
  • xunions1, s2 s1, s2 are two lists xunion
    must return s1 ? s2
  • xunions1, s2
  • nulls1 ? s2
  • nulls2 ? s1
  • T ? xmembcars1, s2 ? xunioncdrs1,s2
  • T ? conscars1, xunioncdrs1,s2
  • xunions1, s2
  • nulls1 ? s2
  • nulls2 ? s1
  • xmembcars1, s2 ? xunioncdrs1,s2
  • T ? conscars1, xunioncdrs1,s2

17
Different styles in Lisp
  • maxListL returns max of number in
    non-empty list L
  • Functional
  • nullcdrL ? carL
  • gtcarL, maxListcdrL ? carL
  • T ? maxListcdrL
  • Functional but better
  • nullcdrL ? carL
  • T ? bigger carL, maxListcdrL //
    define bigger
  • "imperative"
  • maxListL max2carL, cdrL
  • max2x, L
  • nullL ? x
  • gtx, carL ? max2x, cdrL
  • T ? max2 carL, cdrL

18
How Lisp expressions are eval'd
  • 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
    F Note If F is not a built-in function, get
    def. from d-list

19
How Lisp expressions are eval'd
  • (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.

20
Lisp Interpreter Details
  • 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.
  • evlis evaluates a list of Lisp-exps.

21
Lisp Interpreter Details (in Lisp)
  • interpreterdList evalexp, NIL, dList
  • or better output evalinput, NIL,
    dList
  • evlislist, aList, dList
  • nulllist ? NIL
  • T ? cons evalcarlist, aList, dList,
    evliscdrlist, ..,..
  • evconbe, aList, dList // be is of form ((b1
    e1) ... (bn en))
  • nullbe ? NIL // better error!
  • evalcaarbe, aList, dList ?
    evalcadarbe, aList, dList
  • T ? evconcdrbe, aList, dList

22
Lisp Interpreter Details (in Lisp)
  • eval exp, aList, dList
  • atomexp --gt intexp --gt exp
  • eqexp,T --gt T
  • eqexp,NIL --gt NIL
  • inexp,aList --gt getValexp,aList
  • T --gt "unbound variable!"
  • atomcarexp --gt
  • eqcarexp,QUOTE --gt cadrexp
  • eqcarexp,COND --gt evconcdrexp,
    aList, dList
  • eqcarexp,DEFUN --gt "add to dList
    (state change!)"
  • T --gt applycarexp, evliscdrexp,aLis
    t,dList,
  • aList, dList
  • T --gt "error!"

23
Lisp Interpreter Details (in Lisp)
  • apply f, x, aList, dList
  • atomf --gt eqf, CAR --gt caarx //
    why?
  • eqf, CDR --gt cdarx
  • eqf, CONS --gt conscarx, cdrx
    //??
  • eqf, ATOM --gt atomcarx
  • eqf, NULL --gt nullcarx
  • eqf, EQ --gt eqcarx, cadrx
  • T --gt eval cdrgetvalf, dList,
  • addpairscargetvalf, dList, x,
    aList,
  • dList
  • T --gt "error!"
  • Elements on dList of form (f . (pList . body) )
  • addpairspList,x,aList returns new a-list

24
Lisp Interpreter Some comments
  • We assumed s-expressions have been implemented
    no way to specify how to do this within Lisp
  • Fact that Lisp-expns (to be evaluated) function
    bodies were s-exps key in writing a
    meta-circular interpreter (mci)
  • The mci for Lisp defines its operational sem.
    since it tells us how Lisp-expns are to be
    evaluated -- but it assumes we have some basic
    knowledge about Lisp
  • You could change the o.s. of Lisp by rewriting
    appropriate parts of the main functions of the
    mci -- CLOS-MOP allows the Lisp programmer
    to do so!
Write a Comment
User Comments (0)
About PowerShow.com