values are now objects in inferencer

cvs
Slava Pestov 2004-12-20 03:53:41 +00:00
parent f9ba944fde
commit cc43f52bb8
8 changed files with 73 additions and 37 deletions

View File

@ -184,9 +184,11 @@ public class ExternalFactor extends DefaultVocabularyLookup
*/ */
public synchronized FactorWord makeWord(Cons info) public synchronized FactorWord makeWord(Cons info)
{ {
FactorWord w = new FactorWord( String vocabulary = (String)info.car;
(String)info.car, String name = (String)info.next().car;
(String)info.next().car); FactorWord w = super.searchVocabulary(new Cons(vocabulary,null),name);
if(w == null)
w = new FactorWord(vocabulary,name);
w.stackEffect = (String)info.next().next().car; w.stackEffect = (String)info.next().next().car;
return w; return w;
} //}}} } //}}}

View File

@ -55,23 +55,23 @@ USE: prettyprint
#! Collect all literals from all branches. #! Collect all literals from all branches.
[ node-param get ] bind [ [ scan-literal ] each ] each ; [ node-param get ] bind [ [ scan-literal ] each ] each ;
: mentions-literal? ( literal list -- ) : mentions-literal? ( literal list -- ? )
#! Does the given list of result objects refer to this #! Does the given list of result objects refer to this
#! literal? #! literal?
[ dup cons? [ car over = ] [ drop f ] ifte ] some? ; [ dupd value= ] some? nip ;
: consumes-literal? ( literal node -- ? ) : consumes-literal? ( literal node -- ? )
#! Does the dataflow node consume the literal? #! Does the dataflow node consume the literal?
[ [
node-consume-d get mentions-literal? swap dup node-consume-d get mentions-literal? swap
node-consume-r get mentions-literal? nip or dup node-consume-r get mentions-literal? nip or
] bind ; ] bind ;
: produces-literal? ( literal node -- ? ) : produces-literal? ( literal node -- ? )
#! Does the dataflow node produce the literal? #! Does the dataflow node produce the literal?
[ [
node-produce-d get mentions-literal? swap dup node-produce-d get mentions-literal? swap
node-produce-r get mentions-literal? nip or dup node-produce-r get mentions-literal? nip or
] bind ; ] bind ;
: (can-kill?) ( literal node -- ? ) : (can-kill?) ( literal node -- ? )
@ -187,16 +187,16 @@ USE: prettyprint
#swap [ 2drop t ] "can-kill" set-word-property #swap [ 2drop t ] "can-kill" set-word-property
#swap [ kill-node ] "kill-node" set-word-property #swap [ kill-node ] "kill-node" set-word-property
: kill-mask ( literals node -- mask ) : kill-mask ( killing inputs -- mask )
[ node-consume-d get ] bind [ [ over [ over value= ] some? >boolean nip ] map nip ;
dup cons? [ car over contains? ] [ drop f ] ifte
] map nip ;
: reduce-stack-op ( literals node map -- ) : reduce-stack-op ( literals node map -- )
#! If certain values passing through a stack op are being #! If certain values passing through a stack op are being
#! killed, the stack op can be reduced, in extreme cases #! killed, the stack op can be reduced, in extreme cases
#! to a no-op. #! to a no-op.
-rot [ kill-mask swap assoc ] keep -rot [
[ node-consume-d get ] bind kill-mask swap assoc
] keep
over [ [ node-op set ] extend , ] [ 2drop ] ifte ; over [ [ node-op set ] extend , ] [ 2drop ] ifte ;
#over [ 2drop t ] "can-kill" set-word-property #over [ 2drop t ] "can-kill" set-word-property

View File

@ -51,7 +51,7 @@ USE: hashtables
: unify-result ( obj obj -- obj ) : unify-result ( obj obj -- obj )
#! Replace values with unknown result if they differ, #! Replace values with unknown result if they differ,
#! otherwise retain them. #! otherwise retain them.
2dup = [ drop ] [ 2drop gensym ] ifte ; 2dup = [ drop ] [ 2drop <computed-value> ] ifte ;
: unify-stacks ( list -- stack ) : unify-stacks ( list -- stack )
#! Replace differing literals in stacks with unknown #! Replace differing literals in stacks with unknown
@ -85,13 +85,13 @@ USE: hashtables
"Unbalanced branches" throw "Unbalanced branches" throw
] ifte ; ] ifte ;
: infer-branch ( rstate quot save-effect -- namespace ) : infer-branch ( value save-effect -- namespace )
<namespace> [ <namespace> [
save-effect set save-effect set
swap recursive-state set dup value-recursion recursive-state set
copy-interpreter copy-interpreter
dataflow-graph off dataflow-graph off
infer-quot literal infer-quot
#values values-node #values values-node
] extend ; ] extend ;
@ -99,9 +99,9 @@ USE: hashtables
#! This is a hack. undefined-method has a stack effect that #! This is a hack. undefined-method has a stack effect that
#! probably does not match any other branch of the generic, #! probably does not match any other branch of the generic,
#! so we handle it specially. #! so we handle it specially.
\ undefined-method swap tree-contains? ; literal \ undefined-method swap tree-contains? ;
: recursive-branch ( rstate quot -- ) : recursive-branch ( value -- )
#! Set base case if inference didn't fail. #! Set base case if inference didn't fail.
[ [
f infer-branch [ f infer-branch [
@ -109,13 +109,13 @@ USE: hashtables
recursive-state get set-base recursive-state get set-base
] bind ] bind
] [ ] [
[ 2drop ] when [ drop ] when
] catch ; ] catch ;
: infer-base-case ( branchlist -- ) : infer-base-case ( branchlist -- )
[ [
unswons dup terminator? [ dup terminator? [
2drop drop
] [ ] [
recursive-branch recursive-branch
] ifte ] ifte
@ -123,7 +123,7 @@ USE: hashtables
: (infer-branches) ( branchlist -- list ) : (infer-branches) ( branchlist -- list )
dup infer-base-case [ dup infer-base-case [
unswons dup terminator? [ dup terminator? [
t infer-branch [ t infer-branch [
meta-d off meta-r off d-in off meta-d off meta-r off d-in off
] extend ] extend
@ -153,8 +153,9 @@ USE: hashtables
\ ifte [ infer-ifte ] "infer" set-word-property \ ifte [ infer-ifte ] "infer" set-word-property
: vtable>list ( [ vtable | rstate ] -- list ) : vtable>list ( value -- list )
unswons vector>list [ over cons ] map nip ; dup value-recursion swap literal vector>list
[ over <literal-value> ] map nip ;
: infer-dispatch ( -- ) : infer-dispatch ( -- )
#! Infer effects for all branches, unify. #! Infer effects for all branches, unify.

View File

@ -36,6 +36,7 @@ USE: strings
USE: vectors USE: vectors
USE: words USE: words
USE: hashtables USE: hashtables
USE: generic
! Word properties that affect inference: ! Word properties that affect inference:
! - infer-effect -- must be set. controls number of inputs ! - infer-effect -- must be set. controls number of inputs
@ -62,12 +63,42 @@ SYMBOL: recursive-label
! inferred. ! inferred.
SYMBOL: save-effect SYMBOL: save-effect
: gensym-vector ( n -- vector ) ! A value has the following slots:
dup <vector> swap [ gensym over vector-push ] times ;
! the literal object, if any.
SYMBOL: value
! value-type -- the type, if known.
SYMBOL: value-type
GENERIC: literal ( value -- obj )
GENERIC: value= ( literal value -- ? )
TRAITS: computed-value
C: computed-value ( -- value )
[ gensym value set ] extend ;
M: computed-value literal ( value -- obj )
"Cannot use a computed value literally." throw ;
M: computed-value value= ( literal value -- ? )
2drop f ;
TRAITS: literal-value
C: literal-value ( obj rstate -- value )
[ recursive-state set value set ] extend ;
M: literal-value literal ( value -- obj )
[ value get ] bind ;
M: literal-value value= ( literal value -- ? )
literal = ;
: value-recursion ( value -- rstate )
[ recursive-state get ] bind ;
: computed-value-vector ( n -- vector )
[ drop <computed-value> ] vector-project ;
: add-inputs ( count stack -- stack ) : add-inputs ( count stack -- stack )
#! Add this many inputs to the given stack. #! Add this many inputs to the given stack.
>r gensym-vector dup r> vector-append ; >r computed-value-vector dup r> vector-append ;
: ensure ( count stack -- count stack ) : ensure ( count stack -- count stack )
#! Ensure stack has this many elements. Return number of #! Ensure stack has this many elements. Return number of
@ -88,7 +119,7 @@ SYMBOL: save-effect
: produce-d ( count -- ) : produce-d ( count -- )
#! Push count of unknown results. #! Push count of unknown results.
[ gensym push-d ] times ; [ <computed-value> push-d ] times ;
: effect ( -- [ in | out ] ) : effect ( -- [ in | out ] )
#! After inference is finished, collect information. #! After inference is finished, collect information.
@ -111,7 +142,7 @@ DEFER: apply-word
: apply-literal ( obj -- ) : apply-literal ( obj -- )
#! Literals are annotated with the current recursive #! Literals are annotated with the current recursive
#! state. #! state.
dup recursive-state get cons push-d dup recursive-state get <literal-value> push-d
#push dataflow, [ 1 0 node-outputs ] bind ; #push dataflow, [ 1 0 node-outputs ] bind ;
: apply-object ( obj -- ) : apply-object ( obj -- )

View File

@ -178,11 +178,13 @@ USE: prettyprint
] ifte ] ifte
] ifte ; ] ifte ;
: infer-call ( [ rstate | quot ] -- ) : infer-call ( -- )
1 ensure-d 1 ensure-d
dataflow-drop, dataflow-drop,
gensym dup [ gensym dup [
drop pop-d uncons recursive-state set infer-quot drop pop-d dup
value-recursion recursive-state set
literal infer-quot
] with-block ; ] with-block ;
\ call [ infer-call ] "infer" set-word-property \ call [ infer-call ] "infer" set-word-property

View File

@ -5,6 +5,7 @@ USE: inference
USE: words USE: words
USE: math USE: math
USE: kernel USE: kernel
USE: lists
: foo 1 2 3 ; : foo 1 2 3 ;
@ -15,3 +16,5 @@ USE: kernel
[ [ [ 1 ] [ 2 ] ] ] [ [ [ 1 ] [ 2 ] ifte ] dataflow kill-set ] unit-test [ [ [ 1 ] [ 2 ] ] ] [ [ [ 1 ] [ 2 ] ifte ] dataflow kill-set ] unit-test
[ [ [ 1 ] [ 2 ] ] ] [ [ t [ 1 ] [ 2 ] ifte ] dataflow kill-set ] unit-test [ [ [ 1 ] [ 2 ] ] ] [ [ t [ 1 ] [ 2 ] ifte ] dataflow kill-set ] unit-test
[ [ t t f ] ] [ [ 1 2 ] [ 1 2 3 ] [ f <literal-value> ] map kill-mask ] unit-test

View File

@ -25,7 +25,7 @@ USE: math-internals
! dup [ 7 | 7 ] decompose compose [ 7 | 7 ] = ! dup [ 7 | 7 ] decompose compose [ 7 | 7 ] =
! ] all? ! ] all?
! ] unit-test ! ] unit-test
[ 6 ] [ 6 gensym-vector vector-length ] unit-test [ 6 ] [ 6 computed-value-vector vector-length ] unit-test
[ 3 ] [ [ { 1 2 } { 1 2 3 } ] longest-vector ] unit-test [ 3 ] [ [ { 1 2 } { 1 2 3 } ] longest-vector ] unit-test

View File

@ -6,9 +6,6 @@ USE: namespaces
USE: strings USE: strings
USE: test USE: test
[ f ] [ "a" "b" "c" =? ] unit-test
[ "c" ] [ "a" "a" "c" =? ] unit-test
[ f ] [ "A string." f-or-"" ] unit-test [ f ] [ "A string." f-or-"" ] unit-test
[ t ] [ "" f-or-"" ] unit-test [ t ] [ "" f-or-"" ] unit-test
[ t ] [ f f-or-"" ] unit-test [ t ] [ f f-or-"" ] unit-test