principles of programming languages lecture 3: semantics · semantic - informal informal semantics...

62
Principles of Programming Languages Lecture 3: Semantics Andrei Arusoaie 1 1 Department of Computer Science October 17, 2017

Upload: others

Post on 16-Jul-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Principles of Programming LanguagesLecture 3: Semantics

Andrei Arusoaie1

1Department of Computer Science

October 17, 2017

Page 2: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Outline

Semantics: introduction

The K “machinery”

IMP: a simple imperative language in K

Page 3: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Semantics: motivation

C

-$ cat test.cint main(){int x;return (x=1) + (x=2);

}-$ gcc test.c-$ ./a.out ; echo $?4

Java

-$ cat File.javapublic class File {... void main(...) {

int x = 0;println((x=1) + (x=2));

}}-$ javac File.java-$ java File3

Page 4: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Semantics: motivation

C

-$ cat test.cint main(){int x;return (x=1) + (x=2);

}-$ gcc test.c-$ ./a.out ; echo $?4

Java

-$ cat File.javapublic class File {... void main(...) {

int x = 0;println((x=1) + (x=2));

}}-$ javac File.java-$ java File3

Page 5: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Semantics: motivation

C

-$ cat test.cint main(){int x;return (x=1) + (x=2);

}-$ gcc test.c-$ ./a.out ; echo $?4

Java

-$ cat File.javapublic class File {... void main(...) {

int x = 0;println((x=1) + (x=2));

}}-$ javac File.java-$ java File3

Page 6: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Semantics: motivation

GCC: 5.4.0-6 ubuntu

-$ cat test.cint main(){int x;return (x=1) + (x=2);

}-$ gcc test.c-$ ./a.out ; echo $?4

GCC: LLVM version 9.0.0 - clang

-$ cat test.cint main(){int x;return (x=1) + (x=2);

}-$ gcc test.c-$ ./a.out ; echo $?3

Page 7: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Semantics: motivation

GCC: 5.4.0-6 ubuntu

-$ cat test.cint main(){int x;return (x=1) + (x=2);

}-$ gcc test.c-$ ./a.out ; echo $?4

GCC: LLVM version 9.0.0 - clang

-$ cat test.cint main(){int x;return (x=1) + (x=2);

}-$ gcc test.c-$ ./a.out ; echo $?3

Page 8: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Semantics: motivation

GCC: 5.4.0-6 ubuntu

-$ cat test.cint main(){int x;return (x=1) + (x=2);

}-$ gcc test.c-$ ./a.out ; echo $?4

GCC: LLVM version 9.0.0 - clang

-$ cat test.cint main(){int x;return (x=1) + (x=2);

}-$ gcc test.c-$ ./a.out ; echo $?3

Page 9: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Semantics: motivation

GCC: 5.4.0-6 ubuntu

-$ cat test.cint main(){int x;return (x=1) + (x=2);

}-$ gcc test.c-$ ./a.out ; echo $?4

GCC: LLVM version 9.0.0 - clang

-$ cat test.cint main(){int x;return (x=1) + (x=2);

}-$ gcc test.c-$ ./a.out ; echo $?3

Page 10: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Demo

I out-of-lifetime.c

Page 11: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Semantics

I Semantics is concerned with the meaning of languageconstructs

I Semantics must be unambiguousI Semantics must be flexible

Page 12: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Semantic - informal

Informal semantics (examples): natural language

Rationale for the ANSI C Programming Language:

I “Trust the programmer”I “Don’t prevent the programmer from doing what needs to

be done”I “Keep the language small and simple”I “Provide only one way to do an operation”I “Make it fast, even if it is not guaranteed to be portable”

Inexact! – could lead to undefined behavior in programs

Page 13: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Semantic - informal

Informal semantics (examples): natural language

Rationale for the ANSI C Programming Language:

I “Trust the programmer”I “Don’t prevent the programmer from doing what needs to

be done”I “Keep the language small and simple”I “Provide only one way to do an operation”I “Make it fast, even if it is not guaranteed to be portable”

Inexact! – could lead to undefined behavior in programs

Page 14: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Semantic - informal

Informal semantics (examples): natural language

Rationale for the ANSI C Programming Language:

I “Trust the programmer”I “Don’t prevent the programmer from doing what needs to

be done”I “Keep the language small and simple”I “Provide only one way to do an operation”I “Make it fast, even if it is not guaranteed to be portable”

Inexact! – could lead to undefined behavior in programs

