Commit 5125b0cf

5125b0cf6795Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 5 files · ~5 modified

Description

Proc#curry: deferred-application argument accumulator

proc.curry returns an sp_Curry accumulator over the target proc; each [arg] (or .call(arg)) returns a fresh accumulator with the argument appended, and the fully-applied value is realized at the point of use (sp_curry_to_int calls the target with the collected mrb_int arguments). A partial curry thus behaves as a deferred call rather than auto-invoking at arity, which keeps the static type uniform (TY_CURRY) across the [] chain.

  • runtime: sp_Curry { target, nargs, args[16] }, sp_curry_new/apply/to_int, with a GC scan hook marking the target proc.
  • types: TY_CURRY (sp_Curry *), wired through c-type/default/needs_root/ is_scalar_ret.
  • analyze: proc.curry -> TY_CURRY; curry[]/call -> TY_CURRY.
  • codegen: sp_curry_new / sp_curry_apply emission; puts of a curry realizes the int result.

Curry works for non-colliding programs; test/i919 additionally reuses a name as both a lambda parameter and a curry local (c), which the shared-slot block parameter scoping types as a single conflicting variable -- that collision is the separate, deferred per-block-rename issue, not a curry gap.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
5125b0cf6795
Parent
ec4682375e85

lib/sp_runtime.hmodifiedView file

@@ -5074,6 +5074,30 @@
50745074 c->inner = inner;
50755075 return sp_proc_new_meta((void *)sp_proc_compose_fn, c, sp_proc_compose_scan, 1, TRUE, 1, NULL, NULL);
50765076 }
5077+/* Proc#curry: an immutable argument accumulator over an sp_Proc target.
5078+ `proc.curry` makes an empty accumulator; each `[arg]` returns a fresh
5079+ accumulator with `arg` appended; the fully-applied value is realized
5080+ by calling the target with the collected (mrb_int) arguments. Spinel
5081+ defers the call to the point of use (sp_curry_to_int), so a partial
5082+ curry behaves as a deferred call rather than auto-invoking at arity. */
5083+typedef struct { sp_Proc *target; mrb_int nargs; mrb_int args[16]; } sp_Curry;
5084+static void sp_curry_scan(void *p) { sp_Curry *c = (sp_Curry *)p; if (c->target) sp_gc_mark(c->target); }
5085+static sp_Curry *sp_curry_new(sp_Proc *p) {
5086+ sp_Curry *c = (sp_Curry *)sp_gc_alloc(sizeof(sp_Curry), NULL, sp_curry_scan);
5087+ c->target = p; c->nargs = 0;
5088+ return c;
5089+}
5090+static sp_Curry *sp_curry_apply(sp_Curry *c, mrb_int arg) {
5091+ sp_Curry *n = (sp_Curry *)sp_gc_alloc(sizeof(sp_Curry), NULL, sp_curry_scan);
5092+ *n = *c;
5093+ if (n->nargs < 16) n->args[n->nargs++] = arg;
5094+ return n;
5095+}
5096+static mrb_int sp_curry_to_int(sp_Curry *c) {
5097+ if (!c || !c->target) return 0;
5098+ return sp_proc_call(c->target, c->args);
5099+}
5100+
50775101 /* Hash#to_proc cap-scan: the proc's `cap` field IS the source hash
50785102 (a single GC pointer), so marking it keeps the hash alive for the
50795103 proc's lifetime. The per-variant lookup fn is emitted by codegen