Page Menu
Home
Xreate
Search
Configure Global Search
Log In
Docs
Questions
Repository
Issues
Patches
Internal API
Files
F4002570
polymorphism.xml
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Thu, Jul 9, 7:41 AM
Size
12 KB
Mime Type
text/xml
Expires
Sat, Jul 11, 7:41 AM (1 d, 37 m)
Engine
blob
Format
Raw Data
Handle
273394
Attached To
rXR Xreate
polymorphism.xml
View Options
<?xml version="1.0" encoding="UTF-8"?>
<chapter version="5.1" xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xila="http://www.w3.org/2001/XInclude/local-attributes"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:trans="http://docbook.org/ns/transclusion"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:m="http://www.w3.org/1998/Math/MathML"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns:db="http://docbook.org/ns/docbook">
<?xxe-sn 26n42fz1reo 1?>
<title><?xxe-sn 26n42fz1reo 2?>Polymorphism</title>
<para><?xxe-sn 2be091dx6gw 1?>See <link
xlink:href="/d/syntax/#function-specializations"><?xxe-sn 2be091dx6gw 2?>function
specializations</link> for syntax.</para>
<para><?xxe-sn 26n42fz1reo 3?><emphasis><?xxe-sn 26n42fz1reo 4?>Polymorphism</emphasis>
is an umbrella term to denote a number of techniques across different
programing paradigms. They all share the same intention to provide the
ability to easily recombine software components in a different way with as
little manual work on developer's side as possible. It serves two major
goals: <emphasis><?xxe-sn 26n42fz1reo 5?>specialization</emphasis>, which
purports that software initially designed to support a wide range of use
cases is being configured for a specific case, and <emphasis><?xxe-sn 26n42fz1reo 6?>extension</emphasis>
– adapting software to an environment and conditions it was not specifically
designed for.</para>
<para><?xxe-sn 26n42fz1reo 7?>In course of software engineering evolution, a
number of polymorphism techniques was proposed and experimented with, all
suited for different use-cases. Xreate presents a generalized and elegant
approach that exhaustively covers the wide landscape of polymorphism
variations.</para>
<para><?xxe-sn 26n42fz1reo 8?>Polymorphism in Xreate can be applied on two
levels:</para>
<itemizedlist>
<?xxe-sn 26n42fz1reo 9?>
<listitem>
<?xxe-sn 26n42fz1reo a?>
<para><?xxe-sn 26n42fz1reo b?><emphasis><?xxe-sn 26n42fz1reo c?>Functions
level</emphasis>. Function in Xreate can have multiple
<emphasis><?xxe-sn 26n42fz1reo d?>specializations</emphasis>, and
polymorphism is compiler's ability to decide which exactly
specialization to use depending on various factors</para>
</listitem>
<listitem>
<?xxe-sn 26n42fz1reo e?>
<para><?xxe-sn 26n42fz1reo f?><emphasis><?xxe-sn 26n42fz1reo g?>Modules
level</emphasis>. Multiple modules can provide the same service for
users. <link
xlink:href="/d/syntax/modules/#modules-resolution"><?xxe-sn 26n42fz1reo h?>Modules
Resolution</link> is a process to decide which exactly module to
use.</para>
<remark><?xxe-sn 26vapv2jrow pu35zrt1aby9?>По поводу Modules Resolution:
кажется в предыдущем файле я внес несколько исправлений, добавив
апостроф после Modules. По грамматическим правилам это верно, но если
Modules Resolution это устоявшийся термин в твоей документации, то
возможно лучше без апострофа.</remark>
</listitem>
</itemizedlist>
<section>
<?xxe-sn 26n42fz1reo i?>
<title><?xxe-sn 26n42fz1reo j?>Function Level Polymorphism</title>
<para><?xxe-sn 26n42fz1reo k?>Basic idea is to allow developer to define
several functions with the same name or, in other words, several
<emphasis><?xxe-sn 26n42fz1reo l?>specializations</emphasis>. Caller code
then invokes the necessary function by its shared name, but cannot specify
a particular specialization directly. The exact specialization to be
invoked is decided later by a decision process called <emphasis><?xxe-sn 26n42fz1reo m?>polymorphism
resolution</emphasis> carried out by Transcend. This indirect invocation
approach gives enough flexibility to use or replace different
specializations depending on various conditions during compile time as
well as at runtime.</para>
<para><?xxe-sn 26n42fz1reo n?>Each specialization must have unique
<emphasis><?xxe-sn 26n42fz1reo p?>guard</emphasis> (among all
specializations with the same name) to be discernible from others. To
summarize, function invocation is a two-layered process, where the client
code specifies the callee function's shared name, while polymorphism
resolution specifies specialization guard, if needed.</para>
<para><?xxe-sn 26n42fz1reo q?>For example, let us assume that we have been
developing a software program to operate under specified time constraints.
To model an implementation suitable for real time environment, one
specialization of <code><?xxe-sn 26n42fz1reo r?>crucialOperation</code> is
defined with <code><?xxe-sn 26n42fz1reo s?>env(realtime)</code> guard,
i.e. it satisfies some fixed execution time constraints. Caller
<code><?xxe-sn 26n42fz1reo t?>main</code> specifies just a function name
<code><?xxe-sn 26n42fz1reo u?>crucialOperation</code> thus delegating the
decision in respect of guard to polymorphism resolution done elsewhere,
based on the environment's constraints the code is executed in.</para>
<programlisting xml:id="FnLvlPoly_1"><?xxe-sn 26n42fz1reo v?>name="tests/polymorph.cpp: Polymorphs.Doc_FnLvlPoly_1"
guard:: env(realtime)
{
crucialOperation = function:: int
{ 0 }
}
main = function:: int; entry
{
crucialOperation()
} </programlisting>
</section>
<section>
<?xxe-sn 26n42fz1reo w?>
<title><?xxe-sn 26n42fz1reo x?>Polymorphism Resolution</title>
<synopsis><?xxe-sn 26n42fz1reo y?>SYNTAX:
**dfa_callguard**(//call-site-ref//, //guard//)</synopsis>
<itemizedlist>
<?xxe-sn 26n42fz1reo z?>
<listitem>
<?xxe-sn 26n42fz1reo 10?>
<para><?xxe-sn 26n42fz1reo 11?><emphasis><?xxe-sn 26n42fz1reo 12?>call-site-ref</emphasis>
reference to a call site in AST</para>
</listitem>
<listitem>
<?xxe-sn 26n42fz1reo 13?>
<para><?xxe-sn 26n42fz1reo 14?><emphasis><?xxe-sn 26n42fz1reo 15?>guard</emphasis>
resolved function specialization guard</para>
</listitem>
</itemizedlist>
<para><?xxe-sn 26n42fz1reo 16?>When compiler encounters function
invocation that has several specializations it refers to the table
<code><?xxe-sn 26n42fz1reo 17?>dfa_callguard</code> to find out which
specialization to call. It must have an entry with appropriate
<code><?xxe-sn 26n42fz1reo 18?>guard</code> for every invocation site
<code><?xxe-sn 26n42fz1reo 19?>call-site-ref</code> of a polymorphic
function. Polymorphism resolution is a process of filling out
<code><?xxe-sn 26n42fz1reo 1a?>dfa_callguard</code> for the compiler based
on custom Transcend rules reflecting one or another polymorphism
strategy.</para>
</section>
<section>
<?xxe-sn 26n42fz1reo 1b?>
<title><?xxe-sn 26n42fz1reo 1c?>Late Polymorphism</title>
<para><?xxe-sn 26n42fz1reo 1d?>Late Polymorphism is an extension to allow
polymorphism resolution to be based on data known only at runtime, i.e.
resolve function specializations dynamically. The Idea is to use Late
Transcend to access runtime data. See <link
xlink:href="/d/transcend/latetranscend/"><?xxe-sn 26n42fz1reo 1e?>Late
Transcend</link> for details.</para>
<para><?xxe-sn 26n42fz1reo 1f?>Example below demonstrates <code><?xxe-sn 26n42fz1reo 1g?>test</code>
invoking polymorphic function <code><?xxe-sn 26n42fz1reo 1h?>compute</code>:</para>
<programlisting xml:id="LatePoly_1"><?xxe-sn 26n42fz1reo 1i?>name="tests/polymorph.cpp: Polymorphs.Doc_LatePoly_1"
Strategy = type variant {fast, precise}.
guard:: fast
{
compute = function:: int
{0}
}
guard:: precise
{
compute = function:: int
{1}
}
test = function(s:: Strategy; alias(strategy)):: int; entry
{
switch late (s):: int
{
compute():: int; guardalias(strategy)
}
}</programlisting>
<para><?xxe-sn 26n42fz1reo 1j?>Function <code><?xxe-sn 26n42fz1reo 1k?>compute</code>
has two specializations, <code><?xxe-sn 26n42fz1reo 1l?>fast</code> and
<code><?xxe-sn 26n42fz1reo 1m?>precise</code>. We see that <code><?xxe-sn 26n42fz1reo 1n?>test</code>
gets parameter <code><?xxe-sn 26n42fz1reo 1o?>s</code> that dictates exact
strategy to use. Clearly, resolution should work dynamically to deal with
cases like this, for not only the value of the parameter <code><?xxe-sn 26n42fz1reo 1p?>s</code>
is unknown at the compile time, but also it can change with each
<code><?xxe-sn 26n42fz1reo 1q?>test</code> execution.</para>
<para><?xxe-sn 26n42fz1reo 1r?>Operation <code><?xxe-sn 2be091dx6gw 3?>switch late</code>
is compiled into several branches, two in this case, each branch executing
appropriate <code><?xxe-sn 26n42fz1reo 1s?>compute</code> specialization.
The correct branch is executed depending on the current <code><?xxe-sn 26n42fz1reo 1t?>s</code>
value. Custom annotations <code><?xxe-sn 26n42fz1reo 1u?>alias(Alias)</code>
and <code><?xxe-sn 26n42fz1reo 1v?>guardalias(Alias)</code> are used to
assign an alias in order to specify which parameter to use as basis for
resolution.</para>
</section>
<section>
<?xxe-sn 26n42fz1reo 1w?>
<title><?xxe-sn 26n42fz1reo 1x?>Auto Expansion of Late Parameters</title>
<para><?xxe-sn 26n42fz1reo 1y?>In the previous example, operation
<code><?xxe-sn 2be091dx6gw 4?>switch late</code> was used to facilitate
calling of a polymorphic function with late polymorphism resolution. It is
not that convenient to wrap each invocation using <code><?xxe-sn 2be3dypnsao 1?>switch
late</code> whenever we need to call a late polymorphic function. Compiler
uses the late parameter auto expansion technique in order to specifically
handle cases like this.</para>
<para><?xxe-sn 26n42fz1reo 1z?>If compiler discovers that <code><?xxe-sn 26n42fz1reo 20?>late(dfa_callguard())</code>
entry exists for current invocation, and it does not have enclosing
<code><?xxe-sn 2be091dx6gw 5?>switch late</code> already, compiler
automatically generates different branches that invoke relevant
specializations and transfers control to a branch depending on the late
parameter's value. In other words, invocation is implicitly wrapped into
<code><?xxe-sn 2be091dx6gw 6?>switch late</code> if needed.</para>
</section>
</chapter>
<?xxe-revisions
#5 2019-03-07T17:53:18Z pgess
#4 2019-03-07T17:09:57Z pgess
1sPEAAABzxMAJM5rAAEQDVNzwgojBRMMU4Zt1VMFY4VXAMcao16ePpdskCeHIA==
#3 2019-01-04T15:38:33Z Acer2
1sPEAAABzmsAiGDYGwCCDIQFgkZQbGVhc2YgYWJzLiBhPz4iRCIgLbXQs9C40YDRg4DQtbW+0YK4
uNC80YMg0YE/INCV0L+y0Yuy0LDRgNC40LDQs9C00LCw0LfRjCAiImE3eGd5eTVxejFzIi4uLrWM
uNGA0Ysg0LfRgdGMIHNpYXJ5LmNhbWJyaWRnZWl1YT8+MSkgIr27vdGP0L280Yu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