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 
20 
21 
22 #ifdef HAVE_CONFIG_H
23 #  include <config.h>
24 #endif
25 
26 #include <alloca.h>
27 
28 #include "libguile/__scm.h"
29 
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/smob.h"
53 #include "libguile/srcprop.h"
54 #include "libguile/stackchk.h"
55 #include "libguile/strings.h"
56 #include "libguile/threads.h"
57 #include "libguile/throw.h"
58 #include "libguile/validate.h"
59 #include "libguile/values.h"
60 #include "libguile/promises.h"
61 
62 
63 
64 
65 
66 scm_t_bits scm_tc16_promise;
67 
68 SCM_DEFINE (scm_make_promise, "make-promise", 1, 0, 0,
69 	    (SCM thunk),
70 	    "Create a new promise object.\n\n"
71             "@code{make-promise} is a procedural form of @code{delay}.\n"
72             "These two expressions are equivalent:\n"
73             "@lisp\n"
74 	    "(delay @var{exp})\n"
75 	    "(make-promise (lambda () @var{exp}))\n"
76             "@end lisp\n")
77 #define FUNC_NAME s_scm_make_promise
78 {
79   SCM_VALIDATE_THUNK (1, thunk);
80   SCM_RETURN_NEWSMOB2 (scm_tc16_promise,
81 		       SCM_UNPACK (thunk),
82 		       SCM_UNPACK (scm_make_recursive_mutex ()));
83 }
84 #undef FUNC_NAME
85 
86 static int
promise_print(SCM exp,SCM port,scm_print_state * pstate)87 promise_print (SCM exp, SCM port, scm_print_state *pstate)
88 {
89   int writingp = SCM_WRITINGP (pstate);
90   scm_puts ("#<promise ", port);
91   SCM_SET_WRITINGP (pstate, 1);
92   scm_iprin1 (SCM_PROMISE_DATA (exp), port, pstate);
93   SCM_SET_WRITINGP (pstate, writingp);
94   scm_putc ('>', port);
95   return !0;
96 }
97 
98 SCM_DEFINE (scm_force, "force", 1, 0, 0,
99 	    (SCM promise),
100 	    "If @var{promise} has not been computed yet, compute and\n"
101 	    "return @var{promise}, otherwise just return the previously computed\n"
102 	    "value.")
103 #define FUNC_NAME s_scm_force
104 {
105   SCM_VALIDATE_SMOB (1, promise, promise);
106   scm_lock_mutex (SCM_PROMISE_MUTEX (promise));
107   if (!SCM_PROMISE_COMPUTED_P (promise))
108     {
109       SCM ans = scm_call_0 (SCM_PROMISE_DATA (promise));
110       if (!SCM_PROMISE_COMPUTED_P (promise))
111 	{
112 	  SCM_SET_PROMISE_DATA (promise, ans);
113 	  SCM_SET_PROMISE_COMPUTED (promise);
114 	}
115     }
116   scm_unlock_mutex (SCM_PROMISE_MUTEX (promise));
117   return SCM_PROMISE_DATA (promise);
118 }
119 #undef FUNC_NAME
120 
121 
122 SCM_DEFINE (scm_promise_p, "promise?", 1, 0, 0,
123             (SCM obj),
124 	    "Return true if @var{obj} is a promise, i.e. a delayed computation\n"
125 	    "(@pxref{Delayed evaluation,,,r5rs.info,The Revised^5 Report on Scheme}).")
126 #define FUNC_NAME s_scm_promise_p
127 {
128   return scm_from_bool (SCM_TYP16_PREDICATE (scm_tc16_promise, obj));
129 }
130 #undef FUNC_NAME
131 
132 void
scm_init_promises()133 scm_init_promises ()
134 {
135   scm_tc16_promise = scm_make_smob_type ("promise", 0);
136   scm_set_smob_print (scm_tc16_promise, promise_print);
137 
138 #include "libguile/promises.x"
139 
140   scm_add_feature ("delay");
141 }
142 
143 /*
144   Local Variables:
145   c-file-style: "gnu"
146   End:
147 */
148 
149