Re: Contracts macro example

2022-07-20 Thread Zelphir Kaltstahl

Hello Maxime!

On 7/19/22 17:20, Maxime Devos wrote:

Zelphir Kaltstahl schreef op do 14-07-2022 om 23:55 [+]:

   (make-assertion-failure)
   (make-exception-with-message "assertion failed")
   (make-exception-with-irritants (quote (op args* ...)]

Instead of a generic 'assertion failure', I believe a more specific
&contract-failure to be better.

Ah yes, I'll do that!

Also, define*-with-contract / lambda*-with-contract would be nice.
I am not sure how to pattern match optional arguments (maybe not any special 
way) an keyword arguments (probably a special notation or way to do it, I 
guess). But I do seem to remember, that there were some procedures for building 
keyword argument procedures and that I should have an example somewhere. Perhaps 
I need to pattern match on literally #:optional and #:key and combinations of these.

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?


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



  ;; `#t`, the neutral element of `and`.

Guile supports Unicode and UTF-8, so you can write ‘#t’ here instead of
`#t`.  Additionally, maybe #t -> #true?
Do you think I should not use #t here, but another value? My idea was, that 
using #t would not influence the result of an "and operation" at all. But 
perhaps using another value can be useful for checking against it in some 
situation?

Greetings,
Maxime.


Thank you for the input!
Zelphir

--
repositories: https://notabug.org/ZelphirKaltstahl




Re: Contracts macro example

2022-07-20 Thread Maxime Devos

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



OpenPGP_0x49E3EE22191725EE.asc
Description: OpenPGP public key


OpenPGP_signature
Description: OpenPGP digital signature


Re: UTF16 encoding adds BOM everywhere?

2022-07-20 Thread Mark H Weaver
Hi,

Jean Abou Samra  wrote:

> With this code:
> 
> (let ((p (open-output-file "x.txt")))
>    (set-port-encoding! p "UTF16")
>    (display "ABC" p)
>    (close-port p))
> 
> the sequence of bytes in the output file x.txt is
> 
> ['FF', 'FE', '41', '0', 'FF', 'FE', '42', '0', 'FF', 'FE', '43', '0']
> 
> FFE is a little-endian Byte Order Mark (BOM), fine.
> But why is Guile adding it before every character
> instead of just at the beginning of the string?
> Is that expected?

No, this is certainly a bug.  It sounds like the
'at_stream_start_for_bom_write' port flag is not being cleared, as it
should be, after the first character is written.  I suspect that it
worked correctly when I first implemented proper BOM handling in 2013
(commit cdd3d6c9f423d5b95f05193fe3c27d50b56957e9), but the ports code
has seen some major reworking since then.  I guess that BOM handling was
broken somewhere along the way.

I would suggest filing a bug report.  I don't have time to look into it,
sorry.  I don't work on Guile anymore.  I only happened to see your
message by chance.

 Regards,
   Mark

-- 
Disinformation flourishes because many people care deeply about injustice
but very few check the facts.  Ask me about .



Re: UTF16 encoding adds BOM everywhere?

2022-07-20 Thread Jean Abou Samra

Hi Mark,

Thanks for your reply!


Le 20/07/2022 à 22:42, Mark H Weaver a écrit :

Hi,

No, this is certainly a bug.  It sounds like the
'at_stream_start_for_bom_write' port flag is not being cleared, as it
should be, after the first character is written.  I suspect that it
worked correctly when I first implemented proper BOM handling in 2013
(commit cdd3d6c9f423d5b95f05193fe3c27d50b56957e9), but the ports code
has seen some major reworking since then.  I guess that BOM handling was
broken somewhere along the way.

I would suggest filing a bug report.



OK, opened

https://debbugs.gnu.org/cgi/bugreport.cgi?bug=56675




I don't have time to look into it, sorry.  I don't work on Guile anymore.


A pity :-)

Cheers,
Jean