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
|
|
|
|
2005-05-15 21:17:56 -04:00
|
|
|
: longest ( list -- length )
|
2005-07-16 22:16:18 -04:00
|
|
|
[ length ] map 0 [ max ] reduce ;
|
2004-12-31 02:17:45 -05:00
|
|
|
|
|
|
|
: computed-value-vector ( n -- vector )
|
2005-07-16 23:01:51 -04:00
|
|
|
empty-vector [ drop object <computed> ] map ;
|
2004-12-31 02:17:45 -05:00
|
|
|
|
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-05-15 21:17:56 -04:00
|
|
|
dup longest 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.
|
2005-05-22 21:07:24 -04:00
|
|
|
dup [ eq? ] fiber? [
|
2004-12-31 02:17:45 -05:00
|
|
|
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-11-26 22:23:57 -05:00
|
|
|
: unify-stacks ( list -- stack )
|
|
|
|
#! Replace differing literals in stacks with unknown
|
|
|
|
#! results.
|
2005-07-25 17:13:35 -04:00
|
|
|
unify-lengths seq-transpose [ unify-results ] 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-05-22 21:07:24 -04:00
|
|
|
[ uncons length swap length - ] map [ = ] fiber? ;
|
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
|
|
|
|
2005-05-17 16:13:08 -04:00
|
|
|
: unify-dataflow ( effects -- nodes )
|
|
|
|
[ [ dataflow-graph get ] bind ] map ;
|
|
|
|
|
2005-05-22 21:07:24 -04:00
|
|
|
: clone-values ( seq -- seq ) [ clone-value ] map ;
|
2005-05-16 01:15:48 -04:00
|
|
|
|
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.
|
2005-05-22 21:07:24 -04:00
|
|
|
cloned off
|
|
|
|
meta-r [ clone-values ] change
|
|
|
|
meta-d [ clone-values ] change
|
|
|
|
d-in [ clone-values ] change
|
2005-05-17 16:13:08 -04:00
|
|
|
dataflow-graph off
|
|
|
|
current-node 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> [
|
2004-12-26 17:04:08 -05:00
|
|
|
copy-inference
|
2005-05-16 01:15:48 -04:00
|
|
|
dup value-recursion recursive-state set
|
2005-01-31 14:02:09 -05:00
|
|
|
literal-value dup infer-quot
|
2005-02-08 22:02:44 -05:00
|
|
|
active? [
|
2005-05-17 16:13:08 -04:00
|
|
|
#values node,
|
2005-02-08 22:02:44 -05:00
|
|
|
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-05-23 00:25:52 -04:00
|
|
|
[ infer-branch ] map dup unify-effects unify-dataflow ;
|
2004-11-29 23:14:12 -05:00
|
|
|
|
2005-05-17 16:13:08 -04:00
|
|
|
: infer-branches ( branches node -- )
|
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
|
2005-05-14 00:23:00 -04:00
|
|
|
#! base case to this stack effect and try again.
|
2005-05-23 00:25:52 -04:00
|
|
|
[ >r (infer-branches) r> set-node-children ] keep node, ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2005-05-15 21:17:56 -04:00
|
|
|
\ ifte [
|
2005-05-17 16:13:08 -04:00
|
|
|
2 #drop node, pop-d pop-d swap 2list
|
|
|
|
#ifte pop-d drop infer-branches
|
2005-05-15 21:17:56 -04:00
|
|
|
] "infer" set-word-prop
|
2004-12-26 02:52:39 -05:00
|
|
|
|
2005-05-15 21:17:56 -04:00
|
|
|
: vtable>list ( rstate vtable -- list )
|
|
|
|
[ swap <literal> ] map-with >list ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2005-01-14 14:56:19 -05:00
|
|
|
USE: kernel-internals
|
2005-02-24 20:52:17 -05:00
|
|
|
|
2005-05-17 16:13:08 -04:00
|
|
|
\ dispatch [
|
|
|
|
pop-literal vtable>list
|
|
|
|
#dispatch pop-d drop infer-branches
|
|
|
|
] "infer" set-word-prop
|