在逻辑编程中,无用的是什么? [英] In logic programming, what is unnesting for?

查看:135
本文介绍了在逻辑编程中,无用的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

理性计划者"描述了如何使用miniKanren,它类似于Prolog,但是是类似Lisp的语言的库.这本书的第一条诫命"是这样的:

The Reasoned Schemer describes how to use miniKanren, which resembles Prolog but is a library for Lisp-like languages. The book's "First Commandment" is this:

转换值为布尔值的函数 转化为以价值为目标的函数,替换cond 与conde和unsest每个问题和答案. 通过将#s替换为#s,取消对#t(或#f)答案的嵌套 (或#u).

To transform a function whose value is a Boolean into a function whose value is a goal, replace cond with conde and unnest each question and answer. Unnest the answer #t (or #f) by replacing it with #s (or #u).

除非通过几个大致等效的示例,否则它们实际上并没有定义嵌套.最清楚的是:取消嵌套会将您从(list? (cdr l))转到

They don't really define unnesting, except through a few roughly equivalent example. The clearest is this: unnesting takes you from (list? (cdr l)) to

(fresh (d)
       (cdro l d)
       (listo d))

我不明白为什么需要嵌套.例如,对于上述目标,为什么tp write (listo (cdr l))还不够?

I don't understand why unnesting is needed. For instance, for the above goal, why is it not sufficient tp write (listo (cdr l))?

此处所述,我运行了然后定义了一些缺失的部分.

As described here, I ran raco pkg install minikanren and then defined a few missing pieces.

以下是listo的定义及其使用的所有内容,除了minikanren库或Racket的前奏中定义的内容.

Here are the definitions of listo and everything it uses it, except for things defined in the minikanren library or in Racket's prelude.

(define listo
  (lambda (l)
    (conde
     ((nullo l) #s)
     ((pairo l)
      (fresh (d)
             (cdro l d)
             (listo d)))
     (else #u))))

(define nullo
  (lambda (x)
    (== x '())))

(define pairo
  (lambda (p)
    (fresh (a d)
           (conso a d p))))

(define cdro
  (lambda (p d)
    (fresh (a)
           (== (cons a d) p))))

(define conso
  (lambda (head tail result)
    (== (cons head tail) result)))

推荐答案

术语un-nesting表示修改结构的层次结构或删除方括号.

the term un-nesting means to modify the hierarchy of the structure - or to remove brackets.

刚发现这本书 The Reasoned Schemer 和相关的GitHub 页面,该页面也对此进行了定义:

just found the book The Reasoned Schemer and the associated GitHub page, which also defines it:

(car (cdr l))转换为(cdro l v)(caro v r)的过程称为取消嵌套….认识到嵌套和[CPS]之间的相似性.

Process of transforming (car (cdr l)) into (cdro l v) and (caro v r) is called unnesting… . Recognize the similarity between unnesting and [CPS].

这篇关于在逻辑编程中,无用的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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