memoize.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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_SEQ(head,tail) \
  53. MAKMEMO (SCM_M_SEQ, scm_cons (head, tail))
  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. "seq",
  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_unlocked ("#<memoized ", port);
  128. scm_write (scm_unmemoize_expression (memoized), port);
  129. scm_puts_unlocked (">", 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_CALL:
  198. {
  199. SCM proc, args;
  200. proc = REF (exp, CALL, PROC);
  201. args = memoize_exps (REF (exp, CALL, 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_PRIMCALL:
  217. {
  218. SCM proc, args;
  219. if (scm_is_eq (scm_current_module (), scm_the_root_module ()))
  220. proc = MAKMEMO_TOP_REF (REF (exp, PRIMCALL, NAME));
  221. else
  222. proc = MAKMEMO_MOD_REF (list_of_guile, REF (exp, PRIMCALL, NAME),
  223. SCM_BOOL_F);
  224. args = memoize_exps (REF (exp, PRIMCALL, ARGS), env);
  225. return MAKMEMO_CALL (proc, scm_ilength (args), args);
  226. }
  227. case SCM_EXPANDED_SEQ:
  228. return MAKMEMO_SEQ (memoize (REF (exp, SEQ, HEAD), env),
  229. memoize (REF (exp, SEQ, TAIL), env));
  230. case SCM_EXPANDED_LAMBDA:
  231. /* The body will be a lambda-case. */
  232. return memoize (REF (exp, LAMBDA, BODY), env);
  233. case SCM_EXPANDED_LAMBDA_CASE:
  234. {
  235. SCM req, rest, opt, kw, inits, vars, body, alt;
  236. SCM walk, minits, arity, new_env;
  237. int nreq, nopt, ntotal;
  238. req = REF (exp, LAMBDA_CASE, REQ);
  239. rest = scm_not (scm_not (REF (exp, LAMBDA_CASE, REST)));
  240. opt = REF (exp, LAMBDA_CASE, OPT);
  241. kw = REF (exp, LAMBDA_CASE, KW);
  242. inits = REF (exp, LAMBDA_CASE, INITS);
  243. vars = REF (exp, LAMBDA_CASE, GENSYMS);
  244. body = REF (exp, LAMBDA_CASE, BODY);
  245. alt = REF (exp, LAMBDA_CASE, ALTERNATE);
  246. nreq = scm_ilength (req);
  247. nopt = scm_is_pair (opt) ? scm_ilength (opt) : 0;
  248. ntotal = scm_ilength (vars);
  249. /* The vars are the gensyms, according to the divine plan. But we need
  250. to memoize the inits within their appropriate environment,
  251. complicating things. */
  252. new_env = env;
  253. for (walk = req; scm_is_pair (walk);
  254. walk = CDR (walk), vars = CDR (vars))
  255. new_env = scm_cons (CAR (vars), new_env);
  256. minits = SCM_EOL;
  257. for (walk = opt; scm_is_pair (walk);
  258. walk = CDR (walk), vars = CDR (vars), inits = CDR (inits))
  259. {
  260. minits = scm_cons (memoize (CAR (inits), new_env), minits);
  261. new_env = scm_cons (CAR (vars), new_env);
  262. }
  263. if (scm_is_true (rest))
  264. {
  265. new_env = scm_cons (CAR (vars), new_env);
  266. vars = CDR (vars);
  267. }
  268. for (; scm_is_pair (inits); vars = CDR (vars), inits = CDR (inits))
  269. {
  270. minits = scm_cons (memoize (CAR (inits), new_env), minits);
  271. new_env = scm_cons (CAR (vars), new_env);
  272. }
  273. if (!scm_is_null (vars))
  274. abort ();
  275. minits = scm_reverse_x (minits, SCM_UNDEFINED);
  276. if (scm_is_true (kw))
  277. {
  278. /* (aok? (kw name sym) ...) -> (aok? (kw . index) ...) */
  279. SCM aok = CAR (kw), indices = SCM_EOL;
  280. for (kw = CDR (kw); scm_is_pair (kw); kw = CDR (kw))
  281. {
  282. SCM k;
  283. int idx;
  284. k = CAR (CAR (kw));
  285. idx = ntotal - 1 - lookup (CADDR (CAR (kw)), new_env);
  286. indices = scm_acons (k, SCM_I_MAKINUM (idx), indices);
  287. }
  288. kw = scm_cons (aok, scm_reverse_x (indices, SCM_UNDEFINED));
  289. }
  290. if (scm_is_false (alt) && scm_is_false (kw) && scm_is_false (opt))
  291. {
  292. if (scm_is_false (rest))
  293. arity = FIXED_ARITY (nreq);
  294. else
  295. arity = REST_ARITY (nreq, SCM_BOOL_T);
  296. }
  297. else if (scm_is_true (alt))
  298. arity = FULL_ARITY (nreq, rest, nopt, kw, minits,
  299. SCM_MEMOIZED_ARGS (memoize (alt, env)));
  300. else
  301. arity = FULL_ARITY (nreq, rest, nopt, kw, minits, SCM_BOOL_F);
  302. return MAKMEMO_LAMBDA (memoize (body, new_env), arity);
  303. }
  304. case SCM_EXPANDED_LET:
  305. {
  306. SCM vars, exps, body, inits, new_env;
  307. vars = REF (exp, LET, GENSYMS);
  308. exps = REF (exp, LET, VALS);
  309. body = REF (exp, LET, BODY);
  310. inits = SCM_EOL;
  311. new_env = env;
  312. for (; scm_is_pair (vars); vars = CDR (vars), exps = CDR (exps))
  313. {
  314. new_env = scm_cons (CAR (vars), new_env);
  315. inits = scm_cons (memoize (CAR (exps), env), inits);
  316. }
  317. return MAKMEMO_LET (scm_reverse_x (inits, SCM_UNDEFINED),
  318. memoize (body, new_env));
  319. }
  320. case SCM_EXPANDED_LETREC:
  321. {
  322. SCM vars, exps, body, undefs, new_env;
  323. int i, nvars, in_order_p;
  324. vars = REF (exp, LETREC, GENSYMS);
  325. exps = REF (exp, LETREC, VALS);
  326. body = REF (exp, LETREC, BODY);
  327. in_order_p = scm_is_true (REF (exp, LETREC, IN_ORDER_P));
  328. nvars = i = scm_ilength (vars);
  329. undefs = SCM_EOL;
  330. new_env = env;
  331. for (; scm_is_pair (vars); vars = CDR (vars))
  332. {
  333. new_env = scm_cons (CAR (vars), new_env);
  334. undefs = scm_cons (MAKMEMO_QUOTE (SCM_UNDEFINED), undefs);
  335. }
  336. if (in_order_p)
  337. {
  338. SCM body_exps = SCM_EOL, seq;
  339. for (; scm_is_pair (exps); exps = CDR (exps), i--)
  340. body_exps = scm_cons (MAKMEMO_LEX_SET (i-1,
  341. memoize (CAR (exps), new_env)),
  342. body_exps);
  343. seq = memoize (body, new_env);
  344. for (; scm_is_pair (body_exps); body_exps = CDR (body_exps))
  345. seq = MAKMEMO_SEQ (CAR (body_exps), seq);
  346. return MAKMEMO_LET (undefs, seq);
  347. }
  348. else
  349. {
  350. SCM sets = SCM_EOL, inits = SCM_EOL, set_seq;
  351. for (; scm_is_pair (exps); exps = CDR (exps), i--)
  352. {
  353. sets = scm_cons (MAKMEMO_LEX_SET ((i-1) + nvars,
  354. MAKMEMO_LEX_REF (i-1)),
  355. sets);
  356. inits = scm_cons (memoize (CAR (exps), new_env), inits);
  357. }
  358. inits = scm_reverse_x (inits, SCM_UNDEFINED);
  359. sets = scm_reverse_x (sets, SCM_UNDEFINED);
  360. if (scm_is_null (sets))
  361. return memoize (body, env);
  362. for (set_seq = CAR (sets), sets = CDR (sets); scm_is_pair (sets);
  363. sets = CDR (sets))
  364. set_seq = MAKMEMO_SEQ (CAR (sets), set_seq);
  365. return MAKMEMO_LET (undefs,
  366. MAKMEMO_SEQ (MAKMEMO_LET (inits, set_seq),
  367. memoize (body, new_env)));
  368. }
  369. }
  370. case SCM_EXPANDED_DYNLET:
  371. return MAKMEMO_WITH_FLUIDS (memoize_exps (REF (exp, DYNLET, FLUIDS), env),
  372. memoize_exps (REF (exp, DYNLET, VALS), env),
  373. memoize (REF (exp, DYNLET, BODY), env));
  374. default:
  375. abort ();
  376. }
  377. }
  378. SCM_DEFINE (scm_memoize_expression, "memoize-expression", 1, 0, 0,
  379. (SCM exp),
  380. "Memoize the expression @var{exp}.")
  381. #define FUNC_NAME s_scm_memoize_expression
  382. {
  383. SCM_ASSERT_TYPE (SCM_EXPANDED_P (exp), exp, 1, FUNC_NAME, "expanded");
  384. return memoize (exp, scm_current_module ());
  385. }
  386. #undef FUNC_NAME
  387. #define SCM_MAKE_MEMOIZER(STR, MEMOIZER, N) \
  388. (scm_cell (scm_tc16_memoizer, \
  389. SCM_UNPACK (scm_c_make_gsubr (STR, N, 0, 0, MEMOIZER))))
  390. #define SCM_DEFINE_MEMOIZER(STR, MEMOIZER, N) \
  391. SCM_SNARF_INIT(scm_c_define (STR, SCM_MAKE_MEMOIZER (STR, MEMOIZER, N)))
  392. #define SCM_MAKE_REST_MEMOIZER(STR, MEMOIZER, N) \
  393. (scm_cell (scm_tc16_memoizer, \
  394. SCM_UNPACK ((scm_c_make_gsubr (STR, N, 0, 1, MEMOIZER)))))
  395. #define SCM_DEFINE_REST_MEMOIZER(STR, MEMOIZER, N) \
  396. SCM_SNARF_INIT(scm_c_define (STR, SCM_MAKE_REST_MEMOIZER (STR, MEMOIZER, N)))
  397. static SCM m_apply (SCM proc, SCM arg, SCM rest);
  398. static SCM m_call_cc (SCM proc);
  399. static SCM m_call_values (SCM prod, SCM cons);
  400. static SCM m_dynamic_wind (SCM pre, SCM exp, SCM post);
  401. static SCM m_prompt (SCM tag, SCM exp, SCM handler);
  402. SCM_DEFINE_REST_MEMOIZER ("@apply", m_apply, 2);
  403. SCM_DEFINE_MEMOIZER ("@call-with-current-continuation", m_call_cc, 1);
  404. SCM_DEFINE_MEMOIZER ("@call-with-values", m_call_values, 2);
  405. SCM_DEFINE_MEMOIZER ("@dynamic-wind", m_dynamic_wind, 3);
  406. SCM_DEFINE_MEMOIZER ("@prompt", m_prompt, 3);
  407. static SCM m_apply (SCM proc, SCM arg, SCM rest)
  408. #define FUNC_NAME "@apply"
  409. {
  410. long len;
  411. SCM_VALIDATE_MEMOIZED (1, proc);
  412. SCM_VALIDATE_MEMOIZED (2, arg);
  413. len = scm_ilength (rest);
  414. if (len < 0)
  415. abort ();
  416. else if (len == 0)
  417. return MAKMEMO_APPLY (proc, arg);
  418. else
  419. {
  420. SCM tail;
  421. rest = scm_reverse (rest);
  422. tail = scm_car (rest);
  423. rest = scm_cdr (rest);
  424. len--;
  425. while (scm_is_pair (rest))
  426. {
  427. tail = MAKMEMO_CALL (MAKMEMO_MOD_REF (scm_list_1 (scm_from_latin1_symbol ("guile")),
  428. scm_from_latin1_symbol ("cons"),
  429. SCM_BOOL_F),
  430. 2,
  431. scm_list_2 (scm_car (rest), tail));
  432. rest = scm_cdr (rest);
  433. }
  434. return MAKMEMO_APPLY (proc, tail);
  435. }
  436. }
  437. #undef FUNC_NAME
  438. static SCM m_call_cc (SCM proc)
  439. #define FUNC_NAME "@call-with-current-continuation"
  440. {
  441. SCM_VALIDATE_MEMOIZED (1, proc);
  442. return MAKMEMO_CONT (proc);
  443. }
  444. #undef FUNC_NAME
  445. static SCM m_call_values (SCM prod, SCM cons)
  446. #define FUNC_NAME "@call-with-values"
  447. {
  448. SCM_VALIDATE_MEMOIZED (1, prod);
  449. SCM_VALIDATE_MEMOIZED (2, cons);
  450. return MAKMEMO_CALL_WITH_VALUES (prod, cons);
  451. }
  452. #undef FUNC_NAME
  453. static SCM m_dynamic_wind (SCM in, SCM expr, SCM out)
  454. #define FUNC_NAME "memoize-dynwind"
  455. {
  456. SCM_VALIDATE_MEMOIZED (1, in);
  457. SCM_VALIDATE_MEMOIZED (2, expr);
  458. SCM_VALIDATE_MEMOIZED (3, out);
  459. return MAKMEMO_DYNWIND (in, expr, out);
  460. }
  461. #undef FUNC_NAME
  462. static SCM m_prompt (SCM tag, SCM exp, SCM handler)
  463. #define FUNC_NAME "@prompt"
  464. {
  465. SCM_VALIDATE_MEMOIZED (1, tag);
  466. SCM_VALIDATE_MEMOIZED (2, exp);
  467. SCM_VALIDATE_MEMOIZED (3, handler);
  468. return MAKMEMO_PROMPT (tag, exp, handler);
  469. }
  470. #undef FUNC_NAME
  471. SCM_SYMBOL (sym_placeholder, "_");
  472. static SCM unmemoize (SCM expr);
  473. static SCM
  474. unmemoize_exprs (SCM exprs)
  475. {
  476. SCM ret, tail;
  477. if (scm_is_null (exprs))
  478. return SCM_EOL;
  479. ret = scm_list_1 (unmemoize (CAR (exprs)));
  480. tail = ret;
  481. for (exprs = CDR (exprs); !scm_is_null (exprs); exprs = CDR (exprs))
  482. {
  483. SCM_SETCDR (tail, scm_list_1 (unmemoize (CAR (exprs))));
  484. tail = CDR (tail);
  485. }
  486. return ret;
  487. }
  488. static SCM
  489. unmemoize_bindings (SCM inits)
  490. {
  491. SCM ret, tail;
  492. if (scm_is_null (inits))
  493. return SCM_EOL;
  494. ret = scm_list_1 (scm_list_2 (sym_placeholder, unmemoize (CAR (inits))));
  495. tail = ret;
  496. for (inits = CDR (inits); !scm_is_null (inits); inits = CDR (inits))
  497. {
  498. SCM_SETCDR (tail, scm_list_1 (scm_list_2 (sym_placeholder,
  499. unmemoize (CAR (inits)))));
  500. tail = CDR (tail);
  501. }
  502. return ret;
  503. }
  504. static SCM
  505. unmemoize_lexical (SCM n)
  506. {
  507. char buf[16];
  508. buf[15] = 0;
  509. snprintf (buf, 15, "<%u>", scm_to_uint32 (n));
  510. return scm_from_utf8_symbol (buf);
  511. }
  512. static SCM
  513. unmemoize (const SCM expr)
  514. {
  515. SCM args;
  516. if (!SCM_MEMOIZED_P (expr))
  517. abort ();
  518. args = SCM_MEMOIZED_ARGS (expr);
  519. switch (SCM_MEMOIZED_TAG (expr))
  520. {
  521. case SCM_M_APPLY:
  522. return scm_cons (scm_sym_atapply, unmemoize_exprs (args));
  523. case SCM_M_SEQ:
  524. return scm_list_3 (scm_sym_begin, unmemoize (CAR (args)),
  525. unmemoize (CDR (args)));
  526. case SCM_M_CALL:
  527. return scm_cons (unmemoize (CAR (args)), unmemoize_exprs (CDDR (args)));
  528. case SCM_M_CONT:
  529. return scm_list_2 (scm_sym_atcall_cc, unmemoize (args));
  530. case SCM_M_CALL_WITH_VALUES:
  531. return scm_list_3 (scm_sym_at_call_with_values,
  532. unmemoize (CAR (args)), unmemoize (CDR (args)));
  533. case SCM_M_DEFINE:
  534. return scm_list_3 (scm_sym_define, CAR (args), unmemoize (CDR (args)));
  535. case SCM_M_DYNWIND:
  536. return scm_list_4 (scm_sym_at_dynamic_wind,
  537. unmemoize (CAR (args)),
  538. unmemoize (CADR (args)),
  539. unmemoize (CDDR (args)));
  540. case SCM_M_WITH_FLUIDS:
  541. {
  542. SCM binds = SCM_EOL, fluids, vals;
  543. for (fluids = CAR (args), vals = CADR (args); scm_is_pair (fluids);
  544. fluids = CDR (fluids), vals = CDR (vals))
  545. binds = scm_cons (scm_list_2 (unmemoize (CAR (fluids)),
  546. unmemoize (CAR (vals))),
  547. binds);
  548. return scm_list_3 (scm_sym_with_fluids,
  549. scm_reverse_x (binds, SCM_UNDEFINED),
  550. unmemoize (CDDR (args)));
  551. }
  552. case SCM_M_IF:
  553. return scm_list_4 (scm_sym_if, unmemoize (scm_car (args)),
  554. unmemoize (scm_cadr (args)), unmemoize (scm_cddr (args)));
  555. case SCM_M_LAMBDA:
  556. if (scm_is_null (CDDR (args)))
  557. return scm_list_3 (scm_sym_lambda,
  558. scm_make_list (CADR (args), sym_placeholder),
  559. unmemoize (CAR (args)));
  560. else if (scm_is_null (CDDDR (args)))
  561. {
  562. SCM formals = scm_make_list (CADR (args), sym_placeholder);
  563. return scm_list_3 (scm_sym_lambda,
  564. scm_is_true (CADDR (args))
  565. ? scm_cons_star (sym_placeholder, formals)
  566. : formals,
  567. unmemoize (CAR (args)));
  568. }
  569. else
  570. {
  571. SCM body = CAR (args), spec = CDR (args), alt, tail;
  572. alt = CADDR (CDDDR (spec));
  573. if (scm_is_true (alt))
  574. tail = CDR (unmemoize (alt));
  575. else
  576. tail = SCM_EOL;
  577. return scm_cons
  578. (sym_case_lambda_star,
  579. scm_cons (scm_list_2 (scm_list_5 (CAR (spec),
  580. CADR (spec),
  581. CADDR (spec),
  582. CADDDR (spec),
  583. unmemoize_exprs (CADR (CDDDR (spec)))),
  584. unmemoize (body)),
  585. tail));
  586. }
  587. case SCM_M_LET:
  588. return scm_list_3 (scm_sym_let,
  589. unmemoize_bindings (CAR (args)),
  590. unmemoize (CDR (args)));
  591. case SCM_M_QUOTE:
  592. return scm_list_2 (scm_sym_quote, args);
  593. case SCM_M_LEXICAL_REF:
  594. return unmemoize_lexical (args);
  595. case SCM_M_LEXICAL_SET:
  596. return scm_list_3 (scm_sym_set_x, unmemoize_lexical (CAR (args)),
  597. unmemoize (CDR (args)));
  598. case SCM_M_TOPLEVEL_REF:
  599. return args;
  600. case SCM_M_TOPLEVEL_SET:
  601. return scm_list_3 (scm_sym_set_x, CAR (args), unmemoize (CDR (args)));
  602. case SCM_M_MODULE_REF:
  603. return SCM_VARIABLEP (args) ? args
  604. : scm_list_3 (scm_is_true (CDDR (args)) ? scm_sym_at : scm_sym_atat,
  605. scm_i_finite_list_copy (CAR (args)),
  606. CADR (args));
  607. case SCM_M_MODULE_SET:
  608. return scm_list_3 (scm_sym_set_x,
  609. SCM_VARIABLEP (CDR (args)) ? CDR (args)
  610. : scm_list_3 (scm_is_true (CDDDR (args))
  611. ? scm_sym_at : scm_sym_atat,
  612. scm_i_finite_list_copy (CADR (args)),
  613. CADDR (args)),
  614. unmemoize (CAR (args)));
  615. case SCM_M_PROMPT:
  616. return scm_list_4 (scm_sym_at_prompt,
  617. unmemoize (CAR (args)),
  618. unmemoize (CADR (args)),
  619. unmemoize (CDDR (args)));
  620. default:
  621. abort ();
  622. }
  623. }
  624. SCM_DEFINE (scm_memoized_p, "memoized?", 1, 0, 0,
  625. (SCM obj),
  626. "Return @code{#t} if @var{obj} is memoized.")
  627. #define FUNC_NAME s_scm_memoized_p
  628. {
  629. return scm_from_bool (SCM_MEMOIZED_P (obj));
  630. }
  631. #undef FUNC_NAME
  632. SCM_DEFINE (scm_unmemoize_expression, "unmemoize-expression", 1, 0, 0,
  633. (SCM m),
  634. "Unmemoize the memoized expression @var{m}.")
  635. #define FUNC_NAME s_scm_unmemoize_expression
  636. {
  637. SCM_VALIDATE_MEMOIZED (1, m);
  638. return unmemoize (m);
  639. }
  640. #undef FUNC_NAME
  641. SCM_DEFINE (scm_memoized_expression_typecode, "memoized-expression-typecode", 1, 0, 0,
  642. (SCM m),
  643. "Return the typecode from the memoized expression @var{m}.")
  644. #define FUNC_NAME s_scm_memoized_expression_typecode
  645. {
  646. SCM_VALIDATE_MEMOIZED (1, m);
  647. /* The tag is a 16-bit integer so it fits in an inum. */
  648. return SCM_I_MAKINUM (SCM_MEMOIZED_TAG (m));
  649. }
  650. #undef FUNC_NAME
  651. SCM_DEFINE (scm_memoized_expression_data, "memoized-expression-data", 1, 0, 0,
  652. (SCM m),
  653. "Return the data from the memoized expression @var{m}.")
  654. #define FUNC_NAME s_scm_memoized_expression_data
  655. {
  656. SCM_VALIDATE_MEMOIZED (1, m);
  657. return SCM_MEMOIZED_ARGS (m);
  658. }
  659. #undef FUNC_NAME
  660. SCM_DEFINE (scm_memoized_typecode, "memoized-typecode", 1, 0, 0,
  661. (SCM sym),
  662. "Return the memoized typecode corresponding to the symbol @var{sym}.")
  663. #define FUNC_NAME s_scm_memoized_typecode
  664. {
  665. int i;
  666. SCM_VALIDATE_SYMBOL (1, sym);
  667. for (i = 0; i < sizeof(memoized_tags)/sizeof(const char*); i++)
  668. if (strcmp (scm_i_symbol_chars (sym), memoized_tags[i]) == 0)
  669. return scm_from_int32 (i);
  670. return SCM_BOOL_F;
  671. }
  672. #undef FUNC_NAME
  673. SCM_SYMBOL (scm_unbound_variable_key, "unbound-variable");
  674. static void error_unbound_variable (SCM symbol) SCM_NORETURN;
  675. static void error_unbound_variable (SCM symbol)
  676. {
  677. scm_error (scm_unbound_variable_key, NULL, "Unbound variable: ~S",
  678. scm_list_1 (symbol), SCM_BOOL_F);
  679. }
  680. SCM_DEFINE (scm_memoize_variable_access_x, "memoize-variable-access!", 2, 0, 0,
  681. (SCM m, SCM mod),
  682. "Look up and cache the variable that @var{m} will access, returning the variable.")
  683. #define FUNC_NAME s_scm_memoize_variable_access_x
  684. {
  685. SCM mx;
  686. SCM_VALIDATE_MEMOIZED (1, m);
  687. mx = SCM_MEMOIZED_ARGS (m);
  688. switch (SCM_MEMOIZED_TAG (m))
  689. {
  690. case SCM_M_TOPLEVEL_REF:
  691. if (SCM_VARIABLEP (mx))
  692. return mx;
  693. else
  694. {
  695. SCM var = scm_module_variable (mod, mx);
  696. if (scm_is_false (var) || scm_is_false (scm_variable_bound_p (var)))
  697. error_unbound_variable (mx);
  698. SCM_SET_SMOB_OBJECT (m, var);
  699. return var;
  700. }
  701. case SCM_M_TOPLEVEL_SET:
  702. {
  703. SCM var = CAR (mx);
  704. if (SCM_VARIABLEP (var))
  705. return var;
  706. else
  707. {
  708. var = scm_module_variable (mod, var);
  709. if (scm_is_false (var))
  710. error_unbound_variable (CAR (mx));
  711. SCM_SETCAR (mx, var);
  712. return var;
  713. }
  714. }
  715. case SCM_M_MODULE_REF:
  716. if (SCM_VARIABLEP (mx))
  717. return mx;
  718. else
  719. {
  720. SCM var;
  721. mod = scm_resolve_module (CAR (mx));
  722. if (scm_is_true (CDDR (mx)))
  723. mod = scm_module_public_interface (mod);
  724. var = scm_module_lookup (mod, CADR (mx));
  725. if (scm_is_false (scm_variable_bound_p (var)))
  726. error_unbound_variable (CADR (mx));
  727. SCM_SET_SMOB_OBJECT (m, var);
  728. return var;
  729. }
  730. case SCM_M_MODULE_SET:
  731. /* FIXME: not quite threadsafe */
  732. if (SCM_VARIABLEP (CDR (mx)))
  733. return CDR (mx);
  734. else
  735. {
  736. SCM var;
  737. mod = scm_resolve_module (CADR (mx));
  738. if (scm_is_true (CDDDR (mx)))
  739. mod = scm_module_public_interface (mod);
  740. var = scm_module_lookup (mod, CADDR (mx));
  741. SCM_SETCDR (mx, var);
  742. return var;
  743. }
  744. default:
  745. scm_wrong_type_arg (FUNC_NAME, 1, m);
  746. return SCM_BOOL_F;
  747. }
  748. }
  749. #undef FUNC_NAME
  750. void
  751. scm_init_memoize ()
  752. {
  753. scm_tc16_memoized = scm_make_smob_type ("%memoized", 0);
  754. scm_set_smob_mark (scm_tc16_memoized, scm_markcdr);
  755. scm_set_smob_print (scm_tc16_memoized, scm_print_memoized);
  756. scm_tc16_memoizer = scm_make_smob_type ("memoizer", 0);
  757. #include "libguile/memoize.x"
  758. list_of_guile = scm_list_1 (scm_from_latin1_symbol ("guile"));
  759. }
  760. /*
  761. Local Variables:
  762. c-file-style: "gnu"
  763. End:
  764. */