[racket] specifying types for fields in non-polymorphic structures

From: Alexander D. Knauth (alexander at knauth.org)
Date: Fri Apr 25 23:06:43 EDT 2014

Then is there something like and/c for types? (like U is like or/c for types) That could probably solve it.  

It seems like typed racket is already doing this in some cases, for example:
#lang typed/racket

(define positive-real? (make-predicate Positive-Real))

(define (f x)
  (if (positive-real? x)
      (if (exact-integer? x)
          (error "error"))
      (error "error")))

> f
- : (Any -> Positive-Integer)
In this, typed racket figures out that f produces a Positive-Integer, when it was only given that it produces something that is both a Positive-Real and an Integer.  

But I couldn’t find it in the docs.  

On Apr 25, 2014, at 10:48 PM, Sam Tobin-Hochstadt <samth at cs.indiana.edu> wrote:

> What you're looking for is called bounded polymorphism. Sadly, Typed Racket doesn't support this, so I'd try one of the options David suggests.
> Sam
> On Apr 25, 2014 10:46 PM, "Alexander D. Knauth" <alexander at knauth.org> wrote:
> That’s what I tried at first, but my actual struct is a lot more complicated than posn, and it kept giving be type-check errors, and trying to enforce the types of the fields myself just ended up with completely unreadable code and even more type errors, so I gave up on making it polymorphic, but I still wanted to be able to specify specific cases of it as types that would only contain that specific case of it.
> On Apr 25, 2014, at 10:25 PM, David Van Horn <dvanhorn at cs.umd.edu> wrote:
> > On 4/25/14, 9:57 PM, Alexander D. Knauth wrote:
> >> But then the posn constructor doesn’t enforce that it’s arguments have to be Reals, and the posn? predicate doesn’t check it, and the accessors don’t say that they always produce Reals.
> >
> > Maybe I'm not seeing the big picture, but that's what the Posn type is
> > for.  If you apply posn to something other than reals, you won't get a
> > Posn.  If you have a Posn and apply posn-x, you get a real.
> >
> > (define: (f [p : Posn]) : Real
> >  (+ (posn-x p) (posn-y p)))
> >
> > David
> >
> >
> >> On Apr 25, 2014, at 9:49 PM, David Van Horn <dvanhorn at cs.umd.edu> wrote:
> >>
> >>> How about this?
> >>>
> >>> (struct: (x y) posn ([x : x] [y : y]))
> >>> (define-type Posn (posn Real Real))
> >>> (define-type Origin (posn Zero Zero))
> >>>
> >>>
> >
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140425/7f464ae1/attachment.html>

Posted on the users mailing list.