<ulclass="crumbs"><li><ahref="../index.html">Home</a></li><li><ahref="../services.html">Services</a></li><li><ahref="index.html">kinds</a></li><li><ahref="index.html#P">Preliminaries</a></li><li><b>What This Module Does</b></li></ul></div>
<ulclass="toc"><li><ahref="P-wtmd.html#SP1">§1. Prerequisites</a></li><li><ahref="P-wtmd.html#SP2">§2. Kinds, definiteness, and safety</a></li><li><ahref="P-wtmd.html#SP5">§5. Kinds and knowledge</a></li><li><ahref="P-wtmd.html#SP6">§6. A strongly typed language mixing static and dynamic typing</a></li><li><ahref="P-wtmd.html#SP7">§7. Dimensional analysis</a></li><li><ahref="P-wtmd.html#SP8">§8. Conformance and compatibility</a></li><li><ahref="P-wtmd.html#SP10">§10. Kind variables</a></li><li><ahref="P-wtmd.html#SP11">§11. The kinds-test REPL</a></li><li><ahref="P-wtmd.html#SP13">§13. Overview of facilities</a></li></ul><hrclass="tocbar">
<pclass="commentary firstcommentary"><aid="SP1"class="paragraph-anchor"></a><b>§1. Prerequisites. </b>The kinds module is a part of the Inform compiler toolset. It is
full details, but essentially: it's C without predeclarations or header files,
and where functions have names like <spanclass="extract"><spanclass="extract-syntax">Tags::add_by_name</span></span> rather than <spanclass="extract"><spanclass="extract-syntax">add_by_name</span></span>.
<pclass="commentary firstcommentary"><aid="SP2"class="paragraph-anchor"></a><b>§2. Kinds, definiteness, and safety. </b>To begin, an overview of the type system used by Inform, since this module
<pclass="commentary">Inform is like most programming languages in that it deals with a rich
variety of values, that is, individual pieces of data. The number
17, the time "3:15 PM" and the "Entire Game" (a named scene) are all
examples of values. Except that Inform uses the word "kind" rather than
"type" for the different sorts of values which exist, I have tried to
follow conventional computer-science terminology in this source code.<supid="fnref:1"><ahref="#fn:1"rel="footnote">1</a></sup>
</p>
<pclass="commentary">Kinds such as <spanclass="extract"><spanclass="extract-syntax">number</span></span> are "definite", in that they unambiguously say what
format a piece of data has. If the compiler can prove that a value has a
definite kind, it knows exactly how to print it, initialise it and so on.
Variables, constants, literal values and properties all have definite kinds.
</p>
<pclass="commentary">But other kinds, such as <spanclass="extract"><spanclass="extract-syntax">arithmetic value</span></span>, merely express a guarantee
that a value can be used in some way. These are "indefinite". In some
contemporary languages this latter meaning would be a "typeclass"
(e.g., Haskell) or "protocol" (e.g., Swift) but not a "type".<supid="fnref:2"><ahref="#fn:2"rel="footnote">2</a></sup> The
ultimate in indefiniteness is the kind <spanclass="extract"><spanclass="extract-syntax">value</span></span>, which expresses only that
something is a piece of data. Phrase tokens can be indefinite, as this
example shows:
</p>
<blockquote>
<p>To display (X - an arithmetic value):</p>
</blockquote>
<ulclass="footnotetexts"><liclass="footnote"id="fn:1"><pclass="inwebfootnote"><supid="fnref:1"><ahref="#fn:1"rel="footnote">1</a></sup> See for instance definitions in Michael L. Scott, "Programming Language
Pragmatics" (second edition, 2006), chapter 7. We will refer to "kind checking"
and "kind compatibility" rather than "type checking" and "type compatibility",
for example.
<ahref="#fnref:1"title="return to text">↩</a></p></li><liclass="footnote"id="fn:2"><pclass="inwebfootnote"><supid="fnref:2"><ahref="#fn:2"rel="footnote">2</a></sup> Swift syntax blurs this distinction and (rightly) encourages users to
make use of protocols in place of types in, for example, function parameters.
We shall do the same.
<ahref="#fnref:2"title="return to text">↩</a></p></li></ul>
<pclass="commentary firstcommentary"><aid="SP3"class="paragraph-anchor"></a><b>§3. </b>The virtue of knowing that a piece of data has a given kind is that one
<pclass="commentary firstcommentary"><aid="SP4"class="paragraph-anchor"></a><b>§4. </b>One caveat: Inform provides low-level features allowing Inter code to be
<pclass="commentary">This is a legal C function but the deliberate disregard of type safety — in
the use of the <spanclass="extract"><spanclass="extract-syntax">(char *)</span></span> cast notation — is a kind of waiver, where the
author chooses to accept the risk. In a similar way, there are no victims of
Inform's <spanclass="extract"><spanclass="extract-syntax">(-</span></span> and <spanclass="extract"><spanclass="extract-syntax">-)</span></span> notation, only volunteers.
<pclass="commentary firstcommentary"><aid="SP5"class="paragraph-anchor"></a><b>§5. Kinds and knowledge. </b>Inform uses the kinds system when building its world model of knowledge, and
not only to monitor specific computational operations. For example, if the
source text says:
</p>
<blockquote>
<p>A wheelbarrow is a kind of vehicle. The blue garden barrow is a wheelbarrow.</p>
</blockquote>
<pclass="commentary">then the value "blue garden barrow" has kind <spanclass="extract"><spanclass="extract-syntax">wheelbarrow</span></span>, which is
within <spanclass="extract"><spanclass="extract-syntax">vehicle</span></span>, within <spanclass="extract"><spanclass="extract-syntax">thing</span></span>, within <spanclass="extract"><spanclass="extract-syntax">object</span></span>. As this example suggests,
knowledge and property ownership passes through a single-inheritance hierarchy;
that is, each kind inherits directly from only one other kind.
<pclass="commentary firstcommentary"><aid="SP6"class="paragraph-anchor"></a><b>§6. A strongly typed language mixing static and dynamic typing. </b>Programming languages with types are often classified by two criteria.
<pclass="commentary firstcommentary"><aid="SP7"class="paragraph-anchor"></a><b>§7. Dimensional analysis. </b>Inform subjects all calculations with its "quasinumerical" kinds — basically,
all those on which calculation can be performed — to dimensional checking.
</p>
<pclass="commentary">Dimension in this sense is a term drawn from physics. The idea is that when
quantities are multiplied together, their natures are combined as well as
the actual numbers involved. For instance, in
$$ v = f\lambda $$
if the frequency \(f\) of a wave is measured in Hz (counts per second), and
the wavelength \(\lambda\) in m, then the velocity \(v\) must be measured
in m/s: and that is indeed a measure of velocity, so this looks right.
We can tell that the formula
$$ v = f^2\lambda $$
must be wrong because it would result in an acceleration. Physicists use the
term "dimensions" much as computer-scientists use the word "type", and Inform
follows suit.
</p>
<pclass="commentary">See <ahref="3-dmn.html"class="internal">Dimensions</a> for a much fuller discussion.
</p>
<pclass="commentary firstcommentary"><aid="SP8"class="paragraph-anchor"></a><b>§8. Conformance and compatibility. </b>One kind \(K\) "conforms to" another kind \(L\) if values of \(K\) can always be used
where values of \(L\) are expected. For example, in a typical work of IF produced
by Inform, the kind <spanclass="extract"><spanclass="extract-syntax">vehicle</span></span> conforms to <spanclass="extract"><spanclass="extract-syntax">thing</span></span>. This idea can also apply
to kinds of kinds: <spanclass="extract"><spanclass="extract-syntax">number</span></span> conforms to <spanclass="extract"><spanclass="extract-syntax">arithmetic value</span></span> which conforms to
<spanclass="extract"><spanclass="extract-syntax">sayable value</span></span>, for example. See <ahref="2-tlok.html"class="internal">The Lattice of Kinds</a> for how conformance
produces a hierarchical order among possible kinds.
<pclass="commentary">Conformance is an "is-a" relationship: thus a <spanclass="extract"><spanclass="extract-syntax">vehicle</span></span> can safely be stored in
a variable of kind <spanclass="extract"><spanclass="extract-syntax">thing</span></span> because a vehicle is a thing. But a <spanclass="extract"><spanclass="extract-syntax">number</span></span> cannot
be stored in a <spanclass="extract"><spanclass="extract-syntax">real number</span></span> variable directly — integers and real numbers
have completely different data representations at run-time, so the compiler
must generate conversion code (a "cast") to adapt the <spanclass="extract"><spanclass="extract-syntax">number</span></span> value before
it is stored. Sometimes this is possible, sometimes not. A kind \(K\) is
"compatible" with \(L\) if it is. Clearly conformance implies compatibility,
<pclass="commentary firstcommentary"><aid="SP9"class="paragraph-anchor"></a><b>§9. </b>The kind <spanclass="extract"><spanclass="extract-syntax">object</span></span> is of great significance to Inform, partly for historical
<pclass="commentary">But not to us. In the <ahref="index.html"class="internal">kinds</a> module, <spanclass="extract"><spanclass="extract-syntax">object</span></span> is a kind like any other. It
<pclass="commentary firstcommentary"><aid="SP10"class="paragraph-anchor"></a><b>§10. Kind variables. </b>The 26 letters A to Z, written in upper case, can serve as kind variables —
<pclass="commentary">The meaning of text like "list of K" depends on context. If K is currently set to,
say, <spanclass="extract"><spanclass="extract-syntax">number</span></span>, then "list of K" means <spanclass="extract"><spanclass="extract-syntax">list of number</span></span>; if it has no current
setting, then K remains a placeholder and the result is <spanclass="extract"><spanclass="extract-syntax">list of K</span></span>. Note that:
<ulclass="items"><li>(a) The same variable can occur more than once, as in <spanclass="extract"><spanclass="extract-syntax">phrase K -> K</span></span>.
</li><li>(b) Variables can be constrained to conform to something, as in <spanclass="extract"><spanclass="extract-syntax">arithmetic value of kind K</span></span>,
where <spanclass="extract"><spanclass="extract-syntax">K</span></span> remains a placeholder but can only be a kind conforming to <spanclass="extract"><spanclass="extract-syntax">arithmetic value</span></span>.
</li><li>(c) If <spanclass="extract"><spanclass="extract-syntax">K</span></span> remains unknown then any kind using <spanclass="extract"><spanclass="extract-syntax">K</span></span> is necessarily indefinite.
So a variable cannot have the kind <spanclass="extract"><spanclass="extract-syntax">list of K</span></span>, for example.
</li><li>(d) A process called "substitution" enables <spanclass="extract"><spanclass="extract-syntax">list of K</span></span> to be transformed to
<spanclass="extract"><spanclass="extract-syntax">list of numbers</span></span>, or whatever may be. See <ahref="2-knd.html#SP17"class="internal">Kinds::substitute</a>.
</li></ul>
<ulclass="footnotetexts"><liclass="footnote"id="fn:3"><pclass="inwebfootnote"><supid="fnref:3"><ahref="#fn:3"rel="footnote">3</a></sup> Using letters seemed the nearest point of contact with natural
language conventions. In English, we do say pseudo-algebraic things like
"So, let's call our spy Mr X." — or at least we do if we lead slightly
more exciting lives than the present author. The use of letters emphasises
that this is some kind of reference, not a direct identification.
<ahref="#fnref:3"title="return to text">↩</a></p></li><liclass="footnote"id="fn:4"><pclass="inwebfootnote"><supid="fnref:4"><ahref="#fn:4"rel="footnote">4</a></sup> At one time I was tempted by the syntax used in the early functional
programming language Miranda (1985), which uses rows of asterisks <spanclass="extract"><spanclass="extract-syntax">*</span></span>, <spanclass="extract"><spanclass="extract-syntax">**</span></span>,
<spanclass="extract"><spanclass="extract-syntax">***</span></span>, and so on as needed — a syntax making clear that nobody expected
to see many of them at once. But asterisks in natural language have an air
of censorship, of something that must not be named: compare Stéphanie de
Genlis's gothic novella "Histoire de la duchesse de C***" (1782).
<ahref="#fnref:4"title="return to text">↩</a></p></li></ul>
<pclass="commentary firstcommentary"><aid="SP11"class="paragraph-anchor"></a><b>§11. The kinds-test REPL. </b>The <ahref="index.html"class="internal">kinds</a> module provides the Inform type system as a stand-alone utility,
<spanclass="plain-syntax">'</span><spanclass="element-syntax">list of texts</span><spanclass="plain-syntax">':</span><spanclass="string-syntax"> list of texts</span>
<spanclass="plain-syntax">'</span><spanclass="element-syntax">phrase number -> text</span><spanclass="plain-syntax">':</span><spanclass="string-syntax"> phrase number -> text</span>
<spanclass="plain-syntax">'</span><spanclass="element-syntax">relation of numbers to truth states</span><spanclass="plain-syntax">':</span><spanclass="string-syntax"> relation of numbers to truth states</span>
<spanclass="plain-syntax">'</span><spanclass="element-syntax">relation of numbers to numbers</span><spanclass="plain-syntax">':</span><spanclass="string-syntax"> relation of numbers</span>
<pclass="commentary firstcommentary"><aid="SP12"class="paragraph-anchor"></a><b>§12. </b>In <ahref="../kinds-test/index.html"class="internal">kinds-test</a>, the 26 variables are initially unset, but can be given
<spanclass="plain-syntax">'</span><spanclass="element-syntax">list of relations of Q to R</span><spanclass="plain-syntax">':</span><spanclass="string-syntax"> list of relations of Qs to Rs</span>
<spanclass="plain-syntax">'</span><spanclass="element-syntax">substitute number for Q in Q</span><spanclass="plain-syntax">':</span><spanclass="string-syntax"> number</span>
<spanclass="plain-syntax">'</span><spanclass="element-syntax">substitute number for Q in list of Q</span><spanclass="plain-syntax">':</span><spanclass="string-syntax"> list of numbers</span>
<spanclass="plain-syntax">'</span><spanclass="element-syntax">substitute number for Q in list of relations of Q to Q</span><spanclass="plain-syntax">':</span><spanclass="string-syntax"> list of relations of numbers</span>
<spanclass="plain-syntax">'</span><spanclass="element-syntax">X = relation of Y to Y</span><spanclass="plain-syntax">':</span><spanclass="string-syntax"> relation of Ys</span>
<spanclass="plain-syntax">'</span><spanclass="element-syntax">X is definite?</span><spanclass="plain-syntax">':</span><spanclass="string-syntax"> false</span>
<spanclass="plain-syntax">'</span><spanclass="element-syntax">X = relation of numbers to numbers</span><spanclass="plain-syntax">':</span><spanclass="string-syntax"> relation of numbers</span>
<spanclass="plain-syntax">'</span><spanclass="element-syntax">X is definite?</span><spanclass="plain-syntax">':</span><spanclass="string-syntax"> true</span>
<pclass="commentary firstcommentary"><aid="SP13"class="paragraph-anchor"></a><b>§13. Overview of facilities. </b>A kind is represented by a <spanclass="extract"><spanclass="extract-syntax">kind *</span></span> pointer. These actually point to
small trees of <ahref="2-knd.html#SP1"class="internal">kind</a> objects — see <ahref="index.html"class="internal">kinds</a>— because many kinds are
constructed out of others: thus <spanclass="extract"><spanclass="extract-syntax">list of texts</span></span> is the result of applying the
"list of ..." construction to the kind <spanclass="extract"><spanclass="extract-syntax">text</span></span>.<supid="fnref:5"><ahref="#fn:5"rel="footnote">5</a></sup> Kinds not constructed from
other kinds are called "base kinds". Briefly:
</p>
<ulclass="items"><li>● By convention the <spanclass="extract"><spanclass="extract-syntax">NULL</span></span> pointer means "kind unknown".
</li><li>● Commonly needed base kinds, like <spanclass="extract"><spanclass="extract-syntax">number</span></span> or <spanclass="extract"><spanclass="extract-syntax">text</span></span>, have global variables
set equal to them, like <spanclass="extract"><spanclass="extract-syntax">K_number</span></span> or <spanclass="extract"><spanclass="extract-syntax">K_text</span></span>. See <ahref="2-fk.html"class="internal">Familiar Kinds</a>.
</li><li>● Kinds can otherwise be made with <ahref="2-knd.html#SP6"class="internal">Kinds::base_construction</a>,
<spanclass="extract"><spanclass="extract-syntax">list of numbers</span></span> and <spanclass="extract"><spanclass="extract-syntax">relation of numbers to texts</span></span> can be made by:
<ulclass="items"><li>● Kinds for functions are a bit laborious to put together, so <ahref="2-knd.html#SP7"class="internal">Kinds::function_kind</a>
is a convenience.
</li><li>● As with kinds, commonly needed constructors, like <spanclass="extract"><spanclass="extract-syntax">CON_list_of</span></span> or
<spanclass="extract"><spanclass="extract-syntax">CON_relation</span></span>, are available as global values. Again see <ahref="2-fk.html"class="internal">Familiar Kinds</a>.
</li><li>● Two different <spanclass="extract"><spanclass="extract-syntax">kind *</span></span> values can represent the same kind, so don't test
whether \(K\) is the same kind as \(L\) by the pointer comparison <spanclass="extract"><spanclass="extract-syntax">K == L</span></span>. Instead
call <ahref="2-knd.html#SP24"class="internal">Kinds::eq</a> or its negation <ahref="2-knd.html#SP24"class="internal">Kinds::ne</a>.
</li><li>● Call <ahref="2-knd.html#SP25"class="internal">Kinds::conforms_to</a> to test whether \(K\) conforms to \(L\). This is
either true or not true. To find a kind able to hold values of either \(K\) or \(L\),
</li><li>● Call <ahref="2-knd.html#SP25"class="internal">Kinds::compatible</a> to test whether \(K\) is compatible with \(L\),
but note that the reply is three-valued: always, sometimes or never.
</li><li>● Inform makes frequent use of "weakening", where we deliberately weaken a
kind (i.e., make it less restrictive) by ignoring distinctions between subkinds
of some \(W\). For example, the weakening of <spanclass="extract"><spanclass="extract-syntax">list of things</span></span> with respect to
<spanclass="extract"><spanclass="extract-syntax">object</span></span> is <spanclass="extract"><spanclass="extract-syntax">list of objects</span></span>. See <ahref="2-knd.html#SP18"class="internal">Kinds::weaken</a>.
</li><li>● An extensive API of functions is provided in <ahref="2-uk.html"class="internal">Using Kinds</a> to test whether
given kinds have given properties. The most important is <ahref="2-uk.html#SP4"class="internal">Kinds::Behaviour::definite</a>,
which determines whether \(K\) is definite.
</li><li>● New base kinds can be created either by calling <ahref="2-knd.html#SP20"class="internal">Kinds::new_base</a>,<supid="fnref:6"><ahref="#fn:6"rel="footnote">6</a></sup> or in
the <ahref="4-kc2.html"class="internal">Kind Constructors</a> section.<supid="fnref:8"><ahref="#fn:8"rel="footnote">8</a></sup> See <ahref="4-abgtn.html"class="internal">A Brief Guide to Neptune</a> for a
<ulclass="footnotetexts"><liclass="footnote"id="fn:5"><pclass="inwebfootnote"><supid="fnref:5"><ahref="#fn:5"rel="footnote">5</a></sup> "List of ..." is what is called a "kind constructor". This term follows the
traditional usage of "type constructor", but note that Haskell and some other
functional languages mean something related but different by this.
<ahref="#fnref:5"title="return to text">↩</a></p></li><liclass="footnote"id="fn:6"><pclass="inwebfootnote"><supid="fnref:6"><ahref="#fn:6"rel="footnote">6</a></sup> So, for example, Inform acts on text like "A weight is a kind of value." by
<ahref="#fnref:6"title="return to text">↩</a></p></li><liclass="footnote"id="fn:7"><pclass="inwebfootnote"><supid="fnref:7"><ahref="#fn:7"rel="footnote">7</a></sup> Inform's built-in kinds like <spanclass="extract"><spanclass="extract-syntax">number</span></span> or <spanclass="extract"><spanclass="extract-syntax">text</span></span> all come from such files,
not by calls to <ahref="2-knd.html#SP20"class="internal">Kinds::new_base</a>.
<ahref="#fnref:7"title="return to text">↩</a></p></li><liclass="footnote"id="fn:8"><pclass="inwebfootnote"><supid="fnref:8"><ahref="#fn:8"rel="footnote">8</a></sup> Inform stores Neptune files inside kits of Inter, because in practice
built-in kinds always need run-time support written in Inter code, so the two
<ahref="#fnref:8"title="return to text">↩</a></p></li><liclass="footnote"id="fn:9"><pclass="inwebfootnote"><supid="fnref:9"><ahref="#fn:9"rel="footnote">9</a></sup> For instance, after "Puzzle is a kind of thing. Toy is a kind of thing.
Puzzle is a kind of toy.", Inform moves <spanclass="extract"><spanclass="extract-syntax">puzzle</span></span> to be a subkind of <spanclass="extract"><spanclass="extract-syntax">toy</span></span>,
when it had been created as a subkind of <spanclass="extract"><spanclass="extract-syntax">thing</span></span>. It is very arguable that
allowing this is a bad idea, but that ship has sailed.
<ahref="#fnref:9"title="return to text">↩</a></p></li></ul>