CS 541 Lecture -*- Outline -*- * operational semantics of recursion ref: Hennessy, The Semantics of Programming Langauges, section 4.3 (using an idea of D. Schmidt's and ignoring environment issues) ** adding recursion to the lambda calculus ------------------------------------------ RECURSION IN OPERATIONAL SEMANTICS OF THE \-CALCULUS Extended abstract syntax: M, N \in LambdaTerm I \in Identifier M ::= I | (\ I . M) | (M N) | (rec I = M) Additional Transition Axioms: [unroll] (rec I = M) --> Example: (rec fact = (\n . (((IF (eq n 0)) 1) ((mult n) (fact ((sub n) 1)))))) --> (\n . (((IF (eq n 0)) 1) ((mult n) ((rec fact = (\n . (((IF (eq n 0)) 1) ((mult n) (fact ((sub n) 1)))))) ((sub n) 1))))) ------------------------------------------ ... M' where M' = [(rec I = M)/I]M Q: can you "simplify" ((rec ohno = (\n. (ohno n))) 3) for a few transitions? (rec ohno = (\n. (ohno n))) --> (\n.((rec ohno = (\n. (ohno n))) n)) [app-operator] ------------------------------------------------------------- ((rec ohno = (\n. (ohno n))) 3) --> ((\n.((rec ohno = (\n. (ohno n))) n)) 3) [beta] ((\n.((rec ohno = (\n. (ohno n))) n)) 3) --> ((rec ohno = (\n. (ohno n))) 3) Then this is back to where we started. ** while loops in an imperative language ------------------------------------------ WHILE LOOPS IN AN IMPERATIVE LANGUAGE Abstract Syntax: L \in Location B, E \in Expression C \in Command C ::= E | skip | L := E | C1 ; C2 | if E then C1 else C2 | while B do C Computation Semantics: Programs = Command Gamma = T = Answers = input[[C]] = output(g) = ------------------------------------------ ... Command * Store ... { (C,sigma) | C = skip } ... Store ... (C, empty_store) ... sigma, where g = (C,sigma) Expressions are unspecified The terminal configurations are just those whose command is skip To be precise, would have to say what Store is, and what empty_store is. ------------------------------------------ STORES Domains: V \in Value Storable = Value Sigma \in Store = Location -> Storable Auxiliary functions: empty_store: Store empty_store = \L.unbound update: Store -> Location -> Storable -> Store update Sigma L V = \L'. if L = L' then V else Sigma(L') access: Store -> Location -> Storable access Sigma L = Sigma(L) ------------------------------------------ the value domain is left unspecified ------------------------------------------ TRANSITION AXIOMS AND RULES Auxiliary semantic relation: ==>e : Expression * Store -> Value * Store -> o ------------------------------------------ Q: Can you define a suitable grammar for E and a big step semantics for ==>e (this should allow side effects in expressions)? ------------------------------------------ IMPERATIVE COMMANDS PART 1 --> : Command * Store -> Command * Store -> o E, Sigma ==>e V, Sigma' [exp-cmd] ------------------------- E, Sigma --> skip, Sigma' [assign] ------------------------------- L := E, Sigma --> skip, Sigma'' ------------------------------------------ ... E, Sigma ==>e V, Sigma', where Sigma'' = update Sigma' L V Q: What rule(s) would you have for C1 ; C2 ? ... C1, Sigma --> C1', Sigma' [semi1] ------------------------------ C1;C2, Sigma --> C1';C2, Sigma' [semi2] skip;C2, Sigma --> C2, Sigma Q: What is the semantics of L1 := 1; L2 := 2 ? Q: Can you give a 2 rules for if-commands? ------------------------------------------ IMPERATIVE COMMANDS PART 2 B, Sigma ==>e false, Sigma' [whileF] ------------------------------ while B do C, Sigma --> skip, Sigma' B, Sigma ==>e true, Sigma' [whileT] ------------------------------ while B do C, Sigma --> C;while B do C, Sigma' ------------------------------------------ Q: What is the meaning of the program L1 := 1; while L1 < 3 do L1 := L1 + 1 ? See the code in this directory (while.mod). Q: Can you give a big step semantics for commands? What does a while loop look like in that semantics? Q: Can you give a little step semantics for expressions and modify the above rules to use that? ** summary Recursion or looping in the langauge comes out as recursion or looping in the semantics In this sense, the operational semantics of recursion isn't very satisfactory.