]> gitweb.factorcode.org Git - factor.git/blob - core/memoize/memoize.factor
core: whoops, all these moves got missed.
[factor.git] / core / memoize / memoize.factor
1 ! Copyright (C) 2007, 2010 Slava Pestov, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs definitions effects
4 effects.parser fry hashtables.identity kernel kernel.private
5 math sequences sequences.private words ;
6 IN: memoize
7
8 <PRIVATE
9
10 ! We can't use n*quot, narray and firstn from generalizations because
11 ! they're macros, and macros use memoize!
12 : (n*quot) ( n quot -- quotquot )
13     <repetition> [ ] concat-as ;
14
15 : [nsequence] ( length exemplar -- quot )
16     [ [ [ 1 - ] keep ] dip '[ _ _ _ new-sequence ] ]
17     [ drop [ [ set-nth-unsafe ] 2keep [ 1 - ] dip ] (n*quot) ] 2bi
18     [ nip ] 3append ;
19
20 : [firstn] ( length -- quot )
21     [ 0 swap ] swap
22     [ [ nth-unsafe ] 2keep [ 1 + ] dip ] (n*quot)
23     [ 2drop ] 3append ;
24
25 : packer ( seq -- quot )
26     length dup 4 <=
27     [ { [ f ] [ ] [ 2array ] [ 3array ] [ 4array ] } nth ]
28     [ { } [nsequence] ] if ;
29
30 : unpacker ( seq -- quot )
31     length dup dup 4 <=
32     [ { [ drop ] [ ] [ first2-unsafe ] [ first3-unsafe ] [ first4-unsafe ] } nth ]
33     [ [firstn] ] if swap 1 >
34     [ [ { array } declare ] prepose ] when ;
35
36 : pack/unpack ( quot effect -- newquot )
37     [ in>> packer ] [ out>> unpacker ] bi surround ;
38
39 : unpack/pack ( quot effect -- newquot )
40     [ in>> unpacker ] [ out>> packer ] bi surround ;
41
42 : make/n ( table quot effect -- quot )
43     [ unpack/pack '[ _ _ cache ] ] keep pack/unpack ;
44
45 : make/0 ( table quot effect -- quot )
46     out>> [
47         packer '[
48             _ dup first-unsafe
49             [ ] [ @ @ [ 0 rot set-nth-unsafe ] keep ] ?if
50         ]
51     ] keep unpacker compose ;
52
53 : make-memoizer ( table quot effect -- quot )
54     dup in>> length zero? [ make/0 ] [ make/n ] if ;
55
56 PRIVATE>
57
58 : (define-memoized) ( word quot effect hashtable -- )
59     [ [ drop "memo-quot" set-word-prop ] ] dip
60     '[ 2drop _ "memoize" set-word-prop ]
61     [ [ [ dup "memoize" word-prop ] 2dip make-memoizer ] keep define-declared ]
62     3tri ;
63
64 : define-memoized ( word quot effect -- )
65     dup in>> length zero? [ f 1array ] [ H{ } clone ] if
66     (define-memoized) ;
67
68 : define-identity-memoized ( word quot effect -- )
69     dup in>> length zero? [ f 1array ] [ IH{ } clone ] if
70     (define-memoized) ;
71
72 PREDICATE: memoized < word "memoize" word-prop >boolean ;
73
74 M: memoized definer drop \ MEMO: \ ; ;
75
76 M: memoized definition "memo-quot" word-prop ;
77
78 M: memoized reset-word
79     [ call-next-method ]
80     [ { "memoize" "memo-quot" } remove-word-props ]
81     bi ;
82
83 : memoize-quot ( quot effect -- memo-quot )
84     dup in>> length zero? [ f 1array ] [ H{ } clone ] if
85     -rot make-memoizer ;
86
87 : reset-memoized ( word -- )
88     "memoize" word-prop dup sequence?
89     [ f swap set-first ] [ clear-assoc ] if ;
90
91 : invalidate-memoized ( inputs... word -- )
92     [ stack-effect in>> packer call ]
93     [
94         "memoize" word-prop dup sequence?
95         [ f swap set-first ] [ delete-at ] if
96     ]
97     bi ;
98
99 \ invalidate-memoized t "no-compile" set-word-prop