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 ;
13 M: fail summary drop "Matching failed" ;
15 : assure ( ? -- ) [ throw-fail ] unless ; inline
17 : =/fail ( obj1 obj2 -- ) = assure ; inline
19 ! Inverse of a quotation
21 : define-inverse ( word quot -- ) "inverse" set-word-prop ;
23 : define-dual ( word1 word2 -- )
24 2dup swap [ 1quotation define-inverse ] 2bi@ ;
26 : define-involution ( word -- ) dup 1quotation define-inverse ;
28 : define-math-inverse ( word quot1 quot2 -- )
29 pick 1quotation 3array "math-inverse" set-word-prop ;
31 : define-pop-inverse ( word n quot -- )
32 [ dupd "pop-length" set-word-prop ] dip
33 "pop-inverse" set-word-prop ;
35 ERROR: bad-math-inverse ;
37 : next ( revquot -- revquot* first )
38 [ throw-bad-math-inverse ]
39 [ unclip-slice ] if-empty ;
41 : constant-word? ( word -- ? )
44 [ in>> empty? ] bi and ;
46 : assure-constant ( constant -- quot )
47 dup word? [ throw-bad-math-inverse ] when 1quotation ;
49 : swap-inverse ( math-inverse revquot -- revquot* quot )
50 next assure-constant rot second '[ @ swap @ ] ;
52 : pull-inverse ( math-inverse revquot const -- revquot* quot )
53 assure-constant rot first compose ;
55 : ?word-prop ( word/object name -- value/f )
56 over word? [ word-prop ] [ 2drop f ] if ;
58 : undo-literal ( object -- quot ) [ =/fail ] curry ;
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 ;
65 : enough? ( stack word -- ? )
66 dup deferred? [ 2drop f ] [
67 [ [ length ] [ 1quotation inputs ] bi* >= ]
71 : fold-word ( stack word -- stack )
73 [ 1quotation with-datastack ]
74 [ [ [ literalize , ] each ] [ , ] bi* { } ]
77 : fold ( quot -- folded-quot )
78 [ { } [ fold-word ] reduce % ] [ ] make ;
80 ERROR: no-recursive-inverse ;
84 : flattenable? ( object -- ? )
85 { [ word? ] [ primitive? not ] [
86 { "inverse" "math-inverse" "pop-inverse" }
87 [ word-prop ] with any? not
90 : flatten ( quot -- expanded )
92 visited [ over suffix ] change
96 [ visited get member-eq? [ no-recursive-inverse ] when ]
103 ERROR: undefined-inverse ;
105 GENERIC: inverse ( revquot word -- revquot* quot )
107 M: object inverse undo-literal ;
109 M: symbol inverse undo-literal ;
111 M: word inverse undefined-inverse ;
113 M: normal-inverse inverse
114 "inverse" word-prop ;
116 M: math-inverse inverse
117 "math-inverse" word-prop
118 swap next dup \ swap =
119 [ drop swap-inverse ] [ pull-inverse ] if ;
121 M: pop-inverse inverse
122 [ "pop-length" word-prop cut-slice swap >quotation ]
123 [ "pop-inverse" word-prop ] bi compose call( -- quot ) ;
125 : (undo) ( revquot -- )
126 [ unclip-slice inverse % (undo) ] unless-empty ;
128 : [undo] ( quot -- undo )
129 flatten fold reverse [ (undo) ] [ ] make ;
131 MACRO: undo ( quot -- quot ) [undo] ;
133 ! Inverse of selected words
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
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
146 \ not define-involution
147 \ >boolean [ dup { t f } member-eq? assure ] define-inverse
149 \ tuple>array \ >tuple define-dual
150 \ reverse define-involution
152 \ undo 1 [ ] define-pop-inverse
153 \ map 1 [ [undo] '[ dup sequence? assure _ map ] ] define-pop-inverse
155 \ e^ \ log define-dual
156 \ sq \ sqrt define-dual
158 ERROR: missing-literal ;
160 : assert-literal ( n -- n )
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
171 [ assert-literal ] bi@
172 [ swap [ over = ] dip swap [ 2drop f ] [ = [ t ] [ throw-fail ] if ] if ]
177 \ __ [ drop ] define-inverse
179 : both ( object object -- object )
181 \ both [ dup ] define-inverse
188 { >bit-array bit-array? }
190 { >byte-array byte-array? }
193 { >quotation quotation? }
194 } [ '[ dup _ execute assure ] define-inverse ] assoc-each
196 : assure-length ( seq length -- )
197 swap length =/fail ; inline
199 : assure-array ( array -- array )
200 dup array? assure ; inline
202 : undo-narray ( array n -- ... )
204 [ assure-length ] [ firstn ] 2bi ; inline
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
212 \ first [ 1array ] define-inverse
213 \ first2 [ 2array ] define-inverse
214 \ first3 [ 3array ] define-inverse
215 \ first4 [ 4array ] define-inverse
217 \ prefix \ unclip define-dual
218 \ suffix \ unclip-last define-dual
220 \ append 1 [ [ ?tail assure ] curry ] define-pop-inverse
221 \ prepend 1 [ [ ?head assure ] curry ] define-pop-inverse
223 : assure-same-class ( obj1 obj2 -- )
224 [ class-of ] same? assure ; inline
226 \ output>sequence 2 [ [undo] '[ dup _ assure-same-class _ input<sequence ] ] define-pop-inverse
227 \ input<sequence 1 [ [undo] '[ _ { } output>sequence ] ] define-pop-inverse
231 :: undo-if-empty ( result a b -- seq )
232 a call( -- b ) result = [ { } ] [ result b [undo] call( a -- b ) ] if ;
234 :: undo-if* ( result a b -- boolean )
235 b call( -- b ) result = [ f ] [ result a [undo] call( a -- b ) ] if ;
237 \ if-empty 2 [ swap [ undo-if-empty ] 2curry ] define-pop-inverse
239 \ if* 2 [ swap [ undo-if* ] 2curry ] define-pop-inverse
241 ! Constructor inverse
242 : deconstruct-pred ( class -- quot )
243 predicate-def [ dupd call assure ] curry ;
245 : slot-readers ( class -- quot )
246 all-slots [ name>> reader-word 1quotation ] map [ cleave ] curry ;
248 : ?wrapped ( object -- wrapped )
249 dup wrapper? [ wrapped>> ] when ;
251 : boa-inverse ( class -- quot )
252 [ deconstruct-pred ] [ slot-readers ] bi compose ;
254 \ boa 1 [ ?wrapped boa-inverse ] define-pop-inverse
256 : empty-inverse ( class -- quot )
258 [ tuple-slots [ ] any? [ throw-fail ] when ]
261 \ new 1 [ ?wrapped empty-inverse ] define-pop-inverse
263 ! More useful inverse-based combinators
265 : recover-fail ( try fail -- )
267 [ nip ] dip dup fail?
268 [ drop call ] [ nip throw ] if
271 : true-out ( quot effect -- quot' )
272 out>> length '[ @ _ ndrop t ] ;
274 : false-recover ( effect -- quot )
275 in>> length [ ndrop f ] curry [ recover-fail ] curry ;
277 : [matches?] ( quot -- undoes?-quot )
278 [undo] dup infer [ true-out ] [ false-recover ] bi curry ;
280 MACRO: matches? ( quot -- quot' ) [matches?] ;
283 M: no-match summary drop "Fall through in switch" ;
285 : recover-chain ( seq -- quot )
286 [ no-match ] [ swap \ recover-fail 3array >quotation ] reduce ;
288 : [switch] ( quot-alist -- quot )
289 [ dup quotation? [ [ ] swap 2array ] when ] map
290 reverse [ [ [undo] ] dip compose ] { } assoc>map
293 MACRO: switch ( quot-alist -- quot ) [switch] ;