# Replicating the type of \`apply\`

**URL:** <https://racket.discourse.group/t/replicating-the-type-of-apply/770>\
**Category:** Questions & Answers\
**Tags:** typed-racket\
**Created:** [March 5, 2022, 10:08pm UTC](https://racket.discourse.group/t/replicating-the-type-of-apply/770 "2022-03-05T22:08:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![scolobb](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/scolobb/32/108_2.png) [@scolobb](https://racket.discourse.group/u/scolobb)\
**Post date:** [March 5, 2022, 10:08pm UTC](https://racket.discourse.group/t/replicating-the-type-of-apply/770/1 "2022-03-05T22:08:29Z")

</div>

Hello,

I am trying to replicate the type of `apply`:

```scheme
tmp.rkt> apply
- : (All (a b) (-> (-> a * b) (Listof a) b))
#<procedure:apply>

```

So, I define my own function:

```scheme
(: my-apply (All (a b) (-> (-> a * b) (Listof a) b)))
(define (my-apply f xs) (apply f xs))

```

I define a fixed-arity function `f`:

```scheme
(: f (-> Number Number Number))
(define (f x y) (+ x y))

```

Testing time:

```scheme
tmp.rkt> (apply f '(1 2))
- : Number
3
tmp.rkt> (my-apply f '(1 2))
; /home/scolobb/Candies/prj/racket/dds/tmp.rkt:30:0: Type Checker: Polymorphic function `my-apply' could not be applied to arguments:
; Argument 1:
; Expected: (-> a * b)
; Given: (-> Number Number Number)
; Argument 2:
; Expected: (Listof a)
; Given: (List One Positive-Byte)
; ...
; /gnu/store/qck82j07zb03m4q66np999f8bcfyp6m6-racket-8.4/lib/racket/pkgs/typed-racket-lib/typed-racket/typecheck/tc-toplevel.rkt:665:0 tc-toplevel-form
tmp.rkt> ((inst my-apply Number Number) f '(1 2))
; /home/scolobb/Candies/prj/racket/dds/tmp.rkt:32:31: Type Checker: type mismatch
; expected: (-> Number * Number)
; given: (-> Number Number Number)
; in: f
; ...

```

How do I replicate the type of `apply?` My actual use case to write the type for a function `apply`ing a list of functions.

Note that `my-apply` works OK with variable-arity functions, e.g.:

```scheme
tmp.rkt> (my-apply + '(1 2))
- : Integer [more precisely: Nonnegative-Integer]
3

```

However, I would like to `my-apply` to handle both fixed- and variable-arity functions, like `apply`.

---

<div class="post-metadata">

**Author:** ![scolobb](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/scolobb/32/108_2.png) [@scolobb](https://racket.discourse.group/u/scolobb)\
**Post date:** [March 5, 2022, 11:08pm UTC](https://racket.discourse.group/t/replicating-the-type-of-apply/770/2 "2022-03-05T23:08:12Z")

</div>

Just found a solution:

```scheme
(: my-apply (All (b a ...) (-> (-> a ... b) (List a ...) b)))
(define (my-apply f xs) (apply f xs))

```

```scheme
tmp.rkt> (my-apply f '(1 2))
- : Number
3
tmp.rkt> (my-apply + '(1 2 3))
- : Integer [more precisely: Positive-Index]
6

```

I am somewhat surprised that the type of `apply` literally copied and pasted does not work, but I've got myself a solution anyway, so no complaints 🙂

---

<div class="post-metadata">

**Author:** ![samth](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/samth/32/3_2.png) [@samth](https://racket.discourse.group/u/samth)\
**Post date:** [March 7, 2022, 6:26pm UTC](https://racket.discourse.group/t/replicating-the-type-of-apply/770/3 "2022-03-07T18:26:14Z")

</div>

`apply` is handled specially by the type checker, so you can't replicate its behavior exactly for your own code.

---

<div class="post-metadata">

**Author:** ![scolobb](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/scolobb/32/108_2.png) [@scolobb](https://racket.discourse.group/u/scolobb)\
**Post date:** [March 7, 2022, 10:27pm UTC](https://racket.discourse.group/t/replicating-the-type-of-apply/770/4 "2022-03-07T22:27:59Z")

</div>

Thanks @samth! I guessed as much, but it's helpful to have an official confirmation.
