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