Hi Chris, I just pushed an update to struct-plus-plus that I think does what you want. https://pkgd.racket-lang.org/pkgn/package/struct-plus-plus
The package server is telling me "This package needs documentation". I'm not sure why that is, since I've got scribblings/strut-plus-plus.scrbl and I thought that was all I needed. Regardless, here's the link to the HTML file until I can get it sorted: https://www.dropbox.com/s/qj1krqau8nonhxq/struct-plus-plus.html?dl=0 Here's how to implement what you were looking for: #lang racket (require struct-plus-plus) (struct Temp (val)) (struct Label (val)) (struct VarValue (val)) ; The requirements you gave were: ;; "In this case, if name is Temp? then global? has to be #f." ;; "If name is Label? then global? can be #t or #f." (struct++ Result ([name (or/c Temp? Label?)] [global? any/c (negate false?)] ; coerce to #t or #f [value (or/c VarValue? #f)]) (#:rule ("check name" #:check (name global?) [(or (Label? name) (and (Temp? name) (false? global?)))])) #:transparent ) ; Just for fun, I've contracted global? to accept any value but coerce ; it to #t / #f. It's effectively still a boolean field but it's more ; convenient. (Result++ #:name (Temp 7) #:global? #f #:value #f) (with-handlers ([identity identity]) (Result++ #:name (Temp 7) #:global? #t #:value #f)) (Result++ #:name (Label "foo") #:global? #f #:value #f) (Result++ #:name (Label "foo") #:global? #t #:value #f) (Result++ #:name (Label "foo") #:global? 'ok #:value #f) ; 'ok is coerced to #t Output if you run the above: (Result #<Temp> #f #f) (exn:fail:contract "check name: check failed\n name: #<Temp>\n global?: #t" #<continuation-mark-set>) (Result #<Label> #f #f) (Result #<Label> #t #f) (Result #<Label> #t #f) On Fri, Feb 15, 2019 at 7:53 AM Chris GauthierDickey <[email protected]> wrote: > > Still having trouble though, can't get struct/dc to work in the way I'd like. > I tried to simplify my example: > > (define Result/c > (struct/dc Result > (a number?) > (b boolean?) > ; in other words, if a is 0, then b must be > ; false or the contract is broken > #:inv (a b) (if (= a 0) > (eq? b #f) > #t))) > (provide (contract-out [Result Result/c])) > > The documentation on #:inv says it must return a non-#f value, which I assume > means it acts like a predicate? #f if the contract fails, something else > otherwise? In any case, this doesn't work because it says: > > Result: broke its own contract > promised: Result? > produced: #<procedure:Result> > in: (struct/dc > Result > (a number?) > (b boolean?) > #:inv > (a b) > ...) > > I also tried specifying all of the struct functions that are created with > their own contracts, like Result-a, Result-b, Result? and then using Result/c > for Result, and this fails too. > > I think I understand how this is being used in the example in the > documentation with #:lazy, but it's not quite what I'm looking for: I'm > looking to use a contract on the struct constructor (in particular, which is > why I thought #:inv would be the way to go) where one field initialization > depends on another. > > As an aside, I also tried the following: > > (provide (contract-out [Result (or/c (number? boolean? . -> . Result?) > (0 #f . -> . Result?))])) > > But the contract system complained that both might match (I mean I can see > that one is more specific, but I understand it's not a theorem prover, haha). > > Thoughts? > Chris > > On Fri, Feb 15, 2019 at 7:56 AM Chris GauthierDickey <[email protected]> > wrote: >> >> I believe it should be since bst/c is supposed to be a contract. Also I >> noticed the docs at >> https://docs.racket-lang.org/reference/data-structure-contracts.html show a >> similar example: >> >> ; bst-between : number number -> contract >> ; builds a contract for binary search trees >> ; whose values are between low and high >> (define (bst-between/c low high) >> (or/c null? >> (struct/dc node [val (between/c low high)] >> [left (val) #:lazy (bst-between/c low val)] >> [right (val) #:lazy (bst-between/c val high)]))) >> >> >> On Fri, Feb 15, 2019 at 1:31 AM David Storrs <[email protected]> wrote: >>> >>> On Fri, Feb 15, 2019 at 12:18 AM David Storrs <[email protected]> >>> wrote: >>> > >>> > On Thu, Feb 14, 2019 at 9:08 PM Robby Findler >>> > <[email protected]> wrote: >>> > > >>> > > This is what struct/dc is for. Let me know if the docs let you down! >>> > > >>> > > Robby >>> > >>> > This is good to hear about, because I'm actually in the middle of >>> > writing something that it will help with. Thanks, Robby. >>> > >>> > One question: Is there a typo in the example code or am I missing >>> > something? >>> > >>> > (struct bt (val left right)) >>> > (define (bst/c lo hi) >>> > (or/c #f >>> > (struct/dc bt >>> > [val (between/c lo hi)] >>> > [left (val) #:lazy (bst lo val)] ; should that be (bt >>> > lo val)? >>> > [right (val) #:lazy (bst val hi)]))) ; and this be (bt val >>> > hi) ? >>> >>> Ugh. I hate it when I make significant typos in code questions. I >>> should have asked if it was supposed to (bst/c lo val) and (bst/c val >>> hi). (i.e., is there a missing /c in the code?) If not, where is bst >>> defined? >>> >>> > >>> > Also, am I correct that the contract is not actually attached to the >>> > bt structure at this point? That's a thing that would be done >>> > elsewhere, probably in the provide statement. Yes? >>> > >>> > > >>> > > On Thu, Feb 14, 2019 at 7:57 PM Chris GauthierDickey >>> > > <[email protected]> wrote: >>> > >> >>> > >> I'm wondering if it's possible to have a contract for a struct that >>> > >> only allows certain kinds of initializations. For example, I have this: >>> > >> >>> > >> (provide (contract-out >>> > >> [struct Result ((name (or/c Temp? Label?)) (global? >>> > >> boolean?) (value (or/c VarValue? #f)))])) >>> > >> >>> > >> But it's too general. What I'd really like to have is a contract that >>> > >> combines them like an or/c (I can see what this would cause problems, >>> > >> just wondering if there's a clever way around it): >>> > >> (provide (contract-out >>> > >> [or/c (struct Result ((name Temp?) (global? #f) (value (or/c >>> > >> VarValue? #f)))) >>> > >> (struct Result ((name Label?) (global? boolean?) >>> > >> (value (or/c (VarValue #f)))))])) >>> > >> >>> > >> In this case, if name is Temp? then global? has to be #f. If name is >>> > >> Label? then global? can be #t or #f. I'd like to use struct cause it >>> > >> provides all the contracts for accessors and such. Note, I can use a >>> > >> #:guard on the struct, but wondered if it was possible to do something >>> > >> along these lines with a contract. >>> > >> >>> > >> Thanks! >>> > >> Chris >>> > >> >>> > >> >>> > >> -- >>> > >> 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 [email protected]. >>> > >> 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 [email protected]. >>> > > 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 [email protected]. For more options, visit https://groups.google.com/d/optout.

