<?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 2ajela3ur5s 7q?>

  <title><?xxe-sn 2ajela3ur5s 7r?>Transcend</title>

  <para><?xxe-sn 2ajela3ur5s 7s?>Transcend is a compilation phase and a
  process of reasoning about a program in order to influence final compilation
  results.</para>

  <para><?xxe-sn 2ajela3ur5s 7t?>First, the compiler extracts
  <emphasis><?xxe-sn 2ajela3ur5s 7u?>annotations</emphasis> from a source code
  as well as other <emphasis><?xxe-sn 2ezs03nkiyo 4?>logic facts</emphasis>
  from a number of different sources. Second, logic facts are coupled with
  <emphasis><?xxe-sn 2ezs03nkiyo 5?>logic rules</emphasis>, that describe how
  to process input data, to form a complete <emphasis><?xxe-sn 2ezs03nkiyo 6?>logic
  program</emphasis>. Finally, a solver processes the logic program and
  produces decisions that affect and control compilation in a number of ways.
  Crucial difference of annotations from language keywords is that they do not
  have any predefined meaning and semantic; only custom developer provided
  rules make them matter.</para>

  <para><?xxe-sn 2ezs03nkiyo 7?><phrase condition="clasp"><?xxe-sn 2ezs03nkiyo 8?>Currently,
  an external logic solver Clasp is used to perform Transcend tasks. Hence,
  logic rules are written(and annotations are converted to) in the ASP format:
  one of standard first order logic notations.</phrase></para>

  <section>
    <?xxe-sn 2f0cduzin5j -wunr7fl0rw85?>

    <title><?xxe-sn 2f0cduzin5j -wunr7fl0rw84?>Reasoning Solutions</title>

    <para><?xxe-sn 2f0cduzin5j -wunr7fl0rw83?>Transcend prepares solutions to
    verify, control and guide compilation process. For each uncertain
    situation the compiler consults reasoning solutions to know how to
    proceed. Thus, many compilation aspects are delegated to Transcend and are
    ultimately exposed to a software developer. Transcend solution are used in
    a number of ways as follows:</para>

    <itemizedlist>
      <?xxe-sn 2f0cduzin5j -wunr7fl0rw82?>

      <listitem>
        <?xxe-sn 2f0cduzin5j -wunr7fl0rw81?>

        <para><?xxe-sn 2f0cduzin5j -wunr7fl0rw80?><link
        xlink:href="/d/concepts/interpretation/#queries"><?xxe-sn 2fadpf0i3zr -wunr7fl0rw8v?>Direct
        connection</link> via the <code><?xxe-sn 2fadpf0i3zr -wunr7fl0rw8u?>query</code>
        built-in function. A program can request and explicitly process
        received data from Transcend as it sees fit.</para>
      </listitem>

      <listitem>
        <?xxe-sn 2f0cduzin5j -wunr7fl0rw7y?>

        <para><?xxe-sn 2f0cduzin5j -wunr7fl0rw7x?><link
        xlink:href="/d/concepts/polymorphism/"><?xxe-sn 2f0cduzin5j -wunr7fl0rw7w?>Function
        level polymorphism</link>. This is the principal way for Transcend to
        interfere in compilation. Polymorphism is based on a possibility for a
        function to have several specializations. Each time the function is
        invoked in a client code, Transcend decides which exactly
        specialization to call. This gives a deep control over final program
        behaviour.</para>
      </listitem>

      <listitem>
        <?xxe-sn 2f0cduzin5j -wunr7fl0rw7v?>

        <para><?xxe-sn 2f0cduzin5j -wunr7fl0rw7u?><link
        xlink:href="/d/syntax/modules/"><?xxe-sn 2f0cduzin5j -wunr7fl0rw7t?>Module
        level polymorphism</link>. Transcend decides which modules to choose
        that satisfy a client code's requirements.</para>
      </listitem>

      <listitem>
        <?xxe-sn 2f0cduzin5j -wunr7fl0rw7s?>

        <para><?xxe-sn 2f0cduzin5j -wunr7fl0rw7r?>The compiler allows
        Transcend to add hidden parameters every time specific functions are
        invoked. This is implemented to support the <link
        xlink:href="/d/concepts/context/"><?xxe-sn 2f0cduzin5j -wunr7fl0rw7q?>Context</link>
        concept.</para>
      </listitem>

      <listitem>
        <?xxe-sn 2f0cduzin5j -wunr7fl0rw7p?>

        <para><?xxe-sn 2f0cduzin5j -wunr7fl0rw7o?>Diagnostics. Transcend is
        able to validate different aspects of a program and print diagnostic
        messages to signal found inconsistencies.</para>
      </listitem>
    </itemizedlist>

    <para><?xxe-sn 2f0cduzin5j -wunr7fl0rw7n?>As an example, suppose we want
    to control functions visibility and want to separate the <code><?xxe-sn 2f60onvmups 2?>visibility(private)</code>
    functions from the rest of a program:</para>

    <programlisting xml:id="Diagnostics1"><?xxe-sn 2f0cduzin5j -wunr7fl0rw7m?>name="tests/transcend.cpp: Transcend.Doc_Diagnostics1"
