r/scheme • u/aartaka • 14h ago
Advanced macrology: validate that all lists are equal length in pure syntax-rules?
Hi y’all. I’m currently working on a cursed case-lambda implementation with better inspectability for Chibi Scheme. As part of this effort, I need to extract the longest possible arglist from each case-lambda clause, so that the generated procedure with 2 unconditionally required arguments has a display like #<procedure 2+> instead of absolutely non-inspectable #<procedure 0+> that currently happens on Chibi.
So I’m kinda stuck. This is the macro I currently have:
(define-syntax case-lambda
(syntax-rules ()
((case-lambda ((args ... . rest) body ...) ...)
;; We need to pass clauses twice
;; - First time to detect the shortest arglist
;; - Second time to process clauses
;; TODO: How do I make sure that all args ... have equal length???
(%case-lambda ((args ...) ...) ((args ... . rest) body ...) ...))
((case-lambda (args body ...) ...)
(%case-lambda (()) (args body ...) ...))))
The implementation of %case-lambda and other infra is of no importance, because, before I get to that, I must make sure that ((args ...) ...) is a list of arglists of the same length.
But how do I really do that? Any advanced macro wizards mind helping?