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