klisp

an open source interpreter for the Kernel Programming Language.
git clone http://git.hanabi.in/repos/klisp.git
Log | Files | Refs | README

Combiners.html (10575B)


      1 <html lang="en">
      2 <head>
      3 <title>Combiners - klisp Reference Manual</title>
      4 <meta http-equiv="Content-Type" content="text/html">
      5 <meta name="description" content="klisp Reference Manual">
      6 <meta name="generator" content="makeinfo 4.13">
      7 <link title="Top" rel="start" href="index.html#Top">
      8 <link rel="prev" href="Environments.html#Environments" title="Environments">
      9 <link rel="next" href="Continuations.html#Continuations" title="Continuations">
     10 <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
     11 <meta http-equiv="Content-Style-Type" content="text/css">
     12 <style type="text/css"><!--
     13   pre.display { font-family:inherit }
     14   pre.format  { font-family:inherit }
     15   pre.smalldisplay { font-family:inherit; font-size:smaller }
     16   pre.smallformat  { font-family:inherit; font-size:smaller }
     17   pre.smallexample { font-size:smaller }
     18   pre.smalllisp    { font-size:smaller }
     19   span.sc    { font-variant:small-caps }
     20   span.roman { font-family:serif; font-weight:normal; } 
     21   span.sansserif { font-family:sans-serif; font-weight:normal; } 
     22 --></style>
     23 <link rel="stylesheet" type="text/css" href="css/style.css">
     24 </head>
     25 <body>
     26 <div class="node">
     27 <a name="Combiners"></a>
     28 <p>
     29 Next:&nbsp;<a rel="next" accesskey="n" href="Continuations.html#Continuations">Continuations</a>,
     30 Previous:&nbsp;<a rel="previous" accesskey="p" href="Environments.html#Environments">Environments</a>,
     31 Up:&nbsp;<a rel="up" accesskey="u" href="index.html#Top">Top</a>
     32 <hr>
     33 </div>
     34 
     35 <!-- node-name,  next,  previous,  up -->
     36 <h2 class="chapter">9 Combiners</h2>
     37 
     38 <p><a name="index-combiners-126"></a><a name="index-applicatives-127"></a><a name="index-operatives-128"></a>
     39   There are two types of combiners in Kernel, operative and
     40 applicative. Both types are encapsulated. All combiners are immutable. 
     41 Two applicatives are <code>eq?</code> iff their underlying combiners are
     42 <code>eq?</code>.  However, <code>eq?</code>-ness of operatives is only
     43 constrained by the general rules for <code>eq?</code>, which leave
     44 considerable leeway for variation between implementations.  klisp only
     45 considers <code>eq?</code> those operatives constructed by the same call to
     46 a constructor (e.g. <code>$vau</code>).  Two combiners are <code>equal?</code>
     47 iff they are <code>eq?</code>. 
     48 <!-- TODO add xref for eq? and equal? -->
     49 
     50 <div class="defun">
     51 &mdash; Applicative: <b>operative?</b> (<var>operative? . objects</var>)<var><a name="index-operative_003f-129"></a></var><br>
     52 <blockquote><p>  The primitive type predicate for type operative. <code>operative?</code>
     53 returns true iff all the objects in <code>objects</code> are of type
     54 operative. 
     55 </p></blockquote></div>
     56 
     57 <div class="defun">
     58 &mdash; Applicative: <b>applicative?</b> (<var>applicative? . objects</var>)<var><a name="index-applicative_003f-130"></a></var><br>
     59 <blockquote><p>  The primitive type predicate for type applicative. 
     60 <code>applicative?</code> returns true iff all the objects in
     61 <code>objects</code> are of type applicative. 
     62 </p></blockquote></div>
     63 
     64 <div class="defun">
     65 &mdash; Operative: <b>$vau</b> (<var>$vau &lt;formals&gt; &lt;eformal&gt; . &lt;objects&gt;</var>)<var><a name="index-g_t_0024vau-131"></a></var><br>
     66 <blockquote><!-- TODO add xref to formal parameter tree -->
     67         <p><code>&lt;formals&gt;</code> should be a formal parameter tree; <code>&lt;eformal&gt;</code>
     68 should be either a symbol or <code>#ignore</code>.  If <code>&lt;formals&gt;</code> does
     69 not have the correct form for a formal parameter tree, or if
     70 <code>&lt;eformal&gt;</code> is a symbol that also occurs in <code>&lt;formals&gt;</code>, an
     71 error is signaled.
     72 
     73         <p>A <code>vau</code> expression evaluates to an operative; an operative
     74 created in this way is said to be compound. The environment in which
     75 the <code>vau</code> expression was evaluated is remembered as part of the compound
     76 operative, called the compound operative’s static environment. 
     77 <code>&lt;formals&gt;</code> and <code>&lt;objects&gt;</code> are copied as by
     78 <code>copy-es-immutable</code> and the copies are stored as part of the
     79 operative being constructed.  This avoids problem if these structures
     80 are later mutated.
     81 
     82      <!-- TODO add xref to eval or apply as example -->
     83         <p>When the compound operative created by <code>$vau</code> is later called
     84 with an object and an environment, here called respectively the
     85 operand tree and the dynamic environment, the following happens:
     86 
     87           <ol type=1 start=1>
     88 <li>A new, initially empty environment is created, with the static
     89 environment as its parent. This will be called the local environment.
     90 
     91           <li>A stored copy of the formal parameter tree formals is matched in the
     92 local environment to the operand tree, locally binding the symbols of
     93 <!-- TODO add xref to matching -->
     94 formals to the corresponding parts of the operand tree.  eformal is
     95 matched to the dynamic environment; that is, if eformal is a symbol
     96 then that symbol is bound in the local environment to the dynamic
     97 environment.
     98 
     99           <li><!-- TODO add xref to tail context. -->
    100 A stored copy of the expressions is evaluated sequentially from left
    101 to right, with the last (if any) evaluated as a tail context, or if
    102 the list of expressions is empty, the result is inert.
    103              </ol>
    104 
    105         <p>NOTE: Because compound operatives are not a distinct type in Kernel,
    106 they are covered by the encapsulation of type operative.  In
    107 particular, an implementation of Kernel cannot provide a feature that
    108 supports extracting the static environment of any given compound
    109 operative, nor that supports determining whether or not a given
    110 operative is compound. 
    111 </p></blockquote></div>
    112 
    113 <div class="defun">
    114 &mdash; Applicative: <b>wrap</b> (<var>wrap combiner</var>)<var><a name="index-wrap-132"></a></var><br>
    115 <blockquote><p>  The <code>wrap</code> applicative returns an applicative whose underlying
    116 combiner is <code>combiner</code>. 
    117 </p></blockquote></div>
    118 
    119 <div class="defun">
    120 &mdash; Applicative: <b>unwrap</b> (<var>unwrap applicative</var>)<var><a name="index-unwrap-133"></a></var><br>
    121 <blockquote><p>  The <code>unwrap</code> applicative returns the underlying combiner of
    122 <code>applicative</code>. 
    123 </p></blockquote></div>
    124 
    125 <div class="defun">
    126 &mdash; Operative: <b>$lambda</b> (<var>$lambda &lt;formals&gt; . &lt;objects&gt;</var>)<var><a name="index-g_t_0024lambda-134"></a></var><br>
    127 <blockquote><p>  <code>&lt;formals&gt;</code> should be a formal parameter tree.
    128 
    129         <p>The <code>$lambda</code> operative is defined by the following equivalence:
    130      <pre class="example">          ($lambda formals . objects) ==
    131             (wrap ($vau formals #ignore . objects))
    132 </pre>
    133         </blockquote></div>
    134 
    135 <div class="defun">
    136 &mdash; Applicative: <b>apply</b> (<var>apply applicative object </var>[<var>environment</var>])<var><a name="index-apply-135"></a></var><br>
    137 <blockquote><p>  Applicative <code>apply</code> combines the underlying combiner of
    138 <code>applicative</code> with <code>object</code> in a tail context with dynamic
    139 environment <code>environment</code> (if the long form is used) or in an
    140 empty environment (if the short form is used).
    141 
    142         <p>The following equivalences hold:
    143      <pre class="example">          (apply applicative object environment) ==
    144             (eval (cons (unwrap applicative) object) environment)
    145           
    146           (apply applicative object) ==
    147             (apply applicative object (make-environment))
    148 </pre>
    149         </blockquote></div>
    150 
    151 <div class="defun">
    152 &mdash; Applicative: <b>map</b> (<var>map applicative . lists</var>)<var><a name="index-map-136"></a></var><br>
    153 <blockquote><p>  <code>lists</code> must be a nonempty list of lists; if there are two or
    154 <!-- TODO add xref to length -->
    155 more, they must all have the same length. If <code>lists</code> is empty, or
    156 if all of its elements are not lists of the same length, an error is
    157 signaled.
    158 
    159         <p>The <code>map</code> applicative applies <code>applicative</code> element-wise
    160 to the elements of the lists in <code>lists</code> (i.e., applies it to a
    161 list of the first elements of the <code>lists</code>, to a list of the
    162 second elements of the <code>lists</code>, etc.), using the dynamic
    163 environment from which <code>map</code> was called, and returns a list of
    164 the results, in order. The applications may be performed in any order,
    165 as long as their results occur in the resultant list in the order of
    166 their arguments in the original <code>lists</code>.  If <code>lists</code> is a
    167 cyclic list, each argument list to which <code>applicative</code> is applied
    168 is structurally isomorphic to <code>lists</code>.  If any of the elements of
    169 <code>lists</code> is a cyclic list, they all must be, or they wouldn’t all
    170 have the same length.  Let <code>a1...an</code> be their acyclic prefix
    171 lengths, and <code>c1...cn</code> be their cycle lengths.  The acyclic
    172 prefix length <code>a</code> of the resultant list will be the maximum of
    173 the <code>ak</code>, while the cycle length <code>c</code> of the resultant list
    174 will be the least common multiple of the <code>ck</code>.  In the
    175 construction of the result, applicative is called exactly <code>a + c</code>
    176 times. 
    177 <!-- TODO comp/xref for-each -->
    178 </p></blockquote></div>
    179 
    180 <div class="defun">
    181 &mdash; Applicative: <b>string-map</b> (<var>string-map applicative . strings</var>)<var><a name="index-string_002dmap-137"></a></var><br>
    182 &mdash; Applicative: <b>vector-map</b> (<var>vector-map applicative . vectors</var>)<var><a name="index-vector_002dmap-138"></a></var><br>
    183 &mdash; Applicative: <b>bytevector-map</b> (<var>bytevector-map applicative . bytevectors</var>)<var><a name="index-bytevector_002dmap-139"></a></var><br>
    184 <blockquote><p><code>strings</code>, <code>vectors</code>, or <code>bytevectors</code> should be
    185 non-empty lists of the corresponding type and all elements should be
    186 of the same length.
    187 
    188         <p>These applicatives behave as <code>map</code> except that the list of
    189 elements passed to <code>applicative</code> are the n-th chars, objects, or
    190 uint8s of the strings, vectors or bytevectors passed as arguments.
    191 
    192         <p>SOURCE NOTE: These are taken from r7rs. 
    193 </p></blockquote></div>
    194 
    195 <div class="defun">
    196 &mdash; Applicative: <b>combiner?</b> (<var>combiner? . objects</var>)<var><a name="index-combiner_003f-140"></a></var><br>
    197 <blockquote><p>  The primitive type predicate for type combiner. <code>combiner?</code>
    198 returns true iff all the objects in <code>objects</code> are of type
    199 combiner (i.e. applicative or operative). 
    200 </p></blockquote></div>
    201 
    202 <!-- *-texinfo-*- -->
    203    </body></html>
    204