eq.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /* Copyright 1995-1998,2000-2001,2003-2004,2006,2009-2011,2017-2018
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <math.h>
  19. #include <string.h>
  20. #include "array-map.h"
  21. #include "async.h"
  22. #include "bitvectors.h"
  23. #include "boolean.h"
  24. #include "bytevectors.h"
  25. #include "eval.h"
  26. #include "foreign.h"
  27. #include "generalized-arrays.h"
  28. #include "goops.h"
  29. #include "gsubr.h"
  30. #include "hashtab.h"
  31. #include "pairs.h"
  32. #include "private-options.h"
  33. #include "smob.h"
  34. #include "stackchk.h"
  35. #include "strorder.h"
  36. #include "struct.h"
  37. #include "syntax.h"
  38. #include "vectors.h"
  39. #include "eq.h"
  40. static SCM scm_i_eq_p (SCM x, SCM y, SCM rest);
  41. SCM_DEFINE (scm_i_eq_p, "eq?", 0, 2, 1,
  42. (SCM x, SCM y, SCM rest),
  43. "Return @code{#t} if @var{x} and @var{y} are the same object,\n"
  44. "except for numbers and characters. For example,\n"
  45. "\n"
  46. "@example\n"
  47. "(define x (vector 1 2 3))\n"
  48. "(define y (vector 1 2 3))\n"
  49. "\n"
  50. "(eq? x x) @result{} #t\n"
  51. "(eq? x y) @result{} #f\n"
  52. "@end example\n"
  53. "\n"
  54. "Numbers and characters are not equal to any other object, but\n"
  55. "the problem is they're not necessarily @code{eq?} to themselves\n"
  56. "either. This is even so when the number comes directly from a\n"
  57. "variable,\n"
  58. "\n"
  59. "@example\n"
  60. "(let ((n (+ 2 3)))\n"
  61. " (eq? n n)) @result{} *unspecified*\n"
  62. "@end example\n"
  63. "\n"
  64. "Generally @code{eqv?} should be used when comparing numbers or\n"
  65. "characters. @code{=} or @code{char=?} can be used too.\n"
  66. "\n"
  67. "It's worth noting that end-of-list @code{()}, @code{#t},\n"
  68. "@code{#f}, a symbol of a given name, and a keyword of a given\n"
  69. "name, are unique objects. There's just one of each, so for\n"
  70. "instance no matter how @code{()} arises in a program, it's the\n"
  71. "same object and can be compared with @code{eq?},\n"
  72. "\n"
  73. "@example\n"
  74. "(define x (cdr '(123)))\n"
  75. "(define y (cdr '(456)))\n"
  76. "(eq? x y) @result{} #t\n"
  77. "\n"
  78. "(define x (string->symbol \"foo\"))\n"
  79. "(eq? x 'foo) @result{} #t\n"
  80. "@end example")
  81. #define FUNC_NAME s_scm_i_eq_p
  82. {
  83. if (SCM_UNBNDP (x) || SCM_UNBNDP (y))
  84. return SCM_BOOL_T;
  85. while (scm_is_pair (rest))
  86. {
  87. if (!scm_is_eq (x, y))
  88. return SCM_BOOL_F;
  89. x = y;
  90. y = scm_car (rest);
  91. rest = scm_cdr (rest);
  92. }
  93. return scm_from_bool (scm_is_eq (x, y));
  94. }
  95. #undef FUNC_NAME
  96. SCM
  97. scm_eq_p (SCM x, SCM y)
  98. {
  99. return scm_from_bool (scm_is_eq (x, y));
  100. }
  101. /* We compare doubles in a special way for 'eqv?' to be able to
  102. distinguish plus and minus zero and to identify NaNs.
  103. */
  104. static int
  105. real_eqv (double x, double y)
  106. {
  107. return !memcmp (&x, &y, sizeof(double))
  108. || (SCM_UNLIKELY (isnan (x)) && SCM_UNLIKELY (isnan (y)));
  109. }
  110. SCM
  111. scm_real_equalp (SCM x, SCM y)
  112. {
  113. return scm_from_bool (real_eqv (SCM_REAL_VALUE (x),
  114. SCM_REAL_VALUE (y)));
  115. }
  116. SCM
  117. scm_bigequal (SCM x, SCM y)
  118. {
  119. return scm_from_bool (scm_i_bigcmp (x, y) == 0);
  120. }
  121. SCM
  122. scm_complex_equalp (SCM x, SCM y)
  123. {
  124. return scm_from_bool (real_eqv (SCM_COMPLEX_REAL (x),
  125. SCM_COMPLEX_REAL (y))
  126. && real_eqv (SCM_COMPLEX_IMAG (x),
  127. SCM_COMPLEX_IMAG (y)));
  128. }
  129. SCM
  130. scm_i_fraction_equalp (SCM x, SCM y)
  131. {
  132. return scm_from_bool
  133. (scm_is_true (scm_equal_p (SCM_FRACTION_NUMERATOR (x),
  134. SCM_FRACTION_NUMERATOR (y)))
  135. && scm_is_true (scm_equal_p (SCM_FRACTION_DENOMINATOR (x),
  136. SCM_FRACTION_DENOMINATOR (y))));
  137. }
  138. int
  139. scm_i_heap_numbers_equal_p (SCM x, SCM y)
  140. {
  141. if (SCM_IMP (x)) abort();
  142. switch (SCM_TYP16 (x))
  143. {
  144. case scm_tc16_big:
  145. return scm_is_true (scm_bigequal (x, y));
  146. case scm_tc16_real:
  147. return scm_is_true (scm_real_equalp (x, y));
  148. case scm_tc16_complex:
  149. return scm_is_true (scm_complex_equalp (x, y));
  150. case scm_tc16_fraction:
  151. return scm_is_true (scm_i_fraction_equalp (x, y));
  152. default:
  153. abort ();
  154. }
  155. }
  156. static SCM scm_i_eqv_p (SCM x, SCM y, SCM rest);
  157. #include <stdio.h>
  158. SCM_DEFINE (scm_i_eqv_p, "eqv?", 0, 2, 1,
  159. (SCM x, SCM y, SCM rest),
  160. "Return @code{#t} if @var{x} and @var{y} are the same object, or\n"
  161. "for characters and numbers the same value.\n"
  162. "\n"
  163. "On objects except characters and numbers, @code{eqv?} is the\n"
  164. "same as @code{eq?}, it's true if @var{x} and @var{y} are the\n"
  165. "same object.\n"
  166. "\n"
  167. "If @var{x} and @var{y} are numbers or characters, @code{eqv?}\n"
  168. "compares their type and value. An exact number is not\n"
  169. "@code{eqv?} to an inexact number (even if their value is the\n"
  170. "same).\n"
  171. "\n"
  172. "@example\n"
  173. "(eqv? 3 (+ 1 2)) @result{} #t\n"
  174. "(eqv? 1 1.0) @result{} #f\n"
  175. "@end example")
  176. #define FUNC_NAME s_scm_i_eqv_p
  177. {
  178. if (SCM_UNBNDP (x) || SCM_UNBNDP (y))
  179. return SCM_BOOL_T;
  180. while (!scm_is_null (rest))
  181. {
  182. if (!scm_is_true (scm_eqv_p (x, y)))
  183. return SCM_BOOL_F;
  184. x = y;
  185. y = scm_car (rest);
  186. rest = scm_cdr (rest);
  187. }
  188. return scm_eqv_p (x, y);
  189. }
  190. #undef FUNC_NAME
  191. SCM scm_eqv_p (SCM x, SCM y)
  192. #define FUNC_NAME s_scm_i_eqv_p
  193. {
  194. if (scm_is_eq (x, y))
  195. return SCM_BOOL_T;
  196. if (SCM_IMP (x))
  197. return SCM_BOOL_F;
  198. if (SCM_IMP (y))
  199. return SCM_BOOL_F;
  200. /* this ensures that types and scm_length are the same. */
  201. if (SCM_CELL_TYPE (x) != SCM_CELL_TYPE (y))
  202. return SCM_BOOL_F;
  203. switch (SCM_TYP11 (x))
  204. {
  205. default:
  206. break;
  207. case scm_tc11_number:
  208. return scm_from_bool (scm_i_heap_numbers_equal_p (x, y));
  209. }
  210. return SCM_BOOL_F;
  211. }
  212. #undef FUNC_NAME
  213. static SCM scm_i_equal_p (SCM, SCM, SCM);
  214. SCM_PRIMITIVE_GENERIC (scm_i_equal_p, "equal?", 0, 2, 1,
  215. (SCM x, SCM y, SCM rest),
  216. "Return @code{#t} if @var{x} and @var{y} are the same type, and\n"
  217. "their contents or value are equal.\n"
  218. "\n"
  219. "For a pair, string, vector or array, @code{equal?} compares the\n"
  220. "contents, and does so using using the same @code{equal?}\n"
  221. "recursively, so a deep structure can be traversed.\n"
  222. "\n"
  223. "@example\n"
  224. "(equal? (list 1 2 3) (list 1 2 3)) @result{} #t\n"
  225. "(equal? (list 1 2 3) (vector 1 2 3)) @result{} #f\n"
  226. "@end example\n"
  227. "\n"
  228. "For other objects, @code{equal?} compares as per @code{eqv?},\n"
  229. "which means characters and numbers are compared by type and\n"
  230. "value (and like @code{eqv?}, exact and inexact numbers are not\n"
  231. "@code{equal?}, even if their value is the same).\n"
  232. "\n"
  233. "@example\n"
  234. "(equal? 3 (+ 1 2)) @result{} #t\n"
  235. "(equal? 1 1.0) @result{} #f\n"
  236. "@end example\n"
  237. "\n"
  238. "Hash tables are currently only compared as per @code{eq?}, so\n"
  239. "two different tables are not @code{equal?}, even if their\n"
  240. "contents are the same.\n"
  241. "\n"
  242. "@code{equal?} does not support circular data structures, it may\n"
  243. "go into an infinite loop if asked to compare two circular lists\n"
  244. "or similar.\n"
  245. "\n"
  246. "New application-defined object types (Smobs) have an\n"
  247. "@code{equalp} handler which is called by @code{equal?}. This\n"
  248. "lets an application traverse the contents or control what is\n"
  249. "considered @code{equal?} for two such objects. If there's no\n"
  250. "handler, the default is to just compare as per @code{eq?}.")
  251. #define FUNC_NAME s_scm_i_equal_p
  252. {
  253. if (SCM_UNBNDP (x) || SCM_UNBNDP (y))
  254. return SCM_BOOL_T;
  255. while (!scm_is_null (rest))
  256. {
  257. if (!scm_is_true (scm_equal_p (x, y)))
  258. return SCM_BOOL_F;
  259. x = y;
  260. y = scm_car (rest);
  261. rest = SCM_CDR (rest);
  262. }
  263. return scm_equal_p (x, y);
  264. }
  265. #undef FUNC_NAME
  266. SCM
  267. scm_equal_p (SCM x, SCM y)
  268. #define FUNC_NAME s_scm_i_equal_p
  269. {
  270. SCM_CHECK_STACK;
  271. tailrecurse:
  272. SCM_TICK;
  273. if (scm_is_eq (x, y))
  274. return SCM_BOOL_T;
  275. if (SCM_IMP (x))
  276. return SCM_BOOL_F;
  277. if (SCM_IMP (y))
  278. return SCM_BOOL_F;
  279. if (scm_is_pair (x) && scm_is_pair (y))
  280. {
  281. if (scm_is_false (scm_equal_p (SCM_CAR (x), SCM_CAR (y))))
  282. return SCM_BOOL_F;
  283. x = SCM_CDR(x);
  284. y = SCM_CDR(y);
  285. goto tailrecurse;
  286. }
  287. if (SCM_TYP7 (x) == scm_tc7_smob && SCM_TYP16 (x) == SCM_TYP16 (y))
  288. {
  289. int i = SCM_SMOBNUM (x);
  290. if (!(i < scm_numsmob))
  291. return SCM_BOOL_F;
  292. if (scm_smobs[i].equalp)
  293. return (scm_smobs[i].equalp) (x, y);
  294. else
  295. goto generic_equal;
  296. }
  297. /* This ensures that types and scm_length are the same. */
  298. if (SCM_CELL_TYPE (x) != SCM_CELL_TYPE (y))
  299. {
  300. /* Vectors can be equal to one-dimensional arrays.
  301. */
  302. if (scm_is_array (x) && scm_is_array (y))
  303. return scm_array_equal_p (x, y);
  304. return SCM_BOOL_F;
  305. }
  306. switch (SCM_TYP11 (x))
  307. {
  308. default:
  309. /* Check equality between structs of equal type (see cell-type test above). */
  310. if (SCM_STRUCTP (x))
  311. {
  312. if (SCM_INSTANCEP (x))
  313. goto generic_equal;
  314. else
  315. return scm_i_struct_equalp (x, y);
  316. }
  317. break;
  318. case scm_tc11_number:
  319. switch SCM_TYP16 (x)
  320. {
  321. case scm_tc16_big:
  322. return scm_bigequal (x, y);
  323. case scm_tc16_real:
  324. return scm_real_equalp (x, y);
  325. case scm_tc16_complex:
  326. return scm_complex_equalp (x, y);
  327. case scm_tc16_fraction:
  328. return scm_i_fraction_equalp (x, y);
  329. default:
  330. /* assert not reached? */
  331. return SCM_BOOL_F;
  332. }
  333. case scm_tc11_pointer:
  334. return scm_from_bool (SCM_POINTER_VALUE (x) == SCM_POINTER_VALUE (y));
  335. case scm_tc11_string:
  336. return scm_string_equal_p (x, y);
  337. case scm_tc11_bytevector:
  338. return scm_bytevector_eq_p (x, y);
  339. case scm_tc11_array:
  340. return scm_array_equal_p (x, y);
  341. case scm_tc11_bitvector:
  342. return scm_i_bitvector_equal_p (x, y);
  343. case scm_tc11_vector:
  344. case scm_tc11_wvect:
  345. return scm_i_vector_equal_p (x, y);
  346. case scm_tc11_syntax:
  347. if (scm_is_false (scm_equal_p (scm_syntax_wrap (x),
  348. scm_syntax_wrap (y))))
  349. return SCM_BOOL_F;
  350. if (scm_is_false (scm_equal_p (scm_syntax_module (x),
  351. scm_syntax_module (y))))
  352. return SCM_BOOL_F;
  353. x = scm_syntax_expression (x);
  354. y = scm_syntax_expression (y);
  355. goto tailrecurse;
  356. }
  357. /* Otherwise just return false. Dispatching to the generic is the wrong thing
  358. here, as we can hit this case for any two objects of the same type that we
  359. think are distinct, like different symbols. */
  360. return SCM_BOOL_F;
  361. generic_equal:
  362. if (SCM_UNPACK (g_scm_i_equal_p))
  363. return scm_call_2 (g_scm_i_equal_p, x, y);
  364. else
  365. return SCM_BOOL_F;
  366. }
  367. #undef FUNC_NAME
  368. void
  369. scm_init_eq ()
  370. {
  371. #include "eq.x"
  372. }