2005-01-13 14:41:08 -05:00
|
|
|
! Copyright (C) 2004, 2005 Slava Pestov.
|
2005-01-30 15:57:25 -05:00
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2004-11-26 22:23:57 -05:00
|
|
|
IN: inference
|
2005-01-30 15:57:25 -05:00
|
|
|
USING: errors generic interpreter kernel lists math namespaces
|
2005-04-02 02:39:33 -05:00
|
|
|
sequences strings vectors words hashtables prettyprint ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2004-12-31 02:17:45 -05:00
|
|
|
: longest-vector ( list -- length )
|
2005-04-19 20:28:01 -04:00
|
|
|
0 swap [ length max ] each ;
|
2004-12-31 02:17:45 -05:00
|
|
|
|
|
|
|
: computed-value-vector ( n -- vector )
|
|
|
|
[ drop object <computed> ] vector-project ;
|
|
|
|
|
2005-01-01 17:20:48 -05:00
|
|
|
: add-inputs ( count stack -- stack )
|
2004-12-31 02:17:45 -05:00
|
|
|
#! Add this many inputs to the given stack.
|
2005-04-25 19:54:21 -04:00
|
|
|
[ length - computed-value-vector ] keep append ;
|
2004-12-31 02:17:45 -05:00
|
|
|
|
|
|
|
: unify-lengths ( list -- list )
|
|
|
|
#! Pad all vectors to the same length. If one vector is
|
|
|
|
#! shorter, pad it with unknown results at the bottom.
|
2005-01-01 17:20:48 -05:00
|
|
|
dup longest-vector swap [ add-inputs ] map-with ;
|
2004-12-31 02:17:45 -05:00
|
|
|
|
|
|
|
: unify-results ( list -- value )
|
|
|
|
#! If all values in list are equal, return the value.
|
|
|
|
#! Otherwise, unify types.
|
|
|
|
dup all=? [
|
|
|
|
car
|
2004-12-26 01:42:09 -05:00
|
|
|
] [
|
2004-12-31 02:17:45 -05:00
|
|
|
[ value-class ] map class-or-list <computed>
|
2004-12-26 01:42:09 -05:00
|
|
|
] ifte ;
|
2004-12-23 16:37:16 -05:00
|
|
|
|
2004-12-31 02:17:45 -05:00
|
|
|
: vector-transpose ( list -- vector )
|
|
|
|
#! Turn a list of same-length vectors into a vector of lists.
|
2005-04-26 00:35:55 -04:00
|
|
|
dup car length [
|
|
|
|
over [ nth ] map-with
|
2004-12-31 02:17:45 -05:00
|
|
|
] vector-project nip ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
|
|
|
: unify-stacks ( list -- stack )
|
|
|
|
#! Replace differing literals in stacks with unknown
|
|
|
|
#! results.
|
2005-04-19 20:28:01 -04:00
|
|
|
unify-lengths vector-transpose [ unify-results ] seq-map ;
|
2004-12-22 22:16:46 -05:00
|
|
|
|
|
|
|
: balanced? ( list -- ? )
|
2005-01-13 19:49:47 -05:00
|
|
|
#! Check if a list of [[ instack outstack ]] pairs is
|
2004-12-26 01:42:09 -05:00
|
|
|
#! balanced.
|
2005-04-26 00:35:55 -04:00
|
|
|
[ uncons length swap length - ] map all=? ;
|
2004-12-07 23:21:32 -05:00
|
|
|
|
2004-12-26 01:42:09 -05:00
|
|
|
: unify-effect ( list -- in out )
|
2005-01-13 19:49:47 -05:00
|
|
|
#! Unify a list of [[ instack outstack ]] pairs.
|
2004-12-26 01:42:09 -05:00
|
|
|
dup balanced? [
|
|
|
|
unzip unify-stacks >r unify-stacks r>
|
2004-12-25 21:28:47 -05:00
|
|
|
] [
|
2005-02-22 23:07:47 -05:00
|
|
|
"Unbalanced branches" inference-error
|
2004-12-25 21:28:47 -05:00
|
|
|
] ifte ;
|
2004-12-07 23:21:32 -05:00
|
|
|
|
2004-12-26 01:42:09 -05:00
|
|
|
: datastack-effect ( list -- )
|
2005-01-10 23:08:27 -05:00
|
|
|
[ [ effect ] bind ] map
|
2004-12-26 01:42:09 -05:00
|
|
|
unify-effect
|
|
|
|
meta-d set d-in set ;
|
|
|
|
|
|
|
|
: callstack-effect ( list -- )
|
|
|
|
[ [ { } meta-r get ] bind cons ] map
|
|
|
|
unify-effect
|
|
|
|
meta-r set drop ;
|
|
|
|
|
|
|
|
: filter-terminators ( list -- list )
|
2005-02-08 22:02:44 -05:00
|
|
|
#! Remove branches that unconditionally throw errors.
|
|
|
|
[ [ active? ] bind ] subset ;
|
2004-12-07 23:21:32 -05:00
|
|
|
|
2004-12-24 17:29:16 -05:00
|
|
|
: unify-effects ( list -- )
|
2005-02-08 22:02:44 -05:00
|
|
|
filter-terminators [
|
|
|
|
dup datastack-effect callstack-effect
|
|
|
|
] [
|
|
|
|
terminate
|
|
|
|
] ifte* ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2004-12-26 17:04:08 -05:00
|
|
|
SYMBOL: cloned
|
|
|
|
|
2005-01-28 23:55:22 -05:00
|
|
|
: deep-clone ( obj -- obj )
|
|
|
|
#! Clone an object if it hasn't already been cloned in this
|
2004-12-26 17:04:08 -05:00
|
|
|
#! with-deep-clone scope.
|
2005-03-21 14:39:46 -05:00
|
|
|
dup cloned get assq [ ] [
|
2005-02-23 21:50:51 -05:00
|
|
|
dup clone [ swap cloned [ acons ] change ] keep
|
2005-03-21 14:39:46 -05:00
|
|
|
] ?ifte ;
|
2004-12-26 17:04:08 -05:00
|
|
|
|
2005-04-19 20:28:01 -04:00
|
|
|
: deep-clone-seq ( seq -- seq )
|
|
|
|
#! Clone a sequence and each object it contains.
|
|
|
|
[ deep-clone ] seq-map ;
|
2004-12-26 17:04:08 -05:00
|
|
|
|
|
|
|
: copy-inference ( -- )
|
|
|
|
#! We avoid cloning the same object more than once in order
|
|
|
|
#! to preserve identity structure.
|
|
|
|
cloned off
|
2005-04-19 20:28:01 -04:00
|
|
|
meta-r [ deep-clone-seq ] change
|
|
|
|
meta-d [ deep-clone-seq ] change
|
|
|
|
d-in [ deep-clone-seq ] change
|
2004-12-26 17:04:08 -05:00
|
|
|
dataflow-graph off ;
|
2004-12-23 18:26:04 -05:00
|
|
|
|
2004-12-27 15:27:18 -05:00
|
|
|
: infer-branch ( value -- namespace )
|
2005-02-08 22:02:44 -05:00
|
|
|
#! Return a namespace with inferencer variables:
|
|
|
|
#! meta-d, meta-r, d-in. They are set to f if
|
|
|
|
#! terminate was called.
|
2004-12-07 23:21:32 -05:00
|
|
|
<namespace> [
|
2005-02-23 21:50:51 -05:00
|
|
|
uncons pull-tie
|
2004-12-19 22:53:41 -05:00
|
|
|
dup value-recursion recursive-state set
|
2004-12-26 17:04:08 -05:00
|
|
|
copy-inference
|
2005-01-31 14:02:09 -05:00
|
|
|
literal-value dup infer-quot
|
2005-02-08 22:02:44 -05:00
|
|
|
active? [
|
|
|
|
#values values-node
|
|
|
|
handle-terminator
|
|
|
|
] [
|
|
|
|
drop
|
|
|
|
] ifte
|
2004-12-07 23:21:32 -05:00
|
|
|
] extend ;
|
|
|
|
|
2004-12-08 18:39:36 -05:00
|
|
|
: (infer-branches) ( branchlist -- list )
|
2005-01-16 17:58:28 -05:00
|
|
|
#! The branchlist is a list of pairs: [[ value typeprop ]]
|
2004-12-26 02:52:39 -05:00
|
|
|
#! value is either a literal or computed instance; typeprop
|
2005-01-13 19:49:47 -05:00
|
|
|
#! is a pair [[ value class ]] indicating a type propagation
|
2004-12-26 02:52:39 -05:00
|
|
|
#! for the given branch.
|
2004-12-30 02:40:14 -05:00
|
|
|
[
|
|
|
|
[
|
2005-01-16 17:58:28 -05:00
|
|
|
branches-can-fail? [
|
|
|
|
[ infer-branch , ] [ [ drop ] when ] catch
|
2004-12-30 02:40:14 -05:00
|
|
|
] [
|
|
|
|
infer-branch ,
|
|
|
|
] ifte
|
|
|
|
] each
|
|
|
|
] make-list ;
|
2004-11-29 23:14:12 -05:00
|
|
|
|
2004-12-24 17:29:16 -05:00
|
|
|
: unify-dataflow ( inputs instruction effectlist -- )
|
|
|
|
[ [ get-dataflow ] bind ] map
|
|
|
|
swap dataflow, [ node-consume-d set ] bind ;
|
|
|
|
|
2004-11-29 23:14:12 -05:00
|
|
|
: infer-branches ( inputs instruction branchlist -- )
|
2004-11-26 22:23:57 -05:00
|
|
|
#! Recursive stack effect inference is done here. If one of
|
|
|
|
#! the branches has an undecidable stack effect, we set the
|
2004-11-29 23:14:12 -05:00
|
|
|
#! base case to this stack effect and try again. The inputs
|
|
|
|
#! parameter is a vector.
|
2005-01-10 23:08:27 -05:00
|
|
|
(infer-branches) dup unify-effects unify-dataflow ;
|
2004-12-29 03:35:46 -05:00
|
|
|
|
2005-02-22 23:07:47 -05:00
|
|
|
: (with-block) ( [[ label quot ]] quot -- node )
|
2004-12-29 03:35:46 -05:00
|
|
|
#! Call a quotation in a new namespace, and transfer
|
|
|
|
#! inference state from the outer scope.
|
2005-02-22 23:07:47 -05:00
|
|
|
swap car >r [
|
2004-12-29 03:35:46 -05:00
|
|
|
dataflow-graph off
|
|
|
|
call
|
|
|
|
d-in get meta-d get meta-r get get-dataflow
|
|
|
|
] with-scope
|
2005-01-16 17:58:28 -05:00
|
|
|
r> swap #label dataflow, [ node-label set ] extend >r
|
|
|
|
meta-r set meta-d set d-in set r> ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2005-02-24 20:52:17 -05:00
|
|
|
: with-block ( word [[ label quot ]] quot -- node )
|
|
|
|
#! 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
|
2005-04-16 00:23:27 -04:00
|
|
|
recursive-state [ cons ] change
|
2005-02-24 20:52:17 -05:00
|
|
|
r> call
|
|
|
|
] (with-block) ;
|
|
|
|
|
|
|
|
: infer-quot-value ( value -- )
|
|
|
|
gensym dup pick literal-value cons [
|
|
|
|
drop
|
|
|
|
dup value-recursion recursive-state set
|
|
|
|
literal-value dup infer-quot
|
|
|
|
] with-block drop handle-terminator ;
|
|
|
|
|
2005-01-13 14:41:08 -05:00
|
|
|
: boolean-value? ( value -- ? )
|
|
|
|
#! Return if the value's boolean valuation is known.
|
|
|
|
value-class
|
|
|
|
dup \ f = swap
|
|
|
|
builtin-supertypes
|
|
|
|
\ f builtin-supertypes intersection not
|
|
|
|
or ;
|
|
|
|
|
|
|
|
: boolean-value ( value -- ? )
|
|
|
|
#! Only valid if boolean? returns true.
|
|
|
|
value-class \ f = not ;
|
|
|
|
|
|
|
|
: static-branch? ( value -- ? )
|
2005-01-16 17:58:28 -05:00
|
|
|
drop f ;
|
|
|
|
! boolean-value? branches-can-fail? not and ;
|
2004-12-27 15:27:18 -05:00
|
|
|
|
2004-12-26 02:52:39 -05:00
|
|
|
: static-ifte ( true false -- )
|
|
|
|
#! If the branch taken is statically known, just infer
|
|
|
|
#! along that branch.
|
2005-01-13 14:41:08 -05:00
|
|
|
dataflow-drop, pop-d boolean-value [ drop ] [ nip ] ifte
|
2005-02-24 20:52:17 -05:00
|
|
|
infer-quot-value ;
|
2004-12-26 02:52:39 -05:00
|
|
|
|
|
|
|
: dynamic-ifte ( true false -- )
|
|
|
|
#! If branch taken is computed, infer along both paths and
|
|
|
|
#! unify.
|
2005-01-14 14:56:19 -05:00
|
|
|
2list >r 1 meta-d get vector-tail* \ ifte r>
|
2004-12-26 02:16:38 -05:00
|
|
|
pop-d [
|
2005-02-23 21:50:51 -05:00
|
|
|
dup \ general-t <class-tie> ,
|
|
|
|
\ f <class-tie> ,
|
2004-12-26 02:16:38 -05:00
|
|
|
] make-list zip ( condition )
|
2004-11-26 22:23:57 -05:00
|
|
|
infer-branches ;
|
|
|
|
|
2004-12-26 02:52:39 -05:00
|
|
|
: infer-ifte ( -- )
|
|
|
|
#! Infer effects for both branches, unify.
|
|
|
|
[ object general-list general-list ] ensure-d
|
|
|
|
dataflow-drop, pop-d
|
|
|
|
dataflow-drop, pop-d swap
|
2005-01-10 23:08:27 -05:00
|
|
|
peek-d static-branch? [
|
|
|
|
static-ifte
|
|
|
|
] [
|
2004-12-27 15:27:18 -05:00
|
|
|
dynamic-ifte
|
2005-01-10 23:08:27 -05:00
|
|
|
] ifte ;
|
2004-12-26 02:52:39 -05:00
|
|
|
|
2005-03-05 14:45:23 -05:00
|
|
|
\ ifte [ infer-ifte ] "infer" set-word-prop
|
2004-12-13 16:28:28 -05:00
|
|
|
|
2004-12-19 22:53:41 -05:00
|
|
|
: vtable>list ( value -- list )
|
2005-04-02 02:39:33 -05:00
|
|
|
dup value-recursion swap literal-value >list
|
2004-12-22 22:16:46 -05:00
|
|
|
[ over <literal> ] map nip ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2005-02-23 21:50:51 -05:00
|
|
|
: <dispatch-index> ( value -- value )
|
|
|
|
value-literal-ties
|
|
|
|
0 recursive-state get <literal>
|
|
|
|
[ set-value-literal-ties ] keep ;
|
|
|
|
|
2005-02-24 20:52:17 -05:00
|
|
|
: static-dispatch? ( -- )
|
|
|
|
peek-d literal? branches-can-fail? not and ;
|
|
|
|
|
2005-01-14 14:56:19 -05:00
|
|
|
USE: kernel-internals
|
2005-02-24 20:52:17 -05:00
|
|
|
|
|
|
|
: static-dispatch ( vtable -- )
|
2005-03-29 19:11:10 -05:00
|
|
|
>r pop-literal r>
|
2005-02-24 20:52:17 -05:00
|
|
|
dup literal-value swap value-recursion
|
2005-04-26 00:35:55 -04:00
|
|
|
>r nth r> <literal> infer-quot-value ;
|
2005-02-24 20:52:17 -05:00
|
|
|
|
|
|
|
: dynamic-dispatch ( vtable -- )
|
2005-01-14 14:56:19 -05:00
|
|
|
>r 1 meta-d get vector-tail* \ dispatch r>
|
2005-02-24 20:52:17 -05:00
|
|
|
vtable>list
|
2005-02-23 21:50:51 -05:00
|
|
|
pop-d <dispatch-index>
|
|
|
|
over length [ <literal-tie> ] project-with
|
|
|
|
zip infer-branches ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2005-02-24 20:52:17 -05:00
|
|
|
: infer-dispatch ( -- )
|
|
|
|
#! Infer effects for all branches, unify.
|
|
|
|
[ object vector ] ensure-d
|
|
|
|
dataflow-drop, pop-d static-dispatch? [
|
|
|
|
static-dispatch
|
|
|
|
] [
|
|
|
|
dynamic-dispatch
|
|
|
|
] ifte ;
|
|
|
|
|
2005-03-05 14:45:23 -05:00
|
|
|
\ dispatch [ infer-dispatch ] "infer" set-word-prop
|
2004-12-24 17:29:16 -05:00
|
|
|
\ dispatch [ [ fixnum vector ] [ ] ]
|
2005-03-05 14:45:23 -05:00
|
|
|
"infer-effect" set-word-prop
|