CS 541 Lecture -*- Outline -*- * Structural operational semantics ** two styles ------------------------------------------ STYLES OF STRUCTURAL OPERATIONAL SEMANTICS names better for big step, reasoning about program evaluation, natural little step compilers, concurrency computation TTS ------------------------------------------ ** evaluation, big step, or natural semantics big step (Gunter), or Evaluation (M. Hennessy), or natural semantics (Kahn). this is similar to denotational semantics in feel *** idea describe a binary relation (evalsto, ==>) between: Programs = abstract syntax trees and a domain of Answers (program results, values) ------------------------------------------ EVALUATION (BIG STEP) SEMANTICS evaluation relation: evalsto is a idea: ------------------------------------------ ... binary relation between Programs and Answers ... specify that relation directly *** example ------------------------------------------ EXAMPLE: ARITHMETIC EXPRESSIONS Concrete Syntax: ::= | ::= + | - ::= | * Abstract Syntax: ------------------------------------------ ... E1, E2 \in Expression Oper \in Operator Ans, N \in Integer E ::= (num N) | (op Oper E1 E2) Oper ::= add | sub | mult | divide Q: How could we code the abstract syntax for this in \Prolog? --------------------------------------------------------- BIG STEP SEMANTICS FOR ARITHMETIC (num N) ==> N E1 ==> V1, E2 ==> V2, (ap Oper V1 V2) ==> Ans _______________________ (op Oper E1 E2) ==> Ans (ap add N1 N2) ==> Ans, where Ans = N1 + N2 (ap sub N1 N2) ==> Ans, where Ans = N1 - N2 (ap mult N1 N2) ==> Ans, where Ans = N1 * N2 --------------------------------------------------------- Show how to draw the proof trees for doing it by hand! (num 5) ==> 5, (num 2) ==> 2, (ap mult 5 2) ==> 10 _________________________________________________ (op mult (num 5) (num 2)) ==> 10 (num 3) ==> 3, (num 4) ==> 4, (ap mult 3 4) ==> 12 _________________________________________________ (op mult (num 3) (num 4)) ==> 12, (num 8) ==> 8, (ap add 12 8) ==> 20 _________________________________________________ (op add (op mult (num 3) (num 4)) (num 8)) ==> 20 Q: can you show the proof for: (op mult (num 10) (op sub (num 5) (num 3)))? *** in lambda Prolog Now let's do this in \Prolog.... ------------------------------------------ % abstract syntax for arithmetic % concrete syntax: % ::= % | % ::= + | - % ::= | * sig arith_abstract_syntax. kind expression type. kind operator type. % abstract syntax type add operator. type sub operator. type mult operator. type divide operator. type num int -> expression. type op operator -> expression -> expression -> expression. module arith_abstract_syntax. % see the signature file. ------------------------------------------ ------------------------------------------ sig arith. accum_sig arith_abstract_syntax. type evalsto expression -> int -> o. end % Evaluation semantics for arithmetic % expressions, adapted from M. Hennessy: % The Semantics of Programming Languages % (Wiley, 1990). module arith. accumulate arith_abstract_syntax. % auxilliary semantic relations local ap operator -> int -> int -> int -> o. % the evalsto relation (evalsto (num N) N). ((evalsto E1 V1), (evalsto E2 V2), (ap Oper V1 V2 Ans)) => % ---------------------------------- (evalsto (op Oper E1 E2) Ans). (ap add N1 N2 Ans) :- Ans is N1 + N2. (ap sub N1 N2 Ans) :- Ans is N1 - N2. (ap mult N1 N2 Ans) :- Ans is N1 * N2. ------------------------------------------ Talk through the two evalsto rules Try some queries such as the following either on the machine or by hand. $ tjsim arith [arith] ?- (evalsto (op mult (num 5) (num 2)) V). The answer substitution: V = 10 [arith] ?- (evalsto (op add (op mult (num 3) (num 4)) (num 8)) V). The answer substitution: V = 20 **** side conditions and other variations show the variation where the ap rule is made into a function, and side conditions are used Q: What is the best way to show use of side-conditions in proofs? Also show the variation where there are 4 operator rules... **** dealing with errors But for a semantics, we'd like to say something about what happens in the exceptional cases... Q: Can you add division to this? What about division by zero? (list them on the board) do the stuck and summand with error solutions at least - stuck means no rule applies, have to restrict the ap rule with (not (N = 0)). - summand rule would be something like: kind value type. type inInteger int -> value. type error value. ** terminal transition system, little step, computation semantics terminal transition system (Plotkin), little step (C. Gunter), computation (M. Hennessy) essentially using rewriting as a universal machine *** idea To give the semantics of a programming language, use two auxiliary functions: input and output ------------------------------------------ COMPUTATION (LITTLE STEP) SEMANTICS Meaning Programs <-------> Answers | ^ input | | output | | v reducesto* | Gamma <-----------> T def: a in Meaning[[P]] iff ------------------------------------------ the Meaning relation is often partial, and defined by going around the diagram ... there is a g in T such that input[[P]] reducesto* g and output(g) = a. Meaning[[P]] is a function when reducesto* is Church-Rosser (confluent) *** terminal transition systems this is the guts of the system, defining the abstract machine by defining reducesto ------------------------------------------ TERMINAL TRANSITION SYSTEM (TTS) (Gamma, reducesto, T) Gamma: reducesto: T: reducesto* reflexive, transitive closure ------------------------------------------ ... a set of configurations (e.g., g) i.e., configurations of the abstract machine ... a binary relation on Gamma reducesto is often written as ==> or --> (Hennessy) ... subset of terminal configurations, must be such that if g in T, then there is no g' such that (reducesto g g') *** example: arithmethic expressions ------------------------------------------ COMPUTATIONAL SEMANTICS OF ARITHMETIC EXPRESSIONS Programs = Gamma = T = Answers = int input[[P]] = output[[(num N)]] = ------------------------------------------ ... ... { (num N) | N : int } ... P ... N the rest is given by the following \Prolog module. ------------------------------------------ sig comput_arith. accum_sig arith_abstract_syntax, rtc. type reducesto exp -> exp -> o. type output_fun exp -> int -> o. end % Computation semantics for the language % of arithmetic expressions. Adapted % from M. Hennessy: The Semantics of % Programming Languages (Wiley, 1990). module comput_arith. accumulate arith_abstract_syntax, rtc. % the reducesto relation (ap Oper V1 V2 N) => % ------------------------------------ (reducesto (op Oper (num V1) (num V2)) (num N)). (reducesto E1 E1') => % ---------------------------- (reducesto (op Oper E1 E2) (op Oper E1' E2)). (reducesto E2 E2') => % -------------------------- (reducesto (op Oper (num V1) E2) (op Oper (num V1) E2')). % the output relation (output_fun (num N) N). % auxilliary semantic relations local ap operator -> int -> int -> int -> o. (ap add N1 N2 Ans) :- Ans is N1 + N2. (ap sub N1 N2 Ans) :- Ans is N1 - N2. (ap mult N1 N2 Ans) :- Ans is N1 * N2. ------------------------------------------ Q: how would you describe the order of evaluation? Some simple example queries [comput_arith] ?- (reducesto (op mult (num 5) (num 2)) V), (output_fun V N). The answer substitution: N = 10 V = num 10 But what about... ?- (reducesto (op add (op mult (num 3) (num 4)) (num 8)) V), (output_fun V N). no (more) solutions ?- (reducesto (op add (op mult (num 3) (num 4)) (num 8)) V). The answer substitution: V = op add (num 12) (num 8) Q: what has to be done? Need the transitive closure of reducesto can they write that as a higher-order predicate? [comput_arith] ?- (rtc reducesto (op add (op mult (num 3) (num 4)) (num 8)) V). The answer substitution: V = op add (op mult (num 3) (num 4)) (num 8) More solutions (y/n)? y The answer substitution: V = op add (num 12) (num 8) More solutions (y/n)? y The answer substitution: V = num 20 *** Example: the lambda calculus (just mention if already did this) ------------------------------------------ SYNTAX OF THE UNTYPED LAMBDA CALCULUS M ::= term I identifier | (\I . M) abstraction | (M1 M2) application ------------------------------------------ Q: Is this an abstract or concrete syntax? this syntax doesn't allow for superfluous parens, or for leaving any out to apply to a concrete term in which some are left out, or there are extras have to parse by making it match this syntax. ------------------------------------------ COMPUTATIONAL SEMANTICS OF LAMBDA CALCULUS TERMS Programs = LambdaTerm Gamma = T = Answers = input[[M]] = output[[M]] = ------------------------------------------ ... LambdaTerm ... LambdaTerm in beta-eta-normal form ... LambdaTerm / cnv(alpha) (alpha cnv equiv. classes) ... M ... { M' | M' is alpha-convertible to M} (alpha equivalence class) **** axioms for transitions and inference rules ------------------------------------------ TRANSITION AXIOMS AND RULES [beta] ((\I.M) N) --> [eta] (\I.(M I)) --> [reduce-body] ------------------ (\I.M) --> (\I.M') [app-operator] ---------------- (M N) --> (M' N) [app-operand] ---------------- (M N) --> (M N') ------------------------------------------ ... M', where M' = [N/I]M ... M, where I is not free in M ... M --> M' ... M --> M' ... N --> N' explain substitution, have them formalize as rules... Q: Is this little step or big step? Q: Can you trace the execution of (((\x.(\y. (y x))) i) z)? [beta] ((\x.(\y. (y x))) i) --> (\y. (y i)) [app-operator] -------------------------------------- (((\x.(\y. (y x))) i) z) --> ((\y. (y i)) z) [beta] ((\y. (y i)) z) --> (z i) Q: What is the meaning of ((\x. (x x)) (\x.x))? Q: What is the meaning of ((\x.(x f)) (\x.(z x))? Q: What is the meaning of ((\x.(x x)) (\x.(x x)))? **** remarks see the directory lambda-in-lambda-prolog for an encoding in \Prolog of the above rules the lambda calculus reductions are Church-Rosser (confluent) see the directory ../lambda-calculus for more details on the lambda calculus **** variations change to call by value ** Nondeterminism operator choose that introduces nondeterminism choose M M' --> M [Choose-1] choose M M' --> M' [Choose-2] destroys confluence (and referential transparency), but allows for models of parallelism Q: how would you specify angelic, demonic nondeterminism? angelic picks the one that terminates [angelic-1] angelic (num N1) E2 --> (num N1) [angelic-2] angelic E1 (num N2) --> (num N2) E1 --> E1', E2 --> E2' [angelic-rec] ---------------------------------- angelic E1 E2 --> angelic E1' E2' ** process calculi see Chapter 6 of Hennessy's book, Semantics of Programming Languages (Wiley, 1990)