1 of 16

Semantics (Final Pieces)

2 of 16

Closures (in practice)

3 of 16

IMP vs JS

  • IMP:
    • Blocks introduce scopes
    • Var declaration adds variable to the current scope when executed
    • Any reachable scope can be modified
  • JS
    • Scopes only introduced for function blocks (others are merely cosmetic)
    • Var declarations add variables to the current scope if they are present in the function regardless of if or when they execute
    • Any reachable scope can be modified
    • If a variable is in a scope but is not initialized, its value defaults to undefined.

4 of 16

Add Closures: Grammar

 

 

 

 

(create closure)

(call closure)

5 of 16

Block scope in IMP

 

 

 

 

6 of 16

Block scope in JS

 

 

 

 

7 of 16

IMP vs. MITScript vs. JS vs. Python

  • IMP:
    • Blocks introduce scopes
    • Var declaration adds variable to the current scope when executed
    • Any reachable scope can be modified
  • JS
    • Blocks do not introduce scope, they are merely cosmetic
    • Var declarations add variables to the current scope if they are present in the function regardless of if or when they execute
    • Any reachable scope can be modified
    • If a variable is in a scope but is not initialized, its value defaults to undefined.
  • Python
    • There are no blocks
    • There are no var declarations; an assignment introduces a variable to the scope, even if assignment is not executed
    • Only the current scope and the global scope can be modified
    • If a variable is in scope but not initialized, access produces an error (NameError)
  • MITScript
    • Python Semantics + Blocks do not introduce scope, they are merely cosmetic

8 of 16

Examples

Python:

x = 7

def g():

x = 8

def h():

print(x)

x = x + 1

h()

h()

return h

print(x)

hh=g()

hh()

print(x)

print(x) is an error. The assignment in the scope adds automatically adds x to the scope

9 of 16

Records

10 of 16

Objects for Free

var Point = fun(x, y) {

var this = {

x : x;

y : y;

print : fun(){

log("Point(" + this.x + ", " + this.y + ")");

};

};

return this;

};

var p = Point(5,5);

p.print();

11 of 16

Add Records: Grammar

 

 

 

 

(update record)

 

(create record)

12 of 16

Records

  • Record constructor
    • Design decision: how to represent the record contents?

var a = { x : 1; y : 2};

var b = a;

b.x = 3;

print(a.x)

What’s the output?

13 of 16

Objects for Free

var Point = fun(x, y) {

var this = {

x : x;

y : y;

print : fun(){

log("Point(" + this.x + ", " + this.y + ")");

};

};

return this;

};

var p = Point(5,5);

p.print();

14 of 16

Records

  • Record constructor
    • Design decision: how to represent the record contents?

 

 

 

 

 

 

 

 

 

15 of 16

Objects for Free

var Point = fun(x, y) {

var this = {

x : x;

y : y;

print : fun(){

log("Point(" + this.x + ", " + this.y + ")");

};

};

return this;

};

var p = Point(5,5);

p.print();

16 of 16

We just built a whole programming language!

  • Start small with a core calculus
    • Lambda calculus: Functional Programming(Church, 1930)
    • IMP: Imperative Programming (Winskel, 1993)
    • Java: Featherweight Java (Igarashi, Pierce, Wadler, 2002)
    • Javascript (“An Operational Semantics for JavaScript”, Maffeis, Mitchell, Taly, 2008)
    • Rust: Rustbelt (Jung, Jourdan, Krebbers, Dreyer, 2018) Appendix
  • Add only necessary features, as orthogonally as possible
  • Semantics to Interpreters
    • By hand. Recursive interpreter
    • Automatically (like a parser generator!): https://kframework.org