Docsity

CS421 Lecture 15: Transition Semantics - Understanding Programming Language Semantics - Pr, Study notes of Computer Science

Partial preview of the text

Download CS421 Lecture 15: Transition Semantics - Understanding Programming Language Semantics - Pr and more Study notes Computer Science in PDF only on Docsity!

Outline Transition SemanticsLanguage Semantics

CS421 Lecture 15: Transition Semantics^1

Mark Hills [email protected]

University of Illinois at Urbana-Champaign

July 13, 2006

(^1) Based on slides by Mattox Beckman, as updated by Vikram Adve, Gul Agha, and Elsa Gunter

Outline Transition Semantics^ Language Semantics

Language Semantics

Transition Semantics

Outline Transition Semantics^ Language Semantics^ An Overview of Semantics^ Varieties of Language Semantics

Language Semantics

Time flies like an arrow. Fruit flies like a banana.

So far, we have syntax, but we don’t know what any of it means! Language semantics provide a way for us to assign meaning to constructs in a programming language.

Outline Transition Semantics^ Language Semantics^ An Overview of Semantics^ Varieties of Language Semantics

Dynamic vs. Static

Semantics can be grouped into two general categories: static semantics and dynamic semantics. ◮ (^) Static semantics provide meaning based solely on the form of a language construct, or the syntax – not based on execution. These are usually restricted to type checking and inference. ◮ (^) Dynamic semantics provide meaning based on models of evaluation for the language. These tell us what it means to execute a program, for instance.

Outline Transition Semantics^ Language Semantics^ An Overview of Semantics^ Varieties of Language Semantics

Operational Semantics

◮ (^) First, start with a definition of our “machine” which we run programs on ◮ (^) The machine state, including the program, is often called the configuration ◮ (^) Next, describe how to execute programs in a given language by describing how to execute individual statements and parts of statements – rules follow the structure of the program ◮ (^) A program’s “meaning” is defined by how it changes the configuration ◮ (^) Useful as a basis for implementations

Outline Transition Semantics^ Language Semantics^ An Overview of Semantics^ Varieties of Language Semantics

Axiomatic Semantics

Axiomatic semantics are also called Floyd-Hoare logic ◮ (^) based on logic – first-order predicate calculus ◮ (^) semantics represented as a logical system build from axioms and inference rules ◮ (^) mainly suited to simple imperative languages ◮ (^) used to prove a post-condition from a pre-condition: given something holds in the starting state, we can show something else holds in the end state

{Precondition} Program {Postcondition}

Outline Transition Semantics^ Language Semantics^ An Overview of Semantics^ Varieties of Language Semantics

Alternative Methods

There are many other methods which have been devised to give semantics to languages. ◮ (^) “Compiler-based” semantics – the meaning is whatever the compiler says it is ◮ (^) Abstract Machines – similar to operational, an abstract machine with instructions, etc is devised and used to give semantics ◮ (^) Term Rewriting – semantics are formulated as term rewriting systems, with evaluation given by the rewriting relation ◮ (^) Rewriting Logic – an extension of equational logic that provides for concurrency

Outline Transition SemanticsLanguage Semantics

Introduction to Transition Semantics A Sample LanguageConfigurations Semantic Rules ExampleExtending the Language

Transition Semantics

Transition semantics is a form of operational semantics. ◮ (^) Configurations include the code and machine state: (C , m) ◮ (^) Semantics specified as transitions between configurations, altering the machine state ◮ (^) Rules of the form: 〈C , m〉 → 〈C ′, m′〉 ◮ (^) C , C ′^ represents the code yet to be executed ◮ (^) m, m′^ represents the state (store, memory, etc), often a finite map from names to values ◮ (^) May not need m – simple calculator languages with only numbers and operations don’t, for instance Key point: each transition indicates exactly one step of computation

Outline Transition SemanticsLanguage Semantics

Introduction to Transition Semantics A Sample Language Configurations Semantic Rules ExampleExtending the Language

IMP Syntax

Using a variant of BNF, we can specify the syntax for IMP as:

Aexp a ::= n | X | a 0 + a 1 | a 0 − a 1 | a 0 × a 1 Bexp b ::= true | false | a 0 = a 1 | a 0 ≤ a 1 | ¬b | b 0 ∧ b 1 | b 0 ∨ b 1 Com c ::= skip | X := a | c 0 ; c 1 | if b then c 0 else c 1 | while b do c

Important Point: We assume reasonable input programs that parse and we don’t care about precedence, associativity, etc – we assume all that has been figured out for us

Outline Transition SemanticsLanguage Semantics

Introduction to Transition Semantics A Sample Language Configurations Semantic Rules ExampleExtending the Language

IMP Configurations

Our configurations will contain two elements: ◮ (^) The code (a, b, or c) ◮ (^) The state We can define the set of states Σ as functions σ : Loc → N, or functions from locations to integer values.

Outline Transition SemanticsLanguage Semantics

Introduction to Transition Semantics A Sample LanguageConfigurations Semantic Rules ExampleExtending the Language

Arithmetic Expressions: Sums

〈a 0 , σ〉 → 〈a′ 0 , σ〉 〈a 0 + a 1 , σ〉 → 〈a′ 0 + a 1 , σ〉

〈a 1 , σ〉 → 〈a′ 1 , σ〉 〈n 0 + a 1 , σ〉 → 〈n 0 + a′ 1 , σ〉

〈n 0 + n 1 , σ〉 → n, where n = n 0 +int n 1

Outline Transition SemanticsLanguage Semantics

Introduction to Transition Semantics A Sample LanguageConfigurations Semantic Rules ExampleExtending the Language

Arithmetic Expressions: Subtraction

〈a 0 , σ〉 → 〈a′ 0 , σ〉 〈a 0 − a 1 , σ〉 → 〈a′ 0 − a 1 , σ〉

〈a 1 , σ〉 → 〈a′ 1 , σ〉 〈n 0 − a 1 , σ〉 → 〈n 0 − a′ 1 , σ〉

〈n 0 − n 1 , σ〉 → n, where n = n 0 −int n 1

Outline Transition SemanticsLanguage Semantics

Introduction to Transition Semantics A Sample LanguageConfigurations Semantic Rules ExampleExtending the Language

Boolean Rules and Simple Expressions

Our semantic relation for boolean expressions will be of the form:

〈b, σ〉 → t

We assume boolean expressions have no side-effects. ◮ (^) We have simple axioms for boolean constants, including true: 〈true, σ〉 → true ◮ (^) and false: 〈false, σ〉 → false

Outline Transition SemanticsLanguage Semantics

Introduction to Transition Semantics A Sample LanguageConfigurations Semantic Rules ExampleExtending the Language

Boolean Expressions: Equality

〈a 0 , σ〉 → 〈a′ 0 , σ〉 〈a 0 = a 1 , σ〉 → 〈a′ 0 = a 1 , σ〉

〈a 1 , σ〉 → 〈a′ 1 , σ〉 〈n 0 = a 1 , σ〉 → 〈n 0 = a′ 1 , σ〉

〈n 0 = n 1 , σ〉 → b, where b = (n 0 =int n 1 )

The rules for ≤ are similar.