题
维基百科上的文章 延续 说:
"在任何语言支持 关闭, 它可以编写程序继续通过风格 手动实施叫/cc."
要么这是真的,我需要知道如何做到这一点,或者这不是真的,并声明需要加以纠正。
如果这是真的,请告诉我如何实现电话/cc在Lua因为我看不到如何。
我认为我能够实施呼/cc手动如果Lua有协程.克隆功能为解释 在这里,.
如果关闭是不够实施呼/cc然后还有什么需要?
下面的文字是可选择阅读。
P.S.:Lua有一个射击的延续,与其协程表。协程.克隆功能将允许我的克隆它叫它多次,因此有效地制作电话/cc可能的(除非我误会呼/cc)。但是,克隆功能并不存在Lua。有人在Lua因诺琴蒂研究中心的通道建议使用冥王星图书馆(它实现化)调动一个协程,复制,然后解组和它再次使用它。虽然这可能会工作,我更感兴趣的理论执行情况的呼/cc和在寻找什么是实际的最低限度的特点,语言的需要,以便其手册的执行情况。
编辑1: 确定的人,帮助我离开这里,这花了我很长时间,因为我不知道任何方案,但是我想出来的东西,应该帮助我们。请看看在代码如下。第一个是一个程序,在方案,第二个是同样的程序,但在Lua。
希望这将帮助我们。我相信我们 非常 关闭。
P.S.:这些例子是采取从第一个例子上 维基百科上的文章CallCC.
方案的版本
(define call/cc call-with-current-continuation)
; callcc CPS-transformed (thanks to the people from the #scheme channel at freenode.net)
(define cpscallcc
(lambda (consumer k)
(let ((cc (lambda (result) (k result))))
(consumer cc k))))
; this is the continuation we will use to display the "returned" values
(define main-continuation
(lambda (result)
(display "--> ")
(display result)
(newline)))
; define f function non-CPS
(define (f return)
(return 2)
3)
; these are my past attempts at defining a CPS f function
;(define (cps-f return k)
; (k (return 2)) 3)
;(define (cps-f return k)
; (k (lambda ()
; (return 2)
; 3)))
; this is what I came up with - I'm not sure if this is correctly CPS-transformed but I believe so
(define (cps-f return k)
(return 2)
(k 3))
; call the non-CPS f function
(display (f (lambda (x) x))) ; displays 3
(newline)
; call the non-CPS f function with call/cc (I don't understand what this does)
(display (call/cc f)) ; displays 2
(newline)
; now call the CPS version of the f function
(cps-f (lambda (x) x) main-continuation) ; displays --> 3
; now call the CPS version of the f function with the CPS version of call/cc
(cpscallcc cps-f main-continuation) ; displays --> 2 but then it also displays --> 3 afterwards -> I'm not sure why it displays the 3 afterwards, as it should only display the 2 just like the non-CPS versions above
Lua版本
-- callcc CPS-version
cpscallcc = function(consumer, k)
local cc = function(result)
return k(result) -- ?or k(result)
end
return consumer(cc, k) -- ?or return consumer(cc,k)
end
-- define f function non-CPS
f = function(ret)
ret(2)
return 3
end
-- define f function CPS-version (again, not sure this is correct)
cps_f = function(ret, k)
ret(2)
k(3)
end
-- call the non-CPS f function
print(f(function(x) return x end))
-- we cant call the non-CPS f function with callcc because
-- Lua doesnt have callcc, but the line below displays the correct expected output (maybe by accident)
--cpscallcc(f, print)
-- now call the CPS version of the f function
cps_f( function(x) return x end, print ) -- displays 3
; now call the CPS version of the f function with the CPS version of call/cc
cpscallcc( cps_f, print) -- displays 2 and then 3 just like the Scheme version!!
-- so apparently the translation from Scheme to Lua is correct...
我使用的DrScheme和Lua Windows-任何人都想要帮助起来了这是两个容易下载和安装工具,只要工作。
解决方案
有两个先决条件手实施叫/cc每维基百科的报价:
- 语言必须支持封锁
- 你必须写你的节目在继续通过风格(CPS)
我怀疑你不会喜欢#2.
写程序继续通过风格:
- 每个功能必须采取一个持续的论点
- 职能必须返回的呼吁他们继续
因此,使用 k
如名称的继续参数的一个函数就是这样的:
function multiplyadd(k, x, y, z) return k(x * y + z) end
顶层可能使用 print
作为其延续,所以调用 multiplyadd
在高级别会看起来像:
multiplyadd(print, 2, 4, 1)
与,脚手架我们可以定义呼/cc作
function callcc(k,f) return f(k,k) end
注意,上面的 multiplyadd
实际上由于秘籍 *
和 +
是不是在CPS。这是非常繁琐,增加所有的运营商在CPS形式,替换所有的Lua图书馆的功能与CPS等同物,并将其翻译/产生的所有代码CPS;看看 详细信息在这里.
其他提示
我想你忘记的一部分有关编写程序继续通过 风格。一旦你这么做,叫/cc是微不足道(在Lua或在任何其他语言), 作为继续将一个明确的参数的所有功能(电话/cc 包括在内)。
PS:除了封锁,还需要适当的尾呼吁程序中继续 通过风格。
回答这个问题有关的计划呼/cc在Lua:有没有计划呼/cc在Lua。捕获的延续是不是太昂贵,或者需要一些代码析远远超出了什么Lua编译器可以做的。还有的问题,Lua的延续,可能包括部分在C。
与协同程序,但是,我们已经能够实施呼/cc1在Lua(one-shot的延续).这是不够好,为许多采用的延续。
关键词是
它能够实施程序 继续通过风格
(着重号是我的。) 你这样做是通过把定期的"直接的风格"的程序,并将他们来延续传递式(CPS)通过程序转换被称为 CPS变换.关键的是,CPS变换的 call/cc
是一个简单的功能。
这是不切实际的程序员。 CPS转换有两个用途:
- 作为一个理论上的想法,对学习语言的特征,特别是控制运营商
- 作为一个通过在一个编译器,使用CPS作为中间体的语言
你不想去任何地方附近做CPS将在Lua代码,特别是不通过手。
这是可能的:稿到Lua编译器 https://github.com/roblox-ts/roblox-ts + 呼叫-cc在JS https://github.com/zaoqi/callcc.js/blob/master/callcc.js
这是我的cps-转换方案,只是通过它的每一个功能你想转换。
(define (cps-convert function . functions)
# Since "help" is called at 2 different places...
(define (help) (error "syntax: (cps-convert f1 f2 ...)"))
# Single function converter
(define (convert func)
# "name" contains the function's name prefixed with "cps-"
(let ([name (string->symbol
(string-append "cps-" (symbol->string func)))])
# Dirty hack to define "cps-*" in the global environment
`(eval '(begin
# Necessary to prevent the function from being evaluated
(define ,name #f)
# Magic
(set! ,name (lambda (k . args) (k (func args)))))
# Global environment
(interaction-environment))))
# Prerequisite... Call help if condition not met
(if (symbol? function)
# function is a symbol
(cond
# If there is only one function to convert
[(null? functions) (convert function)]
# Else ensure every other "functions" are symbols and convert each
[(every symbol? functions) (apply convert function functions)]
# Oops! Condition not met!
[else (help)])
# Else clause from previous "if"
(help)))