eq.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /* Copyright (C) 1995,1996,1997,1998,2000,2001,2003, 2004, 2006 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include "libguile/_scm.h"
  22. #include "libguile/ramap.h"
  23. #include "libguile/stackchk.h"
  24. #include "libguile/strorder.h"
  25. #include "libguile/async.h"
  26. #include "libguile/root.h"
  27. #include "libguile/smob.h"
  28. #include "libguile/unif.h"
  29. #include "libguile/vectors.h"
  30. #include "libguile/struct.h"
  31. #include "libguile/goops.h"
  32. #include "libguile/objects.h"
  33. #include "libguile/validate.h"
  34. #include "libguile/eq.h"
  35. #include "libguile/private-options.h"
  36. #ifdef HAVE_STRING_H
  37. #include <string.h>
  38. #endif
  39. SCM_DEFINE1 (scm_eq_p, "eq?", scm_tc7_rpsubr,
  40. (SCM x, SCM y),
  41. "Return @code{#t} if @var{x} and @var{y} are the same object,\n"
  42. "except for numbers and characters. For example,\n"
  43. "\n"
  44. "@example\n"
  45. "(define x (vector 1 2 3))\n"
  46. "(define y (vector 1 2 3))\n"
  47. "\n"
  48. "(eq? x x) @result{} #t\n"
  49. "(eq? x y) @result{} #f\n"
  50. "@end example\n"
  51. "\n"
  52. "Numbers and characters are not equal to any other object, but\n"
  53. "the problem is they're not necessarily @code{eq?} to themselves\n"
  54. "either. This is even so when the number comes directly from a\n"
  55. "variable,\n"
  56. "\n"
  57. "@example\n"
  58. "(let ((n (+ 2 3)))\n"
  59. " (eq? n n)) @result{} *unspecified*\n"
  60. "@end example\n"
  61. "\n"
  62. "Generally @code{eqv?} should be used when comparing numbers or\n"
  63. "characters. @code{=} or @code{char=?} can be used too.\n"
  64. "\n"
  65. "It's worth noting that end-of-list @code{()}, @code{#t},\n"
  66. "@code{#f}, a symbol of a given name, and a keyword of a given\n"
  67. "name, are unique objects. There's just one of each, so for\n"
  68. "instance no matter how @code{()} arises in a program, it's the\n"
  69. "same object and can be compared with @code{eq?},\n"
  70. "\n"
  71. "@example\n"
  72. "(define x (cdr '(123)))\n"
  73. "(define y (cdr '(456)))\n"
  74. "(eq? x y) @result{} #t\n"
  75. "\n"
  76. "(define x (string->symbol \"foo\"))\n"
  77. "(eq? x 'foo) @result{} #t\n"
  78. "@end example")
  79. #define FUNC_NAME s_scm_eq_p
  80. {
  81. return scm_from_bool (scm_is_eq (x, y));
  82. }
  83. #undef FUNC_NAME
  84. /* We compare doubles in a special way for 'eqv?' to be able to
  85. distinguish plus and minus zero and to identify NaNs.
  86. */
  87. static int
  88. real_eqv (double x, double y)
  89. {
  90. return !memcmp (&x, &y, sizeof(double)) || (x != x && y != y);
  91. }
  92. #include <stdio.h>
  93. SCM_PRIMITIVE_GENERIC_1 (scm_eqv_p, "eqv?", scm_tc7_rpsubr,
  94. (SCM x, SCM y),
  95. "Return @code{#t} if @var{x} and @var{y} are the same object, or\n"
  96. "for characters and numbers the same value.\n"
  97. "\n"
  98. "On objects except characters and numbers, @code{eqv?} is the\n"
  99. "same as @code{eq?}, it's true if @var{x} and @var{y} are the\n"
  100. "same object.\n"
  101. "\n"
  102. "If @var{x} and @var{y} are numbers or characters, @code{eqv?}\n"
  103. "compares their type and value. An exact number is not\n"
  104. "@code{eqv?} to an inexact number (even if their value is the\n"
  105. "same).\n"
  106. "\n"
  107. "@example\n"
  108. "(eqv? 3 (+ 1 2)) @result{} #t\n"
  109. "(eqv? 1 1.0) @result{} #f\n"
  110. "@end example")
  111. #define FUNC_NAME s_scm_eqv_p
  112. {
  113. if (scm_is_eq (x, y))
  114. return SCM_BOOL_T;
  115. if (SCM_IMP (x))
  116. return SCM_BOOL_F;
  117. if (SCM_IMP (y))
  118. return SCM_BOOL_F;
  119. /* this ensures that types and scm_length are the same. */
  120. if (SCM_CELL_TYPE (x) != SCM_CELL_TYPE (y))
  121. {
  122. /* fractions use 0x10000 as a flag (at the suggestion of Marius Vollmer),
  123. but this checks the entire type word, so fractions may be accidentally
  124. flagged here as unequal. Perhaps I should use the 4th double_cell word?
  125. */
  126. /* treat mixes of real and complex types specially */
  127. if (SCM_INEXACTP (x))
  128. {
  129. if (SCM_REALP (x))
  130. return scm_from_bool (SCM_COMPLEXP (y)
  131. && real_eqv (SCM_REAL_VALUE (x),
  132. SCM_COMPLEX_REAL (y))
  133. && SCM_COMPLEX_IMAG (y) == 0.0);
  134. else
  135. return scm_from_bool (SCM_REALP (y)
  136. && real_eqv (SCM_COMPLEX_REAL (x),
  137. SCM_REAL_VALUE (y))
  138. && SCM_COMPLEX_IMAG (x) == 0.0);
  139. }
  140. if (SCM_FRACTIONP (x) && SCM_FRACTIONP (y))
  141. return scm_i_fraction_equalp (x, y);
  142. return SCM_BOOL_F;
  143. }
  144. if (SCM_NUMP (x))
  145. {
  146. if (SCM_BIGP (x)) {
  147. return scm_from_bool (scm_i_bigcmp (x, y) == 0);
  148. } else if (SCM_REALP (x)) {
  149. return scm_from_bool (real_eqv (SCM_REAL_VALUE (x), SCM_REAL_VALUE (y)));
  150. } else if (SCM_FRACTIONP (x)) {
  151. return scm_i_fraction_equalp (x, y);
  152. } else { /* complex */
  153. return scm_from_bool (real_eqv (SCM_COMPLEX_REAL (x),
  154. SCM_COMPLEX_REAL (y))
  155. && real_eqv (SCM_COMPLEX_IMAG (x),
  156. SCM_COMPLEX_IMAG (y)));
  157. }
  158. }
  159. if (SCM_UNPACK (g_scm_eqv_p))
  160. return scm_call_generic_2 (g_scm_eqv_p, x, y);
  161. else
  162. return SCM_BOOL_F;
  163. }
  164. #undef FUNC_NAME
  165. SCM_PRIMITIVE_GENERIC_1 (scm_equal_p, "equal?", scm_tc7_rpsubr,
  166. (SCM x, SCM y),
  167. "Return @code{#t} if @var{x} and @var{y} are the same type, and\n"
  168. "their contents or value are equal.\n"
  169. "\n"
  170. "For a pair, string, vector or array, @code{equal?} compares the\n"
  171. "contents, and does so using using the same @code{equal?}\n"
  172. "recursively, so a deep structure can be traversed.\n"
  173. "\n"
  174. "@example\n"
  175. "(equal? (list 1 2 3) (list 1 2 3)) @result{} #t\n"
  176. "(equal? (list 1 2 3) (vector 1 2 3)) @result{} #f\n"
  177. "@end example\n"
  178. "\n"
  179. "For other objects, @code{equal?} compares as per @code{eqv?},\n"
  180. "which means characters and numbers are compared by type and\n"
  181. "value (and like @code{eqv?}, exact and inexact numbers are not\n"
  182. "@code{equal?}, even if their value is the same).\n"
  183. "\n"
  184. "@example\n"
  185. "(equal? 3 (+ 1 2)) @result{} #t\n"
  186. "(equal? 1 1.0) @result{} #f\n"
  187. "@end example\n"
  188. "\n"
  189. "Hash tables are currently only compared as per @code{eq?}, so\n"
  190. "two different tables are not @code{equal?}, even if their\n"
  191. "contents are the same.\n"
  192. "\n"
  193. "@code{equal?} does not support circular data structures, it may\n"
  194. "go into an infinite loop if asked to compare two circular lists\n"
  195. "or similar.\n"
  196. "\n"
  197. "New application-defined object types (Smobs) have an\n"
  198. "@code{equalp} handler which is called by @code{equal?}. This\n"
  199. "lets an application traverse the contents or control what is\n"
  200. "considered @code{equal?} for two such objects. If there's no\n"
  201. "handler, the default is to just compare as per @code{eq?}.")
  202. #define FUNC_NAME s_scm_equal_p
  203. {
  204. SCM_CHECK_STACK;
  205. tailrecurse:
  206. SCM_TICK;
  207. if (scm_is_eq (x, y))
  208. return SCM_BOOL_T;
  209. if (SCM_IMP (x))
  210. return SCM_BOOL_F;
  211. if (SCM_IMP (y))
  212. return SCM_BOOL_F;
  213. if (scm_is_pair (x) && scm_is_pair (y))
  214. {
  215. if (scm_is_false (scm_equal_p (SCM_CAR (x), SCM_CAR (y))))
  216. return SCM_BOOL_F;
  217. x = SCM_CDR(x);
  218. y = SCM_CDR(y);
  219. goto tailrecurse;
  220. }
  221. if (SCM_TYP7 (x) == scm_tc7_string && SCM_TYP7 (y) == scm_tc7_string)
  222. return scm_string_equal_p (x, y);
  223. if (SCM_TYP7 (x) == scm_tc7_smob && SCM_TYP16 (x) == SCM_TYP16 (y))
  224. {
  225. int i = SCM_SMOBNUM (x);
  226. if (!(i < scm_numsmob))
  227. return SCM_BOOL_F;
  228. if (scm_smobs[i].equalp)
  229. return (scm_smobs[i].equalp) (x, y);
  230. else
  231. goto generic_equal;
  232. }
  233. /* This ensures that types and scm_length are the same. */
  234. if (SCM_CELL_TYPE (x) != SCM_CELL_TYPE (y))
  235. {
  236. /* treat mixes of real and complex types specially */
  237. if (SCM_INEXACTP (x) && SCM_INEXACTP (y))
  238. {
  239. if (SCM_REALP (x))
  240. return scm_from_bool (SCM_COMPLEXP (y)
  241. && SCM_REAL_VALUE (x) == SCM_COMPLEX_REAL (y)
  242. && SCM_COMPLEX_IMAG (y) == 0.0);
  243. else
  244. return scm_from_bool (SCM_REALP (y)
  245. && SCM_COMPLEX_REAL (x) == SCM_REAL_VALUE (y)
  246. && SCM_COMPLEX_IMAG (x) == 0.0);
  247. }
  248. /* Vectors can be equal to one-dimensional arrays.
  249. */
  250. if (SCM_I_ARRAYP (x) || SCM_I_ARRAYP (y))
  251. return scm_array_equal_p (x, y);
  252. return SCM_BOOL_F;
  253. }
  254. switch (SCM_TYP7 (x))
  255. {
  256. default:
  257. break;
  258. case scm_tc7_number:
  259. switch SCM_TYP16 (x)
  260. {
  261. case scm_tc16_big:
  262. return scm_bigequal (x, y);
  263. case scm_tc16_real:
  264. return scm_real_equalp (x, y);
  265. case scm_tc16_complex:
  266. return scm_complex_equalp (x, y);
  267. case scm_tc16_fraction:
  268. return scm_i_fraction_equalp (x, y);
  269. }
  270. case scm_tc7_vector:
  271. case scm_tc7_wvect:
  272. return scm_i_vector_equal_p (x, y);
  273. }
  274. /* Check equality between structs of equal type (see cell-type test above)
  275. that are not GOOPS instances. GOOPS instances are treated via the
  276. generic function. */
  277. if ((SCM_STRUCTP (x)) && (!SCM_INSTANCEP (x)))
  278. return scm_i_struct_equalp (x, y);
  279. generic_equal:
  280. if (SCM_UNPACK (g_scm_equal_p))
  281. return scm_call_generic_2 (g_scm_equal_p, x, y);
  282. else
  283. return SCM_BOOL_F;
  284. }
  285. #undef FUNC_NAME
  286. void
  287. scm_init_eq ()
  288. {
  289. #include "libguile/eq.x"
  290. }
  291. /*
  292. Local Variables:
  293. c-file-style: "gnu"
  294. End:
  295. */