unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: "Dr. Arne Babenhauserheide" <arne_bab@web.de>
To: guile-user@gnu.org
Subject: define-typed: checking values on proc entry and exit
Date: Fri, 10 May 2024 09:47:31 +0200	[thread overview]
Message-ID: <871q6axg4s.fsf@web.de> (raw)

[-- Attachment #1: Type: text/plain, Size: 3381 bytes --]

Hi,

in #guile on IRC¹, old talked about Typed Racket so I thought whether
that could be done with define-syntax-rule. So I created define-typed.
This is also on my website², but I wanted to share and discuss it here.

I follow the format by [sph-sc], a Scheme to C compiler. It declares
types after the function definition like this:

┌────
│ (define (hello typed-world) (string? string?)
│   typed-world)
└────

┌────
│ (define-syntax-rule (define-typed (procname args ...) (ret? types ...) body ...)
│   (begin
│     (define (procname args ...)
│       ;; define a helper pro
│       (define (helper)
│         body ...)
│       ;; use a typecheck prefix for the arguments
│       (map (λ (type? argument)
│              (unless (type? argument)
│                (error "type error ~a ~a" type? argument)))
│            (list types ...) (list args ...) )
│       ;; get the result
│       (let ((res (helper)))
│         ;; typecheck the result
│         (unless (ret? res)
│           (error "type error: return value ~a does not match ~a"
│                  res ret?))
│         ;; return the result
│         res))
│     ;; add procedure properties
│     (let ((helper (lambda (args ...) body ...)))
│       (set-procedure-properties! procname (procedure-properties helper))
│       ;; preserve the name
│       (set-procedure-property! procname 'name 'procname))))
└────

This supports most features of regular define like docstrings, procedure
properties, and so forth.

┌────
│ (define-typed (hello typed-world) (string? string?)
│   typed-world)
│ (hello "typed")
│ ;; => "typed"
│ (hello 1337)
│ ;; => type error ~a ~a #<procedure string? (_)> 1337
│ (define-typed (hello typed-world) (string? string?)
│   "typed"
│   #((props))
│   typed-world)
│ (procedure-properties hello)
│ ;; => ((name . hello) (documentation . "typed") (props))
└────

This should automate some of the guards of [Optimizing Guile Scheme], so
the compiler can optimize more (i.e. if you check for `real?') but keep
in mind that these checks are not free: only typecheck outside tight
loops.

They provide a type boundary instead of forcing explicit static typing.

Also you can do more advanced checks by providing your own test
procedures and validating your API more elegantly, but these then won’t
help the compiler produce faster code.

But keep in mind that this does not actually provide static program
analysis like while-you-write type checks. It’s simply [syntactic sugar]
for a boundary through which only allowed values can pass. Thanks to
program flow analysis by the just-in-time compiler, it can make your
code faster, but that’s not guaranteed. It may be useful for your next
API definition.

My question now: do you have an idea for a better name than
define-typed?


[sph-sc] <https://github.com/sph-mn/sph-sc>

[Optimizing Guile Scheme]
<https://dthompson.us/posts/optimizing-guile-scheme.html>

[syntactic sugar]
<http://www.phyast.pitt.edu/~micheles/scheme/scheme12.html#are-macros-just-syntactic-sugar>


¹ https://web.libera.chat/?nick=Wisp|?#guile
² https://www.draketo.de/software/guile-snippets#define-typed


Best wishes,
Arne

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 1125 bytes --]

             reply	other threads:[~2024-05-10  7:47 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-10  7:47 Dr. Arne Babenhauserheide [this message]
2024-05-10 22:49 ` define-typed: checking values on proc entry and exit Zelphir Kaltstahl
2024-05-14  1:39   ` Dr. Arne Babenhauserheide
2024-05-15  0:10     ` Dr. Arne Babenhauserheide
2024-05-21  2:30       ` Linas Vepstas
2024-05-11  2:48 ` Olivier Dion
2024-05-13  9:51   ` Ricardo Wurmus
2024-05-14  1:15   ` Dr. Arne Babenhauserheide
2024-05-11  8:18 ` Vivien Kraus
2024-05-14  1:34   ` Dr. Arne Babenhauserheide
2024-05-21  7:05 ` Damien Mattei
2024-06-19 22:29   ` Maxime Devos via General Guile related discussions

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=871q6axg4s.fsf@web.de \
    --to=arne_bab@web.de \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).