Does Perl 6 have an infinite Int?

牧云@^-^@ 提交于 2019-12-05 02:17:00

{new intro that's hopefully better than the unhelpful/misleading original one}

@CarlMäsak, in a comment he wrote below this answer after my first version of it:

Last time I talked to Larry about this {in 2014}, his rationale seemed to be that ... Inf should work for all of Int, Num and Str

(The first version of my answer began with a "recollection" that I've concluded was at least unhelpful and plausibly an entirely false memory.)

In my research in response to Carl's comment, I did find one related gem in #perl6-dev in 2016 when Larry wrote:

then our policy could be, if you want an Int that supports ±Inf and NaN, use Rat instead

in other words, don't make Rat consistent with Int, make it consistent with Num

Larry wrote this post 6.c. I don't recall seeing anything like it discussed for 6.d.

{and now back to the rest of my first answer}


Num in P6 implements the IEEE 754 floating point number type. Per the IEEE spec this type must support several concrete values that are reserved to stand in for abstract concepts, including the concept of positive infinity. P6 binds the corresponding concrete value to the term Inf.

Given that this concrete value denoting infinity already existed, it became a language wide general purpose concrete value denoting infinity for cases that don't involve floating point numbers such as conveying infinity in string and list functions.


The solution to your problem that I propose below is to use a where clause via a subset.

A where clause allows one to specify run-time assignment/binding "typechecks". I quote "typecheck" because it's the most powerful form of check possible -- it's computationally universal and literally checks the actual run-time value (rather than a statically typed view of what that value can be). This means they're slower and run-time, not compile-time, but it also makes them way more powerful (not to mention way easier to express) than even dependent types which are a relatively cutting edge feature that those who are into advanced statically type-checked languages tend to claim as only available in their own world1 and which are intended to "prevent bugs by allowing extremely expressive types" (but good luck with figuring out how to express them... ;)).

A subset declaration can include a where clause. This allows you to name the check and use it as a named type constraint.

So, you can use these two features to get what you want:

subset Int-or-Inf where Int:D | Inf;

Now just use that subset as a type:

my Int-or-Inf $foo; # ($foo contains `Int-or-Inf` type object) 
$foo = 99999999999; # works
$foo = Inf;         # works
$foo = Int-or-Inf;  # works
$foo = Int;         # typecheck failure
$foo = 'a';         # typecheck failure

1. See Does Perl 6 support dependent types? and it seems the rough consensus is no.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!