Stdlib: add Bool module.

master
Daniel Bünzli 2018-08-30 11:45:02 +02:00
parent 90d9b2287a
commit fdba70136f
14 changed files with 205 additions and 2 deletions

View File

@ -35,6 +35,9 @@ Working version
### Standard library:
- GPR#2010: Add Bool module.
(Many fine eyes)
- GPR#2011: Add Int module.
(Many fine eyes)

View File

@ -40,6 +40,7 @@ Here is a short listing, by theme, of the standard library modules.
"List" & p.~\pageref{List} & list operations \\
"StdLabels" & p.~\pageref{StdLabels} & labelized versions of
the above 4 modules \\
"Bool" & p.~\pageref{Bool} & boolean values \\
"Char" & p.~\pageref{Char} & character operations \\
"Uchar" & p.~\pageref{Uchar} & Unicode characters \\
"Int" & p.~\pageref{Int} & integer values \\
@ -106,6 +107,7 @@ be called from C \\
\item \ahref{libref/Array.html}{Module \texttt{Array}: array operations}
\item \ahref{libref/ArrayLabels.html}{Module \texttt{ArrayLabels}: array operations (with labels)}
\item \ahref{libref/Bigarray.html}{Module \texttt{Bigarray}: large, multi-dimensional, numerical arrays}
\item \ahref{libref/Bool.html}{Module \texttt{Bool}: boolean values}
\item \ahref{libref/Buffer.html}{Module \texttt{Buffer}: extensible buffers}
\item \ahref{libref/Bytes.html}{Module \texttt{Bytes}: byte sequences}
\item \ahref{libref/BytesLabels.html}{Module \texttt{BytesLabels}: byte sequences (with labels)}
@ -157,6 +159,7 @@ be called from C \\
\input{Array.tex}
\input{ArrayLabels.tex}
\input{Bigarray.tex}
\input{Bool.tex}
\input{Buffer.tex}
\input{Bytes.tex}
\input{BytesLabels.tex}

View File

@ -38,7 +38,7 @@ LIB=$(ROOTDIR)/stdlib
# Object file prefix
P=stdlib__
LIB_OBJS=$(LIB)/camlinternalFormatBasics.cmo stdlib.cmo \
LIB_OBJS=$(LIB)/camlinternalFormatBasics.cmo stdlib.cmo $(LIB)/$(P)bool.cmo \
$(LIB)/$(P)seq.cmo $(LIB)/$(P)option.cmo $(LIB)/$(P)result.cmo \
$(LIB)/$(P)array.cmo $(LIB)/$(P)list.cmo \
$(LIB)/$(P)char.cmo $(LIB)/$(P)bytes.cmo $(LIB)/$(P)string.cmo \

View File

@ -659,6 +659,7 @@ module Arg = Arg
module Array = Array
module ArrayLabels = ArrayLabels
module Bigarray = Bigarray
module Bool = Bool
module Buffer = Buffer
module Bytes = Bytes
module BytesLabels = BytesLabels

View File

