Strange bug in typed racket

25 views
Skip to first unread message

Antonio Menezes Leitao

unread,
Sep 30, 2015, 3:57:29 PM9/30/15
to Racket Dev
Hi,

I have a program written in typed/racket. Before I try to create a MWE, I would like to know if anyone recognizes the following behavior. Just for reference, (all-shapes) returns a list of line structs. The predicate line? confirms this for the first element of the list.

Welcome to DrRacket, version 6.2.900.17--2015-09-20(6dfc20d/a) [3m].
Language: typed/racket; memory limit: 4096 MB.
> (define shapes (all-shapes))
> (line? (car shapes))
- : Boolean
#t
> (if (line? (car shapes)) 1 2)
- : Integer [more precisely: Positive-Byte]
2

Obviously, I was expecting 1 as a result.

BTW, the same program that is written in typed/racket, when used from (untyped) racket, works as expected:

Welcome to DrRacket, version 6.2.900.17--2015-09-20(6dfc20d/a) [3m].
Language: racket; memory limit: 4096 MB.
> (define shapes (all-shapes))
> (line? (car shapes))
#t
> (if (line? (car shapes)) 1 2)
1

Is there anything I should be looking for in my forthcoming debugging session?

Best,
António.

Asumu Takikawa

unread,
Sep 30, 2015, 4:01:36 PM9/30/15
to Antonio Menezes Leitao, Racket Dev
On 2015-09-30 20:57:07 +0100, Antonio Menezes Leitao wrote:
> Is there anything I should be looking for in my forthcoming debugging
> session?

Can you try this with the TR optimizer off? (put #:no-optimize in the #lang)

Cheers,
Asumu

Antonio Menezes Leitao

unread,
Sep 30, 2015, 4:45:49 PM9/30/15
to Asumu Takikawa, Racket Dev
It makes no difference :-(

I'll try to build a MWE.

Best,
António.

Asumu Takikawa

unread,
Sep 30, 2015, 4:50:30 PM9/30/15
to Antonio Menezes Leitao, Racket Dev
On 2015-09-30 21:45:28 +0100, Antonio Menezes Leitao wrote:
> It makes no difference :-(
> I'll try to build a MWE.

Just to make sure, did you try this at a REPL with the file in no-optimize
mode? Or was the interaction also in the non-optimized module? (the TR REPL is
always optimized, so to test you will want the interaction in a module)

Cheers,
Asumu

Antonio Menezes Leitao

unread,
Sep 30, 2015, 5:38:10 PM9/30/15
to Asumu Takikawa, Racket Dev
Here is a small example that shows the problem:

#lang typed/racket

(struct (A) foo
  ([x : A]))

(struct (A) bar foo ())

(struct (A) baz foo ())

(define (make-foo [i : Integer]) : (foo Integer)
  (if (even? i)
      (bar i)
      (baz i)))

(define foos (map make-foo '(1 2 3 4 5 6)))

(list (baz? (car foos)) (if (baz? (car foos)) 1 2))

;;(#t 1) with #lang typed/racket/no-check
;;(#t 2) with #lang typed/racket


Note that if you remove the return type declaration (foo Integer) from make-foo, it computes the correct result.

Thanks in advance for your help.

Best,
António.


Asumu Takikawa

unread,
Sep 30, 2015, 5:46:43 PM9/30/15
to Antonio Menezes Leitao, Racket Dev
On 2015-09-30 22:37:49 +0100, Antonio Menezes Leitao wrote:
> Note that if you remove the return type declaration (foo Integer) from
> make-foo, it computes the correct result.

Thanks for the minimal example! Looks like if you add #:no-optimize it returns
the same result as no-check mode.

I suspect that TR thinks (baz Any) is disjoint from (foo Integer) in this case
and decides that therefore (baz? (car foos)) must always fail, leading it to
optimize the expression to 2.

Cheers,
Asumu

Sam Tobin-Hochstadt

unread,
Sep 30, 2015, 5:47:39 PM9/30/15
to Antonio Menezes Leitao, Asumu Takikawa, Racket Dev
This is indeed caused by the optimizer removing needed code. Here's a
smaller version:

#lang typed/racket
(struct (A) foo ([x : A]))

(struct (A) baz foo ())

(define (f [i : Integer]) : (foo Integer)
(baz i))

(if (baz? (f 1)) 1 2)

Sam

On Wed, Sep 30, 2015 at 5:37 PM, Antonio Menezes Leitao
<antonio.men...@ist.utl.pt> wrote:
> Here is a small example that shows the problem:
>
> #lang typed/racket
>
> (struct (A) foo
> ([x : A]))
>
> (struct (A) bar foo ())
>
> (struct (A) baz foo ())
>
> (define (make-foo [i : Integer]) : (foo Integer)
> (if (even? i)
> (bar i)
> (baz i)))
>
> (define foos (map make-foo '(1 2 3 4 5 6)))
>
> (list (baz? (car foos)) (if (baz? (car foos)) 1 2))
>
> ;;(#t 1) with #lang typed/racket/no-check
> ;;(#t 2) with #lang typed/racket
>
>
> Note that if you remove the return type declaration (foo Integer) from
> make-foo, it computes the correct result.
>
> Thanks in advance for your help.
>
> Best,
> António.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-dev+...@googlegroups.com.
> To post to this group, send email to racke...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-dev/CAJQmiZXgh_OK8s5mqUXdrshKneHz%2BuJ8QOifUnkz3K4LfTXS8w%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

Antonio Menezes Leitao

unread,
Sep 30, 2015, 5:55:19 PM9/30/15
to Asumu Takikawa, Racket Dev
Indeed, I was trying it at the REPL. Sorry for the confusion (I was expecting the REPL to follow what was specified in the definitions pane).

Thanks,
António.

Reply all
Reply to author
Forward
0 new messages