2006-07-27 18:21:49 -04:00
|
|
|
! Copyright (C) 2004, 2006 Slava Pestov.
|
|
|
|
! See http://factorcode.org/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.
|
2006-08-15 04:57:12 -04:00
|
|
|
dup [ length ] map supremum
|
2006-07-27 18:21:49 -04:00
|
|
|
swap [ add-inputs nip ] 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
|
|
|
|
2006-08-10 16:03:51 -04:00
|
|
|
: unify-stacks ( seq -- stack ) flip [ unify-values ] map ;
|
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
|
|
|
: unbalanced-branches ( in out -- )
|
2006-07-27 18:21:49 -04:00
|
|
|
[ swap unparse " " rot length unparse append3 ] 2map
|
|
|
|
"Unbalanced branches:" add* "\n" join inference-error ;
|
2005-09-20 01:59:38 -04:00
|
|
|
|
2006-08-15 04:57:12 -04:00
|
|
|
: unify-inputs ( max-d-in d-in meta-d -- meta-d )
|
2006-08-10 16:03:51 -04:00
|
|
|
dup [
|
|
|
|
[ >r - r> length + ] keep add-inputs nip
|
|
|
|
] [
|
|
|
|
2nip
|
|
|
|
] if ;
|
|
|
|
|
2005-07-26 16:39:14 -04:00
|
|
|
: unify-effect ( in out -- in out )
|
2006-07-28 00:50:09 -04:00
|
|
|
#! in is a sequence of integers, out is a sequence of
|
|
|
|
#! stacks.
|
2005-09-20 01:59:38 -04:00
|
|
|
2dup balanced? [
|
2006-08-10 16:03:51 -04:00
|
|
|
over supremum -rot
|
|
|
|
[ >r dupd r> unify-inputs ] 2map
|
|
|
|
[ ] subset unify-stacks
|
2005-09-20 01:59:38 -04:00
|
|
|
] [
|
|
|
|
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 -- )
|
2006-07-27 18:21:49 -04:00
|
|
|
d-in over [ hash ] map-with
|
2005-09-28 23:29:00 -04:00
|
|
|
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 )
|
2006-07-27 18:21:49 -04:00
|
|
|
[ dataflow-graph swap hash ] map ;
|
2005-05-17 16:13:08 -04:00
|
|
|
|
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-08-22 02:06:32 -04:00
|
|
|
[
|
2006-08-15 04:57:12 -04:00
|
|
|
copy-inference
|
|
|
|
dup value-recursion recursive-state set
|
|
|
|
value-literal infer-quot
|
|
|
|
terminated? get [ #values node, ] unless
|
|
|
|
] make-hash ;
|
2004-12-07 23:21:32 -05:00
|
|
|
|
2004-12-08 18:39:36 -05:00
|
|
|
: (infer-branches) ( branchlist -- list )
|
2006-08-15 04:57:12 -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, ;
|