[prev in list] [next in list] [prev in thread] [next in thread]
List: racket-users
Subject: Re: [racket] specifying types for fields in non-polymorphic structures
From: Sam Tobin-Hochstadt <samth () cs ! indiana ! edu>
Date: 2014-04-26 14:53:49
Message-ID: CAK=HD+bcXewp_Zas2ogYHgpyUFtFAP3_FgZa_TuckDb1-S16RA () mail ! gmail ! com
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
No, there's no 'and' for types. Adding that would significantly increase
the complexity of the type checker, and so it's not on the horizon for
Typed Racket. Bounded polymorphism, as I mentioned earlier, is more likely
to be added.
Sam
On Apr 25, 2014 11:06 PM, "Alexander D. Knauth" <alexander@knauth.org>
wrote:
> 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)
> x
> (error "error"))
> (error "error")))
>
> > f
> - : (Any -> Positive-Integer)
> #<procedure:f>
> 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@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@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@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@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
>>
>
>
[Attachment #5 (text/html)]
<p dir="ltr">No, there's no 'and' for types. Adding that would \
significantly increase the complexity of the type checker, and so it's not on the \
horizon for Typed Racket. Bounded polymorphism, as I mentioned earlier, is more \
likely to be added.</p>
<p dir="ltr">Sam</p>
<div class="gmail_quote">On Apr 25, 2014 11:06 PM, "Alexander D. Knauth" \
<<a href="mailto:alexander@knauth.org">alexander@knauth.org</a>> wrote:<br \
type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 \
.8ex;border-left:1px #ccc solid;padding-left:1ex"> <div \
style="word-wrap:break-word"><div>Then is there something like and/c for types? (like \
U is like or/c for types) That could probably solve it. \
</div><div><br></div><div>It seems like typed racket is already doing this in some \
cases, for example:</div> <div><div><font face="Courier New">#lang \
typed/racket</font></div><div><font face="Courier New"><br></font></div><div><font \
face="Courier New">(define positive-real? (make-predicate \
Positive-Real))</font></div><div><font face="Courier New"><br> \
</font></div><div><font face="Courier New">(define (f x)</font></div><div><font \
face="Courier New"> (if (positive-real? x)</font></div><div><font face="Courier \
New"> (if (exact-integer? x)</font></div><div><font face="Courier New"> \
x</font></div> <div><font face="Courier New"> (error \
"error"))</font></div><div><font face="Courier New"> (error \
"error")))</font></div></div><div><font face="Courier \
New"><br></font></div><div><font face="Courier New">> f</font></div> <div><font \
face="Courier New"><div>- : (Any -> \
Positive-Integer)</div><div>#<procedure:f></div></font></div><div>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. </div> \
<div><br></div><div>But I couldn't find it in the docs. </div><br><div><div>On Apr \
25, 2014, at 10:48 PM, Sam Tobin-Hochstadt <<a href="mailto:samth@cs.indiana.edu" \
target="_blank">samth@cs.indiana.edu</a>> wrote:</div> <br><blockquote \
type="cite"><p dir="ltr">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.</p><p dir="ltr">Sam</p> <div class="gmail_quote">On Apr 25, 2014 10:46 PM, \
"Alexander D. Knauth" <<a href="mailto:alexander@knauth.org" \
target="_blank">alexander@knauth.org</a>> wrote:<br type="attribution"><blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex">
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.<br>
<br>
On Apr 25, 2014, at 10:25 PM, David Van Horn <<a href="mailto:dvanhorn@cs.umd.edu" \
target="_blank">dvanhorn@cs.umd.edu</a>> wrote:<br> <br>
> On 4/25/14, 9:57 PM, Alexander D. Knauth wrote:<br>
>> 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.<br> ><br>
> Maybe I'm not seeing the big picture, but that's what the Posn type \
is<br> > for. If you apply posn to something other than reals, you won't get \
a<br> > Posn. If you have a Posn and apply posn-x, you get a real.<br>
><br>
> (define: (f [p : Posn]) : Real<br>
> (+ (posn-x p) (posn-y p)))<br>
><br>
> David<br>
><br>
><br>
>> On Apr 25, 2014, at 9:49 PM, David Van Horn <<a \
href="mailto:dvanhorn@cs.umd.edu" target="_blank">dvanhorn@cs.umd.edu</a>> \
wrote:<br> >><br>
>>> How about this?<br>
>>><br>
>>> (struct: (x y) posn ([x : x] [y : y]))<br>
>>> (define-type Posn (posn Real Real))<br>
>>> (define-type Origin (posn Zero Zero))<br>
>>><br>
>>><br>
><br>
<br>
<br>
____________________<br>
Racket Users list:<br>
<a href="http://lists.racket-lang.org/users" \
target="_blank">http://lists.racket-lang.org/users</a><br> </blockquote></div>
</blockquote></div><br></div></blockquote></div>
____________________
Racket Users list:
http://lists.racket-lang.org/users
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic