unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Maxime Devos <maximedevos@telenet.be>
To: Zelphir Kaltstahl <zelphirkaltstahl@posteo.de>
Cc: Guile User <guile-user@gnu.org>
Subject: Re: Contracts macro example
Date: Wed, 20 Jul 2022 10:55:53 +0200	[thread overview]
Message-ID: <40733751-fe8e-1e01-4a41-23b83a0c6e35@telenet.be> (raw)
In-Reply-To: <b580b558-9e07-d7b1-94bf-37f343bf0374@posteo.de>


[-- Attachment #1.1.1: Type: text/plain, Size: 2582 bytes --]

On 20-07-2022 10:39, Zelphir Kaltstahl wrote:

>> It would also be nice to define a global 'require' and 'ensure' and
>> '<?>' somewhere (e.g.: (define-syntax require (identifier-syntax
>> (syntax-error "'require' can only be used as part of a contract
>> construct")))), that way, require / ensure / <?> can be renamed during
>> importing, so all contract things could be prefixed with, say,
>> contract:.
>
> I thought about implementing <?> for the insertion location of the 
> result in a predicate, but initially wanted to keep it simple and get 
> a simple version to work. I think I have seen this for pipelining in 
> an SRFI before … *checks* … Maybe in 
> https://srfi.schemers.org/srfi-197/srfi-197.html, or 
> https://srfi.schemers.org/srfi-26/srfi-26.html, or maybe in some other 
> repository.
>
> Now that the basic version works, I can try to introduce the placeholder.
>
> The idea is to define these globally in the module, so that they can 
> be exported separately, so that they can be renamed upon import, correct?
Yes.
>
> How could a macro check, whether it is used inside something else? If 
> the pattern matching only looks at the form of the macro itself, how 
> can I get the "context", in which it was used and check, whether that 
> is inside a `define-with-contract`? I think I have not yet unlocked 
> that knowledge yet : ) 

That's one way to implement things (syntax-parameterize sounds useful 
here), but that sounds way more complicated than needs to be.  All you 
need to do is:

  * keep the original code
  * Add:
    (define <?> "consider define-syntax+identifier-syntax+syntax-error
    for better error messages but this will do for now)
  * Export <?> (at least, once your code is turned into a module, if the
    users of define-with-contract are in the same module as
    define-with-contract then exporting isn't required though harmless)
  * Likewise for 'require' and 'ensure'

By doing that, syntax-rules knows that its '<?>', 'require' and 'ensure' 
is not just the symbol '<?>' 'require' and 'ensure', but the 
_identifier_ (which keeps being the same identifier after renaming) 
'<?>', 'require' and 'ensure'.

(Note that as a consequence,  if you do that, (let ((require 0)) 
(define-with-contract foo (require) (ensure) (lambda _ 0))) will be a 
syntax error, because the 'require' in define-with-contract now refers 
to the variable 'require' from the let, not the identifier from your 
RnRS module).

That's all you need to do (untested)!


Greetings,
Maxime


[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 929 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]

  reply	other threads:[~2022-07-20  8:55 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-14 23:55 Contracts macro example Zelphir Kaltstahl
2022-07-19 15:20 ` Maxime Devos
2022-07-20  8:39   ` Zelphir Kaltstahl
2022-07-20  8:55     ` Maxime Devos [this message]
2022-07-24  1:21       ` Zelphir Kaltstahl
2022-08-04 16:19         ` Maxime Devos

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=40733751-fe8e-1e01-4a41-23b83a0c6e35@telenet.be \
    --to=maximedevos@telenet.be \
    --cc=guile-user@gnu.org \
    --cc=zelphirkaltstahl@posteo.de \
    /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).