From 71db16c05982a2c8d11b4573d8f7fcd2f0e888fb Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sat, 17 Oct 2026 08:27:52 +0000 Subject: [PATCH] Report 2026-10.33: a const parameter, a variant in a constant, the library by var The ninth thaw, by the owner's word. A const parameter is the caller's variable read in place, never changed by the procedure, and takes any expression of its type (sections 5.9, 7.4, 8, 10). A typed constant may be a variant, or hold variants (section 4). The library's procedures that took a pointer to what they work on take var, const or a value instead; the library volume changes with each module, and the compiler follows 2026-10.32 until it has. STANDARDS.md says how a parameter is chosen; PROPOSALS.md keeps only the call-site marks, ret and keep. --- diff --git a/PROPOSALS.md b/PROPOSALS.md index 001fc11..7e6fa79 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -10,21 +10,24 @@ 2026-10-17 (the second review's findings are under "Open questions", at the end of part 1). The owner judged the feature, and its use, not settled: no change is made for the marks at the call, `ret` or `keep`. -Part 2, the library by `var`, is done on its own, without the marks. +Parts 2, 4 and 5, the library by `var`, a `const` parameter and a +variant in a constant, went into report 2026-10.33 on their own, +without the marks, and are no longer here. As drafted, it is five parts, to be made in one thaw so that programs break once: 1. **Marks at the call** (this section): an argument for a `var` parameter is written `var x`, and one for a `keep` parameter `keep x`. -2. **The library's parameters by `var`, not by pointer** (below): the +2. **The library's parameters by `var`, not by pointer** (now in the + report, 2026-10.33): the `ADR` that stands at nearly every call to the library goes. 3. **`ret` and `keep`** (below): a heading says what a procedure does with what it is given, and the compiler holds the body to it. -4. **A `const` parameter** (below): a record or array only read is +4. **A `const` parameter** (now in the report): a record or array only read is passed in place, unmarked, so that `var` at a call always means a change. -5. **A constant holds a variant** (below): so that a table a procedure +5. **A constant holds a variant** (now in the report): so that a table a procedure keeps, as `Opt.init`'s, can be a constant, not a literal in the frame. @@ -535,53 +538,7 @@ buffer, `Scan.init(var s, keep line[0..n])`, is refused though both end together (a module variable, a `Buf` or the heap is the way out); so is a store through `ADR` of a local. - -### Part 2: the library's parameters by `var`, not by pointer -Most of the library's procedures take what they work on as `^T`: -`Buf.str(ADR(b), s)`, `Vec.push(ADR(v), x)`, `Lock.take(ADR(l))`. None -keeps it past the call; each reads or changes it and returns. That is -what `var` is for (8), and with it the call is `Buf.str(var b, s)`. As it -is, `ADR` stands at nearly every call a program makes to the library, -and says nothing: a reader can't tell the few places where a pointer -is kept from the many where it isn't. `STANDARDS.md` says how our own -code chooses; this would bring the library to it. - -The proposal, procedure by procedure, from reading each one: - -- **To `var`** (it changes what it is given): `Arena.init`, `reset`, - `done`; `Buf.init`, `room`, `add`, `str`, `spare`, `cut`, `reset`, - `done`, and the `Buf` that `Files.load`, `Files.read`, `Fmt.write`, - `Str.join` and `Str.replace` write into; every procedure of `List`; - `Lock.take`, `give`; `Map.init`, `put`, `remove`, `done`; the - procedures of `Opt` that change the `Parser` (`init`, `next`, - `operand`, `scope`, ...); `Sys.openDir`, `nextEntry`, `closeDir`; - `Thread.lock`, `unlock`; `Vec.init`, `room`, `push`, `pop`, `add`, - `tryPush`, `tryAdd`, `resize`, `reset`, `done`. -- **To a value** (it only reads a record of a few words): `Buf.bytes`, - `Vec.all`, `Map.get`, `Map.next`, `Sys.entryKind`. -- **To `const`** (it only reads a record of many words; part 4): - `Opt.at`, and whichever other procedure of `Opt` a reading shows only - reads the `Parser`. -- **Staying pointers**: an allocator, `^Mem.Allocator`, everywhere - (`NEW` takes one, and `^Arena(a)` needs one); `Mem.pages`, `unmap`, - `Sys.mmap`, `munmap`, `mapFile`, which are bytes, not variables; - `Thread.spawn`'s `arg`, which is kept, and the `^Thread` it gives, - which `join` and `signal` take as the program holds it; `Rt.fail`, - which the compiler calls. - -What it costs: it breaks every program that calls these, `src/`, -rmacs, ravite, the tools and the library's own tests among them. The -mending is mechanical, `ADR(x)` to `var x` at each such call, and `p` -to `var p^` where a program holds the thing by pointer, and the -compiler's message for a `^T` given to a `var` names it. The seed -knows `var` parameters but not the marks of part 1, so with them -`src/` and the library it imports wait for the new seed. Alone, this -part changes volume 2 and `raz-lib.3`, not the report. - -Met in rpg and rsqlite, whose `Query`, `Result` and `Stmt` went from -`^T` to `var` and to values, and whose own calls to `Buf` couldn't. - ### Part 3: marking what a procedure does with what it is given: `ret` and `keep` #### The hole @@ -870,80 +827,6 @@ `ret` does. D's experience is that the corners (fields, generics, procedure values) take years; the narrow form here leaves record lifetimes out, and so the first false alarm above, on purpose. - -### Part 4: a `const` parameter - -``` -proc is*(const sc: Scanner; s: []U8): BOOL; -proc text*(ret const sc: Scanner): []U8; -``` - -A `const` parameter is the caller's variable itself, as a `var` one -is, and the procedure may only read it: it may not assign it or a -part of it, give it to a `var` parameter, nor take its `ADR`. Nothing -is copied, whatever its size. Its argument is any expression of the -parameter's type: a variable is passed in place; anything else, a -call's result, a literal, an expression, goes in a temporary of the -statement, and its address is passed. - -It is passed by its address always, so what it means is plain: if the -variable changes while the procedure runs, through a `var` parameter, -a pointer or another thread, the procedure sees the change. -`f(var x, x)`, with `f`'s second parameter `const`, reads `x` as it now -is. (Ada leaves the choice of copy or address to the compiler, and a -program that depends on it is wrong; raz fixes the address, which is -the cheaper and the one a reader expects.) - -It is never marked at the call: the procedure neither changes nor -keeps what it is given (part 1, rule 8). With it, `STANDARDS.md` says: -a record or array only read is a value when it is a few words, and -`const` when it is more; `var` is for what is changed. So `var` at a -call always means a change, and the marks of part 1 stay true. - -A `const` parameter whose type holds an array lends the caller's -variable to `ret`, as a `var` one does (part 3, "Words"). A procedure -type says `const` where its procedure does, and two signatures are -alike only if their `const`s are (5.9). A code procedure receives a -`const` parameter's address in a register, as a `var` one's; `extern` -procedures take none. - -Met in porting the compiler to raz, where the seed's `const x: TItem` -(about 130 bytes) became a value parameter, copied at every call. -Measured once the port built: not what stands out, the compiler's -profile is flat (`Scan.tokPos` is 9%). Met again here, where `Opt`'s -`Parser` and the tools' `Scanner`, many words, are only read by many -procedures, and `var` for them would be a mark that isn't true. - -Costs: the report, 8 and 5.9; the compiler, 30 to 60 lines: the word -in a heading, passing as `var` does, the temporary, and the checks -above. The seed knows `const` parameters in Pascal, not in raz; `src/` -uses them after the new seed, as it does the marks. - -### Part 5: a constant holds a variant - -Today a typed constant holds no variant (4: "no variant and no other -pointer"), and `src/Parse.raz` says "a constant holds no variant". So a -table of `Opt.Row`s, a variant, can't be a constant, and part 3 makes -the literal table at `Opt.init`'s call an error. The plain place for -such a table is a constant: - -``` -const Rows: []Opt.Row = [ - Opt.usage("", "Sh [-c script | file]"), - Opt.option("", 'c', "command", "script", "run script, not standard input"), - Opt.sub("", "run", "run the file named")]; -``` - -A variant's case built from constants, `Opt.usage("", "...")`, or a -case without fields, `dot`, is a constant of the variant's type, and a -variant may stand among a constant's elements and fields as a record -may. Its bytes are as 5.6 lays them out: the case's number, then its -fields, the rest zero. Nothing else changes: a constant is still read -only, and a `match` on one works as on a variable. - -Costs: the report, 4, one line; the compiler, the variant's branch of -the constant writer in `Parse.raz`, which writes the tag and the -case's fields as it writes a record's: about 20 lines. ## A minus sign after an operator diff --git a/STANDARDS.md b/STANDARDS.md index bc93240..cd0e287 100644 --- a/STANDARDS.md +++ b/STANDARDS.md @@ -6,18 +6,21 @@ do. New code keeps to it; old code comes to it when it is next changed, or, for the library's interfaces, at a thaw (`PROPOSALS.md`). -## Parameters: a value, `var`, or a pointer +## Parameters: a value, `const`, `var`, or a pointer -A procedure takes a variable it is given in one of three ways, by what +A procedure takes a variable it is given in one of four ways, by what it does with it: + +| The procedure | The parameter | Example | +|---|---|---| +| reads it, a few words at most (a handle, a slice, a small record) | a value | `Buf.bytes(b: Buf)` | +| reads it, more than a few words | `const` | `Opt.number(const p: Parser; lo, hi: I64)` | +| changes it, and lets go of it when it returns | `var` | `Buf.add(var b: Buf; c: U8)` | +| keeps it past the call, in a record, a module variable or another thread | a pointer, `^T` | `Thread.spawn(p, arg: ^U8)` | -- **It reads it**: a value, when the type is a few words at most (a - handle, a slice, a small record). A larger record or array that is - only read is `var`, since a value parameter is a copy (8; a `const` - parameter waits in `PROPOSALS.md`). -- **It changes it, and lets go of it when it returns**: `var`. -- **It keeps it**, in a record, a module variable or another thread, - past the call: a pointer, `^T`. +A `const` parameter is passed in place, as a `var` one is, so nothing +is copied; and since it can't be changed, `var` in a heading always +means the procedure changes what it is given. So `ADR` at a call says the callee keeps what it is given. A reader who sees `Pg.start(q, ADR(c), sql)` knows that `q` is changed and `c` diff --git a/doc/raz.md b/doc/raz.md index 6ca55fc..c53edea 100644 --- a/doc/raz.md +++ b/doc/raz.md @@ -13,7 +13,7 @@ This report defines the language. It is short on purpose. Whatever it doesn't allow, raz doesn't have. -This is version 2026-10.32 of the report; section 11 says where each +This is version 2026-10.33 of the report; section 11 says where each version's changes are told. 2026-10.11 and 2026-10.12 are a thaw the owner chose, for a language anyone can build again. They break programs written for 2026-10.10 and before: for 2026-10.9, say, the report of compiler @@ -41,7 +41,15 @@ 5.7); and `continue` (section 7.2), which breaks a program that has a name `continue`. 2026-10.32 is an eighth thaw: a constant typed `[]T` may be `[]`, the empty slice (section 4); it breaks no program. +2026-10.33 is a ninth: a `const` parameter, the caller's variable read +in place (section 8), and a variant in a constant (section 4), which +break no program; and the library's procedures that took a pointer to +what they work on take a `var` parameter, a `const` one or a value +instead (the library volume), which breaks a program that calls them +with `ADR`. +The compiler follows version 2026-10.32 until the library has moved. + The language is frozen, softly, from this version on: it changes only to mend a defect, and its library grows only by what breaks no program written for it. What anyone wishes it had waits on a list, for a later @@ -358,10 +366,10 @@ procedure may be declared `forward` and defined later (section 8). **Constants** are evaluated by the compiler. Their expressions use only -literals, other constants, operators, conversions, `SIZE` and the `LEN` -of arrays and strings. A constant is an integer, a real, a `BOOL`, a -name of an enumeration, a set, a string, an array, a record or an -`ERROR`. A constant of an integer type wraps as the +literals, other constants, operators, conversions, `SIZE`, the `LEN` +of arrays and strings, and a variant's cases. A constant is an integer, +a real, a `BOOL`, a name of an enumeration, a set, a string, an array, +a record, a variant or an `ERROR`. A constant of an integer type wraps as the program would: `U8(250) + 10` is 4. Dividing by zero in a constant is an error, and so is dividing the smallest value of a signed type by -1, with `div` or `mod`, which stops a program (section 6.1). A string constant can be indexed and sliced as an array of @@ -375,18 +383,21 @@ w := Text[4..7]; (* "cat" *) ``` -A constant with a type is an `ERROR` (section 5.8), an array or a -record, and the value of an array or record constant is a literal -(section 6.5) of constants. Its elements and fields are integers, -reals, `BOOL`s, enumerations, sets, `ERROR`s, procedures, `nil`, a -string for a `[]U8`, an array constant or the empty slice for a slice -of its element type, and arrays and records of these; no variant and -no other pointer: +A constant with a type is an `ERROR` (section 5.8), an array, a +record or a variant, and the value of an array or record constant is a +literal (section 6.5) of constants, that of a variant one a case built +from constants, as a program builds one (section 5.6). Its elements +and fields are integers, reals, `BOOL`s, enumerations, sets, `ERROR`s, +procedures, `nil`, a string for a `[]U8`, an array constant or the +empty slice for a slice of its element type, and arrays, records and +variants of these; no other pointer. A variant constant must have a +type: `const D = dot;` is an error, `const D: Shape = dot;` isn't. ``` const Primes: [5]I64 = [2, 3, 5, 7, 11]; type Rule = record class: []U8; tag: INT; float: BOOL; end; const Rules: [2]Rule = [{class: "firefox", tag: 2}, {class: "mpv", float: true}]; +const Shapes: []Shape = [circle(1.0), rect(2.0, 3.0), dot]; (* Shape of section 5.6 *) ... p := Primes[i]; ``` @@ -628,9 +639,9 @@ A value of a procedure type is the address of a procedure with that signature, as big as an address, or `nil`. Two signatures are alike when -their parameters have the same types, each `var` or not alike, in order, -and their results are the same types; the parameters' names don't -matter. +their parameters have the same types, each `var`, `const` or neither +alike, in order, and their results are the same types; the parameters' +names don't matter. ### 5.10 Same and assignable @@ -1167,8 +1178,9 @@ `match` takes a variant, an enumeration, an integer type (a `U8` included) or a string, a `[]U8`; other types are an error. It runs the arm of the case `s` holds. An arm may name the case's fields, in order, all of them; the names stand for those fields of `s` -itself, so assigning to one changes `s`; if `s` isn't a variable, they -stand for the fields of a copy. An arm may also leave the fields +itself, so assigning to one changes `s`; if `s` is a constant or a +`const` parameter, they can't be assigned; if `s` isn't a variable, +they stand for the fields of a copy. An arm may also leave the fields unnamed. Every case must have an arm, and only one, unless there is an `| else:` arm, which must come last. @@ -1404,7 +1416,7 @@ | "var" { ident { "," ident } ":" Type ";" } . Signature = "(" [ Params { ";" Params } [ ";" "..." ] ] ")" [ ":" Results ] | [ ":" Results ] . -Params = [ "var" ] ident { "," ident } ":" Type . +Params = [ "var" | "const" ] ident { "," ident } ":" Type . Results = Type | "(" Type { "," Type } ")" . ``` @@ -1418,6 +1430,39 @@ caller's variable itself; its argument must be a variable of exactly the parameter's type. +A `const` parameter is the caller's variable too, read in place, +whatever its size, with nothing copied; the procedure may only read it. +It may not assign it or a part of it (a field, an element of an array +in it), give it or a part of it to a `var` parameter, take the `ADR` of +either, write it through a view (section 6.9), or assign the names a +`match` arm gives its fields. What a slice or a pointer in it points at +isn't part of it: the procedure may write through them, as it may +through a value parameter's. Its argument is any expression of the +parameter's type: a variable or a constant is passed in place, anything +else as a temporary of the statement, as an array a call returns is +(below). If the variable changes while the procedure runs, through a +`var` parameter, a pointer or another thread, the procedure sees the +change. + +``` +type Parser = record args: [][]U8; at: INT; (* ... *) end; + +proc current(const p: Parser): []U8; (* reads p, many words, in place *) +begin + return p.args[p.at]; +end; + +proc advance(var p: Parser); (* changes p *) +begin + INC(p.at); +end; +``` + +A procedure takes what it reads as a value when it is a few words, and +as `const` when it is more; what it changes, as `var`; and a pointer +only for what it keeps past the call, what may be `nil`, an allocator, +or what C or the system takes. + **Results.** A procedure returns nothing, one value, or several. A call that returns several values can only stand on the right of a multiple assignment, alone as a statement, or alone after `return` in a @@ -1484,8 +1529,8 @@ instruction the assembler doesn't know. It is called like any procedure, with the System V convention: integer -and pointer arguments arrive in `rdi`, `rsi`, `rdx`, `rcx`, `r8` and -`r9`, `F32` and `F64` ones in `xmm0` to `xmm7`, and the result goes in +and pointer arguments, and the addresses of `var` and `const` ones, +arrive in `rdi`, `rsi`, `rdx`, `rcx`, `r8` and `r9`, `F32` and `F64` ones in `xmm0` to `xmm7`, and the result goes in `rax` (and `rdx` for a second) or `xmm0`. Its parameters and results are therefore of the types C can take (section 9), or slices, at most six integer and eight real parameters, and at most two results. A @@ -1591,7 +1636,8 @@ through a checked variable. Dereferencing a stale or made-up pointer, indexing through a pointer, a slice kept after the call whose frame holds what it points at (section 7.3), a slice of an array a call -returned kept past its statement (section 8), an index out of range in a +returned, or of a temporary given to a `const` parameter, kept past +its statement (section 8), an index out of range in a module that says `check off;`, writing a string's bytes, a code procedure that breaks its rules, and C code can read or write any memory, and what follows isn't defined. Dereferencing `nil`, dividing @@ -1801,7 +1847,7 @@ | "var" { ident { "," ident } ":" Type ";" } . Signature = "(" [ Params { ";" Params } [ ";" "..." ] ] ")" [ ":" Results ] | [ ":" Results ] . -Params = [ "var" ] ident { "," ident } ":" Type . +Params = [ "var" | "const" ] ident { "," ident } ":" Type . Results = Type | "(" Type { "," Type } ")" . ExternDeclaration = "extern" ( "proc" IdentDef Signature | "var" IdentDef ":" Type ) . diff --git a/doc/report-history.md b/doc/report-history.md index ae894f6..da02095 100644 --- a/doc/report-history.md +++ b/doc/report-history.md @@ -198,3 +198,16 @@ 4). Before, both were errors, since an array has at least one element. It breaks no program. `Vec` gains `pop`, and `Sys.Info` `links`, how many names a file has (volume 2). +- **2026-10.33.** A thaw, the ninth. A parameter may be `const`: the + caller's variable, read in place with nothing copied, which the + procedure may not change; its argument is any expression of its type + (sections 5.9, 7.4, 8 and 10). A typed constant may be a variant, or + hold variants among its elements and fields, each a case built from + constants (section 4). Neither breaks a program. The library's + procedures that took a pointer to what they work on, and kept none, + take it as a `var` parameter when they change it, as a `const` one or + a value when they only read it: `Arena`, `Buf`, `Files.read` and + `load`, `Fmt.write`, `List`, `Lock`, `Map`, `Opt`, `Str.join` and + `replace`, `Sys`'s directories, `Thread.lock` and `unlock`, `Vec` + (volume 2). That breaks a program that calls them with `ADR`; it is + mended by writing `x` for `ADR(x)`, and `p^` for a pointer `p`.