递归绑定中的SML语法限制?

时间:2014-09-16 00:17:40

标签: recursion sml

在SML的递归绑定中似乎存在语法限制,我无法理解。在第二种情况下我没有遇到这些限制(参见下面的来源),在第一种情况下使用自定义运算符时遇到了什么?

以下是我遇到问题的情况。当我想使用自定义运算符时,它会失败,如注释中所述。在我测试SML源的主要SML实现中,只有Poly / ML接受它是有效的,并且所有MLton,MLKit和HaMLet都拒绝它。

错误消息让我感到困惑。我眼中最清楚的是来自HaMLet的那个,它抱怨“在递归值绑定中非法表达”。

(* A datatype to pass recursion as result and to arguments. *)

datatype 'a process = Chain of ('a -> 'a process)

(* A controlling iterator, where the item handler is
 * of type `'a -> 'a process`, so that while handling an item,
 * it's also able to return the next handler to be used, making
 * the handler less passive. *)

val rec iter =
   fn process: int -> int process =>
   fn first: int =>
   fn last: int =>
      let
         val rec step =
            fn (i: int, Chain process) (* -> unit *) =>
               if i < first then ()
               else if i = last then (process i; ())
               else if i > last then ()
               else
                  let val Chain process = process i
                  in step (i + 1, Chain process)
                  end
      in step (first, Chain process)
      end

(* An attempt to set‑up a syntax to make use of the `Chain` constructor,
 * a bit more convenient and readable. *)

val chain: unit * ('a -> 'a process) -> 'a process =
   fn (a, b) => (a; Chain b)

infixr 0 THEN
val op THEN = chain

(* A test of this syntax:
 *   - OK with Poly/ML, which displays “0-2|4-6|8-10|12-14|16-18|20”.
 *   - fails with MLton, which complains about a syntax error on line #44.
 *   - fails with ML Kit, which complains about a syntax error on line #51.
 *   - fails with HaMLet, which complains about a syntax error on line #45.
 * The clearest (while not helpful to me) message comes from HaMLet, which
 * says “illegal expression within recursive value binding”. *)

val rec process: int -> int process =
  (fn x => print (Int.toString x) THEN
  (fn x => print "-"              THEN
  (fn x => print (Int.toString x) THEN
  (fn x => print "|"              THEN
   process))))

val () = iter process 0 20
val () = print "\n"

(* Here is the same without the `THEN` operator. This one works with
 * all of Poly/ML, MLton, ML Kit and HaMLet. *)

val rec process =
   fn x =>
     (print (Int.toString x);
      Chain (fn x => (print "-";
      Chain (fn x => (print (Int.toString x);
      Chain (fn x => (print "|";
      Chain process)))))))

val () = iter process 0 20
val () = print "\n"

(* SML implementations version notes:
 *   - MLton, is the last version, built just yesterday
 *   - Poly/ML is Poly/ML 5.5.2
 *   - ML Kit is MLKit 4.3.7
 *   - HaMLet is HaMLet 2.0.0 *)

更新

我可以解决这个问题,但仍然不理解。如果我删除最外面的括号,则验证:

val rec process: int -> int process =
   fn x => print (Int.toString x) THEN
  (fn x => print "-"              THEN
  (fn x => print (Int.toString x) THEN
  (fn x => print "|"              THEN
   process)))

而不是:

val rec process: int -> int process =
  (fn x => print (Int.toString x) THEN
  (fn x => print "-"              THEN
  (fn x => print (Int.toString x) THEN
  (fn x => print "|"              THEN
   process))))

但为什么会这样呢?一个SML语法微妙吗?它的理性是什么?

1 个答案:

答案 0 :(得分:2)

这只是语言定义中一个过度限制的句子,它说:

  

对于rec中的每个值“ pat = exp ”, exp 的格式必须为“fn match ”。

严格地说,这不允许任何括号。在实践中,这很少是一个问题,因为你几乎总是使用fun声明语法。