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