为什么纯粹的函数式语言不使用引用计数? [英] Why don't purely functional languages use reference counting?

本文介绍了为什么纯粹的函数式语言不使用引用计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在纯功能语言中,数据是不可变的。通过引用计数,创建参考周期需要更改已创建的数据。看起来纯功能语言可以使用引用计数而不用担心循环的可能性。我是对的?如果是这样,为什么他们不?
$ b我知道引用计数在很多情况下比GC慢,但至少减少了停顿时间。在暂停时间不好的情况下,可以选择使用引用计数。

一个错误的假设。拥有循环引用和不可变数据是完全可能的。考虑使用不可变数据创建循环引用的以下C#示例。

  class节点{
public readonly节点其他;
public Node(){
other = new Node(this);
}
公共节点(节点节点){
other = node;






$ b

这种类型的技巧可以用许多功能语言因此任何收集机制都必须处理循环引用的可能性。我并不是说循环引用不可能有一个参考计数机制,只是必须处理它。



通过 ephemient 进行编辑



回应评论...这在Haskell中是微不足道的

$ p $ data $ node Node a = Node {other: :Node a}
recursiveNode = Node {other = recursiveNode}

几乎没有更多的努力在SML中。

 数据类型'a node =单元的NODE  - > '节点
val recursiveNode:unit node =
let mkRecursiveNode()= NODE mkRecursiveNode
in mkRecursiveNode()end

不需要突变。


In purely functional languages, data is immutable. With reference counting, creating a reference cycle requires changing already created data. It seems like purely functional languages could use reference counting without worrying about the possibility of cycles. Am is right? If so, why don't they?

I understand that reference counting is slower than GC in many cases, but at least it reduces pause times. It would be nice to have the option to use reference counting in cases where pause times are bad.

解决方案

Your question is based on a faulty assumption. It's perfectly possible to have circular references and immutable data. Consider the following C# example which uses immutable data to create a circular reference.

class Node { 
  public readonly Node other;
  public Node() { 
    other = new Node(this);
  }
  public Node(Node node) {
    other = node;
  }
}

This type of trick can be done in many functional languages and hence any collection mechanism must deal with the possibility of circular references. I'm not saying a ref counting mechanism is impossible with a circular reference, just that it must be dealt with.

Edit by ephemient

In response to the comment... this is trivial in Haskell

data Node a = Node { other :: Node a }
recursiveNode = Node { other = recursiveNode }

and barely any more effort in SML.

datatype 'a node = NODE of unit -> 'a node
val recursiveNode : unit node =
    let fun mkRecursiveNode () = NODE mkRecursiveNode
    in mkRecursiveNode () end

No mutation required.

这篇关于为什么纯粹的函数式语言不使用引用计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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