eq.c 9.6 KB

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