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 ;
13 M: fail summary drop "Matching failed" ;
15 : assure ( ? -- ) [ fail ] unless ;
17 : =/fail ( obj1 obj2 -- ) = assure ;
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: no-inverse word ;
37 drop "The word cannot be used in pattern matching" ;
39 ERROR: bad-math-inverse ;
41 : next ( revquot -- revquot* first )
43 [ unclip-slice ] if-empty ;
45 : constant-word? ( word -- ? )
48 [ in>> empty? ] bi and ;
50 : assure-constant ( constant -- quot )
51 dup word? [ bad-math-inverse ] when 1quotation ;
53 : swap-inverse ( math-inverse revquot -- revquot* quot )
54 next assure-constant rot second '[ @ swap @ ] ;
56 : pull-inverse ( math-inverse revquot const -- revquot* quot )
57 assure-constant rot first compose ;
59 : ?word-prop ( word/object name -- value/f )
60 over word? [ word-prop ] [ 2drop f ] if ;
62 : undo-literal ( object -- quot ) [ =/fail ] curry ;
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 ;
69 : enough? ( stack word -- ? )
70 dup deferred? [ 2drop f ] [
71 [ [ length ] [ 1quotation infer in>> ] bi* >= ]
75 : fold-word ( stack word -- stack )
77 [ 1quotation with-datastack ] [ [ % ] [ , ] bi* { } ] if ;
79 : fold ( quot -- folded-quot )
80 [ { } [ fold-word ] reduce % ] [ ] make ;
82 ERROR: no-recursive-inverse ;
86 : flattenable? ( object -- ? )
87 { [ word? ] [ primitive? not ] [
88 { "inverse" "math-inverse" "pop-inverse" }
89 [ word-prop ] with contains? not
92 : flatten ( quot -- expanded )
94 visited [ over suffix ] change
98 [ visited get memq? [ no-recursive-inverse ] when ]
105 ERROR: undefined-inverse ;
107 GENERIC: inverse ( revquot word -- revquot* quot )
109 M: object inverse undo-literal ;
111 M: symbol inverse undo-literal ;
113 M: word inverse undefined-inverse ;
115 M: normal-inverse inverse
116 "inverse" word-prop ;
118 M: math-inverse inverse
119 "math-inverse" word-prop
120 swap next dup \ swap =
121 [ drop swap-inverse ] [ pull-inverse ] if ;
123 M: pop-inverse inverse
124 [ "pop-length" word-prop cut-slice swap >quotation ]
125 [ "pop-inverse" word-prop ] bi compose call ;
127 : (undo) ( revquot -- )
128 [ unclip-slice inverse % (undo) ] unless-empty ;
130 : [undo] ( quot -- undo )
131 flatten fold reverse [ (undo) ] [ ] make ;
133 MACRO: undo ( quot -- ) [undo] ;
135 ! Inverse of selected words
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
144 \ not define-involution
145 \ >boolean [ { t f } memq? assure ] define-inverse
147 \ tuple>array \ >tuple define-dual
148 \ reverse define-involution
150 \ undo 1 [ [ call ] curry ] define-pop-inverse
151 \ map 1 [ [undo] [ over sequence? assure map ] curry ] define-pop-inverse
153 \ exp \ log define-dual
154 \ sq \ sqrt define-dual
156 ERROR: missing-literal ;
158 : assert-literal ( n -- n )
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 ^ ] [ [ log ] bi@ / ] define-math-inverse
169 [ assert-literal ] bi@
170 [ swap [ over = ] dip swap [ 2drop f ] [ = [ t ] [ fail ] if ] if ]
175 \ _ [ drop ] define-inverse
177 : both ( object object -- object )
179 \ both [ dup ] define-inverse
181 : assure-length ( seq length -- seq )
189 { >bit-array bit-array? }
191 { >byte-array byte-array? }
194 { >quotation quotation? }
195 } [ \ dup swap \ assure 3array >quotation define-inverse ] assoc-each
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
203 \ first [ 1array ] define-inverse
204 \ first2 [ 2array ] define-inverse
205 \ first3 [ 3array ] define-inverse
206 \ first4 [ 4array ] define-inverse
208 \ prefix \ unclip define-dual
209 \ suffix [ dup but-last swap peek ] define-inverse
211 \ append 1 [ [ ?tail assure ] curry ] define-pop-inverse
212 \ prepend 1 [ [ ?head assure ] curry ] define-pop-inverse
214 ! Constructor inverse
215 : deconstruct-pred ( class -- quot )
216 "predicate" word-prop [ dupd call assure ] curry ;
218 : slot-readers ( class -- quot )
220 [ name>> reader-word 1quotation [ keep ] curry ] map concat
221 [ ] like [ drop ] compose ;
223 : ?wrapped ( object -- wrapped )
224 dup wrapper? [ wrapped>> ] when ;
226 : boa-inverse ( class -- quot )
227 [ deconstruct-pred ] [ slot-readers ] bi compose ;
229 \ boa 1 [ ?wrapped boa-inverse ] define-pop-inverse
231 : empty-inverse ( class -- quot )
233 [ tuple>array rest [ ] contains? [ fail ] when ]
236 \ new 1 [ ?wrapped empty-inverse ] define-pop-inverse
238 ! More useful inverse-based combinators
240 : recover-fail ( try fail -- )
242 [ nip ] dip dup fail?
243 [ drop call ] [ nip throw ] if
246 : true-out ( quot effect -- quot' )
247 out>> '[ @ __ ndrop t ] ;
249 : false-recover ( effect -- quot )
250 in>> [ ndrop f ] curry [ recover-fail ] curry ;
252 : [matches?] ( quot -- undoes?-quot )
253 [undo] dup infer [ true-out ] [ false-recover ] bi curry ;
255 MACRO: matches? ( quot -- ? ) [matches?] ;
258 M: no-match summary drop "Fall through in switch" ;
260 : recover-chain ( seq -- quot )
261 [ no-match ] [ swap \ recover-fail 3array >quotation ] reduce ;
263 : [switch] ( quot-alist -- quot )
264 [ dup quotation? [ [ ] swap 2array ] when ] map
265 reverse [ [ [undo] ] dip compose ] { } assoc>map
268 MACRO: switch ( quot-alist -- ) [switch] ;