Specifications.Stdlib320
The following is the standard library that we will use for CAS CS 320: Concepts of Programming Languages at Boston University in the Fall 2024 semester (Note. This is the first time we're doing this, so it's something of an experiment).
The primary motivation for this library is to give a small subset of the OCaml Standard Library with a bit more documentation and more control on the end of the instructor to decide what functions are "allowed" for assignments. It is also minimally side-effectful.
Since nearly all functions are available in the OCaml Standard Library, you can take a look at Stdlib
documentation for more information on what you see below.
The parentheses in functions below indicate that the following functions are infix operators. This means we can, for example, write 11 + 13
instead of (+) 11 13
(though both are allowed). The associativity and precedence of all operators follow those of the OCaml Standard library. See this table for details.
Integer division, which rounds towards 0.
let _ =
let _ = assert (5 / 3 = 1) in
let _ = assert ((- 5) / 3 = -1) in
let _ = assert (5 / (- 3) = 1) in
()
Raises a Division_by_zero
exception if the second argument is 0 (Note. We won't be dealing with exceptions all that much in this course).
Integer remainder. It satisfies the properties:
x mod y = x mod (abs y)
(- x) mod y = - (x mod y)
And for positive integers it is the remainder of x / y
in the grade-school sense.
From the OCaml Standard Library: "OCaml's floating-point numbers follow the IEEE 754 standard, using double precision (64 bits) numbers. Floating-point operations never raise an exception on overflow, underflow, division by zero, etc. Instead, special IEEE numbers are returned as appropriate..." See this paragraph for more details.
Structural equality. This is ==
in most other languages. You should generally avoid using this for floating-point numbers.
Structural less-than. It behaves as expected on Boolean values (false < true
), characters (by integer value), string (lexicographically), etc.
Structural less-than-or-equal-to, i.e., x <= y
is equivalent to not (x > y)
Comparison, i.e., if x = y
then compare x y = 0
, if x > y
then compare x y > 0
, and if x < y
then compare x y < 0
Converts a floating-point number into an integer, rounding towards 0 (i.e., truncating).
Converts an ASCII code into a character.
Raises an Invalid_argument
exception if its argument is not a valid ASCII code.
Converts a string to a Boolean value. Raises a Failure
exception in the case the argument is not "true"
or "false"
.
Same as the previous function, but returns None
in the case of failure.
Converts a string to an integer. Raises a Failure
exception in the case the argument does not represent a integer.
Same as the previous function, but returns None
in the case of failure.
x |> f |> g
is equivalent to g (f x)
. This operator is useful for pipelining.
f @@ g @@ x
is equivalent to f (g x)
. This operator has low precedence, which is useful if you want to avoid parentheses, e.g., we can write abs @@ 2 + 3
instead of abs (2 + 3)
.
Prints a string followed by a newline character to standard output.
Reads the contents of a file given the filename. We will primarily be using this function.
Reads a line (upto a newline character) from standard input, where the output does not include a the newline character.
Generates a string that can be used as a fresh variable in the interpreters we implement in this course.
module Env : sig ... end
String concatenation, e.g., "hello" ^ "world" ^ "!" = "hello world!"
module Char : sig ... end
module String : sig ... end
List concatenation, e.g., [1;2;3] @ [4;5;6] @ [7;8;9] = [1;2;3;4;5;6;7;8;9]
module List : sig ... end
module Option : sig ... end
module Result : sig ... end
There are some functions we've included for completeness, others because we silently need them for other things to work, and others just in case (Note. Again, this is a new system we're trying out, there may be some things we add or remove throughout the semester).
Evaluates its argument and discards it, returning ()
. We will use this when we start building interpreters to trace information during evaluation in order to maintain that we're writing valid OCaml programs.
Again, we won't do too much exception handling in this course.
val open_out : string -> out_channel
val flush : out_channel -> unit
val output_string : out_channel -> string -> unit
val output_byte : out_channel -> int -> unit
val close_out : out_channel -> unit
val open_in : string -> in_channel
val input_line : in_channel -> string
val input_byte : in_channel -> int
val close_in : in_channel -> unit
module Filename : sig ... end
module Float : sig ... end
A module containing a floating-point approximation of pi. It is in its own module because we want to be consistent with the OCaml Standard Library.
module Random : sig ... end