| 
									
										
										
										
											2012-09-26 20:01:19 -04:00
										 |  |  | ! Copyright (C) 2009, 2011 Doug Coleman, John Benediktsson. | 
					
						
							| 
									
										
										
										
											2009-01-08 19:56:49 -05:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2012-09-26 20:01:19 -04:00
										 |  |  | USING: accessors arrays assocs combinators effects fry | 
					
						
							|  |  |  | generalizations kernel macros math math.order memoize sequences | 
					
						
							|  |  |  | sequences.generalizations sequences.private stack-checker | 
					
						
							|  |  |  | stack-checker.backend stack-checker.errors stack-checker.values | 
					
						
							|  |  |  | stack-checker.visitor words ;
 | 
					
						
							| 
									
										
										
										
											2009-01-08 19:56:49 -05:00
										 |  |  | IN: combinators.smart | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-27 19:21:20 -05:00
										 |  |  | GENERIC: infer-known* ( known -- effect )
 | 
					
						
							| 
									
										
										
										
											2009-03-28 23:26:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-27 19:21:20 -05:00
										 |  |  | : infer-known ( value -- effect )
 | 
					
						
							|  |  |  |     known dup (literal-value?) [ | 
					
						
							|  |  |  |         (literal) [ infer-literal-quot ] with-infer drop
 | 
					
						
							|  |  |  |     ] [ infer-known* ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-08-21 22:17:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-30 17:32:25 -05:00
										 |  |  | IDENTITY-MEMO: inputs/outputs ( quot -- in out )
 | 
					
						
							| 
									
										
										
										
											2011-11-27 19:21:20 -05:00
										 |  |  |     infer [ in>> ] [ out>> ] bi [ length ] bi@ ;
 | 
					
						
							| 
									
										
										
										
											2009-01-08 19:56:49 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-27 19:21:20 -05:00
										 |  |  | : inputs ( quot -- n ) inputs/outputs drop ; inline
 | 
					
						
							| 
									
										
										
										
											2009-01-08 19:56:49 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-27 19:21:20 -05:00
										 |  |  | : outputs ( quot -- n ) inputs/outputs nip ; inline
 | 
					
						
							| 
									
										
										
										
											2009-01-08 19:56:49 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-27 19:21:20 -05:00
										 |  |  | \ inputs/outputs [ | 
					
						
							| 
									
										
										
										
											2011-11-28 17:36:57 -05:00
										 |  |  |     peek-d | 
					
						
							|  |  |  |     infer-known [ | 
					
						
							|  |  |  |         [ pop-d 1array #drop, ] | 
					
						
							|  |  |  |         [ [ in>> ] [ out>> ] bi [ length apply-object ] bi@ ] bi*
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         \ inputs/outputs dup required-stack-effect apply-word/effect | 
					
						
							| 
									
										
										
										
											2011-12-01 01:47:20 -05:00
										 |  |  |         pop-d pop-d swap
 | 
					
						
							|  |  |  |         [ [ input-parameter swap set-known ] [ push-d ] bi ] bi@
 | 
					
						
							| 
									
										
										
										
											2011-11-28 17:36:57 -05:00
										 |  |  |     ] if*
 | 
					
						
							| 
									
										
										
										
											2011-11-27 19:21:20 -05:00
										 |  |  | ] "special" set-word-prop | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: curried infer-known* | 
					
						
							| 
									
										
										
										
											2011-11-28 23:54:17 -05:00
										 |  |  |     quot>> infer-known dup [ | 
					
						
							|  |  |  |         curry-effect | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         drop f
 | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							| 
									
										
										
										
											2011-11-27 19:21:20 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: composed infer-known* | 
					
						
							|  |  |  |     [ quot1>> ] [ quot2>> ] bi
 | 
					
						
							| 
									
										
										
										
											2011-11-28 23:54:17 -05:00
										 |  |  |     [ infer-known ] bi@
 | 
					
						
							|  |  |  |     2dup and [ compose-effects ] [ 2drop f ] if ;
 | 
					
						
							| 
									
										
										
										
											2011-11-27 19:21:20 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: declared-effect infer-known* | 
					
						
							|  |  |  |     known>> infer-known* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-28 23:54:17 -05:00
										 |  |  | M: input-parameter infer-known* drop f ;
 | 
					
						
							| 
									
										
										
										
											2011-11-27 19:21:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-28 17:36:57 -05:00
										 |  |  | M: object infer-known* drop f ;
 | 
					
						
							| 
									
										
										
										
											2011-11-27 19:21:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-28 19:18:51 -05:00
										 |  |  | : drop-inputs ( quot -- )
 | 
					
						
							| 
									
										
										
										
											2011-11-28 15:41:50 -05:00
										 |  |  |     inputs ndrop ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-27 19:21:20 -05:00
										 |  |  | : drop-outputs ( quot -- )
 | 
					
						
							|  |  |  |     [ call ] [ outputs ndrop ] bi ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : keep-inputs ( quot -- )
 | 
					
						
							|  |  |  |     [ ] [ inputs ] bi nkeep ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-28 19:18:51 -05:00
										 |  |  | : output>sequence ( quot exemplar -- seq )
 | 
					
						
							| 
									
										
										
										
											2011-11-27 19:21:20 -05:00
										 |  |  |     [ [ call ] [ outputs ] bi ] dip nsequence ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-28 19:18:51 -05:00
										 |  |  | : output>array ( quot -- array )
 | 
					
						
							| 
									
										
										
										
											2011-11-27 19:21:20 -05:00
										 |  |  |     { } output>sequence ; inline
 | 
					
						
							| 
									
										
										
										
											2015-06-29 19:43:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-06 04:20:10 -04:00
										 |  |  | MACRO: output>sequence-n ( quot exemplar n -- quot )
 | 
					
						
							|  |  |  |     3dup nip [ outputs ] dip - -rot
 | 
					
						
							|  |  |  |     '[ @ [ _ _ nsequence ] _ ndip ] ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MACRO: output>array-n ( quot n -- array )
 | 
					
						
							|  |  |  |     '[ _ { } _ output>sequence-n ] ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-13 21:10:19 -04:00
										 |  |  | : cleave>array ( obj quots -- array )
 | 
					
						
							| 
									
										
										
										
											2012-09-16 20:32:27 -04:00
										 |  |  |     '[ _ cleave ] output>array ; inline
 | 
					
						
							| 
									
										
										
										
											2011-11-27 19:21:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-19 17:58:55 -04:00
										 |  |  | : cleave>sequence ( x seq exemplar -- array )
 | 
					
						
							|  |  |  |     [ '[ _ cleave ] ] dip output>sequence ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-27 19:21:20 -05:00
										 |  |  | : input<sequence ( seq quot -- )
 | 
					
						
							|  |  |  |     [ inputs firstn ] [ call ] bi ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : input<sequence-unsafe ( seq quot -- )
 | 
					
						
							|  |  |  |     [ inputs firstn-unsafe ] [ call ] bi ; inline
 | 
					
						
							| 
									
										
										
										
											2009-04-26 22:22:06 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-28 19:18:51 -05:00
										 |  |  | : reduce-outputs ( quot operation -- )
 | 
					
						
							|  |  |  |     [ [ call ] [ [ drop ] compose outputs ] bi ] dip swap call-n ; inline
 | 
					
						
							| 
									
										
										
										
											2009-01-08 19:56:49 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-07 15:26:35 -05:00
										 |  |  | : sum-outputs ( quot -- n )
 | 
					
						
							| 
									
										
										
										
											2011-11-28 19:18:51 -05:00
										 |  |  |     [ + ] reduce-outputs ; inline
 | 
					
						
							| 
									
										
										
										
											2011-11-27 19:21:20 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : map-outputs ( quot mapper -- )
 | 
					
						
							|  |  |  |     [ drop call ] [ swap outputs ] 2bi napply ; inline
 | 
					
						
							| 
									
										
										
										
											2009-01-18 22:18:52 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-19 14:12:37 -05:00
										 |  |  | MACRO: map-reduce-outputs ( quot mapper reducer -- quot )
 | 
					
						
							|  |  |  |     [ '[ _ _ map-outputs ] ] dip '[ _ _ reduce-outputs ] ;
 | 
					
						
							| 
									
										
										
										
											2009-02-12 03:19:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-28 19:18:51 -05:00
										 |  |  | : append-outputs-as ( quot exemplar -- seq )
 | 
					
						
							| 
									
										
										
										
											2011-11-28 15:41:50 -05:00
										 |  |  |     [ [ call ] [ outputs ] bi ] dip nappend-as ; inline
 | 
					
						
							| 
									
										
										
										
											2009-01-18 22:18:52 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-28 15:41:50 -05:00
										 |  |  | : append-outputs ( quot -- seq )
 | 
					
						
							|  |  |  |     { } append-outputs-as ; inline
 | 
					
						
							| 
									
										
										
										
											2009-08-19 05:38:59 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-28 15:41:50 -05:00
										 |  |  | : preserving ( quot -- )
 | 
					
						
							|  |  |  |     [ inputs ndup ] [ call ] bi ; inline
 | 
					
						
							| 
									
										
										
										
											2009-08-19 05:38:59 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-28 15:41:50 -05:00
										 |  |  | : dropping ( quot -- quot' )
 | 
					
						
							|  |  |  |     inputs '[ _ ndrop ] ; inline
 | 
					
						
							| 
									
										
										
										
											2010-04-29 20:57:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-28 19:18:51 -05:00
										 |  |  | : nullary ( quot -- )
 | 
					
						
							| 
									
										
										
										
											2011-11-28 15:41:50 -05:00
										 |  |  |     dropping call ; inline
 | 
					
						
							| 
									
										
										
										
											2010-04-29 20:57:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-28 19:18:51 -05:00
										 |  |  | : smart-if ( pred true false -- )
 | 
					
						
							| 
									
										
										
										
											2011-11-28 15:41:50 -05:00
										 |  |  |     [ preserving ] 2dip if ; inline
 | 
					
						
							| 
									
										
										
										
											2010-01-29 14:47:06 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-28 19:18:51 -05:00
										 |  |  | : smart-when ( pred true -- )
 | 
					
						
							| 
									
										
										
										
											2011-11-28 15:41:50 -05:00
										 |  |  |     [ ] smart-if ; inline
 | 
					
						
							| 
									
										
										
										
											2010-04-29 20:57:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-28 19:18:51 -05:00
										 |  |  | : smart-unless ( pred false -- )
 | 
					
						
							| 
									
										
										
										
											2011-11-28 15:41:50 -05:00
										 |  |  |     [ [ ] ] dip smart-if ; inline
 | 
					
						
							| 
									
										
										
										
											2010-04-29 20:57:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-28 19:18:51 -05:00
										 |  |  | : smart-if* ( pred true false -- )
 | 
					
						
							| 
									
										
										
										
											2011-11-28 15:41:50 -05:00
										 |  |  |     [ [ [ preserving ] [ dropping ] bi ] dip swap ] dip compose if ; inline
 | 
					
						
							| 
									
										
										
										
											2010-04-29 20:57:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-28 19:18:51 -05:00
										 |  |  | : smart-when* ( pred true -- )
 | 
					
						
							| 
									
										
										
										
											2011-11-28 15:41:50 -05:00
										 |  |  |     [ ] smart-if* ; inline
 | 
					
						
							| 
									
										
										
										
											2010-04-29 20:57:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-28 19:18:51 -05:00
										 |  |  | : smart-unless* ( pred false -- )
 | 
					
						
							| 
									
										
										
										
											2011-11-28 15:41:50 -05:00
										 |  |  |     [ [ ] ] dip smart-if* ; inline
 | 
					
						
							| 
									
										
										
										
											2010-04-29 20:57:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-28 19:18:51 -05:00
										 |  |  | : smart-apply ( quot n -- )
 | 
					
						
							| 
									
										
										
										
											2011-11-28 15:41:50 -05:00
										 |  |  |     [ dup inputs ] dip mnapply ; inline
 | 
					
						
							| 
									
										
										
										
											2012-08-13 22:32:12 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : smart-with ( param obj quot -- obj curry )
 | 
					
						
							|  |  |  |     swapd dup inputs '[ [ _ -nrot ] dip call ] 2curry ; inline
 | 
					
						
							| 
									
										
										
										
											2012-09-26 20:01:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | MACRO: smart-reduce ( reduce-quots -- quot )
 | 
					
						
							|  |  |  |     unzip [ [ ] like ] bi@ dup length dup '[ | 
					
						
							|  |  |  |         [ @ ] dip [ @ _ cleave-curry _ spread* ] each
 | 
					
						
							|  |  |  |     ] ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MACRO: smart-map-reduce ( map-reduce-quots -- quot )
 | 
					
						
							|  |  |  |     [ keys ] [ [ [ ] concat-as ] [ ] map-as ] bi dup length dup '[ | 
					
						
							|  |  |  |         [ first _ cleave ] keep
 | 
					
						
							|  |  |  |         [ @ _ cleave-curry _ spread* ] | 
					
						
							| 
									
										
										
										
											2016-03-25 06:10:47 -04:00
										 |  |  |         [ 1 ] 2dip setup-each (each-integer)
 | 
					
						
							| 
									
										
										
										
											2012-09-26 20:01:19 -04:00
										 |  |  |     ] ;
 | 
					
						
							| 
									
										
										
										
											2012-09-26 20:42:59 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | MACRO: smart-2reduce ( 2reduce-quots -- quot )
 | 
					
						
							|  |  |  |     unzip [ [ ] like ] bi@ dup length dup '[ | 
					
						
							|  |  |  |         [ @ ] 2dip
 | 
					
						
							|  |  |  |         [ @ _ [ cleave-curry ] [ cleave-curry ] bi _ spread* ] 2each
 | 
					
						
							|  |  |  |     ] ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MACRO: smart-2map-reduce ( 2map-reduce-quots -- quot )
 | 
					
						
							|  |  |  |     [ keys ] [ [ [ ] concat-as ] [ ] map-as ] bi dup length dup '[ | 
					
						
							|  |  |  |         [ [ first ] bi@ _ 2cleave ] 2keep
 | 
					
						
							|  |  |  |         [ @ _ [ cleave-curry ] [ cleave-curry ] bi _ spread* ] | 
					
						
							|  |  |  |         [ 1 ] 3dip (2each) (each-integer)
 | 
					
						
							|  |  |  |     ] ;
 |