What is module, exception, FI and UNKNOWN in TAPL's OCaml implementation?

101 Views Asked by At

I'm reading book Types and Programming Languages (https://www.cis.upenn.edu/~bcpierce/tapl/).

In it's chapter-4, An ML Implementation of Arithmetic Expressions, it introduced the info. I downloaded it's ocaml source code arith.tar.gz here: https://www.cis.upenn.edu/~bcpierce/tapl/checkers/.

Here's the start of support.ml:

open Format

module Error = struct

exception Exit of int

type info = FI of string * int * int | UNKNOWN

I have several questions:

Q1

I installed utop(version 2.6.0) with latest homebrew on MacOS, installed libraries with opam install core base. Here's my .ocamlinit:

#use "topfind";;
#thread;;
#require "core.top";;

open Base;;
open Core;;

it gives me alerts:

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.

What's library Format and Base.Format ? Do I still need to open them now ?

Q2

module Error = struct stuck in utop interpreter. What does this line mean ? Why it's been stuck in utop?

Q3

What does exception Exit of int mean ?

Q4

What is FI and UNKNOWN in type info = FI of string * int * int | UNKNOWN ?

1

There are 1 best solutions below

0
yehudi On

Q1: you should use Base.Caml.Format (or Core.Caml.Format) instead of Base.Format (or just drop base/core).

Q2: I think the code and the indentation is wrong and this is what is causing you troubles, usually you define a module like this:

module M = struct
  type t = ...
  let f = ...
end

So basically, struct is to denote the beginning of the module content. utop expects you to type what comes next.

Q3: it's to declare an exception named Exit which comes with an int, exemple of use is :

exception Exit of int

let f () = raise (Exit 1)

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

Q4: it's a sum type composed of two "cases", FI and UNKNOWN, the first one coming with a string and two ints example of use:

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