Re: [racket-users] function dispatch in typed racket

2019-10-14 Thread Sam Tobin-Hochstadt
Typed Racket won't generate type-dispatching code for you
automatically. However, if you write such code the same way you'd
write it in regular Racket, Typed Racket will be ok with that. For
example,

#lang typed/racket

(: id : (case-> (One One -> Number)
((Immutable-Vectorof One) (Immutable-Vectorof One) ->
(Vectorof Number

(define id (lambda (x y)
 (if (number? x)
 (+ x y)
 (for/vector : (Vectorof Number) ([i x] [j y]) (+ i j)

(id 1 1)
(id #(1 1) #(1 1))

On Mon, Oct 14, 2019 at 9:13 AM Raoul Schorer  wrote:
>
> Hi,
>
> I am trying to write an APL compiler (a typed J implementation) with a typed 
> racket backend. APLs have first-class functions that apply to both scalars 
> and arrays, so there are 2 cases for functions of 2 arguments:
>
> the array -- array case
> the scalar -- scalar case
>
> Scalar -- array and array -- scalar cases get their scalar argument lifted to 
> arrays, and are therefore equivalent to the array -- array case.
> Now, with an interpreter if I assign a function to an identifier, it can use 
> just textual replacement to evaluate the function glyph in the runtime 
> context (that's the way J does it). For example:
>
> id =: +  ;;assign the 'add' function to 'id'
>
> 1 id 1   ;;evaluate to '2' using the scalar context on replacement with 
> '+'
>
> 1 1 id 1 1;;evaluates to '2 2' using the array context on replacement 
> with '+'
>
>
> But at compile-time I can't use that trick, can I? So I end up with something 
> like:
> #lang typed/racket
>
> (define id (case-lambda
>  [([x : One] [y : One]) (+ y)]
>  [([x : (Immutable-Vectorof One)] [y : (Immutable-Vectorof One)]) 
> (for/vector ([i x] [j y]) (+ i j))]))
>
> (id 1 1)
> (id #(1 1) #(1 1))
>
> Which fails although the case-lambda is actually legal typed racket, because 
> the second clause is considered dead code and one cannot dispatch on type.
> In my (hobbyist) understanding, the solution seems to require multiple 
> dispatch? Or is there another way to do that in typed racket?
>
> --
> 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.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/racket-users/63e5ec59-23f1-4fc7-8045-beb32409bbca%40googlegroups.com.

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAK%3DHD%2BYYYSUfnE2VVw-bhE45FZXpjNpJFTeXrJvfFOUMAumRNQ%40mail.gmail.com.


[racket-users] function dispatch in typed racket

2019-10-14 Thread Raoul Schorer
Hi,

I am trying to write an APL compiler (a typed J implementation) with a 
typed racket backend. APLs have first-class functions that apply to both 
scalars and arrays, so there are 2 cases for functions of 2 arguments:

   - the *array -- array* case
   - the *scalar -- scalar* case

*Scalar -- array* and *array -- scalar* cases get their scalar argument 
lifted to arrays, and are therefore equivalent to the *array -- array* case.
Now, with an interpreter if I assign a function to an identifier, it can 
use just textual replacement to evaluate the function glyph in the runtime 
context (that's the way J does it). For example:

id =: +  ;;assign the 'add' function to 'id'

1 id 1   ;;evaluate to '2' using the scalar context on replacement with 
'+'

1 1 id 1 1;;evaluates to '2 2' using the array context on replacement 
with '+'


But at compile-time I can't use that trick, can I? So I end up with 
something like:
#lang typed/racket

(define id (case-lambda
 [([x : One] [y : One]) (+ y)]
 [([x : (Immutable-Vectorof One)] [y : (Immutable-Vectorof One
)]) (for/vector ([i x] [j y]) (+ i j))]))

(id 1 1)
(id #(1 1) #(1 1))

Which fails although the case-lambda is actually legal typed racket 
, because the second 
clause is considered dead code and one cannot dispatch on type.
In my (hobbyist) understanding, the solution seems to require multiple 
dispatch? Or is there another way to do that in typed racket?

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/63e5ec59-23f1-4fc7-8045-beb32409bbca%40googlegroups.com.