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: errors
|
|
|
|
USE: interpreter
|
|
|
|
USE: kernel
|
|
|
|
USE: lists
|
|
|
|
USE: math
|
|
|
|
USE: namespaces
|
|
|
|
USE: strings
|
|
|
|
USE: vectors
|
|
|
|
USE: words
|
|
|
|
USE: hashtables
|
2004-12-19 22:53:41 -05:00
|
|
|
USE: generic
|
2004-11-26 22:23:57 -05:00
|
|
|
|
|
|
|
! Word properties that affect inference:
|
|
|
|
! - infer-effect -- must be set. controls number of inputs
|
|
|
|
! expected, and number of outputs produced.
|
|
|
|
! - infer - quotation with custom inference behavior; ifte uses
|
|
|
|
! this. Word is passed on the stack.
|
|
|
|
|
|
|
|
! Amount of results we had to add to the datastack
|
|
|
|
SYMBOL: d-in
|
|
|
|
|
|
|
|
! Recursive state. Alist maps words to hashmaps...
|
|
|
|
SYMBOL: recursive-state
|
|
|
|
! ... with keys:
|
|
|
|
SYMBOL: base-case
|
|
|
|
SYMBOL: entry-effect
|
2004-12-03 22:12:58 -05:00
|
|
|
! When a call to a combinator is compiled, recursion cannot
|
|
|
|
! simply jump to the definition of the combinator. Instead, it
|
|
|
|
! makes a local jump to this label.
|
|
|
|
SYMBOL: recursive-label
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2004-12-08 18:39:36 -05:00
|
|
|
! When inferring stack effects of mutually recursive words, we
|
|
|
|
! don't want to save the fact that one word does not have a
|
|
|
|
! stack effect before the base case of its mutual pair is
|
|
|
|
! inferred.
|
|
|
|
SYMBOL: save-effect
|
|
|
|
|
2004-12-19 22:53:41 -05:00
|
|
|
! A value has the following slots:
|
2004-12-22 22:16:46 -05:00
|
|
|
GENERIC: literal-value ( value -- obj )
|
2004-12-19 22:53:41 -05:00
|
|
|
GENERIC: value= ( literal value -- ? )
|
2004-12-22 22:16:46 -05:00
|
|
|
GENERIC: value-class ( value -- class )
|
2004-12-19 22:53:41 -05:00
|
|
|
|
2004-12-22 22:16:46 -05:00
|
|
|
TRAITS: computed
|
|
|
|
C: computed ( class -- value )
|
|
|
|
[
|
|
|
|
\ value-class set
|
|
|
|
gensym \ literal-value set
|
|
|
|
] extend ;
|
|
|
|
M: computed literal-value ( value -- obj )
|
2004-12-19 22:53:41 -05:00
|
|
|
"Cannot use a computed value literally." throw ;
|
2004-12-22 22:16:46 -05:00
|
|
|
M: computed value= ( literal value -- ? )
|
2004-12-19 22:53:41 -05:00
|
|
|
2drop f ;
|
2004-12-22 22:16:46 -05:00
|
|
|
M: computed value-class ( value -- class )
|
|
|
|
[ \ value-class get ] bind ;
|
|
|
|
|
|
|
|
TRAITS: literal
|
|
|
|
C: literal ( obj rstate -- value )
|
|
|
|
[ recursive-state set \ literal-value set ] extend ;
|
|
|
|
M: literal literal-value ( value -- obj )
|
|
|
|
[ \ literal-value get ] bind ;
|
|
|
|
M: literal value= ( literal value -- ? )
|
|
|
|
literal-value = ;
|
|
|
|
M: literal value-class ( value -- class )
|
|
|
|
literal-value class ;
|
2004-12-19 22:53:41 -05:00
|
|
|
|
|
|
|
: value-recursion ( value -- rstate )
|
|
|
|
[ recursive-state get ] bind ;
|
|
|
|
|
2004-12-23 01:14:07 -05:00
|
|
|
: required-inputs ( typelist stack -- values )
|
|
|
|
>r dup length r> vector-length - dup 0 > [
|
|
|
|
head [ <computed> ] map
|
2004-11-26 22:23:57 -05:00
|
|
|
] [
|
2004-12-23 01:14:07 -05:00
|
|
|
2drop f
|
2004-11-26 22:23:57 -05:00
|
|
|
] ifte ;
|
|
|
|
|
2004-12-23 01:14:07 -05:00
|
|
|
: vector-prepend ( values stack -- stack )
|
|
|
|
>r list>vector dup r> vector-append ;
|
|
|
|
|
|
|
|
: ensure-d ( typelist -- )
|
|
|
|
meta-d get required-inputs dup
|
|
|
|
meta-d [ vector-prepend ] change
|
|
|
|
d-in [ vector-prepend ] change ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
|
|
|
: effect ( -- [ in | out ] )
|
|
|
|
#! After inference is finished, collect information.
|
2004-12-22 22:16:46 -05:00
|
|
|
d-in get vector-length meta-d get vector-length cons ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
|
|
|
: <recursive-state> ( -- state )
|
|
|
|
<namespace> [
|
|
|
|
base-case off effect entry-effect set
|
|
|
|
] extend ;
|
|
|
|
|
|
|
|
: init-inference ( recursive-state -- )
|
|
|
|
init-interpreter
|
2004-12-22 22:16:46 -05:00
|
|
|
0 <vector> d-in set
|
2004-11-26 22:23:57 -05:00
|
|
|
recursive-state set
|
2004-12-08 18:39:36 -05:00
|
|
|
dataflow-graph off
|
|
|
|
save-effect on ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
|
|
|
DEFER: apply-word
|
|
|
|
|
2004-11-27 00:33:17 -05:00
|
|
|
: apply-literal ( obj -- )
|
|
|
|
#! Literals are annotated with the current recursive
|
|
|
|
#! state.
|
2004-12-22 22:16:46 -05:00
|
|
|
dup recursive-state get <literal> push-d
|
2004-12-01 19:48:08 -05:00
|
|
|
#push dataflow, [ 1 0 node-outputs ] bind ;
|
2004-11-27 00:33:17 -05:00
|
|
|
|
2004-11-26 22:23:57 -05:00
|
|
|
: apply-object ( obj -- )
|
|
|
|
#! Apply the object's stack effect to the inferencer state.
|
2004-11-27 00:33:17 -05:00
|
|
|
dup word? [ apply-word ] [ apply-literal ] ifte ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2004-12-02 22:44:36 -05:00
|
|
|
: infer-quot ( quot -- )
|
2004-11-26 22:23:57 -05:00
|
|
|
#! Recursive calls to this word are made for nested
|
|
|
|
#! quotations.
|
|
|
|
[ apply-object ] each ;
|
|
|
|
|
|
|
|
: compose ( first second -- total )
|
|
|
|
#! Stack effect composition.
|
|
|
|
>r uncons r> uncons >r -
|
|
|
|
dup 0 < [ neg + r> cons ] [ r> + cons ] ifte ;
|
|
|
|
|
|
|
|
: raise ( [ in | out ] -- [ in | out ] )
|
|
|
|
uncons 2dup min tuck - >r - r> cons ;
|
|
|
|
|
|
|
|
: 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 raise ;
|
|
|
|
|
2004-12-07 23:21:32 -05:00
|
|
|
: set-base ( [ in | out ] rstate -- )
|
2004-11-26 22:23:57 -05:00
|
|
|
#! Set the base case of the current word.
|
2004-11-27 00:51:28 -05:00
|
|
|
dup [
|
2004-12-07 23:21:32 -05:00
|
|
|
car cdr [
|
2004-11-27 00:51:28 -05:00
|
|
|
entry-effect get swap decompose base-case set
|
|
|
|
] bind
|
|
|
|
] [
|
|
|
|
2drop
|
|
|
|
] ifte ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2004-12-04 23:45:41 -05:00
|
|
|
: check-return ( -- )
|
|
|
|
#! Raise an error if word leaves values on return stack.
|
|
|
|
meta-r get vector-length 0 = [
|
|
|
|
"Word leaves elements on return stack" throw
|
|
|
|
] unless ;
|
|
|
|
|
|
|
|
: values-node ( op -- )
|
|
|
|
#! Add a #values or #return node to the graph.
|
|
|
|
f swap dataflow, [
|
2004-12-02 22:44:36 -05:00
|
|
|
meta-d get vector>list node-consume-d set
|
|
|
|
] bind ;
|
|
|
|
|
|
|
|
: (infer) ( quot -- )
|
2004-12-04 23:45:41 -05:00
|
|
|
f init-inference
|
|
|
|
infer-quot
|
|
|
|
#return values-node check-return ;
|
2004-12-02 22:44:36 -05:00
|
|
|
|
2004-11-26 22:23:57 -05:00
|
|
|
: infer ( quot -- [ in | out ] )
|
|
|
|
#! Stack effect of a quotation.
|
2004-12-02 22:44:36 -05:00
|
|
|
[ (infer) effect ] with-scope ;
|
2004-11-27 00:33:17 -05:00
|
|
|
|
2004-11-26 22:23:57 -05:00
|
|
|
: try-infer ( quot -- effect/f )
|
|
|
|
#! Push f if inference fails.
|
|
|
|
[ infer ] [ [ drop f ] when ] catch ;
|
2004-12-01 19:48:08 -05:00
|
|
|
|
|
|
|
: dataflow ( quot -- dataflow )
|
|
|
|
#! Data flow of a quotation.
|
2004-12-02 22:44:36 -05:00
|
|
|
[ (infer) get-dataflow ] with-scope ;
|
2004-12-22 22:16:46 -05:00
|
|
|
|
|
|
|
: type-infer ( quot -- [ in-types out-types ] )
|
|
|
|
[
|
|
|
|
(infer)
|
2004-12-23 01:14:07 -05:00
|
|
|
d-in get [ value-class ] vector-map vector>list
|
|
|
|
meta-d get [ value-class ] vector-map vector>list 2list
|
2004-12-22 22:16:46 -05:00
|
|
|
] with-scope ;
|