raoul.schorer
2021-7-7 07:25:05

Can #lang typed/racket understand type boundaries in loops/recursion? For example:

#lang typed/racket (struct sequence-thing ([store : Bytes] [size : Index])) (: sequence-thing->list (-> sequence-thing (Listof Byte))) (define (sequence-thing->list st) (define show-stopper (sequence-thing-size st)) (let loop ([index : Index 0] [accumulator : (Listof Byte) null]) (if (< index show-stopper) (loop (add1 index) ((inst cons Byte (Listof Byte)) (bytes-ref (sequence-thing-store st) index) accumulator)) accumulator))) This doesn’t typecheck, because: type mismatch expected: Index given: Positive-Fixnum in: (add1 index) Is there a way to make it see that (add1 index) is of type Index? Or am I misunderstanding something, perhaps?


yfangzhe
2021-7-7 07:28:44

The result of (add1 index) is not always an Index. See these words from the reference: > https://docs.racket-lang.org/ts-reference/type-ref.html?q=Index#%28form._%28%28lib._typed-racket%2Fbase-env%2Fbase-types..rkt%29._.Index%29%29\|Index is bounded by 0 and by the length of the longest possible Racket vector.


sorawee
2021-7-7 07:33:32

Instead of Index, you can use Nonnegative-Fixnum instead, and it should work


sorawee
2021-7-7 07:34:23

spdegabrielle
2021-7-7 07:43:05

We have our first macro contributed to the Syntax Parse Bee! There is a lot of buzz - who will be next? Maybe it will be you? See the announcement for details about the Syntax Parse Bee and how to join in:

https://github.com/syntax-objects/Summer2021/blob/master/ANNOUNCEMENT.md\|https://github.com/syntax-objects/Summer2021/blob/master/ANNOUNCEMENT.md


raoul.schorer
2021-7-7 07:45:32

This does indeed work, thanks! Reading yjqww6’s comment on GitHub though, I wasn’t quite able to understand whether this results in equivalent type constraints as it would if Index was made to typecheck or if this relaxes type constraints somehow?


raoul.schorer
2021-7-7 07:52:56

Aaaaaah yes… I think I understand now! Thanks again!


biz
2021-7-7 12:06:23

This is excellent. I’ve been looking for a way to feed back the knowledge I’ve gained on the syntax/parse library now I’ve mostly figured it out!


ben.knoble
2021-7-7 17:37:14

I know this is old, but updated: https://gist.github.com/benknoble/a01e20f09ee42fbfc6eca42e72200e84 I like the new syntax tremendously.


biz
2021-7-7 18:20:20

@spdegabrielle cloning the repository yields this error when I open syntax-parse-example.scrbl . I get the same error when I run raco pkg install or raco pkg setup



biz
2021-7-7 18:27:25

Ah I planned to make a PR instead. Very well. I’ll paste my code, tests & docs into an issue once it’s finished


spdegabrielle
2021-7-7 18:27:33

The instructions are in the issue template; it asks for the following;

1. your macro 2. an example use of your macro 3. (optional) "before" code that your macro helps to improve The first submission is a great example: https://github.com/syntax-objects/Summer2021/issues/2\|https://github.com/syntax-objects/Summer2021/issues/2


notjack
2021-7-7 21:04:03

wonderful :)