1 <!-- This file is part of Shapes. -->
3 <!-- Shapes is free software: you can redistribute it and/or modify -->
4 <!-- it under the terms of the GNU General Public License as published by -->
5 <!-- the Free Software Foundation, either version 3 of the License, or -->
6 <!-- any later version. -->
8 <!-- Shapes is distributed in the hope that it will be useful, -->
9 <!-- but WITHOUT ANY WARRANTY; without even the implied warranty of -->
10 <!-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -->
11 <!-- GNU General Public License for more details. -->
13 <!-- You should have received a copy of the GNU General Public License -->
14 <!-- along with Shapes. If not, see <http://www.gnu.org/licenses/>. -->
16 <!-- Copyright 2008, 2010, 2013, 2015 Henrik Tidefelt -->
18 <section id="types/containers">
19 <title>Container types</title>
21 <p>Values of types in this section hold values of other types.</p>
24 <coretype name="ConsPair">
28 <p>Type of <field name="car"/> field.</p>
33 <p>Type of <field name="cdr"/> field.</p>
38 <p>Container for two values. Often used to form linked lists and trees.</p>
48 <p>Type of fold sum.</p>
51 <template-state name="S">
58 <type-field name="car">
59 <type><parameter-type name="A"/></type>
60 <description><p>First element.</p></description>
62 <type-field name="cdr">
63 <type><parameter-type name="B"/></type>
64 <description><p>Second element.</p></description>
66 <type-method name="foldl">
74 <arg><type><template-type name="T" /></type></arg>
75 <arg><type><parameter-type name="A" /></type></arg>
78 <type><template-type name="T" /></type>
83 <arg identifier="zero"><type><template-type name="T" /></type></arg>
86 <type><template-type name="T" /></type>
89 <p>Performs a left fold, assuming that the <field name="cdr" /> field also has the same method. The following code would be a valid <str-Shapes /> implementation of the method.
91 <![CDATA[\ op zero → [cdr.foldl op [op zero car]]]]>
97 <type-method name="foldr">
101 <arg identifier="op">
105 <arg><type><parameter-type name="A" /></type></arg>
106 <arg><type><template-type name="T" /></type></arg>
109 <type><template-type name="T" /></type>
114 <arg identifier="zero"><type><template-type name="T" /></type></arg>
117 <type><template-type name="T" /></type>
120 <p>Performs a right fold, assuming that the <field name="cdr" /> field also has the same method. The following code would be a valid <str-Shapes /> implementation of the method.
122 <![CDATA[\ op zero → [op car [cdr.foldr op zero]]]]>
128 <type-method name="foldsl">
132 <arg identifier="op">
136 <arg><type><template-type name="T" /></type></arg>
137 <arg><type><parameter-type name="A" /></type></arg>
138 <state><type><template-state-type name="S" /></type></state>
141 <type><template-type name="T" /></type>
146 <arg identifier="zero"><type><template-type name="T" /></type></arg>
147 <state identifier="state"><type><template-state-type name="S" /></type></state>
150 <type><template-type name="T" /></type>
153 <p>Performs a left fold with state, assuming that the <field name="cdr" /> field also has the same method. The following code would be a valid <str-Shapes /> implementation of the method.
155 <![CDATA[\ op zero •st → [cdr.foldsl op [op zero car •st] •st]]]>
161 <type-method name="foldsr">
165 <arg identifier="op">
169 <arg><type><parameter-type name="A" /></type></arg>
170 <arg><type><template-type name="T" /></type></arg>
171 <state><type><template-state-type name="S" /></type></state>
174 <type><template-type name="T" /></type>
179 <arg identifier="zero"><type><template-type name="T" /></type></arg>
180 <state identifier="state"><type><template-state-type name="S" /></type></state>
183 <type><template-type name="T" /></type>
186 <p>Performs a right fold with state, assuming that the <field name="cdr" /> field also has the same method. The following code would be a valid <str-Shapes /> implementation of the method.
188 <![CDATA[\ op zero •st → [op car [cdr.foldsr op zero •st] •st]]]>
196 <p>When used to build linked lists, one shall use <binding name="nil" /> to terminate the list. Recall that this value is recognized by <binding name="nil?" />.</p>
197 <p>The following example illustrates the extension <a extension="seq-support" /> and how the laziness of <binding name="cons" /> allows infinite streams to be defined.</p>
199 <example-with-output title="Folds" internal-id="example/folds">
200 <source file="features/folds.shape">
201 <![CDATA[<!--#include depth="0" virtual="$(BUILDDIR)$(EXAMPLES)features/folds.shape" -->]]>
204 <![CDATA[<!--#include depth="0" virtual="$(BUILDDIR)$(EXAMPLES)features/folds.stdout" -->]]>
207 <p>Folds over lists come in two flavors — left and right. <named-type name="ConsPair" /> can hold thunks, but infinite streams cannot be folded.</p>
209 </example-with-output>
214 <coretype name="Seq">
218 <p>Type of elements in list.</p>
223 <p>A singly linked list.</p>
226 <binding name="nil?" />
230 <parameterized-type name="SeqPair">
231 <parameter-type name="E"/>
232 </parameterized-type>
233 <named-type name="SeqNil" />
238 <coretype name="SeqPair">
240 <parameterized-type name="Seq"><parameter-type name="E" /></parameterized-type>
245 <p>Type of elements in list.</p>
250 <p>A non-empty singly linked list.</p>
254 <parameterized-type name="ConsPair">
255 <parameter-type name="E"/>
256 <parameterized-type name="Seq">
257 <parameter-type name="E"/>
258 </parameterized-type>
259 </parameterized-type>
264 <coretype name="SeqNil">
266 <parameterized-type name="Seq"><parameter-type name="E" /></parameterized-type>
269 <p>An empty singly linked list.</p>
272 <binding name="nil?" />
275 <binding name="list" />
280 <p>Type of fold sum.</p>
283 <template-state name="S">
290 <type-method name="foldl">
294 <arg identifier="op"></arg>
295 <arg identifier="zero"><type><template-type name="T" /></type></arg>
298 <type><template-type name="T" /></type>
301 <p>Just returns <arg name="zero"/>.</p>
306 <type-method name="foldr">
310 <arg identifier="op"></arg>
311 <arg identifier="zero"><type><template-type name="T" /></type></arg>
314 <type><template-type name="T" /></type>
317 <p>Just returns <arg name="zero"/>.</p>
322 <type-method name="foldsl">
326 <arg identifier="op"></arg>
327 <arg identifier="zero"><type><template-type name="T" /></type></arg>
328 <state identifier="state"><type><template-state-type name="S" /></type></state>
331 <type><template-type name="T" /></type>
334 <p>Just returns <arg name="zero"/>.</p>
339 <type-method name="foldsr">
343 <arg identifier="op"></arg>
344 <arg identifier="zero"><type><template-type name="T" /></type></arg>
345 <state identifier="state"><type><template-state-type name="S" /></type></state>
348 <type><template-type name="T" /></type>
351 <p>Just returns <arg name="zero"/>.</p>
358 <p>The following example illustrates the extension <extension path="..Shapes..Data/seq-support" /> and how the laziness of <value name="cons" /> <example-with-output title="Folds" internal-id="example:folds">
359 <source file="features/folds.blank">
360 <![CDATA[<!--#include depth="0" virtual="$(BUILDDIR)$(EXAMPLES)features/folds.blank" -->]]>
363 <![CDATA[<!--#include depth="0" virtual="$(BUILDDIR)$(EXAMPLES)features/folds.stdout" -->]]>
366 <p>Folds over lists come in two flavors — left and right. <named-type name="ConsPair" /> can hold thunks, but infinite streams cannot be folded.</p>
368 </example-with-output>
373 <coretype name="Array">
375 <p>Values of type <self /> are one-dimensional arrays. The array has a finite size, does not contain any thunks, and any element can be accessed in constant time by specifying its index. The index of the first element is <eq>0</eq>.</p>
380 <arg><type><named-type name="Integer" /></type></arg>
382 <result><named-type name="Value" /></result>
386 <value name="newArray" />
391 <p>Type of fold sum.</p>
396 <p>Type of list elements.</p>
399 <template-state name="S">
406 <type-field name="size">
407 <type><named-type name="Integer" /></type>
408 <description><p>The size of the array.</p></description>
410 <type-field name="range">
411 <type><named-type name="Seq" /></type>
413 <p>Range of valid indices, from low to high. Constant time and space implementation. Efficient for left folds; instead of a right fold over <field name="range" />, one should try to make a left fold over <field name="reverse_range" /> instead.</p>
414 <p>For <inline>arr</inline> of type <self />, <inline>arr.range</inline> is equivalent to
416 <![CDATA[[range '0 count:arr.size]]]>
421 <type-field name="reverse_range">
422 <type><named-type name="Seq" /></type>
424 <p>Reverse range of valid indices, from high to low. Constant time and space implementation. Efficient for left folds; instead of a right fold over <field name="reverse_range" />, one should try to make a left fold over <field name="range" /> instead.</p>
425 <p>For <inline>arr</inline> of type <self />, <inline>arr.reverse_range</inline> is equivalent to
427 <![CDATA[[range arr.size-'1 step:'~1 count:arr.size]]]>
432 <type-field name="list">
433 <type><named-type name="Seq" /></type>
434 <description><p>Converts the array to a list.</p></description>
436 <type-method name="foldl">
440 <arg identifier="op">
444 <arg><type><template-type name="T" /></type></arg>
445 <arg><type><template-type name="E" /></type></arg>
448 <type><template-type name="T" /></type>
453 <arg identifier="zero"><type><template-type name="T" /></type></arg>
456 <type><template-type name="T" /></type>
459 <p>Performs a left fold over the array.</p>
464 <type-method name="foldr">
468 <arg identifier="op">
472 <arg><type><template-type name="E" /></type></arg>
473 <arg><type><template-type name="T" /></type></arg>
476 <type><template-type name="T" /></type>
481 <arg identifier="zero"><type><template-type name="T" /></type></arg>
484 <type><template-type name="T" /></type>
487 <p>Performs a right fold over the array.</p>
492 <type-method name="foldsl">
496 <arg identifier="op">
500 <arg><type><template-type name="T" /></type></arg>
501 <arg><type><template-type name="E" /></type></arg>
502 <state><type><template-state-type name="S" /></type></state>
505 <type><template-type name="T" /></type>
510 <arg identifier="zero"><type><template-type name="T" /></type></arg>
511 <state identifier="state"><type><template-state-type name="S" /></type></state>
514 <type><template-type name="T" /></type>
517 <p>Performs a left fold with state over the array.</p>
522 <type-method name="foldsr">
526 <arg identifier="op">
530 <arg><type><template-type name="T" /></type></arg>
531 <arg><type><template-type name="E" /></type></arg>
532 <state><type><template-state-type name="S" /></type></state>
535 <type><template-type name="T" /></type>
540 <arg identifier="zero"><type><template-type name="T" /></type></arg>
541 <state identifier="state"><type><template-state-type name="S" /></type></state>
544 <type><template-type name="T" /></type>
547 <p>Performs a right fold with state over the array.</p>
555 <coretype name="Structure">
557 <p>Container mirroring the information being passed to the callee in a function application. That is, it may contain both named and ordered fields.</p>
560 <syntax name="structure" />
563 <!-- Having an empty <fields> tag will generate an empty list of fields, rather than the text "A value of type ... has no fields."-->
566 <p>The contents of a <named-type name="Structure" /> value can be passed to a function using the <syntax name="split-call" />. The named fields of a structure may also be accessed just like the named fields of any other compound value. Ordered values may be accessed using trivial helper functions (simply returning the appropriate ordered argument).</p>
567 <p>For an example showing the various ways to work with <named-type name="Structure" /> values, see the <syntax name="structure" /> syntax.</p>