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.
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.
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.
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
28 #include "libguile/__scm.h"
30 #include "libguile/_scm.h"
31 #include "libguile/alist.h"
32 #include "libguile/async.h"
33 #include "libguile/continuations.h"
34 #include "libguile/debug.h"
35 #include "libguile/deprecation.h"
36 #include "libguile/dynwind.h"
37 #include "libguile/eq.h"
38 #include "libguile/eval.h"
39 #include "libguile/feature.h"
40 #include "libguile/fluids.h"
41 #include "libguile/goops.h"
42 #include "libguile/hash.h"
43 #include "libguile/hashtab.h"
44 #include "libguile/list.h"
45 #include "libguile/macros.h"
46 #include "libguile/memoize.h"
47 #include "libguile/modules.h"
48 #include "libguile/ports.h"
49 #include "libguile/print.h"
50 #include "libguile/procprop.h"
51 #include "libguile/programs.h"
52 #include "libguile/root.h"
53 #include "libguile/smob.h"
54 #include "libguile/srcprop.h"
55 #include "libguile/stackchk.h"
56 #include "libguile/strings.h"
57 #include "libguile/threads.h"
58 #include "libguile/throw.h"
59 #include "libguile/validate.h"
60 #include "libguile/values.h"
61 #include "libguile/promises.h"
67 scm_t_bits scm_tc16_promise
;
69 SCM_DEFINE (scm_make_promise
, "make-promise", 1, 0, 0,
71 "Create a new promise object.\n\n"
72 "@code{make-promise} is a procedural form of @code{delay}.\n"
73 "These two expressions are equivalent:\n"
76 "(make-promise (lambda () @var{exp}))\n"
78 #define FUNC_NAME s_scm_make_promise
80 SCM_VALIDATE_THUNK (1, thunk
);
81 SCM_RETURN_NEWSMOB2 (scm_tc16_promise
,
83 SCM_UNPACK (scm_make_recursive_mutex ()));
88 promise_print (SCM exp
, SCM port
, scm_print_state
*pstate
)
90 int writingp
= SCM_WRITINGP (pstate
);
91 scm_puts_unlocked ("#<promise ", port
);
92 SCM_SET_WRITINGP (pstate
, 1);
93 scm_iprin1 (SCM_PROMISE_DATA (exp
), port
, pstate
);
94 SCM_SET_WRITINGP (pstate
, writingp
);
95 scm_putc_unlocked ('>', port
);
99 SCM_DEFINE (scm_force
, "force", 1, 0, 0,
101 "If the promise @var{x} has not been computed yet, compute and\n"
102 "return @var{x}, otherwise just return the previously computed\n"
104 #define FUNC_NAME s_scm_force
106 SCM_VALIDATE_SMOB (1, promise
, promise
);
107 scm_lock_mutex (SCM_PROMISE_MUTEX (promise
));
108 if (!SCM_PROMISE_COMPUTED_P (promise
))
110 SCM ans
= scm_call_0 (SCM_PROMISE_DATA (promise
));
111 if (!SCM_PROMISE_COMPUTED_P (promise
))
113 SCM_SET_PROMISE_DATA (promise
, ans
);
114 SCM_SET_PROMISE_COMPUTED (promise
);
117 scm_unlock_mutex (SCM_PROMISE_MUTEX (promise
));
118 return SCM_PROMISE_DATA (promise
);
123 SCM_DEFINE (scm_promise_p
, "promise?", 1, 0, 0,
125 "Return true if @var{obj} is a promise, i.e. a delayed computation\n"
126 "(@pxref{Delayed evaluation,,,r5rs.info,The Revised^5 Report on Scheme}).")
127 #define FUNC_NAME s_scm_promise_p
129 return scm_from_bool (SCM_TYP16_PREDICATE (scm_tc16_promise
, obj
));
136 scm_tc16_promise
= scm_make_smob_type ("promise", 0);
137 scm_set_smob_print (scm_tc16_promise
, promise_print
);
139 #include "libguile/promises.x"
141 scm_add_feature ("delay");