Page 15: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Semantic - informal

Informal semantics (examples): natural language

Rationale for the ANSI C Programming Language:

I “Trust the programmer”I “Don’t prevent the programmer from doing what needs to

be done”I “Keep the language small and simple”I “Provide only one way to do an operation”I “Make it fast, even if it is not guaranteed to be portable”

Inexact! – could lead to undefined behavior in programs

Page 16: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Semantic - informal

Informal semantics (examples): natural language

Rationale for the ANSI C Programming Language:

I “Trust the programmer”I “Don’t prevent the programmer from doing what needs to

be done”I “Keep the language small and simple”I “Provide only one way to do an operation”I “Make it fast, even if it is not guaranteed to be portable”

Inexact! – could lead to undefined behavior in programs

Page 17: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Semantic - informal

Informal semantics (examples): natural language

Rationale for the ANSI C Programming Language:

I “Trust the programmer”I “Don’t prevent the programmer from doing what needs to

be done”I “Keep the language small and simple”I “Provide only one way to do an operation”I “Make it fast, even if it is not guaranteed to be portable”

Inexact! – could lead to undefined behavior in programs

Page 18: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Semantic - informal

Informal semantics (examples): natural language

Rationale for the ANSI C Programming Language:

I “Trust the programmer”I “Don’t prevent the programmer from doing what needs to

be done”I “Keep the language small and simple”I “Provide only one way to do an operation”I “Make it fast, even if it is not guaranteed to be portable”

Inexact! – could lead to undefined behavior in programs

Page 19: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Semantic styles

Some (formal) semantics styles:I operationalI denotationalI axiomatic

We will focus more on operational semantics styles: Ksemantics, Small-step SOS, Big-Step SOS

Page 20: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Semantic styles

Some (formal) semantics styles:I operationalI denotationalI axiomatic

We will focus more on operational semantics styles: Ksemantics, Small-step SOS, Big-Step SOS

Page 21: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

A framework for defining PL semantics

A framework for defining semantics needs to be:I expressiveI modularI executableI based on some "logic of programs" - enables reasoning

Page 22: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

The K “machinery”

I We saw that K can be used to define syntaxI For semantics we have to understand the following key

ingredients:I KomputationsI ConfigurationsI Rules

Page 23: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Configurations & Komputations & Rules

I We need a way to model program statesI K configurations:

I structures of cells: <k> 2 + 3 + 5 </k>

I Komputations: units of calculusI The <k> cell is special: it contains the $PGM as a list of

computationsI Example: <k> 2 + 3 y � + 5 <k>I y is a separator for a KListI � is placeholder for a computation

I Your first K rule:I rule I1 + I2 => I1 +Int I2

I DEMO!

Page 24: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Configurations & Komputations & Rules

I We need a way to model program statesI K configurations:

I structures of cells: <k> 2 + 3 + 5 </k>

I Komputations: units of calculusI The <k> cell is special: it contains the $PGM as a list of

computationsI Example: <k> 2 + 3 y � + 5 <k>I y is a separator for a KListI � is placeholder for a computation

I Your first K rule:I rule I1 + I2 => I1 +Int I2

I DEMO!

Page 25: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Configurations & Komputations & Rules

I We need a way to model program statesI K configurations:

I structures of cells: <k> 2 + 3 + 5 </k>

I Komputations: units of calculusI The <k> cell is special: it contains the $PGM as a list of

computationsI Example: <k> 2 + 3 y � + 5 <k>I y is a separator for a KListI � is placeholder for a computation

I Your first K rule:I rule I1 + I2 => I1 +Int I2

I DEMO!

Page 26: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Configurations & Komputations & Rules

I We need a way to model program statesI K configurations:

I structures of cells: <k> 2 + 3 + 5 </k>

I Komputations: units of calculusI The <k> cell is special: it contains the $PGM as a list of

computationsI Example: <k> 2 + 3 y � + 5 <k>I y is a separator for a KListI � is placeholder for a computation

I Your first K rule:I rule I1 + I2 => I1 +Int I2

I DEMO!

Page 27: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Filling context in rules

I K rules establish transitions between configurationsI Here is how the above rule should look like:

rule <k> I1 + I2 y K </k>=><k> I1 +Int I2 y K </k>

I The K above is a variable and stands for otherkomputations

I The +Int is the mathematical addition over integersI The K tool completes the context automatically

Page 28: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Filling context in rules

