来自结构的标准 ML 导出运算符作为中缀

Standard ML export operator from structure as infix

我想在结构 a 内声明一个中缀运算符以供在结构外使用。但是即使打开了结构,我似乎也无法在结构外识别 "infixness" 。这是一个使用 Poly/ML:

的例子
> structure A = struct infix 6 ++ fun a ++ b = a + b end;
structure A: sig val ++: int * int -> int end
> 1 A.++ 2;
poly: : error: Type error in function application.
   Function: 1 : int
   Argument: A.++ : int * int -> int
   Reason: Value being applied does not have a function type
Found near 1 A.++ 2
Static Errors
> let open A in 1 ++ 2 end;
poly: : error: Type error in function application.
   Function: 1 : int
   Argument: ++ : int * int -> int
   Reason: Value being applied does not have a function type
Found near let open A in 1 ++ 2 end
Static Errors

这是标准 ML 的限制吗?

是的,标准 ML 不支持此功能。每次 open 该结构时,您都必须重新声明固定性和优先级(可选)。解决它的一种方法是全局声明固定性,即在任何结构之外,但这在单独的编译中没有得到很好的支持,而且它也不是很模块化。您可以在 MLton's InfixingOperators page.

上阅读更多相关信息和可能的解决方法

对于我自己的项目,我在我的文本编辑器中定义了一个快捷方式,它将扩展为 open 声明和固定声明。

此外,作为个人风格指南,我并不是在声明优先顺序。如果我需要将多个中缀运算符混合到同一个表达式中,我宁愿显式地使用括号。眼睛解析标识符作为中缀很容易,解析优先级则不然。