Re: [racket-users] Macro guards question

2019-05-29 Thread Kevin Forchione


> On May 29, 2019, at 11:09 AM, Sorawee Porncharoenwase 
>  wrote:
> 
> (foo a) ;=> 1
> (foo "a") ;=> 2
> (foo 10) ;=> 3
> (foo 'a) ;=> 4
> (foo (bar x)) ;=> 5
Ah… thanks so much for the explanation. That’s put me much closer (I hope!) to 
the solution I’m after. A bit of stumbling around through some documentation 
has got me this far.Being able to handle (foo a) and (foo b) below differently 
is a distinction I’ve been after for a while!

#lang racket

(define-syntax (foo stx)
  (syntax-case stx ()
[(_ arg) (and (identifier? #'arg) (identifier-binding #'arg 0 #t)) #'1]
[(_ arg) (identifier? #'arg) #'2]
[(_ arg) (string? (syntax->datum #'arg)) #'3]
[(_ arg) (number? (syntax->datum #'arg)) #'4]
[(_ (quote* x)) (and (free-identifier=? #'quote* #'quote)
 (symbol? (syntax->datum #'x))) #'5]
[(_ _) #'6]))

(define a #t) ;=> a is top-level bound, b is not.

(foo a)   ;=> 1
(foo b)   ;=> 2
(foo "a") ;=> 3
(foo 10)  ;=> 4
(foo 'a)  ;=> 5
(foo (bar x)) ;=> 6


Kevin

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/506321EF-0D5B-4551-8671-268E6808B015%40gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] anyone using single-flonums?

2019-05-29 Thread Matthew Flatt
At Wed, 29 May 2019 12:33:24 -0400, George Neuner wrote:
> Question: does/will Chez support converting to/from 32-bit floats for C 
> libraries?

Yes.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/5ceeeb63.1c69fb81.1d6e7.1755SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] anyone using single-flonums?

2019-05-29 Thread Doug Williams
I support them in various packages, but I rarely use them, per se. Those
packages would have to be updated, but it wouldn't be a big deal for me.

Doug

On Wed, May 29, 2019 at 9:52 AM Matthew Flatt  wrote:

> Does anyone use single-flonums in Racket?
>
> I don't mean `_float` or `f32vector`s, which convert C `float`s or
> 32-bit array elements into regular double-precision Racket flonums. I
> mean literals like `3.0f0` or functions like `real->single-flonum`,
> which produce a Racket number that uses only 32 bits for arithmetic.
>
> Chez Scheme doesn't support single-precision floating-point numbers,
> and adding them would be a lot of work --- for no benefit, if it turns
> out that no one is using those kinds of numbers.
>
> My guess is that no one uses them currently, because it's rare that
> you'd want to trade speed for *im*precision. Single-flonums in Racket
> are significantly slower than regular flonums, because they're not
> treated as a common case. The only use I can think of, and the one that
> inspired the original experiment, is to try to faithfully replicate a C
> `float` calculation in Racket, but even that possibility has issues.
>
> If no one uses single-precision floats, then I will suggest that we
> remove them from Racket by making numbers like `3.0f0` parse as flonums
> and making `real->single-flonum` raise `exn:fail:unsupported`.
> Obviously, this would be a backward-incompatible change. But if it
> causes little enough trouble, then it could be a good trade to avoid
> problems for Racket CS and future platforms.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/5ceeaac9.1c69fb81.118e3.f708SMTPIN_ADDED_MISSING%40gmr-mx.google.com
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CACvwowWy-q9Vr_GbBgxhRN9cQwGLk4ZO%3DsntG7yuwaRK%3D71oOA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] anyone using single-flonums?

2019-05-29 Thread Dmitry Pavlov




My guess is that no one uses them currently, because it's rare that
you'd want to trade speed for *im*precision. Single-flonums in Racket
are significantly slower than regular flonums, because they're not
treated as a common case. The only use I can think of, and the one that
inspired the original experiment, is to try to faithfully replicate a C
`float` calculation in Racket, but even that possibility has issues.



To my knowledge, one reason to use single-precision numbers is
that they occupy half as much memory, which is a big deal
in e. g. video games and signal processing applications.

No, I do not use them myself in Racket.

Regards,

Dmitry

--
You received this message because you are subscribed to the Google Groups "Racket 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/cb9fe12e-5091-ec55-e542-73c19f8b7483%40iaaras.ru.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Macro guards question

2019-05-29 Thread Sorawee Porncharoenwase
The issue is that #'arg will always be a syntax object. An identifier is a
kind of syntax object, so it makes sense to test (identifier? #’arg).
However, (symbol? #’arg) and (string? #’arg) will always fail.

Suppose then that you invoke the macro with "1" as the operand, it would
fail every case in syntax-case, so syntax-case throws a bad syntax (because
a syntax transformation must be total), causing the error that you saw.

If you insist on using syntax-case, then try this:

(define-syntax (foo stx)
  (syntax-case stx ()
[(_ arg) (identifier? #'arg) #'1]
[(_ arg) (string? (syntax->datum #'arg)) #'2]
[(_ arg) (number? (syntax->datum #'arg)) #'3]
[(_ (quote* x)) (and (free-identifier=? #'quote* #'quote)
 (symbol? (syntax->datum #'x))) #'4]
[(_ _) #'5]))

(foo a) ;=> 1
(foo "a") ;=> 2
(foo 10) ;=> 3
(foo 'a) ;=> 4
(foo (bar x)) ;=> 5

A better way though is to use syntax-parse.

(require (for-syntax syntax/parse))

(define-syntax (foo stx)
  (syntax-parse stx
#:literals (quote)
[(_ arg:id) #'1]
[(_ arg:string) #'2]
[(_ arg:number) #'3]
[(_ (quote x:id)) #'4]
[(_ _) #'5]))

(foo a) ;=> 1
(foo "a") ;=> 2
(foo 10) ;=> 3
(foo 'a) ;=> 4
(foo (bar x)) ;=> 5


On Wed, May 29, 2019 at 10:23 AM Kevin Forchione  wrote:

> Hi Guys,
> What are the rules for macro guards? I’ve only seen examples with
> (identifier? #’val) being used. What about (number? #’val) or (spring?
> #’val)? When I try these I get a foo: bad syntax so I’m suspecting these
> can’t be used or there’s some trick to them.
>
> What I’ve been trying to create (and maybe this isn’t the right way to go
> about it) is a syntax-case that would have have various type checks as
> guards and then select the branch based on whether I’ve got an identifier
> or just a symbol, or a number or a string, etc.
>
> (syntax-case six ()
>   [(_ arg) (identifier? #’arg) #’(identifier-handler arg)]
>   [(_ arg) (symbol? #’arg) #’(symbol-handler arg)]
>   [(_ arg) (string? #’arg) #’(string-handler arg)]
>…)
>
> That sort of thing.
>
> Primarily I find myself running into an issue where I’m using symbols for
> lookup keys and identifiers for their reference values  and running into a
> wall of wanting the macro to go ahead and handle them differently without
> have the old “foo undefined” popping up. :)
>
> Kevin
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/DED7E28E-9B3D-4045-B0DC-CBD3AB11E653%40gmail.com
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CADcuegvP%3DpmJRuZCSFmQjtyo7nxQFmo0JnoXjCW1WpnEdmnMpg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Macro guards question

2019-05-29 Thread Ryan Kramer
#'arg is a syntax object, therefore (number? #'arg) or (string? #'arg) will 
always be false. If you are trying to dispatch based on literals, you could 
use e.g. (number? (syntax-e #'arg)) to identify a numeric literal. But it 
is not very common that a macro handles a numeric literal differently than 
an identifier that is bound to a numeric value at runtime. I'm guessing you 
can use a regular procedure and cond for most of this.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/111c1509-d77f-48d6-8c1f-19a37748cace%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Macro guards question

2019-05-29 Thread Kevin Forchione
Hi Guys,
What are the rules for macro guards? I’ve only seen examples with (identifier? 
#’val) being used. What about (number? #’val) or (spring? #’val)? When I try 
these I get a foo: bad syntax so I’m suspecting these can’t be used or there’s 
some trick to them. 

What I’ve been trying to create (and maybe this isn’t the right way to go about 
it) is a syntax-case that would have have various type checks as guards and 
then select the branch based on whether I’ve got an identifier or just a 
symbol, or a number or a string, etc. 

(syntax-case six ()
  [(_ arg) (identifier? #’arg) #’(identifier-handler arg)]
  [(_ arg) (symbol? #’arg) #’(symbol-handler arg)]
  [(_ arg) (string? #’arg) #’(string-handler arg)]
   …)

That sort of thing. 

Primarily I find myself running into an issue where I’m using symbols for 
lookup keys and identifiers for their reference values  and running into a wall 
of wanting the macro to go ahead and handle them differently without have the 
old “foo undefined” popping up. :) 

Kevin

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/DED7E28E-9B3D-4045-B0DC-CBD3AB11E653%40gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] anyone using single-flonums?

2019-05-29 Thread George Neuner



On 5/29/2019 11:52 AM, Matthew Flatt wrote:

Does anyone use single-flonums in Racket?

I don't mean `_float` or `f32vector`s, which convert C `float`s or
32-bit array elements into regular double-precision Racket flonums. I
mean literals like `3.0f0` or functions like `real->single-flonum`,
which produce a Racket number that uses only 32 bits for arithmetic.

Chez Scheme doesn't support single-precision floating-point numbers,
and adding them would be a lot of work --- for no benefit, if it turns
out that no one is using those kinds of numbers.

My guess is that no one uses them currently, because it's rare that
you'd want to trade speed for *im*precision. Single-flonums in Racket
are significantly slower than regular flonums, because they're not
treated as a common case. The only use I can think of, and the one that
inspired the original experiment, is to try to faithfully replicate a C
`float` calculation in Racket, but even that possibility has issues.

If no one uses single-precision floats, then I will suggest that we
remove them from Racket by making numbers like `3.0f0` parse as flonums
and making `real->single-flonum` raise `exn:fail:unsupported`.
Obviously, this would be a backward-incompatible change. But if it
causes little enough trouble, then it could be a good trade to avoid
problems for Racket CS and future platforms.


Question: does/will Chez support converting to/from 32-bit floats for C 
libraries?  It probably doesn't matter too much if Chez itself only 
*operates* on 64-bit values.  But being able to pass 32-bit values I 
think is important.


Machine learning is a hot topic now, and ANN often are implemented using 
matrix multiplication.  A typical net involves a lot of mulitplications, 
so, at least on regular CPUs, it usually IS done with 32-bit for speed.  
[On some GPUs it is done with 16-bit FP values, and Google has an 8-bit 
FP chip.  But no CPUs support these small FP values, and doing it with 
fixed point is a complex PITA (at least if you try to do it using SIMD 
ops)].


Aside: coming from a C/C++ image processing background, I've 
occasionally missed having SIMD ops native in Racket.  FFI works fine, 
but calling out to a C library "feels" less natural [a bit like being 
forced to use intrinsics or drop into ASM in a C program].  It would be 
nice if Racket's array ops went SIMD at some point.


YMMV,
George




--
You received this message because you are subscribed to the Google Groups "Racket 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/2e9df4a6-da50-7ac2-1bb5-2d0195a44b3a%40comcast.net.
For more options, visit https://groups.google.com/d/optout.


[racket-users] anyone using single-flonums?

2019-05-29 Thread Matthew Flatt
Does anyone use single-flonums in Racket?

I don't mean `_float` or `f32vector`s, which convert C `float`s or
32-bit array elements into regular double-precision Racket flonums. I
mean literals like `3.0f0` or functions like `real->single-flonum`,
which produce a Racket number that uses only 32 bits for arithmetic.

Chez Scheme doesn't support single-precision floating-point numbers,
and adding them would be a lot of work --- for no benefit, if it turns
out that no one is using those kinds of numbers.

My guess is that no one uses them currently, because it's rare that
you'd want to trade speed for *im*precision. Single-flonums in Racket
are significantly slower than regular flonums, because they're not
treated as a common case. The only use I can think of, and the one that
inspired the original experiment, is to try to faithfully replicate a C
`float` calculation in Racket, but even that possibility has issues.

If no one uses single-precision floats, then I will suggest that we
remove them from Racket by making numbers like `3.0f0` parse as flonums
and making `real->single-flonum` raise `exn:fail:unsupported`.
Obviously, this would be a backward-incompatible change. But if it
causes little enough trouble, then it could be a good trade to avoid
problems for Racket CS and future platforms.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/5ceeaac9.1c69fb81.118e3.f708SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] How to implement "APL" bitstrings in racket?

2019-05-29 Thread Raoul Schorer
Hi,

I would like to know how you would implement bitstrings "à la APL", meaning 
bitstrings/bitvectors of arbitrary dimensions with packed representation and 
possibilities for implicit parallelism.

I am under the impression that this essentially means adding a bit type to the 
numeric tower, but I don't really know where to begin. Any suggestion would be 
welcome.

I asked about bitstrings (valued 1|0) implementation in racket on Reddit 
already [1]. I received several suggestions among which:

- mapping Boolean bitvectors to integer vectors (which means no packed 
representation)

- or using the "rebellion" package, which implements packed bitstrings, but the 
implementation is partially imperative, making me worried about parallelism 
perspectives.

Thanks!
Raoul

[1] 
https://www.reddit.com/r/Racket/comments/btrscb/can_true_bitvectors_be_implemented_in_racket/?utm_medium=android_app_source=share

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/911abf34-f5b7-4c96-9e5b-d40f51d5378f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.