factor/library/inference/branches.factor

208 lines
5.8 KiB
Factor
Raw Normal View History

2005-01-13 14:41:08 -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 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-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 ;
: add-inputs ( count stack -- stack )
2004-12-31 02:17:45 -05:00
#! Add this many inputs to the given stack.
[ 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.
dup all=? [
car
] [
2004-12-31 02:17:45 -05:00
[ value-class ] map class-or-list <computed>
] ifte ;
2004-12-31 02:17:45 -05:00
: vector-transpose ( list -- vector )
#! Turn a list of same-length vectors into a vector of lists.
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.
unify-lengths vector-transpose [ unify-results ] map ;
: balanced? ( list -- ? )
#! Check if a list of [[ instack outstack ]] pairs is
#! balanced.
[ uncons length swap length - ] map all=? ;
2004-12-07 23:21:32 -05:00
: unify-effect ( list -- in out )
#! Unify a list of [[ instack outstack ]] pairs.
dup balanced? [
unzip unify-stacks >r unify-stacks r>
2004-12-25 21:28:47 -05:00
] [
"Unbalanced branches" inference-error
2004-12-25 21:28:47 -05:00
] ifte ;
2004-12-07 23:21:32 -05:00
: datastack-effect ( list -- )
[ [ effect ] bind ] map
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 )
#! 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 -- )
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-05-16 01:15:48 -04:00
GENERIC: (deep-clone)
2005-01-28 23:55:22 -05:00
: deep-clone ( obj -- obj )
2005-03-21 14:39:46 -05:00
dup cloned get assq [ ] [
2005-05-16 01:15:48 -04:00
dup (deep-clone) [ swap cloned [ acons ] change ] keep
2005-03-21 14:39:46 -05:00
] ?ifte ;
2004-12-26 17:04:08 -05:00
2005-05-16 01:15:48 -04:00
M: tuple (deep-clone) ( obj -- obj )
#! Clone an object if it hasn't already been cloned in this
#! with-deep-clone scope.
clone dup <mirror> [ deep-clone ] nmap ;
M: vector (deep-clone) ( seq -- seq )
2005-04-19 20:28:01 -04:00
#! Clone a sequence and each object it contains.
[ deep-clone ] map ;
2004-12-26 17:04:08 -05:00
2005-05-16 01:15:48 -04:00
M: cons (deep-clone) ( cons -- cons )
uncons deep-clone >r deep-clone r> cons ;
M: object (deep-clone) ( obj -- obj ) ;
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-05-16 01:15:48 -04:00
meta-r [ deep-clone ] change
meta-d [ deep-clone ] change
d-in [ deep-clone ] 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 )
#! 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
uncons deep-clone pull-tie
cloned off
dup value-recursion recursive-state set
2005-01-31 14:02:09 -05:00
literal-value dup infer-quot
active? [
#values values-node
handle-terminator
] [
drop
] ifte
2004-12-07 23:21:32 -05:00
] extend ;
: (infer-branches) ( branchlist -- list )
#! The branchlist is a list of pairs: [[ value typeprop ]]
#! value is either a literal or computed instance; typeprop
#! is a pair [[ value class ]] indicating a type propagation
#! for the given branch.
2004-12-30 02:40:14 -05:00
[
[
inferring-base-case get [
[ 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
: unify-dataflow ( input instruction effectlist -- )
2004-12-24 17:29:16 -05:00
[ [ get-dataflow ] bind ] map
swap dataflow, [ unit node-consume-d set ] bind ;
2004-12-24 17:29:16 -05:00
: infer-branches ( input 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
#! base case to this stack effect and try again.
(infer-branches) dup unify-effects unify-dataflow ;
2005-05-16 01:15:48 -04:00
: boolean-value? ( value -- ? )
#! Return if the value's boolean valuation is known.
value-class dup \ f = >r \ f class-and null = r> or ;
: boolean-value ( value -- ? )
#! Only valid if boolean? returns true.
value-class \ f = not ;
: static-ifte? ( value -- ? )
#! Is the outcome of this branch statically known?
dup value-safe? swap boolean-value? and ;
: static-ifte ( true false -- )
#! If the branch taken is statically known, just infer
#! along that branch.
1 dataflow-drop, pop-d boolean-value [ drop ] [ nip ] ifte
>literal< infer-quot-value ;
2005-05-15 21:17:56 -04:00
: infer-ifte ( true false -- )
#! If branch taken is computed, infer along both paths and
#! unify.
2005-05-15 21:17:56 -04:00
2list >r pop-d \ ifte r>
2005-05-16 01:15:48 -04:00
pick [ POSTPONE: f general-t ] [ <class-tie> ] map-with
2005-05-15 21:17:56 -04:00
zip ( condition )
2004-11-26 22:23:57 -05:00
infer-branches ;
2005-05-15 21:17:56 -04:00
\ ifte [
2005-05-16 01:15:48 -04:00
2 dataflow-drop, pop-d pop-d swap
peek-d static-ifte? [
static-ifte
] [
infer-ifte
] ifte
2005-05-15 21:17:56 -04:00
] "infer" set-word-prop
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-02-23 21:50:51 -05:00
: <dispatch-index> ( value -- value )
value-literal-ties
0 recursive-state get <literal>
[ set-value-literal-ties ] keep ;
USE: kernel-internals
2005-05-15 21:17:56 -04:00
: infer-dispatch ( rstate vtable -- )
>r >r peek-d \ dispatch r> r>
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-05-16 01:15:48 -04:00
\ dispatch [
pop-literal infer-dispatch
] "infer" set-word-prop
\ dispatch [ [ fixnum vector ] [ ] ] "infer-effect" set-word-prop