Re: SRFI 284: define-typed Dr. Arne Babenhauserheide 26 Sep 2026 22:48 UTC
Artyom Bologov <xxxxxx@aartaka.me> writes:

> Arne,
>
>>>> I’m not well-versed in writing grammars, but I would gladly integrate
>>>> one. I can’t expect you to write it, but I’d be grateful if you did …
>>>
>>> It doesn’t have to be like Uber-Bachus-Naur-Super-Normal-Form-Grammar,
>>> it can be quite informal. Informal grammars are even more useful, I
>>> think—their crudeness and incompleteness forces you to express things in
>>> a simpler way.
>>
>> Then I don’t think I really understood what you mean.
>>
>> Can you give an example?
>>
>> (I may be able to make something quickly from that)
>
> Well, you can write an informal-ish thing, something like this (mostly
> borrowed from R7RS section 7
> <https://standards.scheme.org/r7rs-html5/index.html#7>, but broken
> liberally)

That looks far less intimidating than I thought ☺

Thank you!

> <define-typed> = (define-typed (<name> <arg> ...) <types> <form>*)
> <arg> = symbol
> <name> = symbol
> <form> = s-expression
> <types> = (<argtype> ... (-> <rettype> ...)) ; 1: args with multiple returns
>         | ((<rettype> ...) <argtype> ...) ; 2: args with multiple returns, sph-sc style

sph-sc style is supported by the implementation, but not part of the
SRFI. While I like it, and while it might enable rest arguments (though
my experiment with implementing them failed today), it is also more
error-prone.

>         | (<argtype> ... -> <rettype>) ; 3: args with a single return type
>         | (<rettype> <argtype> ...) ; 4: args with a single return, sph-sc style
>         | (<argtype> ...) ; 5: args only
> <artype> = #f | symbol | #f | (lambda <formals> <body>)
> <rettype> = #f | symbol | (lambda <formals> <body>)
> <formals> = as per R7RS section 4.1.4 https://standards.scheme.org/r7rs-html5/index.html#4.1.4
> <body> = as per R7RS section 4.1.4
>
> Which brings me to another set of questions:
>
> • Are R7RS lambdas the only type of lambdas / arbitrary code allowed as
>   define-typed checks? Is everything else supposed to be a symbol? Any
>   support for user-provided lambda syntaxes?

It should support everything that evaluates to a predicate.

> • (I kind of repeat myself here, but I really don’t understand it:) how
>   does one distinguish between a single-expression return check like
>   (cut = 1 <>) and a list of return checks?

For the sph-sc style you can’t.

With the new release of this SRFI, the distinction is:

1. <argtypes> -> <form>: a single return type, checked by whatever the form evaluates to.
2. <argtypes> (-> <form>): a list of return types, checked by whatever the form evaluates to.
3. <argtypes> (-> <form> <form> <form> ...):  multiple return types, checked by whatever the forms evaluate to.

The distinction between 1 and 2 is that -> is inside the parens in 2.

The distinction between 2 and 3 is that 3 must have at least 2 return
type checks.

<define-typed> = (define-typed (<name> <arg> ...) <types> <body>)
<arg> = symbol
<name> = symbol
<types> = (<argtype> ... -> <rettype>)       ; 1: args with a single return type
        | (<argtype> ... (-> <retlisttype>)) ; 2: args with multiple returns,
                                             ;    checked with a single
                                             ;    procedure that receives the
                                             ;    return values as list
        | (<argtype> ... (-> <rettype> <rettype> <rettype> ...)) ; 3: args with multiple returns
        | (<argtype> ...)                    ; 4: args only
<artype> = #f | symbol | <predicate-form>
<rettype> = #f | symbol | <predicate-form>
<retlisttype> = procedure that receives a list with all return values and returns a true value or #f
<predicate-form> = procedure with one argument that returns a true value or #f
<formals> = as per R7RS section 4.1.4 https://standards.scheme.org/r7rs-html5/index.html#4.1.4
<body> = as per R7RS section 4.1.4

Is it clear this way?

Best wishes,
Arne
--
Unpolitisch sein
heißt politisch sein,
ohne es zu merken.
https://www.draketo.de