2011-11-27 19:21:20 -05:00
|
|
|
! Copyright (C) 2009, 2011 Doug Coleman.
|
2009-01-08 19:56:49 -05:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2011-11-27 19:21:20 -05:00
|
|
|
USING: accessors arrays effects fry generalizations kernel
|
|
|
|
|
macros math math.order sequences sequences.generalizations
|
|
|
|
|
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-27 19:21:20 -05:00
|
|
|
: inputs/outputs ( quot -- in out )
|
|
|
|
|
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 [
|
|
|
|
|
pop-d
|
|
|
|
|
[ 1array #drop, ] [ infer-known ] bi
|
|
|
|
|
[ in>> ] [ out>> ] bi [ length apply-object ] bi@
|
|
|
|
|
] "special" set-word-prop
|
|
|
|
|
|
|
|
|
|
M: curried infer-known*
|
|
|
|
|
quot>> infer-known curry-effect ;
|
|
|
|
|
|
|
|
|
|
M: composed infer-known*
|
|
|
|
|
[ quot1>> ] [ quot2>> ] bi
|
|
|
|
|
[ infer-known ] bi@ compose-effects ;
|
|
|
|
|
|
|
|
|
|
M: declared-effect infer-known*
|
|
|
|
|
known>> infer-known* ;
|
|
|
|
|
|
|
|
|
|
M: input-parameter infer-known* \ inputs/outputs unknown-macro-input ;
|
|
|
|
|
|
|
|
|
|
M: object infer-known* \ inputs/outputs bad-macro-input ;
|
|
|
|
|
|
|
|
|
|
: drop-outputs ( quot -- )
|
|
|
|
|
[ call ] [ outputs ndrop ] bi ; inline
|
|
|
|
|
|
|
|
|
|
: keep-inputs ( quot -- )
|
|
|
|
|
[ ] [ inputs ] bi nkeep ; inline
|
|
|
|
|
|
|
|
|
|
: output>sequence ( quot exemplar -- )
|
|
|
|
|
[ [ call ] [ outputs ] bi ] dip nsequence ; inline
|
|
|
|
|
|
|
|
|
|
: output>array ( quot -- )
|
|
|
|
|
{ } output>sequence ; inline
|
|
|
|
|
|
|
|
|
|
: 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
|
|
|
|
2009-01-09 16:39:47 -05:00
|
|
|
MACRO: reduce-outputs ( quot operation -- newquot )
|
2010-01-14 10:10:13 -05:00
|
|
|
[ dup outputs 1 [-] ] dip n*quot compose ;
|
2009-01-08 19:56:49 -05:00
|
|
|
|
2011-11-27 19:21:20 -05:00
|
|
|
: sum-outputs ( quot -- n )
|
|
|
|
|
[ + ] reduce-outputs ; inline
|
|
|
|
|
|
|
|
|
|
: map-outputs ( quot mapper -- )
|
|
|
|
|
[ drop call ] [ swap outputs ] 2bi napply ; inline
|
2009-01-18 22:18:52 -05:00
|
|
|
|
2011-11-27 19:21:20 -05:00
|
|
|
: map-reduce-outputs ( quot mapper reducer -- )
|
|
|
|
|
[ '[ _ _ map-outputs ] ] dip reduce-outputs ; inline
|
2009-02-12 03:19:41 -05:00
|
|
|
|
2009-01-18 22:18:52 -05:00
|
|
|
MACRO: append-outputs-as ( quot exemplar -- newquot )
|
2010-01-14 10:10:13 -05:00
|
|
|
[ dup outputs ] dip '[ @ _ _ nappend-as ] ;
|
2009-01-18 22:18:52 -05:00
|
|
|
|
2009-05-12 11:32:19 -04:00
|
|
|
MACRO: append-outputs ( quot -- seq )
|
|
|
|
|
'[ _ { } append-outputs-as ] ;
|
2009-08-19 05:38:59 -04:00
|
|
|
|
|
|
|
|
MACRO: preserving ( quot -- )
|
2010-01-14 10:10:13 -05:00
|
|
|
[ inputs ] keep '[ _ ndup @ ] ;
|
2009-08-19 05:38:59 -04:00
|
|
|
|
2010-04-29 20:57:07 -04:00
|
|
|
MACRO: dropping ( quot -- quot' )
|
2010-04-29 21:21:53 -04:00
|
|
|
inputs '[ [ _ ndrop ] ] ;
|
2010-04-29 20:57:07 -04:00
|
|
|
|
2010-06-23 15:54:00 -04:00
|
|
|
MACRO: nullary ( quot -- quot' ) dropping ;
|
2010-04-29 20:57:07 -04:00
|
|
|
|
|
|
|
|
MACRO: smart-if ( pred true false -- quot )
|
2010-01-27 04:23:04 -05:00
|
|
|
'[ _ preserving _ _ if ] ;
|
2010-01-29 14:47:06 -05:00
|
|
|
|
2010-04-29 20:57:07 -04:00
|
|
|
MACRO: smart-when ( pred true -- quot )
|
|
|
|
|
'[ _ _ [ ] smart-if ] ;
|
|
|
|
|
|
|
|
|
|
MACRO: smart-unless ( pred false -- quot )
|
|
|
|
|
'[ _ [ ] _ smart-if ] ;
|
|
|
|
|
|
|
|
|
|
MACRO: smart-if* ( pred true false -- quot )
|
2010-06-23 15:54:00 -04:00
|
|
|
'[ _ [ preserving ] [ dropping ] bi _ swap _ compose if ] ;
|
2010-04-29 20:57:07 -04:00
|
|
|
|
|
|
|
|
MACRO: smart-when* ( pred true -- quot )
|
|
|
|
|
'[ _ _ [ ] smart-if* ] ;
|
|
|
|
|
|
|
|
|
|
MACRO: smart-unless* ( pred false -- quot )
|
|
|
|
|
'[ _ [ ] _ smart-if* ] ;
|
|
|
|
|
|
|
|
|
|
MACRO: smart-apply ( quot n -- quot )
|
2010-01-31 15:46:46 -05:00
|
|
|
[ dup inputs ] dip '[ _ _ _ mnapply ] ;
|