factor/library/inference/branches.factor

174 lines
5.3 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: errors
USE: interpreter
USE: kernel
USE: lists
USE: logic
USE: math
USE: namespaces
USE: stack
USE: strings
USE: vectors
USE: words
USE: hashtables
2004-12-07 23:21:32 -05:00
: unify-d-in ( list -- d-in )
0 swap [ [ d-in get ] bind [ max ] when* ] each ;
2004-11-26 22:23:57 -05:00
: balanced? ( list -- ? )
2004-12-07 23:21:32 -05:00
[ [ d-in get meta-d get and ] bind ] subset
[ [ d-in get meta-d get vector-length - ] bind ] map all=? ;
2004-11-26 22:23:57 -05:00
2004-12-07 23:21:32 -05:00
: longest-vector ( list -- length )
2004-11-26 22:23:57 -05:00
[ vector-length ] map [ > ] top ;
: 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 ;
2004-12-07 23:21:32 -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.
dup longest-vector swap [ dupd ensure nip ] map nip ;
: unify-datastacks ( list -- datastack )
[ [ meta-d get ] bind ] map [ ] subset
unify-lengths unify-stacks ;
: check-lengths ( list -- )
[ vector-length ] map all=? [
"Unbalanced return stack effect" throw
] unless ;
: unify-callstacks ( list -- datastack )
[ [ meta-r get ] bind ] map [ ] subset
dup check-lengths unify-stacks ;
2004-11-26 22:23:57 -05:00
: unify ( list -- )
dup balanced? [
2004-12-07 23:21:32 -05:00
dup unify-d-in d-in set
dup unify-datastacks meta-d set
unify-callstacks meta-r set
2004-11-26 22:23:57 -05:00
] [
"Unbalanced branches" throw
] ifte ;
2004-12-07 23:21:32 -05:00
: infer-branch ( quot -- namespace )
<namespace> [
copy-interpreter
dataflow-graph off
infer-quot
( #values values-node )
] extend ;
2004-11-30 23:56:01 -05:00
: terminator? ( quot -- ? )
#! This is a hack. no-method has a stack effect that
#! probably does not match any other branch of the generic,
#! so we handle it specially.
\ no-method swap tree-contains? ;
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-12-07 23:21:32 -05:00
infer-branch [
d-in get meta-d get vector-length cons
] bind 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-12-07 23:21:32 -05:00
: (infer-branches) ( branchlist -- list )
2004-11-30 23:56:01 -05:00
dup [
car dup terminator? [ drop ] [ recursive-branch ] ifte
] each
[
car dup terminator? [
2004-12-07 23:21:32 -05:00
infer-branch [
meta-d off meta-r off d-in off
] extend
2004-11-30 23:56:01 -05:00
] [
infer-branch
] ifte
2004-12-07 23:21:32 -05:00
] map ;
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.
2004-12-07 23:21:32 -05:00
(infer-branches) [
[ [ get-dataflow ] bind ] map
swap dataflow, [ node-consume-d set ] bind
] keep 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
2004-12-04 23:45:41 -05:00
dataflow-drop, pop-d swap 2list
>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.
2004-11-30 23:56:01 -05:00
unswons vector>list [ unit over cons ] map nip ;
2004-11-26 22:23:57 -05:00
: 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
>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
>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