<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div>Is it possible to have a struct that does certain things according to the guard?</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">(struct (a) foo ([a : a]) #:transparent</font></div><div><font face="Courier New"> #:guard (lambda (a _)</font></div><div><font face="Courier New"> (unless (exact-integer? a)</font></div><div><font face="Courier New"> (error 'foo "expected Integer, given ~v" a))</font></div><div><font face="Courier New"> a))</font></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">(ann (foo (ann 1 Any)) (foo Integer))</font></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">(: x : (foo Any))</font></div><div><font face="Courier New">(define x (foo 1))</font></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">(ann (foo-a x) Integer)</font></div></div><div><div><font face="Courier New"><br></font></div><div><font face="Courier New">;. Type Checker: Polymorphic function `foo1' could not be applied to arguments:</font></div><div><font face="Courier New">;Argument 1:</font></div><div><font face="Courier New">; Expected: a</font></div><div><font face="Courier New">; Given: Any</font></div><div><font face="Courier New">;</font></div><div><font face="Courier New">;Result type: (foo a)</font></div><div><font face="Courier New">;Expected result: (foo Integer)</font></div><div><font face="Courier New">; in: (foo (ann 1 Any))</font></div><div><font face="Courier New">;. Type Checker: Polymorphic function `foo-a' could not be applied to arguments:</font></div><div><font face="Courier New">;Argument 1:</font></div><div><font face="Courier New">; Expected: (foo a)</font></div><div><font face="Courier New">; Given: (foo Any)</font></div><div><font face="Courier New">;</font></div><div><font face="Courier New">;Result type: (a : ....)</font></div><div><font face="Courier New">;Expected result: Integer</font></div><div><font face="Courier New">; in: (foo-a x)</font></div></div><br><div><div>On Sep 25, 2014, at 9:42 PM, Alexander D. Knauth <<a href="mailto:alexander@knauth.org">alexander@knauth.org</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><meta http-equiv="Content-Type" content="text/html charset=windows-1252"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div>What I’m trying to accomplish is something more like this:</div><div><font face="Courier New">#lang typed/racket</font></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">(require "dimensions.rkt")</font></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">(struct (d) unit ([name : Any] [scalar : Positive-Real] [dimension : d]) #:transparent</font></div><div><font face="Courier New"> #:guard (lambda (name scalar dimension _)</font></div><div><font face="Courier New"> (unless (dimension? dimension)</font></div><div><font face="Courier New"> (error 'unit "expected Dimension, given ~v" dimension))</font></div><div><font face="Courier New"> (values name scalar dimension)))</font></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">(define-type (Unitof d) </font><span style="font-family: 'Courier New';">(unit d))</span></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">(define-type Unit (Unitof Dimension))</font></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">(define Unit? (make-predicate Unit))</font></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">(define-type Unitish</font></div><div><font face="Courier New"> (U (Unitof Any)</font></div><div><font face="Courier New"> Dimension</font></div><div><font face="Courier New"> Positive-Real))</font></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">(: ->unit : (All (d) (case-> [(Unitof d) -> (Unitof d)]</font></div><div><font face="Courier New"> [Unitish -> Unit])))</font></div><div><font face="Courier New">(define (->unit u)</font></div><div><font face="Courier New"> (cond [(unit? u)</font></div><div><font face="Courier New"> (unless (Unit? u) ; this should never happen anyway because of the guard</font></div><div><font face="Courier New"> (error '->unit "expected (Unitof Dimension), given ~v" u))</font></div><div><font face="Courier New"> u]</font></div><div><font face="Courier New"> [(dimension? u) </font><span style="font-family: 'Courier New';">(unit u 1 u)]</span></div><div><span style="font-family: 'Courier New';"> [(positive-real? u) (unit u u dimensionless-dimension)]))</span></div><div><br></div><br><div><div>On Sep 25, 2014, at 6:19 PM, Sam Tobin-Hochstadt <<a href="mailto:samth@cs.indiana.edu">samth@cs.indiana.edu</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">No, I don't think you can do this. Can you say more about what you're<br>trying to accomplish?<br><br>Sam<br><br>On Thu, Sep 25, 2014 at 6:15 PM, Alexander D. Knauth<br><<a href="mailto:alexander@knauth.org">alexander@knauth.org</a>> wrote:<br><blockquote type="cite">Do any of you have any advice for getting a function like this to<br>type-check?<br>#lang typed/racket<br><br>(: check-int : (All (a) (case-> [a -> a]<br> [Any -> Integer])))<br>(define (check-int int)<br> (unless (exact-integer? int)<br> (error 'check-int "expected Integer, given ~v" int))<br> int)<br><br>;. Type Checker: type mismatch<br>; expected: a<br>; given: Integer in: int<br><br><br><br>____________________<br> Racket Users list:<br> <a href="http://lists.racket-lang.org/users">http://lists.racket-lang.org/users</a><br><br></blockquote></blockquote></div><br></div>____________________<br> Racket Users list:<br> <a href="http://lists.racket-lang.org/users">http://lists.racket-lang.org/users</a><br></blockquote></div><br></body></html>