The reason the first one doesn't work is that the type checker has
figured out that the variable is mutable and HASN"T figured out that
there are no other threads around that could mutate it. In the second
case, it knows there are no other threads around to mutate it because
even if there were other threads, there is no set!, so it can't be
mutated.

Robby


On Tue, Aug 30, 2016 at 7:44 AM, Alex Knauth <alexan...@knauth.org> wrote:
>
> On Aug 30, 2016, at 2:08 AM, Sourav Datta <soura.ja...@gmail.com> wrote:
>
> I tried this approach with Racket 6.5 and still get the type checker error
> when trying to call the continuation after it has been set.
>
> #lang typed/racket
>
> (define-type EmptySet (U))
>
> (: d-or-s (U False (-> Number EmptySet)))
> (define d-or-s #f)
>
> (: double-or-same (-> Number Number))
> (define (double-or-same x)
> (call/cc (lambda ({c : (-> Number EmptySet)})
>            (set! d-or-s c)
>            (+ x x))))
>
> (double-or-same 10)
>
> - : Number
> 20
>
> (d-or-s 1)
>
> . Type Checker: Cannot apply expression of type (U False (-> Number
> Nothing)), since it is not a function type in: (d-or-s 1)
>
> d-or-s
>
> - : (U False (-> Number Nothing))
> #<continuation>
>
> Not sure why the type checker is complaining that d-or-s is not a function
> type.
>
>
> You're right, the (U False (-> Number EmptySet)) can't be applied directly.
> The reason is that it might be a function type, but it also might be false.
> Applying false as a function would be a type error, so applying something
> that might be false is also a type error.
>
> The way around this should be occurrence typing. The first thing to try is
> an if statement like this:
>
>> (if d-or-s
>       (d-or-s 1)
>       "it wasn't a function")
> . Type Checker: Cannot apply expression of type (U False (-> Number
> Nothing)), since it is not a function type in: (d-or-s 1)
>
> However, it raises the same type error as before. The reason is that
> `d-or-s` being mutable messes it up. The way to get around that is to make a
> new local variable to use in the if statement. Since `f` isn't mutable,
> occurrence typing can work on it, and you can call `f` within the
> then-branch.
>
>> (let ([f d-or-s])
>     (if f
>         (f 1)
>         "it wasn't a function"))
> 1
>
> Alex Knauth
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to