2008-07-20 05:24:37 -04:00
|
|
|
! Copyright (C) 2008 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
|
|
USING: fry vectors sequences assocs math accessors kernel
|
|
|
|
combinators quotations namespaces stack-checker.state
|
|
|
|
stack-checker.backend stack-checker.errors stack-checker.visitor
|
|
|
|
;
|
|
|
|
IN: stack-checker.branches
|
|
|
|
|
|
|
|
: balanced? ( seq -- ? )
|
2008-07-25 03:07:45 -04:00
|
|
|
[ second ] filter [ first2 length - ] map all-equal? ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
|
|
|
: phi-inputs ( seq -- newseq )
|
|
|
|
dup empty? [
|
|
|
|
dup [ length ] map supremum
|
2008-07-22 05:45:03 -04:00
|
|
|
'[ , f pad-left ] map flip
|
2008-07-20 05:24:37 -04:00
|
|
|
] unless ;
|
|
|
|
|
|
|
|
: unify-values ( values -- phi-out )
|
2008-07-25 03:07:45 -04:00
|
|
|
dup sift [ known ] map dup all-eq?
|
2008-07-20 05:24:37 -04:00
|
|
|
[ nip first make-known ] [ 2drop <value> ] if ;
|
|
|
|
|
|
|
|
: phi-outputs ( phi-in -- stack )
|
2008-07-22 05:45:03 -04:00
|
|
|
[ unify-values ] map ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
|
|
|
SYMBOL: quotations
|
|
|
|
|
|
|
|
: unify-branches ( ins stacks -- in phi-in phi-out )
|
2008-07-25 03:07:45 -04:00
|
|
|
zip dup empty? [ drop 0 { } { } ] [
|
2008-07-20 05:24:37 -04:00
|
|
|
dup balanced?
|
|
|
|
[ [ keys supremum ] [ values phi-inputs dup phi-outputs ] bi ]
|
|
|
|
[ quotations get unbalanced-branches-error ]
|
|
|
|
if
|
|
|
|
] if ;
|
|
|
|
|
|
|
|
: branch-variable ( seq symbol -- seq )
|
|
|
|
'[ , _ at ] map ;
|
|
|
|
|
|
|
|
: active-variable ( seq symbol -- seq )
|
|
|
|
[ [ terminated? over at [ drop f ] when ] map ] dip
|
|
|
|
branch-variable ;
|
|
|
|
|
|
|
|
: datastack-phi ( seq -- phi-in phi-out )
|
|
|
|
[ d-in branch-variable ] [ meta-d active-variable ] bi
|
|
|
|
unify-branches
|
|
|
|
[ d-in set ] [ ] [ dup >vector meta-d set ] tri* ;
|
|
|
|
|
|
|
|
: retainstack-phi ( seq -- phi-in phi-out )
|
|
|
|
[ length 0 <repetition> ] [ meta-r active-variable ] bi
|
|
|
|
unify-branches
|
2008-07-22 05:45:03 -04:00
|
|
|
[ drop ] [ ] [ dup >vector meta-r set ] tri* ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
|
|
|
: compute-phi-function ( seq -- )
|
|
|
|
[ quotation active-variable sift quotations set ]
|
|
|
|
[ [ datastack-phi ] [ retainstack-phi ] bi #phi, ]
|
|
|
|
[ [ terminated? swap at ] all? terminated? set ]
|
|
|
|
tri ;
|
|
|
|
|
|
|
|
: infer-branch ( literal -- namespace )
|
|
|
|
[
|
|
|
|
copy-inference
|
|
|
|
nest-visitor
|
|
|
|
[ value>> quotation set ] [ infer-literal-quot ] bi
|
|
|
|
] H{ } make-assoc ; inline
|
|
|
|
|
|
|
|
: infer-branches ( branches -- input children data )
|
|
|
|
[ pop-d ] dip
|
|
|
|
[ infer-branch ] map
|
2008-07-24 00:50:21 -04:00
|
|
|
[ stack-visitor branch-variable ] keep ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-23 01:17:08 -04:00
|
|
|
: (infer-if) ( branches -- )
|
2008-07-20 05:24:37 -04:00
|
|
|
infer-branches [ first2 #if, ] dip compute-phi-function ;
|
|
|
|
|
2008-07-23 01:17:08 -04:00
|
|
|
: infer-if ( -- )
|
|
|
|
2 consume-d
|
|
|
|
dup [ known [ curry? ] [ composed? ] bi or ] contains? [
|
|
|
|
output-d
|
|
|
|
[ rot [ drop call ] [ nip call ] if ]
|
|
|
|
recursive-state get infer-quot
|
|
|
|
] [
|
|
|
|
[ #drop, ] [ [ literal ] map (infer-if) ] bi
|
|
|
|
] if ;
|
|
|
|
|
|
|
|
: infer-dispatch ( -- )
|
|
|
|
pop-literal nip [ <literal> ] map
|
2008-07-20 05:24:37 -04:00
|
|
|
infer-branches [ #dispatch, ] dip compute-phi-function ;
|