@ -12,6 +12,9 @@ stdlib__arrayLabels.cmi : stdlib__seq.cmi
stdlib__bigarray.cmo : stdlib__sys.cmi stdlib__complex.cmi stdlib__array.cmi stdlib__bigarray.cmi
stdlib__bigarray.cmx : stdlib__sys.cmx stdlib__complex.cmx stdlib__array.cmx stdlib__bigarray.cmi
stdlib__bigarray.cmi : stdlib__complex.cmi
stdlib__bool.cmo : stdlib.cmi stdlib__bool.cmi
stdlib__bool.cmx : stdlib.cmx stdlib__bool.cmi
stdlib__bool.cmi :
stdlib__buffer.cmo : stdlib__uchar.cmi stdlib__sys.cmi stdlib__string.cmi stdlib__seq.cmi stdlib__char.cmi stdlib__bytes.cmi \
stdlib__buffer.cmi
stdlib__buffer.cmx : stdlib__uchar.cmx stdlib__sys.cmx stdlib__string.cmx stdlib__seq.cmx stdlib__char.cmx stdlib__bytes.cmx \
@ -213,6 +216,8 @@ stdlib__arrayLabels.cmo : stdlib__array.cmi stdlib__arrayLabels.cmi
stdlib__arrayLabels.p.cmx : stdlib__array.cmx stdlib__arrayLabels.cmi
stdlib__bigarray.cmo : stdlib__sys.cmi stdlib__complex.cmi stdlib__array.cmi stdlib__bigarray.cmi
stdlib__bigarray.p.cmx : stdlib__sys.cmx stdlib__complex.cmx stdlib__array.cmx stdlib__bigarray.cmi
stdlib__bool.cmo : stdlib.cmi stdlib__bool.cmi
stdlib__bool.p.cmx : stdlib.cmx stdlib__bool.cmi
stdlib__buffer.cmo : stdlib__uchar.cmi stdlib__sys.cmi stdlib__string.cmi stdlib__seq.cmi stdlib__char.cmi stdlib__bytes.cmi \
stdlib__buffer.cmi
stdlib__buffer.p.cmx : stdlib__uchar.cmx stdlib__sys.cmx stdlib__string.cmx stdlib__seq.cmx stdlib__char.cmx stdlib__bytes.cmx \

View File

@ -42,7 +42,7 @@ P=stdlib__
OBJS=camlinternalFormatBasics.cmo stdlib.cmo $(OTHERS)
OTHERS= $(P)pervasives.cmo $(P)seq.cmo $(P)option.cmo $(P)result.cmo \
$(P)char.cmo $(P)uchar.cmo $(P)sys.cmo $(P)list.cmo \
$(P)bool.cmo $(P)char.cmo $(P)uchar.cmo $(P)sys.cmo $(P)list.cmo \
$(P)bytes.cmo $(P)string.cmo \
$(P)marshal.cmo $(P)obj.cmo $(P)float.cmo $(P)array.cmo \
$(P)int.cmo $(P)int32.cmo $(P)int64.cmo $(P)nativeint.cmo \

View File

@ -26,6 +26,7 @@ STDLIB_MODULES=\
$(P)array \
$(P)arrayLabels \
$(P)bigarray \
$(P)bool \
$(P)buffer \
$(P)bytes \
$(P)bytesLabels \

31
stdlib/bool.ml Normal file
View File