I K rules establish transitions between configurationsI Here is how the above rule should look like:

rule <k> I1 + I2 y K </k>=><k> I1 +Int I2 y K </k>

I The K above is a variable and stands for otherkomputations

I The +Int is the mathematical addition over integersI The K tool completes the context automatically

Page 29: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Filling context in rules

I K rules establish transitions between configurationsI Here is how the above rule should look like:

rule <k> I1 + I2 y K </k>=><k> I1 +Int I2 y K </k>

I The K above is a variable and stands for otherkomputations

I The +Int is the mathematical addition over integersI The K tool completes the context automatically

Page 30: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Filling context in rules

I K rules establish transitions between configurationsI Here is how the above rule should look like:

rule <k> I1 + I2 y K </k>=><k> I1 +Int I2 y K </k>

I The K above is a variable and stands for otherkomputations

I The +Int is the mathematical addition over integersI The K tool completes the context automatically

Page 31: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Evaluation. Heating and cooling.

I Consider the program: 2 + 3I When we apply rule I1 + I2 => I1 +Int I2 we get:

I <k> 5 </k>

I But, for: 2 + 3 + 5I When we apply rule I1 + I2 => I1 +Int I2 we get:

I <k> 2 + 3 + 5 </k>

I Solution: heating/cooling rules!I Explained on the blackboard!I strictI KResult

Page 32: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Evaluation. Heating and cooling.

I Consider the program: 2 + 3I When we apply rule I1 + I2 => I1 +Int I2 we get:

I <k> 5 </k>

I But, for: 2 + 3 + 5I When we apply rule I1 + I2 => I1 +Int I2 we get:

I <k> 2 + 3 + 5 </k>

I Solution: heating/cooling rules!I Explained on the blackboard!I strictI KResult

Page 33: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Evaluation. Heating and cooling.

I Consider the program: 2 + 3I When we apply rule I1 + I2 => I1 +Int I2 we get:

I <k> 5 </k>

I But, for: 2 + 3 + 5I When we apply rule I1 + I2 => I1 +Int I2 we get:

I <k> 2 + 3 + 5 </k>

I Solution: heating/cooling rules!I Explained on the blackboard!I strictI KResult

Page 34: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

More complex configurations: the IMP configuration

I We need a way to model IMP program states. Why?I Assignments require a state where variables are stored.I We add a new cell called <env> to store variables and

their valuesI IMP configuration:

I Example:

configuration <T><k> x = 3; <k><env> x |-> 0 <env>

</T>

Page 35: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

More complex configurations: the IMP configuration

I We need a way to model IMP program states. Why?I Assignments require a state where variables are stored.I We add a new cell called <env> to store variables and

their valuesI IMP configuration:

I Example:

configuration <T><k> x = 3; <k><env> x |-> 0 <env>

</T>

Page 36: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Rule for assignment

rule <T><k> X = V; y K </k><env> X |-> _ </env>

</T>=><T><k> K </k><env> X |-> V </env>

</T>

Page 37: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Rule for assignment

rule <T><k> X = V; y K </k><env> X |-> _ </env>

</T>=><T><k> K </k><env> X |-> V </env>

</T>

Page 38: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Rules at work

The K rule:

rule <T><k> X = V; y K </k><env> X |-> _ </env>

</T>=>

<T><k> K </k><env> X |-> V </env>

</T>

An example:

<T><k> x = 2; y y = 2; <k><env> x |-> 0 <env>

</T>=><T><k> y = 2; <k><env> x |-> 2 <env>

</T>

Page 39: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Rules at work

The K rule:

rule <T><k> X = V; y K </k><env> X |-> _ </env>

</T>=>

<T><k> K </k><env> X |-> V </env>

</T>

An example:

<T><k> x = 2; y y = 2; <k><env> x |-> 0 <env>

</T>=><T><k> y = 2; <k><env> x |-> 2 <env>

</T>

Page 40: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Rules at work

The K rule:

rule <T><k> X = V; y K </k><env> X |-> _ </env>

</T>=>

<T><k> K </k><env> X |-> V </env>

</T>

An example:

<T><k> x = 2; y y = 2; <k><env> x |-> 0 <env>

</T>=><T><k> y = 2; <k><env> x |-> 2 <env>

</T>

Page 41: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Abstractions

Configuration abstraction: write in rules only what is changing!

The K rule:

rule <T><k> X = V; y K </k><env> X |-> _ </env>

</T>=>

