]> gitweb.factorcode.org Git - factor.git/blob - basis/inverse/inverse.factor
9dc79e91b5a013376997467bfd08622bfc8785af
[factor.git] / basis / inverse / inverse.factor
1 ! Copyright (C) 2007, 2008 Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel words summary slots quotations
4 sequences assocs math arrays stack-checker effects generalizations
5 continuations debugger classes.tuple namespaces make vectors
6 bit-arrays byte-arrays strings sbufs math.functions macros
7 sequences.private combinators mirrors splitting
8 combinators.short-circuit fry words.symbol generalizations call ;
9 RENAME: _ fry => __
10 IN: inverse
11
12 ERROR: fail ;
13 M: fail summary drop "Matching failed" ;
14
15 : assure ( ? -- ) [ fail ] unless ;
16
17 : =/fail ( obj1 obj2 -- ) = assure ;
18
19 ! Inverse of a quotation
20
21 : define-inverse ( word quot -- ) "inverse" set-word-prop ;
22
23 : define-dual ( word1 word2 -- )
24     2dup swap [ 1quotation define-inverse ] 2bi@ ;
25
26 : define-involution ( word -- ) dup 1quotation define-inverse ;
27
28 : define-math-inverse ( word quot1 quot2 -- )
29     pick 1quotation 3array "math-inverse" set-word-prop ;
30
31 : define-pop-inverse ( word n quot -- )
32     [ dupd "pop-length" set-word-prop ] dip
33     "pop-inverse" set-word-prop ;
34
35 ERROR: no-inverse word ;
36 M: no-inverse summary
37     drop "The word cannot be used in pattern matching" ;
38
39 ERROR: bad-math-inverse ;
40
41 : next ( revquot -- revquot* first )
42     [ bad-math-inverse ]
43     [ unclip-slice ] if-empty ;
44
45 : constant-word? ( word -- ? )
46     stack-effect
47     [ out>> length 1 = ]
48     [ in>> empty? ] bi and ;
49
50 : assure-constant ( constant -- quot )
51     dup word? [ bad-math-inverse ] when 1quotation ;
52
53 : swap-inverse ( math-inverse revquot -- revquot* quot )
54     next assure-constant rot second '[ @ swap @ ] ;
55
56 : pull-inverse ( math-inverse revquot const -- revquot* quot )
57     assure-constant rot first compose ;
58
59 : ?word-prop ( word/object name -- value/f )
60     over word? [ word-prop ] [ 2drop f ] if ;
61
62 : undo-literal ( object -- quot ) [ =/fail ] curry ;
63
64 PREDICATE: normal-inverse < word "inverse" word-prop ;
65 PREDICATE: math-inverse < word "math-inverse" word-prop ;
66 PREDICATE: pop-inverse < word "pop-length" word-prop ;
67 UNION: explicit-inverse normal-inverse math-inverse pop-inverse ;
68
69 : enough? ( stack word -- ? )
70     dup deferred? [ 2drop f ] [
71         [ [ length ] [ 1quotation infer in>> ] bi* >= ]
72         [ 3drop f ] recover
73     ] if ;
74
75 : fold-word ( stack word -- stack )
76     2dup enough?
77     [ 1quotation with-datastack ] [ [ % ] [ , ] bi* { } ] if ;
78
79 : fold ( quot -- folded-quot )
80     [ { } [ fold-word ] reduce % ] [ ] make ; 
81
82 ERROR: no-recursive-inverse ;
83
84 SYMBOL: visited
85
86 : flattenable? ( object -- ? )
87     { [ word? ] [ primitive? not ] [
88         { "inverse" "math-inverse" "pop-inverse" }
89         [ word-prop ] with any? not
90     ] } 1&& ; 
91
92 : flatten ( quot -- expanded )
93     [
94         visited [ over suffix ] change
95         [
96             dup flattenable? [
97                 def>>
98                 [ visited get memq? [ no-recursive-inverse ] when ]
99                 [ flatten ]
100                 bi
101             ] [ 1quotation ] if
102         ] map concat
103     ] with-scope ;
104
105 ERROR: undefined-inverse ;
106
107 GENERIC: inverse ( revquot word -- revquot* quot )
108
109 M: object inverse undo-literal ;
110
111 M: symbol inverse undo-literal ;
112
113 M: word inverse undefined-inverse ;
114
115 M: normal-inverse inverse
116     "inverse" word-prop ;
117
118 M: math-inverse inverse
119     "math-inverse" word-prop
120     swap next dup \ swap =
121     [ drop swap-inverse ] [ pull-inverse ] if ;
122
123 M: pop-inverse inverse
124     [ "pop-length" word-prop cut-slice swap >quotation ]
125     [ "pop-inverse" word-prop ] bi compose call( -- quot ) ;
126
127 : (undo) ( revquot -- )
128     [ unclip-slice inverse % (undo) ] unless-empty ;
129
130 : [undo] ( quot -- undo )
131     flatten fold reverse [ (undo) ] [ ] make ;
132
133 MACRO: undo ( quot -- ) [undo] ;
134
135 ! Inverse of selected words
136
137 \ swap define-involution
138 \ dup [ [ =/fail ] keep ] define-inverse
139 \ 2dup [ over =/fail over =/fail ] define-inverse
140 \ 3dup [ pick =/fail pick =/fail pick =/fail ] define-inverse
141 \ pick [ [ pick ] dip =/fail ] define-inverse
142 \ tuck [ swapd [ =/fail ] keep ] define-inverse
143
144 \ not define-involution
145 \ >boolean [ { t f } memq? assure ] define-inverse
146
147 \ tuple>array \ >tuple define-dual
148 \ reverse define-involution
149
150 \ undo 1 [ [ call ] curry ] define-pop-inverse
151 \ map 1 [ [undo] [ over sequence? assure map ] curry ] define-pop-inverse
152
153 \ exp \ log define-dual
154 \ sq \ sqrt define-dual
155
156 ERROR: missing-literal ;
157
158 : assert-literal ( n -- n )
159     dup
160     [ word? ] [ symbol? not ] bi and
161     [ missing-literal ] when ;
162 \ + [ - ] [ - ] define-math-inverse
163 \ - [ + ] [ - ] define-math-inverse
164 \ * [ / ] [ / ] define-math-inverse
165 \ / [ * ] [ / ] define-math-inverse
166 \ ^ [ recip ^ ] [ swap [ log ] bi@ / ] define-math-inverse
167
168 \ ? 2 [
169     [ assert-literal ] bi@
170     [ swap [ over = ] dip swap [ 2drop f ] [ = [ t ] [ fail ] if ] if ]
171     2curry
172 ] define-pop-inverse
173
174 DEFER: _
175 \ _ [ drop ] define-inverse
176
177 : both ( object object -- object )
178     dupd assert= ;
179 \ both [ dup ] define-inverse
180
181 : assure-length ( seq length -- seq )
182     over length =/fail ;
183
184 {
185     { >array array? }
186     { >vector vector? }
187     { >fixnum fixnum? }
188     { >bignum bignum? }
189     { >bit-array bit-array? }
190     { >float float? }
191     { >byte-array byte-array? }
192     { >string string? }
193     { >sbuf sbuf? }
194     { >quotation quotation? }
195 } [ \ dup swap \ assure 3array >quotation define-inverse ] assoc-each
196
197 ! These actually work on all seqs--should they?
198 \ 1array [ 1 assure-length first ] define-inverse
199 \ 2array [ 2 assure-length first2 ] define-inverse
200 \ 3array [ 3 assure-length first3 ] define-inverse
201 \ 4array [ 4 assure-length first4 ] define-inverse
202 \ narray 1 [ [ firstn ] curry ] define-pop-inverse
203
204 \ first [ 1array ] define-inverse
205 \ first2 [ 2array ] define-inverse
206 \ first3 [ 3array ] define-inverse
207 \ first4 [ 4array ] define-inverse
208
209 \ prefix \ unclip define-dual
210 \ suffix [ dup but-last swap peek ] define-inverse
211
212 \ append 1 [ [ ?tail assure ] curry ] define-pop-inverse
213 \ prepend 1 [ [ ?head assure ] curry ] define-pop-inverse
214
215 ! Constructor inverse
216 : deconstruct-pred ( class -- quot )
217     "predicate" word-prop [ dupd call assure ] curry ;
218
219 : slot-readers ( class -- quot )
220     all-slots
221     [ name>> reader-word 1quotation [ keep ] curry ] map concat
222     [ ] like [ drop ] compose ;
223
224 : ?wrapped ( object -- wrapped )
225     dup wrapper? [ wrapped>> ] when ;
226
227 : boa-inverse ( class -- quot )
228     [ deconstruct-pred ] [ slot-readers ] bi compose ;
229
230 \ boa 1 [ ?wrapped boa-inverse ] define-pop-inverse
231
232 : empty-inverse ( class -- quot )
233     deconstruct-pred
234     [ tuple>array rest [ ] any? [ fail ] when ]
235     compose ;
236
237 \ new 1 [ ?wrapped empty-inverse ] define-pop-inverse
238
239 ! More useful inverse-based combinators
240
241 : recover-fail ( try fail -- )
242     [ drop call ] [
243         [ nip ] dip dup fail?
244         [ drop call ] [ nip throw ] if
245     ] recover ; inline
246
247 : true-out ( quot effect -- quot' )
248     out>> '[ @ __ ndrop t ] ;
249
250 : false-recover ( effect -- quot )
251     in>> [ ndrop f ] curry [ recover-fail ] curry ;
252
253 : [matches?] ( quot -- undoes?-quot )
254     [undo] dup infer [ true-out ] [ false-recover ] bi curry ;
255
256 MACRO: matches? ( quot -- ? ) [matches?] ;
257
258 ERROR: no-match ;
259 M: no-match summary drop "Fall through in switch" ;
260
261 : recover-chain ( seq -- quot )
262     [ no-match ] [ swap \ recover-fail 3array >quotation ] reduce ;
263
264 : [switch]  ( quot-alist -- quot )
265     [ dup quotation? [ [ ] swap 2array ] when ] map
266     reverse [ [ [undo] ] dip compose ] { } assoc>map
267     recover-chain ;
268
269 MACRO: switch ( quot-alist -- ) [switch] ;