april

所属分类:编程语言基础
开发工具:Common Lisp
文件大小:0KB
下载次数:0
上传日期:2023-05-09 15:37:25
上 传 者sh-1993
说明:  APL编程语言(其子集)编译为Common Lisp。
(The APL programming language (a subset thereof) compiling to Common Lisp.)

文件列表:
LICENSE (10758, 2023-11-24)
aplesque/ (0, 2023-11-24)
aplesque/aplesque.asd (440, 2023-11-24)
aplesque/aplesque.lisp (163095, 2023-11-24)
aplesque/forms.lisp (17444, 2023-11-24)
aplesque/obsolete.lisp (39863, 2023-11-24)
aplesque/package.lisp (1515, 2023-11-24)
aprepl/ (0, 2023-11-24)
aprepl/aprepl.el (33414, 2023-11-24)
april.asd (655, 2023-11-24)
attic/ (0, 2023-11-24)
attic/functions.lisp (57832, 2023-11-24)
attic/utilities.lisp (3839, 2023-11-24)
compatibility-notes.md (4100, 2023-11-24)
demos/ (0, 2023-11-24)
demos/cnn/ (0, 2023-11-24)
demos/cnn/april-demo.cnn.asd (333, 2023-11-24)
demos/cnn/cnn.apl (7199, 2023-11-24)
demos/cnn/demo.lisp (5437, 2023-11-24)
demos/cnn/package.lisp (169, 2023-11-24)
demos/cnn/setup.lisp (172, 2023-11-24)
demos/fnn/ (0, 2023-11-24)
demos/fnn/april-demo.fnn.asd (349, 2023-11-24)
demos/fnn/demo.lisp (25779, 2023-11-24)
demos/fnn/package.lisp (169, 2023-11-24)
demos/fnn/setup.lisp (172, 2023-11-24)
demos/ncurses/ (0, 2023-11-24)
demos/ncurses/april-demo.ncurses.asd (344, 2023-11-24)
demos/ncurses/loader.lisp (188, 2023-11-24)
demos/ncurses/ncurses.lisp (7469, 2023-11-24)
demos/ncurses/package.lisp (186, 2023-11-24)
... ...

