aboutsummaryrefslogtreecommitdiff
path: root/docs/spec/system.html
blob: 5181867eae0ef5d2f54b06544f4d6932459264f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<head>
  <link href="../favicon.ico" rel="shortcut icon" type="image/x-icon"/>
  <link href="../style.css" rel="stylesheet"/>
  <title>Specification: BQN system-provided values</title>
</head>
<div class="nav"><a href="https://github.com/mlochbaum/BQN">BQN</a> / <a href="../index.html">main</a> / <a href="index.html">spec</a></div>
<h1 id="specification-bqn-system-provided-values">Specification: BQN system-provided values</h1>
<p>This portion of the spec is still potentially subject to major changes.</p>
<p>The <code><span class='Value'></span></code> symbol is used to access values other than primitives provided by BQN.</p>
<p>All system values described in the BQN specification are optional: an implementation does not have to include any of them. However, if a system value with one of the names given below is included, then it must have the specified behavior. For namespaces this rule applies to individual fields as well: a namespace may be provided with only some of the fields, but a field with one of the given names must behave as specified.</p>
<h2 id="execution-and-scope-manipulation">Execution and scope manipulation</h2>
<table>
<thead>
<tr>
<th>Name</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><span class='Function'>•BQN</span></code></td>
<td>Evaluate the argument string in an isolated scope</td>
</tr>
<tr>
<td><code><span class='Function'>•Eval</span></code></td>
<td>Evaluate the argument string in the current scope</td>
</tr>
<tr>
<td><code><span class='Function'>•ScopedEval</span></code></td>
<td>Evaluate the argument string in a child scope</td>
</tr>
<tr>
<td><code><span class='Function'>•Using</span></code></td>
<td>Import all values from the argument namespace</td>
</tr>
</tbody>
</table>
<p>The effect of <code><span class='Function'>•Eval</span></code> should be the same as if its argument were written as source code in the scope where <code><span class='Function'>•Eval</span></code> appears. It can define variables, and modify those in the current scope or a parent.</p>
<p><code><span class='Function'>•ScopedEval</span></code> creates as new scope for evaluation as it is loaded. Other than its syntactic role, it is effectively equivalent to <code><span class='Brace'>{</span><span class='Function'>•Eval</span><span class='Brace'>}</span></code>. Parent scopes are visible from the created scope; to make a scope without this property use <code><span class='Function'>•BQN</span><span class='String'>&quot;•Eval&quot;</span></code> or <code><span class='Function'>•BQN</span><span class='String'>&quot;•ScopedEval&quot;</span></code>.</p>
<h2 id="scripts">Scripts</h2>
<table>
<thead>
<tr>
<th>Name</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><span class='Function'>•Import</span></code></td>
<td>Load a script file</td>
</tr>
<tr>
<td><code><span class='Value'>•args</span></code></td>
<td>Arguments passed to current file</td>
</tr>
<tr>
<td><code><span class='Value'>•path</span></code></td>
<td>Current file's path</td>
</tr>
<tr>
<td><code><span class='Value'>•name</span></code></td>
<td>Current filename</td>
</tr>
</tbody>
</table>
<p><code><span class='Function'>•Import</span></code> loads another BQN script. The script is evaluated in its own isolated scope, and its result is either the result of the last line, or a module if it exports with <code><span class='Gets'></span></code> at the top level. If it is a module, then it must be destructured immediately unless first-class namespaces are possible.</p>
<p>The right argument is a filename, which may be relative or absolute. Relative paths are taken relative to the source file where this instance of <code><span class='Function'>•Import</span></code> was written. The left argument, if given, is the list of arguments that should be passed through to the file as <code><span class='Value'>•args</span></code>. If no left argument is given then <code><span class='Bracket'>⟨⟩</span></code> is used for <code><span class='Value'>•args</span></code>. However, the behavior is different in this case. The same file will only be loaded once in a given BQN program by <code><span class='Function'>•Import</span></code> calls with no left argument: the first such call saves the returned value, even if it is mutable, and subsequent calls return this saved value. To avoid this and reload the file, pass a left argument of <code><span class='Bracket'>⟨⟩</span></code>.</p>
<p><code><span class='Value'>•args</span></code> is the arguments passed as the file was invoked, either from the command line or <code><span class='Function'>•Import</span></code>. For command line calls it is a list of strings.</p>
<p><code><span class='Value'>•path</span></code> simply gives the path of the file in which it appears. It includes a trailing slash but not the name of the file itself.</p>
<p><code><span class='Value'>•name</span></code> gives the name, including the extension, of the file in which it appears. It doesn't include the path.</p>
<h2 id="files">Files</h2>
<p>The system namespace value <code><span class='Value'>•file</span></code> deals with file operations. For the purposes of <code><span class='Value'>•file</span></code>, paths in the filesystem are always strings. As with <code><span class='Function'>•Import</span></code>, file paths may be relative or absolute, and relative paths are relative to <code><span class='Value'>•path</span></code>, except in <code><span class='Value'>•file.</span><span class='Function'>At</span></code> which allows <code><span class='Value'>𝕨</span></code> to specify an alternate base directory. The value <code><span class='Value'>•path</span></code> used for a particular instance of <code><span class='Value'>•file</span></code> is determined by the file that contains that instance.</p>
<p>When a <code><span class='Value'>•file</span></code> function returns a file path or portion of a path, the path is always absolute and canonical, with <code><span class='Value'>.</span></code> and <code><span class='Value'>..</span></code> components removed.</p>
<p>Possible fields of <code><span class='Value'>•file</span></code> are given in the subsections below.</p>
<h3 id="file-paths">File paths</h3>
<p>The following functions manipulate paths and don't access files. Each takes a relative or absolute path <code><span class='Value'>𝕩</span></code>, and <code><span class='Function'>At</span></code> may also take a base directory <code><span class='Value'>𝕨</span></code>.</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><span class='Value'>path</span></code></td>
<td>Path of this source file, that is, <code><span class='Value'>•path</span></code></td>
</tr>
<tr>
<td><code><span class='Function'>At</span></code></td>
<td>Absolute path of file, with optional base <code><span class='Value'>𝕨</span></code></td>
</tr>
<tr>
<td><code><span class='Function'>Name</span></code></td>
<td>File name including extension</td>
</tr>
<tr>
<td><code><span class='Function'>Parent</span></code></td>
<td>Path of the containing directory, with trailing backslash</td>
</tr>
<tr>
<td><code><span class='Function'>BaseName</span></code></td>
<td>File name, with dot and extension removed</td>
</tr>
<tr>
<td><code><span class='Function'>Extension</span></code></td>
<td>File extension, including leading dot</td>
</tr>
<tr>
<td><code><span class='Function'>Parts</span></code></td>
<td>List of parent, base name, and extension</td>
</tr>
</tbody>
</table>
<h3 id="file-metadata">File metadata</h3>
<p>Metadata functions may query information about a file or directory but do not read to or write from it. Each takes a path <code><span class='Value'>𝕩</span></code>, and <code><span class='Function'>Permissions</span></code> also allows new data in <code><span class='Value'>𝕨</span></code>. The returned data in any case is the specified property.</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><span class='Function'>Exists</span></code></td>
<td><code><span class='Number'>1</span></code> if the file exists and <code><span class='Number'>0</span></code> otherwise</td>
</tr>
<tr>
<td><code><span class='Function'>Type</span></code></td>
<td><code><span class='String'>&quot;none&quot;</span></code>, <code><span class='String'>&quot;file&quot;</span></code>, or <code><span class='String'>&quot;dir&quot;</span></code></td>
</tr>
<tr>
<td><code><span class='Function'>Created</span></code></td>
<td>Time created</td>
</tr>
<tr>
<td><code><span class='Function'>Accessed</span></code></td>
<td>Time of last access</td>
</tr>
<tr>
<td><code><span class='Function'>Modified</span></code></td>
<td>Time of last modification</td>
</tr>
<tr>
<td><code><span class='Function'>Size</span></code></td>
<td>Total size in bytes</td>
</tr>
<tr>
<td><code><span class='Function'>Permissions</span></code></td>
<td>Query or set file permissions</td>
</tr>
</tbody>
</table>
<p>Times are Unix timestamps, that is, seconds since the Unix epoch, as used by <a href="#time">time</a> system values.</p>
<h3 id="file-access">File access</h3>
<p>File access functions read or write files, either by manipulating files as a whole or interacting with the contents. Whole-file functions cannot overwrite target files: that is, <code><span class='Function'>Rename</span></code> and <code><span class='Function'>Copy</span></code> must give an error if a file exists at <code><span class='Value'>𝕨</span></code>, and <code><span class='Function'>CreateDir</span></code> if a file exists at <code><span class='Value'>𝕩</span></code>, while <code><span class='Function'>Chars</span></code>, <code><span class='Function'>Lines</span></code>, and <code><span class='Function'>Bytes</span></code> can overwrite the contents of an existing file <code><span class='Value'>𝕨</span></code>. However, these three functions must give an error if <code><span class='Value'>𝕨</span></code> exists and is a directory.</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><span class='Function'>Open</span></code></td>
<td>Return an open file object based on <code><span class='Value'>𝕩</span></code></td>
</tr>
<tr>
<td><code><span class='Function'>Rename</span></code></td>
<td>Rename file <code><span class='Value'>𝕩</span></code> with path <code><span class='Value'>𝕨</span></code></td>
</tr>
<tr>
<td><code><span class='Function'>Copy</span></code></td>
<td>Copy file <code><span class='Value'>𝕩</span></code> to path <code><span class='Value'>𝕨</span></code></td>
</tr>
<tr>
<td><code><span class='Function'>CreateDir</span></code></td>
<td>Create a directory at path <code><span class='Value'>𝕩</span></code></td>
</tr>
<tr>
<td><code><span class='Function'>Remove</span></code></td>
<td>Delete file <code><span class='Value'>𝕩</span></code></td>
</tr>
<tr>
<td><code><span class='Function'>RemoveDir</span></code></td>
<td>Recursively delete directory <code><span class='Value'>𝕩</span></code> and all contents</td>
</tr>
<tr>
<td><code><span class='Function'>List</span></code></td>
<td>Return names of all files in directory <code><span class='Value'>𝕩</span></code></td>
</tr>
<tr>
<td><code><span class='Function'>Chars</span></code></td>
<td>Read from or write to entire file, as characters</td>
</tr>
<tr>
<td><code><span class='Function'>Lines</span></code></td>
<td>Read from or write to entire file, as lines</td>
</tr>
<tr>
<td><code><span class='Function'>Bytes</span></code></td>
<td>Read from or write to entire file, as bytes</td>
</tr>
</tbody>
</table>
<p><code><span class='Function'>Rename</span></code>, <code><span class='Function'>Copy</span></code>, and <code><span class='Function'>CreateDir</span></code> return the path of the new file. <code><span class='Function'>Remove</span></code> and <code><span class='Function'>RemoveDir</span></code> return <code><span class='Number'>1</span></code> to indicate successful removal (and error otherwise).</p>
<p><code><span class='Function'>List</span></code> returns filenames only, without extensions. It lists all files and directories including hidden ones, but not the current and parent directory names <code><span class='Value'>.</span></code> and <code><span class='Value'>..</span></code>.</p>
<p>Functions <code><span class='Function'>Chars</span></code>, <code><span class='Function'>Lines</span></code>, and <code><span class='Function'>Bytes</span></code> are all ambivalent. If only <code><span class='Value'>𝕩</span></code> is given, then it is a filename, and the result is the contents of the file in the appropriate format. If there are two arguments, then <code><span class='Value'>𝕨</span></code> is the filename and <code><span class='Value'>𝕩</span></code> is the desired contents. These are written to the file, overwriting its contents, and the absolute filename <code><span class='Value'>𝕨</span></code> is returned. The three formats are:</p>
<ul>
<li>Chars: BQN characters, or UTF-32. The file is assumed to be UTF-8 encoded.</li>
<li>Lines: BQN strings. The file is decoded as with chars, then split into lines by CR, LR, or CRLF line endings.</li>
<li>Bytes: Single-byte values, stored as BQN characters from <code><span class='String'>@</span></code> to <code><span class='String'>@</span><span class='Function'>+</span><span class='Number'>255</span></code>.</li>
</ul>
<p>The following short names can also be provided for file access. They can be provided, and use the definitions from above even if <code><span class='Value'>•file</span></code> is not provided.</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Equivalent</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><span class='Function'>•FChars</span></code></td>
<td><code><span class='Value'>•file.</span><span class='Function'>Chars</span></code></td>
</tr>
<tr>
<td><code><span class='Function'>•FLines</span></code></td>
<td><code><span class='Value'>•file.</span><span class='Function'>Lines</span></code></td>
</tr>
<tr>
<td><code><span class='Function'>•FBytes</span></code></td>
<td><code><span class='Value'>•file.</span><span class='Function'>Bytes</span></code></td>
</tr>
</tbody>
</table>
<h3 id="open-file-object">Open file object</h3>
<h2 id="input-and-output">Input and output</h2>
<table>
<thead>
<tr>
<th>Name</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><span class='Function'>•Out</span></code></td>
<td>Print argument string</td>
</tr>
<tr>
<td><code><span class='Function'>•Fmt</span></code></td>
<td>Format value for printing</td>
</tr>
</tbody>
</table>
<h2 id="operation-properties">Operation properties</h2>
<table>
<thead>
<tr>
<th>Name</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><span class='Function'>•Type</span></code></td>
<td>Return a number indicating type</td>
</tr>
<tr>
<td><code><span class='Function'>•Glyph</span></code></td>
<td>Return the glyph for a primitive</td>
</tr>
<tr>
<td><code><span class='Function'>•Source</span></code></td>
<td>Return the source of a block, as a string</td>
</tr>
<tr>
<td><code><span class='Function'>•Decompose</span></code></td>
<td>Show the parts of a compound function</td>
</tr>
</tbody>
</table>
<p>Each function in this section is monadic.</p>
<p><code><span class='Function'>•Type</span></code> gives its argument's type, as a number from the table below:</p>
<table>
<thead>
<tr>
<th>Number</th>
<th>Type</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>Array</td>
</tr>
<tr>
<td>1</td>
<td>Number</td>
</tr>
<tr>
<td>2</td>
<td>Character</td>
</tr>
<tr>
<td>3</td>
<td>Function</td>
</tr>
<tr>
<td>4</td>
<td>1-modifier</td>
</tr>
<tr>
<td>5</td>
<td>2-modifier</td>
</tr>
<tr>
<td>6</td>
<td>Namespace</td>
</tr>
</tbody>
</table>
<p><code><span class='Function'>•Glyph</span></code> gives the glyph corresponding to a primitive as a single character, for example returning <code><span class='String'>'+'</span></code> given an argument matching <code><span class='Function'>+</span></code>. It causes an error if the argument is not a primitive.</p>
<p><code><span class='Function'>•Source</span></code> gives a string containing a block's source, including the enclosing braces <code><span class='Brace'>{}</span></code>. It causes an error if the argument is not a block. In contrast to <code><span class='Function'>•Glyph</span></code>, this function does not give full information about <code><span class='Value'>𝕩</span></code> because the result cannot convey environment or mutable identity.</p>
<p><code><span class='Function'>•Decompose</span></code> breaks down one level of a compound function or modifier, returning a list with a code giving what kind of structure it has (as listed in the table below) followed by each of its components. Non-operations do not cause an error, but return code -1, then the argument as a single component. The result is thus a list of length 2 to 4, and <code><span class='Function'>•Decompose</span></code> cannot cause an error.</p>
<table>
<thead>
<tr>
<th>Kind</th>
<th>Code</th>
<th>Components</th>
</tr>
</thead>
<tbody>
<tr>
<td>Non-operation</td>
<td>-1</td>
<td><code><span class='Value'>𝕩</span></code></td>
</tr>
<tr>
<td>Primitive</td>
<td>0</td>
<td><code><span class='Value'>𝕩</span></code></td>
</tr>
<tr>
<td>Block</td>
<td>1</td>
<td><code><span class='Value'>𝕩</span></code></td>
</tr>
<tr>
<td>2-train</td>
<td>2</td>
<td><code>  <span class='Value'>g</span><span class='Separator'>,</span><span class='Value'>h</span></code></td>
</tr>
<tr>
<td>3-train</td>
<td>3</td>
<td><code><span class='Value'>f</span><span class='Separator'>,</span><span class='Value'>g</span><span class='Separator'>,</span><span class='Value'>h</span></code></td>
</tr>
<tr>
<td>1-mod</td>
<td>4</td>
<td><code><span class='Value'>𝕗</span><span class='Separator'>,</span><span class='Value'>𝕣</span></code></td>
</tr>
<tr>
<td>2-mod</td>
<td>5</td>
<td><code><span class='Value'>𝕗</span><span class='Separator'>,</span><span class='Value'>𝕣</span><span class='Separator'>,</span><span class='Value'>𝕘</span></code></td>
</tr>
<tr>
<td>Left partial</td>
<td>6</td>
<td><code><span class='Value'>𝕗</span><span class='Separator'>,</span><span class='Value'>𝕣</span></code></td>
</tr>
<tr>
<td>Right partial</td>
<td>7</td>
<td><code>  <span class='Value'>𝕣</span><span class='Separator'>,</span><span class='Value'>𝕘</span></code></td>
</tr>
</tbody>
</table>
<h2 id="time">Time</h2>
<table>
<thead>
<tr>
<th>Name</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><span class='Function'>•UnixTime</span></code></td>
<td>Time between Unix epoch and function call</td>
</tr>
<tr>
<td><code><span class='Function'>•MonoTime</span></code></td>
<td>Monotonically-increasing time counter for relative measurement</td>
</tr>
<tr>
<td><code><span class='Function'>•Delay</span></code></td>
<td>Wait at least <code><span class='Value'>𝕩</span></code> seconds, and return the actual wait time</td>
</tr>
<tr>
<td><code><span class='Modifier'>•_timed</span></code></td>
<td>Call <code><span class='Function'>𝔽</span></code> on <code><span class='Value'>𝕩</span></code> <code><span class='Value'>𝕨</span><span class='Function'></span><span class='Number'>1</span></code> times, and return the average duration</td>
</tr>
<tr>
<td><code><span class='Modifier2'>•_maxTime_</span></code></td>
<td>Call <code><span class='Function'>𝔽</span></code> on the arguments, but fail if it takes over <code><span class='Value'>𝕨</span><span class='Function'>𝔾</span><span class='Value'>𝕩</span></code> seconds</td>
</tr>
</tbody>
</table>
<p>All times are measured in seconds.</p>
<p>The <a href="https://en.wikipedia.org/wiki/Unix_time">Unix epoch</a> is 1970-01-01 00:00:00 UTC. <code><span class='Function'>•UnixTime</span></code> is intended for absolute time measurement and should be implemented with the method that gives the most accurate result at any given time. <code><span class='Function'>•MonoTime</span></code> is intended for relative measurement and should use the method that gives the most precise time differences over the course of the program. Its return value must never decrease between calls.</p>
<p><code><span class='Modifier'>•_timed</span></code> returns the total time taken divided by the number of function calls (<code><span class='Value'>𝕨</span></code> if provided and 1 otherwise), including the overhead required for the outer loop that counts iterations (which will typically be negligible in comparison to the BQN code).</p>
<p>More accurately the modifier <code><span class='Modifier2'>•_maxTime_</span></code> <em>may</em> fail if execution of <code><span class='Function'>𝔽</span></code> takes over <code><span class='Value'>𝕨</span><span class='Function'>𝔾</span><span class='Value'>𝕩</span></code> seconds, and should fail as quickly as it is practically able to. The most likely way to implement this modifier is to interrupt execution at the given time. If <code><span class='Function'>𝔽</span></code> completes before the interrupt there is no need to measure the amount of time it actually took.</p>