1 USING: arrays generic inference inference.backend
2 inference.dataflow kernel classes kernel.private math
3 math.parser math.private namespaces namespaces.private parser
4 sequences strings vectors words quotations effects tools.test
5 continuations generic.standard sorting assocs definitions
6 prettyprint io inspector classes.tuple classes.union
7 classes.predicate debugger threads.private io.streams.string
8 io.timeouts io.thread sequences.private destructors ;
11 [ ] [ [ 1 ] dataflow [ ] transform-nodes drop ] unit-test
12 [ ] [ [ 1 2 3 ] dataflow [ ] transform-nodes drop ] unit-test
14 { 0 2 } [ 2 "Hello" ] must-infer-as
15 { 1 2 } [ dup ] must-infer-as
17 { 1 2 } [ [ dup ] call ] must-infer-as
18 [ [ call ] infer ] must-fail
20 { 2 4 } [ 2dup ] must-infer-as
22 { 1 0 } [ [ ] [ ] if ] must-infer-as
23 [ [ if ] infer ] must-fail
24 [ [ [ ] if ] infer ] must-fail
25 [ [ [ 2 ] [ ] if ] infer ] must-fail
26 { 4 3 } [ [ rot ] [ -rot ] if ] must-infer-as
30 [ swap 3 ] [ nip 5 5 ] if
36 { 1 1 } [ dup [ ] when ] must-infer-as
37 { 1 1 } [ dup [ dup fixnum* ] when ] must-infer-as
38 { 2 1 } [ [ dup fixnum* ] when ] must-infer-as
40 { 1 0 } [ [ drop ] when* ] must-infer-as
41 { 1 1 } [ [ { { [ ] } } ] unless* ] must-infer-as
44 [ [ 2 2 fixnum+ ] dup [ ] when call ] must-infer-as
47 [ [ [ 2 2 fixnum+ ] ] [ [ 2 2 fixnum* ] ] if call ] infer
50 ! Test inference of termination of control flow
54 : termination-test-2 [ termination-test-1 ] [ 3 ] if ;
56 { 1 1 } [ termination-test-2 ] must-infer-as
58 : infinite-loop infinite-loop ;
60 [ [ infinite-loop ] infer ] must-fail
62 : no-base-case-1 dup [ no-base-case-1 ] [ no-base-case-1 ] if ;
63 [ [ no-base-case-1 ] infer ] must-fail
65 : simple-recursion-1 ( obj -- obj )
66 dup [ simple-recursion-1 ] [ ] if ;
68 { 1 1 } [ simple-recursion-1 ] must-infer-as
70 : simple-recursion-2 ( obj -- obj )
71 dup [ ] [ simple-recursion-2 ] if ;
73 { 1 1 } [ simple-recursion-2 ] must-infer-as
75 : bad-recursion-2 ( obj -- obj )
76 dup [ dup first swap second bad-recursion-2 ] [ ] if ;
78 [ [ bad-recursion-2 ] infer ] must-fail
80 : funny-recursion ( obj -- obj )
81 dup [ funny-recursion 1 ] [ 2 ] if drop ;
83 { 1 1 } [ funny-recursion ] must-infer-as
86 { 1 2 } [ [ first ] keep second ] must-infer-as
91 : fie ( element obj -- ? )
92 dup array? [ foe ] [ eq? ] if ;
94 : foe ( element tree -- ? )
109 { 2 1 } [ fie ] must-infer-as
110 { 2 1 } [ foe ] must-infer-as
119 { 0 0 } [ nested-when ] must-infer-as
121 : nested-when* ( obj -- )
128 { 1 0 } [ nested-when* ] must-infer-as
132 { 0 1 } [ sym-test ] must-infer-as
141 { 1 1 } [ terminator-branch ] must-infer-as
143 : recursive-terminator ( obj -- )
150 { 1 0 } [ recursive-terminator ] must-infer-as
152 GENERIC: potential-hang ( obj -- obj )
153 M: fixnum potential-hang dup [ potential-hang ] when ;
155 [ ] [ [ 5 potential-hang ] infer drop ] unit-test
157 TUPLE: funny-cons car cdr ;
158 GENERIC: iterate ( obj -- )
159 M: funny-cons iterate funny-cons-cdr iterate ;
161 M: real iterate drop ;
163 { 1 0 } [ iterate ] must-infer-as
166 : cat ( obj -- * ) dup [ throw ] [ throw ] if ;
167 : dog ( a b c -- ) dup [ cat ] [ 3drop ] if ;
168 { 3 0 } [ dog ] must-infer-as
172 : friend ( a b c -- ) dup [ friend ] [ monkey ] if ;
173 : monkey ( a b c -- ) dup [ 3drop ] [ friend ] if ;
174 { 3 0 } [ friend ] must-infer-as
176 ! Regression -- same as above but we infer the second word first
178 : blah ( a b c -- ) dup [ blah ] [ blah2 ] if ;
179 : blah2 ( a b c -- ) dup [ blah ] [ 3drop ] if ;
180 { 3 0 } [ blah2 ] must-infer-as
185 dup [ blah3 ] [ dup [ blah4 ] [ blah3 ] if ] if ;
187 dup [ blah4 ] [ dup [ 3drop ] [ blah3 ] if ] if ;
188 { 3 0 } [ blah4 ] must-infer-as
191 : bad-combinator ( obj quot -- )
195 [ swap slip ] keep swap bad-combinator
198 [ [ [ 1 ] [ ] bad-combinator ] infer ] must-fail
202 dup string? [ 2array throw ] unless
203 over string? [ 2array throw ] unless ;
205 { 2 2 } [ bad-input# ] must-infer-as
209 ! This order of branches works
211 : more-crap ( obj -- ) dup [ drop ] [ dup do-crap call ] if ;
212 : do-crap ( obj -- ) dup [ more-crap ] [ do-crap ] if ;
213 [ [ do-crap ] infer ] must-fail
217 : more-crap* ( obj -- ) dup [ drop ] [ dup do-crap* call ] if ;
218 : do-crap* ( obj -- ) dup [ do-crap* ] [ more-crap* ] if ;
219 [ [ do-crap* ] infer ] must-fail
222 : too-deep ( a b -- c )
223 dup [ drop ] [ 2dup too-deep too-deep * ] if ; inline
224 { 2 1 } [ too-deep ] must-infer-as
226 ! Error reporting is wrong
228 M: fixnum xyz 2array ;
230 [ 3 ] bi@ swapd >r 2array swap r> 2array swap ;
232 [ [ xyz ] infer ] [ inference-error? ] must-fail-with
234 ! Doug Coleman discovered this one while working on the
264 { 1 0 } [ A ] must-infer-as
265 { 1 0 } [ B ] must-infer-as
266 { 1 0 } [ C ] must-infer-as
268 ! I found this bug by thinking hard about the previous one
270 : X ( a b -- c d ) dup [ swap Y ] [ ] if ;
271 : Y ( a b -- c d ) X ;
273 { 2 2 } [ X ] must-infer-as
274 { 2 2 } [ Y ] must-infer-as
276 ! This one comes from UI code
278 : #2 ( a b -- ) dup [ call ] [ 2drop ] if ; inline
279 : #3 ( a -- ) [ #1 ] #2 ;
280 : #4 ( a -- ) dup [ drop ] [ dup #4 dup #3 call ] if ;
281 : #1 ( a -- ) dup [ dup #4 dup #3 ] [ ] if drop ;
283 [ \ #4 word-def infer ] must-fail
284 [ [ #1 ] infer ] must-fail
288 : foo ( a b -- c d ) dup [ 2drop f f bar ] [ ] if ;
289 : bar ( a b -- ) [ 2 2 + ] t foo drop call drop ;
291 [ [ foo ] infer ] must-fail
293 [ 1234 infer ] must-fail
296 [ [ [ dup call ] dup call ] infer ]
297 [ inference-error? ] must-fail-with
299 : m dup call ; inline
301 [ [ [ m ] m ] infer ] [ inference-error? ] must-fail-with
303 : m' dup curry call ; inline
305 [ [ [ m' ] m' ] infer ] [ inference-error? ] must-fail-with
307 : m'' [ dup curry ] ; inline
309 : m''' m'' call call ; inline
311 [ [ [ m''' ] m''' ] infer ] [ inference-error? ] must-fail-with
313 : m-if t over if ; inline
315 [ [ [ m-if ] m-if ] infer ] [ inference-error? ] must-fail-with
317 ! This doesn't hang but it's also an example of the
319 [ [ [ [ drop 3 ] swap call ] dup call ] infer ]
320 [ inference-error? ] must-fail-with
322 ! This form should not have a stack effect
324 : bad-recursion-1 ( a -- b )
325 dup [ drop bad-recursion-1 5 ] [ ] if ;
327 [ [ bad-recursion-1 ] infer ] must-fail
329 : bad-bin ( a b -- ) 5 [ 5 bad-bin bad-bin 5 ] [ 2drop ] if ;
330 [ [ bad-bin ] infer ] must-fail
332 [ [ r> ] infer ] [ inference-error? ] must-fail-with
335 [ [ get-slots ] infer ] [ inference-error? ] must-fail-with
337 ! Test some curry stuff
338 { 1 1 } [ 3 [ ] curry 4 [ ] curry if ] must-infer-as
340 { 2 1 } [ [ ] curry 4 [ ] curry if ] must-infer-as
342 [ [ 3 [ ] curry 1 2 [ ] 2curry if ] infer ] must-fail
344 ! Test number protocol
362 ! Test object protocol
365 \ hashcode* must-infer
367 ! Test sequence protocol
370 \ set-length must-infer
373 \ new-resizable must-infer
375 \ lengthen must-infer
377 ! Test assoc protocol
380 \ new-assoc must-infer
381 \ delete-at must-infer
382 \ clear-assoc must-infer
383 \ assoc-size must-infer
384 \ assoc-like must-infer
385 \ assoc-clone-like must-infer
387 { 1 3 } [ [ 2drop f ] assoc-find ] must-infer-as
389 ! Test some random library words
390 \ 1quotation must-infer
391 \ string>number must-infer
401 \ natural-sort must-infer
404 \ define-class must-infer
405 \ define-tuple-class must-infer
406 \ define-union-class must-infer
407 \ define-predicate-class must-infer
409 ! Test words with continuations
410 { 0 0 } [ [ drop ] callcc0 ] must-infer-as
411 { 0 1 } [ [ 4 swap continue-with ] callcc1 ] must-infer-as
412 { 2 1 } [ [ + ] [ ] [ ] cleanup ] must-infer-as
413 { 2 1 } [ [ + ] [ 3drop 0 ] recover ] must-infer-as
417 ! Test stream protocol
418 \ set-timeout must-infer
419 \ stream-read must-infer
420 \ stream-read1 must-infer
421 \ stream-readln must-infer
422 \ stream-read-until must-infer
423 \ stream-write must-infer
424 \ stream-write1 must-infer
425 \ stream-nl must-infer
426 \ stream-format must-infer
427 \ stream-write-table must-infer
428 \ stream-flush must-infer
429 \ make-span-stream must-infer
430 \ make-block-stream must-infer
431 \ make-cell-stream must-infer
433 ! Test stream utilities
435 \ contents must-infer
437 ! Test prettyprinting
442 \ describe must-infer
446 \ io-thread must-infer
448 ! Incorrect stack declarations on inline recursive words should
450 : fooxxx ( a b -- c ) over [ foo ] when ; inline
453 [ [ barxxx ] infer ] must-fail
456 { 1 0 } [ { [ ] } dispatch ] must-infer-as
458 DEFER: inline-recursive-2
459 : inline-recursive-1 ( -- ) inline-recursive-2 ;
460 : inline-recursive-2 ( -- ) inline-recursive-1 ;
462 { 0 0 } [ inline-recursive-1 ] must-infer-as
466 HOOK: my-hook my-var ( -- x )
468 M: integer my-hook "an integer" ;
469 M: string my-hook "a string" ;
471 { 0 1 } [ my-hook ] must-infer-as
475 : calls-deferred-word [ deferred-word ] [ 3 ] if ;
477 { 1 1 } [ calls-deferred-word ] must-infer-as
479 USE: inference.dataflow
481 { 1 0 } [ [ iterate-next ] iterate-nodes ] must-infer-as
485 [ [ iterate-next ] iterate-nodes ] with-node-iterator
488 : nilpotent ( quot -- )
489 t [ [ call ] keep nilpotent ] [ drop ] if ; inline
491 : semisimple ( quot -- )
492 [ call ] keep [ [ semisimple ] keep ] nilpotent drop ; inline
495 [ [ ] [ call ] keep [ [ call ] keep ] nilpotent ]
498 { 0 0 } [ [ ] semisimple ] must-infer-as
500 { 1 0 } [ [ drop ] each-node ] must-infer-as
502 DEFER: an-inline-word
504 : normal-word-3 ( -- )
505 3 [ [ 2 + ] curry ] an-inline-word call drop ;
507 : normal-word-2 ( -- )
510 : normal-word ( x -- x )
511 dup [ normal-word-2 ] when ;
513 : an-inline-word ( obj quot -- )
514 >r normal-word r> call ; inline
516 { 1 1 } [ [ 3 * ] an-inline-word ] must-infer-as
518 { 0 1 } [ [ 2 ] [ 2 ] [ + ] compose compose call ] must-infer-as
520 ERROR: custom-error ;
522 [ T{ effect f 0 0 t } ] [
523 [ custom-error ] infer
526 : funny-throw throw ; inline
528 [ T{ effect f 0 0 t } ] [
529 [ 3 funny-throw ] infer
532 [ T{ effect f 0 0 t } ] [
533 [ custom-error inference-error ] infer
536 [ T{ effect f 1 1 t } ] [
537 [ dup >r 3 throw r> ] infer
540 ! This was a false trigger of the undecidable quotation
542 { 2 1 } [ find-last-sep ] must-infer-as
545 : missing->r-check >r ;
547 [ [ missing->r-check ] infer ] must-fail
549 { 1 0 } [ [ ] map-children ] must-infer-as
552 [ [ [ f dup ] [ dup ] [ ] unfold ] infer ] must-fail
554 [ [ [ f dup ] [ ] [ ] while ] infer ] must-fail
556 : erg's-inference-bug ( -- ) f dup [ erg's-inference-bug ] when ; inline
558 [ [ erg's-inference-bug ] infer ] must-fail
560 : inference-invalidation-a ;
561 : inference-invalidation-b [ inference-invalidation-a ] dip call ; inline
562 : inference-invalidation-c [ + ] inference-invalidation-b ;
564 [ 7 ] [ 4 3 inference-invalidation-c ] unit-test
566 { 2 1 } [ inference-invalidation-c ] must-infer-as
568 [ ] [ "IN: inference.tests : inference-invalidation-a 1 2 ;" eval ] unit-test
570 [ 3 ] [ inference-invalidation-c ] unit-test
572 { 0 1 } [ inference-invalidation-c ] must-infer-as
574 GENERIC: inference-invalidation-d ( obj -- )
576 M: object inference-invalidation-d inference-invalidation-c 2drop ;
578 \ inference-invalidation-d must-infer
580 [ ] [ "IN: inference.tests : inference-invalidation-a ;" eval ] unit-test
582 [ [ inference-invalidation-d ] infer ] must-fail