Skip to content

Latest commit

 

History

History
54 lines (43 loc) · 6.33 KB

read-paths.md

File metadata and controls

54 lines (43 loc) · 6.33 KB

An expression's read paths

Status: accepted (CMD-485, from the inbox's IN-7, CR-227, 2026-11-29), for 0.14.

What a host learns of the paths an expression reads under a root, md.doc say, through comprehension variables too. Today compmd::cel::Program::selections(path) and indexings(path) list the fields read by dot and by index directly under a fixed path; in md.doc.tasks.map(t, t.hours) neither says that t.hours reads hours of the elements of md.doc.tasks, and a host can't work it out safely, since comprehensions nest and a variable may hide another of its name.

A host whose records hold records needs it to:

  • rename a field wherever an expression reads it (hours in t.hours, in md.doc.tasks[0].hours);
  • read only what an expression reads, as deep as it is written (md.doc.book.author.name, the hours of each of md.doc.tasks);
  • type records only along those paths, since a cel::Type has no recursive or named records, and a schema with cycles (a task's parent task) can't be typed whole.

Decisions

  1. A method of its own, Program::paths(root). selections and indexings stay as they are, for their callers (the language server's key rename). paths("md.doc") gives each path the expression reads under md.doc, in the order written, each a list of segments:

    • Field { name, range, by }: a field by dot (.title, .?title, has(x.title)), or by a string literal index (["title"], [?"title"]); range is where the name is written in code points: the name after the dot, or the whole literal, its prefix and quotes with it, so a host rewrites it as a literal; by is Dot or Index.
    • Index { at }: an index that isn't a string literal, a number ([0]) or computed ([i], [name]): a list's element or a map's value, whichever the value is.
    • Each { at }: what a comprehension over it binds its variable to: a list's elements, or a map's keys (CEL iterates a map's keys); at is where the macro's name is written.
  2. A path is as deep as written, and one is given for each place it ends. md.doc.book.author.name is one path, book, author, name; it ends where the chain of dots and indexes does: at an operator, a call (md.doc.title.size() reads title), or the expression's end. md.doc read whole is a path with no segments.

  3. A comprehension's variable carries its range's path. In md.doc.tasks.map(t, t.hours), t stands for tasks, Each, so t.hours is the path tasks, Each, hours. The range itself isn't given as a path of its own when the variable is read, since the variable's paths say how far it is read; it is when the variable isn't (md.doc.tasks.all(t, true) reads tasks, Each). Variables nest (t.subtasks.map(s, s.hours) is tasks, Each, subtasks, Each, hours), and a variable hides one of its name, and the root's first name, inside its comprehension, as in selections. optMap and optFlatMap bind theirs to the optional's value, so it stands for the optional's path itself: md.doc.?book.optMap(b, b.author) is book, author (settled in building it). The paths come in the order they end in the expression.

  4. filter passes its range's elements on. md.doc.tasks.filter(t, t.done) is a list of tasks' elements, so what is read of its elements is read of tasks' elements:

    • a comprehension over it binds its variable to them: in md.doc.tasks.filter(t, t.done).map(u, u.hours), u.hours is tasks, Each, hours;
    • an index of it, plain or optional, is one of them, though not the element of tasks at that index: in md.doc.tasks.filter(t, !t.done)[0].hours, the first open task's hours, the path is tasks, Each, hours, and so it is for [?0].?hours;
    • the list read whole (.size(), an operator) reads tasks.

    A map's result is new values, and no other macro's result is a list of the range's, so nothing read of them is of tasks.

  5. What is read of a value with no path isn't given. A variable whose range has no path under the root (a map's result, a list built by + or ?:, a list literal) is a value of its own: in md.doc.tasks.map(t, t.owner).map(o, o.name), t.owner is tasks, Each, owner, but o.name is no path, and isn't given. A host that types records along the paths an expression reads types such a variable as dyn, so its fields aren't checked.

  6. check is unchanged. Typing records lazily, by path, would change the checker's Scope for every caller; with paths, a host builds its types along the paths an expression reads, before it checks it. A host that types a record with only the fields read can't suggest the field meant for a misspelt one (t.hors), so it types each record on a path with all of its own fields and stops at the records it links to that no path reaches; that is the host's, and needs nothing more of the processor (settled with the requester in CR-227). A follow-up, if a host still needs lazy types.

  7. Rust only. The WebAssembly package doesn't give selections today, and the host that asks uses the crate.

Examples

Expression paths("md.doc")
md.doc.price * 2 price
md.doc["due"] < md.today() due (by index)
md.doc.book.author.name book, author, name
md.doc.tasks.map(t, t.hours).size() tasks, Each, hours
md.doc.tasks.filter(t, t.done).map(u, u.hours) tasks, Each, done; tasks, Each, hours
md.doc.tasks.filter(t, !t.done)[0].hours tasks, Each, done; tasks, Each, hours
md.doc.tasks.map(t, t.owner).map(o, o.name) tasks, Each, owner (o.name reads a value with no path)
md.doc.tasks.exists(t, t.parent.done) tasks, Each, parent, done
has(md.doc.book.author) book, author
md.doc.tasks[0].hours + md.doc.tasks[i].hours tasks, Index, hours; tasks, Index, hours
md.doc.tasks.all(t, true) tasks, Each
md.doc.tasks.map(md, md.doc.x) tasks, Each, doc, x (the variable md hides the root: it is the element)
size(md.doc.tags) tags
md.doc (no segments)

Changes

  • crates/processor/src/cel/paths.rs: the walk, ReadPath and Segment, beside selections.rs; Program::paths in cel.rs, documented with an example.
  • Tests: each row above, nesting, shadowing, optional selection and indexing, ranges in code points.
  • The processor's README, 0.14.0's entry.