- Concepts of Programming Languages

Formal Semantics

Instructor:

Learning Objectives

How to unambiguously define the semantics of a programming language?

  • Identify the difference between syntactic and semantic expressions in operational semantics
  • Identify judgments and reduction rules
  • Apply reduction rules to execute a program

A Small Programming Language

Language Constructs

  • Integer expressions
  • Statements
    • Assignment
    • Statement lists
    • Conditional
    • Loop

Example Program: Factorial

  1. n := -5;
  2. if n>=0
  3. then i := n
  4. else i := 0-n
  5. fi;
  6. f := 1;
  7. while i do
  8. f := f * i;
  9. i := i-1
  10. od

Operational Semantics

  • Operational semantics defines meaning of programs relative to an abstract machine
  • Reduction machine: operates on a program and reduces it to its semantic "value"
    • Uses a store (e.g., a map from variables to values)
    • State of the machine is a program or value and the store or
    • Judgments:

      Executing program in state yields () value and new state

    • Reduction rules:

      Conclusion follows from all premises satisfied

Language of Integer Expressions

Syntax

  1. Expr ::= Number
  2. | Expr '+' Expr
  3. | Expr '-' Expr
  4. | Expr '*' Expr
  5. | '(' Expr ')'

Example

  1. 2+3*4
  2. (2+3) * 4

Semantics

  • Semantic domain: integer arithmetic

  • Value is the meaning of an expression

    • For example, means
  • Numbers evaluate to their value

  • Operations map to integer operations

Reduction Example: Arithmetic Expression

Rules





  • Reduce to its semantic value

  • Deduction tree

Reduction Exercises: Arithmetic Expression

Rules





  • Define the rule for multiplication
  • Reduce to its semantic value

Assignments and Sequential Composition

Syntax

  1. Expr ::= Number
  2. | Expr '+' Expr
  3. | Expr '-' Expr
  4. | Expr '*' Expr
  5. | Ident
  6. | PrgSeq
  7. | '(' Expr ')'
  8. Ident ::= 'a' | 'b' | ... | 'z'
  9. PrgSeq ::= Prg | Prg ';' PrgSeq
  10. Prg ::= Ident ':=' Expr

Example

  1. a := 2+3;
  2. b := (a:=a+1)*4;
  3. a := b-5

Semantics

  • How do we store/look up values of variables?
  • Look up variable value in store

  • Assignment: evaluate expression, then update state


  • Sequence: evaluate subexpressions, chain results

Conditionals and Loops

Syntax

  1. Expr ::= Number
  2. | Expr '+' Expr
  3. | Expr '-' Expr
  4. | Expr '*' Expr
  5. | Ident
  6. | Expr '>=' Expr
  7. | PrgSeq
  8. | '(' Expr ')'
  9. Ident ::= 'a' | 'b' | ... | 'z'
  10. PrgSeq ::= Prg | Prg ';' PrgSeq
  11. Prg ::= Ident ':=' Expr
  12. | 'if' Expr
  13. 'then' Expr
  14. 'else' Expr 'fi'
  15. | 'while' Expr 'do'
  16. Expr
  17. 'od'

Semantics

  • Conditional
    • True:
    • False:
  • Loop
    • End:
    • Recurse:

Summary

  • Operational semantics: defines language in terms of operations of an abstract machine
    • Alternative semantics definitions: denotational semantics, axiomatic semantics
  • Judgments and reduction rules: describe steps of the abstract machine, expressed as conclusions from premises
  • Deduction tree: makes conclusions about program from the meaning of the program's components

Program Reduction Example: Absolute Value

  1. i := -2;
  2. if i>=0
  3. then i := i
  4. else i := 0-i
  5. fi
  • Integer arithmetic
  • Assignment
  • Sequential composition
  • Comparison
  • Conditional

Program Reduction Example: Absolute Value

  • Lemma , where

  • Lemma ,

Program Reduction Example: Factorial

  1. n := -5;
  2. if n>=0
  3. then i := n
  4. else i := 0-n
  5. fi;
  6. f := 1;
  7. while i do
  8. f := f*i;
  9. i := i-1
  10. od
  • Integer arithmetic
  • Assignment
  • Sequential composition
  • Conditional
  • Loop

Program Reduction Example: Factorial

  • Lemma , where

  • Lemma , where

Program Reduction Example: Factorial

  • Lemma , where

  • Lemma

  • Lemma

  • Lemma , where