function dispatch in typed racket

54 views
Skip to first unread message

Raoul Schorer

unread,
Oct 14, 2019, 9:12:58 AM10/14/19
to Racket Users
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?

Sam Tobin-Hochstadt

unread,
Oct 14, 2019, 3:57:32 PM10/14/19
to Raoul Schorer, Racket Users
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))

> --
> 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...@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.
Reply all
Reply to author
Forward
0 new messages