factor/library/inference/branches.factor

155 lines
4.8 KiB
Factor
Raw Normal View History

2004-11-26 22:23:57 -05:00
! :folding=indent:collapseFolds=1:
! $Id$
!
! Copyright (C) 2004 Slava Pestov.
!
! Redistribution and use in source and binary forms, with or without
! modification, are permitted provided that the following conditions are met:
!
! 1. Redistributions of source code must retain the above copyright notice,
! this list of conditions and the following disclaimer.
!
! 2. Redistributions in binary form must reproduce the above copyright notice,
! this list of conditions and the following disclaimer in the documentation
! and/or other materials provided with the distribution.
!
! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
IN: inference
USE: combinators
USE: dataflow
2004-11-26 22:23:57 -05:00
USE: errors
USE: interpreter
USE: kernel
USE: lists
USE: logic
USE: math
USE: namespaces
USE: stack
USE: strings
USE: vectors
USE: words
USE: hashtables
2004-11-29 23:14:12 -05:00
: branch-effect ( -- [ dataflow [ in-d | datastack ] ] )
get-dataflow d-in get meta-d get cons cons ;
: infer-branch ( quot -- [ dataflow [ in-d | datastack ] ] )
2004-11-26 22:23:57 -05:00
#! Infer the quotation's effect, restoring the meta
#! interpreter state afterwards.
[
copy-interpreter
dataflow-graph off
(infer)
2004-11-29 23:14:12 -05:00
branch-effect
] with-scope ;
2004-11-26 22:23:57 -05:00
: difference ( [ in | stack ] -- diff )
#! Stack height difference of infer-branch return value.
uncons vector-length - ;
: balanced? ( list -- ? )
#! Check if a list of [ in | stack ] pairs has the same
#! stack height.
[ difference ] map all=? ;
: max-vector-length ( list -- length )
[ vector-length ] map [ > ] top ;
: unify-lengths ( list -- list )
#! Pad all vectors to the same length. If one vector is
#! shorter, pad it with unknown results at the bottom.
dup max-vector-length swap [ dupd ensure nip ] map nip ;
: unify-result ( obj obj -- obj )
#! Replace values with unknown result if they differ,
#! otherwise retain them.
2dup = [ drop ] [ 2drop gensym ] ifte ;
: unify-stacks ( list -- stack )
#! Replace differing literals in stacks with unknown
#! results.
uncons [ [ unify-result ] vector-2map ] each ;
: unify ( list -- )
#! Unify meta-interpreter state from two branches.
dup balanced? [
unzip
unify-lengths unify-stacks meta-d set
[ > ] top d-in set
] [
"Unbalanced branches" throw
] ifte ;
2004-11-29 23:14:12 -05:00
: recursive-branch ( quot -- )
#! Set base case if inference didn't fail.
2004-11-26 22:23:57 -05:00
[
2004-11-29 23:14:12 -05:00
infer-branch cdr recursive-state get set-base
2004-11-26 22:23:57 -05:00
] [
2004-11-29 23:14:12 -05:00
[ drop ] when
2004-11-26 22:23:57 -05:00
] catch ;
2004-11-29 23:14:12 -05:00
: (infer-branches) ( branchlist -- dataflowlist effectlist )
dup
[ car recursive-branch ] each
[ car infer-branch ] map
unzip ;
: 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.
(infer-branches) >r
swap dataflow, [ node-consume-d set ] bind
r> unify ;
2004-11-26 22:23:57 -05:00
: infer-ifte ( -- )
#! Infer effects for both branches, unify.
2004-11-27 23:09:32 -05:00
3 ensure-d
2004-11-28 21:56:58 -05:00
dataflow-drop, pop-d
dataflow-drop, pop-d 2list
2004-11-29 23:14:12 -05:00
>r 1 meta-d get vector-tail* IFTE r>
2004-11-26 22:23:57 -05:00
pop-d drop ( condition )
infer-branches ;
: vtable>list ( [ vtable | rstate ] -- list )
#! generic and 2generic use vectors of words, we need lists
#! of quotations. Filter out no-method. Dirty workaround;
#! later properly handle throw.
unswons vector>list [
dup \ no-method = [ drop f ] [ unit over cons ] ifte
] map [ ] subset nip ;
: infer-generic ( -- )
#! Infer effects for all branches, unify.
2004-11-27 23:09:32 -05:00
2 ensure-d
2004-11-28 21:56:58 -05:00
dataflow-drop, pop-d vtable>list
2004-11-29 23:14:12 -05:00
>r 1 meta-d get vector-tail* GENERIC r>
2004-11-26 22:23:57 -05:00
infer-branches ;
: infer-2generic ( -- )
#! Infer effects for all branches, unify.
2004-11-27 23:09:32 -05:00
3 ensure-d
2004-11-28 21:56:58 -05:00
dataflow-drop, pop-d vtable>list
2004-11-29 23:14:12 -05:00
>r 2 meta-d get vector-tail* 2GENERIC r>
2004-11-26 22:23:57 -05:00
infer-branches ;
\ ifte [ infer-ifte ] "infer" set-word-property
\ generic [ infer-generic ] "infer" set-word-property
\ generic [ 2 | 0 ] "infer-effect" set-word-property
\ 2generic [ infer-2generic ] "infer" set-word-property
\ 2generic [ 3 | 0 ] "infer-effect" set-word-property