Top Banner
Principles of Programming Languages Lecture 5: Semantics Andrei Arusoaie 1 1 Department of Computer Science October 26, 2021
64

Principles of Programming Languages Lecture 5: Semantics

Feb 15, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Principles of Programming Languages Lecture 5: Semantics

Principles of Programming LanguagesLecture 5: Semantics

Andrei Arusoaie1

1Department of Computer Science

October 26, 2021

Page 2: Principles of Programming Languages Lecture 5: Semantics

Outline

Semantics: introduction

An evaluator for IMP

Page 3: Principles of Programming Languages Lecture 5: Semantics

Semantics: motivation

C

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

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

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 5: Semantics

Semantics: motivation

C

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

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

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 5: Semantics

Semantics: motivation

C

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

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

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 5: Semantics

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: Apple clang 13.0.0

-$ 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 5: Semantics

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: Apple clang 13.0.0

-$ 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 5: Semantics

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: Apple clang 13.0.0

-$ 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 5: Semantics

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: Apple clang 13.0.0

-$ 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 5: Semantics

Semantics

I Semantics is concerned with the meaning of languageconstructs

I Semantics must be unambiguousI Semantics must be flexible

Page 11: Principles of Programming Languages Lecture 5: Semantics

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 12: Principles of Programming Languages Lecture 5: Semantics

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 5: Semantics

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 5: Semantics

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 5: Semantics

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 5: Semantics

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 5: Semantics

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 5: Semantics

Formal Semantics

Some (formal) semantics styles:I operationalI denotationalI axiomatic

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

Page 19: Principles of Programming Languages Lecture 5: Semantics

Formal Semantics

Some (formal) semantics styles:I operationalI denotationalI axiomatic

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

Page 20: Principles of Programming Languages Lecture 5: Semantics

A simple imperative language

Program:

n ::= 10;;i ::= 1;;sum ::= 0;;while (i «= n) do

sum ::= sum +’ i;;i ::= i +’ 1

end

Features:I variablesI arithmetic expressionsI boolean expressionsI assignment statementsI loop statementsI decisional statements

How can we define the semantics of this language?

Page 21: Principles of Programming Languages Lecture 5: Semantics

A simple imperative language

Program:

n ::= 10;;i ::= 1;;sum ::= 0;;while (i «= n) do

sum ::= sum +’ i;;i ::= i +’ 1

end

Features:I variablesI arithmetic expressionsI boolean expressionsI assignment statementsI loop statementsI decisional statements

How can we define the semantics of this language?

Page 22: Principles of Programming Languages Lecture 5: Semantics

A simple imperative language

Program:

n ::= 10;;i ::= 1;;sum ::= 0;;while (i «= n) do

sum ::= sum +’ i;;i ::= i +’ 1

end

Features:I variablesI arithmetic expressionsI boolean expressionsI assignment statementsI loop statementsI decisional statements

How can we define the semantics of this language?

Page 23: Principles of Programming Languages Lecture 5: Semantics

A simple imperative language

Program:

n ::= 10;;i ::= 1;;sum ::= 0;;while (i «= n) do

sum ::= sum +’ i;;i ::= i +’ 1

end

Features:I variablesI arithmetic expressionsI boolean expressionsI assignment statementsI loop statementsI decisional statements

How can we define the semantics of this language?

Page 24: Principles of Programming Languages Lecture 5: Semantics

A simple imperative language

Program:

n ::= 10;;i ::= 1;;sum ::= 0;;while (i «= n) do

sum ::= sum +’ i;;i ::= i +’ 1

end

Features:I variablesI arithmetic expressionsI boolean expressionsI assignment statementsI loop statementsI decisional statements

How can we define the semantics of this language?

Page 25: Principles of Programming Languages Lecture 5: Semantics

A simple imperative language

Program:

n ::= 10;;i ::= 1;;sum ::= 0;;while (i «= n) do

sum ::= sum +’ i;;i ::= i +’ 1

