gpt4 book ai didi

programming-languages - 动态语言的智能感知

转载 作者:行者123 更新时间:2023-12-04 17:59:13 24 4
gpt4 key购买 nike

我正在寻找各种方法来支持动态类型语言的某种级别的智能感知。由于智能感知信息基于类型信息,因此在动态语言中实现这一点存在固有的困难。

你知道实现它的任何算法或方法吗?

最佳答案

您需要编写一个抽象解释器来执行带有类型值的代码。因此,您可以通过 AST 使用抽象解释器,并为每个变量记录发送的消息或已知类型。完成后,您可以使用结构类型等价(又名鸭子类型)推断可能的类型。

PS: in addition to type inference you might want to take a look at "How Program History Can Improve Code Completion" by Romain Robbes, is explains how to further improve auto completion in dynamic languages with most-recently-used information and collaborative filtering.



所以这里是抽象解释如何适用于像这样的代码片段
def groups(array,&block)
groups = Hash.new
array.each { |ea|
key = block.call(ea)
groups[key] = [] unless groups.include? key
groups[key] << ea
}
return groups
end

你会从
array = { :messages => [], :types => [] }
block = { :messages => [], :types => [] }

进而
array = { :messages => [], :types => [] }
block = { :messages => [], :types => [] }
groups = { :messages => [], :types => [Hash] }

进而
array = { :messages => [:each], :types => [] }
block = { :messages => [], :types => [] }
groups = { :messages => [], :types => [Hash] }

进而
array = { :messages => [:each], :types => [] }
block = { :messages => [:call], :types => [] }
groups = { :messages => [], :types => [Hash] }
key = { :messages => [], :types => [] }

进而
array = { :messages => [:each], :types => [] }
block = { :messages => [:call], :types => [] }
groups = { :messages => [:include?,:[]], :types => [Hash] }
group_elements = { :messages => [], :types => [Array] }
key = { :messages => [], :types => [] }

进而
array = { :messages => [:each], :types => [] }
block = { :messages => [:call], :types => [] }
groups = { :messages => [:include?,:[]], :types => [Hash] }
group_elements = { :messages => [:<<], :types => [Array] }
key = { :messages => [], :types => [] }

所以最终我们可以推断出
  • array可能是 Enumerable
  • block可能是 Proc
  • groupsHashArray元素
  • key是任何对象
  • 关于programming-languages - 动态语言的智能感知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2020096/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com