factor/library/inference/words.factor

178 lines
5.2 KiB
Factor
Raw Normal View History

! 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 kernel lists math namespaces
strings vectors words hashtables parser prettyprint ;
: with-dataflow ( param op [[ in# out# ]] quot -- )
#! Take input parameters, execute quotation, take output
#! parameters, add node. The quotation is called with the
#! stack effect.
2004-12-24 17:29:16 -05:00
>r dup car ensure-d
>r dataflow, r> r> rot
[ pick car swap [ length 0 node-inputs ] bind ] keep
pick >r >r nip call r> r> cdr car swap
[ length 0 node-outputs ] bind ; inline
2004-12-23 01:14:07 -05:00
: consume-d ( typelist -- )
[ pop-d 2drop ] each ;
2004-12-23 01:14:07 -05:00
: produce-d ( typelist -- )
[ <computed> push-d ] each ;
2004-12-30 02:40:14 -05:00
: (consume/produce) ( param op effect )
dup >r -rot r>
2004-12-24 17:29:16 -05:00
[ unswons consume-d car produce-d ] with-dataflow ;
2004-12-03 22:12:58 -05:00
: consume/produce ( word [ in-types out-types ] -- )
#! Add a node to the dataflow graph that consumes and
#! produces a number of values.
2004-12-03 22:12:58 -05:00
#call swap (consume/produce) ;
2004-11-26 22:23:57 -05:00
: apply-effect ( word [ in-types out-types ] -- )
2004-11-26 22:23:57 -05:00
#! If a word does not have special inference behavior, we
#! either execute the word in the meta interpreter (if it is
#! side-effect-free and all parameters are literal), or
#! simply apply its stack effect to the meta-interpreter.
2005-01-02 23:57:54 -05:00
over "infer" word-property [
2004-12-24 17:29:16 -05:00
swap car ensure-d call drop
2004-11-26 22:23:57 -05:00
] [
2005-01-02 23:57:54 -05:00
consume/produce
] ifte* ;
2004-11-26 22:23:57 -05:00
: no-effect ( word -- )
"Unknown stack effect: " swap word-name cat2 inference-error ;
2004-11-26 22:23:57 -05:00
: with-block ( word [[ label quot ]] quot -- node )
2004-12-03 22:12:58 -05:00
#! Execute a quotation with the word on the stack, and add
#! its dataflow contribution to a new block node in the IR.
over [
>r
dupd cons
recursive-state cons@
r> call
] (with-block) ;
2004-12-03 22:12:58 -05:00
: recursive? ( word -- ? )
dup word-parameter tree-contains? ;
: inline-compound ( word -- effect node )
#! Infer the stack effect of a compound word in the current
#! inferencer instance. If the word in question is recursive
#! we infer its stack effect inside a new block.
gensym over word-parameter cons [
word-parameter infer-quot effect
] with-block ;
: infer-compound ( word -- )
#! Infer a word's stack effect in a separate inferencer
#! instance.
2004-11-26 22:23:57 -05:00
[
[
recursive-state get init-inference
dup dup inline-compound drop present-effect
[ "infer-effect" set-word-property ] keep
] with-scope consume/produce
] [
[
>r branches-can-fail? [
drop
] [
t "no-effect" set-word-property
] ifte r> rethrow
] when*
] catch ;
2004-11-26 22:23:57 -05:00
2004-12-24 02:52:02 -05:00
GENERIC: (apply-word)
M: object (apply-word) ( word -- )
#! A primitive with an unknown stack effect.
no-effect ;
2004-12-24 02:52:02 -05:00
M: compound (apply-word) ( word -- )
2004-11-26 22:23:57 -05:00
#! Infer a compound word's stack effect.
dup "no-effect" word-property [
no-effect
2004-11-26 22:23:57 -05:00
] [
dup "inline" word-property [
inline-compound 2drop
] [
infer-compound
] ifte
2004-11-26 22:23:57 -05:00
] ifte ;
2004-12-31 02:17:45 -05:00
M: promise (apply-word) ( word -- )
"promise" word-property unit ensure-d ;
2004-12-24 02:52:02 -05:00
M: symbol (apply-word) ( word -- )
apply-literal ;
2005-01-13 17:28:29 -05:00
: with-recursion ( quot -- )
[
inferring-base-case inc
call
] [
inferring-base-case dec
rethrow
] catch ;
: base-case ( word [ label quot ] -- )
2004-12-30 02:40:14 -05:00
[
car over inline-compound [
drop
[ #call-label ] [ #call ] ?ifte
node-op set
node-param set
] bind
2005-01-13 17:28:29 -05:00
] with-recursion ;
: no-base-case ( word -- )
word-name " does not have a base case." cat2 inference-error ;
: recursive-word ( word [ label quot ] -- )
2004-11-26 22:23:57 -05:00
#! Handle a recursive call, by either applying a previously
2004-12-03 22:12:58 -05:00
#! inferred base case, or raising an error. If the recursive
#! call is to a local block, emit a label call node.
2005-01-13 17:28:29 -05:00
inferring-base-case get max-recursion > [
drop no-base-case
2004-11-26 22:23:57 -05:00
] [
2005-01-13 17:28:29 -05:00
inferring-base-case get max-recursion = [
base-case
] [
[ drop inline-compound 2drop ] with-recursion
] ifte
2004-12-30 02:40:14 -05:00
] ifte ;
2004-11-26 22:23:57 -05:00
: apply-word ( word -- )
#! Apply the word's stack effect to the inferencer state.
dup recursive-state get assoc [
2005-01-31 14:02:09 -05:00
recursive-word
2004-11-26 22:23:57 -05:00
] [
dup "infer-effect" word-property [
2004-12-23 18:26:04 -05:00
apply-effect
2004-11-26 22:23:57 -05:00
] [
(apply-word)
] ifte*
] ifte* ;
2004-11-26 22:23:57 -05:00
2004-12-19 22:53:41 -05:00
: infer-call ( -- )
2004-12-23 01:14:07 -05:00
[ general-list ] ensure-d
2004-12-03 22:12:58 -05:00
dataflow-drop,
pop-d gensym dup pick literal-value cons [
drop
dup value-recursion recursive-state set
literal-value dup infer-quot
] with-block drop handle-terminator ;
2004-11-26 22:23:57 -05:00
\ call [ infer-call ] "infer" set-word-property
! These hacks will go away soon
2004-12-31 02:17:45 -05:00
\ * [ [ number number ] [ number ] ] "infer-effect" set-word-property
\ - [ [ number number ] [ number ] ] "infer-effect" set-word-property
\ = [ [ object object ] [ object ] ] "infer-effect" set-word-property
2004-12-31 02:17:45 -05:00
\ undefined-method t "terminator" set-word-property
\ undefined-method [ [ object word ] [ ] ] "infer-effect" set-word-property
\ not-a-number t "terminator" set-word-property
2004-12-27 22:58:43 -05:00
\ throw t "terminator" set-word-property