end

Features:I variablesI arithmetic expressionsI boolean expressionsI assignment statementsI loop statementsI decisional statements

How can we define the semantics of this language?

Page 26: Principles of Programming Languages Lecture 5: Semantics

A simple imperative language

Program:

n ::= 10;;i ::= 1;;sum ::= 0;;while (i «= n) do

sum ::= sum +’ i;;i ::= i +’ 1

end

Features:I variablesI arithmetic expressionsI boolean expressionsI assignment statementsI loop statementsI decisional statements

How can we define the semantics of this language?

Page 27: Principles of Programming Languages Lecture 5: Semantics

A simple imperative language

Program:

n ::= 10;;i ::= 1;;sum ::= 0;;while (i «= n) do

sum ::= sum +’ i;;i ::= i +’ 1

end

Features:I variablesI arithmetic expressionsI boolean expressionsI assignment statementsI loop statementsI decisional statements

How can we define the semantics of this language?

Page 28: Principles of Programming Languages Lecture 5: Semantics

A simple imperative language

Program:

n ::= 10;;i ::= 1;;sum ::= 0;;while (i «= n) do

sum ::= sum +’ i;;i ::= i +’ 1

end

Features:I variablesI arithmetic expressionsI boolean expressionsI assignment statementsI loop statementsI decisional statements

How can we define the semantics of this language?

Page 29: Principles of Programming Languages Lecture 5: Semantics

variables

I variable = storage location at address + symbolic nameI variables can be accessed or modifiedI depending on the language, they may only be able to store

a specified datatype (e.g., integer, string, etc.)I Scope: global, localI Lifetime (extent)I var_name 7→ value

I Environment + memory

Page 30: Principles of Programming Languages Lecture 5: Semantics

variables

I variable = storage location at address + symbolic nameI variables can be accessed or modifiedI depending on the language, they may only be able to store

a specified datatype (e.g., integer, string, etc.)I Scope: global, localI Lifetime (extent)I var_name 7→ value

I Environment + memory

Page 31: Principles of Programming Languages Lecture 5: Semantics

variables

I variable = storage location at address + symbolic nameI variables can be accessed or modifiedI depending on the language, they may only be able to store

a specified datatype (e.g., integer, string, etc.)I Scope: global, localI Lifetime (extent)I var_name 7→ value

I Environment + memory

Page 32: Principles of Programming Languages Lecture 5: Semantics

variables

I variable = storage location at address + symbolic nameI variables can be accessed or modifiedI depending on the language, they may only be able to store

a specified datatype (e.g., integer, string, etc.)I Scope: global, localI Lifetime (extent)I var_name 7→ value

I Environment + memory

Page 33: Principles of Programming Languages Lecture 5: Semantics

variables

I variable = storage location at address + symbolic nameI variables can be accessed or modifiedI depending on the language, they may only be able to store

a specified datatype (e.g., integer, string, etc.)I Scope: global, localI Lifetime (extent)I var_name 7→ value

I Environment + memory

Page 34: Principles of Programming Languages Lecture 5: Semantics

variables

I variable = storage location at address + symbolic nameI variables can be accessed or modifiedI depending on the language, they may only be able to store

a specified datatype (e.g., integer, string, etc.)I Scope: global, localI Lifetime (extent)I var_name 7→ value

I Environment + memory

Page 35: Principles of Programming Languages Lecture 5: Semantics

variables

I variable = storage location at address + symbolic nameI variables can be accessed or modifiedI depending on the language, they may only be able to store

a specified datatype (e.g., integer, string, etc.)I Scope: global, localI Lifetime (extent)I var_name 7→ value

I Environment + memory

Page 36: Principles of Programming Languages Lecture 5: Semantics

Environment

I Environment: variables mapped to valuesDefinition Env := string -> nat.Definition env (string : string) :=if (string_dec string "n")

then 10else 0.

Compute (env n).= 10: natCompute (env i).= 0: natCheck env.env

