miniKanren:如何定义#s 和#u? [英] miniKanren: How to define #s and #u?

查看:63
本文介绍了miniKanren:如何定义#s 和#u?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在miniKanren中,succeed可以定义为(define success (== #t #t)),并且可以定义fail(定义失败(=== #t #f)).但是 #s#u 作为 succeedfail 的简写形式呢,它们出现在 理性策划者?

In miniKanren, succeed can be defined as (define succeed (== #t #t)), and fail can be defined as (define fail (=== #t #f)). But what about #s and #u as short forms of succeed and fail, as they appear in The Reasoned Schemer?

(define #s success) 在 Racket 中产生错误:

(define #s succeed) produces an error in Racket:

Welcome to Racket v7.2.
> (require Racket-miniKanren/miniKanren/mk)
> (define #s succeed)
; readline-input:2:8: read-syntax: expected `(`, `[`, or `{` after `#s` [,bt
;   for context]
#<procedure:...iniKanren/mk.rkt:337:4>
; readline-input:2:18: read-syntax: unexpected `)` [,bt for context]

我感觉这与阅读器宏有关.

I have the feeling that this has something to do with reader macros.

如何在 Scheme 和 Racket 中为 succeed#ufail 定义 #s?

How can I define #s for succeed and #u for fail in Scheme and also in Racket?

我正在使用 Scheme 的规范 miniKanren 实现Racket 的规范 miniKanren 实现.

推荐答案

Racket 中的标识符不能以 # 开头.绑定标识符su 很简单.重新定义 #s#u 的含义并不那么简单,因为它需要在阅读器中发生.通常 #something 会向读者发出信号,表示要阅读一些特别的东西.输入的 (foo bar) 将被读取为一个列表,#(foo bar) 将被读取为一个向量,而 #s(foo bar) 将作为结构读取.您可以在此处阅读有关标准语法的信息:

Identifiers in Racket can not begin with #. It is simple to bind the identifiers s and u. Redefining the meaning of #s and #u is not as simple, since it needs to happen in the reader. Normally #something signals to reader that something special is to be read. The input (foo bar) will be read as a list, #(foo bar) will be read as a vector, and #s(foo bar) will be read as a structure. You can read about the standard syntax here:

https:///docs.racket-lang.org/reference/reader.html?q=%23s#%28mod-path._reader%29

现在如果你想改变#s#u的含义,你需要查看readtables.每次阅读器看到 # 时,它都会查阅一个 readtable 以了解如何处理以下字符.由于读取发生在解析/扩展和评估之前,因此您不能简单地通过调用程序中的函数来更改读取器.您将需要使用#reader 扩展机制或创建您自己的语言.

Now if you want to change the meaning of #s and #u you need to look at readtables. Each time the reader sees an # it consults a readtable to see how to handle the following characters. Since reading happens before parsing/expansion and evaluation, you can't change the reader simply by calling a function in your program. You will need to either use the #reader extension mechanism or create your own language.

有关可读表的更多信息:https://docs.racket-lang.org/reference/readtables.html?q=reader-macro

For more on readtables: https://docs.racket-lang.org/reference/readtables.html?q=reader-macro

指南有一个如何使用阅读器扩展的例子:https://docs.racket-lang.org/guide/hash-reader.html

The Guide has an example of how to use reader extensions: https://docs.racket-lang.org/guide/hash-reader.html

这篇关于miniKanren:如何定义#s 和#u?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