# \`apply\` \`set-union\` in Typed Racket

**URL:** <https://racket.discourse.group/t/apply-set-union-in-typed-racket/2201>\
**Category:** Questions & Answers\
**Tags:** typed-racket\
**Created:** [August 9, 2023, 11:47am UTC](https://racket.discourse.group/t/apply-set-union-in-typed-racket/2201 "2023-08-09T11:47:53Z")\
**Posts on this page:** 3\
**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:** [August 9, 2023, 11:47am UTC](https://racket.discourse.group/t/apply-set-union-in-typed-racket/2201/1 "2023-08-09T11:47:54Z")

</div>

Hello,

I am trying to `apply` `set-union` to a list in Typed Racket. The following happily works and gives the expected answer:

```scheme
#lang typed/racket

(apply set-union (list (set 'a 'b) (set 'c 'd)))

```

However, if I bind the list to a variable with an explicit `Listof` type, I get the following:

```scheme
(define lst : (Listof (Setof Symbol))
  (list (set 'a 'b) (set 'c 'd)))
(apply set-union lst)

```

```scheme
; tmp.rkt:8:0: Type Checker: Bad arguments to function in `apply':
; Domains: (Listof e) (Listof e) *
; (Setof e) (Setof e) *
; Arguments: (Listof (Setof Symbol))
; 
; in: (apply set-union lst)

```

Explicitly instantiating the type of `apply` doesn't help:

```scheme
(apply (inst set-union (Setof Symbol)) lst)

```

```scheme
; tmp.rkt:10:0: Type Checker: Bad arguments to function in `apply':
; Domains: (Listof (Setof Symbol)) (Listof (Setof Symbol)) *
; (Setof (Setof Symbol)) (Setof (Setof Symbol)) *
; Arguments: (Listof (Setof Symbol))
; 
; in: (apply (inst set-union (Setof Symbol)) lst)

```

I suspected that `apply` works for `(list (set 'a 'b) (set 'c 'd))` because the type of the latter is inferred as `(List ...)`, so things break when I force it to `(Listof ...)`, but the following works:

```scheme
(define lst1 : (Listof Number) '(1 2 3))
(apply + lst1)

```

Is there a way to `apply` `set-union` to a `Listof` sets?

---

<div class="post-metadata">

**Author:** ![usao](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/usao/32/1375_2.png) [@usao](https://racket.discourse.group/u/usao)\
**Post date:** [August 9, 2023, 12:01pm UTC](https://racket.discourse.group/t/apply-set-union-in-typed-racket/2201/2 "2023-08-09T12:01:43Z")

</div>

The error message already suggests the problem: `set-union` requires at least **one** argument, that is, the list must be nonempty. To communicate this information with the type checker, you’ll need to use a type like `(Pairof (Setof Symbol) (Listof (Setof Symbol)))`. To do this more conveniently, you can define a type constructor:

```scheme
#lang typed/racket
(define-type (NonEmptyListof A) (Pairof A (Listof A)))

(: lst (NonEmptyListof (Setof Symbol)))
(define lst (list (set 'a 'b) (set 'c 'd)))

(apply set-union lst)

```

---

<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:** [August 9, 2023, 8:57pm UTC](https://racket.discourse.group/t/apply-set-union-in-typed-racket/2201/3 "2023-08-09T20:57:47Z")

</div>

Oooh, thank you @usao , this seems so obvious now that you have explained it to me!

The error message was indeed quite explicit, but I didn't read `(Setof e) (Setof e) *` properly.
