]> gitweb.factorcode.org Git - factor.git/blob - basis/promises/promises.factor
Create basis vocab root
[factor.git] / basis / promises / promises.factor
1 ! Copyright (C) 2004 Chris Double.
2 ! See http://factorcode.org/license.txt for BSD license.
3 !
4 ! Updated by Matthew Willis, July 2006
5 ! Updated by Chris Double, September 2006
6
7 USING: arrays kernel sequences math vectors arrays namespaces
8 quotations parser effects inference words ;
9 IN: promises
10
11 TUPLE: promise quot forced? value ;
12
13 : promise ( quot -- promise )
14   f f \ promise boa ;
15
16 : promise-with ( value quot -- promise )
17   curry promise ;
18
19 : promise-with2 ( value1 value2 quot -- promise )
20   2curry promise ;
21
22 : force ( promise -- value )
23     #! Force the given promise leaving the value of calling the
24     #! promises quotation on the stack. Re-forcing the promise
25     #! will return the same value and not recall the quotation.
26     dup promise-forced? [
27         dup promise-quot call over set-promise-value
28         t over set-promise-forced?
29     ] unless
30     promise-value ;
31
32 : stack-effect-in ( quot word -- n )
33   stack-effect [ ] [ infer ] ?if effect-in length ;
34
35 : make-lazy-quot ( word quot -- quot )
36   [
37     dup ,
38     swap stack-effect-in \ curry <repetition> % 
39     \ promise ,
40   ] [ ] make ;
41
42 : LAZY:
43   CREATE-WORD
44   dup parse-definition
45   make-lazy-quot define ; parsing