/
batBool.ml
84 lines (68 loc) · 2.52 KB
/
batBool.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
(*
* ExtBool - Extended booleans
* Copyright (C) 2007 Bluestorm <bluestorm dot dylc on-the-server gmail dot com>
* 2008 David Teller
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version,
* with the special exception on linking described in file LICENSE.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)
open BatNumber
module BaseBool = struct
type t = bool
external not : bool -> bool = "%boolnot"
(** The boolean negation. *)
external ( && ) : bool -> bool -> bool = "%sequand"
(** The boolean ``and''. Evaluation is sequential, left-to-right:
in [e1 && e2], [e1] is evaluated first, and if it returns [false],
[e2] is not evaluated at all. *)
external ( || ) : bool -> bool -> bool = "%sequor"
(** The boolean ``or''. Evaluation is sequential, left-to-right:
in [e1 || e2], [e1] is evaluated first, and if it returns [true],
[e2] is not evaluated at all. *)
let zero, one = false, true
let neg = not
let succ x = true
let pred x = false
let abs x = x
let add = ( || )
let mul = ( && )
let sub _ = not (*Weird extrapolation*)
let div _ _=
raise (Invalid_argument "Bool.div")
let modulo _ _ =
raise (Invalid_argument "Bool.modulo")
let pow _ _ =
raise (Invalid_argument "Bool.pow")
let min_num, max_num = false, true
let compare = compare
let of_int = function
| 0 -> false
| _ -> true
let to_int = function
| false -> 0
| true -> 1
open BatStd
let of_float = of_int -| int_of_float
let to_float = float_of_int -| to_int
let of_string = function
| "true" | "tt" | "1" -> true
| "false"| "ff" | "0" -> false
| _ -> raise (Invalid_argument "Bool.of_string")
let to_string = string_of_bool
end
include BaseBool
include BatNumber.MakeNumeric(BaseBool)
let print out t = BatInnerIO.nwrite out (to_string t)
let t_printer paren out t = print out t