| 
									
										
										
										
											2008-01-01 14:54:14 -05:00
										 |  |  | ! Copyright (C) 2004, 2008 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-01-01 14:54:14 -05:00
										 |  |  | USING: arrays generic assocs kernel math namespaces parser | 
					
						
							|  |  |  | sequences words vectors math.intervals effects classes | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  | inference.state accessors combinators ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: inference.dataflow | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Computed value | 
					
						
							| 
									
										
										
										
											2008-06-08 16:32:55 -04:00
										 |  |  | : <computed> ( -- value ) \ <computed> counter ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! Literal value | 
					
						
							| 
									
										
										
										
											2008-04-02 22:27:49 -04:00
										 |  |  | TUPLE: value < identity-tuple literal uid recursion ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : <value> ( obj -- value )
 | 
					
						
							| 
									
										
										
										
											2008-04-13 16:06:09 -04:00
										 |  |  |     <computed> recursive-state get value boa ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: value hashcode* nip value-uid ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Result of curry | 
					
						
							|  |  |  | TUPLE: curried obj quot ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | C: <curried> curried | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Result of compose | 
					
						
							|  |  |  | TUPLE: composed quot1 quot2 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | C: <composed> composed | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | UNION: special curried composed ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-02 22:27:49 -04:00
										 |  |  | TUPLE: node < identity-tuple | 
					
						
							|  |  |  | param | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | in-d out-d in-r out-r | 
					
						
							|  |  |  | classes literals intervals | 
					
						
							|  |  |  | history successor children ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: node hashcode* drop node hashcode* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: flatten-curry ( value -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: curried flatten-curry | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  |     [ obj>> flatten-curry ] | 
					
						
							|  |  |  |     [ quot>> flatten-curry ] bi ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: composed flatten-curry | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  |     [ quot1>> flatten-curry ] | 
					
						
							|  |  |  |     [ quot2>> flatten-curry ] bi ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: object flatten-curry , ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : flatten-curries ( seq -- newseq )
 | 
					
						
							|  |  |  |     dup [ special? ] contains? [ | 
					
						
							|  |  |  |         [ [ flatten-curry ] each ] { } make | 
					
						
							|  |  |  |     ] when ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : flatten-meta-d ( -- seq )
 | 
					
						
							|  |  |  |     meta-d get clone flatten-curries ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : modify-values ( node quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  |     { | 
					
						
							|  |  |  |         [ change-in-d ] | 
					
						
							|  |  |  |         [ change-in-r ] | 
					
						
							|  |  |  |         [ change-out-d ] | 
					
						
							|  |  |  |         [ change-out-r ] | 
					
						
							|  |  |  |     } cleave drop ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : node-shuffle ( node -- shuffle )
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  |     [ in-d>> ] [ out-d>> ] bi <effect> ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : param-node ( param class -- node )
 | 
					
						
							| 
									
										
										
										
											2008-04-13 16:06:09 -04:00
										 |  |  |     new swap >>param ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : in-node ( seq class -- node )
 | 
					
						
							| 
									
										
										
										
											2008-04-13 16:06:09 -04:00
										 |  |  |     new swap >>in-d ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : all-in-node ( class -- node )
 | 
					
						
							|  |  |  |     flatten-meta-d swap in-node ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : out-node ( seq class -- node )
 | 
					
						
							| 
									
										
										
										
											2008-04-13 16:06:09 -04:00
										 |  |  |     new swap >>out-d ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : all-out-node ( class -- node )
 | 
					
						
							|  |  |  |     flatten-meta-d swap out-node ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : d-tail ( n -- seq )
 | 
					
						
							|  |  |  |     dup zero? [ drop f ] [ meta-d get swap tail* ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : r-tail ( n -- seq )
 | 
					
						
							|  |  |  |     dup zero? [ drop f ] [ meta-r get swap tail* ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-08 16:32:55 -04:00
										 |  |  | : node-child ( node -- child ) node-children first ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-19 03:11:55 -04:00
										 |  |  | TUPLE: #label < node word loop? returns calls ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : #label ( word label -- node )
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  |     \ #label param-node swap >>word ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-26 19:23:19 -04:00
										 |  |  | PREDICATE: #loop < #label #label-loop? ;
 | 
					
						
							| 
									
										
										
										
											2008-02-12 21:35:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  | TUPLE: #entry < node ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : #entry ( -- node ) \ #entry all-out-node ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  | TUPLE: #call < node ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : #call ( word -- node ) \ #call param-node ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  | TUPLE: #call-label < node ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : #call-label ( label -- node ) \ #call-label param-node ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  | TUPLE: #push < node ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-13 16:06:09 -04:00
										 |  |  | : #push ( -- node ) \ #push new ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  | TUPLE: #shuffle < node ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-13 16:06:09 -04:00
										 |  |  | : #shuffle ( -- node ) \ #shuffle new ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  | TUPLE: #>r < node ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-13 16:06:09 -04:00
										 |  |  | : #>r ( -- node ) \ #>r new ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  | TUPLE: #r> < node ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-13 16:06:09 -04:00
										 |  |  | : #r> ( -- node ) \ #r> new ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  | TUPLE: #values < node ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : #values ( -- node ) \ #values all-in-node ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  | TUPLE: #return < node ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : #return ( label -- node )
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  |     \ #return all-in-node swap >>param ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: #branch < node ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  | TUPLE: #if < #branch ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : #if ( -- node ) peek-d 1array \ #if in-node ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  | TUPLE: #dispatch < #branch ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : #dispatch ( -- node ) peek-d 1array \ #dispatch in-node ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  | TUPLE: #merge < node ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : #merge ( -- node ) \ #merge all-out-node ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  | TUPLE: #terminate < node ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-13 16:06:09 -04:00
										 |  |  | : #terminate ( -- node ) \ #terminate new ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  | TUPLE: #declare < node ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : #declare ( classes -- node ) \ #declare param-node ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : node-inputs ( d-count r-count node -- )
 | 
					
						
							|  |  |  |     tuck | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  |     [ swap d-tail flatten-curries >>in-d drop ] | 
					
						
							|  |  |  |     [ swap r-tail flatten-curries >>in-r drop ] 2bi* ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : node-outputs ( d-count r-count node -- )
 | 
					
						
							|  |  |  |     tuck | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  |     [ swap d-tail flatten-curries >>out-d drop ] | 
					
						
							|  |  |  |     [ swap r-tail flatten-curries >>out-r drop ] 2bi* ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : node, ( node -- )
 | 
					
						
							|  |  |  |     dataflow-graph get [ | 
					
						
							|  |  |  |         dup current-node [ set-node-successor ] change
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         dup dataflow-graph set  current-node set
 | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : node-values ( node -- values )
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  |     { [ in-d>> ] [ out-d>> ] [ in-r>> ] [ out-r>> ] } cleave
 | 
					
						
							|  |  |  |     4array concat ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : last-node ( node -- last )
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  |     dup successor>> [ last-node ] [ ] ?if ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : penultimate-node ( node -- penultimate )
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  |     dup successor>> dup [ | 
					
						
							|  |  |  |         dup successor>> | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |         [ nip penultimate-node ] [ drop ] if
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         2drop f
 | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : #drop ( n -- #shuffle )
 | 
					
						
							|  |  |  |     d-tail flatten-curries \ #shuffle in-node ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : node-exists? ( node quot -- ? )
 | 
					
						
							|  |  |  |     over [ | 
					
						
							|  |  |  |         2dup 2slip rot [ | 
					
						
							|  |  |  |             2drop t
 | 
					
						
							|  |  |  |         ] [ | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  |             >r [ children>> ] [ successor>> ] bi suffix r> | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |             [ node-exists? ] curry contains? | 
					
						
							|  |  |  |         ] if
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         2drop f
 | 
					
						
							|  |  |  |     ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: calls-label* ( label node -- ? )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: node calls-label* 2drop f ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  | M: #call-label calls-label* param>> eq? ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : calls-label? ( label node -- ? )
 | 
					
						
							| 
									
										
										
										
											2008-01-09 17:36:30 -05:00
										 |  |  |     [ calls-label* ] with node-exists? ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : recursive-label? ( node -- ? )
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  |     [ param>> ] keep calls-label? ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | SYMBOL: node-stack | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-08 16:32:55 -04:00
										 |  |  | : >node ( node -- ) node-stack get push ;
 | 
					
						
							|  |  |  | : node> ( -- node ) node-stack get pop ;
 | 
					
						
							|  |  |  | : node@ ( -- node ) node-stack get peek ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  | : iterate-next ( -- node ) node@ successor>> ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : iterate-nodes ( node quot -- )
 | 
					
						
							|  |  |  |     over [ | 
					
						
							|  |  |  |         [ swap >node call node> drop ] keep iterate-nodes | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         2drop
 | 
					
						
							|  |  |  |     ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (each-node) ( quot -- next )
 | 
					
						
							|  |  |  |     node@ [ swap call ] 2keep
 | 
					
						
							|  |  |  |     node-children [ | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |             [ (each-node) ] keep swap
 | 
					
						
							|  |  |  |         ] iterate-nodes | 
					
						
							|  |  |  |     ] each drop
 | 
					
						
							|  |  |  |     iterate-next ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : with-node-iterator ( quot -- )
 | 
					
						
							|  |  |  |     >r V{ } clone node-stack r> with-variable ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : each-node ( node quot -- )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         swap [ | 
					
						
							|  |  |  |             [ (each-node) ] keep swap
 | 
					
						
							|  |  |  |         ] iterate-nodes drop
 | 
					
						
							|  |  |  |     ] with-node-iterator ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  | : map-children ( node quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-02-10 21:32:48 -05:00
										 |  |  |     over [ | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  |         over children>> [ | 
					
						
							|  |  |  |             [ map ] curry change-children drop
 | 
					
						
							|  |  |  |         ] [ | 
					
						
							|  |  |  |             2drop
 | 
					
						
							|  |  |  |         ] if
 | 
					
						
							| 
									
										
										
										
											2008-02-10 21:32:48 -05:00
										 |  |  |     ] [ | 
					
						
							|  |  |  |         2drop
 | 
					
						
							|  |  |  |     ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (transform-nodes) ( prev node quot -- )
 | 
					
						
							|  |  |  |     dup >r call dup [ | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  |         >>successor | 
					
						
							|  |  |  |         successor>> dup successor>> | 
					
						
							|  |  |  |         r> (transform-nodes) | 
					
						
							| 
									
										
										
										
											2008-02-10 21:32:48 -05:00
										 |  |  |     ] [ | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  |         r> 2drop f >>successor drop
 | 
					
						
							| 
									
										
										
										
											2008-02-10 21:32:48 -05:00
										 |  |  |     ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : transform-nodes ( node quot -- new-node )
 | 
					
						
							|  |  |  |     over [ | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  |         [ call dup dup successor>> ] keep (transform-nodes) | 
					
						
							| 
									
										
										
										
											2008-02-10 21:32:48 -05:00
										 |  |  |     ] [ drop ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : node-literal? ( node value -- ? )
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  |     dup value? >r swap literals>> key? r> or ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : node-literal ( node value -- obj )
 | 
					
						
							|  |  |  |     dup value?
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  |     [ nip value-literal ] [ swap literals>> at ] if ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : node-interval ( node value -- interval )
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  |     swap intervals>> at ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : node-class ( node value -- class )
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  |     swap classes>> at object or ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : node-input-classes ( node -- seq )
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  |     dup in-d>> [ node-class ] with map ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-19 03:11:55 -04:00
										 |  |  | : node-output-classes ( node -- seq )
 | 
					
						
							|  |  |  |     dup out-d>> [ node-class ] with map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : node-input-intervals ( node -- seq )
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  |     dup in-d>> [ node-interval ] with map ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : node-class-first ( node -- class )
 | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  |     dup in-d>> first node-class ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : active-children ( node -- seq )
 | 
					
						
							| 
									
										
										
										
											2008-04-26 00:12:44 -04:00
										 |  |  |     children>> [ last-node ] map [ #terminate? not ] filter ;
 | 
					
						
							| 
									
										
										
										
											2008-02-12 18:32:17 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | DEFER: #tail? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-26 19:23:19 -04:00
										 |  |  | PREDICATE: #tail-merge < #merge node-successor #tail? ;
 | 
					
						
							| 
									
										
										
										
											2008-02-12 18:32:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-26 19:23:19 -04:00
										 |  |  | PREDICATE: #tail-values < #values node-successor #tail? ;
 | 
					
						
							| 
									
										
										
										
											2008-02-12 18:32:17 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | UNION: #tail | 
					
						
							| 
									
										
										
										
											2008-02-13 19:42:55 -05:00
										 |  |  |     POSTPONE: f #return #tail-values #tail-merge #terminate ;
 | 
					
						
							| 
									
										
										
										
											2008-02-12 18:32:17 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : tail-call? ( -- ? )
 | 
					
						
							| 
									
										
										
										
											2008-02-14 21:27:18 -05:00
										 |  |  |     #! We don't consider calls which do non-local exits to be | 
					
						
							|  |  |  |     #! tail calls, because this gives better error traces. | 
					
						
							|  |  |  |     node-stack get [ | 
					
						
							| 
									
										
										
										
											2008-04-04 01:33:06 -04:00
										 |  |  |         successor>> [ #tail? ] [ #terminate? not ] bi and
 | 
					
						
							| 
									
										
										
										
											2008-02-14 21:27:18 -05:00
										 |  |  |     ] all? ;
 |