sum = function(a:: int, b:: int) ::      int; visibility(private)
{ 
  a + b
}

calculate = function(a:: int, b:: int):: int; visibility(pub_interface)
{
  sum(a, b)
}

test = function(a:: int, b:: int) ::     int; entry
{
   sum(a, b)       // we may not invoke this directly
}</programlisting>

    <para><?xxe-sn 2f0cduzin5j -wunr7fl0rw7l?>Transcend converts the code
    above to logic facts as follows(the relevant part is shown):</para>

    <programlisting><?xxe-sn 2f0cduzin5j -wunr7fl0rw7j?>function(sum)
function(calculate)
function(test)

bind_func(sum,visibility(private))
bind_func(calculate,visibility(pub_interface))
bind_func(test,entry)

cfa_parent(0,function(sum))
cfa_parent(1,function(calculate))
cfa_parent(2,function(test))

cfa_call(1,sum)
cfa_call(2,calculate)</programlisting>

    <para><?xxe-sn 2f0cduzin5j -wunr7fl0rw7i?>At this point all the
    annotations are completely unknown to the compiler. Next, we need to write
    rules to check that the <code><?xxe-sn 2f0cduzin5j -wunr7fl0rw7h?>visibility(private)</code>
    functions are called either by another <code><?xxe-sn 2f0cduzin5j -wunr7fl0rw7g?>visibility(private)</code>
    ones or by the <code><?xxe-sn 2f0cduzin5j -wunr7fl0rw7f?>visibility(pub_interface)</code>
    functions which are intended to act as a contact point, the only allowed
    gate between the private part and the rest of a program:</para>

    <programlisting xml:id="Diagnostics1_Rules"><?xxe-sn 2f60onvmups 1?>name="tests/transcend.cpp: Transcend.Doc_Diagnostics1"
% Define which functiones are allowed to invoke private ones
allowed(
  visibility(private);
  visibility(pub_interface)
).

