https://en.wikipedia.org/w/index.php?action=history&feed=atom&title=Talk%3ADenotational_semantics%2FCompositionality_in_Programming_LanguageTalk:Denotational semantics/Compositionality in Programming Language - Revision history2025-06-08T15:46:47ZRevision history for this page on the wikiMediaWiki 1.45.0-wmf.4https://en.wikipedia.org/w/index.php?title=Talk:Denotational_semantics/Compositionality_in_Programming_Language&diff=852565937&oldid=prevWOSlinker: tt fix2018-07-29T21:48:16Z<p>tt fix</p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Previous revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 21:48, 29 July 2018</td>
</tr><tr>
<td colspan="2" class="diff-lineno">Line 16:</td>
<td colspan="2" class="diff-lineno">Line 16:</td>
</tr>
<tr>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>===Problems with the functional approach to compostionality in programming languages===</div></td>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>===Problems with the functional approach to compostionality in programming languages===</div></td>
</tr>
<tr>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td>
</tr>
<tr>
<td class="diff-marker" data-marker="−"></td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>Unfortunately, the above is not a general scheme for compositional denotational semantics that is adequate for modern software engineering. An initial difficulty comes with expressions like <tt>8/0</tt>. An inital approach was to define a special element of the domain to be an "error element" and have <tt>8/0<tt> denote the error element. However, the error element approach is unsatisfactory for software engineering. One problem is that it becomes necessary for programs to continually test for the error element. Another problem is that the error element approach does not automatically propagate exceptions higher if they are not caught.</div></td>
<td class="diff-marker" data-marker="+"></td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>Unfortunately, the above is not a general scheme for compositional denotational semantics that is adequate for modern software engineering. An initial difficulty comes with expressions like <tt>8/0</tt>. An inital approach was to define a special element of the domain to be an "error element" and have <tt>8/0<<ins style="font-weight: bold; text-decoration: none;">/</ins>tt> denote the error element. However, the error element approach is unsatisfactory for software engineering. One problem is that it becomes necessary for programs to continually test for the error element. Another problem is that the error element approach does not automatically propagate exceptions higher if they are not caught.</div></td>
</tr>
<tr>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td>
</tr>
<tr>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>An even more serious problem with the above functional approach is that it does not encompass communication and change. Consider the following program in [[Actor_model#ActorScript|ActorScrip]]t:</div></td>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>An even more serious problem with the above functional approach is that it does not encompass communication and change. Consider the following program in [[Actor_model#ActorScript|ActorScrip]]t:</div></td>
</tr>
<tr>
<td colspan="2" class="diff-lineno">Line 26:</td>
<td colspan="2" class="diff-lineno">Line 26:</td>
</tr>
<tr>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>:::''implements'' Cell</div></td>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>:::''implements'' Cell</div></td>
</tr>
<tr>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>::::Read< > → ''return'' contents</div></td>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>::::Read< > → ''return'' contents</div></td>
</tr>
<tr>
<td class="diff-marker" data-marker="−"></td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>::::Write<newContents> → ''return also become''(contents=newContents)</tt></div></td>
<td class="diff-marker" data-marker="+"></td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>::::Write<newContents> → ''return also become''(contents=newContents)</div></td>
</tr>
<tr>
<td colspan="2" class="diff-empty diff-side-deleted"></td>
<td class="diff-marker" data-marker="+"></td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div></tt></div></td>
</tr>
<tr>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td>
</tr>
<tr>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>What is the denotation of the expression "<tt>''return also become''(contents=newContents)</tt>" in the above program?</div></td>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>What is the denotation of the expression "<tt>''return also become''(contents=newContents)</tt>" in the above program?</div></td>
</tr>
<tr>
<td colspan="2" class="diff-lineno">Line 47:</td>
<td colspan="2" class="diff-lineno">Line 48:</td>
</tr>
<tr>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Consider what happens when an expression of the form <tt>"(<L> 1 2)"</tt> is sent an <tt>Eval</tt> message with environment '''E'''. One semantics for application expressions such as this one is the following: <tt><L>, 1</tt> and <tt>2</tt> are each sent <tt>Eval</tt> messages with environment '''E'''. The integers <tt>1</tt> and <tt>2</tt> immediately reply to the <tt>Eval</tt> message with themselves.</div></td>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Consider what happens when an expression of the form <tt>"(<L> 1 2)"</tt> is sent an <tt>Eval</tt> message with environment '''E'''. One semantics for application expressions such as this one is the following: <tt><L>, 1</tt> and <tt>2</tt> are each sent <tt>Eval</tt> messages with environment '''E'''. The integers <tt>1</tt> and <tt>2</tt> immediately reply to the <tt>Eval</tt> message with themselves.</div></td>
</tr>
<tr>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td>
</tr>
<tr>
<td class="diff-marker" data-marker="−"></td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>However, <tt><L><tt> responds to the <tt>Eval</tt> message by creating a [[Closure (computer science)|closure]] '''C''' that has a ''body'' <tt><L></tt> and environment '''E'''. <tt>"(<L> 1 2)"</tt> then sends '''C''' the message '''[1 2]'''.</div></td>
<td class="diff-marker" data-marker="+"></td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>However, <tt><L><<ins style="font-weight: bold; text-decoration: none;">/</ins>tt> responds to the <tt>Eval</tt> message by creating a [[Closure (computer science)|closure]] '''C''' that has a ''body'' <tt><L></tt> and environment '''E'''. <tt>"(<L> 1 2)"</tt> then sends '''C''' the message '''[1 2]'''.</div></td>
</tr>
<tr>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td>
</tr>
<tr>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>When '''C''' receives the message '''[1 2]''', it creates a new environment '''F''' which behaves as follows: </div></td>
<td class="diff-marker"></td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>When '''C''' receives the message '''[1 2]''', it creates a new environment '''F''' which behaves as follows: </div></td>
</tr>
</table>WOSlinkerhttps://en.wikipedia.org/w/index.php?title=Talk:Denotational_semantics/Compositionality_in_Programming_Language&diff=321476536&oldid=prevPi Delport: extract from parent discussion2009-10-22T23:14:00Z<p>extract from parent discussion</p>
<p><b>New page</b></p><div>(The following article extract was moved here from the [[Talk:Denotational semantics#Material in "Compositionality in Programming Language" removed by Sam Staton|parent discussion]]. <span style="white-space:nowrap">—[[User:Piet Delport|Piet Delport]] <small>([[User talk:Piet Delport|talk]]) 2009-10-22 23:13</small></span>)<br />
<br />
==Material in "Compositionality in Programming Language" removed by Sam Staton==<br />
An important aspect of denotational semantics of programming languages is compositionality, by which the denotation of a program is constructed from denotations of its parts. For example consider the expression "8 / 4". Compositionality in this case is to provide a meaning for "8 / 4" in terms of the meanings of "8", "4" and "/".<br />
<br />
===Functional approach===<br />
A functional approach to denotational semantics in is compositional because it is given as follows. We start by considering program fragments, i.e. programs with free variables. A ''typing context'' assigns a type to each free variable. For instance, in the expression (''x'' / ''y'') might be considered in a typing context (''x'':<tt>nat</tt>,''y'':<tt>nat</tt>). We now give a denotational semantics to program fragments, using the following scheme. <br />
#We begin by describing the meaning of the types of our language: the meaning of each type must be a domain. We write 〚τ〛 for the domain denoting the type τ. For instance, the meaning of type <tt>nat</tt> should be the domain of natural numbers: 〚<tt>nat</tt>〛=ℕ<sub>⊥</sub>.<br />
#From the meaning of types we derive a meaning for typing contexts. We set 〚 ''x''<sub>1</sub>:τ<sub>1</sub>,…, ''x''<sub>n</sub>:τ<sub>n</sub>〛 = 〚 τ<sub>1</sub>〛× … ×〚τ<sub>n</sub>〛. For instance, 〚''x'':<tt>nat</tt>,''y'':<tt>nat</tt>〛= ℕ<sub>⊥</sub>×ℕ<sub>⊥</sub>. As a special case, the meaning of the empty typing context, with no variables, is the domain with one element, denoted 1.<br />
#Finally, we must give a meaning to each program-fragment-in-typing-context. Suppose that ''P'' is a program fragment of type σ, in typing context Γ, often written Γ⊢''P'':σ. Then the meaning of this program-in-typing-context must be a continuous function 〚Γ⊢''P'':σ〛:〚Γ〛→〚σ〛. For instance, 〚⊢8:<tt>nat</tt>〛:1→ℕ<sub>⊥</sub> is the constantly "8" function, while 〚''x'':<tt>nat</tt>,''y'':<tt>nat</tt>⊢''x''/''y'':<tt>nat</tt>〛:ℕ<sub>⊥</sub>×ℕ<sub>⊥</sub>→ℕ<sub>⊥</sub> is the function that divides two numbers.<br />
<br />
Now, the meaning of the compound expression (8/4) is determined by composing the three functions 〚⊢8:<tt>nat</tt>〛:1→ℕ<sub>⊥</sub>, 〚⊢4:<tt>nat</tt>〛:1→ℕ<sub>⊥</sub>, and 〚''x'':<tt>nat</tt>,''y'':<tt>nat</tt>⊢''x''/''y'':<tt>nat</tt>〛:ℕ<sub>⊥</sub>×ℕ<sub>⊥</sub>→ℕ<sub>⊥</sub>.<br />
<br />
There is nothing specific about domains and continuous functions here. One can work with a different [[category (mathematics)|category]] instead. For example, in [[game semantics]], the category of games has games as objects and strategies as morphisms: we can interpret types as games, and programs as strategies. For a simple language without recursion, we can make do with the [[Category of sets|category of sets and functions]]. For a language with side-effects, we can work in the [[Kleisli category]] for a monad. For a language with state, we can work in a [[functor category]].<br />
<br />
===Problems with the functional approach to compostionality in programming languages===<br />
<br />
Unfortunately, the above is not a general scheme for compositional denotational semantics that is adequate for modern software engineering. An initial difficulty comes with expressions like <tt>8/0</tt>. An inital approach was to define a special element of the domain to be an "error element" and have <tt>8/0<tt> denote the error element. However, the error element approach is unsatisfactory for software engineering. One problem is that it becomes necessary for programs to continually test for the error element. Another problem is that the error element approach does not automatically propagate exceptions higher if they are not caught.<br />
<br />
An even more serious problem with the above functional approach is that it does not encompass communication and change. Consider the following program in [[Actor_model#ActorScript|ActorScrip]]t:<br />
<br />
<tt><br />
:SimpleCell ≡<br />
::''serializer''<br />
:::contents<br />
:::''implements'' Cell<br />
::::Read< > → ''return'' contents<br />
::::Write<newContents> → ''return also become''(contents=newContents)</tt><br />
<br />
What is the denotation of the expression "<tt>''return also become''(contents=newContents)</tt>" in the above program?<br />
<br />
Scott and Strachey [1971] proposed that the semantics of programming languages be reduced to the semantics of the [[lambda calculus]] and thus inherit the [[Domain_theory#Motivation_and_intuition|denotational semantics of the lambda calculus]]. However, it turned out that general computation could not be implemented in the lambda calculus (see [[Indeterminacy in concurrent computation]]). Thus there arose the problem of how to provide modular denotational semantics for general programming languages. One solution to this problem is to use the <tt>Eval</tt> messages with an environment so that programs obtain their denotational semantics from the methods explained earlier in this article.<br />
<br />
===General compositionality in programming languages===<br />
The general approach to compositonality in programming languages solves the above problems using [[environments]] and [[Actor_model#Customers|customers]] as explained below.<br />
<br />
====Environments====<br />
Environments hold the bindings of identifiers. When an environment is sent a <tt>Lookup</tt> message for an identifier '''x''', it returns the latest (lexical) binding of '''x'''.<br />
<br />
As an example of how this works consider the lambda expression <tt><L></tt> below which implements a tree data structure when supplied with parameters for a <tt>leftSubTree</tt> and <tt>rightSubTree</tt>. When such a tree is given a parameter message <tt>"getLeft"</tt>, it return <tt>leftSubTree</tt> and likewise when given the message <tt>"getRight"</tt> it returns <tt>rightSubTree</tt>.<br />
<br />
λ(leftSubTree, rightSubTree)<br />
λ(message)<br />
''if'' (message == "getLeft") ''then'' leftSubTree<br />
''else if'' (message == "getRight") ''then'' rightSubTree<br />
<br />
Consider what happens when an expression of the form <tt>"(<L> 1 2)"</tt> is sent an <tt>Eval</tt> message with environment '''E'''. One semantics for application expressions such as this one is the following: <tt><L>, 1</tt> and <tt>2</tt> are each sent <tt>Eval</tt> messages with environment '''E'''. The integers <tt>1</tt> and <tt>2</tt> immediately reply to the <tt>Eval</tt> message with themselves.<br />
<br />
However, <tt><L><tt> responds to the <tt>Eval</tt> message by creating a [[Closure (computer science)|closure]] '''C''' that has a ''body'' <tt><L></tt> and environment '''E'''. <tt>"(<L> 1 2)"</tt> then sends '''C''' the message '''[1 2]'''.<br />
<br />
When '''C''' receives the message '''[1 2]''', it creates a new environment '''F''' which behaves as follows: <br />
#When it receives a <tt>Lookup</tt> message for the identifier <tt>leftSubTree</tt>, it responds with <tt>1</tt><br />
#When it receives a <tt>Lookup</tt> message for the identifier <tt>rightSubTree</tt>, it responds with <tt>2</tt><br />
#When it receives a <tt>Lookup</tt> message for any other identifier, it forwards the <tt>Lookup</tt> message to '''E'''<br />
<br />
'''C''' then sends an <tt>Eval</tt> message with environment '''F''' to the following:<br />
<br />
λ(message)<br />
''if'' (message == "getLeft") ''then'' leftSubTree<br />
''else if'' (message == "getRight") ''then'' rightSubTree<br />
<br />
====Arithmetic expressions====<br />
For another example consider the expression "<tt><expression<sub>1</sub>> / <expression<sub>2</sub>></tt>" which has the subexpressions <tt><expression<sub>1</sub>></tt> and <tt><expression<sub>2</sub>></tt>. When the composite expression receives an <tt>Eval</tt> message with an environment '''E''' and a customer '''C''', it sends <tt>Eval</tt> messages to <tt><expression<sub>1</sub>></tt> and <tt><expression<sub>2</sub>></tt> with environment '''E''' and sends '''C''' a newly created '''C<sub>0</sub>'''. When '''C<sub>0</sub>''' has received back two values '''N<sub>1</sub>''' and '''N<sub>2</sub>''', it sends "'''/'''" the message ['''N<sub>1</sub>''' '''N<sub>2</sub>'''] with customer '''C''' to perform the division.<br />
<br />
====Other programming language constructs====<br />
The denotational compositional semantics presented above is very general and can be used for [[Functional programming|functional]], [[Imperative programming|imperative]], [[Concurrent programming language|concurrent]], [[Logic programming|logic]], ''etc.'' programs.<ref>Carl Hewitt. [http://arxiv.org/abs/0812.4852 Common sense for concurrency and strong paraconsistency using unstratified inference and reflection] Arxiv 0812.4852. 2008.</ref> For example it easily provides denotation semantics for constructs that are difficult to formalize using other approaches such as [[Delay (programming)|delays]] and [[Future (programming)|futures]].<br />
<br />
{{reflist|2}}[[Special:Contributions/76.254.235.105|76.254.235.105]] ([[User talk:76.254.235.105|talk]]) 02:59, 16 September 2009 (UTC)</div>Pi Delport