# April #### Array Programming Re-Imagined in Lisp --- Ken Iverson's masterpiece reflected in the medium of Lisp. April compiles a subset of the APL programming language into Common Lisp. Leveraging Lisp's powerful macros and numeric processing faculties, it brings APL's expressive potential to bear for Lisp developers. Replace hundreds of lines of number-crunching code with a single line of APL. ## Why April? APL veritably hums with algorithmic power. As a handful of characters run past the lexer, vast fields of data grow, morph and distil to reveal their secrets. However, APL has hitherto dwelt in an ivory tower, secluded inside monolithic runtime environments. If you have a store of data you'd like to process with APL, getting it there can be an ordeal akin to hauling tons of cargo on donkeys' backs through a narrow mountain pass. The original APL interpreters ran on mainframes whose only input was the keyboard and only output was the printer and the legacy of that implementation approach has persisted to this day, limiting the reach of the language. But no longer. Lisp is the great connector of the software world, digesting and transforming semantic patterns in much the same way that APL works upon numeric patterns. With APL inside of Lisp, databases, streams, binary files and other media are just a few lines of code away from processing with APL. ### Discussion For the time being, discussion of April and its development is happening on the `##phantomics` channel on irc.libera.chat. ## Support April's Development If you'd like to help provide for continuing work on April, you can [contribute through Patreon](https://www.patreon.com/lisp). April is making fast and steady progress toward the goal of being a powerful, full-featured and professional APL implementation, whose ease of extension and integration with other software offers game-changing advantages in the vector language field. Did you know that April's code at the beginning of December 2021 was over 95% different from its code at the beginning of September 2020? ## April Media and Publications ### PLDI 2023 presentation [Presentation of paper on April's deferred evaluation features](https://youtube.com/watch?v=wz9cDRGWnIs&t=8175s) ### ArrayCast interview [Developer interview exploring April's history and features](https://www.arraycast.com/episodes/episode23-andrew-sengul) ### APL Seeds Presentation [Presentation of April at Dyalog's APL Seeds '22 conference](https://youtube.com/watch?v=Wxaqu8E83gE) ### LispNYC April compiler presentation [Video outline and panel discussion showing early April design](https://youtube.com/watch?v=AUEIgfj9koc) ### European Lisp Symposium 2022 paper [April: APL Compiling to Common Lisp](https://doi.org/10.5281/zenodo.6337140) ## Compatibility with Common Lisp Implementations April puts the numeric and array processing capabilities of Common Lisp to the test. It has been verified to work with SBCL, CCL, ECL, ABCL, Clasp, Allegro CL and LispWorks. SBCL and CCL are considered fully compatible with no special provisions made in order to function. ECL and Clasp pass all tests with the help of some specific provisions addressing their differences from other CL implementations. ABCL passes all main tests with the help of some special provisions and all dfn tests except those in the tree library, which cannot be loaded due to limitations of the underlying Java virtual machine. Allegro CL and LispWorks both have a few compatibility issues causing failures of dfn tests. [See this document for a list of all differences in functionality between implementations.](./compatibility-notes.md) ## Automatic Installation April is supplied by the Quicklisp library manager, so the easiest way to install April is through Quicklisp. To install April with Quicklisp, evaluate: ```lisp (ql:quickload 'april) ``` ## Manual Installation If you'd like to install April manually from this repository, you can follow these instructions. ### Cloning the Repository First, clone the repository to a location on your system. For this example, let's say you cloned it to the directory ~/mystuff/april. ### Preparing Quicklisp Enter your Quicklisp local-projects directory (usually ~/quicklisp/local-projects) and create a symbolic link to the directory where you cloned the April repository. For example, if you cloned the repo to ~/mystuff/april and your Quicklisp directory is ~/quicklisp/, enter: ``` cd ~/quicklisp/local-projects ln -s ~/mystuff/april ``` ### Installing Dependencies To complete the installation, just start a Common Lisp REPL and enter: ```lisp (ql:quickload 'april) ``` This will download and install April's dependencies, and with that the package will be built and ready. ## APL Functions and Operators The APL language uses single characters to represent its primitive functions and operators. Most of these symbols are not part of the standard ASCII character set but are unique to APL. To see a list of the glyphs that are supported by April, visit the link below. #### [See the complete April APL lexicon here.](./lexicon.md) Some APL functions and operators won't be added to April since they don't make sense for April's design as a compiler from APL to Lisp. Others may be added in the future. [See the list of features not implemented here.](#whats-not-planned-for-implementation) ## Getting to Know APL A full guide to the APL language is far beyond the scope of this file, but here are links to some good sources. [A high-level introduction to APL.](http://archive.vector.org.uk/art10011550) [This is a detailed language tutorial covering most of the functions and operators in April.](http://microapl.com/APL/tutorial_contents.html) [The original paper by Ken Iverson, creator of APL, detailing the language's underlying philosophy.](https://www.jsoftware.com/papers/tot.htm) If you would like a quick tour of the language, April includes a function that will print demos of all the commands and many APL syntax features. To see the demos, enter: ```lisp * (april (demo)) ``` The `*` indicates a REPL prompt. Prepare for a long read. The demo content that gets printed will tell you the name(s) of the operations that correspond to each symbol and will hopefully give you some idea of what each one does. ### How to Enter APL Characters In order to write APL programs you'll need a way to use the language's special character set. [Click here for information on enabling APL input within Emacs.](#enabling-apl-input-in-emacs) [Click here for information on enabling APL input within Vim.](#enabling-apl-input-in-vim) [Click here for information on enabling APL input universally within GNU/Linux.](#enabling-apl-input-universally-in-gnulinux) ## Basic Evaluation: (april) and (april-f) Evaluating an APL expression is as simple as: ```lisp * (april-f "1+2 3 4") 3 4 5 #(3 4 5) ``` As above, the `*` indicates a REPL prompt and the text below is the expression's output. The macro `(april-f)` (short for april-format) will evaluate any APL string passed to it as the sole argument, returning the final result. Using `(april-f)` will also produce a printout of the output in APL's traditional array printing style, which appears before the actual output value. You can see above how the `3 4 5` is printed out before the value `#(3 4 5)`. APL-style printed arrays are easier to read than Lisp's style of printing arrays; APL can use a simpler style to express its output because it doesn't have as many different data types and structures as Lisp. If you don't need to see the printout, you can use the plain `(april)` macro. Like this: ```lisp * (april "1+2 3 4") #(3 4 5) ``` You should use `(april)` if you're using April to do calculations inside of a larger program and don't need the printout. Otherwise, especially if you're working with large data sets, the system may consume significant resources printing out the results of calculations. Setting state properties for the APL instance can be done like this: ```lisp * (april-f (with (:state :count-from 0)) "9") 0 1 2 3 4 5 6 7 8 #(0 1 2 3 4 5 6 7 8) ``` Instead of an APL string, the first argument to `(april)` or `(april-f)` may be a list of parameters for the APL environment. The APL expression is then passed in the second argument. For example, you can use the `:count-from` parameter to determine whether functions in the evaluated APL code will start counting from 0 or 1. We'll get into more detail on how these parameters work later. ```lisp * (april-f (with (:state :count-from 1)) "9") 1 2 3 4 5 6 7 8 9 #(1 2 3 4 5 6 7 8 9) * (april-f (with (:state :count-from 0)) "9") 0 1 2 3 4 5 6 7 8 #(0 1 2 3 4 5 6 7 8) ``` More APL expressions: ```lisp * (april-f "12") 1 2 3 4 5 6 7 8 9 10 11 12 #(1 2 3 4 5 6 7 8 9 10 11 12) * (april-f "3 412") 1 2 3 4 5 6 7 8 9 10 11 12 #2A((1 2 3 4) (5 6 7 8) (9 10 11 12)) * (april-f "+/3 412") 10 26 42 #(10 26 42) * (april-f "+3 412") 15 18 21 24 #(15 18 21 24) * (april-f "+/[1]3 412") 15 18 21 24 #(15 18 21 24) * (april-f "3 412") 4 3 2 1 8 7 6 5 12 11 10 9 #2A((4 3 2 1) (8 7 6 5) (12 11 10 9)) * (april-f "13 412") 2 3 4 1 6 7 8 5 10 11 12 9 #2A((2 3 4 1) (6 7 8 5) (10 11 12 9)) ``` ### A note on escaping characters April uses the backslash character `\` to implement the expand function and the scan operator. Because of the way Lisp strings work, this character must be escaped with a second `\` before it in order to enter APL code containing backslashes. For example: ```lisp * (april-f "+\\5") 1 3 6 10 15 #(1 3 6 10 15) ``` Inside the `"string"`, the two backslashes evaluate to a single backslash. If you forget about this, you can run into confusing errors. ## Unique Language Features in April For the most part, April's syntax and functions follow standard APL conventions. But there are a few areas where April differs from typical APL implementations along with some unique language features. Most notably: ```lisp ;; k-style if-statements * (april "x←5 $[x>3;8;12]") 8 ;; k-style functions with any number of named arguments * (april "monthlyPayment←{[amt;int;len] (len÷amt×int×0.1)+amt÷len} monthlyPayment[5000;0.8;12]") 450.0d0 ;; numbered branch points instantiated with → syntax * (april "x←1 →1+1 x×←11 1→ x×←3 2→ x×←5 3→ x×←7") 35 ;; symbol-referenced branch points and a branch function with expression-determined branch symbol choice * (april "x←1 (5-3)→two three x×←11 one→ x×←3 two→ x×←5 three→ x×←7") 7 ;; and are used to reference values passed as operands to a user-defined operator * (april "'*' {,} ' b c d'") "* b c d" ;; if the operands passed to a user-defined operator are to be functions, ;; and are used as in other APLs * (april "(2|) {(¨)/} 20") #(1 3 5 7 9 11 13 15 17 19) ``` The biggest difference between April and other APLs lies in its implementation of the `→ branch` function, as shown in the third and fourth examples above. April also allows you to use if-statements and functions with any number of named arguments in the style of Arthur Whitney's k programming language. Because of April's nature as a compiler, user-defined operators use different symbols to refer to operands depending whether the operands are values or functions. The underlined characters `` and `` are used to refer to the operands as values, while the doubled characters `` and `` refer to the operands as functions. The presence of both `` and `` or both `` and `` in a defined operator will cause an error. ### Using rational numbers April is one of a few APL implementations to include rational numbers. They are printed with a lowercase `r` separating the numerator and denominator. Examples: ```lisp * (april-f "÷5") 1 1r2 1r3 1r4 1r5 #(1 1/2 1/3 1/4 1/5) * (april-f "2r3×4") 2r3 4r3 2 8r3 #(2/3 4/3 2 8/3) ``` Rational numbers can also be used as parts of complex numbers: ```lisp * (april-f "3r4J9r5×4") 3r4J9r5 3r2J18r5 9r4J27r5 3J36r5 #(#C(3/4 9/5) #C(3/2 18/5) #C(9/4 27/5) #C(3 36/5)) ``` ### Underscores within numbers In April, you can use underscores to separate parts of a number: ```lisp * (april "1_000_000+5") 1000005 * (april "1__000_000__000_000+5") 1000000000005 * (april "1_00___0_0__00_0+5") 10000005 ``` As shown above, you can use any number of underscores anywhere within a number, they are simply ignored by the reader. Underscores are also used by the printer when printing columns of mixed complex floats and rationals: ```lisp * (april-f "12.2J99.11 3J8 19r13J5r2") 12.20J99.11 3___J_8 19r13J_5r_2 #2A((#C(12.2 99.11)) (#C(3 8)) (#C(19/13 5/2))) ``` Using the underscores as filler keeps the decimal points, rs and Js properly aligned for printing. ### Strings and escaped quotes In April, either single or double quotes can be used to enclose character strings: ```lisp * (april "'abc','def'") "abcdef" * (april "\"ghi\",\"jkl\"") "ghijkl" ``` Note that you must use backslashes to escape double quotes used within Lisp strings, making double quotes a less desirable choice unless you're loading April code from files using `(april-load)`. In order to escape quote characters within an April string, enter the quote character twice. For example: ```lisp * (april "'\'abc'\'") "'abc'" * (april "'''abc'''") "'abc'" ``` ## Compact Function Calls: The (april-c) Macro Want to invoke April functions on some variables with less code? You can use the `(april-c)` macro. For example: ```lisp * (april-c "{×}" 2 8) 16 * (april-c "×" 2 8) ;; the same result as above by passing a string containing just '×' 16 * (april-c "{[a;b;c;d] d↑ca+b}" 3 5 6 10) #(8 8 8 8 8 8 0 0 0 0) ``` After the string where the April function is written, pass the variables that will be input to the function and you'll receive the result with no need for a long `(with (:state ...))` clause. If you wish to pass parameters in a `(with)` clause, you can still do it with `(april-c)`. ```lisp * (april-c (with (:state :count-from 0)) "{}" 7) #(0 1 2 3 4 5 6) ``` The arguments passed to `(april-c)` are in the order ` `. This may seem counterintuitive: ```lisp * (april-c "{-}" 10 5) 5 ``` But keep in mind that a function must always have a right argument, but may or may not have a left argument. Therefore it's easier to remember that the first argument after the function string is always the right argument, and the second argument, if present, is the left argument. The `(april-c)` macro can also be used to compose inline operators with functions. For example: ```lisp * (april-c "{/}" #'+ #(1 2 3 4 5)) 15 * (april-c "{ /}" #'+ #'- #(1 2 3 4 5)) -15 * (april-c "{ /}" #'+ (scalar-function -) #(1 2 3 4 5) 3) #(-6 -9 -12) ``` Note that in operators where a right operand is expected (i.e. those that contain a `` or `` symbol), two operands are expected following the code string. In an operator taking only a left operand, whose code doesn't include `` or ``, one operand is expected following the code string. The arguments to `(april-c)` for an operator are in the order `/ (/ if present) ( if present)`. The left operand comes first in the arguments because all operators must have a left operand, but they may or may not have a right operand. Keep in mind that standard Common Lisp functions like `#'+` do not operate on entire arrays like APL functions do. In order to pass scalar functions into April via `(april-c)` that can be composed with operators and work as you expect scalar functions to when doing operations like `- 3 +/5`, you must pass those functions' symbols through the `(scalar-function)` macro as seen above with `(scalar-function -)`. ## Parameter reference When `(april)` or `(april-f)` is called, you may pass it either a single text string: ```lisp * (april-f "1+1 2 3") ``` Or a parameter object followed by a text string: ```lisp * (april-f (with (:state :count-from 0)) "9") ``` This section details the parameters you can pass to April. ### (test) To run April's test suite, just enter: ```lisp * (april (test)) ``` ### (demo) As mentioned before, you can see demos of April's functions with: ```lisp * (april (demo)) ``` ### (with) `(with)` is the workhorse of April parameters, allowing you to specify many options for an April invocation. The most common sub-parameter passed via `(with)` is `(:state)`. To wit: ```lisp * (april (with (:state :count-from 0 :in ((a 3) (b 5)) :out (a c))) "c←a+b") 3 #(3 4 5 6 7) ``` ### (:state) sub-parameters Let's learn some more about what's going on in that code. The sub-parameters of `(:state)` are: #### :count-from Sets the index from which April counts. Almost always set to 0 or 1. The default value is 1. In the code above, `b` with `b` equal to 5 counts from 0 to 4, whereas with the default `:count-from` value of 1, `b` would count from 1 to 5. When you set :count-from, it only affects the APL code evaluated in the expression to which the :count-from option is passed. For example: ```lisp * (april (with (:state :count-from 0)) "9") #(0 1 2 3 4 5 6 7 8) * (april "9") #(1 2 3 4 5 6 7 8 9) ``` #### :in Passes variables into the April instance that may be used when evaluating the subsequent expressions. In the example above, the variables `a` and `b` are set in the code, with values 1 and 2 respectively. You can use `:in` to pass values from Lisp into the April instance. ```lisp * (april-f (with (:state :in ((a 5) (b 10)))) "1+2+a×b") 53 ``` Please note that April variables follow a stricter naming convention than Lisp variables. When naming the input variables, only alphanumeric characters, underscores and dashes may be used. In keeping with APL tradition, the delta/triangle characters and can be used in variable names as well. Punctuation marks like ?, >, . and ! may not be used as they have separate meanings in April. These characters are allowed in variable names within April: ``` 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_ ``` These variable names are ok for use with the `:in` parameter: ``` a var my_var another-var ``` These are not ok: ``` true! this->that pass/fail? var.name ``` If you use dashes in the names of Lisp variables you pass into April, note that inside April they will be converted to camel case. For example: ```lisp * (april-f (with (:state :in ((one-var 2) (other-var 5)))) "oneVar×otherVar+5") 20 ``` The dash character `-` is used to denote the subtraction function inside April, so you may not use dashes in variable names within the language. One more caveat: it's best to avoid using input variable names with a dash before a number or other non-letter symbol. The dash will be removed and the character following it will cannot be capitalized so information will have been lost. For example: ``` my-var-2 → myVar2 my-var- → myVar ``` #### **An important note about modifying arrays passed to April** When passing array values into April via `:in` or direct function calls using `(april-c)`, be advised that assinging new values to their elements _may or may not_ destructively modify the original arrays. Whether destructive modification happens depends on whether the values assigned are compatible with the element type of the original arrays. Here is an example: ```lisp (let ((a #(1 2 3)) (b #(2 3 4)) (c (make-array 3 :element-type '(unsigned-byte 4) ... ...

近期下载者

相关文件


收藏者