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
|
2006-09-05 01:29:26 -04:00
|
|
|
USING: arrays errors generic io kernel
|
2006-05-15 01:01:47 -04:00
|
|
|
math namespaces parser prettyprint sequences strings
|
2005-09-25 21:54:25 -04:00
|
|
|
vectors words ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2006-09-23 02:40:25 -04:00
|
|
|
TUPLE: inference-error rstate major? ;
|
2006-08-18 04:00:51 -04:00
|
|
|
|
2006-09-23 02:40:25 -04:00
|
|
|
C: inference-error ( msg rstate important? -- error )
|
|
|
|
[ set-inference-error-major? ] keep
|
2006-08-18 04:00:51 -04:00
|
|
|
[ set-inference-error-rstate ] keep
|
|
|
|
[ set-delegate ] keep ;
|
2005-08-04 23:59:45 -04:00
|
|
|
|
2006-08-15 03:01:24 -04:00
|
|
|
: inference-error ( msg -- * )
|
2006-09-23 02:40:25 -04:00
|
|
|
recursive-state get t <inference-error> throw ;
|
|
|
|
|
|
|
|
: inference-warning ( msg -- * )
|
|
|
|
recursive-state get f <inference-error> throw ;
|
2005-08-04 23:59:45 -04:00
|
|
|
|
2006-08-18 03:10:08 -04:00
|
|
|
TUPLE: literal-expected ;
|
2005-08-06 01:59:49 -04:00
|
|
|
|
2006-08-18 03:10:08 -04:00
|
|
|
M: object value-literal
|
2006-09-23 02:40:25 -04:00
|
|
|
<literal-expected> inference-warning ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2005-05-14 00:23:00 -04:00
|
|
|
: pop-literal ( -- rstate obj )
|
2006-01-22 16:40:18 -05:00
|
|
|
1 #drop node,
|
|
|
|
pop-d dup value-recursion swap value-literal ;
|
2005-03-29 19:11:10 -05:00
|
|
|
|
2006-01-22 16:40:18 -05:00
|
|
|
: value-vector ( n -- vector ) [ drop <computed> ] map >vector ;
|
2004-12-23 16:37:16 -05:00
|
|
|
|
2006-07-27 18:21:49 -04:00
|
|
|
: add-inputs ( n stack -- n stack )
|
|
|
|
tuck length - dup 0 >
|
|
|
|
[ dup value-vector [ rot nappend ] keep ]
|
|
|
|
[ drop 0 swap ] if ;
|
|
|
|
|
2005-09-14 00:37:50 -04:00
|
|
|
: ensure-values ( n -- )
|
2006-07-27 18:21:49 -04:00
|
|
|
meta-d [ add-inputs ] change d-in [ + ] change ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2006-08-18 02:05:13 -04:00
|
|
|
: short-effect ( -- pair )
|
2005-09-18 01:37:28 -04:00
|
|
|
d-in get meta-d get length 2array ;
|
2005-08-30 18:12:21 -04:00
|
|
|
|
2005-09-28 20:09:10 -04:00
|
|
|
SYMBOL: terminated?
|
|
|
|
|
2006-08-15 03:01:24 -04:00
|
|
|
: current-effect ( -- effect )
|
|
|
|
d-in get meta-d get length <effect>
|
|
|
|
terminated? get over set-effect-terminated? ;
|
|
|
|
|
|
|
|
SYMBOL: recorded
|
|
|
|
|
2004-11-26 22:23:57 -05:00
|
|
|
: init-inference ( recursive-state -- )
|
2005-09-28 20:09:10 -04:00
|
|
|
terminated? off
|
2006-05-15 18:00:37 -04:00
|
|
|
V{ } clone meta-r set
|
2005-10-29 23:25:38 -04:00
|
|
|
V{ } clone meta-d set
|
2005-09-18 01:37:28 -04:00
|
|
|
0 d-in set
|
2004-11-26 22:23:57 -05:00
|
|
|
recursive-state set
|
2004-12-30 02:40:14 -05:00
|
|
|
dataflow-graph off
|
2005-05-20 23:52:31 -04:00
|
|
|
current-node off ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2005-02-24 20:52:17 -05:00
|
|
|
GENERIC: apply-object
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2004-11-27 00:33:17 -05:00
|
|
|
: apply-literal ( obj -- )
|
2006-09-14 16:14:27 -04:00
|
|
|
#push dup node,
|
|
|
|
swap <value> push-d
|
|
|
|
1 d-tail swap set-node-out-d ;
|
2004-11-27 00:33:17 -05:00
|
|
|
|
2005-02-24 20:52:17 -05:00
|
|
|
M: object apply-object apply-literal ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2005-08-03 23:56:28 -04:00
|
|
|
M: wrapper apply-object wrapped apply-literal ;
|
|
|
|
|
2005-02-08 22:02:44 -05:00
|
|
|
: terminate ( -- )
|
2005-09-28 23:29:00 -04:00
|
|
|
terminated? on #terminate node, ;
|
2005-02-08 22:02:44 -05:00
|
|
|
|
2006-08-15 04:57:12 -04:00
|
|
|
GENERIC: infer-quot ( quot -- )
|
2005-10-13 00:30:44 -04:00
|
|
|
|
2006-08-15 03:01:24 -04:00
|
|
|
M: f infer-quot drop ;
|
2006-05-27 17:39:38 -04:00
|
|
|
|
2006-08-15 03:01:24 -04:00
|
|
|
M: quotation infer-quot
|
2006-07-28 00:50:09 -04:00
|
|
|
[ apply-object terminated? get not ] all? drop ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2005-05-16 01:15:48 -04:00
|
|
|
: infer-quot-value ( rstate quot -- )
|
2005-08-30 18:12:21 -04:00
|
|
|
recursive-state get >r swap recursive-state set
|
|
|
|
infer-quot r> recursive-state set ;
|
2005-05-16 01:15:48 -04:00
|
|
|
|
2006-09-23 15:54:37 -04:00
|
|
|
TUPLE: too-many->r ;
|
2006-08-18 03:10:08 -04:00
|
|
|
|
2006-09-23 15:54:37 -04:00
|
|
|
: check->r ( -- )
|
2005-05-15 21:17:56 -04:00
|
|
|
meta-r get empty? [
|
2006-09-23 15:54:37 -04:00
|
|
|
<too-many->r> inference-error
|
2004-12-04 23:45:41 -05:00
|
|
|
] unless ;
|
|
|
|
|
2006-09-23 15:54:37 -04:00
|
|
|
TUPLE: too-many-r> ;
|
|
|
|
|
|
|
|
: check-r> ( -- )
|
|
|
|
meta-r get empty? [
|
|
|
|
<too-many-r>> inference-error
|
|
|
|
] when ;
|
|
|
|
|
2006-08-18 01:50:34 -04:00
|
|
|
: undo-infer ( -- )
|
|
|
|
recorded get
|
|
|
|
[ "infer" word-prop not ] subset
|
|
|
|
[ f "infer-effect" set-word-prop ] each ;
|
|
|
|
|
2005-05-15 21:17:56 -04:00
|
|
|
: with-infer ( quot -- )
|
|
|
|
[
|
2006-08-15 03:01:24 -04:00
|
|
|
[
|
|
|
|
{ } recursive-state set
|
|
|
|
V{ } clone recorded set
|
|
|
|
f init-inference
|
|
|
|
call
|
2006-09-23 15:54:37 -04:00
|
|
|
check->r
|
2006-08-15 03:01:24 -04:00
|
|
|
] [
|
2006-08-18 01:50:34 -04:00
|
|
|
undo-infer
|
2006-08-15 03:01:24 -04:00
|
|
|
rethrow
|
|
|
|
] recover
|
2005-05-15 21:17:56 -04:00
|
|
|
] with-scope ;
|
2004-12-02 22:44:36 -05:00
|
|
|
|
2005-05-17 16:13:08 -04:00
|
|
|
: infer ( quot -- effect )
|
2006-08-15 03:01:24 -04:00
|
|
|
[ infer-quot short-effect ] with-infer ;
|
2004-11-27 00:33:17 -05:00
|
|
|
|
2005-07-31 23:38:33 -04:00
|
|
|
: (dataflow) ( quot -- dataflow )
|
2005-09-07 17:21:11 -04:00
|
|
|
infer-quot f #return node, dataflow-graph get ;
|
2005-07-31 23:38:33 -04:00
|
|
|
|
2004-12-01 19:48:08 -05:00
|
|
|
: dataflow ( quot -- dataflow )
|
2005-07-31 23:38:33 -04:00
|
|
|
[ (dataflow) ] with-infer ;
|
|
|
|
|
|
|
|
: dataflow-with ( quot stack -- effect )
|
|
|
|
[ meta-d set (dataflow) ] with-infer ;
|