quarb 0.73.0

Query engine for arbors (arboreal graphs - tree-spanned or tree-dominated graphs)
Documentation
//! A command of the aggregation pipe right after a list the text
//! writes, inside parentheses, is refused when the query is parsed
//! (ruling 2026-11-22: "yes for the guard").
//!
//! Inside parentheses a list held in one thread is one thread, as at
//! the top level, and `@|` reads the threads it is handed (ruling of
//! the same day; until 0.73 such a list was opened into its items
//! first). Where the head of the parenthesis is WRITTEN as a list,
//! the text alone shows that one thread holding a list is what the
//! command will meet: `(@(3; 1; 2) @| sort)` sorts one thread and
//! answers the list as written. Nothing useful is spelled that way,
//! and a query written for the opened list would answer otherwise,
//! or, in the body of an open push, grow its value each round (the
//! label propagation `.:c(@($.c; (--tie):.c) @| sort @| first){1;}`
//! took the list whole and ran without end). So the spelling is
//! retired as the others of 0.73 are: refused, naming the plain pipe
//! and the written spread.
//!
//! A written list is a list constructor, the context's topics
//! (`@_`), its positions (`@@_`, with a projection or without) or
//! the pushed values (`@.`), bare or under a condition in brackets,
//! or read through to a field or a property, which is read on each
//! item. A position in the brackets names an item and is no such
//! head, and a head whose value the data decides (`$.c`, `::tags`, a
//! path, a computed bracket) is the run's: it answers, and the
//! warning LONE_LIST tells of one thread holding a list.
//!
//! The check runs on the query the parser returns, the definitions
//! expanded, beside the check of the quantifiers' facts.

use super::ast::visit::{self, Visitor};
use super::ast::{Command, IndexSpec, Operand, Query, RegRef};

/// Whether the text writes this operand as a list.
fn written_list(o: &Operand) -> bool {
    match o {
        Operand::List(_)
        | Operand::Topics
        | Operand::Positions { .. }
        | Operand::Recall(RegRef::Whole) => true,
        // a condition keeps the items that hold: a list still
        Operand::Index {
            base,
            rows: IndexSpec::Cond(_),
            cols: None,
        } => written_list(base),
        // a field or a property read off a list is read on each item
        Operand::Field { base, .. } | Operand::Through { base, .. } => written_list(base),
        _ => false,
    }
}

/// The head as the message names it: as written, a list
/// constructor of several items by its first.
fn head_text(o: &Operand) -> String {
    match o {
        Operand::List(items) if items.len() > 1 => {
            format!("@({}; …)", super::unparse::operand(&items[0]))
        }
        other => super::unparse::operand(other),
    }
}

fn refusal(head: &Operand, first: &Command) -> String {
    let head = head_text(head);
    let command = super::unparse::command(first);
    let bare = command.strip_prefix("@| ").unwrap_or(&command);
    match first {
        Command::Select(_) => format!(
            "'({head} {command})': a list written there is one thread, and '@|' selects \
             among threads; the bracket on the list reads it, '{head}{bare}', and \
             '| ... {command}' opens it into threads first"
        ),
        _ => format!(
            "'({head} {command})': a list written there is one thread, and '@|' reads \
             threads; '| {bare}' reads the list, '| ... {command}' opens it into threads \
             first"
        ),
    }
}

struct Check {
    refusal: Option<String>,
}

impl Visitor for Check {
    fn operand(&mut self, o: &Operand) {
        if self.refusal.is_some() {
            return;
        }
        if let Operand::Piped { expr, commands } = o
            && written_list(expr)
            && let Some(first @ (Command::Agg(_) | Command::Select(_))) = commands.first()
        {
            self.refusal = Some(refusal(expr, first));
            return;
        }
        visit::walk_operand(self, o);
    }
}

/// Refuse a command of the aggregation pipe right after a written
/// list inside parentheses.
pub(crate) fn check(q: &Query) -> Result<(), String> {
    let mut v = Check { refusal: None };
    v.query(q);
    match v.refusal {
        Some(why) => Err(why),
        None => Ok(()),
    }
}