unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* define-typed: checking values on proc entry and exit
@ 2024-05-10  7:47 Dr. Arne Babenhauserheide
  2024-05-10 22:49 ` Zelphir Kaltstahl
                   ` (3 more replies)
  0 siblings, 4 replies; 12+ messages in thread
From: Dr. Arne Babenhauserheide @ 2024-05-10  7:47 UTC (permalink / raw)
  To: guile-user

[-- 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 --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2024-06-19 22:29 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-10  7:47 define-typed: checking values on proc entry and exit Dr. Arne Babenhauserheide
2024-05-10 22:49 ` 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

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).