memoize.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011
  2. * Free Software Foundation, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public License
  6. * as published by the Free Software Foundation; either version 3 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301 USA
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include "libguile/__scm.h"
  23. #include "libguile/_scm.h"
  24. #include "libguile/continuations.h"
  25. #include "libguile/eq.h"
  26. #include "libguile/expand.h"
  27. #include "libguile/list.h"
  28. #include "libguile/macros.h"
  29. #include "libguile/memoize.h"
  30. #include "libguile/modules.h"
  31. #include "libguile/srcprop.h"
  32. #include "libguile/ports.h"
  33. #include "libguile/print.h"
  34. #include "libguile/strings.h"
  35. #include "libguile/throw.h"
  36. #include "libguile/validate.h"
  37. #define CAR(x) SCM_CAR(x)
  38. #define CDR(x) SCM_CDR(x)
  39. #define CAAR(x) SCM_CAAR(x)
  40. #define CADR(x) SCM_CADR(x)
  41. #define CDAR(x) SCM_CDAR(x)
  42. #define CDDR(x) SCM_CDDR(x)
  43. #define CADDR(x) SCM_CADDR(x)
  44. #define CDDDR(x) SCM_CDDDR(x)
  45. #define CADDDR(x) SCM_CADDDR(x)
  46. SCM_SYMBOL (sym_case_lambda_star, "case-lambda*");
  47. /* {Evaluator memoized expressions}
  48. */
  49. scm_t_bits scm_tc16_memoized;
  50. #define MAKMEMO(n, args) \
  51. (scm_cell (scm_tc16_memoized | ((n) << 16), SCM_UNPACK (args)))
  52. #define MAKMEMO_BEGIN(exps) \
  53. MAKMEMO (SCM_M_BEGIN, exps)
  54. #define MAKMEMO_IF(test, then, else_) \
  55. MAKMEMO (SCM_M_IF, scm_cons (test, scm_cons (then, else_)))
  56. #define FIXED_ARITY(nreq) \
  57. scm_list_1 (SCM_I_MAKINUM (nreq))
  58. #define REST_ARITY(nreq, rest) \
  59. scm_list_2 (SCM_I_MAKINUM (nreq), rest)
  60. #define FULL_ARITY(nreq, rest, nopt, kw, inits, alt) \
  61. scm_list_n (SCM_I_MAKINUM (nreq), rest, SCM_I_MAKINUM (nopt), kw, inits, \
  62. alt, SCM_UNDEFINED)
  63. #define MAKMEMO_LAMBDA(body, arity) \
  64. MAKMEMO (SCM_M_LAMBDA, (scm_cons (body, arity)))
  65. #define MAKMEMO_LET(inits, body) \
  66. MAKMEMO (SCM_M_LET, scm_cons (inits, body))
  67. #define MAKMEMO_QUOTE(exp) \
  68. MAKMEMO (SCM_M_QUOTE, exp)
  69. #define MAKMEMO_DEFINE(var, val) \
  70. MAKMEMO (SCM_M_DEFINE, scm_cons (var, val))
  71. #define MAKMEMO_DYNWIND(in, expr, out) \
  72. MAKMEMO (SCM_M_DYNWIND, scm_cons (in, scm_cons (expr, out)))
  73. #define MAKMEMO_WITH_FLUIDS(fluids, vals, expr) \
  74. MAKMEMO (SCM_M_WITH_FLUIDS, scm_cons (fluids, scm_cons (vals, expr)))
  75. #define MAKMEMO_APPLY(proc, args)\
  76. MAKMEMO (SCM_M_APPLY, scm_list_2 (proc, args))
  77. #define MAKMEMO_CONT(proc) \
  78. MAKMEMO (SCM_M_CONT, proc)
  79. #define MAKMEMO_CALL_WITH_VALUES(prod, cons) \
  80. MAKMEMO (SCM_M_CALL_WITH_VALUES, scm_cons (prod, cons))
  81. #define MAKMEMO_CALL(proc, nargs, args) \
  82. MAKMEMO (SCM_M_CALL, scm_cons (proc, scm_cons (SCM_I_MAKINUM (nargs), args)))
  83. #define MAKMEMO_LEX_REF(n) \
  84. MAKMEMO (SCM_M_LEXICAL_REF, SCM_I_MAKINUM (n))
  85. #define MAKMEMO_LEX_SET(n, val) \
  86. MAKMEMO (SCM_M_LEXICAL_SET, scm_cons (SCM_I_MAKINUM (n), val))
  87. #define MAKMEMO_TOP_REF(var) \
  88. MAKMEMO (SCM_M_TOPLEVEL_REF, var)
  89. #define MAKMEMO_TOP_SET(var, val) \
  90. MAKMEMO (SCM_M_TOPLEVEL_SET, scm_cons (var, val))
  91. #define MAKMEMO_MOD_REF(mod, var, public) \
  92. MAKMEMO (SCM_M_MODULE_REF, scm_cons (mod, scm_cons (var, public)))
  93. #define MAKMEMO_MOD_SET(val, mod, var, public) \
  94. MAKMEMO (SCM_M_MODULE_SET, scm_cons (val, scm_cons (mod, scm_cons (var, public))))
  95. #define MAKMEMO_PROMPT(tag, exp, handler) \
  96. MAKMEMO (SCM_M_PROMPT, scm_cons (tag, scm_cons (exp, handler)))
  97. /* Primitives for the evaluator */
  98. scm_t_bits scm_tc16_memoizer;
  99. #define SCM_MEMOIZER_P(x) (SCM_SMOB_PREDICATE (scm_tc16_memoizer, (x)))
  100. #define SCM_MEMOIZER(M) (SCM_SMOB_OBJECT_1 (M))
  101. /* This table must agree with the list of M_ constants in memoize.h */
  102. static const char *const memoized_tags[] =
  103. {
  104. "begin",
  105. "if",
  106. "lambda",
  107. "let",
  108. "quote",
  109. "define",
  110. "dynwind",
  111. "with-fluids",
  112. "apply",
  113. "call/cc",
  114. "call-with-values",
  115. "call",
  116. "lexical-ref",
  117. "lexical-set!",
  118. "toplevel-ref",
  119. "toplevel-set!",
  120. "module-ref",
  121. "module-set!",
  122. "prompt",
  123. };
  124. static int
  125. scm_print_memoized (SCM memoized, SCM port, scm_print_state *pstate)
  126. {
  127. scm_puts ("#<memoized ", port);
  128. scm_write (scm_unmemoize_expression (memoized), port);
  129. scm_puts (">", port);
  130. return 1;
  131. }
  132. static int
  133. lookup (SCM x, SCM env)
  134. {
  135. int i = 0;
  136. for (; scm_is_pair (env); env = CDR (env), i++)
  137. if (scm_is_eq (x, CAR (env)))
  138. return i; /* bound */
  139. abort ();
  140. }
  141. /* Abbreviate SCM_EXPANDED_REF. Copied because I'm not sure about symbol pasting */
  142. #define REF(x,type,field) \
  143. (scm_struct_ref (x, SCM_I_MAKINUM (SCM_EXPANDED_##type##_##field)))
  144. static SCM list_of_guile = SCM_BOOL_F;
  145. static SCM memoize (SCM exp, SCM env);
  146. static SCM
  147. memoize_exps (SCM exps, SCM env)
  148. {
  149. SCM ret;
  150. for (ret = SCM_EOL; scm_is_pair (exps); exps = CDR (exps))
  151. ret = scm_cons (memoize (CAR (exps), env), ret);
  152. return scm_reverse_x (ret, SCM_UNDEFINED);
  153. }
  154. static SCM
  155. memoize (SCM exp, SCM env)
  156. {
  157. if (!SCM_EXPANDED_P (exp))
  158. abort ();
  159. switch (SCM_EXPANDED_TYPE (exp))
  160. {
  161. case SCM_EXPANDED_VOID:
  162. return MAKMEMO_QUOTE (SCM_UNSPECIFIED);
  163. case SCM_EXPANDED_CONST:
  164. return MAKMEMO_QUOTE (REF (exp, CONST, EXP));
  165. case SCM_EXPANDED_PRIMITIVE_REF:
  166. if (scm_is_eq (scm_current_module (), scm_the_root_module ()))
  167. return MAKMEMO_TOP_REF (REF (exp, PRIMITIVE_REF, NAME));
  168. else
  169. return MAKMEMO_MOD_REF (list_of_guile, REF (exp, PRIMITIVE_REF, NAME),
  170. SCM_BOOL_F);
  171. case SCM_EXPANDED_LEXICAL_REF:
  172. return MAKMEMO_LEX_REF (lookup (REF (exp, LEXICAL_REF, GENSYM), env));
  173. case SCM_EXPANDED_LEXICAL_SET:
  174. return MAKMEMO_LEX_SET (lookup (REF (exp, LEXICAL_SET, GENSYM), env),
  175. memoize (REF (exp, LEXICAL_SET, EXP), env));
  176. case SCM_EXPANDED_MODULE_REF:
  177. return MAKMEMO_MOD_REF (REF (exp, MODULE_REF, MOD),
  178. REF (exp, MODULE_REF, NAME),
  179. REF (exp, MODULE_REF, PUBLIC));
  180. case SCM_EXPANDED_MODULE_SET:
  181. return MAKMEMO_MOD_SET (memoize (REF (exp, MODULE_SET, EXP), env),
  182. REF (exp, MODULE_SET, MOD),
  183. REF (exp, MODULE_SET, NAME),
  184. REF (exp, MODULE_SET, PUBLIC));
  185. case SCM_EXPANDED_TOPLEVEL_REF:
  186. return MAKMEMO_TOP_REF (REF (exp, TOPLEVEL_REF, NAME));
  187. case SCM_EXPANDED_TOPLEVEL_SET:
  188. return MAKMEMO_TOP_SET (REF (exp, TOPLEVEL_SET, NAME),
  189. memoize (REF (exp, TOPLEVEL_SET, EXP), env));
  190. case SCM_EXPANDED_TOPLEVEL_DEFINE:
  191. return MAKMEMO_DEFINE (REF (exp, TOPLEVEL_DEFINE, NAME),
  192. memoize (REF (exp, TOPLEVEL_DEFINE, EXP), env));
  193. case SCM_EXPANDED_CONDITIONAL:
  194. return MAKMEMO_IF (memoize (REF (exp, CONDITIONAL, TEST), env),
  195. memoize (REF (exp, CONDITIONAL, CONSEQUENT), env),
  196. memoize (REF (exp, CONDITIONAL, ALTERNATE), env));
  197. case SCM_EXPANDED_APPLICATION:
  198. {
  199. SCM proc, args;
  200. proc = REF (exp, APPLICATION, PROC);
  201. args = memoize_exps (REF (exp, APPLICATION, ARGS), env);
  202. if (SCM_EXPANDED_TYPE (proc) == SCM_EXPANDED_TOPLEVEL_REF)
  203. {
  204. SCM var = scm_module_variable (scm_current_module (),
  205. REF (proc, TOPLEVEL_REF, NAME));
  206. if (SCM_VARIABLEP (var))
  207. {
  208. SCM val = SCM_VARIABLE_REF (var);
  209. if (SCM_MEMOIZER_P (val))
  210. return scm_apply (SCM_SMOB_OBJECT_1 (val), args, SCM_EOL);
  211. }
  212. }
  213. /* otherwise we all fall down here */
  214. return MAKMEMO_CALL (memoize (proc, env), scm_ilength (args), args);
  215. }
  216. case SCM_EXPANDED_SEQUENCE:
  217. return MAKMEMO_BEGIN (memoize_exps (REF (exp, SEQUENCE, EXPS), env));
  218. case SCM_EXPANDED_LAMBDA:
  219. /* The body will be a lambda-case. */
  220. return memoize (REF (exp, LAMBDA, BODY), env);
  221. case SCM_EXPANDED_LAMBDA_CASE:
  222. {
  223. SCM req, rest, opt, kw, inits, vars, body, alt;
  224. SCM walk, minits, arity, new_env;
  225. int nreq, nopt, ntotal;
  226. req = REF (exp, LAMBDA_CASE, REQ);
  227. rest = REF (exp, LAMBDA_CASE, REST);
  228. opt = REF (exp, LAMBDA_CASE, OPT);
  229. kw = REF (exp, LAMBDA_CASE, KW);
  230. inits = REF (exp, LAMBDA_CASE, INITS);
  231. vars = REF (exp, LAMBDA_CASE, GENSYMS);
  232. body = REF (exp, LAMBDA_CASE, BODY);
  233. alt = REF (exp, LAMBDA_CASE, ALTERNATE);
  234. nreq = scm_ilength (req);
  235. nopt = scm_is_pair (opt) ? scm_ilength (opt) : 0;
  236. ntotal = scm_ilength (vars);
  237. /* The vars are the gensyms, according to the divine plan. But we need
  238. to memoize the inits within their appropriate environment,
  239. complicating things. */
  240. new_env = env;
  241. for (walk = req; scm_is_pair (walk);
  242. walk = CDR (walk), vars = CDR (vars))
  243. new_env = scm_cons (CAR (vars), new_env);
  244. minits = SCM_EOL;
  245. for (walk = opt; scm_is_pair (walk);
  246. walk = CDR (walk), vars = CDR (vars), inits = CDR (inits))
  247. {
  248. minits = scm_cons (memoize (CAR (inits), new_env), minits);
  249. new_env = scm_cons (CAR (vars), new_env);
  250. }
  251. if (scm_is_true (rest))
  252. {
  253. new_env = scm_cons (CAR (vars), new_env);
  254. vars = CDR (vars);
  255. }
  256. for (; scm_is_pair (inits); vars = CDR (vars), inits = CDR (inits))
  257. {
  258. minits = scm_cons (memoize (CAR (inits), new_env), minits);
  259. new_env = scm_cons (CAR (vars), new_env);
  260. }
  261. if (!scm_is_null (vars))
  262. abort ();
  263. minits = scm_reverse_x (minits, SCM_UNDEFINED);
  264. if (scm_is_true (kw))
  265. {
  266. /* (aok? (kw name sym) ...) -> (aok? (kw . index) ...) */
  267. SCM aok = CAR (kw), indices = SCM_EOL;
  268. for (kw = CDR (kw); scm_is_pair (kw); kw = CDR (kw))
  269. {
  270. SCM k;
  271. int idx;
  272. k = CAR (CAR (kw));
  273. idx = ntotal - 1 - lookup (CADDR (CAR (kw)), new_env);
  274. indices = scm_acons (k, SCM_I_MAKINUM (idx), indices);
  275. }
  276. kw = scm_cons (aok, scm_reverse_x (indices, SCM_UNDEFINED));
  277. }
  278. if (scm_is_false (alt) && scm_is_false (kw) && scm_is_false (opt))
  279. {
  280. if (scm_is_false (rest))
  281. arity = FIXED_ARITY (nreq);
  282. else
  283. arity = REST_ARITY (nreq, SCM_BOOL_T);
  284. }
  285. else if (scm_is_true (alt))
  286. arity = FULL_ARITY (nreq, rest, nopt, kw, minits,
  287. SCM_MEMOIZED_ARGS (memoize (alt, env)));
  288. else
  289. arity = FULL_ARITY (nreq, rest, nopt, kw, minits, SCM_BOOL_F);
  290. return MAKMEMO_LAMBDA (memoize (body, new_env), arity);
  291. }
  292. case SCM_EXPANDED_LET:
  293. {
  294. SCM vars, exps, body, inits, new_env;
  295. vars = REF (exp, LET, GENSYMS);
  296. exps = REF (exp, LET, VALS);
  297. body = REF (exp, LET, BODY);
  298. inits = SCM_EOL;
  299. new_env = env;
  300. for (; scm_is_pair (vars); vars = CDR (vars), exps = CDR (exps))
  301. {
  302. new_env = scm_cons (CAR (vars), new_env);
  303. inits = scm_cons (memoize (CAR (exps), env), inits);
  304. }
  305. return MAKMEMO_LET (scm_reverse_x (inits, SCM_UNDEFINED),
  306. memoize (body, new_env));
  307. }
  308. case SCM_EXPANDED_LETREC:
  309. {
  310. SCM vars, exps, body, undefs, new_env;
  311. int i, nvars, in_order_p;
  312. vars = REF (exp, LETREC, GENSYMS);
  313. exps = REF (exp, LETREC, VALS);
  314. body = REF (exp, LETREC, BODY);
  315. in_order_p = scm_is_true (REF (exp, LETREC, IN_ORDER_P));
  316. nvars = i = scm_ilength (vars);
  317. undefs = SCM_EOL;
  318. new_env = env;
  319. for (; scm_is_pair (vars); vars = CDR (vars))
  320. {
  321. new_env = scm_cons (CAR (vars), new_env);
  322. undefs = scm_cons (MAKMEMO_QUOTE (SCM_UNDEFINED), undefs);
  323. }
  324. if (in_order_p)
  325. {
  326. SCM body_exps = SCM_EOL;
  327. for (; scm_is_pair (exps); exps = CDR (exps), i--)
  328. body_exps = scm_cons (MAKMEMO_LEX_SET (i-1,
  329. memoize (CAR (exps), new_env)),
  330. body_exps);
  331. body_exps = scm_cons (memoize (body, new_env), body_exps);
  332. body_exps = scm_reverse_x (body_exps, SCM_UNDEFINED);
  333. return MAKMEMO_LET (undefs, MAKMEMO_BEGIN (body_exps));
  334. }
  335. else
  336. {
  337. SCM sets = SCM_EOL, inits = SCM_EOL;
  338. for (; scm_is_pair (exps); exps = CDR (exps), i--)
  339. {
  340. sets = scm_cons (MAKMEMO_LEX_SET ((i-1) + nvars,
  341. MAKMEMO_LEX_REF (i-1)),
  342. sets);
  343. inits = scm_cons (memoize (CAR (exps), new_env), inits);
  344. }
  345. inits = scm_reverse_x (inits, SCM_UNDEFINED);
  346. return MAKMEMO_LET
  347. (undefs,
  348. MAKMEMO_BEGIN (scm_list_2 (MAKMEMO_LET (inits, MAKMEMO_BEGIN (sets)),
  349. memoize (body, new_env))));
  350. }
  351. }
  352. case SCM_EXPANDED_DYNLET:
  353. return MAKMEMO_WITH_FLUIDS (memoize_exps (REF (exp, DYNLET, FLUIDS), env),
  354. memoize_exps (REF (exp, DYNLET, VALS), env),
  355. memoize (REF (exp, DYNLET, BODY), env));
  356. default:
  357. abort ();
  358. }
  359. }
  360. SCM_DEFINE (scm_memoize_expression, "memoize-expression", 1, 0, 0,
  361. (SCM exp),
  362. "Memoize the expression @var{exp}.")
  363. #define FUNC_NAME s_scm_memoize_expression
  364. {
  365. SCM_ASSERT_TYPE (SCM_EXPANDED_P (exp), exp, 1, FUNC_NAME, "expanded");
  366. return memoize (exp, scm_current_module ());
  367. }
  368. #undef FUNC_NAME
  369. #define SCM_MAKE_MEMOIZER(STR, MEMOIZER, N) \
  370. (scm_cell (scm_tc16_memoizer, \
  371. SCM_UNPACK (scm_c_make_gsubr (STR, N, 0, 0, MEMOIZER))))
  372. #define SCM_DEFINE_MEMOIZER(STR, MEMOIZER, N) \
  373. SCM_SNARF_INIT(scm_c_define (STR, SCM_MAKE_MEMOIZER (STR, MEMOIZER, N)))
  374. #define SCM_MAKE_REST_MEMOIZER(STR, MEMOIZER, N) \
  375. (scm_cell (scm_tc16_memoizer, \
  376. SCM_UNPACK ((scm_c_make_gsubr (STR, N, 0, 1, MEMOIZER)))))
  377. #define SCM_DEFINE_REST_MEMOIZER(STR, MEMOIZER, N) \
  378. SCM_SNARF_INIT(scm_c_define (STR, SCM_MAKE_REST_MEMOIZER (STR, MEMOIZER, N)))
  379. static SCM m_apply (SCM proc, SCM arg, SCM rest);
  380. static SCM m_call_cc (SCM proc);
  381. static SCM m_call_values (SCM prod, SCM cons);
  382. static SCM m_dynamic_wind (SCM pre, SCM exp, SCM post);
  383. static SCM m_prompt (SCM tag, SCM exp, SCM handler);
  384. SCM_DEFINE_REST_MEMOIZER ("@apply", m_apply, 2);
  385. SCM_DEFINE_MEMOIZER ("@call-with-current-continuation", m_call_cc, 1);
  386. SCM_DEFINE_MEMOIZER ("@call-with-values", m_call_values, 2);
  387. SCM_DEFINE_MEMOIZER ("@dynamic-wind", m_dynamic_wind, 3);
  388. SCM_DEFINE_MEMOIZER ("@prompt", m_prompt, 3);
  389. static SCM m_apply (SCM proc, SCM arg, SCM rest)
  390. #define FUNC_NAME "@apply"
  391. {
  392. long len;
  393. SCM_VALIDATE_MEMOIZED (1, proc);
  394. SCM_VALIDATE_MEMOIZED (2, arg);
  395. len = scm_ilength (rest);
  396. if (len < 0)
  397. abort ();
  398. else if (len == 0)
  399. return MAKMEMO_APPLY (proc, arg);
  400. else
  401. {
  402. SCM tail;
  403. rest = scm_reverse (rest);
  404. tail = scm_car (rest);
  405. rest = scm_cdr (rest);
  406. len--;
  407. while (scm_is_pair (rest))
  408. {
  409. tail = MAKMEMO_CALL (MAKMEMO_MOD_REF (scm_list_1 (scm_from_latin1_symbol ("guile")),
  410. scm_from_latin1_symbol ("cons"),
  411. SCM_BOOL_F),
  412. 2,
  413. scm_list_2 (scm_car (rest), tail));
  414. rest = scm_cdr (rest);
  415. }
  416. return MAKMEMO_APPLY (proc, tail);
  417. }
  418. }
  419. #undef FUNC_NAME
  420. static SCM m_call_cc (SCM proc)
  421. #define FUNC_NAME "@call-with-current-continuation"
  422. {
  423. SCM_VALIDATE_MEMOIZED (1, proc);
  424. return MAKMEMO_CONT (proc);
  425. }
  426. #undef FUNC_NAME
  427. static SCM m_call_values (SCM prod, SCM cons)
  428. #define FUNC_NAME "@call-with-values"
  429. {
  430. SCM_VALIDATE_MEMOIZED (1, prod);
  431. SCM_VALIDATE_MEMOIZED (2, cons);
  432. return MAKMEMO_CALL_WITH_VALUES (prod, cons);
  433. }
  434. #undef FUNC_NAME
  435. static SCM m_dynamic_wind (SCM in, SCM expr, SCM out)
  436. #define FUNC_NAME "memoize-dynwind"
  437. {
  438. SCM_VALIDATE_MEMOIZED (1, in);
  439. SCM_VALIDATE_MEMOIZED (2, expr);
  440. SCM_VALIDATE_MEMOIZED (3, out);
  441. return MAKMEMO_DYNWIND (in, expr, out);
  442. }
  443. #undef FUNC_NAME
  444. static SCM m_prompt (SCM tag, SCM exp, SCM handler)
  445. #define FUNC_NAME "@prompt"
  446. {
  447. SCM_VALIDATE_MEMOIZED (1, tag);
  448. SCM_VALIDATE_MEMOIZED (2, exp);
  449. SCM_VALIDATE_MEMOIZED (3, handler);
  450. return MAKMEMO_PROMPT (tag, exp, handler);
  451. }
  452. #undef FUNC_NAME
  453. SCM_SYMBOL (sym_placeholder, "_");
  454. static SCM unmemoize (SCM expr);
  455. static SCM
  456. unmemoize_exprs (SCM exprs)
  457. {
  458. SCM ret, tail;
  459. if (scm_is_null (exprs))
  460. return SCM_EOL;
  461. ret = scm_list_1 (unmemoize (CAR (exprs)));
  462. tail = ret;
  463. for (exprs = CDR (exprs); !scm_is_null (exprs); exprs = CDR (exprs))
  464. {
  465. SCM_SETCDR (tail, scm_list_1 (unmemoize (CAR (exprs))));
  466. tail = CDR (tail);
  467. }
  468. return ret;
  469. }
  470. static SCM
  471. unmemoize_bindings (SCM inits)
  472. {
  473. SCM ret, tail;
  474. if (scm_is_null (inits))
  475. return SCM_EOL;
  476. ret = scm_list_1 (scm_list_2 (sym_placeholder, unmemoize (CAR (inits))));
  477. tail = ret;
  478. for (inits = CDR (inits); !scm_is_null (inits); inits = CDR (inits))
  479. {
  480. SCM_SETCDR (tail, scm_list_1 (scm_list_2 (sym_placeholder,
  481. unmemoize (CAR (inits)))));
  482. tail = CDR (tail);
  483. }
  484. return ret;
  485. }
  486. static SCM
  487. unmemoize_lexical (SCM n)
  488. {
  489. char buf[16];
  490. buf[15] = 0;
  491. snprintf (buf, 15, "<%u>", scm_to_uint32 (n));
  492. return scm_from_locale_symbol (buf);
  493. }
  494. static SCM
  495. unmemoize (const SCM expr)
  496. {
  497. SCM args;
  498. if (!SCM_MEMOIZED_P (expr))
  499. abort ();
  500. args = SCM_MEMOIZED_ARGS (expr);
  501. switch (SCM_MEMOIZED_TAG (expr))
  502. {
  503. case SCM_M_APPLY:
  504. return scm_cons (scm_sym_atapply, unmemoize_exprs (args));
  505. case SCM_M_BEGIN:
  506. return scm_cons (scm_sym_begin, unmemoize_exprs (args));
  507. case SCM_M_CALL:
  508. return scm_cons (unmemoize (CAR (args)), unmemoize_exprs (CDDR (args)));
  509. case SCM_M_CONT:
  510. return scm_list_2 (scm_sym_atcall_cc, unmemoize (args));
  511. case SCM_M_CALL_WITH_VALUES:
  512. return scm_list_3 (scm_sym_at_call_with_values,
  513. unmemoize (CAR (args)), unmemoize (CDR (args)));
  514. case SCM_M_DEFINE:
  515. return scm_list_3 (scm_sym_define, CAR (args), unmemoize (CDR (args)));
  516. case SCM_M_DYNWIND:
  517. return scm_list_4 (scm_sym_at_dynamic_wind,
  518. unmemoize (CAR (args)),
  519. unmemoize (CADR (args)),
  520. unmemoize (CDDR (args)));
  521. case SCM_M_WITH_FLUIDS:
  522. {
  523. SCM binds = SCM_EOL, fluids, vals;
  524. for (fluids = CAR (args), vals = CADR (args); scm_is_pair (fluids);
  525. fluids = CDR (fluids), vals = CDR (vals))
  526. binds = scm_cons (scm_list_2 (unmemoize (CAR (fluids)),
  527. unmemoize (CAR (vals))),
  528. binds);
  529. return scm_list_3 (scm_sym_with_fluids,
  530. scm_reverse_x (binds, SCM_UNDEFINED),
  531. unmemoize (CDDR (args)));
  532. }
  533. case SCM_M_IF:
  534. return scm_list_4 (scm_sym_if, unmemoize (scm_car (args)),
  535. unmemoize (scm_cadr (args)), unmemoize (scm_cddr (args)));
  536. case SCM_M_LAMBDA:
  537. if (scm_is_null (CDDR (args)))
  538. return scm_list_3 (scm_sym_lambda,
  539. scm_make_list (CADR (args), sym_placeholder),
  540. unmemoize (CAR (args)));
  541. else if (scm_is_null (CDDDR (args)))
  542. {
  543. SCM formals = scm_make_list (CADR (args), sym_placeholder);
  544. return scm_list_3 (scm_sym_lambda,
  545. scm_is_true (CADDR (args))
  546. ? scm_cons_star (sym_placeholder, formals)
  547. : formals,
  548. unmemoize (CAR (args)));
  549. }
  550. else
  551. {
  552. SCM body = CAR (args), spec = CDR (args), alt, tail;
  553. alt = CADDR (CDDDR (spec));
  554. if (scm_is_true (alt))
  555. tail = CDR (unmemoize (alt));
  556. else
  557. tail = SCM_EOL;
  558. return scm_cons
  559. (sym_case_lambda_star,
  560. scm_cons (scm_list_2 (scm_list_5 (CAR (spec),
  561. CADR (spec),
  562. CADDR (spec),
  563. CADDDR (spec),
  564. unmemoize_exprs (CADR (CDDDR (spec)))),
  565. unmemoize (body)),
  566. tail));
  567. }
  568. case SCM_M_LET:
  569. return scm_list_3 (scm_sym_let,
  570. unmemoize_bindings (CAR (args)),
  571. unmemoize (CDR (args)));
  572. case SCM_M_QUOTE:
  573. return scm_list_2 (scm_sym_quote, args);
  574. case SCM_M_LEXICAL_REF:
  575. return unmemoize_lexical (args);
  576. case SCM_M_LEXICAL_SET:
  577. return scm_list_3 (scm_sym_set_x, unmemoize_lexical (CAR (args)),
  578. unmemoize (CDR (args)));
  579. case SCM_M_TOPLEVEL_REF:
  580. return args;
  581. case SCM_M_TOPLEVEL_SET:
  582. return scm_list_3 (scm_sym_set_x, CAR (args), unmemoize (CDR (args)));
  583. case SCM_M_MODULE_REF:
  584. return SCM_VARIABLEP (args) ? args
  585. : scm_list_3 (scm_is_true (CDDR (args)) ? scm_sym_at : scm_sym_atat,
  586. scm_i_finite_list_copy (CAR (args)),
  587. CADR (args));
  588. case SCM_M_MODULE_SET:
  589. return scm_list_3 (scm_sym_set_x,
  590. SCM_VARIABLEP (CDR (args)) ? CDR (args)
  591. : scm_list_3 (scm_is_true (CDDDR (args))
  592. ? scm_sym_at : scm_sym_atat,
  593. scm_i_finite_list_copy (CADR (args)),
  594. CADDR (args)),
  595. unmemoize (CAR (args)));
  596. case SCM_M_PROMPT:
  597. return scm_list_4 (scm_sym_at_prompt,
  598. unmemoize (CAR (args)),
  599. unmemoize (CADR (args)),
  600. unmemoize (CDDR (args)));
  601. default:
  602. abort ();
  603. }
  604. }
  605. SCM_DEFINE (scm_memoized_p, "memoized?", 1, 0, 0,
  606. (SCM obj),
  607. "Return @code{#t} if @var{obj} is memoized.")
  608. #define FUNC_NAME s_scm_memoized_p
  609. {
  610. return scm_from_bool (SCM_MEMOIZED_P (obj));
  611. }
  612. #undef FUNC_NAME
  613. SCM_DEFINE (scm_unmemoize_expression, "unmemoize-expression", 1, 0, 0,
  614. (SCM m),
  615. "Unmemoize the memoized expression @var{m}.")
  616. #define FUNC_NAME s_scm_unmemoize_expression
  617. {
  618. SCM_VALIDATE_MEMOIZED (1, m);
  619. return unmemoize (m);
  620. }
  621. #undef FUNC_NAME
  622. SCM_DEFINE (scm_memoized_expression_typecode, "memoized-expression-typecode", 1, 0, 0,
  623. (SCM m),
  624. "Return the typecode from the memoized expression @var{m}.")
  625. #define FUNC_NAME s_scm_memoized_expression_typecode
  626. {
  627. SCM_VALIDATE_MEMOIZED (1, m);
  628. /* The tag is a 16-bit integer so it fits in an inum. */
  629. return SCM_I_MAKINUM (SCM_MEMOIZED_TAG (m));
  630. }
  631. #undef FUNC_NAME
  632. SCM_DEFINE (scm_memoized_expression_data, "memoized-expression-data", 1, 0, 0,
  633. (SCM m),
  634. "Return the data from the memoized expression @var{m}.")
  635. #define FUNC_NAME s_scm_memoized_expression_data
  636. {
  637. SCM_VALIDATE_MEMOIZED (1, m);
  638. return SCM_MEMOIZED_ARGS (m);
  639. }
  640. #undef FUNC_NAME
  641. SCM_DEFINE (scm_memoized_typecode, "memoized-typecode", 1, 0, 0,
  642. (SCM sym),
  643. "Return the memoized typecode corresponding to the symbol @var{sym}.")
  644. #define FUNC_NAME s_scm_memoized_typecode
  645. {
  646. int i;
  647. SCM_VALIDATE_SYMBOL (1, sym);
  648. for (i = 0; i < sizeof(memoized_tags)/sizeof(const char*); i++)
  649. if (strcmp (scm_i_symbol_chars (sym), memoized_tags[i]) == 0)
  650. return scm_from_int32 (i);
  651. return SCM_BOOL_F;
  652. }
  653. #undef FUNC_NAME
  654. SCM_SYMBOL (scm_unbound_variable_key, "unbound-variable");
  655. static void error_unbound_variable (SCM symbol) SCM_NORETURN;
  656. static void error_unbound_variable (SCM symbol)
  657. {
  658. scm_error (scm_unbound_variable_key, NULL, "Unbound variable: ~S",
  659. scm_list_1 (symbol), SCM_BOOL_F);
  660. }
  661. SCM_DEFINE (scm_memoize_variable_access_x, "memoize-variable-access!", 2, 0, 0,
  662. (SCM m, SCM mod),
  663. "Look up and cache the variable that @var{m} will access, returning the variable.")
  664. #define FUNC_NAME s_scm_memoize_variable_access_x
  665. {
  666. SCM mx;
  667. SCM_VALIDATE_MEMOIZED (1, m);
  668. mx = SCM_MEMOIZED_ARGS (m);
  669. switch (SCM_MEMOIZED_TAG (m))
  670. {
  671. case SCM_M_TOPLEVEL_REF:
  672. if (SCM_VARIABLEP (mx))
  673. return mx;
  674. else
  675. {
  676. SCM var = scm_module_variable (mod, mx);
  677. if (scm_is_false (var) || scm_is_false (scm_variable_bound_p (var)))
  678. error_unbound_variable (mx);
  679. SCM_SET_SMOB_OBJECT (m, var);
  680. return var;
  681. }
  682. case SCM_M_TOPLEVEL_SET:
  683. {
  684. SCM var = CAR (mx);
  685. if (SCM_VARIABLEP (var))
  686. return var;
  687. else
  688. {
  689. var = scm_module_variable (mod, var);
  690. if (scm_is_false (var))
  691. error_unbound_variable (CAR (mx));
  692. SCM_SETCAR (mx, var);
  693. return var;
  694. }
  695. }
  696. case SCM_M_MODULE_REF:
  697. if (SCM_VARIABLEP (mx))
  698. return mx;
  699. else
  700. {
  701. SCM var;
  702. mod = scm_resolve_module (CAR (mx));
  703. if (scm_is_true (CDDR (mx)))
  704. mod = scm_module_public_interface (mod);
  705. var = scm_module_lookup (mod, CADR (mx));
  706. if (scm_is_false (scm_variable_bound_p (var)))
  707. error_unbound_variable (CADR (mx));
  708. SCM_SET_SMOB_OBJECT (m, var);
  709. return var;
  710. }
  711. case SCM_M_MODULE_SET:
  712. /* FIXME: not quite threadsafe */
  713. if (SCM_VARIABLEP (CDR (mx)))
  714. return CDR (mx);
  715. else
  716. {
  717. SCM var;
  718. mod = scm_resolve_module (CADR (mx));
  719. if (scm_is_true (CDDDR (mx)))
  720. mod = scm_module_public_interface (mod);
  721. var = scm_module_lookup (mod, CADDR (mx));
  722. SCM_SETCDR (mx, var);
  723. return var;
  724. }
  725. default:
  726. scm_wrong_type_arg (FUNC_NAME, 1, m);
  727. return SCM_BOOL_F;
  728. }
  729. }
  730. #undef FUNC_NAME
  731. void
  732. scm_init_memoize ()
  733. {
  734. scm_tc16_memoized = scm_make_smob_type ("%memoized", 0);
  735. scm_set_smob_mark (scm_tc16_memoized, scm_markcdr);
  736. scm_set_smob_print (scm_tc16_memoized, scm_print_memoized);
  737. scm_tc16_memoizer = scm_make_smob_type ("memoizer", 0);
  738. #include "libguile/memoize.x"
  739. list_of_guile = scm_list_1 (scm_from_latin1_symbol ("guile"));
  740. }
  741. /*
  742. Local Variables:
  743. c-file-style: "gnu"
  744. End:
  745. */