# Rank PolymoRphic Text editor (RPRT) A compositional, point-free language for text manipulation. # 1. Core Concepts ## 1.1 Two Fundamental Types All functions work with two core types: 1. **Selection** — addresses and regions in files - Rank 0: Position (integer offset) - Rank 1: Range is a pair of positions [start,end) with capture groups - Rank 2: A list of ranges with capture groups - Rank 3: Indexed selection is a mapping of files -> list of ranges with capture groups 2. **Text** — string data # 2. Selection Functions Selection functions produce Selection outputs and take zero or more Selection inputs. They come in nullary, unary (monadic), and binary (dyadic) forms. ## 2.1 Core Selection Functions There are ten core selection functions in RPRT. Nine of them have nullary and unary (monadic) forms. The tenth, `,` (span), additionally has binary (dyadic) semantics - it is the only selection function that takes two arguments. In the nullary case there are no rank semantics. In the unary case we provide their semantics as a function of the input rank below. | Function | Nullary | Rank | |----------|----------------------------------------------------|---------| | `.` | Current selection | matched | | `$` | End of file | 0 | | `#n` | Character n in file | 0 | | `n` | Line n in file | 1 | | `/re/` | First match forward in file from start | 1 | | `x/re/` | All matches within file | 2 | | `+n` | Line n in file | 1 | | `X/re/` | All files and their contents whose name matches re | 3 | | `,` | Entire file (0,$) | 2 | In the unary case, all defined functions are rank preserving. Their action on ranks > 1 are given by broadcasting, that is, `α F` where `α` has rank 2 is computed by applying `F` to each rank 1 range in `α` and collecting the results into a list of ranges, mutatis mutandis for rank 3. **Note:** The functions `.` and `X` have no unary or higher arity semantics. To use them as such is erroneous. **Note:** When the input has rank 0 (position), all defined unary functions are the identity since positions have no internal structure within which to search or operate. | Function | α Rank 1 | |-----------|-------------------| | `α /re/` | First re within α | | `α x/re/` | All re within α | | `α $` | End position of α | | `α #n` | Char n within α | | `α n` | Line n within α | | `α +n` | Line n within α | | `α ,` | Identity | The span function `,` is the only selection function with binary (dyadic) semantics. It creates ranges by spanning from one selection to another. **Semantics by rank:** | Rank of α | Rank of ω | Rank of result | Semantics | |-----------|-----------|----------------|-------------------------------------------------------| | 0 | 0 | 1 | Range from α to ω | | 0 | 1 | 1 | Range from α to end of ω | | 1 | 0 | 1 | Range from start of α to ω | | 1 | 1 | 1 | Range from start of α to end of ω | | 0/1 | 2 | 2 | α to each range in ω (broadcast) | | 2 | 0/1 | 2 | Each range in α to ω (broadcast) | | 2 | 2 | 2 | Pairwise ranges (zip shortest) | | 0 | 3 | 3 | α to each range in ω (broadcast) | | 3 | 0 | 3 | Each range in α to ω (broadcast) | | 3 | 1/2 | - | Erroneous (TODO: maybe makes sense if indices align?) | | 1/2 | 3 | - | Erroneous (TODO: maybe makes sense if indices align?) | ## 2.2 Operators Operators take functions as arguments and return new functions. ### 2.2.1 The Reverse Operator `'` The reverse operator `'` takes a selection function and reverses its direction. **Note:** `'F` is not defined when `F` is one of the functions `.X,` . | Function | Nullary | Rank | |----------|-------------------------------|------| | `'$` | Start of file (position 0) | 0 | | `'#n` | Character n from end of file | 0 | | `'n` | Line n from end of file | 1 | | `'/re/` | First match backward from end | 1 | | `'x/re/` | All matches, right-to-left | 2 | | `'+n` | Line -n in file | 1 | In the unary case, all functions are rank preserving, and all of the above functions are the identity on rank 0 inputs. Their action on ranks > 1 is by broadcasting. | Function | α Rank 1 | |------------|--------------------------------| | `α '$` | Start position of α | | `α '#n` | Char n from end of α | | `α 'n` | Line n from end of α | | `α '/re/` | First match backward within α | | `α 'x/re/` | All matches right-to-left in α | | `α '+n` | Line -n within α | **Note:** When the input has rank 0 (position), all defined reversed unary functions are the identity since positions have no internal structure within which to search or operate. ### 2.2.2 The Sequential Operator `;` The sequential operator `;` transforms a selection function from operating "within" a selection to operating "starting from the end" of that selection. **Note:** It is erroneous to use `;F` in the nullary case. TODO: or are there interesting semantics? **Note:** `;F` is not defined when `F` is one of `.X,` . | Function | α Rank 0 | α Rank 1 | |------------|----------------------------|-----------------------------------| | `α ;$` | End of file | End of file (ignores α) | | `α ;#n` | Character n after α | Character n after end of α | | `α ;n` | Line n after α | Line n after end of α | | `α ;/re/` | First match forward from α | First match forward from end of α | | `α ;x/re/` | All matches forward from α | All matches forward from end of α | | `α ;+n` | Line n after α | Line n after end of α | ### 2.2.3 The Complement Operator `~` The complement operator `~` takes a selection function and produces a new selection function which returns the complement. Naturally this changes rank somewhat, as explained in the below table. | Rank of output of `F` | Rank of output of `~F` | Semantics | |-----------------------|------------------------|------------------------------------------------------------------| | 0 | 2 | The complement of the position its file as a selection | | 1 | 2 | The complement of the range in its file as a selection | | 2 | 2 | The selection corresponding to the ranges between the selections | | 3 | 3 | The broadcast of `~` to each file | ## 2.3 Composition and Parsing **Operator application:** Functions are modified by operators. All operators commute: `';F` = `;'F`. **Precedence:** Following BQN rules: - Operators bind tighter than functions - Functions apply left-to-right - Binary functions are infix ## 3. Text Functions Text functions modify selections, and consume zero or one selections, and one Text value.