For / annotation list in typed / racket

I am trying to add types to some numerical rocket code in the hope of making it faster, but I am stuck in working on the macro / list extension in the code below.

(: index-member ((Listof Any) (Listof Any) -> (Listof Index))) (define (index-member xs ys) (filter-not negative? (for/list ([(ann i Index) (in-range (ann (length xs) Index))]) (if (member (list-ref xs i) ys) i -1)))) 

This function returns the index list of foreach x, which is a member of y. It works on Racket, but it seems like I can't get past the Typed Racket type check. In particular, the error:

Type Checker: error when expanding a macro - insufficient type information in typecheck. add additional type annotations to: (for / list ((((ann index) (in-range (ann (length xs) Index)))) (if (member (list-ref xs i) ys) -1))

Can you provide annotations that go past type checking and / or explain why these type annotations are insufficient?

+4
source share
1 answer

The key is to use the for/list: form, as it allows you to add type annotations to the main for/list form, to give Typed Racket more recommendations. I made a few other adjustments to get the types to align (for example, using filter on top of filter-not , avoiding in-range , etc.):

 #lang typed/racket (: index-member ((Listof Any) (Listof Any) -> (Listof Index))) (define (index-member xs ys) (filter index? (for/list: : (Listof Integer) ([i : Index (length xs)]) (if (member (list-ref xs i) ys) i -1)))) 

This actually reveals a weakness in the filter-not (a filter smarter about the type of the returned list), which I will cover in the commit.

+4
source

Source: https://habr.com/ru/post/1499633/


All Articles