SML(Poly)是否具有类似CL的REPL?

时间:2012-03-04 14:45:35

标签: common-lisp read-eval-print-loop ml

以下是Ron Garret的“在JPL上发表”的引用:

“调试在距离1亿英里的1亿美元硬件上运行的程序是一种有趣的体验。在航天器上运行读取 - 评估 - 打印循环在发现和解决问题方面证明是非常宝贵的。” p>

作为一个初学者试图决定在哪里跳,我倾向于ML,因为一位前教授赞不绝口,我发现很多书将Lambda微积分讨论与ML和ML相结合看起来相当理智。 (我最终会教这个。)

那么,ML是否有一个REPL,就像Lisp一样,你可以在运行时“添加更多代码”,即Garret先生的1亿美元硬件是否可以在ML上运行?

2 个答案:

答案 0 :(得分:5)

Poly / ML默认使用REPL启动,类似于SML / NJ。此外,您可以在运行时轻松调用编译器:它将为您生成本机代码,并以LISP中eval的方式将其添加到ML环境中,但它是静态类型和真实的机器代码,而不是解释为

除了官方的SML标准之外,结构PolyML.Compiler提供了各种(相对稳定的)接口。

以下是Poly / ML 5.5或5.6的工作示例:

fun eval text =
  let
    fun print s = (TextIO.output (TextIO.stdOut, s); TextIO.flushOut TextIO.stdOut);

    val line = ref 1;
    val in_buffer = ref (String.explode text);
    val out_buffer = ref ([]: string list);

    fun output () = String.concat (rev (! out_buffer));

    fun get () =
      (case ! in_buffer of
        [] => NONE
      | c :: cs => (in_buffer := cs; if c = #"\n" then line := ! line + 1 else (); SOME c));
    fun put s = out_buffer := s :: ! out_buffer;
    fun put_message {message = msg1, hard, location = {startLine = line, ...}, context} =
     (put (if hard then "Error: " else "Warning: ");
      PolyML.prettyPrint (put, 76) msg1;
      (case context of NONE => () | SOME msg2 => PolyML.prettyPrint (put, 76) msg2);
      put ("Line " ^ Int.toString line ^ "\n"));

    val parameters =
     [PolyML.Compiler.CPOutStream put,
      PolyML.Compiler.CPErrorMessageProc put_message,
      PolyML.Compiler.CPLineNo (fn () => ! line)];
    val _ =
      (while not (List.null (! in_buffer)) do
        PolyML.compiler (get, parameters) ())
      handle exn =>
        (put ("Exception- " ^ General.exnMessage exn ^ " raised");
          print (output ()); raise exn);
  in print (output ()) end;

现在我们可以在普通的Poly / ML REPL中调用它,如下所示:

> eval "1 + 1";
val it = 2: int
val it = (): unit
> eval "fun f 0 = 1 | f n = n * f (n - 1)";
val f = fn: int -> int
val it = (): unit
> eval "f 42";
val it = 1405006117752879898543142606244511569936384000000000: int
val it = (): unit
> f 42;
val it = 1405006117752879898543142606244511569936384000000000: int

这为静态类型的SML世界提供了LISP风格的元编程。

请注意,结构PolyML.Compiler还有其他选项来控制运行时编译器调用的行为。最好在polyml邮件列表中询问一下。

答案 1 :(得分:2)

PolyML有一个REPL。我不知道它的详细信息,但如果它与SML / NJ类似,那么当它运行时,你无法用它来运行正在运行的程序。如果你想这样做,Common Lisp或Squeak是你最好的选择 - 大多数其他编程语言社区都认为更新实时程序的想法是因为它运行不好(或者至少也是如此) - 默认情况下有危险的想法。

但是要学习标准ML。在我看来,它是规范的功能语言。理解它可以很容易地理解为什么函数式编程是强大的,并且还可以通过它们与它的偏差来帮助你理解整个函数式编程语言。