2005-01-13 14:41:08 -05:00
|
|
|
! Copyright (C) 2004, 2005 Slava Pestov.
|
2005-01-30 15:57:25 -05:00
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2004-11-26 22:23:57 -05:00
|
|
|
IN: inference
|
2005-09-11 20:46:55 -04:00
|
|
|
USING: arrays errors generic hashtables interpreter kernel math
|
2005-08-21 20:50:14 -04:00
|
|
|
namespaces parser prettyprint sequences strings vectors words ;
|
2004-12-31 02:17:45 -05:00
|
|
|
|
2005-07-27 20:13:11 -04: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.
|
2005-09-20 01:59:38 -04:00
|
|
|
dup 0 [ length max ] reduce swap [ add-inputs ] map-with ;
|
2004-12-31 02:17:45 -05:00
|
|
|
|
2005-08-04 17:39:39 -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.
|
2006-01-22 16:40:18 -05:00
|
|
|
dup all-eq? [ first ] [ drop <computed> ] if ;
|
2004-12-23 16:37:16 -05:00
|
|
|
|
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 ;
|
2004-12-22 22:16:46 -05:00
|
|
|
|
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
|
|
|
|
2005-09-20 01:59:38 -04: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 -- )
|
2005-10-29 23:25:38 -04:00
|
|
|
{ "Unbalanced branches:" } -rot [
|
2006-04-17 17:17:34 -04:00
|
|
|
swap unparse " " rot length unparse append3
|
2005-09-20 01:59:38 -04:00
|
|
|
] 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.
|
2005-09-20 01:59:38 -04:00
|
|
|
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-09-28 23:29:00 -04:00
|
|
|
: active-variable ( seq symbol -- seq )
|
|
|
|
swap [
|
|
|
|
terminated? over hash [ 2drop f ] [ hash ] if
|
|
|
|
] map-with ;
|
|
|
|
|
2005-07-26 16:39:14 -04:00
|
|
|
: datastack-effect ( seq -- )
|
2005-09-28 23:29:00 -04:00
|
|
|
dup d-in active-variable
|
|
|
|
swap meta-d active-variable
|
|
|
|
unify-effect meta-d set d-in set ;
|
2004-12-26 01:42:09 -05:00
|
|
|
|
2005-07-26 16:39:14 -04:00
|
|
|
: callstack-effect ( seq -- )
|
2006-01-02 00:51:03 -05:00
|
|
|
dup length 0 <array>
|
2006-05-15 18:00:37 -04:00
|
|
|
swap meta-r active-variable
|
|
|
|
unify-effect meta-r set drop ;
|
2004-12-07 23:21:32 -05:00
|
|
|
|
2005-07-26 16:39:14 -04:00
|
|
|
: unify-effects ( seq -- )
|
2005-11-13 22:04:14 -05:00
|
|
|
dup datastack-effect dup callstack-effect
|
|
|
|
[ terminated? swap hash ] all? terminated? set ;
|
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 ( -- )
|
2006-05-15 18:00:37 -04:00
|
|
|
meta-r [ clone ] change
|
2005-07-27 01:46:06 -04:00
|
|
|
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 )
|
2005-02-08 22:02:44 -05:00
|
|
|
#! Return a namespace with inferencer variables:
|
2006-05-15 01:37:11 -04:00
|
|
|
#! meta-d, meta-c, d-in. They are set to f if
|
2005-02-08 22:02:44 -05:00
|
|
|
#! terminate was called.
|
2005-08-22 02:06:32 -04:00
|
|
|
[
|
2005-08-30 18:12:21 -04:00
|
|
|
[
|
|
|
|
base-case-continuation set
|
|
|
|
copy-inference
|
|
|
|
dup value-recursion recursive-state set
|
2006-01-22 16:40:18 -05:00
|
|
|
dup value-literal infer-quot
|
2005-09-28 20:09:10 -04:00
|
|
|
terminated? get [ #values node, ] unless
|
2005-08-30 18:12:21 -04:00
|
|
|
f
|
2005-09-18 01:37:28 -04:00
|
|
|
] callcc1 [ terminate ] when drop
|
2005-08-22 02:06:32 -04:00
|
|
|
] make-hash ;
|
2004-12-07 23:21:32 -05:00
|
|
|
|
2004-12-08 18:39:36 -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
|
2005-05-14 00:23:00 -04:00
|
|
|
#! 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
|
2005-08-06 02:44:25 -04:00
|
|
|
node, #merge node, ;
|