Docsity

Operational Semantics of Programming Languages: A Deep Dive into Evaluating Expressions, Study notes of Programming Languages

Partial preview of the text

Download Operational Semantics of Programming Languages: A Deep Dive into Evaluating Expressions and more Study notes Programming Languages in PDF only on Docsity!

CMSC 330: Organization of

Programming Languages

Operational Semantics

Review of PL

  • (^) Syntax
    • (^) Regular expressions
    • (^) Finite automata
    • (^) Context-free grammars
  • (^) Semantics
    • (^) Operational semantics
    • (^) Lambda calculus
  • (^) Implementation
    • (^) Concurrency
    • (^) Generics
    • (^) Argument passing
    • (^) Scoping
    • (^) Exceptions

Operational Semantics

  • (^) There are several different kinds of semantics
    • (^) Denotational : A program is a mathematical function
    • (^) Axiomatic : Develop a logical proof of a program
      • (^) Give predicates that hold when a program (or part) is executed
  • (^) We will briefly look at operational semantics
    • (^) A program is defined by how you execute it on a mathematical model of a machine
  • (^) We will look at a subset of OCaml as an example

Roadmap: Semantics of a program

Grammar: SA A  id = E E  T+E | T T  P * T | P P  id | n | (E) Program: X = 2 + 3 Compilation: Push X Push 2 Push 3 Add top 2 Assign top to second Postorder: postfix X 2 3 + = (Rec Des) Parsing: ast =

2 3 X value Program semantics: =

2 3 X

OCaml Programs

  • (^) E ::= x | n | true | false | [] | if E then E else E | fun x = E | E E
  • (^) x stands for any identifier
  • (^) n stands for any integer
  • (^) true and false stand for the two boolean values
  • (^) [] is the empty list
  • (^) Using = in fun instead of -> to avoid some confusion later

Values

  • (^) v ::= n | true | false | [] | v::v
    • (^) n is an integer ( not a string corresp. to an integer)
      • (^) Same idea for true , false, []
    • (^) v1::v2 is the pair with v1 and v
      • (^) This will be used to build up lists
      • (^) Notice: nothing yet requires v2 to be a list
    • (^) Important: Be sure to understand the difference between program text S and mathematical objects v. - (^) E.g., the text 3 evaluates to the mathematical number 3
    • (^) To help, we’ll use different colors and italics
      • (^) This is usually not done, and it’s up to the reader to remember which is which

Operational Semantics Rules

  • (^) Each basic entity evaluates to the corresponding value n → n true → true false → false [] → []

Operational Semantics Rules (cont’d)

  • (^) How about built-in functions?
    • (^) We’re applying the + function
      • (^) Ignore currying for the moment, and pretend we have multi- argument functions
    • (^) On the right-hand side, we’re computing the mathematical sum; the left-hand side is source code
    • (^) But what about + (+ 3 4) 5?
      • (^) We need recursion
        • n m → n + m

Error Cases

  • (^) Because we wrote n , m in the hypothesis, we mean that they must be integers
  • But what if E 1 and E 2 aren’t integers?
    • (^) E.g., what if we write + false true?
    • (^) It can be parsed, but we can’t execute it
  • (^) We will have no rule that covers such a case
    • (^) Runtime error!

+ E

1

E

2 → n + m

E

1 → n E 2 → m

Trees of Semantic Rules

  • (^) When we apply rules to an expression, we actually get a tree - (^) Corresponds to the recursive evaluation procedure - (^) For example: + (+ 3 4 ) 5 + ( + 3 4) 5 →

Rule for ::

  • (^) So :: allocates a pair in memory
  • Are there any conditions on E 1 and E 2
  • No! We will allow E 2 to be anything
  • (^) OCaml’s type system will disallow non-lists

:: E

1

E

2

E

1 → v 1

E

2 → v 2 v 1 :: v 2

Rules for Identifiers

  • (^) Let’s assume for now that the only identifiers are parameter names - (^) Ex. (fun x = + x 3) 4 - (^) When we see x in the body, we need to look it up - (^) So we need to keep some sort of environment - (^) This will be a map from identifiers to values

Rules for Identifiers and Application

  • (^) To evaluate a user-defined function applied to an argument: - (^) Evaluate the argument (call-by-value) - (^) Evaluate the function body in an environment in which the formal parameter is bound to the actual argument - (^) Return the result A ⊢ x → A(x) A ⊢ ( (fun x = E 1

) E

2 ) → v'

A ⊢ E

2 → v A, x↦ v ⊢ E 1 → v' no hypothesis means “in all cases”

Operational Semantics Rules

A ⊢ (fun x = e 1 ) e 2 → v' A ⊢ e 2 → v A, x↦ v ⊢ e 1 → v' A ⊢ x → v A ⊢ + e 1 e 2 → n + m A ⊢ e 1 → n A ⊢ e 2 → m A ⊢ n → n SInt SAdd SFun SVar x↦ v ∈ A