[plt-scheme] type signature for a multi-parameter function of a fixed number of arguments in Typed Scheme?

From: Benjamin L.Russell (DekuDekuplex at Yahoo.com)
Date: Thu Oct 16 06:11:07 EDT 2008

This time, I would like to write a type signature for a
multi-parameter function of a fixed number of arguments in Typed
Scheme, but the documentation ("Typed Scheme: Scheme with Static
Types" at http://docs.plt-scheme.org/typed-scheme/) is not clear on
this.

I know how to write a type signature for a function of one argument;
e.g.:

#lang typed-scheme

(: fac (Number -> Number))
(define (fac n)
  (if (= n 0)
      1
      (* n (fac (- n 1)))))

> (fac 5)
- : Number
120
> 

I also know how to write a type signature for a function of zero
arguments; e.g.:

#lang typed-scheme

(: hello-world-function (-> Void))
(define (hello-world-function)
  (display "Hello, world!\n"))

> (hello-world-function)
Hello, world!
> 

Additionally, I know how to write a type signature for a function of a
variable number of arguments:

#lang typed-scheme

(: my-plus-variable (Number * -> Number))
(define (my-plus-variable . ns)
  (if (null? ns)
      0
      (+ (car ns) (apply my-plus-variable (cdr ns)))))

> (my-plus-variable)
- : Number
0
> (my-plus-variable 1 2)
- : Number
3
> (my-plus-variable 1 2 3)
- : Number
6
> 

However, I don't know how to write a type signature for a function of
a fixed number n of arguments, where n > 1.  Here are my attempts to
write a function, my-plus, which takes exactly two arguments n1 and
n2, and returns their sum:

#lang typed-scheme

(: my-plus Number -> Number -> Number)
(define (my-plus n1 n2)
  (+ n1 n2))

. <unsaved editor>:3:0: type declaration: too many arguments in: (:
my-plus Number -> Number -> Number)

Here's another attempt:

#lang typed-scheme

(: my-plus (Number -> (Number -> Number)))
(define (my-plus n1 n2)
  (+ n1 n2))

. <unsaved editor>:4:0: typecheck: Expected function with 1 argument,
but got function with 2 arguments in: (define (my-plus n1 n2) (+ n1
n2))
.

I want to pass my-plus two separate arguments of type Number, n1 and
n2, and return their sum.  This is how my function is written in
regular #lang scheme:

#lang scheme

(define (my-plus n1 n2)
  (+ n1 n2))

> (my-plus 1 2)
3
> 

Can somebody please explain how I can write this function in Typed
Scheme?

Also, does Typed Scheme support currying (from the results that I have
seen so far, apparently it doesn't)?

-- Benjamin L. Russell



Posted on the users mailing list.