There is, but it's called "wrap your data in a struct". The type checker doesn't know that serialization guarantees your schema, and you haven't proved that *only* previously-serialized data will be constructed. In order to represent this knowledge with types, you can create a struct wrapper in a module that exports functions that guarantee the property is checked exactly once, when the struct is constructed. This doesn't remove the O(N) pass, but it does prevent you from having *multiple* O(N) passes as you hand off data between functions. With the struct wrapper, constructing instances requires the O(N) check but passing instances between functions that accept the struct type is a constant-time struct predicate test.
On Thursday, February 28, 2019 at 2:43:32 PM UTC-8, Brian Craft wrote: > > So, when working with large data that is internal to an app, where schema > is guaranteed by serialization, there's no way to load that without another > O(N) pass to satisfy the type checker? > > > On Thursday, February 28, 2019 at 1:03:48 PM UTC-8, Sam Tobin-Hochstadt > wrote: >> >> Yes, a cast to a List type checks all the elements of the list. >> There's no way to tell if every element of list is a string in less >> than O(N) time -- that information just isn't available anywhere. >> >> Sam >> >> On Thu, Feb 28, 2019 at 3:52 PM Brian Craft <craft...@gmail.com> wrote: >> > >> > Really? A cast is also O(N)? >> > >> > On Thursday, February 28, 2019 at 11:11:01 AM UTC-8, Sam >> Tobin-Hochstadt wrote: >> >> >> >> Any of these solutions need a O(N) traversal of the data. That's >> >> because we need to do a full traversal to be sure we got an actual >> >> (Listof String) from read-json -- there's no way around it. >> >> >> >> Sam >> >> >> >> On Thu, Feb 28, 2019 at 2:04 PM Brian Craft <craft...@gmail.com> >> wrote: >> >> > >> >> > I would think there'd be a large performance issue, as well, due to >> needing an O(N) walk of the data. I'm having type checker issues with >> (time), so haven't tested it, but maybe (cast) will get me past those. >> >> > >> >> > Thanks! >> >> > >> >> > On Monday, February 25, 2019 at 4:26:52 PM UTC-8, Philip McGrath >> wrote: >> >> >> >> >> >> An alternative to figuring out how to satisfy the type checker is >> to use `cast`, e.g.: >> >> >> #lang typed/racket >> >> >> (require typed/json) >> >> >> (string-join >> >> >> (cast >> >> >> (string->jsexpr >> >> >> "[\"The\",\"quick\",\"brown\",\"fox\",\"...\"]") >> >> >> (Listof String))) >> >> >> >> >> >> Obviously this has pros and cons, the main pro being that it runs. >> The biggest con, in my view, is that you effectively are dropping into >> untyped world. If you figure out how to satisfy the type checker, you get >> assurance that your code is well-typed, which is presumably what you want >> if your using Typed Racket in the first place. Using `cast` means that you >> are responsible for making sure that expression has the type you say it >> does, without help from Typed Racket, or you will get a runtime error. For >> example, I often forget that `read-json` might return `eof` rather than a >> `JSExpr`. Also, `cast` uses the contract machinery, which can carry >> performance costs. >> >> >> >> >> >> But the fact that it runs is not a small benefit, especially if it >> lets you fill in other parts of your program and come back to remove the >> `cast` later. It also lets you take advantage of Typed Racket to generate >> any complicated runtime checks. >> >> >> >> >> >> -Philip >> >> >> >> >> >> >> >> >> On Mon, Feb 25, 2019 at 6:49 PM <jackh...@gmail.com> wrote: >> >> >>> >> >> >>> A JSExpr is one of a couple of things: >> >> >>> >> >> >>> - A list >> >> >>> - A hash with symbol keys >> >> >>> - A number >> >> >>> - A string >> >> >>> - A boolean >> >> >>> - Null >> >> >>> >> >> >>> The (andmap string?) approach implicitly assumes you're giving it >> a list. But it might be something else instead, so you want this: (and >> (list? js) (andmap string? js)) >> >> >>> >> >> >>> On Monday, February 25, 2019 at 3:44:38 PM UTC-8, Brian Craft >> wrote: >> >> >>>> >> >> >>>> So, that also gives me a type error: >> >> >>>> >> >> >>>> Type Checker: Polymorphic function `andmap' could not be applied >> to arguments: >> >> >>>> Domains: (-> a b ... b c) (Listof a) (Listof b) ... b >> >> >>>> (-> a c : d) (Listof a) >> >> >>>> Arguments: (-> Any Boolean : String) (U EOF JSExpr) >> >> >>>> >> >> >>>> in: (andmap string? s) >> >> >>>> >> >> >>>> >> >> >>>> On Monday, February 25, 2019 at 3:22:12 PM UTC-8, Sam >> Tobin-Hochstadt wrote: >> >> >>>>> >> >> >>>>> I think (andmap string? ...) is probably the easiest way to >> check that. >> >> >>>>> >> >> >>>>> Sam >> >> >>>>> >> >> >>>>> On Mon, Feb 25, 2019, 6:20 PM Brian Craft <craft...@gmail.com> >> wrote: >> >> >>>>>> >> >> >>>>>> In typed racket, parsing a string list gives me a JSExpr, which >> is a union. I need to pass it to functions that operate on string lists, >> but can't figure out how to please the type checker. Maybe with occurrence >> typing? But I don't know how to assert "this is a list of strings". >> >> >>>>>> >> >> >>>>>> -- >> >> >>>>>> You received this message because you are subscribed to the >> Google Groups "Racket Users" group. >> >> >>>>>> To unsubscribe from this group and stop receiving emails from >> it, send an email to racket-users...@googlegroups.com. >> >> >>>>>> For more options, visit https://groups.google.com/d/optout. >> >> >>> >> >> >>> -- >> >> >>> You received this message because you are subscribed to the Google >> Groups "Racket Users" group. >> >> >>> To unsubscribe from this group and stop receiving emails from it, >> send an email to racket-users...@googlegroups.com. >> >> >>> For more options, visit https://groups.google.com/d/optout. >> >> > >> >> > -- >> >> > You received this message because you are subscribed to the Google >> Groups "Racket Users" group. >> >> > To unsubscribe from this group and stop receiving emails from it, >> send an email to racket-users...@googlegroups.com. >> >> > For more options, visit https://groups.google.com/d/optout. >> > >> > -- >> > You received this message because you are subscribed to the Google >> Groups "Racket Users" group. >> > To unsubscribe from this group and stop receiving emails from it, send >> an email to racket-users...@googlegroups.com. >> > For more options, visit https://groups.google.com/d/optout. >> > -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.