如何检查/导出/序列化(狡猾的)Scheme 环境

发布于 2024-11-05 03:17:40 字数 325 浏览 4 评论 0原文

我想在另一个 guile 进程中导出或复制方案环境。我想象的算法会做这样的事情来序列化:

(map (lambda (var val) (display (quasiquote (define ,var ,val))
                       (newline))
     (get-current-environment))

然后我会在另一端读取/评估它。

然而,虽然有些函数可以返回当前环境,但它们采用某种内部格式,我无法映射。怎样才能“行走”如上的环境呢?或者,我还能如何将环境复制到另一个进程中?

I'd like to export or replicate a scheme environment in another guile process. The algorithm I'm imagining would do something like this to serialize:

(map (lambda (var val) (display (quasiquote (define ,var ,val))
                       (newline))
     (get-current-environment))

And then I'd read/eval that on the other end.

However, while there are functions that return the current environment, they are in some internal format that I can't just map across. How can I "walk" the environment as the above? Alternatively, how else can I replicate an environment into another process?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尘曦 2024-11-12 03:17:40

你可以像这样分解所谓的“当前环境”:

(define (get-current-binding-list)
        (let* ((e (current-module))  ;; assume checking current-module

               (h (struct-ref e 0))  ;; index 0 is current vars hashtable
              )
       (hash-map->list cons h)  ;; return a vars binding list
    ))

并且你可以调用 (get-current-binding-list) 来获取当前模块中的变量绑定列表。
请注意,此列表中的每个元素都是一对符号和变量类型,例如(符号名称.变量类型)。所以你可以这样打印:
例如,你有一个 var 绑定:

(define abc 5)

然后:

(let ((vl (get-current-binding-list)))
      (assoc-ref vl 'abc)
      )

==> #<变量 9bb5108 值:5>
这个结果是变量“abc”的“变量类型”。您可以使用variable-ref 过程获取它的值。

因此,您可以跟踪所有绑定并在代码中执行某些操作,只需打印 var-name 和 var-value 即可。

我知道我的回答太简短,但我认为有足够的信息可以帮助您在手册中找到更多详细信息。
希望这会对您有所帮助。

you may decompose the so-called "current-environment" like this:

(define (get-current-binding-list)
        (let* ((e (current-module))  ;; assume checking current-module

               (h (struct-ref e 0))  ;; index 0 is current vars hashtable
              )
       (hash-map->list cons h)  ;; return a vars binding list
    ))

and you can call (get-current-binding-list) to get variables binding list in current-module.
Please note that each element in this list is a pair of symbol and variable type, say, (symbol-name . variable-type). So you may print it like this:
for a instance ,you got a var binding:

(define abc 5)

then:

(let ((vl (get-current-binding-list)))
      (assoc-ref vl 'abc)
      )

==> #<variable 9bb5108 value: 5>
This result is a "variable type" of variable "abc". You can get it's value with variable-ref procedure.

So you can trace all the bindings and do something ,in your code ,it's simply print var-name and var-value.

I know my answer is too brief, but I think there's enough information to help you to find more details in the manual.
Hope this will help you.

倾听心声的旋律 2024-11-12 03:17:40

你不能真正序列化Scheme环境。我什至不知道是否可以(可移植地)序列化延续。哦,别忘了 FFI。端口和线程也是不可序列化的。

You can't really serialize Scheme environment. I don't known even it's possible to (portably) serialize continuations. Oh, and don't forget about FFIs. Ports and threads are unserializable too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文