gpt4 book ai didi

ocaml - 为什么我可以添加任意数量的 ;到底在OCaml顶层就行了?

转载 作者:行者123 更新时间:2023-12-01 12:10:55 25 4
gpt4 key购买 nike

我是 OCaml 的新手,我想知道这个语句是如何工作的:

# 1 + 1 ;;;;
- : int = 2
# 1 + 1 ;;;;;;;;
- : int = 2

;;;;视为合并 1+1blank(((1 + 1);););; ?或者是 ;;;;以其他方式被视为多字符?

最佳答案

discussed here , ;;在顶级 REPL 中忽略它之后的所有内容。将代码写入文件并编译它们时,不需要双分号。引用 Daniil Baturin 的 What I wish I knew when learning OCaml ,

The answer to the question “When do I need the ;; within OCaml source code?” is never.

It's not a part of the language and is only used by the interpreter as an end of input mark.



revised syntax Ocaml 的摆脱 ;;有以下论点:

The double semicolon in OCaml exists for historical reasons: the first parsers were driven by the tokens, not by the rules: all constructions needed to have a specific token.

But because of the introduction of modules in OCaml, the double semicolon, which was mandatory in Caml Light to end sentences, became optional: the reason is that in OCaml, a 'phrase' and a 'structure item' are actually the same notion. The problem is that the double semicolon is associated with the idea of 'terminating' something: for a phrase, it is exact, but not for a structure item inside a structure, since other structure items and the keyword 'end' follow.

That choice of letting the double semicolon be optional in normal syntax has introduced several problems:

  • A structure item is actually ended by the beginning of the next structure item; it means that all structure items must start with a keyword; otherwise there is an ambiguity. For example, you cannot write:

    print_string "hello, world"
    print_newline ()

    because it is interpreted as a call to print_string with 3 parameters (and typing error). The advocated solution is to write:

    let _ = print_string "hello, world"
    let _ = print_newline ()

    Mmm....

  • But this solution does not work interactively: in the toplevel, you cannot ask people to type the beginning of the next sentence to see the result of the current one. Therefore the double semicolon still remains! The property that we write in the toplevel like in source files has been lost.

  • In structures and objects, the fact that you don't end the structure items and object items make the programs more difficult to read. If you write a short object or structure item in one only line, it is very difficult to see where the items start and end.

My opinion is that the structure items should end with a token in a context where there is never need to read another token. This ensures a correct behavior in the interactive toplevel. The fact that the sequence is closed, in the revised syntax, frees the simple semicolon. And a simple semicolon is perfectly acceptable inside structures and objects, to end their item, the same way they close a record item. In the revised syntax, this ending semicolon is mandatory.

It is easier to treat a language whose all phrases end with a token: at end of the sentences, the characters and the tokens streams are synchronized (no need to read an extra token to be sure that the phrase is ended). This property can bring simplifications in other treatments (extraction of comments or code for documentation, indentation, editors modes, interactive tools).

[...]

关于ocaml - 为什么我可以添加任意数量的 ;到底在OCaml顶层就行了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51815342/

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