behavior_contracts/primitives.rs
1//! primitives — codegen が直呼びできる安定 primitive 公開面(bc#36 / typed-codegen.md §3.4)。
2//!
3//! A0 = 公開面の確定(emitter 生成ロジックは A1/#37)。生成コードが「細粒度 primitive を
4//! 呼ぶ」ための callable surface。
5//!
6//! 最重要規律: **演算子意味論は再実装しない**、かつ **codegen は IR を出力しない**(bc no-ir
7//! invariant)。各 native primitive は被演算子を**評価済み Value** で受け、意味論 SSoT の
8//! [`crate::expr`] value-core(`arith`/`compare`/`make_obj`/… — `evaluate` の各アームと**同一実装**)
9//! へ委譲する。よって意味論(INT_OVERFLOW / PRECISION_LOSS / MOD_ZERO / NULL_REF / TYPE_MISMATCH /
10//! FORBIDDEN_KEY / code-point 順 等)は `evaluate` と完全一致(実装が一本なので divergence 不能)。
11//! 短絡(and/or/cond/coalesce)は生成コードが native 制御フローで綴じ、bool 検査だけ primitive を使う。
12//!
13//! 生 IR をそのまま実行する経路(`ir` exec surface / execute_bundle)は `crate::expr::evaluate` を
14//! 直接使う(primitives 面は経由しない)。
15//!
16//! 加えて run_plan / run_plan_parallel の bounded 並行 / skip・unproduced / hydration helper を
17//! 再エクスポートまたは薄く公開する(いずれも既存実装を SSoT として共有)。
18
19use crate::expr::{ExprFailure, ExprFailureCode};
20use crate::plan::RelationKind;
21use crate::value::Value;
22
23/// primitive の返却型(`crate::expr::evaluate` と同一)。
24pub type R = Result<Value, ExprFailure>;
25
26// ── (a) 演算子/構築/リテラルの codegen callable surface ───────────────────────
27// bc no-ir invariant: codegen は IR を出力しない。かつてここには「単一キー IR ノードを組んで
28// `evaluate` に委譲する」薄いラッパ(ref/obj/add/…/eval_expr)が並んでいたが、それは生成コードに
29// インタプリタを再導入する松葉杖だった。撤去済み。callable surface は下記 (f)/(f0) の *_native
30// のみ(被演算子を評価済み Value で受け、`crate::expr::*` value-core = evaluate と同一実装へ委譲)。
31// 生 IR の実行が要る経路(`ir` exec surface / execute_bundle)は `crate::expr::evaluate` を直接使う。
32
33// ── (f0) 演算子/構築/リテラルのネイティブ面(bc no-ir invariant)──────────────────
34//
35// 直線 emitter (A2 rust) は被演算子を native emit → `?` で評価済み Value を得て、下の native
36// primitive に **値のまま**渡す。これらは `evaluate`(インタプリタ)も `json!`(IR ノード
37// 構築)も**呼ばない**。意味論(overflow / finite / code-point 順 / MOD_ZERO / FORBIDDEN_KEY /
38// short-circuit の bool 検査 等)は SSoT の `crate::expr::*` value-core を Value 直で呼ぶ
39// ——core は `evaluate` と**実装が一本**なので divergence しない。短絡(and/or/coalesce/cond)は
40// 生成コード側が native 制御フローで表現し、bool 検査だけ `require_bool_native` を使う。
41
42/// add / sub / mul(被演算子は評価済み Value)。
43pub fn add_native(a: Value, b: Value) -> R {
44 crate::expr::arith("add", &a, &b)
45}
46pub fn sub_native(a: Value, b: Value) -> R {
47 crate::expr::arith("sub", &a, &b)
48}
49pub fn mul_native(a: Value, b: Value) -> R {
50 crate::expr::arith("mul", &a, &b)
51}
52pub fn neg_native(a: Value) -> R {
53 crate::expr::neg(&a)
54}
55pub fn div_native(a: Value, b: Value) -> R {
56 crate::expr::div(&a, &b)
57}
58pub fn mod_native(a: Value, b: Value) -> R {
59 crate::expr::rem(&a, &b)
60}
61pub fn eq_native(a: Value, b: Value) -> R {
62 crate::expr::eq_ne("eq", &a, &b)
63}
64pub fn ne_native(a: Value, b: Value) -> R {
65 crate::expr::eq_ne("ne", &a, &b)
66}
67pub fn lt_native(a: Value, b: Value) -> R {
68 crate::expr::compare("lt", &a, &b)
69}
70pub fn le_native(a: Value, b: Value) -> R {
71 crate::expr::compare("le", &a, &b)
72}
73pub fn gt_native(a: Value, b: Value) -> R {
74 crate::expr::compare("gt", &a, &b)
75}
76pub fn ge_native(a: Value, b: Value) -> R {
77 crate::expr::compare("ge", &a, &b)
78}
79pub fn len_native(a: Value) -> R {
80 crate::expr::len(&a)
81}
82/// not(bool 検査 → 反転)。
83pub fn not_native(a: Value) -> R {
84 Ok(Value::Bool(!crate::expr::require_bool(&a, "not")?))
85}
86
87/// obj 構築(キーは静的・順序保存で codegen が渡す。`__proto__` は FORBIDDEN_KEY)。
88pub fn obj_native(pairs: Vec<(String, Value)>) -> R {
89 crate::expr::make_obj(pairs)
90}
91/// arr 構築(要素は評価済み Value)。
92pub fn arr_native(elems: Vec<Value>) -> R {
93 Ok(Value::Arr(elems))
94}
95
96/// `{int:"…"}` リテラル。
97pub fn int_lit_native(s: &str) -> R {
98 crate::expr::int_lit(s)
99}
100/// `{float:n}` リテラル。
101pub fn float_lit_native(n: f64) -> R {
102 crate::expr::float_lit(n)
103}
104/// bare number リテラル(§2.3 分類)。
105pub fn number_lit_native(n: f64) -> R {
106 crate::expr::number_lit(n)
107}
108/// bare スカラリテラル(null / bool / string)。型付き `R` を返すので生成コードで型注釈不要。
109pub fn null_native() -> R {
110 Ok(Value::Null)
111}
112pub fn bool_native(b: bool) -> R {
113 Ok(Value::Bool(b))
114}
115pub fn str_native(s: &str) -> R {
116 Ok(Value::Str(s.to_string()))
117}
118
119/// 短絡の bool 検査(and/or/cond/not の生成 native 制御フローが Value→bool に使う)。
120pub fn require_bool_native(v: Value, ctx: &str) -> Result<bool, ExprFailure> {
121 crate::expr::require_bool(&v, ctx)
122}
123
124// ── (f) 脱解釈ネイティブ面(Phase B — typed-codegen.md §4.1)─────────────────────
125//
126// 単純式(`ref`/`refOpt`/`concat`)は**式ツリーを `evaluate` で解釈しない**。直線 emitter
127// (A2 rust) は各 port を下の native helper 直呼び+ネイティブ被演算子へ展開する。よって
128// これらの関数は `evaluate`(インタプリタ)も `json!`(IR ノード構築)も**呼ばない**——
129// scope walk と `String` 連結を直接行う。意味論(NULL_REF / MISSING_PROP / UNKNOWN_BINDING /
130// TYPE_MISMATCH / refOpt の null 伝播 / code-point 連結)は [`crate::expr::evaluate`] の
131// `ref`/`refOpt`/`concat` アームと**同一**になるよう写し取る(同じ ExprFailureCode を返す)。
132// 対照的に overflow/短絡系(add/sub/…/and/or/cond/coalesce)は fail-closed 意味論を SSoT に
133// 一本化するため上の `evaluate` 委譲 primitive を使い続ける(native 化しない)。
134
135fn expr_fail(code: ExprFailureCode, message: String) -> ExprFailure {
136 ExprFailure { code, message }
137}
138
139/// `ref` のネイティブ実装(`evaluate` 非経由)。scope→field を直接歩く。
140/// 中間 null は `NULL_REF`(`evaluate` の `ref` アームと同一)。
141pub fn ref_native(path: &[&str], scope: &[(String, Value)]) -> R {
142 ref_walk(path, scope, false)
143}
144
145/// `refOpt` のネイティブ実装(`evaluate` 非経由)。中間 null は null 伝播。
146pub fn ref_opt_native(path: &[&str], scope: &[(String, Value)]) -> R {
147 ref_walk(path, scope, true)
148}
149
150/// scope 束縛 head → object field を native に歩く(`eval_ref` の写像)。`optional=true` は
151/// 中間 null で null を返す(refOpt)、false は `NULL_REF`(ref)。
152fn ref_walk(path: &[&str], scope: &[(String, Value)], optional: bool) -> R {
153 let op = if optional { "refOpt" } else { "ref" };
154 if path.is_empty() {
155 return Err(expr_fail(
156 ExprFailureCode::InvalidNode,
157 format!("{op} expects a non-empty string path"),
158 ));
159 }
160 let head = path[0];
161 let mut cur: Value = match scope.iter().find(|(k, _)| k == head) {
162 Some((_, v)) => v.clone(),
163 None => {
164 return Err(expr_fail(
165 ExprFailureCode::UnknownBinding,
166 format!("unknown binding: {head}"),
167 ))
168 }
169 };
170 for &seg in &path[1..] {
171 match cur {
172 Value::Null => {
173 if optional {
174 return Ok(Value::Null);
175 }
176 return Err(expr_fail(
177 ExprFailureCode::NullRef,
178 format!("null intermediate at .{seg} (use ?.)"),
179 ));
180 }
181 Value::Obj(ref pairs) => match pairs.iter().find(|(k, _)| k == seg) {
182 Some((_, v)) => cur = v.clone(),
183 None => {
184 return Err(expr_fail(
185 ExprFailureCode::MissingProp,
186 format!("missing property .{seg}"),
187 ))
188 }
189 },
190 ref other => {
191 return Err(expr_fail(
192 ExprFailureCode::TypeMismatch,
193 format!("cannot access .{seg} on {}", other.type_name()),
194 ))
195 }
196 }
197 }
198 Ok(cur)
199}
200
201/// `concat` のネイティブ実装(`evaluate` 非経由)。既にネイティブ評価済みの parts を
202/// `String::push_str` で連結する。非 string 部分は `TYPE_MISMATCH`(`evaluate` の `concat`
203/// アームと同一)。arity<2 は不正 IR(`InvalidNode`)。parts は emitter が各要素を再帰的に
204/// native emit した Value 列(=式ツリー解釈が消えている)。
205pub fn concat_native(parts: &[Value]) -> R {
206 if parts.len() < 2 {
207 return Err(expr_fail(
208 ExprFailureCode::InvalidNode,
209 format!("concat expects >= 2 args, got {}", parts.len()),
210 ));
211 }
212 let mut s = String::new();
213 for p in parts {
214 match p {
215 Value::Str(part) => s.push_str(part),
216 other => {
217 return Err(expr_fail(
218 ExprFailureCode::TypeMismatch,
219 format!(
220 "concat: strings only (got {}; no implicit toString)",
221 other.type_name()
222 ),
223 ))
224 }
225 }
226 }
227 Ok(Value::Str(s))
228}
229
230// ── (c) bounded 並行プリミティブ(順序保存)─────────────────────────────────────
231/// 入力順を保存し in-flight を `concurrency` で上限する bounded 並行 map。
232///
233/// execution-plan.md §4 の determinism 規律(output[i]=worker(items[i]) / 完了順非依存 /
234/// bounded dispatch)を単体 primitive として公開する。runtime は同期のため
235/// [`std::thread::scope`] の scoped worker で実装する([`crate::plan::run_plan_parallel`] と同一機構)。
236///
237/// `worker` は `Fn + Sync`(並行呼び出し安全)を要求する。結果は index 固定スロットに書くため
238/// 完了順に依存しない(逐次 map と同一の結果列)。
239pub fn map_with_concurrency<T, U, F>(items: &[T], concurrency: i64, worker: F) -> Vec<U>
240where
241 T: Sync,
242 U: Send,
243 F: Fn(&T, usize) -> U + Sync,
244{
245 let n = items.len();
246 let limit = concurrency.max(1) as usize;
247 if limit <= 1 || n <= 1 {
248 return items
249 .iter()
250 .enumerate()
251 .map(|(i, it)| worker(it, i))
252 .collect();
253 }
254 // 各スロットは Option<U>。worker は disjoint な index にしか書かないので競合しない。
255 let mut slots: Vec<Option<U>> = (0..n).map(|_| None).collect();
256 let cursor = std::sync::atomic::AtomicUsize::new(0);
257 let workers = limit.min(n);
258 let worker_ref = &worker;
259 let items_ref = items;
260 {
261 // 生ポインタ経由で disjoint スロットへ書き込む(各 index は 1 worker のみが触る)。
262 struct SlotsPtr<U>(*mut Option<U>);
263 unsafe impl<U: Send> Sync for SlotsPtr<U> {}
264 let base = SlotsPtr(slots.as_mut_ptr());
265 std::thread::scope(|s| {
266 for _ in 0..workers {
267 let cursor = &cursor;
268 let base = &base;
269 s.spawn(move || loop {
270 let i = cursor.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
271 if i >= n {
272 break;
273 }
274 let out = worker_ref(&items_ref[i], i);
275 // SAFETY: index i はこの worker だけが取得した(AtomicUsize で一意)ので
276 // slots[i] への書き込みは disjoint・データ競合なし。
277 unsafe {
278 *base.0.add(i) = Some(out);
279 }
280 });
281 }
282 });
283 }
284 slots
285 .into_iter()
286 .map(|o| o.expect("all slots filled"))
287 .collect()
288}
289
290// ── (d) skip / unproduced-value helper ────────────────────────────────────────
291/// 未生成 Port(Skip)の表現値。single→Null / connection→空 connection。
292pub fn unproduced_value(kind: Option<RelationKind>) -> Value {
293 match kind {
294 Some(RelationKind::Connection) => skip_connection(),
295 _ => Value::Null,
296 }
297}
298/// single relation の未生成 = Null。
299pub fn skip_single() -> Value {
300 Value::Null
301}
302/// connection relation の未生成 = {items:[],cursor:null}。
303pub fn skip_connection() -> Value {
304 Value::Obj(vec![
305 ("items".to_string(), Value::Arr(vec![])),
306 ("cursor".to_string(), Value::Null),
307 ])
308}
309
310// ── (e) hydration / relation helper ───────────────────────────────────────────
311/// behavior.rs の map.into と同一規律の zip-attach helper。
312///
313/// `over` の各要素へ `key` で `values[k]` を書き戻した augment 済みリストを返す
314/// (over と同じ長さ・順序、親不変=コピー、guard skip 要素は無変更で pass-through)。
315/// `kept_idx` は augment 対象の over index(昇順)。augment 対象が object でなければ Err。
316pub fn hydrate_into(
317 over: &[Value],
318 key: &str,
319 kept_idx: &[usize],
320 values: &[Value],
321) -> Result<Vec<Value>, String> {
322 let mut augmented: Vec<Value> = Vec::with_capacity(over.len());
323 let mut k = 0usize;
324 for (i, el) in over.iter().enumerate() {
325 if k < kept_idx.len() && kept_idx[k] == i {
326 match el {
327 Value::Obj(pairs) => {
328 let mut next: Vec<(String, Value)> = pairs.clone();
329 // 既存 key を上書き(無ければ追記)。宣言順を保つ。
330 if let Some(slot) = next.iter_mut().find(|(kk, _)| kk == key) {
331 slot.1 = values[k].clone();
332 } else {
333 next.push((key.to_string(), values[k].clone()));
334 }
335 augmented.push(Value::Obj(next));
336 }
337 _ => {
338 return Err(format!(
339 "hydrate_into: element {i} is not an object (into requires object elements)"
340 ))
341 }
342 }
343 k += 1;
344 } else {
345 augmented.push(el.clone());
346 }
347 }
348 Ok(augmented)
349}