: string -> nat

I The environment is a function!

Page 37: Principles of Programming Languages Lecture 5: Semantics

Environment update

I Update = a new function s.t. the value for x is updated to v:

Definition update (x : string)(v : nat)(env : Env) : Env :=fun y => if (string_dec x y)

then velse env y.

Page 38: Principles of Programming Languages Lecture 5: Semantics

Arithmetic expressions

I Syntax:

BNF:AExp ::= N

| AExp ‘+’ AExp| AExp ‘*’ AExp

Inductive AExp :=| anum : nat -> AExp| aplus : AExp -> AExp -> AExp| amul : AExp -> AExp -> AExp.

I Semantics:

Fixpoint aeval (a : AExp) : nat :=match a with| anum v => v| aplus a1 a2 => (aeval a1) + (aeval a2)| amul a1 a2 => (aeval a1) * (aeval a2)end.

I What about expressions in a PL: i ::= i +’ 1;; ?

Page 39: Principles of Programming Languages Lecture 5: Semantics

Arithmetic expressions

I Syntax:

BNF:AExp ::= N

| AExp ‘+’ AExp| AExp ‘*’ AExp

Inductive AExp :=| anum : nat -> AExp| aplus : AExp -> AExp -> AExp| amul : AExp -> AExp -> AExp.

I Semantics:

Fixpoint aeval (a : AExp) : nat :=match a with| anum v => v| aplus a1 a2 => (aeval a1) + (aeval a2)| amul a1 a2 => (aeval a1) * (aeval a2)end.

I What about expressions in a PL: i ::= i +’ 1;; ?

Page 40: Principles of Programming Languages Lecture 5: Semantics

Arithmetic expressions

I Syntax:

BNF:AExp ::= N

| AExp ‘+’ AExp| AExp ‘*’ AExp

Inductive AExp :=| anum : nat -> AExp| aplus : AExp -> AExp -> AExp| amul : AExp -> AExp -> AExp.

I Semantics:

Fixpoint aeval (a : AExp) : nat :=match a with| anum v => v| aplus a1 a2 => (aeval a1) + (aeval a2)| amul a1 a2 => (aeval a1) * (aeval a2)end.

I What about expressions in a PL: i ::= i +’ 1;; ?

Page 41: Principles of Programming Languages Lecture 5: Semantics

Arithmetic expressions

I Syntax:

BNF:AExp ::= N

| AExp ‘+’ AExp| AExp ‘*’ AExp

Inductive AExp :=| anum : nat -> AExp| aplus : AExp -> AExp -> AExp| amul : AExp -> AExp -> AExp.

I Semantics:

Fixpoint aeval (a : AExp) : nat :=match a with| anum v => v| aplus a1 a2 => (aeval a1) + (aeval a2)| amul a1 a2 => (aeval a1) * (aeval a2)end.

I What about expressions in a PL: i ::= i +’ 1;; ?

Page 42: Principles of Programming Languages Lecture 5: Semantics

Arithmetic expressions with variables

I Syntax:

BNF:AExp ::= string

| N| AExp ‘+’ AExp| AExp ‘*’ AExp

Inductive AExp :=| var : string -> AExp| anum : nat -> AExp| aplus : AExp -> AExp -> AExp| amul : AExp -> AExp -> AExp.

I Semantics:

Fixpoint aeval (a : AExp) : nat :=match a with| var v => ???| anum v => v| aplus a1 a2 => (aeval a1) + (aeval a2)| amul a1 a2 => (aeval a1) * (aeval a2)end.

I We need an environment to evaluate expressions withvariables!

Page 43: Principles of Programming Languages Lecture 5: Semantics

Arithmetic expressions with variables

I Syntax:

BNF:AExp ::= string

| N| AExp ‘+’ AExp| AExp ‘*’ AExp

Inductive AExp :=| var : string -> AExp| anum : nat -> AExp| aplus : AExp -> AExp -> AExp| amul : AExp -> AExp -> AExp.

