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?