> 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.

Reply via email to