<T><k> K </k><env> X |-> V </env>

</T>

An example:

<T><k> x = 2; y y = 2; <k><env> x |-> 0 <env>

</T>=><T><k> y = 2; <k><env> x |-> 2 <env>

</T>

The K definition compiler fills the context for us!

Page 42: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Abstractions

Configuration abstraction: write in rules only what is changing!

The K rule:

rule <T><k> X = V; y K </k><env> X |-> _ </env>

</T>=>

<T><k> K </k><env> X |-> V </env>

</T>

An example:

<T><k> x = 2; y y = 2; <k><env> x |-> 0 <env>

</T>=><T><k> y = 2; <k><env> x |-> 2 <env>

</T>

The K definition compiler fills the context for us!

Page 43: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Abstractions

Configuration abstraction: write in rules only what is changing!

The K rule:

rule <T><k> X = V; y K </k><env> X |-> _ </env>

</T>=>

<T><k> K </k><env> X |-> V </env>

</T>

An example:

<T><k> x = 2; y y = 2; <k><env> x |-> 0 <env>

</T>=><T><k> y = 2; <k><env> x |-> 2 <env>

</T>

The K definition compiler fills the context for us!

Page 44: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Abstractions

Configuration abstraction: write in rules only what is changing!

The K rule:

rule <T><k> X = V; y K </k><env> X |-> _ </env>

</T>=>

<T><k> K </k><env> X |-> V </env>

</T>

An example:

<T><k> x = 2; y y = 2; <k><env> x |-> 0 <env>

</T>=><T><k> y = 2; <k><env> x |-> 2 <env>

</T>

The K definition compiler fills the context for us!

Page 45: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Abstractions

Configuration abstraction: write in rules only what is changing!

The K rule:

rule <T><k> X = V; y K </k><env> X |-> _ </env>

</T>=>

<T><k> K </k><env> X |-> V </env>

</T>

An example:

<T><k> x = 2; y y = 2; <k><env> x |-> 0 <env>

</T>=><T><k> y = 2; <k><env> x |-> 2 <env>

</T>

The K definition compiler fills the context for us!

Page 46: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Abstractions

Local rewrites: put the rewrite inside the cell!

rulerulerule

<k> X = V; y K </k><env> X |-> _ </env>=><k> K </k><env> X |-> V </env>

rule <T><k> (X = V; => .) y K </k><env> X |-> (_ => V)</env>

</T>

Page 47: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Abstractions

Local rewrites: put the rewrite inside the cell!

rulerulerule

<k> X = V; y K </k><env> X |-> _ </env>=><k> K </k><env> X |-> V </env>

rule <T><k> (X = V; => .) y K </k><env> X |-> (_ => V)</env>

</T>

Page 48: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Abstractions

Local rewrites: put the rewrite inside the cell!

rulerulerule

<k> X = V; y K </k><env> X |-> _ </env>=><k> K </k><env> X |-> V </env>

rule <T><k> (X = V; => .) y K </k><env> X |-> (_ => V)</env>

</T>

Page 49: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Abstractions

Local rewrites: put the rewrite inside the cell!

rulerulerule

<k> X = V; y K </k><env> X |-> _ </env>=><k> K </k><env> X |-> V </env>

rule <T><k> (X = V; => .) ...</k><env> X |-> (_ => V)</env>

</T>

Page 50: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Heating and cooling rules

I Recall:rule <T>

<k> (X = V; => .) ...</k><env> X |-> (_ => V)</env>

</T>

I This rules works fine when V is a result!I Example: x = 2 + 2;

I If V is not a value (e.g., 2 + 2) then we evaluate it! How?I Heating and cooling rules:

I syntax Stmt ::= Id "=" Exp ";" [strict(2)]I Heating: 2 + 2 y x = �;I Compute result: 4 y x = �;I Cooling: x = 4;I Now we can apply the rule!

Page 51: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Heating and cooling rules

I Recall:rule <T>

<k> (X = V; => .) ...</k><env> X |-> (_ => V)</env>

</T>

I This rules works fine when V is a result!I Example: x = 2 + 2;

I If V is not a value (e.g., 2 + 2) then we evaluate it! How?I Heating and cooling rules:

I syntax Stmt ::= Id "=" Exp ";" [strict(2)]I Heating: 2 + 2 y x = �;I Compute result: 4 y x = �;I Cooling: x = 4;I Now we can apply the rule!

Page 52: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Heating and cooling rules

I Recall:rule <T>

