factor/library/inference/inference.factor

142 lines
4.1 KiB
Factor
Raw Normal View History

2005-01-13 17:28:29 -05:00
! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
2004-11-26 22:23:57 -05:00
IN: inference
USING: errors generic interpreter io kernel lists math
namespaces parser prettyprint sequences strings vectors words ;
2004-11-26 22:23:57 -05:00
! This variable takes a boolean value.
2005-01-13 17:28:29 -05:00
SYMBOL: inferring-base-case
! Called when a recursive call during base case inference is
! found. Either tries to infer another branch, or gives up.
SYMBOL: base-case-continuation
TUPLE: inference-error message rstate data-stack call-stack ;
: inference-error ( msg -- )
recursive-state get meta-d get meta-r get
<inference-error> throw ; inline
M: inference-error error. ( error -- )
"! Inference error:" print
dup inference-error-message print
"! Recursive state:" print
2005-08-27 15:33:29 -04:00
inference-error-rstate sequence. ;
2005-08-06 01:59:49 -04:00
M: value literal-value ( value -- )
{
"A literal value was expected where a computed value was found.\n"
"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-08-06 01:59:49 -04:00
} concat inference-error ;
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.
! - infer - quotation with custom inference behavior; ifte uses
! this. Word is passed on the stack.
! Vector of results we had to add to the datastack. Ie, the
! inputs.
2004-11-26 22:23:57 -05:00
SYMBOL: d-in
: pop-literal ( -- rstate obj )
1 #drop node, pop-d dup value-recursion swap literal-value ;
2005-03-29 19:11:10 -05:00
2005-07-27 01:46:06 -04:00
: computed-value-vector ( n -- vector )
empty-vector dup [ drop <computed> ] nmap ;
: required-inputs ( n stack -- values )
length - 0 max computed-value-vector ;
2004-11-26 22:23:57 -05:00
2004-12-23 01:14:07 -05:00
: ensure-d ( typelist -- )
length meta-d get required-inputs dup
meta-d [ append ] change
d-in [ append ] change ;
2004-11-26 22:23:57 -05:00
2005-05-17 16:13:08 -04:00
: hairy-node ( node effect quot -- )
over car ensure-d
-rot 2dup car length 0 rot node-inputs
2slip
second length 0 rot node-outputs ; inline
2005-05-17 16:13:08 -04:00
: effect ( -- [[ in# out# ]] )
#! After inference is finished, collect information.
2005-08-01 16:22:53 -04:00
d-in get length object <repeated> >list
meta-d get length object <repeated> >list 2list ;
: no-base-case ( word -- )
{
"The base case of a recursive word could not be inferred.\n"
"This means the word calls itself in every control flow path.\n"
"See the handbook for details."
} concat inference-error ;
2004-11-26 22:23:57 -05:00
: init-inference ( recursive-state -- )
init-interpreter
2005-08-25 15:27:38 -04:00
{ } clone 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
current-node off ;
2004-11-26 22:23:57 -05:00
GENERIC: apply-object
2004-11-26 22:23:57 -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, ;
M: object apply-object apply-literal ;
2004-11-26 22:23:57 -05:00
M: wrapper apply-object wrapped apply-literal ;
: active? ( -- ? )
#! Is this branch not terminated?
d-in get meta-d get and ;
: terminate ( -- )
#! Ignore this branch's stack effect.
meta-d off meta-r off d-in off ;
2004-12-02 22:44:36 -05:00
: infer-quot ( quot -- )
2004-11-26 22:23:57 -05:00
#! Recursive calls to this word are made for nested
#! quotations.
2005-07-30 02:08:59 -04:00
[ active? [ apply-object t ] [ drop f ] ifte ] all? drop ;
2004-11-26 22:23:57 -05:00
2005-05-16 01:15:48 -04:00
: infer-quot-value ( rstate quot -- )
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? [
"Word leaves " meta-r get length number>string
" 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 -- )
[
inferring-base-case off
[ no-base-case ] base-case-continuation set
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.
[ infer-quot effect ] with-infer ;
2005-07-31 23:38:33 -04:00
: (dataflow) ( quot -- dataflow )
infer-quot #return node, dataflow-graph get ;
: 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 ;