1 ! Copyright (C) 2010 Joe Groff
2 ! See https://factorcode.org/license.txt for BSD license
3 USING: accessors arrays assocs combinators
4 combinators.short-circuit effects fry kernel locals math
5 math.order namespaces sequences stack-checker.errors
6 stack-checker.state stack-checker.values ;
7 IN: stack-checker.row-polymorphism
9 : with-inner-d ( quot -- inner-d )
11 [ meta-d length inner-d-index set call ] dip
12 inner-d-index get [ min inner-d-index set ] keep ; inline
14 :: (effect-here) ( inner-d old-meta-d-length old-input-count -- effect )
15 old-meta-d-length inner-d - input-count get old-input-count - +
16 terminated? get [ [ 0 ] [ meta-d length inner-d - ] if [ "x" <array> ] bi@ ] keep
17 <terminated-effect> ; inline
19 : with-effect-here ( quot -- effect )
20 meta-d length input-count get
21 [ with-inner-d ] 2dip (effect-here) ; inline
23 : (diff-variable) ( diff variable vars -- diff' )
24 [ key? ] [ '[ _ _ at - ] ] [ '[ _ _ set-at 0 ] ] 2tri if ;
26 : (check-variable) ( actual-count declared-count variable vars -- diff ? )
27 [ - ] 2dip dupd '[ _ _ (diff-variable) t ] [ dup 0 <= ] if ;
29 : adjust-variable ( diff var vars -- )
30 pick 0 >= [ at+ ] [ 3drop ] if ; inline
32 :: check-variable ( vars declared actual slot var-slot -- diff ok? var )
33 actual declared [ slot call length ] bi@ declared var-slot call
34 [ vars (check-variable) ] keep ; inline
36 :: unify-variables ( in-diff in-ok? in-var out-diff out-ok? out-var vars -- ? )
37 { [ in-ok? ] [ out-ok? ] [ in-diff out-diff = ] } 0&& dup [
38 in-var [ in-diff swap vars adjust-variable ] when*
39 out-var [ out-diff swap vars adjust-variable ] when*
42 ! A bit of a hack. If the declared effect is one-sided monomorphic and the actual effect is a
43 ! shallow subtype of the root effect, adjust it here
44 :: (balance-actual-depth) ( declared actual -- depth/f )
48 [ declared out-var>> not ]
49 [ actual out>> length declared out>> length < ]
50 } 0&& ] [ declared out>> length actual out>> length - ] }
52 [ declared in-var>> not ]
53 [ declared out-var>> ]
54 [ actual in>> length declared in>> length < ]
55 } 0&& ] [ declared in>> length actual in>> length - ] }
59 : (balance-by) ( effect n -- effect' )
63 [ nip terminated?>> ] 2tri <terminated-effect> ;
65 : balance-actual ( declared actual -- declared actual' )
66 2dup (balance-actual-depth) [ (balance-by) ] when* ;
68 : (check-variables) ( vars declared actual -- ? )
70 [ [ in>> ] [ in-var>> ] check-variable ]
71 [ [ out>> ] [ out-var>> ] check-variable ]
72 [ 2drop ] 3tri unify-variables ;
74 : check-variables ( vars declared actual -- ? )
75 dup terminated?>> [ 3drop t ] [ (check-variables) ] if ;
77 : combinator-branches-effects ( branches -- quots declareds actuals )
78 [ [ known>callable ] { } map-as ]
79 [ [ effect>> ] { } map-as ]
80 [ [ actual>> ] { } map-as ] tri ;
82 : combinator-unbalanced-branches-error ( known -- * )
83 [ word>> ] [ branches>> <reversed> combinator-branches-effects ] bi
84 unbalanced-branches-error ;
86 : check-declared-effect ( known effect -- )
88 2dup [ [ variables>> ] [ effect>> ] bi ] dip check-variables
89 [ 2drop ] [ drop combinator-unbalanced-branches-error ] if ;