Re: Question about how to check a symbol is bound
> On 28/06/2023 14:09, felix.winkelm...@bevuta.com wrote: > >> > >> (define-object-type bar > >> (field-1 name-of-library#foo) > >> (field-2 name-of-other-library#some-other-type)) > >> > >> ...even though calling symbol-value on those symbols at run time works > >> just fine. It seems that the symbols imported into the environment at > >> macro expansion time are handled differently in some way. > >> > > > > I guess during expansion identifiers are renamed to some internal gensym > > and thus not accessible by name (which is the whole point of a hygienic > > macro system). Could you not register the "foo" type in a expansion time > > hash table? > > Alas, the requirement is that things like "foo" are part of the lexical > environment - so can be imported from modules, renamed, be hidden inside > local scopes, all that stuff! > I guess some macro wizard will come up with a way to do this. But I still think that keeping an expansion/compile-time registry and accessing it using procedure macros (via "strip-syntax"ed identifiers to drop all renaming artifacts) is a possible solution. felix
Re: Question about how to check a symbol is bound
On 28/06/2023 14:09, felix.winkelm...@bevuta.com wrote: I guess during expansion identifiers are renamed to some internal gensym and thus not accessible by name (which is the whole point of a hygienic macro system). Oh, and as a side thing, I don't think this is exactly "unhygienic". Well, the thing I'm trying to do is hygienic, and I'm just being forced to explore unhygienic techniques to try and recreate that hygienic solution. After all, we can do: (define-for-syntax foo 123) (define-syntax ... ... foo ...) ...and get 123 out of foo at macro expansion time. What I want to be able to do is: (define-inner-thing X 123) (define-outer-thing Y (X)) ...and have define-outer-thing's macro expander be able to get 123 from X. It doesn't really need to know about X as a symbol, and all this symbol-value nastiness is just a way to try and convert from X passed to it as a symbol to the value of X, which I'd rather do in a hygienic manner - if I could! Of course, we can do something like this: (define-syntax define-inner-thing (ir-macro-transformer ... `(define-syntax ,NAME (syntax-rules (context1 context2) ((_ context1) ,generated-code-for-context1) ((_ context2) ,generated-code-for-context2 ...) (define-syntax define-outer-thing (syntax-rules () ((define-outer-thing NAME ((FIELD TYPE) ...)) ...(TYPE context1)... ...(TYPE context2)...))) ...and thereby have define-inner-thing use arbitrary low-level macros to generate arbitrary code snippets and wrap them in a macro binding, that define-outer-thing can then pull out by invoking the macro. But define-outer-thing can only splice the generated code into its expansion, and not do things like combined generated ssql snippets and ssql->sql them at macro expansion time, which would be nice: if I follow that path I'll need to expand to code that calls a pure function on constant arguments at runtime. felix -- Alaric Snell-Pym (M0KTN neé M7KIT) http://www.snell-pym.org.uk/alaric/ OpenPGP_signature Description: OpenPGP digital signature
Re: Question about how to check a symbol is bound
On 28/06/2023 14:09, felix.winkelm...@bevuta.com wrote: (define-object-type bar (field-1 name-of-library#foo) (field-2 name-of-other-library#some-other-type)) ...even though calling symbol-value on those symbols at run time works just fine. It seems that the symbols imported into the environment at macro expansion time are handled differently in some way. I guess during expansion identifiers are renamed to some internal gensym and thus not accessible by name (which is the whole point of a hygienic macro system). Could you not register the "foo" type in a expansion time hash table? Alas, the requirement is that things like "foo" are part of the lexical environment - so can be imported from modules, renamed, be hidden inside local scopes, all that stuff! felix Thanks, -- Alaric Snell-Pym (M0KTN neé M7KIT) http://www.snell-pym.org.uk/alaric/ OpenPGP_signature Description: OpenPGP digital signature
Re: Question about how to check a symbol is bound
> > (define-object-type bar >(field-1 name-of-library#foo) >(field-2 name-of-other-library#some-other-type)) > > ...even though calling symbol-value on those symbols at run time works > just fine. It seems that the symbols imported into the environment at > macro expansion time are handled differently in some way. > I guess during expansion identifiers are renamed to some internal gensym and thus not accessible by name (which is the whole point of a hygienic macro system). Could you not register the "foo" type in a expansion time hash table? felix
Re: Question about how to check a symbol is bound
On 23/06/2023 14:35, Kon Lovett wrote: (symbol-value foo #f) Error: unbound variable: foo #;2> (symbol-value 'foo #f) #f (symbol-value foo #f) is asking for the symbol-value of the derefed variable `foo’ Does anybody have any tips for using symbol-value at macroexpansion time? I am trying to port a library from chicken 4 to 5, that lets users write something along these lines (full details available on request, I'm trying to just extract and summarise the important parts here): (define-custom-type foo ...some metadata...) And then later: (define-object-type bar ...list of fields and types...) Eg: ;; A foo contains three strings (define-custom-type foo string string string) (define-object-type bar (field-1 foo) (field-2 some-other-type)) Now, the implementation of define-object-type needs to be able to examine the contents of the foo custom-type at macro expansion time (to give a bit more detail, this is a wrapper around a SQL database and it needs to know the structure of everything to pre-generate SQL queries, create table statements, and so on) I've had partial success with making define-custom-type expand into a define-for-syntax that wraps a processed form of its metadata into a value and binds it to the symbol "foo", then making define-object-type find the type name symbols and call symbol-value on them - but this doesn't work if the type name is imported from a library. Even if I make the user write: (define-object-type bar (field-1 name-of-library#foo) (field-2 name-of-other-library#some-other-type)) ...even though calling symbol-value on those symbols at run time works just fine. It seems that the symbols imported into the environment at macro expansion time are handled differently in some way. Is there a way to do what I want? I'm also working on an alternate approach - where define-custom-type defines a macro that actually embeds the parts of the implementation of define-object-type that depend on the structure and make define-object-type invoke that macro as part of its expansion - but it's so inside out it makes my head hurt so I'm not 100% sure it'll be possible yet (at the very least, it might leave some string/list concatenation to run time, which is disappointing). And it's a sad violation of abstraction boundaries between define-custom-type and define-object-type. Hey ho. PS: Hi everyone! I'm still alive! -- Alaric Snell-Pym (M0KTN neé M7KIT) http://www.snell-pym.org.uk/alaric/ OpenPGP_signature Description: OpenPGP digital signature