我们如何证明continuation monad没有有效的实例MonadFix?
我在Visual Studio 2015中打开一些旧的VB.NET项目,当我编辑代码时,VS会更改语法:
它删除了连接中的"_":
'Before
myString = "ABC" & _
"DEF"
'After
myString = "ABC" &
"DEF"
Run Code Online (Sandbox Code Playgroud)
或者在之前添加一个空格!:
'Before
myDatatable.Rows(0)!myColumn
'After
myDatatable.Rows(0) !myColumn
Run Code Online (Sandbox Code Playgroud)
此语法与Visual Studio 2010或2013不兼容.
如何禁用此更改?
在阅读了Eric Lippert的回答之后,我得到了这样的印象,await并且call/cc几乎是同一枚硬币的两面,最多只有语法差异.然而,在尝试call/cc在C#5中实际实现时,我遇到了一个问题:要么我误解了call/cc(这是相当可能的),要么等待只是让人联想到call/cc.
考虑这样的伪代码:
function main:
foo();
print "Done"
function foo:
var result = call/cc(bar);
print "Result: " + result;
function bar(continuation):
print "Before"
continuation("stuff");
print "After"
Run Code Online (Sandbox Code Playgroud)
如果我对call/cc的理解是正确的,那么应该打印:
Before
Result: stuff
Done
Run Code Online (Sandbox Code Playgroud)
至关重要的是,当调用延续时,程序状态将与调用历史一起恢复,以便foo返回main并永远不会返回bar.
但是,如果await在C#中使用,则调用continuation 不会还原此调用历史记录.foo返回bar,并且没有办法(我可以看到)await可以用来使正确的调用历史记录成为延续的一部分.
请解释一下:我是否完全误解了操作call/cc,或者await只是不完全相同call/cc?
现在我知道了答案,我不得不说有充分的理由认为它们非常相似.考虑上面的程序在伪C#-5中的样子:
function main:
foo();
print "Done"
async function foo:
var result = …Run Code Online (Sandbox Code Playgroud) 在连续链中传播异常的正确方法是什么?
t.ContinueWith(t2 =>
{
if(t2.Exception != null)
throw t2.Exception;
/* Other async code. */
})
.ContinueWith(/*...*/);
t.ContinueWith(t2 =>
{
if(t2.IsFaulted)
throw t2.Exception;
/* Other async code. */
})
.ContinueWith(/*...*/);
t.ContinueWith(t2 =>
{
if(t2.Exception != null)
return t2;
/* Other async code. */
})
.ContinueWith(/*...*/);
t.ContinueWith(t2 =>
{
if(t2.IsFaulted)
return t2;
/* Other async code. */
})
.ContinueWith(/*...*/);
t.ContinueWith(t2 =>
{
t2.Wait();
/* Other async code. */
})
.ContinueWith(/*...*/);
t.ContinueWith(t2 =>
{
/* Other async code. */
}, TaskContinuationOptions.NotOnFaulted) // Don't …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用CPS来简化我的Python解释器中的控制流实现.具体来说,在实现return/ break/时continue,我必须手动存储状态和展开,这很乏味.我已经读过以这种方式实现异常处理非常棘手.我想要的是每个eval函数能够将控制流程引导到下一条指令或完全不同的指令.
一些比我更有经验的人建议将CPS作为一种正确处理这一问题的方法.我真的很喜欢它如何简化解释器中的控制流程,但我不确定为了实现这一点我需要做多少实际操作.
我需要在AST上运行CPS转换吗?我应该将这个AST降低到较小的较低级别的IR,然后进行转换吗?
我是否需要更新评估者以接受各地的成功延续?(我这样假设).
我想我通常理解CPS转换:目标是在整个AST中包含所有表达式的延续.
我也有点困惑Contmonad适合这里,因为宿主语言是Haskell.
编辑:这是AST的浓缩版本.它是Python语句,表达式和内置值的1-1映射.
data Statement
= Assignment Expression Expression
| Expression Expression
| Break
| While Expression [Statement]
data Expression
| Attribute Expression String
| Constant Value
data Value
= String String
| Int Integer
| None
Run Code Online (Sandbox Code Playgroud)
为了评估陈述,我使用eval:
eval (Assignment (Variable var) expr) = do
value <- evalExpr expr
updateSymbol var value
eval (Expression e) = do
_ <- evalExpr e
return () …Run Code Online (Sandbox Code Playgroud) 我已经成功地使用目标C中的标准NSAssert(condition_which_should_evaluate_true,@"错误消息")语句,并在断点导航器中添加"所有异常"断点,以编程方式指定条件时,使我的调试版本停止执行.
好又好,但是大多数时候我在调试时,我还希望在那之后继续正常的程序执行.在断言失败后继续执行该程序有助于追踪混淆/错误的来源.至少就我记得在不同平台上编程时的情况而言.
在Objective C开发中是否有标准的方法可以做到这一点?
请考虑ReStructuredText中的以下列表:
Broken list example
-------------------
#. First do spam
#. Then do ``eggs``
.. note::
Nobody expects the Spanish Inquisistion
#. The list restarts after the note
Run Code Online (Sandbox Code Playgroud)
在Sphinx中编译列表时,注释后的数字将重置为1:

任何想法如何在一note节后继续编号列表?
假设我有一个多行命令:
if 2>1 \
and 3>2:
print True
Run Code Online (Sandbox Code Playgroud)
在一个if块中,我可以通过使用括号来包装行来在其中一个条件旁边添加注释:
if (2>1 #my comment
and 3>2):
print True
Run Code Online (Sandbox Code Playgroud)
事实上,它与PEP 8指南的推荐方式一致:
包装长行的首选方法是在括号,括号和括号内使用Python隐含的行继续.通过将表达式包装在括号中,可以在多行中分割长行.这些应该优先使用反斜杠来继续行.
但是,有时您需要使用continuation.例如,long,多个with语句不能使用隐式延续.那么,如何在特定行旁边添加注释?这不起作用:
with open('a') as f1, #my comment\
open('b') as f2:
print True
Run Code Online (Sandbox Code Playgroud)
更一般地说,是否有一种在特定延续线旁边添加注释的通用方法?
我有一些简单的原始操作,例如:
在的情况下,操作单子:
import Control.Monad.Operational
type Process a = Program ProcessI a
data ProcessI a where
GetInput :: ProcessI String
Dump :: String -> ProcessI ()
getInput :: Process String
getInput = singleton GetInput
dump :: String -> Process ()
dump = singleton . Dump
Run Code Online (Sandbox Code Playgroud)
或者在免费 monad的情况下:
import Control.Monad.Free
type Process = Free ProcessF
data ProcessF a
= GetInput (String -> a)
| Dump String a
deriving (Functor)
getInput :: Process String
getInput = liftF $ GetInput id …Run Code Online (Sandbox Code Playgroud) 考虑函数的Haskell中的以下示例quux以及continuation monad和的定义callCC.
instance Monad (Cont r) where
return x = cont ($ x)
s >>= f = cont $ \c -> runCont s $ \x -> runCont (f x) c
callCC :: ((a -> Cont r b) -> Cont r a) -> Cont r a
callCC f = cont $ \h -> runCont (f (\a -> cont $ \_ -> h a)) h
quux :: Cont r Int
quux = callCC $ \k -> do
let …Run Code Online (Sandbox Code Playgroud) continuation ×10
haskell ×4
c# ×2
.net ×1
assertions ×1
breakpoints ×1
callcc ×1
exception ×1
monadfix ×1
monads ×1
objective-c ×1
operational ×1
python ×1
task ×1
vb.net ×1