2005-01-13 17:28:29 -05:00
|
|
|
! Copyright (C) 2004, 2005 Slava Pestov.
|
2005-02-22 23:07:47 -05:00
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2004-11-26 22:23:57 -05:00
|
|
|
IN: inference
|
2005-09-25 21:54:25 -04:00
|
|
|
USING: arrays errors generic inspector interpreter io kernel
|
|
|
|
lists math namespaces parser prettyprint sequences strings
|
|
|
|
vectors words ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2005-05-14 00:23:00 -04:00
|
|
|
! This variable takes a boolean value.
|
2005-01-13 17:28:29 -05:00
|
|
|
SYMBOL: inferring-base-case
|
2005-01-10 23:08:27 -05:00
|
|
|
|
2005-08-30 18:12:21 -04:00
|
|
|
! Called when a recursive call during base case inference is
|
|
|
|
! found. Either tries to infer another branch, or gives up.
|
|
|
|
SYMBOL: base-case-continuation
|
|
|
|
|
2005-08-04 23:59:45 -04:00
|
|
|
TUPLE: inference-error message rstate data-stack call-stack ;
|
|
|
|
|
|
|
|
: inference-error ( msg -- )
|
|
|
|
recursive-state get meta-d get meta-r get
|
2005-09-17 22:25:18 -04:00
|
|
|
<inference-error> throw ;
|
2005-08-04 23:59:45 -04:00
|
|
|
|
|
|
|
M: inference-error error. ( error -- )
|
|
|
|
"! Inference error:" print
|
|
|
|
dup inference-error-message print
|
|
|
|
"! Recursive state:" print
|
2005-09-25 21:54:25 -04:00
|
|
|
inference-error-rstate describe ;
|
2005-08-04 23:59:45 -04:00
|
|
|
|
2005-08-06 01:59:49 -04:00
|
|
|
M: value literal-value ( value -- )
|
2005-10-29 16:53:47 -04:00
|
|
|
@{
|
2005-08-06 01:59:49 -04:00
|
|
|
"A literal value was expected where a computed value was found.\n"
|
2005-08-30 18:12:21 -04:00
|
|
|
"This means the word you are inferring applies 'call' or 'execute'\n"
|
|
|
|
"to a value that is not known at compile time.\n"
|
|
|
|
"See the handbook for details."
|
2005-10-29 16:53:47 -04:00
|
|
|
}@ concat inference-error ;
|
2005-08-06 01:59:49 -04:00
|
|
|
|
2004-11-26 22:23:57 -05:00
|
|
|
! Word properties that affect inference:
|
|
|
|
! - infer-effect -- must be set. controls number of inputs
|
|
|
|
! expected, and number of outputs produced.
|
2005-09-24 15:21:17 -04:00
|
|
|
! - infer - quotation with custom inference behavior; if uses
|
2004-11-26 22:23:57 -05:00
|
|
|
! this. Word is passed on the stack.
|
|
|
|
|
2004-12-23 16:37:16 -05:00
|
|
|
! Vector of results we had to add to the datastack. Ie, the
|
|
|
|
! inputs.
|
2004-11-26 22:23:57 -05:00
|
|
|
SYMBOL: d-in
|
|
|
|
|
2005-05-14 00:23:00 -04:00
|
|
|
: pop-literal ( -- rstate obj )
|
2005-08-04 23:59:45 -04:00
|
|
|
1 #drop node, pop-d dup value-recursion swap literal-value ;
|
2005-03-29 19:11:10 -05:00
|
|
|
|
2005-09-18 01:37:28 -04:00
|
|
|
: value-vector ( n -- vector ) [ drop <value> ] map >vector ;
|
2004-12-23 16:37:16 -05:00
|
|
|
|
2005-09-18 01:37:28 -04:00
|
|
|
: required-inputs ( n stack -- n ) length - 0 max ;
|
|
|
|
|
|
|
|
: add-inputs ( n stack -- stack )
|
|
|
|
tuck required-inputs dup 0 >
|
2005-09-24 15:21:17 -04:00
|
|
|
[ value-vector swap append ] [ drop ] if ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2005-09-14 00:37:50 -04:00
|
|
|
: ensure-values ( n -- )
|
2005-09-18 01:37:28 -04:00
|
|
|
dup meta-d get required-inputs d-in [ + ] change
|
|
|
|
meta-d [ add-inputs ] change ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2005-09-18 01:37:28 -04:00
|
|
|
: effect ( -- @{ in# out# }@ )
|
2005-01-16 17:58:28 -05:00
|
|
|
#! After inference is finished, collect information.
|
2005-09-18 01:37:28 -04:00
|
|
|
d-in get meta-d get length 2array ;
|
2005-08-30 18:12:21 -04:00
|
|
|
|
2005-09-28 20:09:10 -04:00
|
|
|
SYMBOL: terminated?
|
|
|
|
|
2004-11-26 22:23:57 -05:00
|
|
|
: init-inference ( recursive-state -- )
|
2005-09-28 20:09:10 -04:00
|
|
|
terminated? off
|
|
|
|
{ } clone meta-r set
|
|
|
|
{ } clone meta-d set
|
2005-09-18 01:37:28 -04:00
|
|
|
0 d-in set
|
2004-11-26 22:23:57 -05:00
|
|
|
recursive-state set
|
2004-12-30 02:40:14 -05:00
|
|
|
dataflow-graph off
|
2005-05-20 23:52:31 -04:00
|
|
|
current-node off ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2005-02-24 20:52:17 -05:00
|
|
|
GENERIC: apply-object
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2004-11-27 00:33:17 -05:00
|
|
|
: apply-literal ( obj -- )
|
|
|
|
#! Literals are annotated with the current recursive
|
|
|
|
#! state.
|
2005-08-05 00:05:04 -04:00
|
|
|
<literal> push-d 1 #push node, ;
|
2004-11-27 00:33:17 -05:00
|
|
|
|
2005-02-24 20:52:17 -05:00
|
|
|
M: object apply-object apply-literal ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2005-08-03 23:56:28 -04:00
|
|
|
M: wrapper apply-object wrapped apply-literal ;
|
|
|
|
|
2005-02-08 22:02:44 -05:00
|
|
|
: terminate ( -- )
|
|
|
|
#! Ignore this branch's stack effect.
|
2005-09-28 23:29:00 -04:00
|
|
|
terminated? on #terminate node, ;
|
2005-02-08 22:02:44 -05:00
|
|
|
|
2005-10-13 00:30:44 -04:00
|
|
|
GENERIC: infer-quot
|
|
|
|
|
|
|
|
M: general-list infer-quot ( quot -- )
|
2004-11-26 22:23:57 -05:00
|
|
|
#! Recursive calls to this word are made for nested
|
|
|
|
#! quotations.
|
2005-09-28 20:09:10 -04:00
|
|
|
[ terminated? get [ drop f ] [ apply-object t ] if ] all? drop ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2005-05-16 01:15:48 -04:00
|
|
|
: infer-quot-value ( rstate quot -- )
|
2005-08-30 18:12:21 -04:00
|
|
|
recursive-state get >r swap recursive-state set
|
|
|
|
infer-quot r> recursive-state set ;
|
2005-05-16 01:15:48 -04:00
|
|
|
|
2004-12-04 23:45:41 -05:00
|
|
|
: check-return ( -- )
|
|
|
|
#! Raise an error if word leaves values on return stack.
|
2005-05-15 21:17:56 -04:00
|
|
|
meta-r get empty? [
|
2005-08-21 20:50:14 -04:00
|
|
|
"Word leaves " meta-r get length number>string
|
2005-08-04 23:59:45 -04:00
|
|
|
" element(s) on return stack. Check >r/r> usage." append3
|
|
|
|
inference-error
|
2004-12-04 23:45:41 -05:00
|
|
|
] unless ;
|
|
|
|
|
2005-05-15 21:17:56 -04:00
|
|
|
: with-infer ( quot -- )
|
|
|
|
[
|
2005-05-20 23:52:31 -04:00
|
|
|
inferring-base-case off
|
2005-09-14 00:37:50 -04:00
|
|
|
base-case-continuation off
|
2005-05-15 21:17:56 -04:00
|
|
|
f init-inference
|
|
|
|
call
|
|
|
|
check-return
|
|
|
|
] with-scope ;
|
2004-12-02 22:44:36 -05:00
|
|
|
|
2005-05-17 16:13:08 -04:00
|
|
|
: infer ( quot -- effect )
|
2004-11-26 22:23:57 -05:00
|
|
|
#! Stack effect of a quotation.
|
2005-07-27 20:13:11 -04:00
|
|
|
[ infer-quot effect ] with-infer ;
|
2004-11-27 00:33:17 -05:00
|
|
|
|
2005-07-31 23:38:33 -04:00
|
|
|
: (dataflow) ( quot -- dataflow )
|
2005-09-07 17:21:11 -04:00
|
|
|
infer-quot f #return node, dataflow-graph get ;
|
2005-07-31 23:38:33 -04:00
|
|
|
|
2004-12-01 19:48:08 -05:00
|
|
|
: dataflow ( quot -- dataflow )
|
|
|
|
#! Data flow of a quotation.
|
2005-07-31 23:38:33 -04:00
|
|
|
[ (dataflow) ] with-infer ;
|
|
|
|
|
|
|
|
: dataflow-with ( quot stack -- effect )
|
|
|
|
#! Infer starting from a stack of values.
|
|
|
|
[ meta-d set (dataflow) ] with-infer ;
|