Re: [racket-users] [Racket Users] Macros and literals question

2018-09-17 Thread Matthew Butterick

> On Sep 17, 2018, at 11:57 AM, Kevin Forchione  wrote:
> 
> In a nutshell I’m working with some hash tables whose keys are symbol and 
> whose values may be other keys or values such as identifiers, and I got a bit 
> tired of quoting all my symbols for functions and decided to use some macros 
> when working with the tables.

OK. Yes, in general you need a macro to convert something like `(id bar)` to 
`(other-id 'bar)`, because otherwise the naked `bar` is treated as an 
identifier (and therefore as usual needs a binding, or the program won't run). 
For even more notational convenience, you might also look at the `read-cdot` 
parameter and `#%dot` identifier, which can expand `table.key` to something 
like `(hash-ref table 'key)`.

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] [Racket Users] Macros and literals question

2018-09-17 Thread Kevin Forchione


> On Sep 16, 2018, at 10:07 PM, Matthew Butterick  wrote:
> 
> 
>> On Sep 16, 2018, at 2:13 PM, Kevin Forchione > > wrote:
>> 
>> Thanks! That’s just what I wanted. Is there a way in Racket to determine if 
>> a quoted symbol has an associated procedure? 
> 
> 
> 
> #lang racket
> (require rackunit)
> 
> (define-syntax (bound-to-proc? stx)
>   (syntax-case stx ()
> [(_ 'x)
>  (and (identifier? #'x) (identifier-binding #'x))
>  #'(procedure? x)]
> [_ #'#f]))
> 
> (define bound-to-proc +)
> (define not-bound-to-proc 42)
> 
> (check-true (bound-to-proc? 'bound-to-proc))
> (check-false (bound-to-proc? 'not-bound-to-proc))

Sorry for the typo and thanks for the further explanation. Your original 
response to my query was more in line with what I’m trying to accomplish, while 
my second question was more in the realm of  curiosity. In a nutshell I’m 
working with some hash tables whose keys are symbol and whose values may be 
other keys or values such as identifiers, and I got a bit tired of quoting all 
my symbols for functions and decided to use some macros when working with the 
tables. Probably more than curiosity as I can foresee a need to retrieve a 
value from a table and either apply it, which means somehow getting at the 
binding. 

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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] [Racket Users] Macros and literals question

2018-09-17 Thread Alexis King
> On Sep 17, 2018, at 12:21, Kevin Forchione  wrote:
> 
> That seems to be the nature of macros, and I’m not sure what the solution to 
> that paradox is, apart from perhaps building a symbol/function hash table as 
> part of a define. Presumably Racke does something like that for eva & 
> namespaces, but I’m surprised there isn’t a symbol->procedure function. 

In general, I think Philip has hit the nail on the head. I, too, thought of 
other Lisp systems when you used the wording “associated procedure” for a 
symbol, and I agree that concept makes less sense in Racket. In a more 
traditional Lisp, symbols themselves are rich values with lots of information 
attached to them, but Racket follows the Scheme approach, which treats symbols 
as nothing more than interned strings. They are just names, not bindings.

To elaborate a little more on this, it might make sense to think of the 
“associated procedure” for a symbol in Common Lisp, but in Racket, that 
information is stored in a separate place: namespaces. A Racket namespace 
represents an environment, and it maps names to values. At runtime, if you want 
to get the value bound to a particular symbol in a given namespace, you can use 
namespace-variable-value[1], but there are some caveats to that.

As mentioned above, in Common Lisp, a package-qualified symbol is precise, but 
a Racket symbol is only meaningful in context. This means that the symbol 'foo 
might be mapped to one thing in one namespace but something completely 
different in another namespace. This is a philosophically different approach to 
binding, but the practical takeaway is that you need to be really careful to 
use the *right* namespace if you want to use namespaces in Racket. A namespace 
can be created that corresponds to the top-level of a module, using 
module->namespace, or it can be an entirely independent, “free-floating” 
top-level environment created with make-base-namespace. You can acquire a 
namespace that corresponds to the surrounding lexical environment using 
define-namespace-anchor[2] and namespace-anchor->namespace[3] or 
#%variable-reference[4] and variable-reference->namespace[5].

However, beware! As Philip alludes to, namespaces only include top-level or 
module-level bindings, *not* local bindings! Racket provides no reflective 
capabilities to inspect local bindings (at least not in general; you could 
theoretically write a #lang that implements this capability yourself). These 
are opaque, and indeed, the Racket compiler does not maintain information about 
the structure of local bindings.

To summarize: Racket is a relatively static language relative to other Lisps, 
and even to other Schemes. The language is entirely {lexically, statically} 
bound. It provides limited reflective capabilities to manipulate top-level 
namespaces and inspect module-level bindings, but no more. There are definitely 
various advantages to this (e.g. local reasoning, encapsulation, optimization), 
but you may need to think differently about Racket’s binding model if coming 
from other Lisp systems.

Alexis

[1]: 
http://docs.racket-lang.org/reference/Namespaces.html#%28def._%28%28quote._~23~25kernel%29._namespace-variable-value%29%29
[2]: 
http://docs.racket-lang.org/reference/Namespaces.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-namespace-anchor%29%29
[3]: 
http://docs.racket-lang.org/reference/Namespaces.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._namespace-anchor-~3enamespace%29%29
[4]: 
http://docs.racket-lang.org/reference/Locationsvariable-reference.html#%28form._%28%28quote._~23~25kernel%29._~23~25variable-reference%29%29
[5]: 
http://docs.racket-lang.org/reference/Namespaces.html#%28def._%28%28quote._~23~25kernel%29._variable-reference-~3enamespace%29%29

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] [Racket Users] Macros and literals question

2018-09-17 Thread Matthew Butterick

> On Sep 17, 2018, at 10:21 AM, Kevin Forchione  wrote:
> 
> That seems to be the nature of macros, and I’m not sure what the solution to 
> that paradox is, apart from perhaps building a symbol/function hash table as 
> part of a define. Presumably Racke does something like that for eva & 
> namespaces, but I’m surprised there isn’t a symbol->procedure function. 


I take a question as I find it, even if its necessity is not clear to me ;) It 
seems a teeny bit possible that you're moving against the grain of how 
identifiers & bindings usually work, and thus certain aspects seem more 
complicated than they need to. 

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] [Racket Users] Macros and literals question

2018-09-17 Thread Philip McGrath
It might help to know a bit more context about what you're trying to do.

I think other Lisp-family languages use these terms in different ways, but
in Racket it isn't usual to talk about a symbol being bound to something. A
symbol is just a type of value (like an integer, a list, a vector, a
function, or anything else) and exists only at the run-time of some code.
Binding, on the other hand, is a compile-time notion. Functions like
`identifier-binding` operate on identifiers, which are syntax objects that
wrap symbols together with lexical context and source-location information:
in other words, the first argument to `identifier-binding` must satisfy
`identifier?`, which means it will never satisfy `symbol?`. Given renaming,
lexical scope, and other features, a symbol isn't enough information to
specify a binding, and I believe that at run-time symbolic names go away
and are replaced by some form of de Bruijn indexes. (Take that last part
with a grain of salt: I don't spend much time with low-level runtime
details.) Also, in `#lang racket`, given that there isn't a static type
system, the question of whether an identifier is bound to a procedure isn't
answerable at compile-time: Matthew's macro thus expands to false for
unbound identifiers, but expands to a `procedure?` check at runtime for all
bound identifiers.

At the same time, if you explain what you are trying to do at a
higher-level, there very well may be a way to do it that involves
implementation techniques you wouldn't think to ask about. For example, if
you want unbound identifiers to be self-quoting as symbols, the answer
would involve defining a macro named `#%top`.

Finally, I'm not sure that I understand your example:

> [define [foo symbols) (bound-to-proc? sum))
>
Ignoring the mismatched parentheses, this use of `bound-to-proc?` doesn't
match the first clause of Matthew's `syntax-case` because there is no
`quote`, and it looks like Matthew tried to implement your request to
determine "if a quoted symbol has an associated procedure".

(The implementation of `bound-to-proc?` would be more robust if
`syntax-case` were given `quote` as a literal, but that way lies madness:
what about `quasiquote`, or macros you don't know about that expand to
symbols? Using `local-expand` could help … but, again, the best thing would
be to know more about what you're ultimately trying to do.)

-Philip


On Mon, Sep 17, 2018 at 12:21 PM Kevin Forchione  wrote:

>
>
> On Sep 16, 2018, at 10:07 PM, Matthew Butterick  wrote:
>
> #lang racket
> (require rackunit)
>
> (define-syntax (bound-to-proc? stx)
>   (syntax-case stx ()
> [(_ 'x)
>  (and (identifier? #'x) (identifier-binding #'x))
>  #'(procedure? x)]
> [_ #'#f]))
>
> (define bound-to-proc +)
> (define not-bound-to-proc 42)
>
> (check-true (bound-to-proc? 'bound-to-proc))
> (check-false (bound-to-proc? 'not-bound-to-proc))
>
>
> Thanks, Matthew. This approach works as long as the macro is being used
> outside of a definition, but I suspect to build any sort of logic around it
> they’d have to be macros as well. For instance, this fails because the
> macro captures symptoms and not the value passed to the function:
>
> [define [foo symbols) (bound-to-proc? sum))
>
> That seems to be the nature of macros, and I’m not sure what the solution
> to that paradox is, apart from perhaps building a symbol/function hash
> table as part of a define. Presumably Racke does something like that for
> eva & namespaces, but I’m surprised there isn’t a symbol->procedure
> function.
>
> 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.
> 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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] [Racket Users] Macros and literals question

2018-09-17 Thread Kevin Forchione


> On Sep 16, 2018, at 10:07 PM, Matthew Butterick  wrote:
> 
> #lang racket
> (require rackunit)
> 
> (define-syntax (bound-to-proc? stx)
>   (syntax-case stx ()
> [(_ 'x)
>  (and (identifier? #'x) (identifier-binding #'x))
>  #'(procedure? x)]
> [_ #'#f]))
> 
> (define bound-to-proc +)
> (define not-bound-to-proc 42)
> 
> (check-true (bound-to-proc? 'bound-to-proc))
> (check-false (bound-to-proc? 'not-bound-to-proc))

Thanks, Matthew. This approach works as long as the macro is being used outside 
of a definition, but I suspect to build any sort of logic around it they’d have 
to be macros as well. For instance, this fails because the macro captures 
symptoms and not the value passed to the function:

[define [foo symbols) (bound-to-proc? sum)) 

That seems to be the nature of macros, and I’m not sure what the solution to 
that paradox is, apart from perhaps building a symbol/function hash table as 
part of a define. Presumably Racke does something like that for eva & 
namespaces, but I’m surprised there isn’t a symbol->procedure function. 

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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] [Racket Users] Macros and literals question

2018-09-16 Thread Matthew Butterick

> On Sep 16, 2018, at 2:13 PM, Kevin Forchione  > wrote:
> 
> Thanks! That’s just what I wanted. Is there a way in Racket to determine if a 
> quoted symbol has an associated procedure? 



#lang racket
(require rackunit)

(define-syntax (bound-to-proc? stx)
  (syntax-case stx ()
[(_ 'x)
 (and (identifier? #'x) (identifier-binding #'x))
 #'(procedure? x)]
[_ #'#f]))

(define bound-to-proc +)
(define not-bound-to-proc 42)

(check-true (bound-to-proc? 'bound-to-proc))
(check-false (bound-to-proc? 'not-bound-to-proc))

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] [Racket Users] Macros and literals question

2018-09-16 Thread Kevin Forchione


> On Sep 15, 2018, at 7:27 PM, Philip McGrath  wrote:
> 
> Sure. For example:
> 
> #lang racket
> 
> (require syntax/parse/define
>  rackunit)
> 
> (define-syntax-parser foo
>   [(_ arg:id)
>#''arg]
>   [(_ arg:expr)
>#'arg])
> 
> (check-eqv? (foo 10) 10)
> (check-eq? (foo hello) 'hello)
> (check-pred procedure? (foo (λ (x) (* 2 x
> 
> -Philip
> 
> 
> On Sat, Sep 15, 2018 at 9:17 PM Kevin Forchione  > wrote:
> Hi guys,
> Is there a way to define a macro so that an argument will be quoted only when 
> it is a symbol? Something like this:
> 
> (foo 10) => 10
> (foo hello) => ‘hello
> (foo (lambda (x) (* 2 x))) => 
> etc.

Thanks! That’s just what I wanted. Is there a way in Racket to determine if a 
quoted symbol has an associated procedure? 

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 
> .
> 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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] [Racket Users] Macros and literals question

2018-09-15 Thread Philip McGrath
Sure. For example:

#lang racket

(require syntax/parse/define
 rackunit)

(define-syntax-parser foo
  [(_ arg:id)
   #''arg]
  [(_ arg:expr)
   #'arg])

(check-eqv? (foo 10) 10)
(check-eq? (foo hello) 'hello)
(check-pred procedure? (foo (λ (x) (* 2 x

-Philip


On Sat, Sep 15, 2018 at 9:17 PM Kevin Forchione  wrote:

> Hi guys,
> Is there a way to define a macro so that an argument will be quoted only
> when it is a symbol? Something like this:
>
> (foo 10) => 10
> (foo hello) => ‘hello
> (foo (lambda (x) (* 2 x))) => 
> etc.
>
> 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.
> 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.
For more options, visit https://groups.google.com/d/optout.


[racket-users] [Racket Users] Macros and literals question

2018-09-15 Thread Kevin Forchione
Hi guys,
Is there a way to define a macro so that an argument will be quoted only when 
it is a symbol? Something like this:

(foo 10) => 10
(foo hello) => ‘hello
(foo (lambda (x) (* 2 x))) => 
etc.

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.
For more options, visit https://groups.google.com/d/optout.