| 
									
										
										
										
											2008-06-30 17:06:14 -04:00
										 |  |  | ! Copyright (C) 2007, 2008 Daniel Ehrenberg. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-07-02 01:20:01 -04:00
										 |  |  | USING: accessors kernel words summary slots quotations | 
					
						
							| 
									
										
										
										
											2008-08-22 23:07:59 -04:00
										 |  |  | sequences assocs math arrays stack-checker effects generalizations | 
					
						
							| 
									
										
										
										
											2008-09-10 23:11:40 -04:00
										 |  |  | continuations debugger classes.tuple namespaces make vectors | 
					
						
							| 
									
										
										
										
											2008-06-30 17:06:14 -04:00
										 |  |  | bit-arrays byte-arrays strings sbufs math.functions macros | 
					
						
							| 
									
										
										
										
											2009-01-04 15:59:55 -05:00
										 |  |  | sequences.private combinators mirrors splitting | 
					
						
							| 
									
										
										
										
											2009-03-16 21:11:36 -04:00
										 |  |  | combinators.short-circuit fry words.symbol generalizations ;
 | 
					
						
							| 
									
										
										
										
											2008-12-02 17:28:11 -05:00
										 |  |  | RENAME: _ fry => __ | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: inverse | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-07 00:38:04 -05:00
										 |  |  | ERROR: fail ;
 | 
					
						
							| 
									
										
										
										
											2009-01-04 15:59:55 -05:00
										 |  |  | M: fail summary drop "Matching failed" ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : assure ( ? -- ) [ fail ] unless ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-07 00:38:04 -05:00
										 |  |  | : =/fail ( obj1 obj2 -- ) = assure ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! Inverse of a quotation | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : define-inverse ( word quot -- ) "inverse" set-word-prop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-13 10:39:34 -05:00
										 |  |  | : define-dual ( word1 word2 -- )
 | 
					
						
							|  |  |  |     2dup swap [ 1quotation define-inverse ] 2bi@ ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-13 10:58:31 -05:00
										 |  |  | : define-involution ( word -- ) dup 1quotation define-inverse ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-21 17:56:28 -05:00
										 |  |  | : define-math-inverse ( word quot1 quot2 -- )
 | 
					
						
							| 
									
										
										
										
											2007-11-29 12:06:52 -05:00
										 |  |  |     pick 1quotation 3array "math-inverse" set-word-prop ;
 | 
					
						
							| 
									
										
										
										
											2007-11-21 17:56:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-22 00:43:30 -05:00
										 |  |  | : define-pop-inverse ( word n quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-12-07 00:38:04 -05:00
										 |  |  |     [ dupd "pop-length" set-word-prop ] dip
 | 
					
						
							| 
									
										
										
										
											2007-11-22 00:43:30 -05:00
										 |  |  |     "pop-inverse" set-word-prop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-07 00:38:04 -05:00
										 |  |  | ERROR: no-inverse word ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | M: no-inverse summary | 
					
						
							|  |  |  |     drop "The word cannot be used in pattern matching" ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-07 00:38:04 -05:00
										 |  |  | ERROR: bad-math-inverse ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-21 17:56:28 -05:00
										 |  |  | : next ( revquot -- revquot* first )
 | 
					
						
							| 
									
										
										
										
											2008-12-07 00:38:04 -05:00
										 |  |  |     [ bad-math-inverse ] | 
					
						
							| 
									
										
										
										
											2008-09-06 18:15:25 -04:00
										 |  |  |     [ unclip-slice ] if-empty ;
 | 
					
						
							| 
									
										
										
										
											2007-11-21 17:56:28 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : constant-word? ( word -- ? )
 | 
					
						
							|  |  |  |     stack-effect | 
					
						
							| 
									
										
										
										
											2008-12-07 00:38:04 -05:00
										 |  |  |     [ out>> length 1 = ] | 
					
						
							|  |  |  |     [ in>> empty? ] bi and ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-21 17:56:28 -05:00
										 |  |  | : assure-constant ( constant -- quot )
 | 
					
						
							| 
									
										
										
										
											2008-12-07 00:38:04 -05:00
										 |  |  |     dup word? [ bad-math-inverse ] when 1quotation ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-21 17:56:28 -05:00
										 |  |  | : swap-inverse ( math-inverse revquot -- revquot* quot )
 | 
					
						
							| 
									
										
										
										
											2008-12-02 01:25:23 -05:00
										 |  |  |     next assure-constant rot second '[ @ swap @ ] ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-21 17:56:28 -05:00
										 |  |  | : pull-inverse ( math-inverse revquot const -- revquot* quot )
 | 
					
						
							|  |  |  |     assure-constant rot first compose ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : ?word-prop ( word/object name -- value/f )
 | 
					
						
							|  |  |  |     over word? [ word-prop ] [ 2drop f ] if ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-07 00:38:04 -05:00
										 |  |  | : undo-literal ( object -- quot ) [ =/fail ] curry ;
 | 
					
						
							| 
									
										
										
										
											2007-11-29 12:06:52 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-26 19:23:19 -04:00
										 |  |  | PREDICATE: normal-inverse < word "inverse" word-prop ;
 | 
					
						
							|  |  |  | PREDICATE: math-inverse < word "math-inverse" word-prop ;
 | 
					
						
							|  |  |  | PREDICATE: pop-inverse < word "pop-length" word-prop ;
 | 
					
						
							| 
									
										
										
										
											2007-11-29 12:06:52 -05:00
										 |  |  | UNION: explicit-inverse normal-inverse math-inverse pop-inverse ;
 | 
					
						
							| 
									
										
										
										
											2007-11-22 00:43:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-07 01:45:46 -04:00
										 |  |  | : enough? ( stack word -- ? )
 | 
					
						
							|  |  |  |     dup deferred? [ 2drop f ] [ | 
					
						
							| 
									
										
										
										
											2009-01-06 22:14:22 -05:00
										 |  |  |         [ [ length ] [ 1quotation infer in>> ] bi* >= ] | 
					
						
							| 
									
										
										
										
											2008-04-07 01:45:46 -04:00
										 |  |  |         [ 3drop f ] recover
 | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-03-29 16:18:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-07 01:45:46 -04:00
										 |  |  | : fold-word ( stack word -- stack )
 | 
					
						
							| 
									
										
										
										
											2008-03-29 16:18:46 -04:00
										 |  |  |     2dup enough? | 
					
						
							| 
									
										
										
										
											2009-01-06 22:14:22 -05:00
										 |  |  |     [ 1quotation with-datastack ] [ [ % ] [ , ] bi* { } ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-03-29 16:18:46 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : fold ( quot -- folded-quot )
 | 
					
						
							| 
									
										
										
										
											2009-01-06 22:14:22 -05:00
										 |  |  |     [ { } [ fold-word ] reduce % ] [ ] make ;  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ERROR: no-recursive-inverse ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SYMBOL: visited | 
					
						
							| 
									
										
										
										
											2008-03-29 16:18:46 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : flattenable? ( object -- ? )
 | 
					
						
							| 
									
										
										
										
											2008-04-07 01:45:46 -04:00
										 |  |  |     { [ word? ] [ primitive? not ] [ | 
					
						
							| 
									
										
										
										
											2008-03-29 16:18:46 -04:00
										 |  |  |         { "inverse" "math-inverse" "pop-inverse" } | 
					
						
							| 
									
										
										
										
											2009-01-29 23:19:07 -05:00
										 |  |  |         [ word-prop ] with any? not
 | 
					
						
							| 
									
										
										
										
											2008-06-10 22:06:36 -04:00
										 |  |  |     ] } 1&& ;  | 
					
						
							| 
									
										
										
										
											2008-03-29 16:18:46 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : flatten ( quot -- expanded )
 | 
					
						
							| 
									
										
										
										
											2009-01-06 22:14:22 -05:00
										 |  |  |     [ | 
					
						
							|  |  |  |         visited [ over suffix ] change
 | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |             dup flattenable? [ | 
					
						
							|  |  |  |                 def>> | 
					
						
							|  |  |  |                 [ visited get memq? [ no-recursive-inverse ] when ] | 
					
						
							|  |  |  |                 [ flatten ] | 
					
						
							|  |  |  |                 bi
 | 
					
						
							|  |  |  |             ] [ 1quotation ] if
 | 
					
						
							|  |  |  |         ] map concat
 | 
					
						
							|  |  |  |     ] with-scope ;
 | 
					
						
							| 
									
										
										
										
											2007-11-29 12:06:52 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-07 00:38:04 -05:00
										 |  |  | ERROR: undefined-inverse ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-29 12:06:52 -05:00
										 |  |  | GENERIC: inverse ( revquot word -- revquot* quot )
 | 
					
						
							| 
									
										
										
										
											2007-11-22 00:43:30 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: object inverse undo-literal ;
 | 
					
						
							| 
									
										
										
										
											2008-03-29 16:18:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-22 00:43:30 -05:00
										 |  |  | M: symbol inverse undo-literal ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-07 00:38:04 -05:00
										 |  |  | M: word inverse undefined-inverse ;
 | 
					
						
							| 
									
										
										
										
											2008-03-29 16:18:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-29 12:06:52 -05:00
										 |  |  | M: normal-inverse inverse | 
					
						
							|  |  |  |     "inverse" word-prop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-22 00:43:30 -05:00
										 |  |  | M: math-inverse inverse | 
					
						
							|  |  |  |     "math-inverse" word-prop | 
					
						
							|  |  |  |     swap next dup \ swap =
 | 
					
						
							|  |  |  |     [ drop swap-inverse ] [ pull-inverse ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: pop-inverse inverse | 
					
						
							| 
									
										
										
										
											2008-12-07 00:38:04 -05:00
										 |  |  |     [ "pop-length" word-prop cut-slice swap >quotation ] | 
					
						
							| 
									
										
										
										
											2009-03-15 19:19:29 -04:00
										 |  |  |     [ "pop-inverse" word-prop ] bi compose call( -- quot ) ;
 | 
					
						
							| 
									
										
										
										
											2007-11-22 00:43:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-21 17:56:28 -05:00
										 |  |  | : (undo) ( revquot -- )
 | 
					
						
							| 
									
										
										
										
											2008-09-06 18:15:25 -04:00
										 |  |  |     [ unclip-slice inverse % (undo) ] unless-empty ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : [undo] ( quot -- undo )
 | 
					
						
							| 
									
										
										
										
											2008-03-29 16:18:46 -04:00
										 |  |  |     flatten fold reverse [ (undo) ] [ ] make ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | MACRO: undo ( quot -- ) [undo] ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-28 15:33:58 -05:00
										 |  |  | ! Inverse of selected words | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-13 10:58:31 -05:00
										 |  |  | \ swap define-involution | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | \ dup [ [ =/fail ] keep ] define-inverse | 
					
						
							|  |  |  | \ 2dup [ over =/fail over =/fail ] define-inverse | 
					
						
							|  |  |  | \ 3dup [ pick =/fail pick =/fail pick =/fail ] define-inverse | 
					
						
							| 
									
										
										
										
											2008-12-07 00:38:04 -05:00
										 |  |  | \ pick [ [ pick ] dip =/fail ] define-inverse | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | \ tuck [ swapd [ =/fail ] keep ] define-inverse | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-13 10:58:31 -05:00
										 |  |  | \ not define-involution | 
					
						
							| 
									
										
										
										
											2008-11-06 14:20:27 -05:00
										 |  |  | \ >boolean [ { t f } memq? assure ] define-inverse | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-13 10:39:34 -05:00
										 |  |  | \ tuple>array \ >tuple define-dual | 
					
						
							| 
									
										
										
										
											2009-01-13 10:58:31 -05:00
										 |  |  | \ reverse define-involution | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | \ undo 1 [ [ call ] curry ] define-pop-inverse | 
					
						
							|  |  |  | \ map 1 [ [undo] [ over sequence? assure map ] curry ] define-pop-inverse | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-13 10:39:34 -05:00
										 |  |  | \ exp \ log define-dual | 
					
						
							|  |  |  | \ sq \ sqrt define-dual | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-07 00:38:04 -05:00
										 |  |  | ERROR: missing-literal ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : assert-literal ( n -- n )
 | 
					
						
							| 
									
										
										
										
											2008-12-07 00:38:04 -05:00
										 |  |  |     dup
 | 
					
						
							|  |  |  |     [ word? ] [ symbol? not ] bi and
 | 
					
						
							|  |  |  |     [ missing-literal ] when ;
 | 
					
						
							| 
									
										
										
										
											2007-11-21 17:56:28 -05:00
										 |  |  | \ + [ - ] [ - ] define-math-inverse | 
					
						
							|  |  |  | \ - [ + ] [ - ] define-math-inverse | 
					
						
							|  |  |  | \ * [ / ] [ / ] define-math-inverse | 
					
						
							|  |  |  | \ / [ * ] [ / ] define-math-inverse | 
					
						
							| 
									
										
										
										
											2009-02-04 18:31:25 -05:00
										 |  |  | \ ^ [ recip ^ ] [ swap [ log ] bi@ / ] define-math-inverse | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | \ ? 2 [ | 
					
						
							| 
									
										
										
										
											2008-03-29 21:36:58 -04:00
										 |  |  |     [ assert-literal ] bi@
 | 
					
						
							| 
									
										
										
										
											2008-12-07 00:38:04 -05:00
										 |  |  |     [ swap [ over = ] dip swap [ 2drop f ] [ = [ t ] [ fail ] if ] if ] | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     2curry
 | 
					
						
							|  |  |  | ] define-pop-inverse | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-07 01:45:46 -04:00
										 |  |  | DEFER: _ | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | \ _ [ drop ] define-inverse | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : both ( object object -- object )
 | 
					
						
							|  |  |  |     dupd assert= ;
 | 
					
						
							|  |  |  | \ both [ dup ] define-inverse | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : assure-length ( seq length -- seq )
 | 
					
						
							|  |  |  |     over length =/fail ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     { >array array? } | 
					
						
							|  |  |  |     { >vector vector? } | 
					
						
							|  |  |  |     { >fixnum fixnum? } | 
					
						
							|  |  |  |     { >bignum bignum? } | 
					
						
							|  |  |  |     { >bit-array bit-array? } | 
					
						
							|  |  |  |     { >float float? } | 
					
						
							|  |  |  |     { >byte-array byte-array? } | 
					
						
							|  |  |  |     { >string string? } | 
					
						
							|  |  |  |     { >sbuf sbuf? } | 
					
						
							|  |  |  |     { >quotation quotation? } | 
					
						
							|  |  |  | } [ \ dup swap \ assure 3array >quotation define-inverse ] assoc-each
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! These actually work on all seqs--should they? | 
					
						
							|  |  |  | \ 1array [ 1 assure-length first ] define-inverse | 
					
						
							|  |  |  | \ 2array [ 2 assure-length first2 ] define-inverse | 
					
						
							|  |  |  | \ 3array [ 3 assure-length first3 ] define-inverse | 
					
						
							|  |  |  | \ 4array [ 4 assure-length first4 ] define-inverse | 
					
						
							| 
									
										
										
										
											2009-02-04 18:31:25 -05:00
										 |  |  | \ narray 1 [ [ firstn ] curry ] define-pop-inverse | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | \ first [ 1array ] define-inverse | 
					
						
							|  |  |  | \ first2 [ 2array ] define-inverse | 
					
						
							|  |  |  | \ first3 [ 3array ] define-inverse | 
					
						
							|  |  |  | \ first4 [ 4array ] define-inverse | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-13 10:39:34 -05:00
										 |  |  | \ prefix \ unclip define-dual | 
					
						
							| 
									
										
										
										
											2008-05-07 02:38:34 -04:00
										 |  |  | \ suffix [ dup but-last swap peek ] define-inverse | 
					
						
							| 
									
										
										
										
											2008-04-13 00:59:11 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-04 15:59:55 -05:00
										 |  |  | \ append 1 [ [ ?tail assure ] curry ] define-pop-inverse | 
					
						
							|  |  |  | \ prepend 1 [ [ ?head assure ] curry ] define-pop-inverse | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! Constructor inverse | 
					
						
							|  |  |  | : deconstruct-pred ( class -- quot )
 | 
					
						
							|  |  |  |     "predicate" word-prop [ dupd call assure ] curry ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : slot-readers ( class -- quot )
 | 
					
						
							| 
									
										
										
										
											2008-09-03 13:33:00 -04:00
										 |  |  |     all-slots | 
					
						
							| 
									
										
										
										
											2008-09-02 17:01:19 -04:00
										 |  |  |     [ name>> reader-word 1quotation [ keep ] curry ] map concat
 | 
					
						
							| 
									
										
										
										
											2007-11-22 00:43:30 -05:00
										 |  |  |     [ ] like [ drop ] compose ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : ?wrapped ( object -- wrapped )
 | 
					
						
							| 
									
										
										
										
											2008-06-30 17:06:14 -04:00
										 |  |  |     dup wrapper? [ wrapped>> ] when ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : boa-inverse ( class -- quot )
 | 
					
						
							| 
									
										
										
										
											2008-12-07 00:38:04 -05:00
										 |  |  |     [ deconstruct-pred ] [ slot-readers ] bi compose ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-13 16:06:27 -04:00
										 |  |  | \ boa 1 [ ?wrapped boa-inverse ] define-pop-inverse | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : empty-inverse ( class -- quot )
 | 
					
						
							|  |  |  |     deconstruct-pred | 
					
						
							| 
									
										
										
										
											2009-01-29 23:19:07 -05:00
										 |  |  |     [ tuple>array rest [ ] any? [ fail ] when ] | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     compose ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-13 16:06:27 -04:00
										 |  |  | \ new 1 [ ?wrapped empty-inverse ] define-pop-inverse | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! More useful inverse-based combinators | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : recover-fail ( try fail -- )
 | 
					
						
							|  |  |  |     [ drop call ] [ | 
					
						
							| 
									
										
										
										
											2008-12-07 00:38:04 -05:00
										 |  |  |         [ nip ] dip dup fail? | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |         [ drop call ] [ nip throw ] if
 | 
					
						
							|  |  |  |     ] recover ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-22 00:43:30 -05:00
										 |  |  | : true-out ( quot effect -- quot' )
 | 
					
						
							| 
									
										
										
										
											2008-12-02 17:28:11 -05:00
										 |  |  |     out>> '[ @ __ ndrop t ] ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-22 00:43:30 -05:00
										 |  |  | : false-recover ( effect -- quot )
 | 
					
						
							| 
									
										
										
										
											2008-08-29 11:28:01 -04:00
										 |  |  |     in>> [ ndrop f ] curry [ recover-fail ] curry ;
 | 
					
						
							| 
									
										
										
										
											2007-11-22 00:43:30 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : [matches?] ( quot -- undoes?-quot )
 | 
					
						
							| 
									
										
										
										
											2008-12-07 00:38:04 -05:00
										 |  |  |     [undo] dup infer [ true-out ] [ false-recover ] bi curry ;
 | 
					
						
							| 
									
										
										
										
											2007-11-22 00:43:30 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | MACRO: matches? ( quot -- ? ) [matches?] ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-07 00:38:04 -05:00
										 |  |  | ERROR: no-match ;
 | 
					
						
							| 
									
										
										
										
											2007-11-22 00:43:30 -05:00
										 |  |  | M: no-match summary drop "Fall through in switch" ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : recover-chain ( seq -- quot )
 | 
					
						
							|  |  |  |     [ no-match ] [ swap \ recover-fail 3array >quotation ] reduce ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-22 00:43:30 -05:00
										 |  |  | : [switch]  ( quot-alist -- quot )
 | 
					
						
							| 
									
										
										
										
											2008-04-07 01:45:46 -04:00
										 |  |  |     [ dup quotation? [ [ ] swap 2array ] when ] map
 | 
					
						
							| 
									
										
										
										
											2008-12-07 00:38:04 -05:00
										 |  |  |     reverse [ [ [undo] ] dip compose ] { } assoc>map
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     recover-chain ;
 | 
					
						
							| 
									
										
										
										
											2007-11-22 00:43:30 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | MACRO: switch ( quot-alist -- ) [switch] ;
 |