/usr/share/doc/racket/ts-guide/beginning.html is in racket-doc 6.7-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"/><title>2 Beginning Typed Racket</title><link rel="stylesheet" type="text/css" href="../scribble.css" title="default"/><link rel="stylesheet" type="text/css" href="../racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-style.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../doc-site.css" title="default"/><script type="text/javascript" src="../scribble-common.js"></script><script type="text/javascript" src="../manual-racket.js"></script><script type="text/javascript" src="../manual-racket.js"></script><script type="text/javascript" src="../doc-site.js"></script><script type="text/javascript" src="../local-redirect/local-redirect.js"></script><script type="text/javascript" src="../local-redirect/local-user-redirect.js"></script><!--[if IE 6]><style type="text/css">.SIEHidden { overflow: hidden; }</style><![endif]--></head><body id="doc-racket-lang-org"><div class="tocset"><div class="tocview"><div class="tocviewlist tocviewlisttopspace"><div class="tocviewtitle"><table cellspacing="0" cellpadding="0"><tr><td style="width: 1em;"><a href="javascript:void(0);" title="Expand/Collapse" class="tocviewtoggle" onclick="TocviewToggle(this,"tocview_0");">▼</a></td><td></td><td><a href="index.html" class="tocviewlink" data-pltdoc="x">The Typed Racket Guide</a></td></tr></table></div><div class="tocviewsublisttop" style="display: block;" id="tocview_0"><table cellspacing="0" cellpadding="0"><tr><td align="right">1 </td><td><a href="quick.html" class="tocviewlink" data-pltdoc="x">Quick Start</a></td></tr><tr><td align="right">2 </td><td><a href="" class="tocviewselflink" data-pltdoc="x">Beginning Typed Racket</a></td></tr><tr><td align="right">3 </td><td><a href="more.html" class="tocviewlink" data-pltdoc="x">Specifying Types</a></td></tr><tr><td align="right">4 </td><td><a href="types.html" class="tocviewlink" data-pltdoc="x">Types in Typed Racket</a></td></tr><tr><td align="right">5 </td><td><a href="occurrence-typing.html" class="tocviewlink" data-pltdoc="x">Occurrence Typing</a></td></tr><tr><td align="right">6 </td><td><a href="typed-untyped-interaction.html" class="tocviewlink" data-pltdoc="x">Typed-<wbr></wbr>Untyped Interaction</a></td></tr><tr><td align="right">7 </td><td><a href="optimization.html" class="tocviewlink" data-pltdoc="x">Optimization in Typed Racket</a></td></tr><tr><td align="right">8 </td><td><a href="caveats.html" class="tocviewlink" data-pltdoc="x">Caveats and Limitations</a></td></tr></table></div></div><div class="tocviewlist"><table cellspacing="0" cellpadding="0"><tr><td style="width: 1em;"><a href="javascript:void(0);" title="Expand/Collapse" class="tocviewtoggle" onclick="TocviewToggle(this,"tocview_1");">►</a></td><td>2 </td><td><a href="" class="tocviewselflink" data-pltdoc="x">Beginning Typed Racket</a></td></tr></table><div class="tocviewsublistbottom" style="display: none;" id="tocview_1"><table cellspacing="0" cellpadding="0"><tr><td align="right">2.1 </td><td><a href="#%28part._.Datatypes_and_.Unions%29" class="tocviewlink" data-pltdoc="x">Datatypes and Unions</a></td></tr><tr><td align="right">2.2 </td><td><a href="#%28part._.Type_.Errors%29" class="tocviewlink" data-pltdoc="x">Type Errors</a></td></tr></table></div></div></div><div class="tocsub"><div class="tocsubtitle">On this page:</div><table class="tocsublist" cellspacing="0"><tr><td><span class="tocsublinknumber">2.1<tt> </tt></span><a href="#%28part._.Datatypes_and_.Unions%29" class="tocsubseclink" data-pltdoc="x">Datatypes and Unions</a></td></tr><tr><td><span class="tocsublinknumber">2.2<tt> </tt></span><a href="#%28part._.Type_.Errors%29" class="tocsubseclink" data-pltdoc="x">Type Errors</a></td></tr></table></div></div><div class="maincolumn"><div class="main"><div class="versionbox"><span class="version">6.7</span></div><div class="navsettop"><span class="navleft"><form class="searchform"><input class="searchbox" style="color: #888;" type="text" value="...search manuals..." title="Enter a search string to search the manuals" onkeypress="return DoSearchKey(event, this, "6.7", "../");" onfocus="this.style.color="black"; this.style.textAlign="left"; if (this.value == "...search manuals...") this.value="";" onblur="if (this.value.match(/^ *$/)) { this.style.color="#888"; this.style.textAlign="center"; this.value="...search manuals..."; }"/></form> <a href="../index.html" title="up to the documentation top" data-pltdoc="x" onclick="return GotoPLTRoot("6.7");">top</a></span><span class="navright"> <a href="quick.html" title="backward to "1 Quick Start"" data-pltdoc="x">← prev</a> <a href="index.html" title="up to "The Typed Racket Guide"" data-pltdoc="x">up</a> <a href="more.html" title="forward to "3 Specifying Types"" data-pltdoc="x">next →</a></span> </div><h3 x-source-module="(lib "typed-racket/scribblings/ts-guide.scrbl")" x-source-pkg="typed-racket-doc" x-part-tag=""beginning"">2<tt> </tt><a name="(part._beginning)"></a>Beginning Typed Racket</h3><p>Recall the typed module from <a href="quick.html" data-pltdoc="x">Quick Start</a>:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=guide&rel=Module_Syntax.html%23%2528part._hash-lang%2529&version=6.7" class="RktModLink Sq" data-pltdoc="x"><span class="RktMod">#lang</span></a><span class="hspace"> </span><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=index.html&version=6.7" class="RktModLink Sq" data-pltdoc="x"><span class="RktSym">typed/racket</span></a></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=special-forms.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fprims..rkt%2529._struct%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">struct</a></span><span class="hspace"> </span><span class="RktSym">pt</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktPn">[</span><span class="RktSym">x</span><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=special-forms.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fprims..rkt%2529._%7E3a%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">:</a></span><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=type-ref.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fbase-types..rkt%2529._.Real%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">Real</a></span><span class="RktPn">]</span><span class="hspace"> </span><span class="RktPn">[</span><span class="RktSym">y</span><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=special-forms.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fprims..rkt%2529._%7E3a%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">:</a></span><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=type-ref.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fbase-types..rkt%2529._.Real%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">Real</a></span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=special-forms.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fprims..rkt%2529._%7E3a%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">:</a></span><span class="hspace"> </span><span class="RktSym">distance</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=type-ref.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fbase-types-extra..rkt%2529._-%7E3e%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><span class="RktSym">pt</span><span class="hspace"> </span><span class="RktSym">pt</span><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=type-ref.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fbase-types..rkt%2529._.Real%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">Real</a></span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=special-forms.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fprims..rkt%2529._define%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">distance</span><span class="hspace"> </span><span class="RktSym">p1</span><span class="hspace"> </span><span class="RktSym">p2</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._sqrt%2529%2529&version=6.7" class="RktValLink Sq" data-pltdoc="x">sqrt</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%252B%2529%2529&version=6.7" class="RktValLink Sq" data-pltdoc="x">+</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528lib._racket%252Fmath..rkt%2529._sqr%2529%2529&version=6.7" class="RktValLink Sq" data-pltdoc="x">sqr</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._-%2529%2529&version=6.7" class="RktValLink Sq" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">pt-x</span><span class="hspace"> </span><span class="RktSym">p2</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">pt-x</span><span class="hspace"> </span><span class="RktSym">p1</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528lib._racket%252Fmath..rkt%2529._sqr%2529%2529&version=6.7" class="RktValLink Sq" data-pltdoc="x">sqr</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._-%2529%2529&version=6.7" class="RktValLink Sq" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">pt-y</span><span class="hspace"> </span><span class="RktSym">p2</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">pt-y</span><span class="hspace"> </span><span class="RktSym">p1</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote><p>Let us consider each element of this program in turn.</p><blockquote class="SCodeFlow"><p><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=guide&rel=Module_Syntax.html%23%2528part._hash-lang%2529&version=6.7" class="RktModLink Sq" data-pltdoc="x"><span class="RktMod">#lang</span></a><span class="hspace"> </span><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=index.html&version=6.7" class="RktModLink Sq" data-pltdoc="x"><span class="RktSym">typed/racket</span></a></p></blockquote><p>This specifies that the module is written in the
<a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=index.html&version=6.7" class="RktModLink Sq" data-pltdoc="x"><span class="RktSym">typed/racket</span></a> language, which is a typed version of the
<a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=index.html&version=6.7" class="RktModLink Sq" data-pltdoc="x"><span class="RktSym">racket</span></a> language. Typed versions of other languages
are provided as well; for example, the
<a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=index.html&version=6.7" class="RktModLink Sq" data-pltdoc="x"><span class="RktSym">typed/racket/base</span></a> language corresponds to
<a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=index.html&version=6.7" class="RktModLink Sq" data-pltdoc="x"><span class="RktSym">racket/base</span></a>.</p><blockquote class="SCodeFlow"><p><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=special-forms.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fprims..rkt%2529._struct%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">struct</a></span><span class="hspace"> </span><span class="RktSym">pt</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktPn">[</span><span class="RktSym">x</span><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=special-forms.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fprims..rkt%2529._%7E3a%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">:</a></span><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=type-ref.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fbase-types..rkt%2529._.Real%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">Real</a></span><span class="RktPn">]</span><span class="hspace"> </span><span class="RktPn">[</span><span class="RktSym">y</span><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=special-forms.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fprims..rkt%2529._%7E3a%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">:</a></span><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=type-ref.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fbase-types..rkt%2529._.Real%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">Real</a></span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></p></blockquote><p><div class="SIntrapara"><blockquote class="refpara"><blockquote class="refcolumn"><blockquote class="refcontent"><p>Typed Racket provides modified versions of core Racket forms,
which permit type annotations. Previous versions of Typed Racket provided
these with a <span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=special-forms.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fprims..rkt%2529._%7E3a%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">:</a></span> suffix, but these are now only included as legacy
forms for backwards compatibility.</p></blockquote></blockquote></blockquote></div><div class="SIntrapara">This defines a new structure, named <span class="RktSym">pt</span>, with two fields,
<span class="RktSym">x</span> and <span class="RktSym">y</span>. Both fields are specified to have the type
<span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=type-ref.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fbase-types..rkt%2529._.Real%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">Real</a></span>, which corresponds to the <a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=numbers.html%23%2528tech._real._number%2529&version=6.7" class="techoutside Sq" data-pltdoc="x"><span class="techinside">real numbers</span></a>.
The
<span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=special-forms.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fprims..rkt%2529._struct%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">struct</a></span> form corresponds to its untyped counterpart from
<a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=index.html&version=6.7" class="RktModLink Sq" data-pltdoc="x"><span class="RktSym">racket</span></a>—<wbr></wbr>when porting a program from
<a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=index.html&version=6.7" class="RktModLink Sq" data-pltdoc="x"><span class="RktSym">racket</span></a> to <a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=index.html&version=6.7" class="RktModLink Sq" data-pltdoc="x"><span class="RktSym">typed/racket</span></a>, simply add
type annotations to existing field declarations.</div></p><blockquote class="SCodeFlow"><p><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=special-forms.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fprims..rkt%2529._%7E3a%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">:</a></span><span class="hspace"> </span><span class="RktSym">distance</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=type-ref.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fbase-types-extra..rkt%2529._-%7E3e%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><span class="RktSym">pt</span><span class="hspace"> </span><span class="RktSym">pt</span><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=type-ref.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fbase-types..rkt%2529._.Real%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">Real</a></span><span class="RktPn">)</span><span class="RktPn">)</span></p></blockquote><p>This declares that <span class="RktSym">distance</span> has the type <span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=type-ref.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fbase-types-extra..rkt%2529._-%7E3e%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="stt"> </span><span class="RktSym">pt</span><span class="stt"> </span><span class="RktSym">pt</span><span class="stt"> </span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=type-ref.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fbase-types..rkt%2529._.Real%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">Real</a></span><span class="RktPn">)</span>.</p><p>The type <span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=type-ref.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fbase-types-extra..rkt%2529._-%7E3e%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="stt"> </span><span class="RktSym">pt</span><span class="stt"> </span><span class="RktSym">pt</span><span class="stt"> </span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=type-ref.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fbase-types..rkt%2529._.Real%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">Real</a></span><span class="RktPn">)</span> is a function type, that is, the type
of a procedure. The input type, or domain, is two arguments of
type <span class="RktSym">pt</span>, which refers to an instance of the <span class="RktSym">pt</span>
structure. The <span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=type-ref.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fbase-types-extra..rkt%2529._-%7E3e%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span> indicates that this is a function
type. The range type, or output type, is the last element in the
function type, in this case <span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=type-ref.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fbase-types..rkt%2529._.Real%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">Real</a></span>.</p><p>If you are familiar with <a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=contracts.html%23%2528tech._contract%2529&version=6.7" class="techoutside Sq" data-pltdoc="x"><span class="techinside">contracts</span></a>, the notation for function
types is similar to function contract combinators.</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=special-forms.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fprims..rkt%2529._define%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">distance</span><span class="hspace"> </span><span class="RktSym">p1</span><span class="hspace"> </span><span class="RktSym">p2</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._sqrt%2529%2529&version=6.7" class="RktValLink Sq" data-pltdoc="x">sqrt</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%252B%2529%2529&version=6.7" class="RktValLink Sq" data-pltdoc="x">+</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528lib._racket%252Fmath..rkt%2529._sqr%2529%2529&version=6.7" class="RktValLink Sq" data-pltdoc="x">sqr</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._-%2529%2529&version=6.7" class="RktValLink Sq" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">pt-x</span><span class="hspace"> </span><span class="RktSym">p2</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">pt-x</span><span class="hspace"> </span><span class="RktSym">p1</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528lib._racket%252Fmath..rkt%2529._sqr%2529%2529&version=6.7" class="RktValLink Sq" data-pltdoc="x">sqr</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._-%2529%2529&version=6.7" class="RktValLink Sq" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">pt-y</span><span class="hspace"> </span><span class="RktSym">p2</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">pt-y</span><span class="hspace"> </span><span class="RktSym">p1</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote><p>This definition is unchanged from the untyped version of the code.
The goal of Typed Racket is to allow almost all definitions to be
typechecked without change. The typechecker verifies that the body of
the function has the type <span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=type-ref.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fbase-types..rkt%2529._.Real%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">Real</a></span>, under the assumption that
<span class="RktSym">p1</span> and <span class="RktSym">p2</span> have type <span class="RktSym">pt</span>, taking these types
from the earlier type declaration. Since the body does have this type,
the program is accepted.</p><p>In the Typed Racket <a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=guide&rel=intro.html%23%2528tech._repl%2529&version=6.7" class="techoutside Sq" data-pltdoc="x"><span class="techinside">REPL</span></a>, calling <span class="RktSym">distance</span> will
show the result as usual and will also print the result’s type:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">distance</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">pt</span><span class="hspace"> </span><span class="RktVal">0</span><span class="hspace"> </span><span class="RktVal">0</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">pt</span><span class="hspace"> </span><span class="RktVal">3.1415</span><span class="hspace"> </span><span class="RktVal">2.7172</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktOut">- : Real</span></p></td></tr><tr><td><p><span class="RktRes">4.153576541969583</span></p></td></tr></table></blockquote><p>Just evaluating the function name will print the function value and its type,
which can be useful for discovering the types that Typed Racket ascribes to
Racket functions. Alternatively, the <span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=Exploring_Types.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fprims..rkt%2529._%7E3aprint-type%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">:print-type</a></span> command will just
print the type:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktSym">distance</span></td></tr><tr><td><p><span class="RktOut">- : (-> pt pt Real)</span></p></td></tr><tr><td><p><span class="RktRes">#<procedure:distance></span></p></td></tr><tr><td><span class="stt">> </span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string-length%2529%2529&version=6.7" class="RktValLink Sq" data-pltdoc="x">string-length</a></span></td></tr><tr><td><p><span class="RktOut">- : (-> String Index)</span></p></td></tr><tr><td><p><span class="RktRes">#<procedure:string-length></span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=Exploring_Types.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fprims..rkt%2529._%7E3aprint-type%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">:print-type</a></span><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string-ref%2529%2529&version=6.7" class="RktValLink Sq" data-pltdoc="x">string-ref</a></span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktOut">(-> String Integer Char)</span></p></td></tr></table></blockquote><h4 x-source-module="(lib "typed-racket/scribblings/ts-guide.scrbl")" x-source-pkg="typed-racket-doc" x-part-tag=""Datatypes_and_Unions"">2.1<tt> </tt><a name="(part._.Datatypes_and_.Unions)"></a>Datatypes and Unions</h4><p>Many data structures involve multiple variants. In Typed Racket, we
represent these using <span style="font-style: italic">union types</span>, written <span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=type-ref.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fbase-types-extra..rkt%2529._.U%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">U</a></span><span class="stt"> </span><span class="RktSym">t1</span><span class="stt"> </span><span class="RktSym">t2</span><span class="stt"> </span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._......%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">...</a></span><span class="RktPn">)</span>.</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=guide&rel=Module_Syntax.html%23%2528part._hash-lang%2529&version=6.7" class="RktModLink Sq" data-pltdoc="x"><span class="RktMod">#lang</span></a><span class="hspace"> </span><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=index.html&version=6.7" class="RktModLink Sq" data-pltdoc="x"><span class="RktSym">typed/racket</span></a></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=special-forms.html%23%2528form._%2528%2528lib._typed%252Fracket%252Fbase..rkt%2529._define-type%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">define-type</a></span><span class="hspace"> </span><span class="RktSym">Tree</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=type-ref.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fbase-types-extra..rkt%2529._.U%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">U</a></span><span class="hspace"> </span><span class="RktSym">leaf</span><span class="hspace"> </span><span class="RktSym">node</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=special-forms.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fprims..rkt%2529._struct%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">struct</a></span><span class="hspace"> </span><span class="RktSym">leaf</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktPn">[</span><span class="RktSym">val</span><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=special-forms.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fprims..rkt%2529._%7E3a%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">:</a></span><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=type-ref.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fbase-types..rkt%2529._.Number%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">Number</a></span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=special-forms.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fprims..rkt%2529._struct%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">struct</a></span><span class="hspace"> </span><span class="RktSym">node</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktPn">[</span><span class="RktSym">left</span><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=special-forms.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fprims..rkt%2529._%7E3a%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">:</a></span><span class="hspace"> </span><span class="RktSym">Tree</span><span class="RktPn">]</span><span class="hspace"> </span><span class="RktPn">[</span><span class="RktSym">right</span><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=special-forms.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fprims..rkt%2529._%7E3a%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">:</a></span><span class="hspace"> </span><span class="RktSym">Tree</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=special-forms.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fprims..rkt%2529._%7E3a%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">:</a></span><span class="hspace"> </span><span class="RktSym">tree-height</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=type-ref.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fbase-types-extra..rkt%2529._-%7E3e%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><span class="RktSym">Tree</span><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=type-ref.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fbase-types..rkt%2529._.Integer%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">Integer</a></span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=special-forms.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fprims..rkt%2529._define%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">tree-height</span><span class="hspace"> </span><span class="RktSym">t</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._cond%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">cond</a></span><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym">leaf?</span><span class="hspace"> </span><span class="RktSym">t</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktVal">1</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._else%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">else</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._max%2529%2529&version=6.7" class="RktValLink Sq" data-pltdoc="x">max</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%252B%2529%2529&version=6.7" class="RktValLink Sq" data-pltdoc="x">+</a></span><span class="hspace"> </span><span class="RktVal">1</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">tree-height</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">node-left</span><span class="hspace"> </span><span class="RktSym">t</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%252B%2529%2529&version=6.7" class="RktValLink Sq" data-pltdoc="x">+</a></span><span class="hspace"> </span><span class="RktVal">1</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">tree-height</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">node-right</span><span class="hspace"> </span><span class="RktSym">t</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=special-forms.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fprims..rkt%2529._%7E3a%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">:</a></span><span class="hspace"> </span><span class="RktSym">tree-sum</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=type-ref.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fbase-types-extra..rkt%2529._-%7E3e%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><span class="RktSym">Tree</span><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=type-ref.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fbase-types..rkt%2529._.Number%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">Number</a></span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=special-forms.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fprims..rkt%2529._define%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">tree-sum</span><span class="hspace"> </span><span class="RktSym">t</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._cond%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">cond</a></span><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym">leaf?</span><span class="hspace"> </span><span class="RktSym">t</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">leaf-val</span><span class="hspace"> </span><span class="RktSym">t</span><span class="RktPn">)</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._else%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">else</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%252B%2529%2529&version=6.7" class="RktValLink Sq" data-pltdoc="x">+</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">tree-sum</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">node-left</span><span class="hspace"> </span><span class="RktSym">t</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">tree-sum</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">node-right</span><span class="hspace"> </span><span class="RktSym">t</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote><p>In this module, we have defined two new datatypes: <span class="RktSym">leaf</span> and
<span class="RktSym">node</span>. We’ve also defined the type name <span class="RktSym">Tree</span> to be
<span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=ts-reference&rel=type-ref.html%23%2528form._%2528%2528lib._typed-racket%252Fbase-env%252Fbase-types-extra..rkt%2529._.U%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">U</a></span><span class="stt"> </span><span class="RktSym">node</span><span class="stt"> </span><span class="RktSym">leaf</span><span class="RktPn">)</span>, which represents a binary tree of numbers. In
essence, we are saying that the <span class="RktSym">tree-height</span> function accepts
a <span class="RktSym">Tree</span>, which is either a <span class="RktSym">node</span> or a <span class="RktSym">leaf</span>,
and produces a number.</p><p>In order to calculate interesting facts about trees, we have to take
them apart and get at their contents. But since accessors such as
<span class="RktSym">node-left</span> require a <span class="RktSym">node</span> as input, not a
<span class="RktSym">Tree</span>, we have to determine which kind of input we
were passed.</p><p>For this purpose, we use the predicates that come with each defined
structure. For example, the <span class="RktSym">leaf?</span> predicate distinguishes
<span class="RktSym">leaf</span>s from all other Typed Racket values. Therefore, in the
first branch of the <span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._cond%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">cond</a></span> clause in <span class="RktSym">tree-sum</span>, we know
that <span class="RktSym">t</span> is a <span class="RktSym">leaf</span>, and therefore we can get its value
with the <span class="RktSym">leaf-val</span> function.</p><p>In the else clauses of both functions, we know that <span class="RktSym">t</span> is not
a <span class="RktSym">leaf</span>, and since the type of <span class="RktSym">t</span> was <span class="RktSym">Tree</span> by
process of elimination we can determine that <span class="RktSym">t</span> must be a
<span class="RktSym">node</span>. Therefore, we can use accessors such as
<span class="RktSym">node-left</span> and <span class="RktSym">node-right</span> with <span class="RktSym">t</span> as input.</p><p>The process by which Typed Racket type-checks the bodies of the
<span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._cond%2529%2529&version=6.7" class="RktStxLink Sq" data-pltdoc="x">cond</a></span> clauses, using information from the predicate checks,
is called <a href="occurrence-typing.html#%28tech._occurrence._typing%29" class="techoutside" data-pltdoc="x"><span class="techinside">occurrence typing</span></a> and is described in detail in
<a href="occurrence-typing.html" data-pltdoc="x">Occurrence Typing</a>.</p><h4 x-source-module="(lib "typed-racket/scribblings/ts-guide.scrbl")" x-source-pkg="typed-racket-doc" x-part-tag=""Type_Errors"">2.2<tt> </tt><a name="(part._.Type_.Errors)"></a>Type Errors</h4><p>When Typed Racket detects a type error in the module, it raises an
error before running the program.</p><p><div class="SIntrapara">Example:</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/docs/6.7/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._add1%2529%2529&version=6.7" class="RktValLink Sq" data-pltdoc="x">add1</a></span><span class="hspace"> </span><span class="RktVal">"not a number"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktErr">eval:9:0: Type Checker: type mismatch</span></p></td></tr><tr><td><p><span class="RktErr"></span><span class="hspace"> </span><span class="RktErr">expected: Number</span></p></td></tr><tr><td><p><span class="RktErr"></span><span class="hspace"> </span><span class="RktErr">given: String</span></p></td></tr><tr><td><p><span class="RktErr"></span><span class="hspace"> </span><span class="RktErr">in: "not a number"</span></p></td></tr></table></blockquote></div></p><div class="navsetbottom"><span class="navleft"><form class="searchform"><input class="searchbox" style="color: #888;" type="text" value="...search manuals..." title="Enter a search string to search the manuals" onkeypress="return DoSearchKey(event, this, "6.7", "../");" onfocus="this.style.color="black"; this.style.textAlign="left"; if (this.value == "...search manuals...") this.value="";" onblur="if (this.value.match(/^ *$/)) { this.style.color="#888"; this.style.textAlign="center"; this.value="...search manuals..."; }"/></form> <a href="../index.html" title="up to the documentation top" data-pltdoc="x" onclick="return GotoPLTRoot("6.7");">top</a></span><span class="navright"> <a href="quick.html" title="backward to "1 Quick Start"" data-pltdoc="x">← prev</a> <a href="index.html" title="up to "The Typed Racket Guide"" data-pltdoc="x">up</a> <a href="more.html" title="forward to "3 Specifying Types"" data-pltdoc="x">next →</a></span> </div></div></div><div id="contextindicator"> </div></body></html>
|