|
|
|
|
|
by xisukar
2755 days ago
|
|
> In P6, every type that's created using any of its type constructors is a sum type with two sub-types.
> This includes types defined in the standard language such as Int. The sub-types are named D and U.
> For example, to refer to Int's D sub-type, use `Int:D`. Probably your comment hints to this already in which case the following
will be redundant but I wanted to mention them more explicitly. This is
mainly for people unfamiliar with Perl 6. Using the type smileys(:U, :D) are also ways to specifically match against
a type object (`Int`, `Str`, `Rat`, etc.) and an instance object of
a type object (-5, 'Hello', 1/5, etc.): Int ~~ Int:U; # True
-5 ~~ Int:U; # False
-5 ~~ Int:D; # True
Int ~~ Int:D; # False
There is also the :_ smiley which is used by default whenever
the other two aren't specified: Int ~~ Int; # True, same as: Int ~~ Int:_;
-5 ~~ Int; # True, same as: -5 ~~ Int:_;
|
|
Regardless, I'm glad you've written your comment to clarify. :)
I'd be delighted to hear about any of the several big or small mistakes I am about to make in the following.
Also an honest opinion about whether the following approach I take boils down to one that is complete gibberish, vaguely interesting but terribly complex, OK but meh, good but a bit abstract, enlightening, or something else. If you do let me know, please clarify whether you mean what you say for just you, or for what you think might be true for others that don't know P6 at all. TIA.
----
Like a lot of P6 stuff this basic aspect of its type system is both childishly simple but also so general, abstract, and high level, and comes at things from such a different angle than pretty much all other languages, that it can be difficult to explain.
Another attempt at what I was trying to get at in my second footnote in my GP comment...
----
A P6 type and its two standard sub-types are a simple and fun unification of the universal/existential "quantification" that our inner logician loves and the "article definiteness" distinction recognized by our linguistic side.
From https://en.wikipedia.org/wiki/Article_(grammar):
> The articles in English grammar are "the" and "a/an" ... In languages that employ articles, every common noun, with some exceptions, is expressed with a certain definiteness, definite or indefinite
From https://en.wikipedia.org/wiki/Quantifier_(logic):
> The two most common quantifiers mean "for all" [Universal] and "there exists" [Existential].
----
:D denotes a definite thing.
It is directly analogous to "the".
The value `42` is considered definite.
Definiteness implies that a thing actually exists. Its the linguistic analog of existential quantification.
Many coders talk about happy/sad path processing. :D is associated with the happy path, perhaps because it has no existential crisis. :D
----
:U is related to Universal quantification.
As a type, it is also related to "a". Quoting wikipedia:
> An indefinite article indicates that its noun is not a particular one identifiable to the listener.
:U corresponds to an indefinite thing, something that's only identifiable as a Universal, not a Definite thing.
:U is also associated with Undefined things, even if they are supposed to be undefined.
In the context of talk about happy/sad path processing, :U is associated with the Unhappy path, because it can denote a thing that should be definite but isn't.
----
As a value, a type name without a `:D` or `:U` smiley is the same as the type name with a `:U`.
As a type, a type name without a smiley is the same as the type name with a `:_`, where the `_` denotes either `D` or `U`.
Thus:
* `Int`, as a thing in of itself, is the same as an `Int:U`.
* `Int`, as a type of thing, denotes either an `Int:U` or an `Int:D`.