% Check every invocation for violations:
warning("Visibility violation", FuncFrom, FuncTo):-
  cfa_call(ScopeFrom, FuncTo); 
  bind_func(FuncTo, visibility(private));
  scope_func_dict(ScopeFrom, FuncFrom);
  { bind_func(FuncFrom, Visibility): allowed(Visibility) } 0.</programlisting>

    <para><?xxe-sn 2f60onvmups 3?>Given the rules Transcend goes through all
    the invocations to check are there any possible visibility violations. For
    this example Transcend generates a diagnostic message: <code><?xxe-sn 2f60onvmups 4?>warning("Visibility violation",test,sum)</code>.</para>

    <para><?xxe-sn 2f60onvmups 5?>This way it is possible to validate and
    guide compilation process by analysing program properties employing
    provided processing rules.</para>
  </section>

  <section>
    <?xxe-sn 2ajela3ur5s 7w?>

    <title><?xxe-sn 2ajela3ur5s 7x?>Annotations' Syntax</title>

    <para><?xxe-sn 2ajela3ur5s 7y?>Xreate's annotations comprise optional
    supplementary information to be processed by the compiler along with a
    source code proper with an end goal being to affect a compilation process.
    Annotations are special expressions in the following form:</para>

    <informaltable>
      <?xxe-sn 2ajela3ur5s 7z?>

      <tgroup cols="2">
        <?xxe-sn 2ajela3ur5s 80?>

        <colspec colwidth="183*"><?xxe-sn 2ajela3ur5s 81?></colspec>

        <colspec colwidth="817*"><?xxe-sn 2ajela3ur5s 82?></colspec>

        <tbody>
          <?xxe-sn 2ajela3ur5s 83?>

          <row>
            <?xxe-sn 2ajela3ur5s 84?>

            <entry><?xxe-sn 2ajela3ur5s 85?>Literals</entry>

            <entry><?xxe-sn 2ajela3ur5s 86?>Strings, numbers. Example:
            <code><?xxe-sn 2ajela3ur5s 87?>5, "success"</code></entry>
          </row>

          <row>
            <?xxe-sn 2ajela3ur5s 88?>

            <entry><?xxe-sn 2ajela3ur5s 89?>Predicates</entry>

            <entry><?xxe-sn 2ajela3ur5s 8a?>Predicates have zero or more
            arguments. Example: <code><?xxe-sn 2ajela3ur5s 8b?>final, status(broken), version("12.0.3", unstable)</code></entry>
          </row>

          <row>
            <?xxe-sn 2ajela3ur5s 8c?>

            <entry><?xxe-sn 2ajela3ur5s 8d?>Negation</entry>

            <entry><?xxe-sn 2ajela3ur5s 8e?>Example: <code><?xxe-sn 2ajela3ur5s 8f?>-i12n, -access(allowed)</code></entry>
          </row>
        </tbody>
      </tgroup>
    </informaltable>

    <para><?xxe-sn 2ajela3ur5s 8g?>Various Xreate's entities can be annotated.
    At the time following entities are supported:</para>

    <informaltable>
      <?xxe-sn 2ajela3ur5s 8h?>

      <tgroup cols="4">
        <?xxe-sn 2ajela3ur5s 8i?>

        <tbody>
          <?xxe-sn 2ajela3ur5s 8j?>

          <row>
            <?xxe-sn 2ajela3ur5s 8k?>

            <entry><?xxe-sn 2ajela3ur5s 8l?><link
            xlink:href="#expressions"><?xxe-sn 2ajela3ur5s 8m?>Expressions</link></entry>

            <entry><?xxe-sn 2ajela3ur5s 8n?><link
            xlink:href="#code-blocks-and-context"><?xxe-sn 2ajela3ur5s 8o?>Code
            blocks and context</link></entry>

            <entry><?xxe-sn 2ajela3ur5s 8p?><link
            xlink:href="/d/syntax/#functions"><?xxe-sn 2ajela3ur5s 8q?>Functions</link>
            and <link xlink:href="/d/syntax/#function-guards"><?xxe-sn 2ajela3ur5s 8r?>Function
            Guards</link></entry>

            <entry><?xxe-sn 2ajela3ur5s 8s?><link
            xlink:href="/d/syntax/modules/#module-annotations"><?xxe-sn 2ajela3ur5s 8t?>Modules</link></entry>
          </row>
        </tbody>
      </tgroup>
    </informaltable>

    <section>
      <?xxe-sn 2ajela3ur5s 8u?>

      <title><?xxe-sn 2ajela3ur5s 8v?>Expressions</title>

      <synopsis><?xxe-sn 2ajela3ur5s 8w?>SYNTAX:
