我可以在Scheme 的过程内部定义一个全局变量吗?

发布于 2024-08-14 11:08:56 字数 493 浏览 3 评论 0原文

我有一种情况,我想做一些类似的事情......

(define (def a b)
    (store a b) ; store the definition of 'a' somewhere
    (define-global a b)) ; also define 'a' so that its definition
                         ; is accessible later in the program

这有可能吗?据我所知 define-global 不存在,因此过程中的 define 语句仅适用于本地环境。

这是为了在方案中为嵌入式 DSL 创建“def”过程,因此除了进行定义之外,我还需要将定义存储在我自己的符号表中。最终我想“拦截”符号查找以应用我自己的转换,返回符号查找的表达式而不是实际执行它。

我正在使用 Gambit-C 方案。

谢谢。

I have a situation where I'd like to do something like...

(define (def a b)
    (store a b) ; store the definition of 'a' somewhere
    (define-global a b)) ; also define 'a' so that its definition
                         ; is accessible later in the program

Is this possible somehow? As far as I know define-global doesn't exist, so define statements inside procedures apply only to the local environment.

This is intended for creating a 'def' procedure for an embedded DSL in scheme, so in addition to making the definition I need to store the definition in my own symbol table. Eventually I want to 'intercept' symbol look-ups to apply my own transformation, returning an expression for the symbol look-up instead of actually performing it.

I'm using Gambit-C Scheme.

Thanks.

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

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

发布评论

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

评论(3

陌生 2024-08-21 11:08:56

不,至少没有任何 Gambit 特定的低级钩子。但这是有充分理由的:您的建议将导致无法进行有效的编译,因为绑定可以随时更改。

如果您的目标是实现 DSL,那么将值保留在您自己的表中,并在 DSL 中实现变量查找的代码中进行查找非常有意义。此外,它自然会引导您实现 DSL 存在于其自己的世界中的实现,与实现它的代码分开。 (例如,如果您有上述情况,那么当 DSL 内的代码定义一个名为 def 的变量时,您期望会发生什么?)

No, at least not without any Gambit specific low-level hooks. But this is for a good reason: what you're suggesting would make it impossible to do efficient compilation, since bindings can change at any time.

If your goal is to implement a DSL, then keeping the values in your own table, and doing the lookup in code that implements variable lookup in your DSL makes a lot of sense. In addition, it will naturally lead you to an implementation where the DSL lives in its own world, separately from the code that implements it. (For example, if you have the above, then what would you expect to happen when code inside the DSL defines a variable called def?)

浅唱ヾ落雨殇 2024-08-21 11:08:56

也许有办法解决这个问题。我一直在寻找一种在玩具方案中实现这一目标的方法,并且偶然发现了一种在特定环境中定义的方法。如果 gambit 有一个用于 eval 的环境选项,那么您应该能够通过调用 (interaction-environment) 来获取父作用域中的环境。将其保存在全局环境中或通过参数传递。您可以执行以下操作:(

定义全局环境(交互环境))

;;
;;其他代码并进入调用堆栈

(eval (cons 'define (cons symbol-a (cons value '()))) global-environment)

这应该使用传入的环境评估表达式并得到您想要的内容。如果一切正常的话。它取决于调用以获取对全局环境的引用以及可以将环境作为参数的评估。

There might be a way to pull this off. I was looking for a means of pulling this off in a toy scheme and happened across a means of defining within a specific environment. If gambit has an environment option to eval, then you should be able to get the environment in you parent scope with a call to (interaction-environment). Either save this in the global environment or pass it through arguments. You could be able to do something like:

(define global-environment (interaction-environment))

;;
;; other code and decent into the call stack

(eval (cons 'define (cons symbol-a (cons value '()))) global-environment)

This should evaluated the expression using the passed in environment and get about what you want. If everything is working correctly. It hinges on having the call to get a reference to the global environment and an eval that can take an environment as an argument.

温柔少女心 2024-08-21 11:08:56
;; It's always possible to create a global variable
;; (assigned to #f so you know if it's been assigned to yet)
;; and just use it later.
(define *some-global-var* #f)
;; then later on...
(set! *some-global-var* 234234) ;; or w/e you want
(if *some-global-var* ;; it's been assigned
    (do-something *some-global-var*))

;; But in your case, a (hash) table is definitely
;; much better than unorganized free variables.
;; Gambit-C has hash tables built in. So, you can
;; define the hash table early on...
(define *globals* (make-table))
;; And assign to it later. Use the "variable name"
;; for the key.
(table-set! *globals* 'x 3)
(table-set! *globals* 'y 4)
;; Whenever you want you plan to access
;; a lot, it's best to use a "let"
(let ((x (table-ref *globals* 'x)) (y (table-ref *globals* 'y)))
  (println "The sum of " x " and " y " is " (+ x y) "."))
The sum of 3 and 4 is 7.

;; Gambit also has structures, and they too are useful
;; for organizing data (*kind of* like C structs).
;; Note that it's possible to assign structures to hash
;; tables -- just like anything else -- and sometimes
;; it's *very* useful to do so.
(define-structure person name age)
(define jy (make-person "jyaan" 9000))
(person-name jy)
"jyaan"
(person-age-set! jy 25)
(println jy)
#<person #3 name: "jyaan" age: 25>
;; It's always possible to create a global variable
;; (assigned to #f so you know if it's been assigned to yet)
;; and just use it later.
(define *some-global-var* #f)
;; then later on...
(set! *some-global-var* 234234) ;; or w/e you want
(if *some-global-var* ;; it's been assigned
    (do-something *some-global-var*))

;; But in your case, a (hash) table is definitely
;; much better than unorganized free variables.
;; Gambit-C has hash tables built in. So, you can
;; define the hash table early on...
(define *globals* (make-table))
;; And assign to it later. Use the "variable name"
;; for the key.
(table-set! *globals* 'x 3)
(table-set! *globals* 'y 4)
;; Whenever you want you plan to access
;; a lot, it's best to use a "let"
(let ((x (table-ref *globals* 'x)) (y (table-ref *globals* 'y)))
  (println "The sum of " x " and " y " is " (+ x y) "."))
The sum of 3 and 4 is 7.

;; Gambit also has structures, and they too are useful
;; for organizing data (*kind of* like C structs).
;; Note that it's possible to assign structures to hash
;; tables -- just like anything else -- and sometimes
;; it's *very* useful to do so.
(define-structure person name age)
(define jy (make-person "jyaan" 9000))
(person-name jy)
"jyaan"
(person-age-set! jy 25)
(println jy)
#<person #3 name: "jyaan" age: 25>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文