Debian Codegraph @

erlang/README

raw
1
2 0. Introduction
3 ===============
4
5 The GNU APL interpreter can be called directly from Erlang programs.
6
7 Calling into APL from Erlang is a way to improve on the Erlang performance
8 when doing calculations involving large amounts of data.
9
10 The interface between Erlang and APL uses the Erlang native function interface
11 (NIF) on the Erlang side and libapl, kindly contributed by Dr. Dirk Laurie, on
12 the APL side. Almost all features of the GNU APL interpreter, that is commands,
13 expressions, variables, user-defined functions, and workspaces, are accessible
14 from Erlang.
15
16 In the simplest case one can call a APL function with Erlang values as
17 arguments and returning the result as Erlang value. This is not very efficient,
18 though, because the conversion between Erlang values and back may take more
19 time than the computation on the APL side itself.
20
21 A more advanced approach is to create APL variables and possibly APL functions
22 on the APL side and to exchange only the absolute minimum between Erlang and
23 APL. The APL functions and variables can be saved and restored on the APL side
24 into separate files (but under the control of Erlang). It is also possible
25 to load APL workspaces that were not developed for the use with Erlang and
26 call their functions from Erlang. As a matter of fact, the APL interpreter
27 is not even aware of Erlang's presence.
28
29 1. Building the interface
30 =========================
31
32 A. Although not strictly required, it is a good idea to build the command-line
33 version. See the README files in the top-level directory for details. For
34 the impatient, this is the usual
35
36 ./configure
37 make
38 sudo make install
39
40 This build a binary called 'apl'. Although you can create all APL functions
41 and variables that you need from Erlang, it is simpler to create an APL
42 workspace or script separately and then load the entire workspace or script
43 with an APL command executed from Erlang.
44
45 B. A prerequisite is the installation of Erlang and, in particular, the header
46 files for the NIF interface.
47
48 C. Now you can build the Erlang interface to APL:
49
50 ./configure --with-erlang
51 make
52 sudo make install
53
54 This creates two files: an Erlang module 'apl.erl', and a shared library
55 'liberlang_APL_nif.so'. Both files are being installed in the directory
56 for all other APL related libraries. That directory is normally
57 /usr/local/lib/apl or /usr/lib/apl. but this can be changed with ./configure
58 options. Please note that 'apl.erl' contains the absolute path to
59 'liberlang_APL_nif.so', so if you move 'liberlang_APL_nif.so' (why would you?)
60 then you have to change and recompile 'apl.erl' accordingly. On the other
61 side, 'apl.erl' or 'apl.beam' after compiling can be moved to other places
62 where Erlang finds them.
63
64 The file 'apl.erl' contains a few examples (see example/0) of how to use
65 the interface to APL. You can use them to test if the interface works and
66 remove them if they are no longer needed.
67
68 2. Using the interface
69 ======================
70
71 The Erlang module 'apl' has be initialized before using it:
72
73 apl:init().
74
75 If you forget to call init() then you will get an error like this:
76
77 Eshell V8.2 (abort with ^G)
78 1> apl:statement("1+2×○5").
79 ** exception exit: nif_library_not_loaded
80 in function apl:statement_ucs/1 (apl.erl, line 32)
81
82 On the other hand, calling apl:init() twice gives an error like this:
83
84 2> apl:init().
85 {error,{reload,"Reload not supported by this NIF library."}}
86
87 After the apl library was initialized you can execute APL commands in the
88 APL interpreter or evaluate APL expressions, define APL functions, set APL
89 variables and so on. This is done by the functions apl:command/1 and
90 apl:statement/1.
91
92 A. apl:command/1
93 ----------------
94 apl:command/1 executes an APL command like )LOAD workspace or )SAVE
95 workspace and returns the printout of the command to Erlang. If you are
96 new to APL then
97
98 apl_help() -> io:format("~ts~n", [command(")HELP")]).
99
100 might be handy. GNU APL characters are always Unicode, which means that
101 strings from APL are integer lists in Erlang (therefore "~ts~n" in
102 io:format which displays Unicode strings in a more readable form.
103
104 B. apl:statement/1
105 -------------------
106 apl:statement/1 performs one or more APL statements and returns a result
107 for each of them. In order to avoid the exchange of too much data between
108 Erlang and APL, a committed APL value (i.e. an APL value that was assigned
109 to an APL variable) is not transferred back to Erlang but replaced by an
110 Erlang atom 'committed_value'. If you still need the value then you can
111 read the APL variable later on or, more elegantly, use the APL functions
112 ⊢ and ⊣ to convert between committed APL values and normal APL values.
113
114 C. fix_function/1
115 -----------------
116 fix_function/1 creates a function on the APL side. The argument is a list
117 of lines, each line being an Erlang string. For example:
118
119 fix_function([ "Z←FOO B",
120 "Z←B + 1" ]).
121
122 creates an APL function named FOO which returns its argument incremented
123 by 1. As an aside, it could be a good strategy to use lowercase names for
124 functions and variables on the Erlang side and uppercase names for functions
125 and variables on the APL side.
126
127 D. reading APL variables
128 ------------------------
129 reading APL variables is fairly simple: just execute a statement with only
130 the name of the variable, just like in an Erlang shell. If you need only
131 a part of a variable, say a row or a columns of a variable, then it is
132 ALWAYS better to do the selection on the APL side as to minimize the amount
133 of data exchanged between Erlang and APL. Examples:
134
135 apl:statement('A'). % read entire APL variable A
136 apl:statement('A[3;]'). % read row 3 of APL variable A
137 apl:statement('A[;3]'). % read column 3 of APL variable A
138 apl:statement('A[2;3]'). % read element in row 2 and column 3 of A
139
140 E. writing an Erlang value to an APL variable
141 ---------------------------------------------
142 writing an Erlang value to an APL variable can be done in several ways.
143 For small values you use apl:statement/1 with the value being encoded in
144 the statement:
145
146 apl:statement('A←10 10⍴0'). % set A to 10 rows and 10 columns containing 0
147 apl:statement('A[3;←1'). % set row 3 of APL variable A to 1
148 apl:statement('A[;3]←2'). % set column 3 of APL variable A to 2
149 apl:statement('A[2;3]←3'). % set element in row 2 and column 3 of A to 3
150
151 If the APL value can be computed from other APL variables, possibly using
152 built-in or user-defined APL functions, then You use apl:statement/1 as
153 well:
154
155 apl:statement('B←10 100⍴A'). % set B to A,A,A,A,A,A,A,A,A,A
156
157 The above methods involve transformations of the value on the Erlang side
158 and parsing of the value on the APL side. If the value is huge, then this
159 can become fairly inefficient. For that reason, there exists a function
160 apl:set_variable/3 which sets an APL variable to an Erlang value, for
161 example:
162
163 apl:set_variable("Z", [5], [1,2,3,4,5]).
164
165 The arguments of set_variable/3 are: (1) the name of the variable, (2) the
166 structure (called the 'shape' in APL) of the variable, and (3) the elements
167 of the variable.
168
169 The shape tells in which form (like vectors, matrices, the elements are
170 arranged:
171
172 shape elements
173 [] single element (scalar APL value)
174 [V] N elements (1-dimensional APL vector)
175 [R,C] R×C elements (APL matrix with R roes and C columns)
176 [B,R,C] B×R×C elements (3-dimensional APL value)
177 ... up to 8 dimensions
178
179 F. Mapping between items in Erlang values and items in APL values. In APL
180 In APL every value is a pair { Shape, Ravel }. Ravel is a list of Cells,
181 where a cell is an elementary data type like numbers or characters. The
182 Shape is an integer vector that determines how the cells are laid out. For
183 example the two APL matrices
184
185 1 2 3 1 2
186 4 5 6 and 3 4
187 5 6
188
189 have the same Ravel [1 2 3 4 5 6] but different shapes: [2 3] resp. [3 2].
190
191 An arbitrary APL value V is represented in Erlang as a 3-tuple:
192
193 V = { 'value', Shape, Ravel }, where:
194
195 - 'value' is an Erlang atom tagging the 3-tuple as an APL value,
196 - Shape is a list of integers (sometimes called axes or dimensions),
197 - the product of the items in Shape is equal to the length of Ravel,
198 - Ravel is a list of Erlang Terms where the mapping between Cells in APL
199 Erlang terms is the following:
200
201 APL Cell Erlang Term
202 --------------------------------------------------
203 character (Unicode) integer
204 integer integer
205 double double
206 complex {'complex', double, double}
207 nested (sub-) value {'value', Shape, Ravel}
208
209 The function apl:e2a/1 can be used to convert an Erlang term to a value
210 triple.
211
212 On the interface between Erlang and APL, a non-committed APL value is
213 returned as a tuple {value, [s1, s2...], [e1, e2, ...]} with sN being the
214 0 or more elements of the shape and eN being the 0 or more elements being
215 arranged according to the shape. APL values are always rectangular.
216
217 Characters (and therefore also strings) in GNU APL are always Unicodes,
218 which are represented as integers in Erlang. To display APL strings
219 properly, you can either:
220
221 1. display them on the APL side (displaying the value is the default
222 behavior of APL unless the value is committed), or
223
224 2. use io:format with format string "~ts" on the Erlang side. Conversely,
225 an Erlang string, possibly containing APL characters like, for example,
226 "∆∇" remains the same on the APL side. This is because Erlang has no data
227 type for characters and uses integers instead.
228
229 G. Performance considerations
230 -----------------------------
231
232 The Erlang interface to APL can be divided into a high-level interface and
233 a low-level interface.
234
235 The high-level interface consists of the Erlang functions
236
237 command/1, % execute an APL command
238 statement/1, and % execute one or more APL statements
239
240 The argument if command/1 resp. statement/1 is an Erlang string which is
241 exactly what you would enter in an interactive APL session if you were
242 using APL directly instead of from APL.
243
244 The low-level interface consists of a number of eval_XXX functions. These
245 functions give you direct access to the "workhorses" of APL, bypassing
246 quite some trans-coding. We will explain this with an example.
247
248 The example adds two vector 1 2 3 and 4 5 6, which results in 5 7 9 (i.e.
249 1 + 4, 2 + 5, and 3 + 6).
250
251 We first add the vector using the high-level interface:
252
253 Eshell V8.2 (abort with ^G)
254
255 1> apl:init().
256 libapl initialized.
257 ok
258
259 2> apl:statement("1 2 3 + 4 5 6").
260 5 7 9 % APL interpreter output
261 [{value,[3],[5,7,9]}] % Erlanf shell output
262
263 The first thing to note is that the expected result 5 7 9 is displayed twice:
264 the first line comes from APL interpreter and the second from the Erlang
265 shell. The APL interpreter works similar to an Erlang shell in that it
266 prints the result of a statement if the statement computes a value. Other
267 than Erlang, however the result is NOT printed if it was assigned to an
268 APL variable. Assigning an APL value to a variable turns the value into
269 what is called a 'committed value' in APL slang. In GNU APL there is a
270 built-in function ⊣ which turns a value into a committed value. This
271 function is quite useful for suppressing the printout of the APL interpreter:
272
273 3> apl:statement("⊣1 2 3 + 4 5 6").
274 [committed_value]
275
276 Now to the low-level interface. In APL every function has 0, 1, or 2
277 value arguments, 0, 1, or 2 function arguments, and optionally an axis
278 argument. Many, but not all, combinations of value arguments, function
279 arguments and axis arguments are allowed, because some make no sense. For
280 example, the axis argument is normally related to one of the value arguments,
281 so it makes no sense to have an axis argument for a functions that has no
282 value arguments, Likewise it makes no sense to have 2 function arguments but
283 no value arguments because an APL operator (an APL function that takes 1 or
284 2 function arguments) will need value arguments when it calls the functions
285 that are passed as function arguments to it.
286
287 In Erlang we use a concept of function signatures to describe how many
288 and what kind of arguments an APL function requires. You know that concept
289 from Erlang already: foo/2 stands an Erlang function foo() which takes 2
290 arguments. In APL things are a little trickier because the type of argument
291 matters. Therefore instead of a single number for the argument count, we
292 use the letters A, L, R, X, and B with the fallowing meanings:
293
294 A: for a left value argument,
295 L: for a left function argument,
296 R: for a right function argument (implies that L is also present)
297 LR: for a left and a right function argument,
298 X: for an axis argument,
299 B: for a right value argument,
300
301 The letters always occur in the order above, but some may missing. The above
302 letters are also commonly used by APL programmers to specify the first
303 line of APL functions (which primarily determines the signature of an APL
304 function, similar to the left of -> when defining an Erlang function).
305
306 The most complex Erlang function in the low-level Erlang interface to APL
307 is eval_ALRXB/4. It is called with 4 arguments, say A, LR, X and B. The
308 arguments A, X, and B are value arguments, that is, they are passed
309 directly to an APL function or operator which is denoted by LR, which is an
310 Erlang string.
311
312 The Erlang string LR contains either one function name (of a function) or
313 two names (the first name is then the name of a function and the second name
314 is the of a monadic operator whose function argument is the function named
315 first), or three names (the second name is then the name of a dyadic
316 operator and the first and third names are the function arguments for that
317 operator.
318
319 A name is either user-defined (consisting primarily of letters and a
320 handful of APL characters that are also allowed in names (¯ ∆ and ⍙), or a
321 system name (consisting either of a single APL characters (like + = × ÷ ...)
322 or names beginning with ⎕ followed by a few letters).
323
324 On the first view It would have been cleaner to make the function arguments
325 of separate arguments of e.g. eval_ALRXB, but lumping them together in one
326 string LR (as opposed to three string arguments L, OPER, and R) is far more
327 readable.
328
329 In APL there are 3 cases corresponding to 3 groups of function signatures;
330 each group having different combinations of value and axis arguments:
331
332 ----------------------------------------------------------------------------
333 Group LR example1 LR example2 LR example3
334 --------------------------------------------------------------------------
335 1. function FOO "FOO" "+" "⎕CR"
336 2. monadic user operator FOO "BAR FOO" "+ FOO"
337 2. monadic APL operator / "BAR /" "+/"
338 3. dyadic user operator FOO "BAR FOO RRR" "+ FOO ×"
339 3. dyadic APL operator ⍣ "BAR ⍣ RRR" "+ ⍣ ×"
340 --------------------------------------------------------------------------
341
342 The following table lists the 13 Erlang functions n the low-level interface
343 that correspond to all 13 possible function signatures in APL and hopefully
344 explains in a better way what was said above:
345
346 ----------------------------------------------------------------------------
347 Function % Explanation APL header example
348 ----------------------------------------------------------------------------
349 eval_/1, % niladic APL function F: Z←F
350 eval_AB/3, % dyadic APL function F: Z←A F B
351 eval_ALB/3, % monadic APL operator: Z←A (L M) B
352 eval_AXB/4, % dyadic APL function F with axis: Z←A F [X] B
353 eval_ALRB/3, % dyadic APL operator D: Z←A (L D R) B
354 eval_ALXB/4, % monadic APL operator M with axis: Z←A (L M) [X] B
355 eval_ALRXB/4, % dyadic APL operator D with axis: Z←A (L D R)[X] B
356 eval_B/2, % monadic APL function F: Z← F B
357 eval_LB/2, % monadic APL operator: Z← (L M) B
358 eval_XB/3, % monadic APL function F with axis: Z← F [X] B
359 eval_LRB/2, % dyadic APL operator D: Z←(L D R) B
360 eval_LXB/3, % monadic APL operator M with axis: Z←(L M) [X] B
361 eval_LRXB/3 % dyadic APL operator D with axis: Z←(L D R) [X] B
362 ----------------------------------------------------------------------------
363
364 Coming back to the example: we want to compute 1 2 3 + 4 5 6. The function
365 in the low-level interface is therefore eval_AB/3 having 2 value arguments
366 A = 1 2 3 and B = 4 5 6. and no function argument L or R. The function name
367 is "+", so we call:
368
369 3> apl:eval_AB({value,[3],[1,2,3]}, "+", {value,[3],[4,5,6]}).
370 {value,[3],[5,7,9]}
371
372 Comparing this with the high-level interface we see the following
373 differences:
374
375 1. The result {value,[3],[5,7,9]} is the same as the result we got on
376 the high level interface. That is good.
377
378 2. there is no result printed by the APL interpreter. This is because the
379 low-level interface bypasses the APL interpreter and calls its workhorses
380 (= the implementation of its primitives) directly.
381
382 3. The arguments of APL functions are now Erlang terms as opposed to
383 Erlang strings. And the result returned by both the high-level interface
384 as by the low-level interface is also an Erlang term.
385
386 COROLLARY: The high-level interface is closer to the "classical"
387 (interactive) usage of APL, and is also more readable. However it incurs
388 a substantial performance penalty for translating Erlang terms to Erlang
389 strings understood by the high level interface. This penalty is roughly
390 proportional to the lengths of the arguments of APL functions. For simple
391 functions on the APL side, like "+" in the example above, the penalty will
392 be far bigger then the time needed for the computations itself.
393
394 It may be worth noting, though, that in many real-world cases, the penalty
395 of the high-level interface can be avoided completely. The trick is to store
396 the results of computations on the APL side in APL variables as long as
397 possible and not in Erlang variables. Remembering the partial results of APL
398 computations in APL variables is a simple and efficient way to avoid that
399 data is transferred back and forth between Erlang and APL.
400
401 H. Erlang considerations
402 ------------------------
403 Multiple Erlang processes can use the interface to APL at the same time.
404 There is a semaphore in the interface that makes the Erlang functions
405 discussed above atomic. Needless to say that chaos can still break out if
406 different processes update the same variable(s) at the same time.
407
408 Also, APL is executed in the same thread as the caller in Erlang. The
409 implementation of the functions in both the high-level interface and in the
410 low-level interface in Erlang_APL_nif.c are marked with the DIRTY_FLAG,
411 which tells the Erlang scheduler that the functions will not yield to other
412 Erlang processes in the same thread of the operating system. This flag is,
413 however, not supported in older OTP versions, and will negatively impact
414 the Erlang real-time performance if the DIRTY_FLAG is not supported and
415 computations on the APL side take a longer time.
416
417 I. Unicode versus UTF8.
418 =======================
419 Some of the functions in apl.erl, in particular command/1 and statement/1
420 have two internal function variants ending with _ucs or _utf8 respectively.
421
422 The _ucs variant uses strings with 1 integer per character, and that integer
423 can be larger than 255. In contrast, the utf8 variant uses possibly more
424 than one integer per character (like 2 or 3 for APL characters) but each
425 integer fits into a byte.
426
427 For example, the Erlang string "∆∇" has two integers 16#2206 and 16#2207
428 on the Erlang side and two Unicode characters on the APL side. The UTF8
429 encoding of the same Erlang string "∆∇" has UTF8 encodings e2 88 86 and
430 e2 88 86 for a total of 6 characters.
431
432 In most cases you will not care about UTF8 encodings, but sometimes, for
433 example when handling web pages (which are normally UTF8 encoded) you can
434 avoid unnecessary trans-coding between Unicode and UTF8
435 by using e.g. statement_utf8/1 instead of statement_ucs/1.
436
437 The default for such functions is UCS (1 Erlang integer = 1 APL character).
438 Therefore, for example, statement/1 is the same as statement_ucs/1. That,
439 in turn makes it possible to specify APL expressions verbatim as Erlang
440 strings, for example:
441
442 apl:statement( "1+2×○5" ).
443 32.41592654 ← APL printout out the result
444 [{value,[],[32.41592653589793]}] ← Erlang printout of the result
445
446 J. Issues
447 ---------
448
449 Depending on your platform and the location where GNU APL is installed, a
450 few things may be needed.
451
452 I1. make sure that liberlang_APL_nif.so will be found - either by setting
453 the environment variable LD_LIBRARY_PATH properly, or by adding an entry
454 to /etc/ld.so.conf. See man ldconfig for details.
455
456 2. make sure that APserver is found or not used. You can start APserver
457 manually beforehand like this:
458
459 APserver -H &
460
461 or you can start apl like this:
462
463 apl --noSV
464
465 Shared variables are a compatibility left-over from the 1960s while Erlang was
466 created in the 1990s. It is therefore quite unlikely that APL Shared variables
467 and Erlang are being used toghether and if so, the extra effort of starting
468 APserver manually beforehand seems good enough.
469
470 K. Further references
471 ---------------------
472
473 See also: http://erlang.org/doc/tutorial/nif.html
474