modules structs hashes and operational semantics
play

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


  1. CS 152: Programming Language Paradigms Modules, Structs, Hashes, and Operational Semantics Prof. Tom Austin San José State University

  2. Modules

  3. Review Modules from HW 1 (in-class)

  4. How do we organize code in Java? • Packages provide units of code • Keywords specify method access

  5. In Java, keywords specify access • public • protected • no keyword (package access) • private

  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

  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 .

  8. Organizing Code in Racket • Exports specify public values: (provide big-add) • Imports specify code dependencies: (require "big-num.rkt")

  9. Structs and Hashes

  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>

  11. Hashes • Hashes are maps of key/value pairs. • Unlike Java, hashes are immutable. • <example in class>

  12. Formal Semantics

  13. Why do we need formal semantics?

  14. Everyone knows what an if statement does, right? if true then x = 1 At the end of this code else snippet, the value of x will be 1 x = 0

  15. Everyone knows what an if statement does, right? if false then x = 1 else At the end of this code snippet, the value of x will be 0 x = 0

  16. Everyone knows what an if statement does, right? if 0 then Will x be set to 0, like in C/C++? x = 1 Will x be set to 1, else like in Ruby? x = 0 Or will it be an error, like in Java?

  17. Everyone knows what an if statement does, right? x = if true Is assignment valid or an error? then 1 else 0

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

  19. A Review of Compilers We don't care Lexer/ about source tokens Parser code Tokenizer lexing or parsing. Abstract Compiler Interpreter Syntax Tree (AST) Machine code Commands We don't care if we have a compiler or interpreter

  20. A Review of Compilers We don't care Lexer/ about source tokens Parser code Tokenizer lexing or parsing. Abstract Compiler Interpreter Syntax Tree (AST) Machine code Commands We don't care if we have a compiler or interpreter

  21. ASTs are the key to Abstract understanding a language Syntax Tree (AST)

  22. Bool* Language e ::= expressions: true constant true | false constant false | if e conditional then e Despite appearances, these are really ASTs else e

  23. Values in Bool* v ::= values: true constant true | false constant false

  24. Formal Semantic Styles • Operational semantics – Big-step (or "natural") – Small-step (or "structural") • Axiomatic semantics • Denotational semantics

  25. Formal Semantic Styles • Operational semantics – Big-step (or "natural") – Small-step (or "structural") • Axiomatic semantics • Denotational semantics

  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.

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

  28. Big-Step Evaluation Relation An expression e … … evaluates to … ⇓ e v … a value v .

  29. Big-Step Evaluation Relation limits when the rule applies Preconditions ⇓ e v

  30. Big-step semantics for Bool* B-IfTrue e1 ⇓ true e2 ⇓ v if e1 then e2 else e3 ⇓ v e1 ⇓ false e3 ⇓ v B-IfFalse if e1 then e2 else e3 ⇓ v v ⇓ v B-Value

  31. Bool* big-step example true ⇓ true false ⇓ false if true ⇓ false then false false ⇓ false else true if (if true then false else true) then true ⇓ false else false

  32. Converting our rules into code (in-class)

  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.

  34. Extended Bool* Language e ::= true | false | if e then e else e | n Let's extend our | succ e semantics to handle these new language | pred e constructs

  35. Lab: Write a Bool* Interpreter • Starter code is available on the course website • Extend Bool* with numbers, succ , and pred

  36. Pop Quiz: Write operational semantics e ::= e and e | e or e | not e e ⇓ v | true | false v ::= true | false

  37. Adding State to Semantics

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

  39. SpartanLang (continued) v ::= i integers | b booleans binary operators op ::= + | - | \ | * | < | > | <= | >=

  40. Bool* vs. SpartanLang evaluation Bool* relation: e ⇓ v SpartanLang relation: ⇓ v, σ ' e, σ A "store", represented by the Greek letter sigma

  41. The Store • A mapping of references to values • Roughly analogous to the heap in Java

  42. Key store operations • σ (x) – get the value for reference x . • σ [x:=v] – create a copy of store σ , except … – reference x has value v .

  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.

  44. HW 2: Write an Interpreter for SpartanLang. Details in Canvas

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend