(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*) (** The value semantics an engine runs under. The engine ({!Vdbe_core}, {!Ir}, {!Closure}) decides nothing about values by itself: how two values compare, what counts as true, what an operator computes, how text collates and how an aggregate folds are a dialect's rules, given as a module of type {!S}. The operators below are the vocabulary the engine and a dialect share; their meaning is the dialect's. *) (** The type for comparison operators. *) type cmp = Eq | Ne | Lt | Le | Gt | Ge (** The type for arithmetic and bitwise operators. *) type arith = Add | Sub | Mul | Div | Mod | Band | Bor | Shl | Shr type agg_acc = { step : Catalog.Value.t array -> int -> unit; (** [step row args] folds one input row, whose argument values are [row.(args)] through [row.(args + nargs - 1)]. They are read during the call, so [row] may be reused for the next one. *) step_reg : Catalog.Register.t array -> int -> unit; (** [step_reg regs args] is {!field-step} over the registers [regs], read in place during the call, as sqlite3's OP_AggStep reads its registers. *) finish : unit -> Catalog.Value.t; (** [finish ()] is the folded value. *) } (** The type for one aggregate's running fold over a group's rows. Its whole state is the fold's own, so a group costs its answer rather than its input. *) (** The type for a dialect's semantics over registers: what the register VM ({!Vdbe_core}) compares, tests and computes by. *) module type REGISTER = sig type affinity (** The type for what a comparison converts both operands to before it compares them. *) val truth : Catalog.Register.t -> bool (** [truth r] is whether [r] passes a filter. *) val compare : Catalog.Register.t -> Catalog.Register.t -> int (** [compare a b] orders two registers, neither of them NULL, negative, zero or positive as [a] is below, equal to or above [b]. *) val compare_with : affinity option -> Catalog.Register.t -> Catalog.Register.t -> int (** [compare_with aff a b] is {!compare} with [aff] applied to both operands first. *) val settles : affinity option -> bool (** [settles aff] is [true] iff {!settle} [aff] converts some register: a comparison under another affinity has nothing to settle. *) val settle : affinity option -> Catalog.Register.t -> unit (** [settle aff r] applies to [r] once what {!compare_with} [aff] applies to an operand at every comparison, where that depends on [r] alone, so that a constant operand is converted before the first row rather than at each. It is idempotent. *) val compare_settled : affinity option -> Catalog.Register.t -> Catalog.Register.t -> int (** [compare_settled aff a b] is {!compare_with} [aff a b] for a [b] that {!settle} [aff] has settled. *) val arith : arith -> Catalog.Register.t -> Catalog.Register.t -> Catalog.Register.t -> unit (** [arith op dst a b] sets [dst] to [a op b]. *) end (** The type for a dialect's value semantics: its semantics over registers, and what the IR ({!Ir}) evaluates a value, a sort and an aggregate by. *) module type S = sig include REGISTER type collation (** The type for the collating sequences text is compared under. *) type op (** The type for the binary operators the engine has no opcode for, which it carries opaquely and evaluates through a run's hooks. *) type action (** The type for what a trigger program's [RAISE()] does to the statement that fired it. *) val collate : collation -> Catalog.Value.t -> Catalog.Value.t -> int (** [collate c a b] orders [a] and [b] under the collating sequence [c], as a sort key, a [DISTINCT] or a [GROUP BY] carrying [c] orders them. *) val key : collation -> string -> string (** [key c s] is the bytes [c] compares the text [s] by: two texts collate equal exactly when their keys are byte-equal. *) val compare_value : cmp -> affinity option -> collation option -> Catalog.Value.t -> Catalog.Value.t -> Catalog.Value.t (** [compare_value op aff coll a b] is the value of the comparison [a op b] under the affinity [aff] and the sequence [coll]. *) val is_eq : affinity option -> collation option -> Catalog.Value.t -> Catalog.Value.t -> Catalog.Value.t (** [is_eq aff coll a b] is the value of the NULL-safe equality [a IS b]. *) val in_list : affinity option -> collation option -> Catalog.Value.t -> Catalog.Value.t list -> Catalog.Value.t (** [in_list aff coll v cands] is the value of [v IN (cands)], each candidate compared to [v] under [aff] and [coll]. *) val and_ : Catalog.Value.t -> Catalog.Value.t -> Catalog.Value.t (** [and_ a b] is the value of [a AND b]. *) val or_ : Catalog.Value.t -> Catalog.Value.t -> Catalog.Value.t (** [or_ a b] is the value of [a OR b]. *) val not_ : Catalog.Value.t -> Catalog.Value.t (** [not_ v] is the value of [NOT v]. *) val op_calls : op -> bool (** [op_calls op] is [true] iff the dialect evaluates [op] as a function call, which decides when a constant holding it is computed. *) val deterministic : string -> bool (** [deterministic name] is [true] iff a call of the function [name] answers the same value for the same arguments throughout a run. *) val aggregate : string -> nargs:int -> coll:collation option -> agg_acc (** [aggregate name ~nargs ~coll] is a fresh fold of the aggregate [name] over rows carrying [nargs] argument values, comparing under [coll]. *) end