@ -0,0 +1,31 @@
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* The OCaml programmers *)
(* *)
(* Copyright 2018 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
type t = bool
external not : bool -> bool = "%boolnot"
let negate sat v = not (sat v)
external ( && ) : bool -> bool -> bool = "%sequand"
external ( || ) : bool -> bool -> bool = "%sequor"
let equal : bool -> bool -> bool = ( = )
let compare : bool -> bool -> int = Stdlib.compare
let to_int = function false -> 0 | true -> 1
let to_float = function false -> 0. | true -> 1.
let of_string = function
| "false" -> Some false
| "true" -> Some true
| _ -> None
let to_string = function false -> "false" | true -> "true"

65
stdlib/bool.mli Normal file
View File

@ -0,0 +1,65 @@
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* The OCaml programmers *)
(* *)
(* Copyright 2018 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
(** Boolean values.
@since 4.08 *)
(** {1:bools Booleans} *)
type t = bool
(** The type for boolean values. *)
val not : bool -> bool
(** [not b] is the boolean negation of [b]. *)
val negate : ('a -> bool) -> ('a -> bool)
(** [negate p v] is [not (p v)]. *)
external ( && ) : bool -> bool -> bool = "%sequand"
(** [e0 && e1] is the lazy boolean conjunction of expressions [e0] and [e1].
If [e0] evaluates to [false], [e1] is not evaluated. Right-associative
operator at precedence level 3/11. *)
external ( || ) : bool -> bool -> bool = "%sequor"
(** [e0 || e1] is the lazy boolean disjunction of expressions [e0] and [e1].
If [e0] evaluates to [true], [e1] is not evaluated. Right-associative
operator at precedence level 2/11. *)
(** {1:preds Predicates and comparisons} *)
val equal : bool -> bool -> bool
(** [equal b0 b1] is [true] iff [b0] and [b1] are both either [true]
or [false]. *)
val compare : bool -> bool -> int
(** [compare b0 b1] is a total order on boolean values. [false] is smaller
than [true]. *)
(** {1:convert Converting} *)
val to_int : bool -> int
(** [to_int b] is [0] if [b] is [false] and [1] if [b] is [true]. *)
val to_float : bool -> float
(** [to_float b] is [0.] if [b] is [false] and [1.] if [b] is [true]. *)
val of_string : string -> bool option
(** [of_string s] is [Some true] if [s] is ["true"], [Some false] if [s]
is ["false"] and [None] otherwise. *)
val to_string : bool -> string
(** [to_string b] is ["true"] if [b] is [true] and ["false"] if [b] is
[false]. *)

View File

@ -571,6 +571,7 @@ module Arg = Arg
module Array = Array
module ArrayLabels = ArrayLabels
module Bigarray = Bigarray
module Bool = Bool
module Buffer = Buffer
module Bytes = Bytes
module BytesLabels = BytesLabels

View File

@ -1240,6 +1240,7 @@ module Arg = Arg
module Array = Array
module ArrayLabels = ArrayLabels
module Bigarray = Bigarray
module Bool = Bool
module Buffer = Buffer
module Bytes = Bytes
module BytesLabels = BytesLabels

View File

@ -0,0 +1 @@
test.ml

View File

@ -0,0 +1,90 @@
(* TEST
*)
let test_not () =
assert (Bool.not false = true);
assert (Bool.not true = false);
()
let test_negate () =
assert (Bool.negate (Bool.equal true) true = false);
assert (Bool.negate (Bool.equal true) false = true);
()
let test_and () =
let wit = ref 0 in
assert (Bool.( && ) (incr wit; false) (incr wit; false) = false);
assert (!wit = 1); wit := 0;
assert (Bool.( && ) (incr wit; false) (incr wit; true) = false);
assert (!wit = 1); wit := 0;
assert (Bool.( && ) (incr wit; true) (incr wit; false) = false);
assert (!wit = 2); wit := 0;
assert (Bool.( && ) (incr wit; true) (incr wit; true) = true);
assert (!wit = 2); wit := 0;
()
let test_or () =
let wit = ref 0 in
assert (Bool.( || ) (incr wit; false) (incr wit; false) = false);
assert (!wit = 2); wit := 0;
assert (Bool.( || ) (incr wit; false) (incr wit; true) = true);
assert (!wit = 2); wit := 0;
assert (Bool.( || ) (incr wit; true) (incr wit; false) = true);
assert (!wit = 1); wit := 0;
assert (Bool.( || ) (incr wit; true) (incr wit; true) = true);
assert (!wit = 1); wit := 0;
()
let test_equal () =
assert (Bool.equal false false = true);
assert (Bool.equal false true = false);
assert (Bool.equal true false = false);
assert (Bool.equal true true = true);
()
let test_compare () =
assert (Bool.compare false false = 0);
assert (Bool.compare false true = -1);
assert (Bool.compare true false = 1);
assert (Bool.compare true true = 0);
()
let test_to_int () =
assert (Bool.to_int false = 0);
assert (Bool.to_int true = 1);
()
let test_to_float () =
assert (Bool.to_float false = 0.);
assert (Bool.to_float true = 1.);
()
let test_of_string () =
assert (Bool.of_string "false" = Some false);
assert (Bool.of_string "true" = Some true);
assert (Bool.of_string "heyho" = None);
assert (Bool.of_string "1" = None);
assert (Bool.of_string "0" = None);
()
let test_to_string () =
assert (Bool.to_string false = "false");
assert (Bool.to_string true = "true");
()
let tests () =
test_not ();
test_negate ();
test_and ();
test_or ();
test_equal ();
test_compare ();
test_to_int ();
test_to_float ();
test_of_string ();
test_to_string ();
()
let () =
tests ();
print_endline "OK"

View File

@ -0,0 +1 @@
OK