//expression// **::** //type//**;** //annotation-list//.</synopsis>

      <itemizedlist>
        <?xxe-sn 2ajela3ur5s 8x?>

        <listitem>
          <?xxe-sn 2ajela3ur5s 8y?>

          <para><?xxe-sn 2ajela3ur5s 8z?><emphasis><?xxe-sn 2ajela3ur5s 90?>type</emphasis>
          The expression's type</para>
        </listitem>

        <listitem>
          <?xxe-sn 2ajela3ur5s 91?>

          <para><?xxe-sn 2ajela3ur5s 92?><emphasis><?xxe-sn 2ajela3ur5s 93?>annotation-list</emphasis>
          one or more annotations attached to the expression. Annotations are
          delimited by semicolon.</para>
        </listitem>
      </itemizedlist>

      <para><?xxe-sn 2ajela3ur5s 94?>An example:</para>

      <programlisting xml:id="Expressions1"><?xxe-sn 2ajela3ur5s 95?>name="tests/transcend.cpp: Transcend.Doc_Expressions1"
test = function:: int; entry
{
  x = 5:: int; arithmetic(fast).                           //an annotation applied to an identifier
  y = ((x - 8):: int; arithm_optimization(off)) * 2:: int. //an annotation applied to a nested expression
  
  x+y
}</programlisting>

      <para><?xxe-sn 2ezs03nkiyo 9?>For readability's sake compound
      statements(e.g. <link xlink:href="/d/syntax/#loops"><?xxe-sn 2ezs03nkiyo a?>loops</link>)
      have slightly different syntax for annotations. See particular
      statement's syntax for details.</para>

      <note>
        <?xxe-sn 2ezs03nkiyo 2?>

        <para><?xxe-sn 2ezs03nkiyo 3?>A current limitation is that
        expression's annotations are always preceded by a type, hence to
        annotate nested expressions it is necessary to duplicate the
        expression's type.</para>
      </note>
    </section>

    <section>
      <?xxe-sn 2ajela3ur5s 9a?>

      <title><?xxe-sn 2ajela3ur5s 9b?>Code Blocks and Context</title>

      <synopsis><?xxe-sn 2ajela3ur5s 9c?>SYNTAX:
**context** :: //annotations-list//.</synopsis>

      <itemizedlist>
        <?xxe-sn 2ajela3ur5s 9d?>

        <listitem>
          <?xxe-sn 2ajela3ur5s 9e?>

          <para><?xxe-sn 2ajela3ur5s 9f?><emphasis><?xxe-sn 2ajela3ur5s 9g?>annotations-list</emphasis>
          A list of annotations delimited by semicolon</para>
        </listitem>
      </itemizedlist>

      <para><?xxe-sn 2ajela3ur5s 9h?>Code block annotations are called
      <emphasis><?xxe-sn 2ajela3ur5s 9i?>context</emphasis>. The keyword
      <code><?xxe-sn 2ajela3ur5s 9j?>context</code> is reserved to declare
      annotations within an enclosing code block.</para>

      <para><?xxe-sn 2ajela3ur5s 9k?>An example:</para>

      <programlisting xml:id="Codeblocks1"><?xxe-sn 2ajela3ur5s 9l?>name="tests/transcend.cpp: Transcend.Doc_Codeblocks1"
