On the plane ride back from ICFP last month I had a chance to re-read and better understand Plotkin’s Set-theoretical and other elementary models of the λ-calculus (Technical Report 1972, Theoretical Computer Science 1993) and to read, for the first time, Engeler’s Algebras and combinators (Algebra Universalis 1981). As I wrote in my draft paper Declarative semantics for functional languages: compositional, extensional, and elementary, the main intuitions behind my simple semantics are present in these earlier papers, but until now I did not understand these other semantics deeply enough to give a crisp explanation of the similarities and differences. (The main intuitions are also present in the early work on intersection type systems, and my semantics is more closely related to those systems. A detailed explanation of that relationship is given in the draft paper.)
I should note that Engeler’s work was in the context of combinators (S and K), not the λ-calculus, but of course the λ-calculus can be encoded into combinators. I’ve ported his definitions to the λ-calculus, along the lines suggested by Plotkin (1993), to make for easier comparison. In addition, I’ll extend both Engeler and Plotkin’s semantics to include integers and integer arithmetic in addition to the λ-calculus. Here’s the syntax for the λ-calculus that we consider here: n∈Zx∈X(program variables)⊕::=+∣−∣×∣÷E∋e::=n∣e⊕e∣x∣λx.e∣ee∣ifetheneelsee
Values
Perhaps the best place to start the comparison is in the definition of what I’ll call values. All three semantics give an inductive definition of values and all three involve finite sets, but in different ways. I’ll write VS for my definition, VP for Plotkin’s, and VE for Engeler’s. VS=Z+Pf(VS×VS)VP=Z+Pf(VP)×Pf(VP)VE=Z+Pf(VE)×VE
Plotkin’s values VP include only a single input-output pair from a function’s graph. For example, ({0},{1}) is one of the meanings for the term (λx.x+1). Engeler’s values also include just a single entry. For example, ({0},1) is one of the meanings for the term (λx.x+1). In this example we have not made use of the finite sets in the input and output of Plotkin’s values. To do so, let us consider a higher-order example, such as the term (λf.f1+f2). For Plotkin, the following value is one of its meanings: ({({1},{3}),({2},{4})},{7})
The difference between Plotkin and Engeler’s values can be seen in functions that return functions. Consider the K combinator (λx.λy.x). For Plotkin, the following value is one of its meanings: ({1},{({0},{1}),({2},{1})})
The Domains
The semantics of Plotkin, Engeler, and myself does not use values for the domain, but instead a set of values. That is P(VS)P(VP)P(VE)
The role of the outer P is intimately tied to the meaning of functions in Plotkin and Engeler’s semantics because the values themselves only record a single input-output pair. The outer P is needed to represent all of the input-output pairs for a given function. While the P is also necessary for functions in my semantics, one can view it generically as providing non-determinism and therefore somewhat orthogonal to the meaning of functions per se. Next let’s take a look at the semantics.
Comparing the Semantics
Here is Plotkin’s semantics EP. Let V,V′ range over finite sets of values. EP[[n]]ρ={n}EP[[e1⊕e2]]ρ={n1⊕n2∣n1∈EP[[e1]]ρ∧n2∈EP[[e2]]ρ}EP[[x]]ρ=ρ(x)EP[[λx.e]]ρ={(V,V′)∣V′⊆EP[[e]]ρ(x:=V)}EP[[e1e2]]ρ=⋃{V′|∃V.(V,V′)∈EP[[e1]]ρ∧V⊆EP[[e2]]ρ}EP[[ife1thene2elsee3]]ρ={v|∃n.n∈EP[[e1]]ρ∧(n≠0⟹v∈EP[[e2]]ρ)∧(n=0⟹v∈EP[[e3]]ρ)}
Next we consider Engeler’s semantics EE. EE[[n]]ρ={n}EE[[e1⊕e2]]ρ={n1⊕n2∣n1∈EE[[e1]]ρ∧n2∈EE[[e2]]ρ}EE[[x]]ρ=ρ(x)EE[[λx.e]]ρ={(V,v′)∣v′∈EE[[e]]ρ(x:=V)}EE[[e1e2]]ρ={v′|∃V.(V,v′)∈EE[[e1]]ρ∧V⊆EE[[e2]]ρ}EE[[ife1thene2elsee3]]ρ={v|∃n.n∈EE[[e1]]ρ∧(n≠0⟹v∈EE[[e2]]ρ)∧(n=0⟹v∈EE[[e3]]ρ)}
Finally we review my semantics ES. For it we need to define an ordering on values that is just equality for integers and ⊆ on function graphs. Let t range over Pf(V×V). n⊑nt1⊆t2t1⊑t2
Relational Semantics
For people like myself with a background in operational semantics, there is another view of the semantics that is helpful to look at. We can turn the above dentoational semantics into a relational semantics (like a big-step semantics) that hides the P by making use of the following isomorphism (where V is one of VS, VP, or VE). E→(X⇀V)→P(V)≅E×(X⇀V)×V
For comparison, let us also turn Plotkin’s semantics into a relation. ρ⊢Pn⇒nρ⊢Pe1⇒n1ρ⊢Pe2⇒n2ρ⊢Pe1⊕e2⇒n1⊕n2v∈ρ(x)ρ⊢Px⇒v∀v′∈V′.ρ(x:=V)⊢Pe⇒v′ρ⊢Pλx.e⇒(V,V′)ρ⊢Pe1⇒(V,V′)∀v2∈V.ρ⊢Pe2⇒v2v′∈V′ρ⊢P(e1e2)⇒v′ρ⊢Pe1⇒nn≠0ρ⊢Pe2⇒vρ⊢Pife1thene2elsee3⇒vρ⊢Pe1⇒0ρ⊢Pe3⇒vρ⊢Pife1thene2elsee3⇒v
The relational version of Engeler’s semantics removes the need for quantification in the lambda rule, but the application rule still has ∀v2∈V. ρ(x:=V)⊢Ee⇒v′ρ⊢Eλx.e⇒(V,v′)ρ⊢Ee1⇒(V,v′)∀v2∈V.ρ⊢Ee2⇒v2ρ⊢E(e1e2)⇒v′
Conclusion
My semantics is similar to Plotkin and Engeler’s in that
The domain is a set of values, and values are inductively defined and involve finite sets.
Self application is enabled by allowing a kind of subsumption on functions.
The really nice thing about all three semantics is that they are simple; very little mathematics is necessary to understand them, which is important pedagogically, practically (easier for practitioners to apply such semantics), and aesthetically (Occam’s razor!).
My semantics is different to Plotkin and Engeler’s in that
the definition of values places Pf so that functions are literally represented by finite graphs, and
environments map each variable to a single value, and
⊑ is used instead of ⊆ to enable self application.
The upshot of these (relatively minor) differences is that my semantics may be easier to understand.
No comments:
Post a Comment