我正在阅读类型和编程语言 (https://www.cis.upenn.edu/~bcpierce/tapl/)一书。
在第四章中,是算术表达式的ML实现,介绍了info.我下载了它的ocaml源代码arith.tar.gz在这里:https://www.cis.upenn.edu/~bcpierce/tapl/checkers/。
这里是support.ml的开始
open Format
module Error = struct
exception Exit of int
type info = FI of string * int * int | UNKNOWN我有几个问题:
Q1
我在MacOS上用最新的自制软件安装了utop(版本2.6.0),安装了带有opam install core base的库。这是我的.ocamlinit
#use "topfind";;
#thread;;
#require "core.top";;
open Base;;
open Core;;它给了我提醒:
utop # open Format;;
Line 1, characters 5-11:
Alert deprecated: module Base.Format
[2016-09] this element comes from the stdlib distributed with OCaml.
[Base] doesn't export a [Format] module, although the
[Caml.Format.formatter] type is available (as [Formatter.t])
for interaction with other libraries.
utop # open Base.Format;;
Line 1, characters 5-16:
Alert deprecated: module Base.Format
[2016-09] this element comes from the stdlib distributed with OCaml.
[Base] doesn't export a [Format] module, although the
[Caml.Format.formatter] type is available (as [Formatter.t])
for interaction with other libraries.图书馆Format和Base.Format是什么?我现在还需要打开吗?
Q2
module Error = struct卡在utop解释器中。这条线是什么意思?为什么卡在utop里?
Q3
exception Exit of int是什么意思?
Q4
FI和UNKNOWN在type info = FI of string * int * int | UNKNOWN中是什么?
发布于 2021-01-27 16:41:06
Q1:您应该使用Base.Caml.Format (或Core.Caml.Format)而不是Base.Format (或者只是删除基/核心)。
Q2:我认为代码和缩进是错误的,这就是给您带来麻烦的原因,通常您定义的模块如下:
module M = struct
type t = ...
let f = ...
end基本上,struct是指模块内容的开头。utop希望您键入接下来的内容。
Q3:它声明一个名为Exit的异常,它附带一个int,例如:
exception Exit of int
let f () = raise (Exit 1)
let () =
try
f ()
with
| Exit n -> Format.printf "exit %d@." nQ4:它是一个和类型,由两个“case”组成,FI和UNKNOWN,第一个带有字符串和两个ints使用示例:
type info =
| FI of string * int * int
| UNKNOWN
let print x =
match x with
| FI (s, i1, i2) -> Format.printf "FI (%s, %d, %d)" s i1 i2
| UNKNOWN -> Format.printf "UNKNOWN"
let () =
print (FI ("hello", 1, 2));
print UNKNOWNhttps://stackoverflow.com/questions/65390535
复制相似问题