执行类型推断的代码

发布于 2024-09-13 08:02:20 字数 259 浏览 9 评论 0原文

我正在研究一种具有全局多态类型推断的实验性编程语言。

我最近让该算法运行得足够好,可以正确输入我扔给它的示例代码。我现在正在寻找更复杂的东西来练习边缘情况。

谁能指出我可以用于此目的的非常粗糙和可怕的代码片段的来源?我确信函数式编程世界里有很多这样的东西。我特别寻找用函数递归做坏事的例子,因为我需要检查以确保函数扩展正确终止,但一切都很好——我需要构建一个测试套件。有什么建议吗?

我的语言很大程度上是命令式的,但任何 ML 风格的代码都应该很容易转换。

I'm working on an experimental programming language that has global polymorphic type inference.

I recently got the algorithm working sufficiently well to correctly type the bits of sample code I'm throwing at it. I'm now looking for something more complex that will exercise the edge cases.

Can anyone point me at a source of really gnarly and horrible code fragments that I can use for this? I'm sure the functional programming world has plenty. I'm particularly looking for examples that do evil things with function recursion, as I need to check to make sure that function expansion terminates correctly, but anything's good --- I need to build a test suite. Any suggestions?

My language is largely imperative, but any ML-style code ought to be easy to convert.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静水深流 2024-09-20 08:02:20

我的总体策略实际上是从相反的方向接近它——确保它拒绝不正确的东西!

也就是说,这里是我通常使用的一些标准“确认”测试:

急切的固定点组合器(无耻地从 此处):

datatype 'a t = T of 'a t -> 'a

val y = fn f => (fn (T x) => (f (fn a => x (T x) a)))
               (T (fn (T x) => (f (fn a => x (T x) a))))

明显的相互递归:

fun f x = g (f x)
and g x = f (g x)

也请检查那些深度嵌套的 let 表达式:

val a = let
   val b = let 
      val c = let
         val d = let
            val e = let
               val f = let
                  val g = let
                     val h = fn x => x + 1
                  in h end
               in g end
            in f end
         in e end
      in d end
   in c end
in b end

深度嵌套的高阶函数!

fun f g h i j k l m n = 
   fn x => fn y => fn z => x o g o h o i o j o k o l o m o n o x o y o z

我不知道是否必须有值限制才能合并可变引用。如果是这样,看看会发生什么:

fun map' f [] = []
  | map' f (h::t) = f h :: map' f t

fun rev' [] = []
  | rev' (h::t) = rev' t @ [h]

val x = map' rev'

您可能需要以标准方式实现 maprev :)

然后使用实际引用(从 此处):

val stack =
let val stk = ref [] in
  {push = fn x => stk := x :: !stk,
   pop  = fn () => stk := tl (!stk),
   top  = fn () => hd (!stk)}
end

希望这些能在某种程度上有所帮助。确保尝试构建一组可以以某种自动方式重新运行的回归测试,以确保所有类型推断在您所做的所有更改中都能正确运行:)

My general strategy is actually to approach it from the opposite direction -- ensure that it rejects incorrect things!

That said, here are some standard "confirmation" tests I usually use:

The eager fix point combinator (unashamedly stolen from here):

datatype 'a t = T of 'a t -> 'a

val y = fn f => (fn (T x) => (f (fn a => x (T x) a)))
               (T (fn (T x) => (f (fn a => x (T x) a))))

Obvious mutual recursion:

fun f x = g (f x)
and g x = f (g x)

Check out those deeply nested let expressions too:

val a = let
   val b = let 
      val c = let
         val d = let
            val e = let
               val f = let
                  val g = let
                     val h = fn x => x + 1
                  in h end
               in g end
            in f end
         in e end
      in d end
   in c end
in b end

Deeply nested higher order functions!

fun f g h i j k l m n = 
   fn x => fn y => fn z => x o g o h o i o j o k o l o m o n o x o y o z

I don't know if you have to have the value restriction in order to incorporate mutable references. If so, see what happens:

fun map' f [] = []
  | map' f (h::t) = f h :: map' f t

fun rev' [] = []
  | rev' (h::t) = rev' t @ [h]

val x = map' rev'

You might need to implement map and rev in the standard way :)

Then with actual references lying around (stolen from here):

val stack =
let val stk = ref [] in
  {push = fn x => stk := x :: !stk,
   pop  = fn () => stk := tl (!stk),
   top  = fn () => hd (!stk)}
end

Hope these help in some way. Make sure to try to build a set of regression tests you can re-run in some automatic fashion to ensure that all of your type inference behaves correctly through all changes you make :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文