Modules, Structs, Hashes, and Operational Semantics Prof. Tom - - PowerPoint PPT Presentation

modules structs hashes and operational semantics
SMART_READER_LITE
LIVE PREVIEW

Modules, Structs, Hashes, and Operational Semantics Prof. Tom - - PowerPoint PPT Presentation

CS 152: Programming Language Paradigms Modules, Structs, Hashes, and Operational Semantics Prof. Tom Austin San Jos State University Modules Review Modules from HW 1 (in-class) How do we organize code in Java? Packages provide units


slide-1
SLIDE 1

CS 152: Programming Language Paradigms

  • Prof. Tom Austin

San José State University

Modules, Structs, Hashes, and Operational Semantics

slide-2
SLIDE 2

Modules

slide-3
SLIDE 3

Review Modules from HW 1

(in-class)

slide-4
SLIDE 4

How do we organize code in Java?

  • Packages provide units
  • f code
  • Keywords specify

method access

slide-5
SLIDE 5

In Java, keywords specify access

  • public
  • protected
  • no keyword (package access)
  • private
slide-6
SLIDE 6

Java method access levels

  • public: everyone
  • protected: subclasses and

code in the same package

  • no keyword (package access):

code in the same package

  • private: only code in the

current class

slide-7
SLIDE 7

Organizing Code in Racket

Racket organizes code in terms of imports and exports.

  • The library specifies which code is

available to others by the provide keyword.

–anything not named by provide is hidden.

  • If you want to use the library, you

use the keyword require.

slide-8
SLIDE 8

Organizing Code in Racket

  • Exports specify public values:

(provide big-add)

  • Imports specify code dependencies:

(require "big-num.rkt")

slide-9
SLIDE 9

Structs and Hashes

slide-10
SLIDE 10

Structs

  • Structures allow us to create more

sophisticated data structures:

(struct name (field1 field2 … ))

  • Once we have a structure, we can

destructure it with the match keyword to get at the contents.

  • <Example in class>
slide-11
SLIDE 11

Hashes

  • Hashes are maps of key/value

pairs.

  • Unlike Java, hashes are

immutable.

  • <example in class>
slide-12
SLIDE 12

Formal Semantics

slide-13
SLIDE 13

Why do we need formal semantics?

slide-14
SLIDE 14

Everyone knows what an if statement does, right? if true then x = 1 else x = 0

At the end of this code snippet, the value of x will be 1

slide-15
SLIDE 15

Everyone knows what an if statement does, right? if false then x = 1 else x = 0

At the end of this code snippet, the value of x will be 0

slide-16
SLIDE 16

Everyone knows what an if statement does, right? if 0 then x = 1 else x = 0

Will x be set to 0, like in C/C++? Will x be set to 1, like in Ruby? Or will it be an error, like in Java?

slide-17
SLIDE 17

Everyone knows what an if statement does, right? x = if true then 1 else 0

Is assignment valid or an error?

slide-18
SLIDE 18

Formal semantics define how a language works concisely and with minimal ambiguity.

slide-19
SLIDE 19

A Review of Compilers

Lexer/ Tokenizer Parser

source code tokens

Abstract Syntax Tree (AST) Compiler

Machine code

Interpreter

Commands

We don't care about lexing or parsing. We don't care if we have a compiler or interpreter

slide-20
SLIDE 20

A Review of Compilers

Lexer/ Tokenizer Parser

source code tokens

Compiler

Machine code

Interpreter

Commands

We don't care about lexing or parsing. We don't care if we have a compiler or interpreter

Abstract Syntax Tree (AST)

slide-21
SLIDE 21

Abstract Syntax Tree (AST)

ASTs are the key to understanding a language

slide-22
SLIDE 22

Bool* Language

e ::= true | false | if e then e else e expressions: constant true constant false conditional

Despite appearances, these are really ASTs

slide-23
SLIDE 23

Values in Bool*

v ::= true | false values: constant true constant false

slide-24
SLIDE 24

Formal Semantic Styles

  • Operational semantics

–Big-step (or "natural") –Small-step (or "structural")

  • Axiomatic semantics
  • Denotational semantics
slide-25
SLIDE 25

Formal Semantic Styles

  • Operational semantics

–Big-step (or "natural") –Small-step (or "structural")

  • Axiomatic semantics
  • Denotational semantics
slide-26
SLIDE 26

Operational semantics specify how expressions should be evaluated. There are two different approaches. Small-step semantics evaluate an expression until it is in normal form & cannot be evaluated any further.

slide-27
SLIDE 27

In contrast, big-step operational semantics evaluate every expression to a value. Big-step rules tend to have a recursive structure.

slide-28
SLIDE 28

Big-Step Evaluation Relation

e v

An expression e … … evaluates to … … a value v.

slide-29
SLIDE 29

Big-Step Evaluation Relation

e v

Preconditions

limits when the rule applies

slide-30
SLIDE 30

Big-step semantics for Bool*

e1 ⇓ true e2 ⇓ v if e1 then e2 else e3 ⇓ v

B-IfTrue

e1 ⇓ false e3 ⇓ v if e1 then e2 else e3 ⇓ v

B-IfFalse

v ⇓ v

B-Value

slide-31
SLIDE 31

Bool* big-step example

if (if true then false else true) then true else false

if true then false else true ⇓ false

true ⇓ true false ⇓ false

false ⇓ false

⇓ false

slide-32
SLIDE 32

Converting our rules into code

(in-class)

slide-33
SLIDE 33

Bool* extension: numbers Users demand a new feature – numbers! We will add 3 new features:

  • Numbers, represented by n
  • succ, which takes a number and returns

the next highest number.

  • pred, which takes a number and returns

the next lowest number.

slide-34
SLIDE 34

Extended Bool* Language

e ::= true | false | if e then e else e | n | succ e | pred e

Let's extend our semantics to handle these new language constructs

slide-35
SLIDE 35

Lab: Write a Bool* Interpreter

  • Starter code is available on the

course website

  • Extend Bool* with numbers,

succ, and pred

slide-36
SLIDE 36

Pop Quiz: Write operational semantics

e ::= e and e | e or e | not e | true | false v ::= true | false

e ⇓ v

slide-37
SLIDE 37

Adding State to Semantics

slide-38
SLIDE 38

SpartanLang

e ::= !x | v | x:=e | e;e | e op e | if e then e else e end | while e do e end dereferencing values assignment sequence binary operations conditionals while loops

slide-39
SLIDE 39

SpartanLang (continued)

v ::= i | b

  • p ::= + | -

| \ | * | < | > | <= | >= integers booleans binary operators

slide-40
SLIDE 40

Bool* vs. SpartanLang evaluation

e ⇓ v e,σ ⇓ v,σ'

Bool* relation: SpartanLang relation:

A "store", represented by the Greek letter sigma

slide-41
SLIDE 41

The Store

  • A mapping of references to values
  • Roughly analogous to the heap in

Java

slide-42
SLIDE 42

Key store operations

  • σ(x)

–get the value for reference x.

  • σ[x:=v]

–create a copy of store σ, except … –reference x has value v.

slide-43
SLIDE 43

Special syntax for references

In languages like ML, references are accessed with special syntax:

  • x = ref 42

creates a new reference with value 42 and stores the reference in variable x.

  • x := 7

changes the value of the reference to 7.

  • !x

– gets the value of the reference that x refers to.

slide-44
SLIDE 44

HW 2: Write an Interpreter for SpartanLang.

Details in Canvas