factor/library/inference/inference.factor

144 lines
3.8 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
2005-03-07 22:11:36 -05:00
USING: errors generic interpreter kernel lists math namespaces
2005-04-02 02:39:33 -05:00
prettyprint sequences strings unparser 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
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 )
2005-05-17 16:13:08 -04:00
1 #drop node, pop-d >literal< ;
2005-03-29 19:11:10 -05:00
: (ensure-types) ( typelist n stack -- )
pick [
3dup >r >r car r> r> nth value-class-and
>r >r cdr r> 1 + r> (ensure-types)
] [
3drop
] ifte ;
: ensure-types ( typelist stack -- )
dup length pick length - dup 0 < [
2005-05-18 16:26:22 -04:00
swap >r neg swap tail 0 r>
] [
swap
] ifte (ensure-types) ;
2004-12-23 01:14:07 -05:00
: required-inputs ( typelist stack -- values )
>r dup length r> length - dup 0 > [
2005-05-18 16:26:22 -04:00
swap head [ <computed> ] map
2004-11-26 22:23:57 -05:00
] [
2004-12-23 01:14:07 -05:00
2drop f
2004-11-26 22:23:57 -05:00
] ifte ;
2004-12-23 01:14:07 -05:00
: ensure-d ( typelist -- )
dup meta-d get ensure-types
2005-04-19 20:28:01 -04:00
meta-d get required-inputs >vector 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
cdr car length 0 rot node-outputs ; inline
: (present-effect) ( vector -- list )
2005-04-02 02:39:33 -05:00
>list [ value-class ] map ;
: present-effect ( [[ d-in meta-d ]] -- [ in-types out-types ] )
2004-11-26 22:23:57 -05:00
#! After inference is finished, collect information.
uncons >r (present-effect) r> (present-effect) 2list ;
: simple-effect ( [[ d-in meta-d ]] -- [[ in# out# ]] )
#! After inference is finished, collect information.
uncons length >r length r> cons ;
2004-11-26 22:23:57 -05:00
: init-inference ( recursive-state -- )
init-interpreter
0 <vector> 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-17 16:13:08 -04:00
current-node off
inferring-base-case 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-05-17 16:13:08 -04:00
recursive-state get <literal> push-d 1 #push node, ;
M: object apply-object apply-literal ;
2004-11-26 22:23:57 -05:00
: active? ( -- ? )
#! Is this branch not terminated?
d-in get meta-d get and ;
: effect ( -- [[ d-in meta-d ]] )
d-in get meta-d get cons ;
: terminate ( -- )
#! Ignore this branch's stack effect.
meta-d off meta-r off d-in off ;
: terminator? ( obj -- ? )
#! Does it throw an error?
dup word? [ "terminator" word-prop ] [ drop f ] ifte ;
: handle-terminator ( quot -- )
#! If the quotation throws an error, do not count its stack
#! effect.
[ terminator? ] some? [ terminate ] when ;
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.
active? [
[ unswons apply-object infer-quot ] when*
] [
drop
] ifte ;
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
dup infer-quot handle-terminator
r> recursive-state set ;
2005-05-15 21:17:56 -04:00
: check-active ( -- )
active? [ "Provable runtime error" inference-error ] unless ;
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 elements on return stack" inference-error
2004-12-04 23:45:41 -05:00
] unless ;
2005-05-15 21:17:56 -04:00
: with-infer ( quot -- )
[
f init-inference
call
check-active
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-05-15 21:17:56 -04:00
[ infer-quot effect present-effect ] with-infer ;
: dataflow ( quot -- dataflow )
#! Data flow of a quotation.
2005-05-17 16:13:08 -04:00
[ infer-quot #return node, dataflow-graph get ] with-infer ;