Re: Data (type-)checking utilities?
Daphne Preston-Kendal 17 Jul 2024 09:23 UTC
I would be very interested to read a SRFI on this.
Have you looked at Racket’s facilities for ‘contracts’? That might be another useful guideline to inspire you, besides CL ‘the’ and Olin’s ‘check-arg’. (I assume that’s your inspiration for the identical facility you propose in (2).)
Daphne
> On 14 Jul 2024, at 23:27, Artyom Bologov <xxxxxx@aartaka.me> wrote:
>
> Hi Arthur,
>
>> Editor here. I would welcome a SRFI proposing a well-thought-out collection of type-checking primitives. Would you be willing to share
>> a few examples to give people on this mailing list a preview?
>
> Sure! So what one often needs with regards to type checking is:
> (1) Checking the value(s) returned by the form.
> (2) Checking function arguments.
> (3) Dispatching the behavior over types.
>
> (1) might be achieved by something like Common Lisp THE operator. I'm
> naming my macro achieving the same goal RETURN-CHECKED for no particular
> reason:
>
> (return-checked real? (sqrt 2))
> ;; => 1.4142135623730951
>
> (2) might be approached from two sides. The first is checking arguments
> one-by-one in the function body:
>
> (define (foo a b c)
> (check-arg integer? a 'foo)
> (check-arg integer? b 'foo)
> ;; Or even (return-checked integer? (if c a b))
> (if c a b))
>
> the second option is integrating the checking into function definition,
> mimicking statically typed languages:
>
> (define-checked (foo (a integer?) (b integer?) c)
> (if c a b))
>
> (3) is achieved by something like Common Lisp typecase, here called
> check-case:
>
> (define (->logint x)
> (check-case
> x
> (integer? x)
> (boolean? (if x 1 0))))
>
> There are more utils in the works, but these are the core of what I'm
> thinking about.
>
> Thanks,
> --
> Artyom Bologov