首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在TAPL的OCaml实现中,模块、异常、FI和未知是什么?

在TAPL的OCaml实现中,模块、异常、FI和未知是什么?
EN

Stack Overflow用户
提问于 2020-12-21 09:45:41
回答 1查看 75关注 0票数 0

我正在阅读类型和编程语言 (https://www.cis.upenn.edu/~bcpierce/tapl/)一书。

在第四章中,是算术表达式的ML实现,介绍了info.我下载了它的ocaml源代码arith.tar.gz在这里:https://www.cis.upenn.edu/~bcpierce/tapl/checkers/

这里是support.ml的开始

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
#use "topfind";;
#thread;;
#require "core.top";;

open Base;;
open Core;;

它给了我提醒:

代码语言:javascript
运行
复制
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.

图书馆FormatBase.Format是什么?我现在还需要打开吗?

Q2

module Error = struct卡在utop解释器中。这条线是什么意思?为什么卡在utop里?

Q3

exception Exit of int是什么意思?

Q4

FIUNKNOWNtype info = FI of string * int * int | UNKNOWN中是什么?

EN

回答 1

Stack Overflow用户

发布于 2021-01-27 16:41:06

Q1:您应该使用Base.Caml.Format (或Core.Caml.Format)而不是Base.Format (或者只是删除基/核心)。

Q2:我认为代码和缩进是错误的,这就是给您带来麻烦的原因,通常您定义的模块如下:

代码语言:javascript
运行
复制
module M = struct
  type t = ...
  let f = ...
end

基本上,struct是指模块内容的开头。utop希望您键入接下来的内容。

Q3:它声明一个名为Exit的异常,它附带一个int,例如:

代码语言:javascript
运行
复制
exception Exit of int

let f () = raise (Exit 1)

let () =
  try
    f ()
  with
  | Exit n -> Format.printf "exit %d@." n

Q4:它是一个和类型,由两个“case”组成,FI和UNKNOWN,第一个带有字符串和两个ints使用示例:

代码语言:javascript
运行
复制
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 UNKNOWN
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65390535

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档