2006-05-15 01:49:07 -04:00
|
|
|
! Copyright (C) 2004, 2006 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2006-08-24 02:09:54 -04:00
|
|
|
USING: arrays errors generic hashtables kernel
|
2005-12-16 22:24:39 -05:00
|
|
|
math math-internals namespaces parser prettyprint sequences
|
|
|
|
strings vectors words ;
|
2006-05-15 18:00:37 -04:00
|
|
|
IN: inference
|
2004-11-28 19:07:24 -05:00
|
|
|
|
2005-09-14 00:37:50 -04:00
|
|
|
: consume-values ( n node -- )
|
|
|
|
over ensure-values
|
2006-07-19 23:10:02 -04:00
|
|
|
over 0 rot node-inputs
|
|
|
|
meta-d get [ length swap - ] keep set-length ;
|
2004-12-22 22:16:46 -05:00
|
|
|
|
2005-09-14 00:37:50 -04:00
|
|
|
: produce-values ( n node -- )
|
2006-07-19 23:10:02 -04:00
|
|
|
>r [ drop <computed> ] map dup r> set-node-out-d
|
|
|
|
meta-d get swap nappend ;
|
2005-09-09 00:17:19 -04:00
|
|
|
|
2006-08-15 04:57:12 -04:00
|
|
|
: recursing? ( word -- label/f )
|
|
|
|
recursive-state get <reversed> assoc ;
|
|
|
|
|
|
|
|
: make-call-node ( word -- node )
|
|
|
|
dup "inline" word-prop
|
|
|
|
[ dup recursing? [ #call-label ] [ #call ] ?if ]
|
|
|
|
[ #call ]
|
|
|
|
if ;
|
|
|
|
|
2005-05-17 16:13:08 -04:00
|
|
|
: consume/produce ( word effect -- )
|
2006-08-15 04:57:12 -04:00
|
|
|
meta-d get clone >r
|
2006-09-14 16:14:27 -04:00
|
|
|
swap make-call-node dup node,
|
2006-08-15 03:01:24 -04:00
|
|
|
over effect-in length over consume-values
|
|
|
|
over effect-out length over produce-values
|
2006-09-14 16:14:27 -04:00
|
|
|
r> over #call-label? [ swap set-node-in-d ] [ 2drop ] if
|
|
|
|
effect-terminated? [ terminate ] when ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2006-08-18 03:10:08 -04:00
|
|
|
TUPLE: no-effect word ;
|
|
|
|
|
2006-08-15 05:24:30 -04:00
|
|
|
: no-effect ( word -- * )
|
2006-09-23 02:40:25 -04:00
|
|
|
<no-effect> inference-warning ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2006-08-10 16:33:15 -04:00
|
|
|
: nest-node ( -- ) #entry node, ;
|
2005-12-16 22:24:39 -05:00
|
|
|
|
2006-08-10 16:33:15 -04:00
|
|
|
: unnest-node ( new-node -- new-node )
|
|
|
|
dup node-param #return node,
|
|
|
|
dataflow-graph get 1array over set-node-children ;
|
2005-12-16 22:24:39 -05:00
|
|
|
|
2006-08-15 04:57:12 -04:00
|
|
|
: add-recursive-state ( word label -- )
|
|
|
|
2array recursive-state [ swap add ] change ;
|
2005-11-22 21:41:41 -05:00
|
|
|
|
2006-08-15 04:57:12 -04:00
|
|
|
: inline-block ( word -- node-block variables )
|
2005-11-22 21:41:41 -05:00
|
|
|
[
|
2006-08-10 16:33:15 -04:00
|
|
|
copy-inference nest-node
|
2006-08-15 04:57:12 -04:00
|
|
|
gensym 2dup add-recursive-state
|
2006-08-10 16:33:15 -04:00
|
|
|
#label >r word-def infer-quot r>
|
|
|
|
unnest-node
|
2005-11-22 21:41:41 -05:00
|
|
|
] make-hash ;
|
|
|
|
|
|
|
|
: apply-infer ( hash -- )
|
|
|
|
{ meta-d meta-r d-in }
|
|
|
|
[ [ swap hash ] keep set ] each-with ;
|
|
|
|
|
|
|
|
GENERIC: collect-recursion* ( label node -- )
|
|
|
|
|
2006-08-15 03:01:24 -04:00
|
|
|
M: node collect-recursion* 2drop ;
|
2005-11-22 21:41:41 -05:00
|
|
|
|
2006-08-15 03:01:24 -04:00
|
|
|
M: #call-label collect-recursion*
|
2006-08-10 16:33:15 -04:00
|
|
|
tuck node-param eq? [ node-in-d , ] [ drop ] if ;
|
2005-11-22 21:41:41 -05:00
|
|
|
|
|
|
|
: collect-recursion ( #label -- seq )
|
|
|
|
dup node-param swap
|
|
|
|
[ [ collect-recursion* ] each-node-with ] { } make ;
|
|
|
|
|
|
|
|
: join-values ( node -- )
|
2006-08-10 16:03:51 -04:00
|
|
|
collect-recursion meta-d get add unify-lengths unify-stacks
|
2006-08-15 04:57:12 -04:00
|
|
|
meta-d [ length tail* >vector ] change ;
|
2005-11-22 21:41:41 -05:00
|
|
|
|
|
|
|
: splice-node ( node -- )
|
|
|
|
dup node-successor [
|
|
|
|
dup node, penultimate-node f over set-node-successor
|
|
|
|
dup current-node set
|
|
|
|
] when drop ;
|
|
|
|
|
|
|
|
: inline-closure ( word -- )
|
2006-08-15 04:57:12 -04:00
|
|
|
dup inline-block over recursive-label? [
|
2005-11-22 21:41:41 -05:00
|
|
|
meta-d get >r
|
2006-08-15 04:57:12 -04:00
|
|
|
drop join-values inline-block apply-infer
|
2006-03-04 02:53:22 -05:00
|
|
|
r> over set-node-in-d node,
|
2005-11-22 21:41:41 -05:00
|
|
|
] [
|
|
|
|
apply-infer node-child node-successor splice-node drop
|
|
|
|
] if ;
|
2004-11-28 19:07:24 -05:00
|
|
|
|
2006-08-15 04:57:12 -04:00
|
|
|
: infer-compound ( word -- effect )
|
2004-11-26 22:23:57 -05:00
|
|
|
[
|
2006-01-19 23:28:45 -05:00
|
|
|
recursive-state get init-inference
|
2006-08-15 04:57:12 -04:00
|
|
|
[ inline-block nip [ current-effect ] bind ] keep
|
2006-08-15 03:01:24 -04:00
|
|
|
] with-scope over consume/produce ;
|
2005-05-20 23:52:31 -04:00
|
|
|
|
2005-07-31 23:38:33 -04:00
|
|
|
GENERIC: apply-word
|
2004-12-24 02:52:02 -05:00
|
|
|
|
2006-08-18 03:10:08 -04:00
|
|
|
M: object apply-word no-effect ;
|
2005-02-22 23:07:47 -05:00
|
|
|
|
2006-08-15 03:01:24 -04:00
|
|
|
TUPLE: effect-error word effect ;
|
2006-01-19 23:28:45 -05:00
|
|
|
|
2006-08-18 03:10:08 -04:00
|
|
|
: effect-error ( word effect -- * )
|
|
|
|
<effect-error> inference-error ;
|
2006-08-15 03:01:24 -04:00
|
|
|
|
|
|
|
: check-effect ( word effect -- )
|
2006-08-18 01:50:34 -04:00
|
|
|
over "infer" word-prop [
|
|
|
|
2drop
|
|
|
|
] [
|
|
|
|
over recorded get push
|
|
|
|
dup pick "declared-effect" word-prop dup
|
|
|
|
[ effect<= [ effect-error ] unless ] [ 2drop ] if
|
|
|
|
"infer-effect" set-word-prop
|
|
|
|
] if ;
|
2006-08-15 03:01:24 -04:00
|
|
|
|
|
|
|
M: compound apply-word
|
2005-07-31 23:38:33 -04:00
|
|
|
[
|
2006-08-15 04:57:12 -04:00
|
|
|
dup infer-compound check-effect
|
2005-09-21 01:12:16 -04:00
|
|
|
] [
|
2005-09-20 20:18:01 -04:00
|
|
|
swap t "no-effect" set-word-prop rethrow
|
2005-09-21 01:12:16 -04:00
|
|
|
] recover ;
|
2005-05-15 21:17:56 -04:00
|
|
|
|
2006-08-18 01:50:34 -04:00
|
|
|
: ?no-effect ( word -- )
|
|
|
|
dup "no-effect" word-prop [ no-effect ] [ drop ] if ;
|
|
|
|
|
2005-07-31 23:38:33 -04:00
|
|
|
: apply-default ( word -- )
|
2006-08-18 01:50:34 -04:00
|
|
|
dup ?no-effect
|
2006-07-25 00:14:18 -04:00
|
|
|
dup "infer-effect" word-prop [
|
|
|
|
over "infer" word-prop [
|
2006-08-15 03:01:24 -04:00
|
|
|
swap effect-in length ensure-values call drop
|
2005-02-24 20:52:17 -05:00
|
|
|
] [
|
2006-07-26 15:03:49 -04:00
|
|
|
consume/produce
|
2005-09-24 15:21:17 -04:00
|
|
|
] if*
|
2006-07-25 00:14:18 -04:00
|
|
|
] [
|
|
|
|
apply-word
|
|
|
|
] if* ;
|
2005-02-24 20:52:17 -05:00
|
|
|
|
2006-08-15 03:01:24 -04:00
|
|
|
M: word apply-object apply-default ;
|
2005-02-24 20:52:17 -05:00
|
|
|
|
2006-08-15 03:01:24 -04:00
|
|
|
M: symbol apply-object apply-literal ;
|
2005-11-13 22:04:14 -05:00
|
|
|
|
2006-08-18 03:10:08 -04:00
|
|
|
TUPLE: recursive-declare-error word ;
|
2005-05-20 23:52:31 -04:00
|
|
|
|
2006-08-15 03:01:24 -04:00
|
|
|
: recursive-effect ( word -- effect )
|
2006-08-18 03:51:41 -04:00
|
|
|
dup stack-effect
|
2006-08-18 03:10:08 -04:00
|
|
|
[ ] [ <recursive-declare-error> inference-error ] ?if ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2006-08-15 03:01:24 -04:00
|
|
|
M: compound apply-object
|
2006-08-18 01:14:22 -04:00
|
|
|
dup "inline" word-prop [
|
|
|
|
dup recursive-state get peek first eq? [
|
|
|
|
dup recursive-effect consume/produce
|
|
|
|
] [
|
|
|
|
inline-closure
|
|
|
|
] if
|
2004-11-26 22:23:57 -05:00
|
|
|
] [
|
2006-08-18 01:14:22 -04:00
|
|
|
dup recursing? [
|
|
|
|
dup recursive-effect consume/produce
|
|
|
|
] [
|
|
|
|
apply-default
|
|
|
|
] if
|
2006-08-15 03:01:24 -04:00
|
|
|
] if ;
|