DOC PREVIEW
UMD CMSC 330 - Operational Semantics

This preview shows page 1-2-3-27-28-29 out of 29 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1CMSC 330:Operational Semantics2IntroductionWe’ve looked at several formal methods for defining the syntax of a programming language•Regular expressions•Context-free grammarsWhat about formal methods for defining the semantics of a programming language?•I.e., what does a program mean?3Roadmap: Compilation of ProgramGrammar:SAA  id = EE  T+E | TT  P * T | PP  id | n | (E)Program:X = 2 + 3Compilation:Push XPush 2Push 3Add top 2Assign top tosecondPostorder:postfixX 2 3 + =Recursive Descent Parsing: AST=+32XProgram semantics:What does program mean?Is this correct?What is program supposed to do?4Formal SemanticsFormal semantics of a programming language•Mathematical model of all possible computations performed by programs written in that languageThree main approaches to formal semantics•Denotational•Operational•Axiomatic5Formal Semantics (cont.)Denotational semantics•Translate parts of language into another languageUsually a mathematical functionMeaning of program expressed as mathematical objectOperational semantics•Describe effect of parts of languageUsually on (a mathematical model of) an abstract machineFor lambda calculus, can use syntactic transformationsAxiomatic semantics•Describe each part of language through logical axiomsFoundation of many program verification systems6Operational SemanticsWe will briefly look at operational semantics•Using a subset of OCaml as an exampleUseful for•Specifying the meaning of a program•Proving the correctness of an algorithmThrough formal verificationFor cryptographic algorithms, combinatorial circuits, etc…Currently limited to smaller programs7Roadmap: Semantics of a ProgramGrammar:SAA  id = EE  T+E | TT  P * T | PP  id | n | (E)Program:X = 2 + 3Compilation:Push XPush 2Push 3Add top 2Assign top tosecondPostorder:postfixX 2 3 + =Recursive Descent Parsing: AST=+32XvalueProgram Semantics:=+32X8EvaluationWe’re going to define a relation E → v•This means “expression E evaluates to v”So we need a formal way of defining programs and of defining things they may evaluate toWe’ll use grammars to describe each of these•One to describe abstract syntax trees E•One to describe OCaml values v9OCaml ProgramsE ::= x | n | true | false | [] | if E then E else E| fun x = E | E E•x stands for any identifier•n stands for any integer•true and false stand for the two boolean values•[] is the empty list•Using = in fun instead of -> to avoid some confusion later10Valuesv ::= n | true | false | [] | v::v•n is an integer (not a string corresp. to an integer)Same idea for true, false, []•v1::v2 is the pair with v1 and v2This will be used to build up listsNotice: nothing yet requires v2 to be a list•Important: Be sure to understand the difference between program text S and mathematical objects vE.g., the text 3 evaluates to the mathematical number 3•To help, we’ll use different colors and italicsIf not present, it’s up to you to remember which is which11Grammars for TreesWe’re just using grammars to describe treesE ::= x | n | true | false | [] | if E then E else E| fun x = E | E Ev ::= n | true | false | [] | v::vtype value =Val_Num of int| Val_Bool of bool| Val_Nil| Val_Pair of value *valuetype ast =Id of string| Num of int| Bool of bool| Nil| If of ast * ast * ast| Fun of string * ast| App of ast * astGiven a program, we know how to convert it to an AST using recursive descent parsingGoal: For any AST, we want an operational rule to obtain a value that represents the execution of that AST12Operational Semantics RulesEach basic entity evaluates to its corresponding valuen → ntrue → truefalse → false[] → []13Operational Semantics Rules (cont.)How about built-in functions?•We’re applying the + functionWe put parens around it because it’s not in infix notation•Will skip this from now onIgnore currying for the moment•Pretend we have multi-argument functions•On the right-hand side, we’re computing the mathematical sum; the left-hand side is source code•But what about + (+ 3 4) 5 ?We need recursion( + ) n m → n + m14Rules with HypothesesTo evaluate + E1E2, we need to evaluate E1, then evaluate E2, then add the results•This is call-by-value•This is a “natural deduction” style rule•It says that if the hypotheses above the line hold, then the conclusion below the line holdsi.e., if E1executes to value n and if E2executes to value m, then + E1E2executes to value n+m+ E1E2→ n + mE1→ n E2→ m15Error CasesWhat if E1 and E2 aren’t integers?•E.g., what if we write + false true ?•It can be parsed, but we can’t execute itPrevious rule does not cover such a case•Because we wrote n, m in the hypothesisSo they must be integers Convention•If there is no rule to cover a caseThen the expression is erroneous•A program that evaluates to an erroneous expressionProduces a run-time error in practice+ E1E2→ n + mE1→ n E2→ m16Trees of Semantic RulesWhen we apply rules to an expression, we actually get a tree•Corresponds to the recursive evaluation procedureFor example: + (+ 3 4 ) 5+ ( + 3 4) 5 →(+ 3 4) → 5 →3 → 4 →73 451217Rules for IfExamples•if false then 3 else 4 →4•if true then 3 else 4 →3Notice that only one branch is evaluatedif E1then E2 else E3→E1→ true E2→ vvif E1then E2 else E3→E1→ false E3→ vv18Rule for ::So :: allocates a pair in memoryAre there any conditions on E1and E2?•No! We will allow E2to be anything•OCaml’s type system will disallow non-lists:: E1E2→E1→ v1E2→ v2v1::v219Rules for IdentifiersLet’s assume for now that the only identifiers are parameter names•Example: (fun x = + x 3) 4•When we see x in the body, we need to look it up•So we need to keep some sort of environmentThis will be a map from identifiers to values20Semantics with EnvironmentsExtend rules to the form A; E → v•Means in environment A, program text E evaluates to vNotation•We write • for the empty environment (may be omitted)•We write A(x) for the value that x maps to in A•We write A, x:v for the same environment as A, except xis now vx might or might not have mapped to anything in A•We write A, A' for the environment with the bindings of A'added to and overriding the bindings of A21Rules for Identifiers and ApplicationTo evaluate a user-defined function applied to an argument:•Evaluate the argument


View Full Document

UMD CMSC 330 - Operational Semantics

Documents in this Course
Exam #1

Exam #1

6 pages

Quiz #1

Quiz #1

2 pages

Midterm 2

Midterm 2

12 pages

Exam #2

Exam #2

7 pages

Ocaml

Ocaml

7 pages

Parsing

Parsing

38 pages

Threads

Threads

12 pages

Ruby

Ruby

7 pages

Quiz #3

Quiz #3

2 pages

Threads

Threads

7 pages

Quiz #4

Quiz #4

2 pages

Exam #2

Exam #2

6 pages

Exam #1

Exam #1

6 pages

Threads

Threads

34 pages

Quiz #4

Quiz #4

2 pages

Threads

Threads

26 pages

Exam #2

Exam #2

9 pages

Exam #2

Exam #2

6 pages

Load more
Download Operational Semantics
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Operational Semantics and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Operational Semantics 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?