007500e10a428b9937d72c3cee6e48d6f9112232
[bpt/guile.git] / libguile / boolean.h
1 /* classes: h_files */
2
3 #ifndef SCM_BOOLEAN_H
4 #define SCM_BOOLEAN_H
5
6 /* Copyright (C) 1995,1996,2000, 2006, 2008, 2009, 2010 Free Software Foundation, Inc.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 */
23
24 \f
25
26 #include "libguile/__scm.h"
27
28 \f
29
30 /* Boolean Values. Obviously there are #t and #f, but there is also nil to deal
31 * with. We choose to treat nil as a false boolean. All options might silently
32 * break existing code, but this one seems most responsible.
33 *
34 */
35
36 /*
37 * Use these macros if it's important (for correctness)
38 * that %nil MUST be considered true
39 */
40 #define scm_is_false_and_not_nil(x) (scm_is_eq ((x), SCM_BOOL_F))
41 #define scm_is_true_or_nil(x) (!scm_is_eq ((x), SCM_BOOL_F))
42
43 /*
44 * Use these macros if %nil will never be tested,
45 * for increased efficiency.
46 */
47 #define scm_is_false_assume_not_nil(x) (scm_is_eq ((x), SCM_BOOL_F))
48 #define scm_is_true_assume_not_nil(x) (!scm_is_eq ((x), SCM_BOOL_F))
49
50 /*
51 * See the comments preceeding the definitions of SCM_BOOL_F and
52 * SCM_MATCHES_BITS_IN_COMMON in tags.h for more information on
53 * how the following macro works.
54 */
55 #if SCM_ENABLE_ELISP
56 # define scm_is_false_or_nil(x) \
57 (SCM_MATCHES_BITS_IN_COMMON ((x), SCM_ELISP_NIL, SCM_BOOL_F))
58 #else
59 # define scm_is_false_or_nil(x) (scm_is_false_assume_not_nil (x))
60 #endif
61 #define scm_is_true_and_not_nil(x) (!scm_is_false_or_nil (x))
62
63 /* %nil is false. */
64 #define scm_is_false(x) (scm_is_false_or_nil (x))
65 #define scm_is_true(x) (!scm_is_false (x))
66
67 /*
68 * Since we know SCM_BOOL_F and SCM_BOOL_T differ by exactly one bit,
69 * and that SCM_BOOL_F and SCM_ELISP_NIL differ by exactly one bit,
70 * and that they of course can't be the same bit (or else SCM_BOOL_T
71 * and SCM_ELISP_NIL be would equal), it follows that SCM_BOOL_T and
72 * SCM_ELISP_NIL differ by exactly two bits, and these are the bits
73 * which will be ignored by SCM_MATCHES_BITS_IN_COMMON below.
74 *
75 * See the comments preceeding the definitions of SCM_BOOL_F and
76 * SCM_MATCHES_BITS_IN_COMMON in tags.h for more information.
77 *
78 * If SCM_ENABLE_ELISP is true, then scm_is_bool_or_nil(x)
79 * returns 1 if and only if x is one of the following: SCM_BOOL_F,
80 * SCM_BOOL_T, SCM_ELISP_NIL, or SCM_XXX_ANOTHER_BOOLEAN_DONT_USE_0.
81 * Otherwise, it returns 0.
82 */
83 #if SCM_ENABLE_ELISP
84 # define scm_is_bool_or_nil(x) \
85 (SCM_MATCHES_BITS_IN_COMMON ((x), SCM_BOOL_T, SCM_ELISP_NIL))
86 #else
87 # define scm_is_bool_or_nil(x) (scm_is_bool_and_not_nil (x))
88 #endif
89
90 #define scm_is_bool_and_not_nil(x) \
91 (SCM_MATCHES_BITS_IN_COMMON ((x), SCM_BOOL_F, SCM_BOOL_T))
92
93 SCM_API int scm_is_bool (SCM);
94
95 #define scm_is_bool(x) (scm_is_bool_or_nil (x))
96
97 #define scm_from_bool(x) ((x) ? SCM_BOOL_T : SCM_BOOL_F)
98 SCM_API int scm_to_bool (SCM x);
99
100 \f
101
102 /*
103 * The following macros efficiently implement boolean truth testing as
104 * expected by most lisps, which treat '() aka SCM_EOL as false.
105 *
106 * Since we know SCM_ELISP_NIL and SCM_BOOL_F differ by exactly one
107 * bit, and that SCM_ELISP_NIL and SCM_EOL differ by exactly one bit,
108 * and that they of course can't be the same bit (or else SCM_BOOL_F
109 * and SCM_EOL be would equal), it follows that SCM_BOOL_F and SCM_EOL
110 * differ by exactly two bits, and these are the bits which will be
111 * ignored by SCM_MATCHES_BITS_IN_COMMON below.
112 *
113 * See the comments preceeding the definitions of SCM_BOOL_F and
114 * SCM_MATCHES_BITS_IN_COMMON in tags.h for more information.
115 *
116 * scm_is_lisp_false(x) returns 1 if and only if x is one of the
117 * following: SCM_BOOL_F, SCM_ELISP_NIL, SCM_EOL or
118 * SCM_XXX_ANOTHER_LISP_FALSE_DONT_USE. Otherwise, it returns 0.
119 */
120 #if SCM_ENABLE_ELISP
121 # define scm_is_lisp_false(x) \
122 (SCM_MATCHES_BITS_IN_COMMON ((x), SCM_BOOL_F, SCM_EOL))
123 # define scm_is_lisp_true(x) (!scm_is_lisp_false(x))
124 #endif
125
126 \f
127
128 SCM_API SCM scm_not (SCM x);
129 SCM_API SCM scm_boolean_p (SCM obj);
130
131 SCM_INTERNAL void scm_init_boolean (void);
132
133 #endif /* SCM_BOOLEAN_H */
134
135 /*
136 Local Variables:
137 c-file-style: "gnu"
138 End:
139 */