From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: "Dr. Arne Babenhauserheide" Newsgroups: gmane.lisp.guile.user Subject: Re: define-typed: checking values on proc entry and exit Date: Tue, 14 May 2024 03:34:46 +0200 Message-ID: <87zfstuqfd.fsf@web.de> References: <871q6axg4s.fsf@web.de> <691ca3a2aa40387f05bfa611983e242881aa116f.camel@planete-kraus.eu> Mime-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha256; protocol="application/pgp-signature" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="30273"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.12.4; emacs 30.0.50 To: Vivien Kraus Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Wed May 15 02:08:40 2024 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1s72CG-0007b0-Mu for guile-user@m.gmane-mx.org; Wed, 15 May 2024 02:08:40 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1s72Bv-0002mS-S8; Tue, 14 May 2024 20:08:19 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s72Bu-0002m6-Ar for guile-user@gnu.org; Tue, 14 May 2024 20:08:18 -0400 Original-Received: from mout.web.de ([212.227.17.11]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s72Bs-0000Fg-KQ for guile-user@gnu.org; Tue, 14 May 2024 20:08:18 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1715731694; x=1716336494; i=arne_bab@web.de; bh=ay/nkMJ4wOi2kIDoGj3v3xMn8hdAfhTnSDbOoXLMiKI=; h=X-UI-Sender-Class:Resent-To:Resent-From:Resent-Date:From:To: Subject:In-Reply-To:References:Date:Message-ID:MIME-Version: Content-Type:cc:content-transfer-encoding:content-type:date:from: message-id:mime-version:reply-to:subject:to; b=QP19Sdbr1tzMj1uMUspizjBoq0SRK+UnVzgHjWt5w+xIrYpDuePHdiq5BYBpe7th zb+YTMvNfXQDw+fKUgHrxFt3U6Z8MS/VNYoQL4ijrCAGVEujjZF7Nha7YJ51S4BJ0 2G1nmcKczvUUcFiFkkx+L3pW8o3cGNQiseZ3AT7l/s5m5rhXCjo3myD/KJMekUPnO 9YCJEq8JmfvAnebDjDeAzLXHJ1/ROVC8jyN4Y1H4CqAW3QFi+VZUQFBFgEydm7B6u kNcaGwHddfcwSVjmF4C5AzxcqcJ8zq+i6RWyUkc9ZNObyC2nvPk0PsJD/A24RGq93 VZRvFjkvooBhcKxB9w== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Original-Received: from fluss ([80.136.28.187]) by smtp.web.de (mrweb105 [213.165.67.124]) with ESMTPSA (Nemesis) id 1MECGl-1sGcYt2Oij-004iYX; Wed, 15 May 2024 02:08:14 +0200 Resent-To: guile-user@gnu.org Resent-From: "Dr. Arne Babenhauserheide" Resent-Date: Wed, 15 May 2024 02:08:14 +0200 Resent-Message-ID: <871q64uec1.fsf@web.de> In-Reply-To: <691ca3a2aa40387f05bfa611983e242881aa116f.camel@planete-kraus.eu> (Vivien Kraus's message of "Sat, 11 May 2024 10:18:06 +0200") X-Provags-ID: V03:K1:aummBZEhXnY5hU5qCntsVizxj27pjJqrxp6wNEtiAKkN1WSyfoL UIXzu+WHpkPQNm81k5Ps4VWnSfA/giNr854kMPgUMJjCWP55M0CCjwixB1Rsrn2lZMFPlq6 pZLGwAt1pa/Gb5Qb5Op0Crsoql2omn8uvOyoRL9b1F/4IR8pcrpHCyppipHwbavk34ciGCg Cf9e1OSZ4S6ukb4T91KEg== UI-OutboundReport: notjunk:1;M01:P0:M1olIzxZo+8=;RD6IWZrKgwL5fZsoXEkP7BWAF+U UYEwt9icmbqL+OqL4O/S/WA17gr6E0AVubmVQlxFeDZAEf/TwLLXLzzBk9HME9SEH0R2ga6AV g+vROmeVD4Xem/SBw3Ky+lDOu4HmS14En0q7yaKaaz8NngxoJaMB2R4h0AYt550ExHPRLfUa2 15Np68ysx8Si0DZwpIK+blrgfVv3x1zKnEuI1d8Fcw3sE8CY1IAYJO0QRv5CwKRl8g70GnJbT w0SAOFScY3BdUhWg5GQirQpjZ6XjVe/HhxSq+ee+7cyEAh+5fUhwdcEiSmxVnUr7RgYgjjVPe +83rDg45zqUBMq3ARVe+tVT9URx7EfuCLCvdRvuPJwqC/NY6+2L7hZqGwXyDA1kE6T1fJMO5y am8Ctd+tcRfYDN8McGCFa6Ajpka2nXmX4WV3hZE2Rf4V+eQklyJmhzP8UtejBVly+gKCl7lXD RhoWxpwgNiKexQM3xJYcqVMMWuzaiDtuOvfMJEDGMd34v9nNzQ9cDq0w6VcmzCC/JZIzNL+UC OhYdAyghvIW+n/ClFmFQoMN2iOGSp9BCBNsPCjrXIEaZbGID1tTfpj1mFXt0F9DUYU6x+TBwl /fYeCuy/vg5YDxCd2AQt3bDRqx/3RXiRmi6HhGAVumur9Xo1nk7seIyF/ocW40/CsACIBCZUM nJU8fM3QARpQAMD+caxM1UrmEob3AJ5S9bE4+gPUKn+0U2VlQHRWLE4LEnmkW8bylk4+xxEB7 uReXzZ3tLvCWMOfkAo/g9yZdJJbDUk7vfIni21jODRygW9OGsqAA/u6bgvNv4Dq4TFg/K12B Received-SPF: pass client-ip=212.227.17.11; envelope-from=arne_bab@web.de; helo=mout.web.de X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.user:19649 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Vivien Kraus writes: > Hello! > > This is an interesting approach, thank you. > > Le vendredi 10 mai 2024 =C3=A0 09:47 +0200, Dr. Arne Babenhauserheide a > =C3=A9crit=C2=A0: >> =E2=94=82=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ;; get the result >> =E2=94=82=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (let ((res (helper))) >> =E2=94=82=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ;; typecheck t= he result >> =E2=94=82=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (unless (ret? = res) >> =E2=94=82=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (e= rror "type error: return value ~a does not match ~a" >> =E2=94=82=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 res ret?)) >> =E2=94=82=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ;; return the = result >> =E2=94=82=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 res)) > > A nice improvement would be to support multiple return values, for > instance by using call-with-values, and checking the return value with > (apply ret? res) instead of (ret? res). > > What do you think? That=E2=80=99s a nice idea! With some experimentation I got it working: =E2=94=8C=E2=94=80=E2=94=80=E2=94=80=E2=94=80 =E2=94=82 (import (srfi :11 let-values)) =E2=94=82 (define-syntax-rule (define-typed (procname args ...) (ret? types= ...) body ...) =E2=94=82 (begin =E2=94=82 (define (procname args ...) =E2=94=82 ;; create a sub-procedure to run after typecheck =E2=94=82 (define (helper) =E2=94=82 body ...) =E2=94=82 ;; use a typecheck prefix for the arguments =E2=94=82 (map (=CE=BB (type? argument) =E2=94=82 (unless (type? argument) =E2=94=82 (error "type error ~a ~a" type? argument))) =E2=94=82 (list types ...) (list args ...) ) =E2=94=82 ;; get the result =E2=94=82 (let-values ((res (helper))) =E2=94=82 ;; typecheck the result =E2=94=82 (unless (apply ret? res) =E2=94=82 (error "type error: return value ~a does not match ~a" =E2=94=82 res ret?)) =E2=94=82 ;; return the result =E2=94=82 (apply values res))) =E2=94=82 ;; add procedure properties via an inner procedure =E2=94=82 (let ((helper (lambda (args ...) body ...))) =E2=94=82 (set-procedure-properties! procname (procedure-properties h= elper)) =E2=94=82 ;; preserve the name =E2=94=82 (set-procedure-property! procname 'name 'procname)))) =E2=94=94=E2=94=80=E2=94=80=E2=94=80=E2=94=80 This supports most features of regular define like docstrings, procedure properties, multiple values (thanks to Vivien!), and so forth. =E2=94=8C=E2=94=80=E2=94=80=E2=94=80=E2=94=80 =E2=94=82 (define-typed (hello typed-world) (string? string?) =E2=94=82 typed-world) =E2=94=82 (hello "typed") =E2=94=82 ;; =3D> "typed" =E2=94=82 (hello 1337) =E2=94=82 ;; =3D> type error ~a ~a # 1337 =E2=94=82 (define-typed (hello typed-world) (string? string?) =E2=94=82 "typed" =E2=94=82 #((props)) =E2=94=82 typed-world) =E2=94=82 (procedure-properties hello) =E2=94=82 ;; =3D> ((name . hello) (documentation . "typed") (props)) =E2=94=82 (define-typed (multiple-values num) ((=CE=BB(a b) (> a b)) number= ?) =E2=94=82 (values (* 2 (abs num)) num)) =E2=94=82 (multiple-values -3) =E2=94=82 ;; =3D> 6 =E2=94=82 ;; =3D> -3 =E2=94=94=E2=94=80=E2=94=80=E2=94=80=E2=94=80 Best wishes, Arne --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQJEBAEBCAAuFiEE801qEjXQSQPNItXAE++NRSQDw+sFAmZCv7YQHGFybmVfYmFi QHdlYi5kZQAKCRAT741FJAPD6/DcD/4lO/rdjSLEthIwu1/fgJnfv3gmLQhaYSBq CgFdk7ib+JwRs/EUDXV7qU82DuYNUGLbqhOPYPDLA+gcK9BaNdsDTwZ6vQe33cf+ kp8KLrUlXTVP4hwOyx5XE8PTlDKsGF5SdaouiGJRted5OLIl0jVpxtxxivoZqH3l FA8eWffyAN/nRxszBghldZ2KYEFx0Ah+qvIU4LkRIytOj/qLipEA6aJiDrAs+cjF NNP3iAJhfRymhk9L91Bcx3SwTuE8KfPoPZb5nJQYY6H8KqHQNmgJIOZpbzhkBsFs SvT4Lttw9V6WrAt1GMVGj50YmGIGpRfOmzyF+SetYF6ptBo9uteVE7DJddjtbgh+ syy/1r56D7NzRilGBT+jurWPVQIAS9ST0vURbVhbVd7rHWRObjOyRcIFhkP2Zovc x1cWtUwyWEgzRP0ZsVqjfqkJSBBFqQpzbRPGfFCFo0uhtm3RktfTPox10uiqw7OF hc/6QBBSyXjYz3LU5advCTGaLfV/7Jtv+aqWheppRhWgKFJbS+yduzcBQGhNoxVZ yWiEEaOgddnkIHvxiEbugkDdEO8ncemylcnKWzTMPMKqsmloEqL4ru+/zUdXSrjQ cIBfw4AdIIJvq9TIX84HCMqDsZP8cA59bSOUrWCcKHBIX2GJMQYympc2lIZK+Frw eLu8T+B2q4jEBAEBCAAuFiEE3Si95tmHXKvOSosd3M8NswvBBUgFAmZCv7YQHGFy bmVfYmFiQHdlYi5kZQAKCRDczw2zC8EFSAUlA/9wzaDMobRtXEzqxdOP9bhV/SHG MpFGE49phK63JncvPPJfI8yTK6Ml94feJuB+bs8wKKqQt15JTkIDKU2O437Go7t9 UlXGXu5iqvjLR/851HjJpZscPfA/ZVCJ6cWLJOzeKbQIFmDfDcsnHzkb8sQCsmGM BYCUZyR9q8nvuUgJWQ== =pxUs -----END PGP SIGNATURE----- --=-=-=--