an alternative semantics yet another alternative semantics
play

An alternative semantics Yet another alternative semantics Judgments - PDF document

An alternative semantics Yet another alternative semantics Judgments of the form E V Use explicit environments, not substitution expression E reduces fully to normal form V closer still to real interpreter big-step


  1. An alternative semantics Yet another alternative semantics Judgments of the form E � V Use explicit environments, not substitution • “expression E reduces fully to normal form V ” • closer still to real interpreter • big-step operational semantics (CBV) environment ρ : a sequence of I = V pairs Can formalize different reduction semantics • records the value of each bound identifier E.g., call-by-value reduction: (Big-step) judgments of the form ρ |− E � V [ ] λ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - • “in environment ρ , ( λ I: τ .E ) � ( λ I: τ .E ) expression E reduces fully to normal form V ” � ( λ I: τ .E ) � ⁄ � E 1 E 2 V 2 [V 2 I ]E V [ ] app - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ( ) � E 1 E 2 V [ ] ∈ ρ if I=V var - - - - - - - - - - - - - - - - - - - - - - - - ρ |− � I V Comparison with small-step: [ ] λ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ρ |− ( λ I: τ .E ) � ( λ I: τ .E ) • specifies same result values • simpler, fewer tedious rules ρ |− � ( λ I: τ .E ) ρ |− � E 1 E 2 V 2 • closely matches recursive interpreter implementation ρ I=V 2 , |− � E V 2 [ ] ∉ dom ρ ( ) if I app - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - • not as nice for proofs, since each step is “bigger” ρ |− ( ) � E 1 E 2 V Craig Chambers 204 CSE 505 Craig Chambers 205 CSE 505 Closures A question Values become pairs of lambdas and environments What types should be given to the formals below? V ::= < λ I : τ . E , ρ > ( λ x: ? . x x) Revised rules: loop ≡ (( λ z: ? . z z) ( λ z: ? . z z)) [ ] ∈ ρ if I=V var - - - - - - - - - - - - - - - - - - - - - - - - ρ |− � I V Y ≡ ( λ f: ? . ( λ x: ? . f (x x)) ( λ x: ? . f (x x))) [ ] λ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ρ |− ( λ I: τ .E ) � < λ I: τ .E ρ > , ρ |− � < λ I: τ .E ρ '> , ρ |− � E 1 E 2 V 2 ρ ' I=V 2 , |− � E V 2 [ ] ∉ dom ρ ' ( ) if I app - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ρ |− ( ) � E 1 E 2 V Comparison with substitution-based semantics: • specifies “equivalent” result values • apply environment as substitution to lambda to get same result • but multiple closures represent same substituted lambda • very close match to interpreter implementation • much more complicated � bad for proofs Craig Chambers 206 CSE 505 Craig Chambers 207 CSE 505

  2. Amazing fact #5: Adding explicit recursive values All simply typed λ -calculus programs terminate! Make simply typed λ -calculus more expressive by adding a new primitive to define recursive values: fix Cannot assign types to any program involving self-application • would require infinite or circular or recursive types Additional syntax: ::= ... | fix E But self-application was used for loop , Y , etc. E • cannot write looping or recursive programs in simply typed λ -calculus, at least in this way Additional typing rule: Γ |− E : τ → τ [ ] fix - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Thm ( Strong normalization ). Γ |− ( ) : τ fix E Every simply typed λ -calculus term has a normal form. • all type-correct programs are guaranteed to terminate! Additional (small-step) reduction rule: Simply typed λ -calculus is not Turing-complete! [ ] fix - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ( ) → ( ) fix E E fix E • bad for expressiveness in a real PL • good in restricted domains where we need termination guarantees Example of use: • type checkers nat ≡ ( ∗→∗ ) →∗→∗ • OS packet filters fact ≡ fix ( λ fact: nat → nat . • ... λ n: nat . if ( isZero n) one ( mul n (fact ( pred n)))) Craig Chambers 208 CSE 505 Craig Chambers 209 CSE 505 Other extensions Ints Can design more realistic languages by extending λ -calculus Additional syntax for types, expressions, and values: τ ::= ... | int Formalize semantics using typing rules and reduction rules E ::= ... | 0 | ... | E 1 + E 2 | ... ::= ... | 0 | ... V Examples: • ints Additional typing rules: • bools • let [ ] if k ∈ numeral - - - - - - - - - - - - - - - - - - - - - Nat Γ |− k :int • records • tagged unions Γ |− Γ |− E 1 :int E 2 :int [ ] + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - • recursive types, e.g. lists Γ |− ( ) :int E 1 + E 2 • mutable references Additional (big-step) evaluation rules: [ ] val - - - - - - - - - - - - - � V V � � E 1 V 1 E 2 V 2 ˆ V 2 [ ] V V 1 + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - = ( ) � E 1 + E 2 V Note: didn’t have to change any existing rules to add these new features � they’re orthogonal Craig Chambers 210 CSE 505 Craig Chambers 211 CSE 505

  3. Bools Let Additional syntax for types, expressions, and values: Additional syntax for expressions: τ ::= ... | bool E ::= ... | let I = E 1 in E 2 ::= ... | true | false E | if E 1 then E 2 else E 3 Additional typing rules: ::= ... | true | false V Additional typing rules: [ ] [ ] true - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - false - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Γ |− Γ |− true :bool false :bool Γ |− Γ |− E 2 : τ Γ |− E 2 : τ E 1 :bool [ ] if - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Γ |− ( ) : τ if E 1 then E 2 else E 3 Additional (big-step) evaluation rules: Additional (big-step) evaluation rules: � � E 1 true E 2 V 2 [ ] if true - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ( ) � if E 1 then E 2 else E 3 V 2 � � E 1 false E 3 V 3 [ ] if false - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ( ) � if E 1 then E 2 else E 3 V 3 Craig Chambers 212 CSE 505 Craig Chambers 213 CSE 505 Records Tagged unions Additional syntax for types, expressions, and values: A tagged union type is a primitive version of an ML datatype: τ ::= ... | { I 1 : τ 1 , ... , I k : τ k } a set of labeled alternative types A value of a tagged union type is one of the labels E ::= ... | { I 1 = E 1 , ... , I k = E k } | # I E ::= ... | { I 1 = V 1 , ... , I k = V k } tagging a value of the corresponding alternative type V • in contrast to records whose values have all of the labeled element types Additional typing rules: Example: let u = ( if ... then <A=5> else <B=true>) in (* u has type <A:int, B:bool> *) case u of <A=i> => printInt i | <B=b> => printBool b Additional (big-step) evaluation rules: Craig Chambers 214 CSE 505 Craig Chambers 215 CSE 505

Recommend


More recommend