# Predicate Inconsistency in with List, Listof in Polymorphic Functions

**URL:** <https://racket.discourse.group/t/predicate-inconsistency-in-with-list-listof-in-polymorphic-functions/3187>\
**Category:** Questions & Answers\
**Tags:** question, typed-racket\
**Created:** [September 28, 2024, 8:30pm UTC](https://racket.discourse.group/t/predicate-inconsistency-in-with-list-listof-in-polymorphic-functions/3187 "2024-09-28T20:30:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wolfraam](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@wolfraam](https://racket.discourse.group/u/wolfraam)\
**Post date:** [September 28, 2024, 8:30pm UTC](https://racket.discourse.group/t/predicate-inconsistency-in-with-list-listof-in-polymorphic-functions/3187/1 "2024-09-28T20:30:46Z")

</div>

Hello,

If I define a function taking a heterogeneous list (`List A ... A`) and try to coerce the argument to a pair with `(assert lst pair?)` or an `unless` guard, the program typechecks, but the assertion fails even when I pass a non-empty list. However, print debugging shows that both `(pair? lst)` and `(list? lst)` return `#t`. Weirder still, if I define a different function that takes a homogeneous list (`Listof A`) with a body identical to the first function, there is no discrepancy; this second function returns just fine.

Is this an issue of my understanding of `List` in TR, or is this a TR bug (8.14 [cs])?

Example code:

```racket
#lang typed/racket/base

(: ttest-homo (All (A) (-> (Listof A) (Listof A))))
(define (ttest-homo lst)
  (display (format "pair? ~a; list? ~a~%" (pair? lst) (list? lst)))
  (unless (pair? lst) ; or (assert inp pair?)
    (error 'wat))
  lst)

(: ttest-hetero (All (A ...) (-> (List A ... A) (List A ... A))))
(define (ttest-hetero lst)
  (display (format "pair? ~a; list? ~a~%" (pair? lst) (list? lst)))
  (unless (pair? lst)
    (error 'wat))
  lst)
(define inp (ann '(1 2 3) (List Natural Natural Natural)))

(ttest-hetero inp)
;pair? #t; list? #t
;error: wat

(ttest-homo inp)
;pair? #t; list? #t
;'(1 2 3)

```

Background:  
I am trying to grok polymorphic functions in TR and have been experimenting by trying to implement a simple function that copies a heterogeneous list. I ran into the above issue when trying to appease the TC. Incidentally, if anyone knows a way to implement the following function so that it typechecks with the given signature, please let me know.

```racket
(: list-copy (All (A ...) (-> (List A ... A) (List A ... A))))
(define (list-copy lst)
  (if (null? lst)
      lst
      (cons (car lst) (list-copy (cdr lst)))))

```

Thank you

edited to correct output

---

<div class="post-metadata">

**Author:** ![EmEf](https://avatars.discourse-cdn.com/v4/letter/e/53a042/32.png) [@EmEf](https://racket.discourse.group/u/EmEf)\
**Post date:** [September 28, 2024, 9:42pm UTC](https://racket.discourse.group/t/predicate-inconsistency-in-with-list-listof-in-polymorphic-functions/3187/2 "2024-09-28T21:42:30Z")

</div>

Stepping through the macros that TR uses here, we get to this strange expansion:

```scheme
 (define-values:102 (ttest-hetero)
     (lambda:103 (lst)
       (#%app:105 display (#%app:106 format (quote "pair? ~a; list? ~a~%") (#%app:107 pair? lst) (#%app:108 list? lst)))
       (if (begin (#%app:109 pair? lst) (quote #f)) (let-values:110 () lst) (let-values:111 () lst))))

```

The `(begin … (quote #f))` suggests something bad is happening here.

---

<div class="post-metadata">

**Author:** ![wolfraam](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@wolfraam](https://racket.discourse.group/u/wolfraam)\
**Post date:** [September 28, 2024, 10:33pm UTC](https://racket.discourse.group/t/predicate-inconsistency-in-with-list-listof-in-polymorphic-functions/3187/3 "2024-09-28T22:33:00Z")

</div>

Well, that answers that part of the question. I guess I'll start working on an Issue on Github.

---

<div class="post-metadata">

**Author:** ![wolfraam](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@wolfraam](https://racket.discourse.group/u/wolfraam)\
**Post date:** [September 28, 2024, 11:50pm UTC](https://racket.discourse.group/t/predicate-inconsistency-in-with-list-listof-in-polymorphic-functions/3187/4 "2024-09-28T23:50:04Z")

</div>

[#1396](https://github.com/racket/typed-racket/issues/1396). I looked through the issues and it looks similar to [#412](https://github.com/racket/typed-racket/issues/412).