<k> (X = V; => .) ...</k><env> X |-> (_ => V)</env>

</T>

I This rules works fine when V is a result!I Example: x = 2 + 2;

I If V is not a value (e.g., 2 + 2) then we evaluate it! How?I Heating and cooling rules:

I syntax Stmt ::= Id "=" Exp ";" [strict(2)]I Heating: 2 + 2 y x = �;I Compute result: 4 y x = �;I Cooling: x = 4;I Now we can apply the rule!

Page 53: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Heating and cooling rules

I Recall:rule <T>

<k> (X = V; => .) ...</k><env> X |-> (_ => V)</env>

</T>

I This rules works fine when V is a result!I Example: x = 2 + 2;

I If V is not a value (e.g., 2 + 2) then we evaluate it! How?I Heating and cooling rules:

I syntax Stmt ::= Id "=" Exp ";" [strict(2)]I Heating: 2 + 2 y x = �;I Compute result: 4 y x = �;I Cooling: x = 4;I Now we can apply the rule!

Page 54: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Heating and cooling rules

I Recall:rule <T>

<k> (X = V; => .) ...</k><env> X |-> (_ => V)</env>

</T>

I This rules works fine when V is a result!I Example: x = 2 + 2;

I If V is not a value (e.g., 2 + 2) then we evaluate it! How?I Heating and cooling rules:

I syntax Stmt ::= Id "=" Exp ";" [strict(2)]I Heating: 2 + 2 y x = �;I Compute result: 4 y x = �;I Cooling: x = 4;I Now we can apply the rule!

Page 55: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Heating and cooling rules

I Recall:rule <T>

<k> (X = V; => .) ...</k><env> X |-> (_ => V)</env>

</T>

I This rules works fine when V is a result!I Example: x = 2 + 2;

I If V is not a value (e.g., 2 + 2) then we evaluate it! How?I Heating and cooling rules:

I syntax Stmt ::= Id "=" Exp ";" [strict(2)]I Heating: 2 + 2 y x = �;I Compute result: 4 y x = �;I Cooling: x = 4;I Now we can apply the rule!

Page 56: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Heating and cooling rules

I Recall:rule <T>

<k> (X = V; => .) ...</k><env> X |-> (_ => V)</env>

</T>

I This rules works fine when V is a result!I Example: x = 2 + 2;

I If V is not a value (e.g., 2 + 2) then we evaluate it! How?I Heating and cooling rules:

I syntax Stmt ::= Id "=" Exp ";" [strict(2)]I Heating: 2 + 2 y x = �;I Compute result: 4 y x = �;I Cooling: x = 4;I Now we can apply the rule!

Page 57: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Heating and cooling rules

I Recall:rule <T>

<k> (X = V; => .) ...</k><env> X |-> (_ => V)</env>

</T>

I This rules works fine when V is a result!I Example: x = 2 + 2;

I If V is not a value (e.g., 2 + 2) then we evaluate it! How?I Heating and cooling rules:

I syntax Stmt ::= Id "=" Exp ";" [strict(2)]I Heating: 2 + 2 y x = �;I Compute result: 4 y x = �;I Cooling: x = 4;I Now we can apply the rule!

Page 58: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Heating and cooling rules

I Recall:rule <T>

<k> (X = V; => .) ...</k><env> X |-> (_ => V)</env>

</T>

I This rules works fine when V is a result!I Example: x = 2 + 2;

I If V is not a value (e.g., 2 + 2) then we evaluate it! How?I Heating and cooling rules:

I syntax Stmt ::= Id "=" Exp ";" [strict(2)]I Heating: 2 + 2 y x = �;I Compute result: 4 y x = �;I Cooling: x = 4;I Now we can apply the rule!

Page 59: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

IMP

I We will define a simple imperative language in KI Features:

I Arithmetic and boolean expressionsI Statements: assignments, decisional statement, loops,

blocks, sequencesI DEMO

Page 60: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

IMP

I We will define a simple imperative language in KI Features:

I Arithmetic and boolean expressionsI Statements: assignments, decisional statement, loops,

blocks, sequencesI DEMO

Page 61: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Lab - this week

I Extend IMP with various features

Page 62: Principles of Programming Languages Lecture 3: Semantics · Semantic - informal Informal semantics (examples):natural language Rationale for the ANSI C Programming Language: I“Trust

Bibliography

I Sections 2.5 and Chapter 6 from the [Gabbrielli&Martini2010].