principles of programming languages lecture 3: semantics · semantic - informal informal semantics...
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/1.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/2.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/3.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/4.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/5.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/6.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/7.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/8.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/9.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/10.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/11.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/13.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/14.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/15.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/16.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/17.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/18.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/19.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/20.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/21.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/22.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/23.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/24.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/25.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/26.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/27.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/28.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/29.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/30.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/31.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/32.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/33.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/34.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/35.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/36.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/37.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/38.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/39.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/40.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/41.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/42.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/43.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/44.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/45.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/46.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/47.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/48.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/49.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/50.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/51.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/52.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/53.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/54.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/55.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/56.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/57.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/58.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/59.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/60.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/61.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022050119/5f4f493d2afa395c63033d87/html5/thumbnails/62.jpg)
Bibliography
I Sections 2.5 and Chapter 6 from the [Gabbrielli&Martini2010].