2004-11-03 23:35:36 -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
|
2004-11-06 21:03:35 -05:00
|
|
|
USE: logic
|
2004-11-03 23:35:36 -05:00
|
|
|
USE: math
|
|
|
|
|
USE: namespaces
|
|
|
|
|
USE: stack
|
|
|
|
|
USE: strings
|
|
|
|
|
USE: vectors
|
|
|
|
|
USE: words
|
2004-11-06 21:03:35 -05:00
|
|
|
USE: hashtables
|
2004-11-03 23:35:36 -05:00
|
|
|
|
|
|
|
|
! Word properties that affect inference:
|
|
|
|
|
! - infer-effect -- must be set. controls number of inputs
|
|
|
|
|
! expected, and number of outputs produced.
|
|
|
|
|
! - meta-infer -- evaluate word in meta-interpreter if set.
|
|
|
|
|
! - infer - quotation with custom inference behavior; ifte uses
|
|
|
|
|
! this. Word is passed on the stack.
|
2004-11-20 16:57:01 -05:00
|
|
|
! - recursive-infer - if true, inferencer will always invoke
|
|
|
|
|
! itself recursively with this word, instead of solving a
|
|
|
|
|
! fixed-point equation for recursive calls.
|
2004-11-03 23:35:36 -05:00
|
|
|
|
2004-11-06 21:03:35 -05:00
|
|
|
! Amount of results we had to add to the datastack
|
2004-11-03 23:35:36 -05:00
|
|
|
SYMBOL: d-in
|
2004-11-06 21:03:35 -05:00
|
|
|
! Amount of results we had to add to the callstack
|
2004-11-03 23:35:36 -05:00
|
|
|
SYMBOL: r-in
|
2004-11-20 16:57:01 -05:00
|
|
|
|
|
|
|
|
! Recursive state. Alist maps words to hashmaps...
|
2004-11-06 21:03:35 -05:00
|
|
|
SYMBOL: recursive-state
|
2004-11-20 16:57:01 -05:00
|
|
|
! ... with keys:
|
|
|
|
|
SYMBOL: base-case
|
|
|
|
|
SYMBOL: entry-effect
|
2004-11-03 23:35:36 -05:00
|
|
|
|
|
|
|
|
: gensym-vector ( n -- vector )
|
|
|
|
|
dup <vector> swap [ gensym over vector-push ] times ;
|
|
|
|
|
|
|
|
|
|
: inputs ( count stack -- stack )
|
|
|
|
|
#! Add this many inputs to the given stack.
|
2004-11-05 17:41:54 -05:00
|
|
|
>r gensym-vector dup r> vector-append ;
|
2004-11-03 23:35:36 -05:00
|
|
|
|
2004-11-05 17:41:54 -05:00
|
|
|
: ensure ( count stack -- count stack )
|
2004-11-17 20:59:28 -05:00
|
|
|
#! Ensure stack has this many elements. Return number of
|
|
|
|
|
#! elements added.
|
2004-11-03 23:35:36 -05:00
|
|
|
2dup vector-length > [
|
2004-11-05 17:41:54 -05:00
|
|
|
[ vector-length - dup ] keep inputs
|
2004-11-03 23:35:36 -05:00
|
|
|
] [
|
2004-11-05 17:41:54 -05:00
|
|
|
>r drop 0 r>
|
2004-11-03 23:35:36 -05:00
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
|
|
: ensure-d ( count -- )
|
|
|
|
|
#! Ensure count of unknown results are on the stack.
|
2004-11-06 21:03:35 -05:00
|
|
|
meta-d get ensure meta-d set d-in +@ ;
|
2004-11-03 23:35:36 -05:00
|
|
|
|
|
|
|
|
: consume-d ( count -- )
|
|
|
|
|
#! Remove count of elements.
|
|
|
|
|
[ pop-d drop ] times ;
|
|
|
|
|
|
|
|
|
|
: produce-d ( count -- )
|
|
|
|
|
#! Push count of unknown results.
|
|
|
|
|
[ gensym push-d ] times ;
|
|
|
|
|
|
2004-11-06 21:03:35 -05:00
|
|
|
: consume/produce ( [ in | out ] -- )
|
|
|
|
|
unswons dup ensure-d consume-d produce-d ;
|
|
|
|
|
|
2004-11-03 23:35:36 -05:00
|
|
|
: standard-effect ( word [ in | out ] -- )
|
2004-11-05 17:41:54 -05:00
|
|
|
#! If a word does not have special inference behavior, we
|
|
|
|
|
#! either execute the word in the meta interpreter (if it is
|
|
|
|
|
#! side-effect-free and all parameters are literal), or
|
|
|
|
|
#! simply apply its stack effect to the meta-interpreter.
|
2004-11-03 23:35:36 -05:00
|
|
|
over "meta-infer" word-property [
|
|
|
|
|
drop host-word
|
|
|
|
|
] [
|
2004-11-06 21:03:35 -05:00
|
|
|
nip consume/produce
|
2004-11-03 23:35:36 -05:00
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
|
|
: apply-effect ( word [ in | out ] -- )
|
|
|
|
|
#! Helper word for apply-word.
|
|
|
|
|
dup car ensure-d
|
|
|
|
|
over "infer" word-property dup [
|
|
|
|
|
nip nip call
|
|
|
|
|
] [
|
|
|
|
|
drop standard-effect
|
|
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
|
|
: no-effect ( word -- )
|
|
|
|
|
"Unknown stack effect: " swap word-name cat2 throw ;
|
|
|
|
|
|
2004-11-20 16:57:01 -05:00
|
|
|
: (effect) ( -- [ in | stack ] )
|
|
|
|
|
d-in get meta-d get cons ;
|
|
|
|
|
|
|
|
|
|
: effect ( -- [ in | out ] )
|
|
|
|
|
#! After inference is finished, collect information.
|
|
|
|
|
d-in get meta-d get vector-length cons ;
|
|
|
|
|
|
|
|
|
|
: <recursive-state> ( -- state )
|
|
|
|
|
<namespace> [
|
|
|
|
|
base-case off effect entry-effect set
|
|
|
|
|
] extend ;
|
|
|
|
|
|
2004-11-03 23:35:36 -05:00
|
|
|
DEFER: (infer)
|
|
|
|
|
|
2004-11-06 21:03:35 -05:00
|
|
|
: apply-compound ( word -- )
|
2004-11-20 16:57:01 -05:00
|
|
|
#! Infer a compound word's stack effect.
|
|
|
|
|
dup <recursive-state> cons recursive-state cons@
|
2004-11-06 21:03:35 -05:00
|
|
|
word-parameter (infer)
|
|
|
|
|
recursive-state uncons@ drop ;
|
|
|
|
|
|
2004-11-03 23:35:36 -05:00
|
|
|
: apply-word ( word -- )
|
2004-11-06 21:03:35 -05:00
|
|
|
#! Apply the word's stack effect to the inferencer state.
|
2004-11-03 23:35:36 -05:00
|
|
|
dup "infer-effect" word-property dup [
|
|
|
|
|
apply-effect
|
|
|
|
|
] [
|
2004-11-06 21:03:35 -05:00
|
|
|
drop dup compound? [ apply-compound ] [ no-effect ] ifte
|
|
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
|
|
: current-word ( -- word )
|
|
|
|
|
#! Push word we're currently inferring effect of.
|
|
|
|
|
recursive-state get car car ;
|
|
|
|
|
|
2004-11-20 16:57:01 -05:00
|
|
|
: current-state ( -- word )
|
|
|
|
|
#! Push word we're currently inferring effect of.
|
|
|
|
|
recursive-state get car cdr ;
|
|
|
|
|
|
|
|
|
|
: no-base-case ( word -- )
|
|
|
|
|
word-name " does not have a base case." cat2 throw ;
|
2004-11-06 21:03:35 -05:00
|
|
|
|
2004-11-06 21:20:05 -05:00
|
|
|
: check-recursion ( -- )
|
|
|
|
|
#! If at the location of the recursive call, we're taking
|
|
|
|
|
#! more items from the stack than producing, we have a
|
|
|
|
|
#! diverging recursion.
|
|
|
|
|
d-in get meta-d get vector-length > [
|
|
|
|
|
current-word word-name " diverges." cat2 throw
|
|
|
|
|
] when ;
|
|
|
|
|
|
2004-11-20 16:57:01 -05:00
|
|
|
: recursive-word ( word state -- )
|
2004-11-06 21:03:35 -05:00
|
|
|
#! Handle a recursive call, by either applying a previously
|
|
|
|
|
#! inferred base case, or raising an error.
|
2004-11-20 16:57:01 -05:00
|
|
|
base-case swap hash dup [
|
|
|
|
|
nip consume/produce
|
|
|
|
|
] [
|
|
|
|
|
drop no-base-case
|
|
|
|
|
] ifte ;
|
2004-11-06 21:03:35 -05:00
|
|
|
|
|
|
|
|
: apply-object ( obj -- )
|
|
|
|
|
#! Apply the object's stack effect to the inferencer state.
|
2004-11-20 16:57:01 -05:00
|
|
|
#! There are three options: recursive-infer words always
|
|
|
|
|
#! cause a recursive call of the inferencer, regardless.
|
|
|
|
|
#! Be careful, you might hang the inferencer. Other words
|
|
|
|
|
#! solve a fixed-point equation if a recursive call is made,
|
|
|
|
|
#! otherwise the inferencer is invoked recursively if its
|
|
|
|
|
#! not a recursive call.
|
2004-11-06 21:03:35 -05:00
|
|
|
dup word? [
|
2004-11-20 16:57:01 -05:00
|
|
|
dup "recursive-infer" word-property [
|
2004-11-06 21:03:35 -05:00
|
|
|
apply-word
|
2004-11-20 16:57:01 -05:00
|
|
|
] [
|
|
|
|
|
dup recursive-state get assoc dup [
|
|
|
|
|
check-recursion recursive-word
|
|
|
|
|
] [
|
|
|
|
|
drop apply-word
|
|
|
|
|
] ifte
|
|
|
|
|
] ifte
|
2004-11-06 21:03:35 -05:00
|
|
|
] [
|
|
|
|
|
push-d
|
2004-11-03 23:35:36 -05:00
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
|
|
: init-inference ( -- )
|
|
|
|
|
init-interpreter
|
|
|
|
|
0 d-in set
|
2004-11-06 21:03:35 -05:00
|
|
|
0 r-in set
|
|
|
|
|
f recursive-state set ;
|
2004-11-03 23:35:36 -05:00
|
|
|
|
|
|
|
|
: (infer) ( quot -- )
|
2004-11-05 17:41:54 -05:00
|
|
|
#! Recursive calls to this word are made for nested
|
|
|
|
|
#! quotations.
|
2004-11-06 21:03:35 -05:00
|
|
|
[ apply-object ] each ;
|
2004-11-03 23:35:36 -05:00
|
|
|
|
2004-11-17 20:59:28 -05:00
|
|
|
: infer-branch ( quot -- [ in-d | datastack ] )
|
2004-11-05 17:41:54 -05:00
|
|
|
#! Infer the quotation's effect, restoring the meta
|
|
|
|
|
#! interpreter state afterwards.
|
2004-11-20 16:57:01 -05:00
|
|
|
[ copy-interpreter (infer) (effect) ] with-scope ;
|
2004-11-03 23:35:36 -05:00
|
|
|
|
2004-11-04 21:36:33 -05:00
|
|
|
: difference ( [ in | stack ] -- diff )
|
2004-11-05 17:41:54 -05:00
|
|
|
#! Stack height difference of infer-branch return value.
|
2004-11-04 21:36:33 -05:00
|
|
|
uncons vector-length - ;
|
|
|
|
|
|
2004-11-17 20:59:28 -05:00
|
|
|
: balanced? ( list -- ? )
|
|
|
|
|
#! Check if a list of [ in | stack ] pairs has the same
|
|
|
|
|
#! stack height.
|
|
|
|
|
[ difference ] map all=? ;
|
2004-11-04 21:36:33 -05:00
|
|
|
|
2004-11-17 20:59:28 -05:00
|
|
|
: max-vector-length ( list -- length )
|
|
|
|
|
[ vector-length ] map [ > ] top ;
|
2004-11-05 17:41:54 -05:00
|
|
|
|
2004-11-17 20:59:28 -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 max-vector-length swap [ dupd ensure nip ] map nip ;
|
2004-11-05 17:41:54 -05:00
|
|
|
|
|
|
|
|
: unify-result ( obj obj -- obj )
|
|
|
|
|
#! Replace values with unknown result if they differ,
|
|
|
|
|
#! otherwise retain them.
|
|
|
|
|
2dup = [ drop ] [ 2drop gensym ] ifte ;
|
|
|
|
|
|
2004-11-17 20:59:28 -05:00
|
|
|
: unify-stacks ( list -- stack )
|
2004-11-05 17:41:54 -05:00
|
|
|
#! Replace differing literals in stacks with unknown
|
|
|
|
|
#! results.
|
2004-11-17 20:59:28 -05:00
|
|
|
uncons [ [ unify-result ] vector-2map ] each ;
|
2004-11-04 21:36:33 -05:00
|
|
|
|
2004-11-17 20:59:28 -05:00
|
|
|
: unify ( list -- )
|
2004-11-05 17:41:54 -05:00
|
|
|
#! Unify meta-interpreter state from two branches.
|
2004-11-17 20:59:28 -05:00
|
|
|
dup balanced? [
|
|
|
|
|
unzip
|
|
|
|
|
unify-lengths unify-stacks meta-d set
|
|
|
|
|
[ > ] top d-in set
|
2004-11-03 23:35:36 -05:00
|
|
|
] [
|
2004-11-17 20:59:28 -05:00
|
|
|
"Unbalanced branches" throw
|
2004-11-03 23:35:36 -05:00
|
|
|
] ifte ;
|
|
|
|
|
|
2004-11-20 16:57:01 -05:00
|
|
|
: compose ( first second -- total )
|
|
|
|
|
#! Stack effect composition.
|
|
|
|
|
>r uncons r> uncons >r -
|
|
|
|
|
dup 0 < [ neg + r> cons ] [ r> + cons ] ifte ;
|
|
|
|
|
|
|
|
|
|
: decompose ( first second -- solution )
|
|
|
|
|
#! Return a stack effect such that first*solution = second.
|
|
|
|
|
2dup 2car
|
|
|
|
|
2dup > [ "No solution to decomposition" throw ] when
|
|
|
|
|
swap - -rot 2cdr >r + r> cons ;
|
|
|
|
|
|
2004-11-06 21:03:35 -05:00
|
|
|
: set-base ( [ in | stack ] -- )
|
|
|
|
|
#! Set the base case of the current word.
|
2004-11-20 16:57:01 -05:00
|
|
|
uncons vector-length cons
|
|
|
|
|
current-state [
|
|
|
|
|
entry-effect get swap decompose base-case set
|
|
|
|
|
] bind ;
|
2004-11-06 21:03:35 -05:00
|
|
|
|
2004-11-17 20:59:28 -05:00
|
|
|
: recursive-branch ( quot -- )
|
2004-11-20 16:57:01 -05:00
|
|
|
#! Set base case if inference didn't fail
|
2004-11-17 20:59:28 -05:00
|
|
|
[ infer-branch set-base ] [ [ drop ] when ] catch ;
|
2004-11-06 21:03:35 -05:00
|
|
|
|
2004-11-17 20:59:28 -05:00
|
|
|
: infer-branches ( brachlist -- )
|
2004-11-06 21:03:35 -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.
|
2004-11-17 20:59:28 -05:00
|
|
|
dup [ recursive-branch ] each [ infer-branch ] map unify ;
|
2004-11-06 21:03:35 -05:00
|
|
|
|
2004-11-03 23:35:36 -05:00
|
|
|
: infer-ifte ( -- )
|
2004-11-05 17:41:54 -05:00
|
|
|
#! Infer effects for both branches, unify.
|
2004-11-17 20:59:28 -05:00
|
|
|
pop-d pop-d 2list pop-d drop ( condition ) infer-branches ;
|
|
|
|
|
|
|
|
|
|
: vtable>list ( vtable -- list )
|
|
|
|
|
#! generic and 2generic use vectors of words, we need lists
|
|
|
|
|
#! of quotations. Filter out no-method. Dirty workaround;
|
|
|
|
|
#! later properly handle throw.
|
|
|
|
|
vector>list [
|
|
|
|
|
dup \ no-method = [ drop f ] [ unit ] ifte
|
|
|
|
|
] map [ ] subset ;
|
|
|
|
|
|
|
|
|
|
: infer-generic ( -- )
|
|
|
|
|
#! Infer effects for all branches, unify.
|
|
|
|
|
pop-d vtable>list peek-d drop ( dispatch ) infer-branches ;
|
|
|
|
|
|
|
|
|
|
: infer-2generic ( -- )
|
|
|
|
|
#! Infer effects for all branches, unify.
|
|
|
|
|
pop-d vtable>list
|
|
|
|
|
peek-d drop ( dispatch )
|
|
|
|
|
peek-d drop ( dispatch )
|
|
|
|
|
infer-branches ;
|
2004-11-06 21:03:35 -05:00
|
|
|
|
|
|
|
|
: infer ( quot -- [ in | out ] )
|
|
|
|
|
#! Stack effect of a quotation.
|
|
|
|
|
[ init-inference (infer) effect ] with-scope ;
|
2004-11-03 23:35:36 -05:00
|
|
|
|
|
|
|
|
\ call [ pop-d (infer) ] "infer" set-word-property
|
|
|
|
|
\ ifte [ infer-ifte ] "infer" set-word-property
|
|
|
|
|
|
2004-11-17 20:59:28 -05:00
|
|
|
\ 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
|
|
|
|
|
|
2004-11-03 23:35:36 -05:00
|
|
|
\ >r [ pop-d push-r ] "infer" set-word-property
|
|
|
|
|
\ r> [ pop-r push-d ] "infer" set-word-property
|
|
|
|
|
|
|
|
|
|
\ drop t "meta-infer" set-word-property
|
|
|
|
|
\ dup t "meta-infer" set-word-property
|
2004-11-11 16:45:55 -05:00
|
|
|
\ swap t "meta-infer" set-word-property
|
2004-11-03 23:35:36 -05:00
|
|
|
\ over t "meta-infer" set-word-property
|
|
|
|
|
\ pick t "meta-infer" set-word-property
|
2004-11-11 16:45:55 -05:00
|
|
|
\ nip t "meta-infer" set-word-property
|
|
|
|
|
\ tuck t "meta-infer" set-word-property
|
2004-11-03 23:35:36 -05:00
|
|
|
\ rot t "meta-infer" set-word-property
|