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 <lyss...@gmail.com> wrote:

>
>
> On Sep 16, 2018, at 10:07 PM, Matthew Butterick <m...@mbtype.com> 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.

Reply via email to