test = function:: int
{
  context:: arithm_optimization(off).

  x = 10 :: int.
  2 * x - 16
}</programlisting>
    </section>

    <section>
      <?xxe-sn 2ajela3ur5s 9m?>

      <title><?xxe-sn 2ajela3ur5s 9n?>Special Annotations</title>

      <para><?xxe-sn 2ajela3ur5s 9o?>Despite of "annotations do not have any
      predefined meaning" being a rule of thumb, still there are deviations in
      Xreate; few annotation that have special meaning are:</para>

      <informaltable>
        <?xxe-sn 2ajela3ur5s 9p?>

        <tgroup cols="2">
          <?xxe-sn 2ajela3ur5s 9q?>

          <colspec colwidth="91*"><?xxe-sn 2ezs03nkiyo b?></colspec>

          <colspec colwidth="909*"><?xxe-sn 2ezs03nkiyo c?></colspec>

          <tbody>
            <?xxe-sn 2ajela3ur5s 9t?>

            <row>
              <?xxe-sn 2ajela3ur5s 9u?>

              <entry><?xxe-sn 2ajela3ur5s 9v?><code><?xxe-sn 2ajela3ur5s 9w?>entry</code></entry>

              <entry><?xxe-sn 2ajela3ur5s 9x?>Specifies the entry point of a
              program. See <link xlink:href="/d/syntax/#functions"><?xxe-sn 2ajela3ur5s 9y?>Function
              Syntax</link>.</entry>
            </row>

            <row>
              <?xxe-sn 2ajela3ur5s 9z?>

              <entry><?xxe-sn 2ajela3ur5s a0?><code><?xxe-sn 2ajela3ur5s a1?>final</code></entry>

              <entry><?xxe-sn 2ajela3ur5s a2?>Specifies the fixed point of
              loops. See <link xlink:href="/d/syntax/#loops"><?xxe-sn 2ajela3ur5s a3?>Loops</link>.</entry>
            </row>
          </tbody>
        </tgroup>
      </informaltable>
    </section>
  </section>

  <section>
    <?xxe-sn 2ajela3ur5s a4?>

    <title><?xxe-sn 2ajela3ur5s a5?>Annotations and Reasoning</title>

    <para><?xxe-sn 2ajela3ur5s a6?>Annotations is a mechanism to express
    additional pieces of information and can occur in a source code or
    accompanying files. They are of declarative nature in a sense that they
    express specific properties of and relations between different entities,
    such as: identifiers and expressions, statements, code blocks, functions,
    modules or even a whole program.</para>

    <para><?xxe-sn 2ajela3ur5s a7?>Annotations are <emphasis><?xxe-sn 2ezs03nkiyo d?>explicit
    facts </emphasis>about a source code provided by a code writer in an
    explicit form. Beside annotations, there are other sources of information,
    in other words, <emphasis><?xxe-sn 2ezs03nkiyo f?>implicit
    facts</emphasis> that are automatically inferred by various analysis
    phases to express useful insights regarding an analysed code. Such pieces
    aren't exposed directly to a developer but accessed and used internally at
    the reasoning phase along with annotations. All types of input are summed
    up below:</para>

    <informaltable>
      <?xxe-sn 2ajela3ur5s a9?>

      <tgroup cols="2">
        <?xxe-sn 2ajela3ur5s aa?>

        <colspec colwidth="287*"><?xxe-sn 2ajela3ur5s ab?></colspec>

        <colspec colwidth="713*"><?xxe-sn 2ajela3ur5s ac?></colspec>

        <tbody>
          <?xxe-sn 2ajela3ur5s ad?>

          <row>
            <?xxe-sn 2ajela3ur5s ae?>

            <entry><?xxe-sn 2ajela3ur5s af?>Explicit annotations</entry>

            <entry><?xxe-sn 2ajela3ur5s ag?>Annotations provided by a code
            writer in order to explicitly affect compilation.</entry>
          </row>

          <row>
            <?xxe-sn 2ajela3ur5s ah?>

            <entry><?xxe-sn 2ajela3ur5s ai?>Code analysis</entry>

            <entry><?xxe-sn 2ajela3ur5s aj?>Different code analyses provide
            information implicitly inferred from a source code.</entry>
          </row>

          <row>
            <?xxe-sn 2ajela3ur5s ak?>

            <entry><?xxe-sn 2ajela3ur5s al?>Supervision</entry>

            <entry><?xxe-sn 2ajela3ur5s am?>External facts that reflect a
            hardware and/or an environment where compilation takes place or
            where a program is supposed to work. There are many supervision
            layers possible, from custom compiler's mode, to OS- or computer-
            or even network-wide levels. In other words, supervision covers
            local or client supplied annotations, e.g. prohibits web cam
            usage, or external network resources access.</entry>
          </row>

          <row>
            <?xxe-sn 2ajela3ur5s an?>

            <entry><?xxe-sn 2ajela3ur5s ao?>Audit</entry>

            <entry><?xxe-sn 2ajela3ur5s ap?>Third party supplied external
            information that reflects additional software properties, e.g.
            results of security audit.</entry>
          </row>

          <row>
            <?xxe-sn 2ajela3ur5s aq?>

            <entry><?xxe-sn 2ajela3ur5s ar?>Reasoning rules</entry>

            <entry><?xxe-sn 2ajela3ur5s as?>Reasoning infers additional facts
            from a pool of previously gathered information. It is done by
            using <emphasis><?xxe-sn 2ajela3ur5s at?>resoning rules</emphasis>
            that govern reasoning process.</entry>
          </row>
        </tbody>
      </tgroup>
    </informaltable>
  </section>

  <section>
    <?xxe-sn 2ajela3ur5s au?>

    <title><?xxe-sn 2ajela3ur5s av?>Why Annotations Matter</title>

    <para><?xxe-sn 2ajela3ur5s aw?>This section goes through differences that
    annotations have over traditional statements.</para>

    <section>
      <?xxe-sn 2ajela3ur5s ax?>

      <title><?xxe-sn 2ajela3ur5s ay?>Extensible</title>

      <para><?xxe-sn 2ajela3ur5s az?>Annotations are not a part of language
      syntax and there is no predefined set of allowed annotations. A
      developer is free to define and use custom annotations to express
      desired properties without prior changes to the compiler.</para>
    </section>

    <section>
      <?xxe-sn 2ajela3ur5s b3?>

      <title><?xxe-sn 2ajela3ur5s b4?>Indicative Nature</title>

      <para><?xxe-sn 2ajela3ur5s b5?>Unlike language statements that have well
      defined compilation output, annotation do not. They are rather of
      suggestive nature and actual impact depends on many factors. Annotations
      comprise only an input for a reasoning procedure that ultimately decides
      how exactly to influence compilation.</para>
    </section>

    <section>
      <?xxe-sn 2ajela3ur5s b6?>

      <title><?xxe-sn 2ajela3ur5s b7?>Cooperation</title>

      <para><?xxe-sn 2ajela3ur5s b8?>Annotations are gathered from different
      sources enabling annotations to complement each other. The compiler
      blends in annotations originated from various sources seamlessly to
      improve decisions quality.</para>
    </section>

    <section>
      <?xxe-sn 2ajela3ur5s b9?>

      <title><?xxe-sn 2ajela3ur5s ba?>External Sources and
      Adaptability</title>

      <para><?xxe-sn 2ajela3ur5s bb?>Annotations can be used not only to
      express properties of a program itself but also properties of a local
      environment(OS, computer, local network) where the program is supposed
      to run. Thus, in fact, the compiler operates by information that is
      usually inaccessible during software development and can't be reflected
      in a program code otherwise. In essence, it is <emphasis><?xxe-sn 2ezs03nkiyo g?>postcompilation</emphasis>
      - a process of adjustments to a particular environment and functioning
      mode.</para>
    </section>

    <section>
      <?xxe-sn 2ajela3ur5s bc?>

      <title><?xxe-sn 2ajela3ur5s bd?>Feedback</title>

      <para><?xxe-sn 2ajela3ur5s be?>Traditionally software has a rigid
      strictly defined hierarchical structure of interaction between
      components. For example, let us focus on a client-service model. A
      service works the same regardless of a particular client in order to be
      compatible with as wide set of different clients as possible. However
      clients may use only small subset of service functionality or use it in
      a specific and predictable order. Generally speaking, the service in
      case of having additional information about a particular client is able
      to serve requests more efficiently in many situations.</para>

      <para><?xxe-sn 2ajela3ur5s bg?>It can be conceptualized as a
      <emphasis><?xxe-sn 2ajela3ur5s bh?>feedback</emphasis> from a client.
      Annotations is a good candidate to express feedback to improve
      interaction quality.</para>
    </section>

    <section>
      <?xxe-sn 2ajela3ur5s bi?>

      <title><?xxe-sn 2ajela3ur5s bj?>Resiliency</title>

      <para><?xxe-sn 2ajela3ur5s bk?>As a program grows and goes through
      multiple reworks and rewrites many initial assumptions about the code
      and relation between different parts invalidated. Some implementation
      details and approaches became outdated and code quality deteriorates. A
      dIfferent picture is if the code uses annotations to express important
      properties. Any changes in an underlying code lead to a restarting of a
      reasoning procedure which adjust implementation decisions to be as
      optimal in a changed environment as before.</para>
    </section>
  </section>
</chapter>