I Semantics:

Fixpoint aeval (a : AExp) : nat :=match a with| var v => ???| anum v => v| aplus a1 a2 => (aeval a1) + (aeval a2)| amul a1 a2 => (aeval a1) * (aeval a2)end.

I We need an environment to evaluate expressions withvariables!

Page 44: Principles of Programming Languages Lecture 5: Semantics

Arithmetic expressions with variables

I Syntax:

BNF:AExp ::= string

| N| AExp ‘+’ AExp| AExp ‘*’ AExp

Inductive AExp :=| var : string -> AExp| anum : nat -> AExp| aplus : AExp -> AExp -> AExp| amul : AExp -> AExp -> AExp.

I Semantics:

Fixpoint aeval (a : AExp) : nat :=match a with| var v => ???| anum v => v| aplus a1 a2 => (aeval a1) + (aeval a2)| amul a1 a2 => (aeval a1) * (aeval a2)end.

I We need an environment to evaluate expressions withvariables!

Page 45: Principles of Programming Languages Lecture 5: Semantics

Arithmetic expressions with variables

I Syntax:

BNF:AExp ::= string

| N| AExp ‘+’ AExp| AExp ‘*’ AExp

Inductive AExp :=| var : string -> AExp| anum : nat -> AExp| aplus : AExp -> AExp -> AExp| amul : AExp -> AExp -> AExp.

I Semantics:

Fixpoint aeval (a : AExp) : nat :=match a with| var v => ???| anum v => v| aplus a1 a2 => (aeval a1) + (aeval a2)| amul a1 a2 => (aeval a1) * (aeval a2)end.

I We need an environment to evaluate expressions withvariables!

Page 46: Principles of Programming Languages Lecture 5: Semantics

Evaluate expression with variablesI Semantics:

Fixpoint aeval (a : AExp) (env : Env) : nat :=match a with| var v => env v| anum v => v| aplus a1 a2 => (aeval a1 env) + (aeval a2 env)| amul a1 a2 => (aeval a1 env) * (aeval a2 env)end.

I Notations and testing:Coercion var : string >-> AExp.Coercion anum : nat >-> AExp.Notation "A +’ B" := (aplus A B) (at level 49).Notation "A *’ B" := (amul A B) (at level 48).Compute aeval (2 +’ 3 *’ 4) env.= 14: natCompute env n.= 10: natCompute aeval (2 +’ 3 *’ n) env.= 32: nat

Page 47: Principles of Programming Languages Lecture 5: Semantics

Boolean expressions

I Syntax:Inductive BExp :=| btrue : BExp| bfalse : BExp| blessthan : AExp -> AExp -> BExp| band : BExp -> BExp -> BExp| bnot : BExp -> BExp.

I Semantics:Fixpoint beval (b : BExp) (env : Env) : bool :=match b with| btrue => true| bfalse => false| blessthan a1 a2 => Nat.leb (aeval a1 env) (aeval a2 env)| band b1 b2 => andb (beval b1 env) (beval b2 env)| bnot b’ => negb (beval b’ env)end.

Page 48: Principles of Programming Languages Lecture 5: Semantics

Boolean expressions

I Syntax:Inductive BExp :=| btrue : BExp| bfalse : BExp| blessthan : AExp -> AExp -> BExp| band : BExp -> BExp -> BExp| bnot : BExp -> BExp.

I Semantics:Fixpoint beval (b : BExp) (env : Env) : bool :=match b with| btrue => true| bfalse => false| blessthan a1 a2 => Nat.leb (aeval a1 env) (aeval a2 env)| band b1 b2 => andb (beval b1 env) (beval b2 env)| bnot b’ => negb (beval b’ env)end.

Page 49: Principles of Programming Languages Lecture 5: Semantics

Assignments

I Syntax:Inductive Stmt :=| assignment : string -> AExp -> Stmt.Notation "A ::= B" := (assignment A B) (at level 54).Check n ::= 100 .n ::= 100

