feat(lang): 27.3 - a reducer on a table's row: reducer T in S.table on A, @Target, Row<T>, @RowVerb, @Column
An action names one row of a ludic.base Table<T> by its handle, in a field marked @Target, and `reducer Deer in Herd.deer on Spook(r: mut Row<Deer>, n: Noise, a: Spook)` runs once, for that row alone (the table may sit down a path, S.w.tab). The drain resolves the handle (tb_row) and hands the reducer a Row<T> - new in ludic.base: tb, row, h, rec - that the queue keeps, one per row reducer, filled in place, so a targeted action allocates nothing; a stale handle runs nothing, and LUDIC_ACTIONS_LOG=1 prints a line for it (@alloc_ok). Row reducers order among an action's by their state's name, then the table's path. Checked at compile time (actions_rows.ludic): the row reaches r.rec and r.h only - r.tb / r.row refused, the view never assigned, stored, copied or handed on except to a @RowVerb (a function of the record's own module taking Row<T> first; any other function taking a row is refused); a field marked @Column (a table column mirrors it) is not written through r.rec; only the module owning the state declares a row reducer; one @Target, an int, per action; the states between the row and the action are read. `mut` is allowed on a Row<T> parameter. ludic schema's code section gains row_reducers (record, table, state, action, target, predicted, net, module, at) and row_verbs (name, record, module, at), and every action its target; row reducers are left out of `reducers`. ludic deps and ludic-lsp name a row reducer `reducer Deer in Herd.deer on Spook`. vocab: @Target, @Column, @RowVerb; docs/language pages; LANGUAGE.md "A reducer on a row"; examples actions/rows and ten rejects; test.ludic feat, reject and schema cases (not run); changes/row-reducers.md. Reseeded; bootstrap-cfree fixpoint holds (307497 lines); Maroon Lake's `ludic build --check` is clean against this tree. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
d22abae476
commit
ab0b84b87e
37 changed files with 125923 additions and 110956 deletions
44
examples/actions/rows.ludic
Normal file
44
examples/actions/rows.ludic
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
# rows.ludic - 27.3: a reducer on a row. An action that names one row of a Table by its handle
|
||||
# (@Target) is reduced for that row alone: the drain resolves the handle and hands the reducer a
|
||||
# Row it keeps, so nothing is allocated. A field a column mirrors (@Column) is written only by the
|
||||
# record's @RowVerbs, which keep the column with it; a handle whose row is gone runs nothing.
|
||||
import "ludic.base"
|
||||
program RowActions {
|
||||
numbers float
|
||||
property Deer {
|
||||
@Column x: float = 0.0 # mirrored by column 0 of the herd's table
|
||||
fear: int = 0
|
||||
}
|
||||
state Herd { deer: Table<Deer> = null }
|
||||
state Noise { loud: int = 2 }
|
||||
action Spook { @Target who: int = -1, by: int = 1 }
|
||||
action Bolt { @Target who: int = -1, dx: float = 0.0 }
|
||||
|
||||
reducer Deer in Herd.deer on Spook(r: mut Row<Deer>, n: Noise, a: Spook) {
|
||||
r.rec.fear += a.by * n.loud
|
||||
if r.rec.fear > 3 { dispatch Bolt { who: r.h, dx: 5.0 } } # a follow-on, for the same row
|
||||
}
|
||||
reducer Deer in Herd.deer on Bolt(r: mut Row<Deer>, a: Bolt) { deer_move(r, r.rec.x + a.dx) }
|
||||
reducer Noise on Spook(n: mut Noise, a: Spook) { n.loud += 1 } # a state's reducer on the same action
|
||||
|
||||
@RowVerb function deer_move(r: mut Row<Deer>, x: float) -> void {
|
||||
r.rec.x = x
|
||||
tb_set_f(r.tb, 0, r.row, x)
|
||||
}
|
||||
|
||||
entry (h: mut Herd) {
|
||||
let tb: Table<Deer> = table_new(1, 0)
|
||||
h.deer = tb
|
||||
let a = tb_add(tb, new Deer)
|
||||
let b = tb_add(tb, new Deer { x: 10.0 })
|
||||
let c = tb_add(tb, new Deer)
|
||||
tb_remove(tb, c)
|
||||
dispatch Spook { who: a, by: 1 } # Noise's reducer runs after Deer's: 2, then 3 for b
|
||||
dispatch Spook { who: b, by: 2 } # 6 > 3: b bolts
|
||||
dispatch Spook { who: c, by: 9 } # c is gone: its row reducers run nothing
|
||||
drain_actions()
|
||||
let ra = tb_row(tb, a)
|
||||
let rb = tb_row(tb, b)
|
||||
print(`{tb_rec(tb, ra).fear} {tb_rec(tb, rb).fear} {int(tb_rec(tb, rb).x)} {int(tb_f(tb, 0, rb))}`)
|
||||
}
|
||||
}
|
||||
12
examples/rejected/row_reducer_column.ludic
Normal file
12
examples/rejected/row_reducer_column.ludic
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
# 27.3: a field a column mirrors is written only by a @RowVerb, which keeps the column with it
|
||||
import "ludic.base"
|
||||
program RowReducerColumn {
|
||||
property Deer {
|
||||
@Column x: int = 0
|
||||
fear: int = 0
|
||||
}
|
||||
state Herd { deer: Table<Deer> = null }
|
||||
action Push { @Target who: int = -1, dx: int = 1 }
|
||||
reducer Deer in Herd.deer on Push(r: mut Row<Deer>, a: Push) { r.rec.x += a.dx }
|
||||
entry { print(1) }
|
||||
}
|
||||
9
examples/rejected/row_reducer_no_target.ludic
Normal file
9
examples/rejected/row_reducer_no_target.ludic
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
# 27.3: a row reducer's action names its row in a field marked @Target
|
||||
import "ludic.base"
|
||||
program RowReducerNoTarget {
|
||||
property Deer { fear: int = 0 }
|
||||
state Herd { deer: Table<Deer> = null }
|
||||
action Spook { who: int = -1 }
|
||||
reducer Deer in Herd.deer on Spook(r: mut Row<Deer>, a: Spook) { r.rec.fear += 1 }
|
||||
entry { print(1) }
|
||||
}
|
||||
10
examples/rejected/row_reducer_not_verb.ludic
Normal file
10
examples/rejected/row_reducer_not_verb.ludic
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# 27.3: a row is handed only to its record's @RowVerbs
|
||||
import "ludic.base"
|
||||
program RowReducerNotVerb {
|
||||
property Deer { fear: int = 0 }
|
||||
state Herd { deer: Table<Deer> = null }
|
||||
action Spook { @Target who: int = -1 }
|
||||
function scare(r: mut Row<Deer>) -> void { r.rec.fear += 1 }
|
||||
reducer Deer in Herd.deer on Spook(r: mut Row<Deer>, a: Spook) { scare(r) }
|
||||
entry { print(1) }
|
||||
}
|
||||
7
examples/rejected/row_reducer_owner.ludic
Normal file
7
examples/rejected/row_reducer_owner.ludic
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# 27.3: a table's rows are reduced only by the module that owns its state
|
||||
import "ludic.base"
|
||||
import "row_reducer_owner/herd"
|
||||
program RowReducerOwner {
|
||||
reducer Deer in Herd.deer on Spook(r: mut Row<Deer>, a: Spook) { r.rec.fear += 1 }
|
||||
entry { print(1) }
|
||||
}
|
||||
5
examples/rejected/row_reducer_owner/herd/index.ludic
Normal file
5
examples/rejected/row_reducer_owner/herd/index.ludic
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# herd/index.ludic - the herd's state and its table; its rows are its own module's to reduce
|
||||
module herd uses ludic_base
|
||||
export property Deer { fear: int = 0 }
|
||||
export state Herd { deer: Table<Deer> = null }
|
||||
export action Spook { @Target who: int = -1 }
|
||||
9
examples/rejected/row_reducer_params.ludic
Normal file
9
examples/rejected/row_reducer_params.ludic
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
# 27.3: a row reducer takes its row, mut, first and the action last
|
||||
import "ludic.base"
|
||||
program RowReducerParams {
|
||||
property Deer { fear: int = 0 }
|
||||
state Herd { deer: Table<Deer> = null }
|
||||
action Spook { @Target who: int = -1 }
|
||||
reducer Deer in Herd.deer on Spook(r: Row<Deer>, a: Spook) { print(r.rec.fear) }
|
||||
entry { print(1) }
|
||||
}
|
||||
11
examples/rejected/row_reducer_reach.ludic
Normal file
11
examples/rejected/row_reducer_reach.ludic
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
# 27.3: a row reducer reaches its row's record and handle, not its table
|
||||
import "ludic.base"
|
||||
program RowReducerReach {
|
||||
property Deer { fear: int = 0 }
|
||||
state Herd { deer: Table<Deer> = null }
|
||||
action Spook { @Target who: int = -1 }
|
||||
reducer Deer in Herd.deer on Spook(r: mut Row<Deer>, a: Spook) {
|
||||
tb_remove(r.tb, r.h)
|
||||
}
|
||||
entry { print(1) }
|
||||
}
|
||||
9
examples/rejected/row_reducer_table.ludic
Normal file
9
examples/rejected/row_reducer_table.ludic
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
# 27.3: a row reducer names a Table of its record in its state
|
||||
import "ludic.base"
|
||||
program RowReducerTable {
|
||||
property Deer { fear: int = 0 }
|
||||
state Herd { count: int = 0 }
|
||||
action Spook { @Target who: int = -1 }
|
||||
reducer Deer in Herd.count on Spook(r: mut Row<Deer>, a: Spook) { r.rec.fear += 1 }
|
||||
entry { print(1) }
|
||||
}
|
||||
10
examples/rejected/row_reducer_writes_state.ludic
Normal file
10
examples/rejected/row_reducer_writes_state.ludic
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# 27.3: a row reducer writes its row alone; the states it declares are read
|
||||
import "ludic.base"
|
||||
program RowReducerWritesState {
|
||||
property Deer { fear: int = 0 }
|
||||
state Herd { deer: Table<Deer> = null }
|
||||
state Noise { loud: int = 0 }
|
||||
action Spook { @Target who: int = -1 }
|
||||
reducer Deer in Herd.deer on Spook(r: mut Row<Deer>, n: mut Noise, a: Spook) { n.loud += 1 }
|
||||
entry { print(1) }
|
||||
}
|
||||
7
examples/rejected/rowverb_no_row.ludic
Normal file
7
examples/rejected/rowverb_no_row.ludic
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# 27.3: a @RowVerb takes its row first
|
||||
import "ludic.base"
|
||||
program RowVerbNoRow {
|
||||
property Deer { fear: int = 0 }
|
||||
@RowVerb function scare(n: int) -> void { print(n) }
|
||||
entry { print(1) }
|
||||
}
|
||||
6
examples/rejected/target_twice.ludic
Normal file
6
examples/rejected/target_twice.ludic
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
# 27.3: an action names one row: one @Target field
|
||||
import "ludic.base"
|
||||
program TargetTwice {
|
||||
action Swap { @Target a: int = -1, @Target b: int = -1 }
|
||||
entry { print(1) }
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue