From mboxrd@z Thu Jan  1 00:00:00 1970
Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail
From: Christina O'Donnell <cdo@mutix.org>
Newsgroups: gmane.comp.gnu.guix.user,gmane.lisp.guile.user
Subject: Re: Getting all symbols in a Scheme file as a list
Date: Mon, 5 Feb 2024 13:04:34 +0000
Message-ID: <e14033c6-a437-b1d2-287b-9565f1d1e117@mutix.org>
References: <03f7199a-daf2-e070-8156-6837d7e20e32@mutix.org>
 <20240204220328.j93T2B00C1cshW70193U6r@laurent.telenet-ops.be>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214";
	logging-data="26855"; mail-complaints-to="usenet@ciao.gmane.io"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
 Thunderbird/102.15.0
Cc: "help-guix@gnu.org" <help-guix@gnu.org>
To: M <maximedevos@telenet.be>, "guile-user@gnu.org" <guile-user@gnu.org>
Original-X-From: help-guix-bounces+gcggh-help-guix=m.gmane-mx.org@gnu.org Mon Feb 05 14:05:14 2024
Return-path: <help-guix-bounces+gcggh-help-guix=m.gmane-mx.org@gnu.org>
Envelope-to: gcggh-help-guix@m.gmane-mx.org
Original-Received: from lists.gnu.org ([209.51.188.17])
	by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
	(Exim 4.92)
	(envelope-from <help-guix-bounces+gcggh-help-guix=m.gmane-mx.org@gnu.org>)
	id 1rWyew-0006lQ-81
	for gcggh-help-guix@m.gmane-mx.org; Mon, 05 Feb 2024 14:05:14 +0100
Original-Received: from localhost ([::1] helo=lists1p.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.90_1)
	(envelope-from <help-guix-bounces@gnu.org>)
	id 1rWyeW-0002YR-0M; Mon, 05 Feb 2024 08:04:48 -0500
Original-Received: from eggs.gnu.org ([2001:470:142:3::10])
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <cdo@mutix.org>)
 id 1rWyeQ-0002Xg-BC; Mon, 05 Feb 2024 08:04:43 -0500
Original-Received: from vmi993448.contaboserver.net ([194.163.141.236] helo=mutix.org)
 by eggs.gnu.org with esmtp (Exim 4.90_1)
 (envelope-from <cdo@mutix.org>)
 id 1rWyeM-0003Pa-Ov; Mon, 05 Feb 2024 08:04:42 -0500
Original-Received: from [192.168.1.172]
 (host86-132-246-87.range86-132.btcentralplus.com [86.132.246.87])
 (Authenticated sender: cdo)
 by mutix.org (Postfix) with ESMTPSA id 11C86A63374;
 Mon,  5 Feb 2024 14:04:35 +0100 (CET)
Content-Language: en-US
In-Reply-To: <20240204220328.j93T2B00C1cshW70193U6r@laurent.telenet-ops.be>
Received-SPF: pass client-ip=194.163.141.236; envelope-from=cdo@mutix.org;
 helo=mutix.org
X-Spam_score_int: -34
X-Spam_score: -3.5
X-Spam_bar: ---
X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, HTML_MESSAGE=0.001,
 NICE_REPLY_A=-1.543, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001,
 T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-Content-Filtered-By: Mailman/MimeDel 2.1.29
X-BeenThere: help-guix@gnu.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: <help-guix.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/help-guix>,
 <mailto:help-guix-request@gnu.org?subject=unsubscribe>
List-Archive: <https://lists.gnu.org/archive/html/help-guix>
List-Post: <mailto:help-guix@gnu.org>
List-Help: <mailto:help-guix-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/help-guix>,
 <mailto:help-guix-request@gnu.org?subject=subscribe>
Errors-To: help-guix-bounces+gcggh-help-guix=m.gmane-mx.org@gnu.org
Original-Sender: help-guix-bounces+gcggh-help-guix=m.gmane-mx.org@gnu.org
Xref: news.gmane.io gmane.comp.gnu.guix.user:17826 gmane.lisp.guile.user:19440
Archived-At: <http://permalink.gmane.org/gmane.comp.gnu.guix.user/17826>

Hi,

> There is no such thing as symbol definitions in Scheme – you can make 
> symbols with symbol->string, but you can’t define a symbol to 
> anything, symbols simply are.
>
> You can, however, define variables, which have a symbol as name (and 
> that name may depend on context in case of hygienic macros or renamed 
> imports/exports in modules).
Thanks for the clarification. I'll try to remember this.

> You can probably find a simpler way of doing it by locating where 
> “,binding” is implemented (in the Guile source code). It probably uses 
> the module reflection API. This API is documented in
>
> https://www.gnu.org/software/guile/manual/html_node/Module-System-Reflection.html
>
> However, the documentation is incomplete – IIRC there is a procedure 
> ‘module-bindings’ to find a list of top-level definitions, but it 
> doesn’t seem to be documented.
>
Got it! The REPL uses module-for-each, in (ice-9 boot-9), which is 
exported publicly:

(define (module-for-each proc module)
   "Call PROC on each symbol in MODULE, with arguments of (SYMBOL 
VARIABLE)."
   (hash-for-each proc (module-obarray module)))

This is called from (system repl command):

(define-meta-command (binding repl)
   "binding
List current bindings."
   (module-for-each (lambda (k v) (format #t "~23A ~A\n" k v))
                    (current-module)))

That'll do it for me!

> To find what modules a module uses, there is module-uses, though I 
> doubt it is reliable w.r.t. optimisation, inlining, uses of (@ (module 
> name) variable), …
>
> So, instead, I would propose to instead work  on the Tree-IL level 
> (https://www.gnu.org/software/guile/manual/html_node/Tree_002dIL.html). 
> In particular, see <toplevel-define> for definitions and 
> <module-ref>,<module-set!> for uses of other modules.
>
> This also allows for more fine-grained information – e.g. these 
> <module-ref> etc. objects contain the location in the source code, and 
> if a <module-ref> is inside a <toplevel-define> then you know that it 
> is the procedure (assuming it is a procedure) of the <toplevel-define> 
> that uses the variable of the <module-ref>.
>
> (See 
> https://www.gnu.org/software/guile/manual/html_node/The-Scheme-Compiler.html 
> for how to compile stuff – also, IIRC, the compilation procedure 
> accepts ports instead of only S-expressions, despite what the example 
> suggests.)
>
Thanks. I know what I'm read next.

Thank you so much for your help!
  - Christina

On 04/02/2024 21:03, M wrote:
>
> >I'm trying to write a Guile script to trace symbol definition and 
> reference
>
> between modules in a large Guile repo (GNU/Guix), for the purposes of 
> large
>
> scale refactoring.
>
> There is no such thing as symbol definitions in Scheme – you can make 
> symbols with symbol->string, but you can’t define a symbol to 
> anything, symbols simply are.
>
> You can, however, define variables, which have a symbol as name (and 
> that name may depend on context in case of hygienic macros or renamed 
> imports/exports in modules).
>
> >I'm wondering how I could programmatically get all the values in a
>
> Scheme file
>
> as an S-expression. From the manual, I know that the REPL has meta keyword
>
> ',binding' and ',apropos'
>
> <https://www.gnu.org/software/guile/manual/guile.html#index-apropos>
>
> which let
>
> you search and list all bindings in accessible to a module. These are
>
> exactly
>
> what I need only, since they're meta-commands, they don't produce Scheme
>
> expressions.
>
> >Does anyone have any pointers? Should I go down the route of,
>
> >    (open-input-pipe (string-append "guile -l" file "-c ,binding"))
>
> >? Seems a little bit baroque to me, I'd expect a simpler way of doing
>
> it. Any
>
> libraries anyone knows of?
>
> No. The quoting is incorrect when file has spaces, \, …, guile in PATH 
> might not be the Guile that is being run, … (For the former, consider 
> open-input-pipe*.)
>
> You can probably find a simpler way of doing it by locating where 
> “,binding” is implemented (in the Guile source code). It probably uses 
> the module reflection API. This API is documented in
>
> https://www.gnu.org/software/guile/manual/html_node/Module-System-Reflection.html
>
> However, the documentation is incomplete – IIRC there is a procedure 
> ‘module-bindings’ to find a list of top-level definitions, but it 
> doesn’t seem to be documented.
>
> To find what modules a module uses, there is module-uses, though I 
> doubt it is reliable w.r.t. optimisation, inlining, uses of (@ (module 
> name) variable), …
>
> So, instead, I would propose to instead work  on the Tree-IL level 
> (https://www.gnu.org/software/guile/manual/html_node/Tree_002dIL.html). 
> In particular, see <toplevel-define> for definitions and 
> <module-ref>,<module-set!> for uses of other modules.
>
> This also allows for more fine-grained information – e.g. these 
> <module-ref> etc. objects contain the location in the source code, and 
> if a <module-ref> is inside a <toplevel-define> then you know that it 
> is the procedure (assuming it is a procedure) of the <toplevel-define> 
> that uses the variable of the <module-ref>.
>
> (See 
> https://www.gnu.org/software/guile/manual/html_node/The-Scheme-Compiler.html 
> for how to compile stuff – also, IIRC, the compilation procedure 
> accepts ports instead of only S-expressions, despite what the example 
> suggests.)
>
> Best regards,
>
> Maxime Devos
>