Skip to content

Commit f172b69

Browse files
authored
Documentation: streamline Source specs (#1648)
* Documentation: fixes #1497 and more * work in progress
1 parent 06d7f04 commit f172b69

20 files changed

+118
-139
lines changed

docs/lib/array.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
/**
2-
* returns
2+
* **primitive**; returns
33
* the current length of array <CODE>x</CODE>, which is 1 plus the
44
* highest index that has been used so far in an array assignment on
55
* <CODE>x</CODE>. Here literal array expressions are counted too: The
66
* array <CODE>[10, 20, 30]</CODE> has a length of 3.
7+
* Time: <CODE>Θ(1)</CODE>, space: <CODE>Θ(1)</CODE>
78
* @param {array} x - given array
89
* @returns {number} current length of array
910
*/
1011
function array_length(x) {}
1112

1213
/**
13-
* returns <CODE>true</CODE> if <CODE>x</CODE>
14+
* **primitive**; returns <CODE>true</CODE> if <CODE>x</CODE>
1415
* is an array, and <CODE>false</CODE> if it is not.
16+
* Time: <CODE>Θ(1)</CODE>, space: <CODE>Θ(1)</CODE>
1517
* @param {value} x - given value
1618
* @returns {boolean} whether <CODE>x</CODE> is an array
1719
*/

docs/lib/list.js

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,70 @@
11
// \texttt{list.js START} \begin{lstlisting}
22

33
/**
4-
* makes a pair whose head (first component) is <CODE>x</CODE>
5-
* and whose tail (second component) is <CODE>y</CODE>.
4+
* **primitive**; makes a pair whose head (first component) is <CODE>x</CODE>
5+
* and whose tail (second component) is <CODE>y</CODE>; time: <CODE>Θ(1)</CODE, space: <CODE>Θ(1)</CODE>.
66
* @param {value} x - given head
77
* @param {value} y - given tail
88
* @returns {pair} pair with <CODE>x</CODE> as head and <CODE>y</CODE> as tail.
99
*/
1010
function pair(x, y) {}
1111

1212
/**
13-
* returns <CODE>true</CODE> if <CODE>x</CODE> is a
14-
* pair and false otherwise.
13+
* **primitive**; returns <CODE>true</CODE> if <CODE>x</CODE> is a
14+
* pair and false otherwise; time: <CODE>Θ(1)</CODE, space: <CODE>Θ(1)</CODE>.
1515
* @param {value} x - given value
1616
* @returns {boolean} whether <CODE>x</CODE> is a pair
1717
*/
1818
function is_pair(x) {}
1919

2020
/**
21-
* returns head (first component) of given pair <CODE>p</CODE>
21+
* **primitive**; returns head (first component) of given pair <CODE>p</CODE>; time: <CODE>Θ(1)</CODE, space: <CODE>Θ(1)</CODE>.
2222
* @param {pair} p - given pair
2323
* @returns {value} head of <CODE>p</CODE>
2424
*/
2525
function head(p) {}
2626

2727
/**
28-
* returns tail (second component of given pair <CODE>p</CODE>
28+
* **primitive**; returns tail (second component of given pair <CODE>p</CODE>; time: <CODE>Θ(1)</CODE, space: <CODE>Θ(1)</CODE>.
2929
* @param {pair} p - given pair
3030
* @returns {value} tail of <CODE>p</CODE>
3131
*/
3232
function tail(p) {}
3333

3434
/**
35-
* returns <CODE>true</CODE> if <CODE>x</CODE> is the
36-
* empty list <CODE>null</CODE>, and <CODE>false</CODE> otherwise.
35+
* **primitive**; returns <CODE>true</CODE> if <CODE>x</CODE> is the
36+
* empty list <CODE>null</CODE>, and <CODE>false</CODE> otherwise; time: <CODE>Θ(1)</CODE, space: <CODE>Θ(1)</CODE>.
3737
* @param {value} x - given value
3838
* @returns {boolean} whether <CODE>x</CODE> is <CODE>null</CODE>
3939
*/
4040
function is_null(x) {}
4141

4242
/**
43-
* Returns <CODE>true</CODE> if
43+
* **primitive**; returns <CODE>true</CODE> if
4444
* <CODE>xs</CODE> is a list as defined in the textbook, and
4545
* <CODE>false</CODE> otherwise. Iterative process;
46-
* time: <CODE>O(n)</CODE>, space: <CODE>O(1)</CODE>, where <CODE>n</CODE>
46+
* time: <CODE>Θ(n)</CODE>, space: <CODE>Θ(1)</CODE>, where <CODE>n</CODE>
4747
* is the length of the
4848
* chain of <CODE>tail</CODE> operations that can be applied to <CODE>xs</CODE>.
49-
* recurses down the list and checks that it ends with the empty list null
49+
* <CODE>is_list</CODE> recurses down the list and checks that it ends with the empty list null
5050
* @param {value} xs - given candidate
5151
* @returns whether {xs} is a list
5252
*/
5353
function is_list(xs) {}
5454

5555
/**
56-
* Given <CODE>n</CODE> values, returns a list of length <CODE>n</CODE>.
57-
* The elements of the list are the given values in the given order.
56+
* **primitive**; given <CODE>n</CODE> values, returns a list of length <CODE>n</CODE>.
57+
* The elements of the list are the given values in the given order; time: <CODE>Θ(n)</CODE, space: <CODE>Θ(n)</CODE>.
5858
* @param {value} value1,value2,...,value_n - given values
5959
* @returns {list} list containing all values
6060
*/
6161
function list(value1, value2, ...values ) {}
6262

6363
/**
64-
* visualizes <CODE>x</CODE> in a separate drawing
65-
* area in the Source Academy using a box-and-pointer diagram; time, space:
66-
* O(n), where n is the total number of data structures such as
67-
* pairs in all the separate structures provided in <CODE>x</CODE>.
64+
* visualizes the arguments in a separate drawing
65+
* area in the Source Academy using box-and-pointer diagrams; time, space:
66+
* <CODE>Θ(n)</CODE>, where <CODE>n</CODE> is the total number of data structures such as
67+
* pairs in the arguments.
6868
* @param {value} value1,value2,...,value_n - given values
6969
* @returns {value} given <CODE>x</CODE>
7070
*/
@@ -80,9 +80,10 @@ function list(value1, value2, ...values ) {}
8080
* need to be the same. If both are <CODE>undefined</CODE> or both are
8181
* <CODE>null</CODE>, the result is <CODE>true</CODE>. Otherwise they are compared
8282
* with <CODE>===</CODE> (using the definition of <CODE>===</CODE> in the
83-
* respective Source language in use). Time, space:
84-
* <CODE>O(n)</CODE>, where <CODE>n</CODE> is the number of pairs in
85-
* <CODE>x</CODE>.
83+
* respective Source language in use).
84+
* Time, space:
85+
* <CODE>Θ(n)</CODE>, where <CODE>n</CODE> is the total number of data structures such as
86+
* pairs in <CODE>x</CODE> and <CODE>y</CODE>.
8687
* @param {value} x - given value
8788
* @param {value} y - given value
8889
* @returns {boolean} whether <CODE>x</CODE> is structurally equal to <CODE>y</CODE>
@@ -109,8 +110,8 @@ function equal(xs, ys) {
109110
/**
110111
* Returns the length of the list
111112
* <CODE>xs</CODE>.
112-
* Iterative process; time: <CODE>O(n)</CODE>, space:
113-
* <CODE>O(1)</CODE>, where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
113+
* Iterative process; time: <CODE>Θ(n)</CODE>, space:
114+
* <CODE>Θ(1)</CODE>, where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
114115
* @param {list} xs - given list
115116
* @returns {number} length of <CODE>xs</CODE>
116117
*/
@@ -124,8 +125,8 @@ function $length(xs, acc) {
124125
/**
125126
* Returns a list that results from list
126127
* <CODE>xs</CODE> by element-wise application of unary function <CODE>f</CODE>.
127-
* Iterative process; time: <CODE>O(n)</CODE>,
128-
* space: <CODE>O(n)</CODE>, where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
128+
* Iterative process; time: <CODE>Θ(n)</CODE> (apart from <CODE>f</CODE>),
129+
* space: <CODE>Θ(n)</CODE> (apart from <CODE>f</CODE>), where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
129130
* <CODE>f</CODE> is applied element-by-element:
130131
* <CODE>map(f, list(1, 2))</CODE> results in <CODE>list(f(1), f(2))</CODE>.
131132
* @param {function} f - unary
@@ -146,7 +147,7 @@ function $map(f, xs, acc) {
146147
* Makes a list with <CODE>n</CODE>
147148
* elements by applying the unary function <CODE>f</CODE>
148149
* to the numbers 0 to <CODE>n - 1</CODE>, assumed to be a nonnegative integer.
149-
* Iterative process; time: <CODE>O(n)</CODE>, space: <CODE>O(n)</CODE>.
150+
* Iterative process; time: <CODE>Θ(n)</CODE> (apart from <CODE>f</CODE>), space: <CODE>Θ(n)</CODE> (apart from <CODE>f</CODE>).
150151
* @param {function} f - unary function
151152
* @param {number} n - given nonnegative integer
152153
* @returns {list} resulting list
@@ -161,8 +162,8 @@ function $build_list(i, fun, already_built) {
161162
/**
162163
* Applies unary function <CODE>f</CODE> to every
163164
* element of the list <CODE>xs</CODE>.
164-
* Iterative process; time: <CODE>O(n)</CODE>, space: <CODE>O(1)</CODE>,
165-
* Where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
165+
* Iterative process; time: <CODE>Θ(n)</CODE> (apart from <CODE>f</CODE>), space: <CODE>Θ(1)</CODE> (apart from <CODE>f</CODE>),
166+
* where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
166167
* <CODE>f</CODE> is applied element-by-element:
167168
* <CODE>for_each(fun, list(1, 2))</CODE> results in the calls
168169
* <CODE>fun(1)</CODE> and <CODE>fun(2)</CODE>.
@@ -185,6 +186,9 @@ function for_each(fun, xs) {
185186
* Returns a string that represents
186187
* list <CODE>xs</CODE> using the text-based box-and-pointer notation
187188
* <CODE>[...]</CODE>.
189+
* Iterative process; time: <CODE>Θ(n)</CODE> where <CODE>n</CODE> is the size of the list, space: <CODE>Θ(m)</CODE> where <CODE>m</CODE> is the length of the string.
190+
* The process is iterative, but consumes space <CODE>O(m)</CODE>
191+
* because of the result string.
188192
* @param {list} xs - given list
189193
* @returns {string} <CODE>xs</CODE> converted to string
190194
*/
@@ -205,9 +209,9 @@ function $list_to_string(xs, cont) {
205209

206210
/**
207211
* Returns list <CODE>xs</CODE> in reverse
208-
* order. Iterative process; time: <CODE>O(n)</CODE>,
209-
* space: <CODE>O(n)</CODE>, where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
210-
* The process is iterative, but consumes space <CODE>O(n)</CODE>
212+
* order. Iterative process; time: <CODE>Θ(n)</CODE>,
213+
* space: <CODE>Θ(n)</CODE>, where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
214+
* The process is iterative, but consumes space <CODE>Θ(n)</CODE>
211215
* because of the result list.
212216
* @param {list} xs - given list
213217
* @returns {list} <CODE>xs</CODE> in reverse
@@ -224,8 +228,8 @@ function $reverse(original, reversed) {
224228
/**
225229
* Returns a list that results from
226230
* appending the list <CODE>ys</CODE> to the list <CODE>xs</CODE>.
227-
* Iterative process; time: <CODE>O(n)</CODE>, space:
228-
* <CODE>O(n)</CODE>, where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
231+
* Iterative process; time: <CODE>Θ(n)</CODE>, space:
232+
* <CODE>Θ(n)</CODE>, where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
229233
* In the result, null at the end of the first argument list
230234
* is replaced by the second argument, regardless what the second
231235
* argument consists of.
@@ -247,8 +251,8 @@ function $append(xs, ys, cont) {
247251
* whose head is identical to
248252
* <CODE>v</CODE> (using <CODE>===</CODE>); returns <CODE>null</CODE> if the
249253
* element does not occur in the list.
250-
* Iterative process; time: <CODE>O(n)</CODE>,
251-
* space: <CODE>O(1)</CODE>, where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
254+
* Iterative process; time: <CODE>Θ(n)</CODE>,
255+
* space: <CODE>Θ(1)</CODE>, where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
252256
* @param {value} v - given value
253257
* @param {list} xs - given list
254258
* @returns {list} postfix sublist that starts with <CODE>v</CODE>
@@ -266,7 +270,7 @@ function member(v, xs) {
266270
* is identical (<CODE>===</CODE>) to <CODE>v</CODE>.
267271
* Returns the original
268272
* list if there is no occurrence. Iterative process;
269-
* time: <CODE>O(n)</CODE>, space: <CODE>O(n)</CODE>, where <CODE>n</CODE>
273+
* time: <CODE>Θ(n)</CODE>, space: <CODE>Θ(n)</CODE>, where <CODE>n</CODE>
270274
* is the length of <CODE>xs</CODE>.
271275
* @param {value} v - given value
272276
* @param {list} xs - given list
@@ -290,7 +294,7 @@ function $remove(v, xs, acc) {
290294
* Returns the original
291295
* list if there is no occurrence.
292296
* Iterative process;
293-
* time: <CODE>O(n)</CODE>, space: <CODE>O(n)</CODE>, where <CODE>n</CODE>
297+
* time: <CODE>Θ(n)</CODE>, space: <CODE>Θ(n)</CODE>, where <CODE>n</CODE>
294298
* is the length of <CODE>xs</CODE>.
295299
* @param {value} v - given value
296300
* @param {list} xs - given list
@@ -313,7 +317,7 @@ function $remove_all(v, xs, acc) {
313317
* <CODE>pred</CODE>
314318
* returns <CODE>true</CODE>.
315319
* Iterative process;
316-
* time: <CODE>O(n)</CODE>, space: <CODE>O(n)</CODE>,
320+
* time: <CODE>Θ(n)</CODE> (apart from <CODE>pred</CODE>), space: <CODE>Θ(n)</CODE> (apart from <CODE>pred</CODE>),
317321
* where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
318322
* @param {function} pred - unary function returning boolean value
319323
* @param {list} xs - given list
@@ -335,7 +339,7 @@ function $filter(pred, xs, acc) {
335339
* numbers starting from <CODE>start</CODE> using a step size of 1, until
336340
* the number exceeds (<CODE>&gt;</CODE>) <CODE>end</CODE>.
337341
* Iterative process;
338-
* time: <CODE>O(n)</CODE>, space: <CODE>O(n)</CODE>,
342+
* time: <CODE>Θ(n)</CODE>, space: <CODE>Θ(n)</CODE>,
339343
* where <CODE>n</CODE> is <CODE>end - start</CODE>.
340344
* @param {number} start - starting number
341345
* @param {number} end - ending number
@@ -355,7 +359,7 @@ function $enum_list(start, end, acc) {
355359
* of list <CODE>xs</CODE> at position <CODE>n</CODE>,
356360
* where the first element has index 0.
357361
* Iterative process;
358-
* time: <CODE>O(n)</CODE>, space: <CODE>O(1)</CODE>,
362+
* time: <CODE>Θ(n)</CODE>, space: <CODE>Θ(1)</CODE>,
359363
* where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
360364
* @param {list} xs - given list
361365
* @param {number} n - given position
@@ -380,9 +384,8 @@ function list_ref(xs, n) {
380384
* list. Thus, <CODE>accumulate(f,zero,list(1,2,3))</CODE> results in
381385
* <CODE>f(1, f(2, f(3, zero)))</CODE>.
382386
* Iterative process;
383-
* time: <CODE>O(n)</CODE>, space: <CODE>O(n)</CODE>,
384-
* where <CODE>n</CODE> is the length of <CODE>xs</CODE>
385-
* assuming <CODE>f</CODE> takes constant time.
387+
* time: <CODE>Θ(n)</CODE> (apart from <CODE>f</CODE>), space: <CODE>Θ(n)</CODE> (apart from <CODE>f</CODE>),
388+
* where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
386389
* @param {function} f - binary function
387390
* @param {value} initial - initial value
388391
* @param {list} xs - given list
@@ -399,8 +402,11 @@ function $accumulate(f, initial, xs, cont) {
399402

400403

401404
/**
402-
* Optional second argument.
403-
* Similar to <CODE>display</CODE>, but formats well-formed lists nicely if detected.
405+
* Optional second argument.
406+
* Similar to <CODE>display</CODE>, but formats well-formed lists nicely if detected;
407+
* time, space:
408+
* <CODE>Θ(n)</CODE>, where <CODE>n</CODE> is the total number of data structures such as
409+
* pairs in <CODE>x</CODE>.
404410
* @param {value} xs - list structure to be displayed
405411
* @param {string} s to be displayed, preceding <CODE>xs</CODE>
406412
* @returns {value} xs, the first argument value

docs/lib/stream.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,8 @@ function stream_tail(xs) {
3535
/**
3636
* Returns <CODE>true</CODE> if
3737
* <CODE>xs</CODE> is a stream as defined in the textbook, and
38-
* <CODE>false</CODE> otherwise. Iterative process;
39-
* time: <CODE>O(n)</CODE>, space: <CODE>O(1)</CODE>, where <CODE>n</CODE>
40-
* is the length of the
41-
* chain of <CODE>stream_tail</CODE> operations that can be applied to <CODE>xs</CODE>.
42-
* recurses down the stream and checks that it ends with the empty stream null.
38+
* <CODE>false</CODE> otherwise. Iterative process.
39+
* Recurses down the stream and checks that it ends with the empty stream null.
4340
* Laziness: No: <CODE>is_stream</CODE> needs to force the given stream.
4441
* @param {value} xs - given candidate
4542
* @returns {boolean} whether <CODE>xs</CODE> is a stream
@@ -107,8 +104,7 @@ function stream() {
107104
/**
108105
* Returns the length of the stream
109106
* <CODE>xs</CODE>.
110-
* Iterative process; time: <CODE>O(n)</CODE>, space:
111-
* <CODE>O(1)</CODE>, where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
107+
* Iterative process.
112108
* Lazy? No: The function needs to explore the whole stream
113109
* @param {stream} xs - given stream
114110
* @returns {number} length of <CODE>xs</CODE>
@@ -165,8 +161,7 @@ function build_stream(fun, n) {
165161
/**
166162
* Applies unary function <CODE>f</CODE> to every
167163
* element of the stream <CODE>xs</CODE>.
168-
* Iterative process; time: <CODE>O(n)</CODE>, space: <CODE>O(1)</CODE>,
169-
* Where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
164+
* Iterative process.
170165
* <CODE>f</CODE> is applied element-by-element:
171166
* <CODE>stream_for_each(f, stream(1, 2))</CODE> results in the calls
172167
* <CODE>f(1)</CODE> and <CODE>f(2)</CODE>.
@@ -188,9 +183,8 @@ function stream_for_each(fun, xs) {
188183

189184
/**
190185
* Returns stream <CODE>xs</CODE> in reverse
191-
* order. Iterative process; time: <CODE>O(n)</CODE>,
192-
* space: <CODE>O(n)</CODE>, where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
193-
* The process is iterative, but consumes space <CODE>O(n)</CODE>
186+
* order. Iterative process.
187+
* The process is iterative, but consumes space <CODE>Ω(n)</CODE>
194188
* because of the result stream.
195189
* Lazy? No: <CODE>stream_reverse</CODE>
196190
* forces the exploration of the entire stream
@@ -232,8 +226,7 @@ function stream_append(xs, ys) {
232226
* whose head is identical to
233227
* <CODE>v</CODE> (using <CODE>===</CODE>); returns <CODE>null</CODE> if the
234228
* element does not occur in the stream.
235-
* Iterative process; time: <CODE>O(n)</CODE>,
236-
* space: <CODE>O(1)</CODE>, where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
229+
* Iterative process.
237230
* Lazy? Sort-of: <CODE>stream_member</CODE>
238231
* forces the stream only until the element
239232
* is found.
@@ -375,9 +368,7 @@ function eval_stream(s, n) {
375368
* Returns the element
376369
* of stream <CODE>xs</CODE> at position <CODE>n</CODE>,
377370
* where the first element has index 0.
378-
* Iterative process;
379-
* time: <CODE>O(n)</CODE>, space: <CODE>O(1)</CODE>,
380-
* where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
371+
* Iterative process.
381372
* Lazy? Sort-of: <CODE>stream_ref</CODE> only forces the computation of
382373
* the first <CODE>n</CODE> elements, and leaves the rest of
383374
* the stream untouched.

docs/specs/source_1.tex

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,10 @@
2929

3030
\input source_typing
3131

32-
\section{Standard Libraries}
32+
\input source_standard
3333

34-
The following libraries are always available in this language.
35-
36-
\input source_misc
37-
38-
\input source_math
34+
The standard library for Source \S\ 1 is documented in
35+
\href{}{online documentation of Source \S\ 1}.
3936

4037
\input source_js_differences
4138

docs/specs/source_1_lazy.tex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ \section{Changes}
5454

5555
\input source_typing
5656

57-
\section{Standard Libraries}
58-
59-
The following libraries are always available in this language.
57+
\input source_standard
6058

6159
\input source_misc
6260

docs/specs/source_1_typed.tex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@
3333

3434
\input source_typing
3535

36-
\section{Standard Libraries}
37-
38-
The following libraries are always available in this language.
36+
\input source_standard
3937

4038
\input source_misc
4139

docs/specs/source_2.tex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ \section{Changes}
3737

3838
\input source_typing
3939

40-
\section{Standard Libraries}
41-
42-
The following libraries are always available in this language.
40+
\input source_standard
4341

4442
\input source_misc
4543

0 commit comments

Comments
 (0)