: Stmt

I Assignments modify the environmentI Semantics:

Fixpoint eval (s : Stmt) (env : Env) : Env :=match s with| assignment x a => update x (aeval a env) envend.

I (eval (n ::= 100) env) is of type Env!

Page 50: Principles of Programming Languages Lecture 5: Semantics

Assignments

I Syntax:Inductive Stmt :=| assignment : string -> AExp -> Stmt.Notation "A ::= B" := (assignment A B) (at level 54).Check n ::= 100 .n ::= 100

: Stmt

I Assignments modify the environmentI Semantics:

Fixpoint eval (s : Stmt) (env : Env) : Env :=match s with| assignment x a => update x (aeval a env) envend.

I (eval (n ::= 100) env) is of type Env!

Page 51: Principles of Programming Languages Lecture 5: Semantics

Assignments

I Syntax:Inductive Stmt :=| assignment : string -> AExp -> Stmt.Notation "A ::= B" := (assignment A B) (at level 54).Check n ::= 100 .n ::= 100

: Stmt

I Assignments modify the environmentI Semantics:

Fixpoint eval (s : Stmt) (env : Env) : Env :=match s with| assignment x a => update x (aeval a env) envend.

I (eval (n ::= 100) env) is of type Env!

Page 52: Principles of Programming Languages Lecture 5: Semantics

Assignments

I Syntax:Inductive Stmt :=| assignment : string -> AExp -> Stmt.Notation "A ::= B" := (assignment A B) (at level 54).Check n ::= 100 .n ::= 100

: Stmt

I Assignments modify the environmentI Semantics:

Fixpoint eval (s : Stmt) (env : Env) : Env :=match s with| assignment x a => update x (aeval a env) envend.

I (eval (n ::= 100) env) is of type Env!

Page 53: Principles of Programming Languages Lecture 5: Semantics

Assignments

I Syntax:Inductive Stmt :=| assignment : string -> AExp -> Stmt.Notation "A ::= B" := (assignment A B) (at level 54).Check n ::= 100 .n ::= 100

: Stmt

I Assignments modify the environmentI Semantics:

Fixpoint eval (s : Stmt) (env : Env) : Env :=match s with| assignment x a => update x (aeval a env) envend.

I (eval (n ::= 100) env) is of type Env!

Page 54: Principles of Programming Languages Lecture 5: Semantics

Programs: sequence of statements

I Syntax:Inductive Stmt :=...| seq s1 s2 => eval s2 (eval s1 env)...Notation "S S’" := (assignment S S’) (at level 54).Check n ::= 100 ;; i ::= 7 .n ::= 100 ;; i ::= 7

: Stmt

I Semantics:Fixpoint eval (s : Stmt) (env : Env) : Env :=match s with...| seq s1 s2 => eval s2 (eval s1 env)end.

Page 55: Principles of Programming Languages Lecture 5: Semantics

Programs: sequence of statements

I Syntax:Inductive Stmt :=...| seq s1 s2 => eval s2 (eval s1 env)...Notation "S S’" := (assignment S S’) (at level 54).Check n ::= 100 ;; i ::= 7 .n ::= 100 ;; i ::= 7

: Stmt

I Semantics:Fixpoint eval (s : Stmt) (env : Env) : Env :=match s with...| seq s1 s2 => eval s2 (eval s1 env)end.

Page 56: Principles of Programming Languages Lecture 5: Semantics

Programs: sequence of statements

I Syntax:Inductive Stmt :=...| seq s1 s2 => eval s2 (eval s1 env)...Notation "S S’" := (assignment S S’) (at level 54).Check n ::= 100 ;; i ::= 7 .n ::= 100 ;; i ::= 7

: Stmt

I Semantics:Fixpoint eval (s : Stmt) (env : Env) : Env :=match s with...| seq s1 s2 => eval s2 (eval s1 env)end.

