MonadFix使用严格的语言 [英] MonadFix in strict language

查看:67
本文介绍了MonadFix使用严格的语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Ocaml中的类似haskell的do表示法的camlp4扩展进行研究,并试图弄清楚GHC如何编译递归do-bindings(通过-XDoRec启用).
我想知道单峰定点组合器是否可能以严格的语言(例如Ocaml/F#/SML/...)存在?
如果是,它看起来如何?会很有用吗?

I'm working on camlp4 extension for haskell-like do notation in Ocaml, and trying to figure out how GHC compiles recursive do-bindings (enabled with -XDoRec).
I wonder if it possible for monadic fixpoint combinator to exist in strict language (like Ocaml/F#/SML/...)?
If yes, how can it look like? Would it be very useful?

推荐答案

F#计算表达式语法(与Haskell do相关)支持递归:

The F# computation expression syntax (related to Haskell do) supports recursion:

let rec ones = seq {
  yield 1
  yield! ones }

之所以支持此功能,是因为计算生成器除了要支持其他monadic(或 MonadPlus )操作之外,还必须支持Delay操作.该代码被翻译为类似的内容:

This is supported because the computation builder has to support Delay operation in addition to other monadic (or MonadPlus) operations. The code is translated to something like:

let rec ones = 
  seq.Combine
    ( seq.Yield(1),
      seq.Delay(fun () -> seq.YieldFrom(ones)) )

Delay的类型通常为(unit -> M<'T>) -> M<'T>,其窍门是将具有效果的计算(或立即递归引用)包装到按需评估的延迟计算中.

The type of Delay is, in general, (unit -> M<'T>) -> M<'T> and the trick is that it wraps a computation with effects (or immediate recursive reference) into a delayed computation that is evaluated on demand.

如果您想了解有关该机制如何在F#中工作的更多信息,那么以下两篇文章是相关的:

If you want to learn more about how the mechanism works in F#, then the following two papers are relevant:

  • Syntax Matters: Writing abstract computations in F#
  • Initializing Mutually Referential Abstract Objects: The Value Recursion Challenge

第一个描述如何消除F#计算表达式的语法(以及如何插入Delay-通常,F#如何将延迟和急切的计算与效果结合在一起),第二个描述F#如何处理let rec声明带有值的值-类似于上面的ones值.

The first one describes how the F# computation expression syntax is desugared (and how Delay is inserted - and in general, how F# combines delayed and eager computations with effects) and the second one describes how F# handles let rec declarations with values - like the ones value above.

这篇关于MonadFix使用严格的语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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