| 
									
										
										
										
											2009-05-01 23:14:26 -04:00
										 |  |  | ! Copyright (C) 2007, 2009 Daniel Ehrenberg. | 
					
						
							| 
									
										
										
										
											2008-06-30 17:06:14 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2009-05-27 11:26:26 -04:00
										 |  |  | USING: accessors kernel locals words summary slots quotations | 
					
						
							| 
									
										
										
										
											2010-02-16 06:32:53 -05:00
										 |  |  | sequences assocs math arrays stack-checker effects continuations | 
					
						
							|  |  |  | classes.tuple namespaces make vectors bit-arrays byte-arrays | 
					
						
							|  |  |  | strings sbufs math.functions macros sequences.private | 
					
						
							|  |  |  | combinators mirrors splitting combinators.smart | 
					
						
							| 
									
										
										
										
											2009-05-01 23:14:26 -04:00
										 |  |  | combinators.short-circuit fry words.symbol generalizations | 
					
						
							| 
									
										
										
										
											2010-05-18 18:36:47 -04:00
										 |  |  | sequences.generalizations classes ;
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-01 23:14:26 -04:00
										 |  |  | : assure ( ? -- ) [ fail ] unless ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-01 23:14:26 -04:00
										 |  |  | : =/fail ( obj1 obj2 -- ) = assure ; inline
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-29 13:52:46 -04:00
										 |  |  | PREDICATE: normal-inverse < word "inverse" word-prop >boolean ;
 | 
					
						
							|  |  |  | PREDICATE: math-inverse < word "math-inverse" word-prop >boolean ;
 | 
					
						
							|  |  |  | PREDICATE: pop-inverse < word "pop-length" word-prop >boolean ;
 | 
					
						
							| 
									
										
										
										
											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 ] [ | 
					
						
							| 
									
										
										
										
											2010-01-14 14:04:14 -05:00
										 |  |  |         [ [ length ] [ 1quotation inputs ] 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-04-22 20:35:51 -04:00
										 |  |  |     [ 1quotation with-datastack ] | 
					
						
							|  |  |  |     [ [ [ literalize , ] each ] [ , ] 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>> | 
					
						
							| 
									
										
										
										
											2009-10-28 16:02:00 -04:00
										 |  |  |                 [ visited get member-eq? [ no-recursive-inverse ] when ] | 
					
						
							| 
									
										
										
										
											2009-01-06 22:14:22 -05:00
										 |  |  |                 [ 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-01 23:14:26 -04:00
										 |  |  | \ bi@ 1 [ [undo] '[ _ bi@ ] ] define-pop-inverse | 
					
						
							|  |  |  | \ tri@ 1 [ [undo] '[ _ tri@ ] ] define-pop-inverse | 
					
						
							|  |  |  | \ bi* 2 [ [ [undo] ] bi@ '[ _ _ bi* ] ] define-pop-inverse | 
					
						
							|  |  |  | \ tri* 3 [ [ [undo] ] tri@ '[ _ _ _ tri* ] ] define-pop-inverse | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-13 10:58:31 -05:00
										 |  |  | \ not define-involution | 
					
						
							| 
									
										
										
										
											2009-10-28 16:02:00 -04:00
										 |  |  | \ >boolean [ dup { t f } member-eq? assure ] define-inverse | 
					
						
							| 
									
										
										
										
											2008-11-06 14:20:27 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-01 23:14:26 -04:00
										 |  |  | \ undo 1 [ ] define-pop-inverse | 
					
						
							|  |  |  | \ map 1 [ [undo] '[ dup sequence? assure _ map ] ] define-pop-inverse | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-02 12:59:09 -04:00
										 |  |  | \ e^ \ log define-dual | 
					
						
							| 
									
										
										
										
											2009-01-13 10:39:34 -05:00
										 |  |  | \ 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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-01 23:14:26 -04:00
										 |  |  | DEFER: __ | 
					
						
							|  |  |  | \ __ [ drop ] define-inverse | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : both ( object object -- object )
 | 
					
						
							|  |  |  |     dupd assert= ;
 | 
					
						
							|  |  |  | \ both [ dup ] define-inverse | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     { >array array? } | 
					
						
							|  |  |  |     { >vector vector? } | 
					
						
							|  |  |  |     { >fixnum fixnum? } | 
					
						
							|  |  |  |     { >bignum bignum? } | 
					
						
							|  |  |  |     { >bit-array bit-array? } | 
					
						
							|  |  |  |     { >float float? } | 
					
						
							|  |  |  |     { >byte-array byte-array? } | 
					
						
							|  |  |  |     { >string string? } | 
					
						
							|  |  |  |     { >sbuf sbuf? } | 
					
						
							|  |  |  |     { >quotation quotation? } | 
					
						
							| 
									
										
										
										
											2009-05-01 23:14:26 -04:00
										 |  |  | } [ '[ dup _ execute assure ] define-inverse ] assoc-each
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : assure-length ( seq length -- )
 | 
					
						
							|  |  |  |     swap length =/fail ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : assure-array ( array -- array )
 | 
					
						
							|  |  |  |     dup array? assure ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-01 23:14:26 -04:00
										 |  |  | : undo-narray ( array n -- ... )
 | 
					
						
							|  |  |  |     [ assure-array ] dip
 | 
					
						
							|  |  |  |     [ assure-length ] [ firstn ] 2bi ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \ 1array [ 1 undo-narray ] define-inverse | 
					
						
							|  |  |  | \ 2array [ 2 undo-narray ] define-inverse | 
					
						
							|  |  |  | \ 3array [ 3 undo-narray ] define-inverse | 
					
						
							|  |  |  | \ 4array [ 4 undo-narray ] define-inverse | 
					
						
							|  |  |  | \ narray 1 [ '[ _ undo-narray ] ] 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 | 
					
						
							| 
									
										
										
										
											2009-05-25 17:38:33 -04:00
										 |  |  | \ suffix [ dup but-last swap last ] 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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-01 23:14:26 -04:00
										 |  |  | : assure-same-class ( obj1 obj2 -- )
 | 
					
						
							| 
									
										
										
										
											2012-07-21 13:22:44 -04:00
										 |  |  |     [ class-of ] same? assure ; inline
 | 
					
						
							| 
									
										
										
										
											2009-05-01 23:14:26 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | \ output>sequence 2 [ [undo] '[ dup _ assure-same-class _ input<sequence ] ] define-pop-inverse | 
					
						
							|  |  |  | \ input<sequence 1 [ [undo] '[ _ { } output>sequence ] ] define-pop-inverse | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-27 11:26:26 -04:00
										 |  |  | ! conditionals | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :: undo-if-empty ( result a b -- seq )
 | 
					
						
							|  |  |  |    a call( -- b ) result = [ { } ] [ result b [undo] call( a -- b ) ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :: undo-if* ( result a b -- boolean )
 | 
					
						
							|  |  |  |    b call( -- b ) result = [ f ] [ result a [undo] call( a -- b ) ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \ if-empty 2 [ swap [ undo-if-empty ] 2curry ] define-pop-inverse | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \ if* 2 [ swap [ undo-if* ] 2curry ] define-pop-inverse | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! Constructor inverse | 
					
						
							|  |  |  | : deconstruct-pred ( class -- quot )
 | 
					
						
							| 
									
										
										
										
											2011-11-22 02:00:52 -05:00
										 |  |  |     predicate-def [ dupd call assure ] curry ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : slot-readers ( class -- quot )
 | 
					
						
							| 
									
										
										
										
											2009-08-19 19:53:44 -04:00
										 |  |  |     all-slots [ name>> reader-word 1quotation ] map [ cleave ] curry ;
 | 
					
						
							| 
									
										
										
										
											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' )
 | 
					
						
							| 
									
										
										
										
											2010-01-14 10:10:13 -05:00
										 |  |  |     out>> length '[ @ _ ndrop t ] ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-22 00:43:30 -05:00
										 |  |  | : false-recover ( effect -- quot )
 | 
					
						
							| 
									
										
										
										
											2010-01-14 10:10:13 -05:00
										 |  |  |     in>> length [ 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-19 19:53:44 -04:00
										 |  |  | MACRO: switch ( quot-alist -- ) [switch] ;
 |