Download Operational Semantics, Formal Semantics - Notes | CMSC 330 and more Study notes Programming Languages in PDF only on Docsity!
CMSC 330: Organization of
Programming Languages
Operational Semantics
CMSC 330 2
Introduction
We’ve looked at several formal methods for defining the syntax of a programming language
- Regular expressions
- Context-free grammars
What about formal methods for defining the semantics of a programming language?
- I.e., what does a program mean?
CMSC 330 3
Roadmap: Compilation of Program
Grammar: SA AE id = ET+E | T TP P * T | Pid | n | (E)
Program: X = 2 + 3
Compilation: Push X Push 2Push 3 Add top 2Assign top to second
Postorder:postfix X 2 3 + =
Recursive DescentParsing: AST
2 3
X
Program semantics:What does program mean
Is this correct?What is program supposed to do? CMSC 330 4
Formal Semantics
Formal semantics of a programming language
- Mathematical model of all possible computations performed by programs written in that language
Three main approaches to formal semantics
- Denotational
- Operational
- Axiomatic
CMSC 330 5
Formal Semantics (cont.)
Denotational semantics
- Translate parts of language into another language Usually a mathematical function Equivalent to compilation Operational semantics
- Describe effect of parts of language Usually on (a mathematical model of) an abstract machine For lambda calculus, can use syntactic transformations Equivalent to interpretation Axiomatic semantics
- Describe each part of language through logical axioms CMSC 330 6
Operational Semantics
We will briefly look at operational semantics
- Using a subset of OCaml as an example
Useful for
- Specifying the meaning of a program
- Proving the correctness of an algorithm Through formal verification For cryptographic algorithms, combinatorial circuits, etc… Currently limited to smaller programs
CMSC 330 7
Roadmap: Semantics of a Program
Grammar: SA AE id = ET+E | T TP P * T | Pid | n | (E)
Program: X = 2 + 3
Compilation: Push X Push 2Push 3 Add top 2Assign top to second
Postorder:postfix X 2 3 + =
Recursive DescentParsing: AST
2 3
X
value
Program Semantics:
2 3
X
CMSC 330 8
Evaluation
We’re going to define a relation E → v
- This means “expression E evaluates to v”
So we need a formal way of defining programs and of defining things they may evaluate to
We’ll use grammars to describe each of these
- One to describe abstract syntax trees E
- One to describe OCaml values v
CMSC 330 9
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
CMSC 330 10
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 If not present, it’s up to you to remember which is which
CMSC 330 11
Grammars for Trees We’re just using grammars to describe trees E ::= x | n | true | false | [] | if E then E else E | fun x = E | E E v ::= n | true | false | [] | v::v
type value = Val_Num of int | Val_Bool of bool | Val_Nil | Val_Pair of value * value
type ast = Id of string | Num of int | Bool of bool | Nil | If of ast * ast * ast | Fun of string * ast | App of ast * ast
Given a program, we know how to convert it to an AST using recursive descent parsing
Goal: For any AST, we want an operational rule to obtain a value that represents the execution of that AST (^) CMSC 330 12
Operational Semantics Rules
Each basic entity evaluates to its corresponding value
n → n
true → true
false → false
[] → []
CMSC 330 19
Rules for Identifiers
Let’s assume for now that the only identifiers are parameter names
- Example: (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
CMSC 330 20
Semantics with Environments
Extend rules to the form A; E → v
- Means in environment A, program text E evaluates to v Notation
- We write • for the empty environment (may be omitted)
- We write A(x) for the value that x maps to in A
- We write A, x: v for the same environment as A, except x is now v x might or might not have mapped to anything in A
- We write A, A' for the environment with the bindings of A' added to and overriding the bindings of A
CMSC 330 21
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”
CMSC 330 22
Example: (fun x = + x 3) 4 =?
•; (fun x = + x 3) 4 →
•; 4 → 4 •, x: 4 ; + x 3 →
•, x: 4 ; x → 4 •, x: 4 ; 3 → 3 7
7
CMSC 330 23
Nested Functions
This works for cases of nested functions
- ...as long as they are fully applied
But what about the true higher-order cases?
- Passing functions as arguments, and returning functions as results
- We need closures to handle this case
- ...and a closure was just a function and an environment
- We already have notation around for writing both parts
CMSC 330 24
Closures
Formally, we add closures (A, λ x.E) to values
- A is the environment in which the closure was created
- x is the parameter name
- E is the source code for the body λ x is a binding of x in E v ::= n | true | false | [] | v::v | (A, λ x.E)
CMSC 330 25
Revised Rule for Lambda
To evaluate a function definition, create a closure when the function is created
- Notice that we don’t look inside the function body
A; fun x = E → (A, λ x.E)
CMSC 330 26
Revised Rule for Application
To apply something to an argument:
- Evaluate it to produce a closure
- Evaluate the argument (call-by-value)
- Evaluate the body of the closure, in The current environment, extended with the closure’s environment, extended with the binding for the parameter
A; (E 1 E 2 ) →
A; E 1 → (A', λ x.E) A, A', x: v ; E → v'
A; E 2 → v
v'
CMSC 330 27
Example
Let = (fun x = (fun y = + x y)) 3
•; (fun x = (fun y = + x y)) 3 →
•; (fun x = (fun y = + x y)) →
( x: 3, λ y.(+ x y))
( • , λ x.(fun y = + x y))
x: 3 ; (fun y = + x y) →
( x: 3, λ y.(+ x y))
CMSC 330 28
Example (cont.)
•; ( 4 )→
•; → ( x: 3, λ y.(+ x y))
•; 4 → 4 x: 3 , y: 4 ; (+ x y) →
7
CMSC 330 29
Why Did We Do This?
Operational semantics are useful for
- Describing languages Not just OCaml! It’s pretty hard to describe a big language like C or Java, but we can at least describe the core components of the language
- Giving a precise specification of how they work Look in any language standard – they tend to be vague in many places and leave things undefined
- Reasoning about programs We can actually prove that programs do something or don’t do something, because we have a precise definition of how they work