factor/library/inference/branches.factor

102 lines
3.0 KiB
Factor
Raw Normal View History

2005-01-13 14:41:08 -05:00
! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
2004-11-26 22:23:57 -05:00
IN: inference
USING: arrays errors generic hashtables interpreter kernel math
namespaces parser prettyprint sequences strings vectors words ;
2004-12-31 02:17:45 -05:00
: unify-lengths ( seq -- seq )
2004-12-31 02:17:45 -05:00
#! Pad all vectors to the same length. If one vector is
#! shorter, pad it with unknown results at the bottom.
dup 0 [ length max ] reduce swap [ add-inputs ] map-with ;
2004-12-31 02:17:45 -05:00
2005-08-07 00:00:57 -04:00
: unify-length ( seq seq -- seq )
2array unify-lengths first2 ;
2005-08-07 00:00:57 -04:00
: unify-values ( seq -- value )
2004-12-31 02:17:45 -05:00
#! If all values in list are equal, return the value.
2005-07-27 01:46:06 -04:00
#! Otherwise, unify.
2005-09-24 15:21:17 -04:00
dup all-eq? [ first ] [ drop <value> ] if ;
2005-07-26 16:39:14 -04:00
: unify-stacks ( seq -- stack )
2004-11-26 22:23:57 -05:00
#! Replace differing literals in stacks with unknown
#! results.
2005-09-18 01:37:28 -04:00
[ ] subset dup empty?
2005-09-24 15:21:17 -04:00
[ drop f ] [ unify-lengths flip [ unify-values ] map ] if ;
2005-07-26 16:39:14 -04:00
: balanced? ( in out -- ? )
2005-09-24 15:21:17 -04:00
[ dup [ length - ] [ 2drop f ] if ] 2map
2005-09-18 01:37:28 -04:00
[ ] subset all-equal? ;
2004-12-07 23:21:32 -05:00
: unify-in-d ( seq -- n )
#! Input is a sequence of positive integers or f.
#! Output is the maximum or 0.
0 [ [ max ] when* ] reduce ;
: unbalanced-branches ( in out -- )
{ "Unbalanced branches:" } -rot [
swap number>string " " rot length number>string
append3
] 2map append "\n" join inference-error ;
2005-07-26 16:39:14 -04:00
: unify-effect ( in out -- in out )
2005-09-18 01:37:28 -04:00
#! In is a sequence of integers; out is a sequence of stacks.
2dup balanced? [
unify-stacks >r unify-in-d r>
] [
unbalanced-branches
2005-09-24 15:21:17 -04:00
] if ;
2004-12-07 23:21:32 -05:00
2005-07-26 16:39:14 -04:00
: datastack-effect ( seq -- )
dup [ d-in swap hash ] map
swap [ meta-d swap hash ] map
unify-effect
meta-d set d-in set ;
2005-07-26 16:39:14 -04:00
: callstack-effect ( seq -- )
2005-09-18 01:37:28 -04:00
dup length 0 <repeated>
2005-07-26 16:39:14 -04:00
swap [ meta-r swap hash ] map
unify-effect
meta-r set drop ;
2005-07-26 16:39:14 -04:00
: filter-terminators ( seq -- seq )
#! Remove branches that unconditionally throw errors.
[ [ active? ] bind ] subset ;
2004-12-07 23:21:32 -05:00
2005-07-26 16:39:14 -04:00
: unify-effects ( seq -- )
2005-09-18 01:37:28 -04:00
dup datastack-effect callstack-effect ;
2004-11-26 22:23:57 -05:00
2005-05-17 16:13:08 -04:00
: unify-dataflow ( effects -- nodes )
[ [ dataflow-graph get ] bind ] map ;
2004-12-26 17:04:08 -05:00
: copy-inference ( -- )
2005-07-27 01:46:06 -04:00
meta-r [ clone ] change
meta-d [ clone ] change
2005-09-18 01:37:28 -04:00
d-in [ ] change
2005-05-17 16:13:08 -04:00
dataflow-graph off
current-node off ;
2004-12-23 18:26:04 -05:00
2004-12-27 15:27:18 -05:00
: infer-branch ( value -- namespace )
#! Return a namespace with inferencer variables:
#! meta-d, meta-r, d-in. They are set to f if
#! terminate was called.
[
[
base-case-continuation set
copy-inference
dup value-recursion recursive-state set
dup literal-value infer-quot
active? [ #values node, ] when
f
2005-09-18 01:37:28 -04:00
] callcc1 [ terminate ] when drop
] make-hash ;
2004-12-07 23:21:32 -05:00
: (infer-branches) ( branchlist -- list )
2005-09-17 22:25:18 -04:00
[ infer-branch ] map dup unify-effects unify-dataflow ;
2004-11-29 23:14:12 -05:00
2005-05-17 16:13:08 -04:00
: infer-branches ( branches node -- )
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
#! base case to this stack effect and try again.
2005-07-27 01:46:06 -04:00
[ >r (infer-branches) r> set-node-children ] keep
node, #merge node, ;