Page 57: Principles of Programming Languages Lecture 5: Semantics

Loops

I Syntax:Inductive Stmt :=...| while : BExp -> Stmt -> Stmt....

I Semantics:Fixpoint eval (s : Stmt) (env : Env) : Env :=match s with...| while b s’ => if (beval b env)

then (eval (seq s’ (while b s’)) env)else env

end.

I ERROR:Error: Cannot guess decreasing argument of fix.

I Solution: define eval as a relation!

Page 58: Principles of Programming Languages Lecture 5: Semantics

Loops

I Syntax:Inductive Stmt :=...| while : BExp -> Stmt -> Stmt....

I Semantics:Fixpoint eval (s : Stmt) (env : Env) : Env :=match s with...| while b s’ => if (beval b env)

then (eval (seq s’ (while b s’)) env)else env

end.

I ERROR:Error: Cannot guess decreasing argument of fix.

I Solution: define eval as a relation!

Page 59: Principles of Programming Languages Lecture 5: Semantics

Loops

I Syntax:Inductive Stmt :=...| while : BExp -> Stmt -> Stmt....

I Semantics:Fixpoint eval (s : Stmt) (env : Env) : Env :=match s with...| while b s’ => if (beval b env)

then (eval (seq s’ (while b s’)) env)else env

end.

I ERROR:Error: Cannot guess decreasing argument of fix.

I Solution: define eval as a relation!

Page 60: Principles of Programming Languages Lecture 5: Semantics

Evaluation as a relation

I Assignment:aeval a E = v

(eval (x ::= a) E (x 7→ v ; E)

I Sequence:eval s1 E1 E′ eval s2 E′ E2

(eval (seq s1 s2) E1 E2)

I Loop (true case):

beval b E1 = true eval s E1 E′ eval (while b s) E′ E2(eval (while b s) E1 E2)

I Loop (false case):beval b E = false

(eval (while b s) E E)

Page 61: Principles of Programming Languages Lecture 5: Semantics

Evaluation as a relation

I Assignment:aeval a E = v

(eval (x ::= a) E (x 7→ v ; E)

I Sequence:eval s1 E1 E′ eval s2 E′ E2

(eval (seq s1 s2) E1 E2)

I Loop (true case):

beval b E1 = true eval s E1 E′ eval (while b s) E′ E2(eval (while b s) E1 E2)

I Loop (false case):beval b E = false

(eval (while b s) E E)

Page 62: Principles of Programming Languages Lecture 5: Semantics

Evaluation as a relation

I Assignment:aeval a E = v

(eval (x ::= a) E (x 7→ v ; E)

I Sequence:eval s1 E1 E′ eval s2 E′ E2

(eval (seq s1 s2) E1 E2)

I Loop (true case):

beval b E1 = true eval s E1 E′ eval (while b s) E′ E2(eval (while b s) E1 E2)

I Loop (false case):beval b E = false

(eval (while b s) E E)

Page 63: Principles of Programming Languages Lecture 5: Semantics

Evaluation as a relation

I Assignment:aeval a E = v

(eval (x ::= a) E (x 7→ v ; E)

I Sequence:eval s1 E1 E′ eval s2 E′ E2

(eval (seq s1 s2) E1 E2)

I Loop (true case):

beval b E1 = true eval s E1 E′ eval (while b s) E′ E2(eval (while b s) E1 E2)

I Loop (false case):beval b E = false

(eval (while b s) E E)

Page 64: Principles of Programming Languages Lecture 5: Semantics

Bibliography

I Chapter Simple Imperative Programs in SoftwareFoundations - Volume 1, Benjamin C. Pierce, ArthurAzevedo de Amorim, Chris Casinghino, Marco Gaboardi,Michael Greenberg, Catalin Hritcu, Vilhelm Sjöberg,Andrew Tolmach, Brent Yorgeyhttps://softwarefoundations.cis.upenn.edu/lf-current/Imp.html