Mailing List Archive

1 2 3  View All
Re: PPC Elevator Pitch for Perl::Types [ In reply to ]
* Corwin Brust <corwin@bru.st> [2023-08-22 19:51:42 -0500]:

> On Tue, Aug 22, 2023 at 5:34?PM Oodler 577 via perl5-porters
> <perl5-porters@perl.org> wrote:
> >
> > Nobody actually knows what Oshun is proposing yet, even at this year's
> > TPFC, Curtis was still admittedly in the design phase. This is also indicated
> > by the nature of the discussion. I am not sure a specification even exists.
> >
>
> Pardon me for asking, but are you calling Curtis a liar?
> Otherwise, I must assume you are behind on your emails as I was able to find,
> only slightly upthread, where Curtis says:

Not here, no. How is that.

I called him a liar in another thread, but about his characterization
of Perl::Types implying it was vaporware. We've discussed this.

Oshun is clearly in flux, have you seen Curtis' keynote? He says
as much. Maybe not in those words, but it's almost entirely
speculative.

Here's the part of the talk that Curtis presented as a budding
thought, and which made it clear to Will (and I) that Perl::Types
needed to be brought up now. Not later. Will and I were both sitting
in the room watching the talk. You will hear at some point in the
video, Will is commenting to Curtis that Perl already has a type
system. That type system is not Perl::Types, but Perl::Types takes
advantage of the existing "types" system. Therefore it's a hugely
missed opportunity to ignore that it exists. And it is real.

https://youtu.be/uT0S-jfO1mE?feature=shared&t=2002

It's a good talk, I suggest you watch the full thing.

>
> > We offer:
> > [...]
> > A full, draft spec which needs to be updated when we agree on syntax
>
> After a brief conversation with a web-search tool about "perl Oshun
> specification", I was quickly reading:
>
> https://github.com/Perl-Apollo/oshun/blob/main/lib/Data/Checks/Specification.pod
>
> What sort of research did you do before making the above statement?

I follow the conversations on their Github discussion forum.

Had not seen the "spec". But even if I had, it just confirms what
I was saying. This is not knock against the project, it's in a very
early phase. That's why Perl::Types is getting brought up now.
The Oshun spec, says:

=head1 STATUS

This is very early alpha code, designed to prototype an idea that might one
day be a core feature of Perl.

Every aspect of this module is subject to sudden and radical change,
from the check syntax itself, to the names of the various checks,
to their essential behaviours, features, and limitations.

B<DO I<NOT> USE THIS MODULE IN PRODUCTION CODE.>

>
> I ask because you have recently reminded us of our responsibilities
> toward quality of discourse, here on p5p.

Thank you for pointing out Curtis shared the link.

Cheers,
Brett

>
> > FWIW, I found this module linked on the Wikipedia page for DsC, which is from
>
> Thanks.
>

--
--
oodler@cpan.org
oodler577@sdf-eu.org
SDF-EU Public Access UNIX System - http://sdfeu.org
irc.perl.org #openmp #pdl #native
Re: PPC Elevator Pitch for Perl::Types [ In reply to ]
* Christian Walde <walde.christian@gmail.com> [2023-08-23 00:53:12 +0200]:

> On Wed, 23 Aug 2023 00:37:20 +0200, Oodler 577 <oodler577@sdf-eu.org> wrote:
>
> > * Leon Timmermans <fawaka@gmail.com> [2023-08-23 00:29:39 +0200]:
> >
> > > On Wed, Aug 23, 2023 at 12:22?AM Christian Walde <walde.christian@gmail.com>
> > > wrote:
> > >
> > > > that said: is there a known pause policy regarding your identified
> > > > behavior of squatting?
> > >
> > > ?Perl reserves all lowercase namespaces for pragmas. That doesn't mean you
> > > can't write a pragma, but you should get the blessing of p5p (
> > > perl5-porters@perl.org).? [1]
>
> that is a quite different thing from a squatting policy, but very applicable here, thanks leon :)
>
> > Or as it were, for 21 years, PAUSE.
>
> what does this mean?

I didn't think that p5p controlled CPAN, PAUSE Admins (or whatever they are called) do.

>
> > > So there is a contingency policy for namespaces like these, though I don't
> > > think we've ever used it.
> >
> > Sure let me know when you do something about types.pm.
>
> the order of operations here is somewhat different
>
> optimally you would've presented a proposal, gained concensus on it, and with agreement have uploaded a distribution to CPAN, thus grabbing the namespaces will did already grab ( https://pause.perl.org/pause/authenquery?ACTION=peek_perms&pause99_peek_perms_by=a&pause99_peek_perms_query=wbraswell&pause99_peek_perms_sub=Submit )
>
> (you did not do that, you just grabbed them without asking anyone)
>
> then, as well with said concensus, contacted pause admins to enact said concensus on the permissions of items conflicting with yours
>
> if you are asking for pre-concensus unilateral action, then your perl-types dist would currently also be a target
>
> otherwise you will need to ask for this to be enacted, and once concensus has given the go-ahead, you will receive the appropriate replies

Your turn to explain what you're trying to tell me.

Cheers,
Brett

> --
> With regards,
> Christian Walde
>

--
--
oodler@cpan.org
oodler577@sdf-eu.org
SDF-EU Public Access UNIX System - http://sdfeu.org
irc.perl.org #openmp #pdl #native
Re: PPC Elevator Pitch for Perl::Types [ In reply to ]
On Wed, 23 Aug 2023 04:14:03 +0200, Oodler 577 <oodler577@sdf-eu.org> wrote:

>> > > So there is a contingency policy for namespaces like these, though I don't
>> > > think we've ever used it.
>> >
>> > Sure let me know when you do something about types.pm.
>>
>> the order of operations here is somewhat different
>>
>> [...]
>
> Your turn to explain what you're trying to tell me.

unless you do something different, you won't see anything being done

--
With regards,
Christian Walde
Re: PPC Elevator Pitch for Perl::Types [ In reply to ]
On 2023-08-22 11:43 a.m., Oodler 577 via perl5-porters wrote:
> ... your DbC efforts
> (which btw do not conflict with Perl::Types, a point we've made).
> Perhaps you can show where Perl::Types, as we've demonstrated would
> conflict with Oshun. I am not suggesting they are directly composable,
> but Perl::Types could definitely assist you where otherwise you'd
> just be using regular expressions everywhere (behind some sugar,
> of course).

That last thing you said about "otherwise you'd just be using regular
expressions everywhere" seems like an unfounded assumption to me.

While Perl::Types could serve as a foundation for Oshun, there's also no reason
at all that an Oshun implementation couldn't be written against Perl directly
AND Oshun would use exactly the same methods Perl::Types would to access the
"real" Perl type system directly and NOT using regular expressions.

It is true that some other type implementations in Perl use regular expressions,
but Oshun is a design spec for user-visible syntax and behavior, and there's no
reason at all it would necessarily be implemented with regular expressions.

In fact I would assume the opposite, that an Oshun implementation being more
modern would attach to core Perl very closely and exploit directly everything
that Perl::Types would, and have the same performance potential.

I would expect Oshun to follow the same kind of path Corinna did, which yielded
a very tight and efficient implementation in principle.

-- Darren Duncan
Re: PPC Elevator Pitch for Perl::Types [ In reply to ]
* Darren Duncan <darren@darrenduncan.net> [2023-08-22 19:44:50 -0700]:

> On 2023-08-22 11:43 a.m., Oodler 577 via perl5-porters wrote:
> > ... your DbC efforts
> > (which btw do not conflict with Perl::Types, a point we've made).
> > Perhaps you can show where Perl::Types, as we've demonstrated would
> > conflict with Oshun. I am not suggesting they are directly composable,
> > but Perl::Types could definitely assist you where otherwise you'd
> > just be using regular expressions everywhere (behind some sugar,
> > of course).
>
> That last thing you said about "otherwise you'd just be using regular
> expressions everywhere" seems like an unfounded assumption to me.
>
> While Perl::Types could serve as a foundation for Oshun, there's also no
> reason at all that an Oshun implementation couldn't be written against Perl
> directly AND Oshun would use exactly the same methods Perl::Types would to
> access the "real" Perl type system directly and NOT using regular
> expressions.
>
> It is true that some other type implementations in Perl use regular
> expressions, but Oshun is a design spec for user-visible syntax and
> behavior, and there's no reason at all it would necessarily be implemented
> with regular expressions.
>
> In fact I would assume the opposite, that an Oshun implementation being more
> modern would attach to core Perl very closely and exploit directly
> everything that Perl::Types would, and have the same performance potential.
>
> I would expect Oshun to follow the same kind of path Corinna did, which
> yielded a very tight and efficient implementation in principle.

Why would you expect that?

Cheers,
Brett

>
> -- Darren Duncan
>

--
--
oodler@cpan.org
oodler577@sdf-eu.org
SDF-EU Public Access UNIX System - http://sdfeu.org
irc.perl.org #openmp #pdl #native
Re: PPC Elevator Pitch for Perl::Types [ In reply to ]
On 2023-08-22 8:13 p.m., Oodler 577 via perl5-porters wrote:
>> I would expect Oshun to follow the same kind of path Corinna did, which
>> yielded a very tight and efficient implementation in principle.
>
> Why would you expect that?

I would expect Oshun to not use regular expressions to evaluate fundamental
types because that is the best solution. If Perl::Types can find a better way
than using regular expressions, then Oshun can apply the same better solution
directly as well. Why would this not be a reasonable assumption?

-- Darren Duncan
Re: PPC Elevator Pitch for Perl::Types [ In reply to ]
Oshun has undergone a great deal of change and development since TPRC,
that's how fast it's moving. Using the keynote as an indicator to the
progress of the project misses quite a lot of work/discussion that's
happened since.

Avery

On Wed, 23 Aug 2023 at 13:41, Oodler 577 via perl5-porters <
perl5-porters@perl.org> wrote:

> * Corwin Brust <corwin@bru.st> [2023-08-22 19:51:42 -0500]:
>
> > On Tue, Aug 22, 2023 at 5:34?PM Oodler 577 via perl5-porters
> > <perl5-porters@perl.org> wrote:
>
> Oshun is clearly in flux, have you seen Curtis' keynote? He says
> as much. Maybe not in those words, but it's almost entirely
> speculative.
>
Re: PPC Elevator Pitch for Perl::Types [ In reply to ]
* Avery Adams <oldtechaa@gmail.com> [2023-08-23 15:45:09 +1200]:

> Oshun has undergone a great deal of change and development since TPRC,
> that's how fast it's moving. Using the keynote as an indicator to the
> progress of the project misses quite a lot of work/discussion that's
> happened since.

Thank you, that's my point.

Brett

>
> Avery
>
> On Wed, 23 Aug 2023 at 13:41, Oodler 577 via perl5-porters <
> perl5-porters@perl.org> wrote:
>
> > * Corwin Brust <corwin@bru.st> [2023-08-22 19:51:42 -0500]:
> >
> > > On Tue, Aug 22, 2023 at 5:34?PM Oodler 577 via perl5-porters
> > > <perl5-porters@perl.org> wrote:
> >
> > Oshun is clearly in flux, have you seen Curtis' keynote? He says
> > as much. Maybe not in those words, but it's almost entirely
> > speculative.
> >

--
--
oodler@cpan.org
oodler577@sdf-eu.org
SDF-EU Public Access UNIX System - http://sdfeu.org
irc.perl.org #openmp #pdl #native
Re: PPC Elevator Pitch for Perl::Types [ In reply to ]
* Darren Duncan <darren@darrenduncan.net> [2023-08-22 20:29:38 -0700]:

> On 2023-08-22 8:13 p.m., Oodler 577 via perl5-porters wrote:
> > > I would expect Oshun to follow the same kind of path Corinna did, which
> > > yielded a very tight and efficient implementation in principle.
> >
> > Why would you expect that?
>
> I would expect Oshun to not use regular expressions to evaluate fundamental
> types because that is the best solution. If Perl::Types can find a better
> way than using regular expressions, then Oshun can apply the same better
> solution directly as well. Why would this not be a reasonable assumption?

I still don't follow. It's okay, I know poeple are dead sick of this thread.

Cheers,
Brett

>
> -- Darren Duncan

--
--
oodler@cpan.org
oodler577@sdf-eu.org
SDF-EU Public Access UNIX System - http://sdfeu.org
irc.perl.org #openmp #pdl #native
Re: PPC Elevator Pitch for Perl::Types [ In reply to ]
On Wed, Aug 23, 2023 at 12:21?AM Avery Adams <oldtechaa@gmail.com> wrote:
>
> Resending to list.
>
> ---------- Forwarded message ---------
> From: Avery Adams <oldtechaa@gmail.com>
> Date: Wed, 23 Aug 2023 at 15:55
> Subject: Re: PPC Elevator Pitch for Perl::Types
> To: Darren Duncan <darren@darrenduncan.net>
>
>
> With Oshun providing dynamic "types", I would have to imagine it would be less performant even using the same approach to data checking, simply because the types aren't predefined with known structure/characteristics. Would that not be the case?

"It depends." I don't think the types themselves (or numbers known at
compile time) are the problem.

I'm not an expert here and I'm seriously hoping someone steps up to
call me an idiot and wrong but as best I can tell the performance hit
isn't entirely the assertion check itself, it's the slow down on
_everything_ that simply having the assertion causes. As was mentioned
in this thread there are only so many ways to attach extra behavior to
a variable in Perl and we've spent since 1995 (or possibly 1985)
optimizing the common case of having no extra behavior. The minute you
add a type annotation we need to include a way to look up the
assertion check, which means we no longer have a "simple" SV (or AV or
HV) and all the optimized code isn't used.

An implementation of anything like Oshun would, I think, have to work
roughly the same way as an implementation of Perl::Types would, I
think, have to work. You'd have to annotate the variable to call an
extra function during runtime when the variable is set. Whether that
function is a CV defined at compile time, or one of the SvOK macros …
you still have to link to it via magic vtable or something like that
and you lose every optimized hotpath for variables that don't have
magic attached to them. So the performance becomes vaguely the same
regardless of how the structure of types are predefined / known at
compile time.

Both Oshun and Perl::Types will, at least initially[1], slow down any
application that enables them unless/until some kind of Optimizer is
written to compile typed Perl into untyped Perl so that the core
runtime can "just work" (or we fix the hotpaths to allow typed code
but I don't that isn't at least as much work as an optimizer, I just
know _I_ can't/won't write it)

Additionally because Perl allows perfectly valid code like:

my $foo = int(rand(2)) % 2) ? \1 : [];
say $$foo; # dies 50% of the time

The correctness is only going to be seen at runtime with a slightly
better error message after this is re-written to:

my ScalarRef $foo = int(rand(2)) %2 ? \1 : []; # dies 50% of the time
say $$foo;

Or more the more sneaky and common:

my Int $foo = 1;
say $foo . " coercions have occured"; # boom?

($foo is no longer an SvIV but Perl::Types doesn't say if this should
be an error or not, but the next time our assertion check is executed
it'll blow up. I believe the current spec for Oshun says it's not an
error and you've got to be OK with that as a limitation.)

Any optimizer we write will need to be smart enough to allow these
kinds of things through with enough of the type system left to throw
intelligent errors, otherwise we're just begging for support tickets.
This means that _some_ of our variables will probably still have some
kind of magic attached to them and we still take a performance hit,
even with optimized code, but _maybe_ the optimizations are enough to
balance that out and/or increase performance? Who knows, it's a future
that's as-yet unwritten.

So from this list:

* increased performance
* code correctness
* type safety (type checking)
* memory safety (bounds checking)
* documentation of code intent
* potential for polymorphism
* potential for derived or synthetic types

Realistically all I've seen evidence for in initially from _either_
Oshun or Perl::Types is[2]:

* (type checking)
* documentation of code intent
* potential for polymorphism

I think long-term it opens the door for experiments with more
optimizations once we have enough annotated Perl in the world, but
nobody should be selling that as an advantage right now.

Personally I see this as "worth it" but only if I'm not limited to
C's type-bestiary, so Perl::Types isn't really interesting to me.

-Chris

[1]: Someone might possibly write an optimizer that takes advantage of
the type annotations to improve performance, but that's neither simple
nor easy.
[2]: 'memory safety (bounds checking)' and 'potential for derived or
synthetic types' aren't really problems Perl currently faces afaict
Re: PPC Elevator Pitch for Perl::Types [ In reply to ]
> Re: PPC Elevator Pitch for Perl::Types
> Thread Previous | Thread Next
> From: Dave Mitchell
> Date: August 22, 2023 10:03
> Subject: Re: PPC Elevator Pitch for Perl::Types
> Message ID: ZOSHzZ2SS1lMJdZy@iabyn.com

> On Tue, Aug 22, 2023 at 04:19:04AM +0000, Oodler 577 via perl5-porters wrote:
> > Yes you are correct, the current mechanism for enabling type-checking
> > for subroutine calls is essentially a type of source filter. As
> > mentioned in our first reply:
> >
> > > > The Perl compiler currently supports type enforcement for subroutine calls, so that is our starting point for Perl::Types.
> >
> > This source-filter-like functionality is currently contained in
> > the Perl compiler's file `Class.pm`

> So just for the avoidance of doubt, is the proposal that Perl::Types,
> when running in a perl (rather than RPerl) environment, will use perl's
> actual source filter mechanism, or that it will use some other
> "essentially a type of source filter" thing?

> If the latter, please expand.

It is not an actual source filter as documented in `perlfilter`,
although it is the same general concept. As mentioned in the
previous response, _"this source-filter-like functionality is
currently contained in the Perl compiler's file `Class.pm` and is
triggered by including `use RPerl;` in a Perl source code file."_
You can see the filter-like implementation in the link and code
snippet from our last response:

https://metacpan.org/release/WBRASWELL/RPerl-7.000000/source/lib/RPerl/CompileUnit/Module/Class.pm#L673-715

```perl
if ( $CHECK eq 'ON' ) {
my $i = 0; # integer
foreach my $subroutine_argument ( @{$subroutine_arguments} ) {
# only enable type-checking for arguments of supported type;
# NEED UPGRADE: enable checking of user-defined Class types & all other remaining RPerl types
if (exists $TYPES_SUPPORTED->{$subroutine_argument->[0]}) {
$subroutine_arguments_check_code .= q{ rperltypes::} . $subroutine_argument->[0] . '_CHECK( $_[' . $i . '] );' . "\n"; # does work, hard-code all automatically-generated type-checking code to 'rperltypes::' namespace
}
$i++;
}

activate_subroutine_args_checking( $package_name, $subroutine_name, $subroutine_type, $subroutine_arguments_check_code, $module_filename_long );
$inside_subroutine = 0;
$subroutine_arguments_line = q{};
}
```

As mentioned, _"you can see that both `ON` and `TRACE` call
`activate_subroutine_args_checking()`, which is where the type-checking
calls are inserted into a new subroutine, that wraps around the
original un-type-checked subroutine"_:

https://metacpan.org/release/WBRASWELL/RPerl-7.000000/source/lib/RPerl/CompileUnit/Module/Class.pm#L1016-1183

```perl
# re-define subroutine call to include type checking code; new header style
do
{
no strict;

# create unchecked symbol table entry for original subroutine
*{ $package_name . '::__UNCHECKED_' . $subroutine_name } = \&{ $package_name . '::' . $subroutine_name }; # short form, symbol table direct, not strict

# delete original symtab entry,
undef *{ $package_name . '::' . $subroutine_name };

# re-create new symtab entry pointing to checking code plus unchecked symtab entry
$subroutine_definition_code .=
'*' . $package_name . '::' . $subroutine_name . ' = sub { ' .
$subroutine_definition_diag_code .
($subroutine_arguments_check_code or "\n") .
' return ' . $package_name . '::__UNCHECKED_' . $subroutine_name . '(@ARG);' . "\n" . '};';

# create new checked symtab entries, for use by Exporter
$check_code_subroutine_name = $package_name . '::__CHECK_CODE_' . $subroutine_name;
$subroutine_definition_code .= "\n" . '*' . $package_name . '::__CHECKED_' . $subroutine_name . ' = \&' . $package_name . '::' . $subroutine_name . "\n" . ';';

$subroutine_definition_code .= "\n" . '*' . $check_code_subroutine_name . ' = sub {' . "\n" . ' my $retval ' . q{ =<<'EOF';} . "\n" . $subroutine_arguments_check_code . "\n" . 'EOF' . "\n" . '};' . "\n";
};

eval($subroutine_definition_code) or (RPerl::diag('ERROR ECOPR02, PRE-PROCESSOR: Possible failure to enable type checking for subroutine ' . $package_name . '::' . $subroutine_name . '(),' . "\n" . $EVAL_ERROR . "\n" . 'not croaking'));
if ($EVAL_ERROR) { croak 'ERROR ECOPR03, PRE-PROCESSOR: Failed to enable type checking for subroutine ' . $package_name . '::' . $subroutine_name . '(),' . "\n" . $EVAL_ERROR . "\n" . 'croaking'; }
```

> If the former, note that we generally regard the perl source filter
> mechanism as deeply flawed, and wouldn't recommend its use in production
> code. The basic problem comes down to the old mantra that "only perl can
> parse Perl". A source filter may correctly parse the source 99.9% of the
> time, but on those 0.1% occasions, it will get something wrong. Maybe it
> will get the start of a // operator confused with the start of a pattern.
> Or maybe it will trip over some complex code embeded into a pattern:
> /...(?{ $x = '/'; }) .../. Or whatever. At that point, code will get
> injected at the wrong point into the source, and the end user gets a
> compile error from code that isn't even in their source, which is
> completely mystifying and hard to debug.

> Switch.pm used source filters, and it was the cause of an endless stream
> of tickets. It was eventually deprecated and removed from core in 5.14.0.

Yes we agree this could be a source of problems.

Rather than using a source filter (or similar mechanism as detailed
above), can you please suggest a more stable and reliable
implementation?

> > Regarding your `my number $x; $x = 'foo';` example, this will
> > require the use of a `tie` or similar mechanism

> Again, this is very vague. Do you actually mean perl's tie / magic
> mechanism? If not, what "similar mechanism" are you proposing to use?

As mentioned, _"in the past, we have always simply allowed the
C(++) compiler to provide this functionality for us; however, this
will have to be explicitly implemented when `Perl::Types` is
refactored into its own distribution."_

Thus, we are not committed to any specific implementation, as long
as we choose a mechanism that allows us to (eventually) handle
_"arbitrarily-nested data structures"_ by _"intercepting any
modifications to internal elements and calling `foo_CHECK()` or
`foo_CHECKTRACE()` for each change."_

> The problem with using actual ties, is that they make the variable very
> slow, even if the actual tie methods such as FETCH() are XS code. That's
> because a lot of code paths in the perl interpreter have optimisations for
> non-magic values. For example, look at pp_add() in pp_hot.c in the perl
> source. This implements perl's '+' operator. Obviously in general the two
> operands can be anything - ints, ,nums, strings, magic values, overloaded
> objects etc, and mixtures thereof. But in pp_add(), the first thing it
> does it check the flags of the two operands, and says:

> If they are both non magical and non ref, and if both are either
> simple ints or simple nums, then just add the two values, check they
> haven't overflowed, set the return value and return. Otherwise go
> through the complex 200-lines-of-code path which handles magic, mixed
> types etc.

> So if, for example, an integer variable was marked with get magic, it
> would become a lot slower when being added. Similar considerations apply
> in many places.

> Also, once tie magic starts being applied to arrays and hashes, you start
> to hit edges cases. It's very had to make a tied aggregate behave *exactly*
> like a plain array/hash in all circumstances, even ignoring the slowdown.

Yes we agree this could also be a source of problems, plus slowdowns.

Rather than using `tie`, can you please suggest a more performant
and reliable implementation?

> > So, as mentioned in the original Elevator Pitch, we can "utilize
> > Perl data types to achieve a number of benefits including but not
> > limited to":
> >
> > * increased performance

> So how would you get increased performance? I've already pointed out that
> magic tends to slow things down, and the cost of calling a check routine
> will slow things down even further.

Adding data types to your Perl source code is the first step on
the path toward compiling your Perl source code, which can provide
anywhere from 10x to 400x (or more) runtime performance increase.
Yes you are correct that enabling type-checking will introduce
runtime overhead to interpreted Perl code, but that slowdown
disappears once your Perl code is fully compiled because the
type-checking is done at compile time rather than runtime.

For those who want type-checking for interpreted Perl code only,
we have done our best to minimize the runtime overhead by allowing
the developer to choose between `foo_CHECKTRACE()` and the slightly
faster `foo_CHECK()`. As mentioned, _"type checking is currently
controlled on a per-file basis using the `TYPE_CHECKING` preprocessor
directive"_:

https://metacpan.org/release/WBRASWELL/RPerl-7.000000/source/lib/RPerl/Test/TypeCheckingTrace/AllTypes.pm#L1-2

```perl
# [[[ PREPROCESSOR ]]]
# <<< TYPE_CHECKING: TRACE >>>
```

_"The `TYPE_CHECKING` directive can have a value of `OFF` for
disabled, `ON` to call the `foo_CHECK()` macros/functions, and
`TRACE` to call the `foo_CHECKTRACE()` macros/functions. The only
difference between `ON` and `TRACE` is the inclusion of the offending
subroutine and variable names for easier debugging."_

Furthermore, the type-checking is performed by C macros for scalar
data types, and by C functions (which call those underlying C
macros) for array and hash data structures, which is the fastest
possible solution we could find. We even re-implemented our original
scalar type-checking from C functions to C macros for increased
performance.

Here is an example of the fast (but now-deprecated) C functions
for checking the `number` type AKA `NV`:

https://metacpan.org/release/WBRASWELL/RPerl-7.000000/source/lib/RPerl/DataType/Number.cpp#L18-38

```c
// TYPE-CHECKING SUBROUTINES DEPRECATED IN FAVOR OF EQUIVALENT MACROS
void number_CHECK(SV* possible_number) {
if (not(SvOK(possible_number))) {
croak("\nERROR ENV00, TYPE-CHECKING MISMATCH, CPPOPS_PERLTYPES & CPPOPS_CPPTYPES:\nnumber value expected but undefined/null value found,\ncroaking");
}
if (not(SvNOKp(possible_number) || SvIOKp(possible_number))) {
croak("\nERROR ENV01, TYPE-CHECKING MISMATCH, CPPOPS_PERLTYPES & CPPOPS_CPPTYPES:\nnumber value expected but non-number value found,\ncroaking");
}
};
void number_CHECKTRACE(SV* possible_number, const char* variable_name, const char* subroutine_name) {
if (not(SvOK(possible_number))) {
croak("\nERROR ENV00, TYPE-CHECKING MISMATCH, CPPOPS_PERLTYPES & CPPOPS_CPPTYPES:\nnumber value expected but undefined/null value found,\nin variable %s from subroutine %s,\ncroaking",
variable_name, subroutine_name);
}
if (not(SvNOKp(possible_number) || SvIOKp(possible_number))) {
croak("\nERROR ENV01, TYPE-CHECKING MISMATCH, CPPOPS_PERLTYPES & CPPOPS_CPPTYPES:\nnumber value expected but non-number value found,\nin variable %s from subroutine %s,\ncroaking",
variable_name, subroutine_name);
}
};
```
Here is an example of the functionally-equivalent and even-faster
C macros, as mentioned in our first reply:

https://metacpan.org/release/WBRASWELL/RPerl-7.000000/source/lib/RPerl/DataType/Number.h#L137-149

```c
// [[[ TYPE-CHECKING MACROS ]]]
#define number_CHECK(possible_number) \
(not(SvOK(possible_number)) ? \
croak("\nERROR ENV00, TYPE-CHECKING MISMATCH, CPPOPS_PERLTYPES & CPPOPS_CPPTYPES:\nnumber value expected but undefined/null value found,\ncroaking") : \
(not(SvNOKp(possible_number) || SvIOKp(possible_number)) ? \
croak("\nERROR ENV01, TYPE-CHECKING MISMATCH, CPPOPS_PERLTYPES & CPPOPS_CPPTYPES:\nnumber value expected but non-number value found,\ncroaking") : \
(void)0))
#define number_CHECKTRACE(possible_number, variable_name, subroutine_name) \
(not(SvOK(possible_number)) ? \
croak("\nERROR ENV00, TYPE-CHECKING MISMATCH, CPPOPS_PERLTYPES & CPPOPS_CPPTYPES:\nnumber value expected but undefined/null value found,\nin variable %s from subroutine %s,\ncroaking", variable_name, subroutine_name) : \
(not(SvNOKp(possible_number) || SvIOKp(possible_number)) ? \
croak("\nERROR ENV01, TYPE-CHECKING MISMATCH, CPPOPS_PERLTYPES & CPPOPS_CPPTYPES:\nnumber value expected but non-number value found,\nin variable %s from subroutine %s,\ncroaking", variable_name, subroutine_name) : \
(void)0))
```

For those who want data types in their interpreted code as a form
of annotation only, they can set `TYPE_CHECKING` to `OFF` for even
better performance. You can always turn it back on later.

```perl
# [[[ PREPROCESSOR ]]]
# <<< TYPE_CHECKING: OFF >>>
```

For those who don't want data types in their Perl code at all...
just don't `use Perl::Types;`, it's that simple. Your code won't
be affected in anyway, and you can safely continue without any data
types of any kind, just like Perl has always been in the past.

> > * memory safety (bounds checking)

> Can you give an example of where currently perl isn't memory safe, but
> Perl::Types would make it so?

We are not currently aware of any such issues with the Perl
interpreter, and our goal is not to attempt to point out anything
wrong with the interpreter's memory management. Rather, this is
again primarily related to the need for memory bounds checking for
those who wish to compile their Perl code.

That being said, in the future it may certainly be possible for
the Perl interpreter to utilize data type and data structure
information to implement optimizations in memory and/or performance.

For example, a Perl array can declare its own specific maximum
index (length minus 1) during creation, which is used by the Perl
compiler (and subsequently the C(++) compiler) for standard memory
management purposes. In the following example, the declared
`arrayref` length is 4, with an `undef` being assigned to the
maximum index of 3 (`4 - 1`) in order to avoid `Useless use of
array element in void context` when `use warnings;` is enabled:

```perl
my integer::arrayref $foo->[4 - 1] = undef;
```

The current experimental syntax for declaring an `array` is very similar, but without the need for `undef` and with the unfortunate need for a temporary `$TYPED_foo` scalar:

```perl
my integer::array @foo = my $TYPED_foo->[4 - 1];
```

The Perl interpreter could potentially utilize the array's maximum
index to more effectively organize allocated memory, reduce calls
to garbage collection, increase efficiency of accessing contiguous
array elements, etc.

As with our previously-discussed pure-Perl `$RETURN_TYPE` syntax,
the current experimental syntax for declaring array lenths may
leave a bit to be desired, but it does not require any change to
the Perl parser and was the best we could come up with so far.
(Also, in the `arrayref` example the Perl interpreter actually
creates an anonymous array of length 4, while in the `array` example
it creates an array of length 1.)

Can you please suggest possible pure-Perl syntax alternatives for
specifying the various maximum indices of an arbitrarily-nested
`array` and `arrayref`, without requiring any changes to the existing
Perl internals?

> > * potential for polymorphism

> Please explain further.

There are many different styles or variations of polymorphism in
computer science. For the sake of brevity, we will only address
one style here: function overloading AKA ad hoc polymorphism.
These same ideas can likely be extended to include other styles of
polymorhism as well.

For example, imagine the following expansion of our original
`squared()` subroutine example:

```perl
#!/usr/bin/perl
use strict;
use warnings;

use Perl::Types;

sub squared {
{ my integer $RETURN_TYPE };
( my integer $base ) = @ARG;
return $base ** 2;
}

sub squared {
{ my number $RETURN_TYPE };
( my number $base ) = @ARG;
return $base * $base;
}

sub squared {
{ my string $RETURN_TYPE };
( my string $base ) = @ARG;
return $base x (length $base);
}

print squared(23), "\n";
print squared(21.12), "\n";
print squared('howdy'), "\n";
```

Wouldn't it be nice if we got this...

```
529
446.0544
howdyhowdyhowdyhowdyhowdy
```

... instead of this?

```
Subroutine squared redefined at ./sub_test.pl line 14.
Subroutine squared redefined at ./sub_test.pl line 20.
2323
21.1221.1221.1221.1221.12
howdyhowdyhowdyhowdyhowdy
```
What are your thoughts on this kind of polymorphism, function overloading based on argument and/or return value data type(s)?

> Ok, that was the technical side of things. Now on to the policy side.
> 20 years or so ago, perl went through a phase of adding lots of 'useful'
> modules to the perl core. This was widely seen as a mistake.

> First, it made a particular module seem to be officially endorsed.
> Hypothetically, "among several XML parser modules, we chosen this one as
> the best and the one you should use". When design flaws in XML::Parser
> (say) are discovered, and the general recommendation is to use
> XML::BetterParser instead, XML::Parser is still sitting in the perl core
> getting an unfair endorsement. And removing it from core is hard, because
> now lots of people used it, and they're using it because, well, we
> endorsed it!

Our goal is for Perl::Types to be the official data type system of
Perl, in the same exact way that Corinna is the official class
system of Perl. If a so-called "dual life" distribution is the
wrong way to achieve this, then we're fine with that.

> Second, if modules stop being actively maintained by their author(s) for
> whatever reason, then suddenly we become responsible for maintaining it.
> It's enough work just maintaining the core perl interpreter, without
> taking on extra responsibilities.

Yes we agree, which is why we have formed the Perl::Types Committee
and are in the process of training new Perl::Types developers to
avoid a low bus factor.

> Third, in these days of package managers etc, it's relatively easy for
> people to install the CPAN modules they want. They will also get the
> newest and best version by default, rather than using the old buggy version
> bundled with perl. So there is less good reason to bundle a package with
> perl.

Our goal is for Perl::Types (or its descendent) to be included in
every future version of Perl, in the same exact way that Corinna
will presumably be included in every future version of Perl. We
are fine with handling versioning in the same way as Corinna,
whatever that may be.

> So these days the policy is not to bundle CPAN modules with perl unless
> they are necessary, usually because they are needed as part of the tool
> chain. There have been very few new modules added to the perl core in the
> last 15 years or so that are capable of acting as standalone CPAN
> distributions instead.

> So the short answer is that even if Perl::Types really appealed to us, it
> is very unlikely that we would agree to bundle it. And I think we're
> far from a consensus yet that it appeals.

Since you have made a strong case against a dual-life distribution
being included in the Perl core, can you instead please explain
the specific process which Corinna followed to get merged into the
Perl interpreter?

Perhaps Perl::Types can act like Object::Pad, as the experimental
distribution out of which we can derive the desired Perl interpreter
code in the same way that Corinna was derived from Object::Pad?

> On a purely individual note, I am finding Perl::Types very unappealing so
> far.

Can you please give us a specific list of all the things you find
unappealing?

On Behalf of the _Perl::Types Committee_,
Brett Estrade

--
oodler@cpan.org
oodler577@sdf-eu.org
SDF-EU Public Access UNIX System - http://sdfeu.org
irc.perl.org #openmp #pdl #native
Re: PPC Elevator Pitch for Perl::Types [ In reply to ]
> On Sep 2, 2023, at 12:11 AM, Oodler 577 via perl5-porters <perl5-porters@perl.org> wrote:
> Our goal is for Perl::Types to be the official data type system of
> Perl, in the same exact way that Corinna is the official class
> system of Perl.

Corinna isn’t a dual-life module, and Paul didn’t even start the conversation about what should go into core until Object::Pad had existed for a year or two (Object::Pad’s first release was October 2019, 5.37.0 was may 2022 … and Corinna was in a later release of the 5.37 series) so the conversation about *how* things could be implemented happened long before anything was brought to p5p. Additionally the features in 5.38 are a *very* stripped down subset of Object::Pad and Corinna, and literally the smallest subset we thought would be useful enough for people to experiment. There are several totally uncontroversial pieces that weren’t added to 5.38 because they weren’t necessary for the smallest possible solution (e.g. Roles).

Note that the PPC process started in the summer of 2021, so the formalities for Corinna weren’t entirely there. It used something more akin to the way Peter Martini got subroutine signatures in, but thankfully with far less drama because there are a lot more stop-gaps and processes to handle the system than there used to be. (The PPC process being a refactoring of these processes, partly I suspect inspired by the ad-hoc conversations that were happening around Corinna)

So if you want to follow this solution, build a working prototype and demonstrate that the ideas even work in principle in an environment that doesn’t depend on RPerl’s C++ compiler. Make some PRs to fix the short comings in the Perl interpreter that make implementing that proof of concept difficult. Then demonstrate how your code would work as a core system. All the while suffering through the sometimes heated arguments about syntax and semantics.

People have flat out said that your semantics won’t work but I haven’t yet seen a response to that.

For example what happens with this code:

my $var1 = 234;
my integer $var2 = 456; # Perl::Types
my $total = $var1 + $var2;

Without the RPerl compiler is this an error? Does $total inherit the integer type? What if I say `$total += 0.1`? What happens if instead of 234 I shift from @ARGV? What if $var1 is a dual var? What if it’s an overloaded object? What if it’s a tie? What if I call bultin::stringify on $var1 (i.e. join “”, $var1;)?

What’s your answer to people who are currently using Type::Tiny? Are they able to upgrade their types to your system? If not is that something you’re planning for?

Corinna had answers to its version of these questions by the time anything ended up on 5.37, and the stuff in 5.37 was the smallest sensible piece of it. I don’t pretend that the Corinna process was flawless, but if you want to follow its path you have about two years of proof of concept to get started on.

-Chris
Re: PPC Elevator Pitch for Perl::Types [ In reply to ]
On Wed, Aug 23, 2023 at 7:30?AM Chris Prather <chris@prather.org> wrote:

> "It depends." I don't think the types themselves (or numbers known at
> compile time) are the problem.
>
> I'm not an expert here and I'm seriously hoping someone steps up to
> call me an idiot and wrong but as best I can tell the performance hit
> isn't entirely the assertion check itself, it's the slow down on
> _everything_ that simply having the assertion causes. As was mentioned
> in this thread there are only so many ways to attach extra behavior to
> a variable in Perl and we've spent since 1995 (or possibly 1985)
> optimizing the common case of having no extra behavior. The minute you
> add a type annotation we need to include a way to look up the
> assertion check, which means we no longer have a "simple" SV (or AV or
> HV) and all the optimized code isn't used.
>

We have a distinction between get and set magic, so for scalars we can
actually do efficient shallow type checks, see Magic::Check on CPAN for an
implementation of this.

Arrays and hashes are still screwed though, and we made some API choices in
the past that make fixing it now more difficult than one would hope.


> Realistically all I've seen evidence for in initially from _either_
> Oshun or Perl::Types is[2]:
>
> * (type checking)
> * documentation of code intent
> * potential for polymorphism
>

Agreed.
Re: PPC Elevator Pitch for Perl::Types [ In reply to ]
On Sat, Sep 02, 2023 at 04:11:02AM +0000, Oodler 577 via perl5-porters wrote:
> > If the latter, please expand.
>
> It is not an actual source filter as documented in `perlfilter`,
> although it is the same general concept. As mentioned in the
> previous response, _"this source-filter-like functionality is
> currently contained in the Perl compiler's file `Class.pm` and is
> triggered by including `use RPerl;` in a Perl source code file."_
> You can see the filter-like implementation in the link and code
> snippet from our last response:

We seem to be misunderstanding each other at a fairly fundamental level.

My understanding (and please correct me if I'm wrong) is that your desired
eventual outcome of this discussion is that a distribution called
Perl:Types will be available on CPAN, and will also bundled into the perl
distribution, so that someone with a standard perl install can type into
their code something like:

use Perl::Types;
my integer $x = f(); # croaks if f() doesn't return an int.

This is independent of the existence of RPerl - i.e. some code from RPerl
may be incorporated into Perl::Types source, but the perl use isn't
required to install RPerl or use any of the weird RPerl syntax.

So, with that understanding, I've expressed curiosity as to how you are
going to implement this functionality using only the resources available
in perl. You said "Source filters". I said that was a bad idea. You've now
said "it's something similar to a source filter - look at this RPerl
code".

Why should I look at RPerl code? I'm not interested in RPerl. I Don't like
RPerl.

Do you believe it is possible to implement Perl::Types in perl? If so, how?
If not, are you suggesting proposals for new hooks or APIs that should be
incorporated into perl to make adding type systems easier? Or what?
I really have no idea what it is you're asking.

> Yes we agree this could be a source of problems.
>
> Rather than using a source filter (or similar mechanism as detailed
> above), can you please suggest a more stable and reliable
> implementation?

I have no idea. It may not be possible. It's not my problem.


> Rather than using `tie`, can you please suggest a more performant
> and reliable implementation?

No I can't. I haven't got a clue. I've spent 20 years working on the
internals of perl, and I think what you are requesting is a very difficult
to solve problem that may well have no solution.

> Adding data types to your Perl source code is the first step on
> the path toward compiling your Perl source code, which can provide
> anywhere from 10x to 400x (or more) runtime performance increase.

Ah yes, sprinkle on a bit of magic pixie dust and suddenly perl can be
made 400x faster.

> Yes you are correct that enabling type-checking will introduce
> runtime overhead to interpreted Perl code, but that slowdown
> disappears once your Perl code is fully compiled because the
> type-checking is done at compile time rather than runtime.

That way lies RPerl, surely?

> For those who want type-checking for interpreted Perl code only,
> we have done our best to minimize the runtime overhead by allowing
> the developer to choose between `foo_CHECKTRACE()` and the slightly
> faster `foo_CHECK()`. As mentioned, _"type checking is currently
> controlled on a per-file basis using the `TYPE_CHECKING` preprocessor
> directive"_:

I don't understand that all all. Please stop just pointing at random bits
of RPerl code which I am completely unfamiliar with and expecting that to
be an explanation.

> > > * memory safety (bounds checking)
>
> > Can you give an example of where currently perl isn't memory safe, but
> > Perl::Types would make it so?
>
> We are not currently aware of any such issues with the Perl
> interpreter, and our goal is not to attempt to point out anything
> wrong with the interpreter's memory management. Rather, this is
> again primarily related to the need for memory bounds checking for
> those who wish to compile their Perl code.

So using Perl::Types doesn't actually add any value to user's code as
regards memory safety.

> Can you please suggest possible pure-Perl syntax alternatives for
> specifying the various maximum indices of an arbitrarily-nested
> `array` and `arrayref`, without requiring any changes to the existing
> Perl internals?

Again, stop trying to make everything my responsibility.

> What are your thoughts on this kind of polymorphism, function overloading based on argument and/or return value data type(s)?

I don't have any strong opinions on it, but personally I don't see what
value it adds. It seems to be that, after adding a type system which
unduly limits the flexibility of your code, adding polymorphism based on
type just allows you to then remove *some* of that inconvenience.

I like the fact that in perl I can throw an int or a float or
string at a function, and it will already always do the Right Thing.
In perl I can write

while (<>) {
$foo += $1 if /foo=(\d+)/;
}

without having to worry about string to integer conversions or whatever.

> > On a purely individual note, I am finding Perl::Types very unappealing so
> > far.
>
> Can you please give us a specific list of all the things you find
> unappealing?

Well, the basic problem is that we still don't know what semantics
Perl::Types is supposed to provide or how it will be implemented.

So my objections are partly conjectural. But IIUC, RPerl's "type" system
is really just the CPU-level type system. I.e. what you get in C etc,
where an int is a simple 32-bit or 64-bit entity, which is fast but unsafe.

In C, if you do

int i = 0x7fffffffffffffff;
i *= 4;
printf "%d\n", i;

then you'll get some sort of wrapped-around value.

In perl, if you do

my $i = 0x7fffffffffffffff;
$i *= 4;
printf "%s\n", $i;

you get

3.68934881474191e+19

because internally, perl goes to great lengths to avoid overflows and loss
of precision, automatically converting internally between signed int,
unsigned int and doubles etc.

So I'm not keen on the idea of downgrading perl's dynamic type system into
a C-style type system.

In terms of making perl go a lot faster, I have various ideas, but little
time to realise them - there are always too many basic things to fix.

But I much prefer the idea of keeping perl as *perl* (dynamic typing etc)
and let the interpreter do the clever stuff: such as speculative execution
branches where perl assumes (based on code analysis and previous calls
to that code position) that the result will be an integer, and so skips
using full-blown SVs and just does direct integer arithmetic. If at some
point during execution an overflow is detected or whatever, then perl
jumps back to a checkpoint, and this time takes the "heavy" branch using
full-blown SVs etc.

So in something like $a[$x+$y*$z], perl detects that $x, $y and $z are
usually integer values, and takes a short-cut. If that doesn't work out,
then it still gets the right result, just more slowly.

So the user gets the performance benefits without having to rewrite
all their code to include 'integer' declarations everywhere.

As a final caveat, note that I'm more of an internals guy than a language
design guy. So my views on proposed new language features shouldn't be
taken too seriously. But if I say that a particular proposal will be hard
or impossible to implement, take that seriously.

--
The Enterprise successfully ferries an alien VIP from one place to another
without serious incident.
-- Things That Never Happen in "Star Trek" #7
Re: PPC Elevator Pitch for Perl::Types [ In reply to ]
(originally sent privately, sent to list on explicit request by oodler)

On Sat, 02 Sep 2023 06:11:02 +0200, Oodler 577 via perl5-porters <perl5-porters@perl.org> wrote:

> https://metacpan.org/release/WBRASWELL/RPerl-7.000000/source/lib/RPerl/CompileUnit/Module/Class.pm#L673-715

fwiw, some of the rather muted enthusiasm you're seeing for this project stems from the fact that (corroborated by several random perl devs i polled) this code looks harder to maintain than it should be, seemingly not being deduplicated, and not like native perl code at all

it feels like it was written by somebody who knows a different programming language really well and preferred not to get too familiar with perl

here's code that does somewhat similar work, but in a way that inspires confidence

https://metacpan.org/release/HAARG/Moo-2.005005/source/lib/Method/Generate/Accessor.pm#L429

--
With regards,
Christian Walde
Re: PPC Elevator Pitch for Perl::Types [ In reply to ]
* Christian Walde <walde.christian@gmail.com> [2023-09-04 10:08:13 +0200]:

> (originally sent privately, sent to list on explicit request by oodler)
>
> On Sat, 02 Sep 2023 06:11:02 +0200, Oodler 577 via perl5-porters <perl5-porters@perl.org> wrote:
>
> > https://metacpan.org/release/WBRASWELL/RPerl-7.000000/source/lib/RPerl/CompileUnit/Module/Class.pm#L673-715
>
> fwiw, some of the rather muted enthusiasm you're seeing for this project stems from the fact that (corroborated by several random perl devs i polled) this code looks harder to maintain than it should be, seemingly not being deduplicated, and not like native perl code at all

The results of your informal poll sound udderly ridiculous.

>
> it feels like it was written by somebody who knows a different programming language really well and preferred not to get too familiar with perl
>
> here's code that does somewhat similar work, but in a way that inspires confidence
>
> https://metacpan.org/release/HAARG/Moo-2.005005/source/lib/Method/Generate/Accessor.pm#L429

Okay, thanks.

Cheers,
Brett

>
> --
> With regards,
> Christian Walde

--
--
oodler@cpan.org
oodler577@sdf-eu.org
SDF-EU Public Access UNIX System - http://sdfeu.org
irc.perl.org #openmp #pdl #native
Re: PPC Elevator Pitch for Perl::Types [ In reply to ]
On Mon, 04 Sep 2023 23:44:04 +0200, Oodler 577 <oodler577@sdf-eu.org> wrote:

>> > https://metacpan.org/release/WBRASWELL/RPerl-7.000000/source/lib/RPerl/CompileUnit/Module/Class.pm#L673-715
>>
>> fwiw, some of the rather muted enthusiasm you're seeing for this project stems from the fact that (corroborated by several random perl devs i polled) this code looks harder to maintain than it should be, seemingly not being deduplicated, and not like native perl code at all
>The results of your informal poll sound udderly ridiculous.

why? :)

--
With regards,
Christian Walde
Re: PPC Elevator Pitch for Perl::Types [ In reply to ]
* Christian Walde <walde.christian@gmail.com> [2023-09-05 01:59:20 +0200]:

> On Mon, 04 Sep 2023 23:44:04 +0200, Oodler 577 <oodler577@sdf-eu.org> wrote:
>
> > > > https://metacpan.org/release/WBRASWELL/RPerl-7.000000/source/lib/RPerl/CompileUnit/Module/Class.pm#L673-715
> > >
> > > fwiw, some of the rather muted enthusiasm you're seeing for this project stems from the fact that (corroborated by several random perl devs i polled) this code looks harder to maintain than it should be, seemingly not being deduplicated, and not like native perl code at all
> > The results of your informal poll sound udderly ridiculous.
>
> why? :)

Because it's BS. Get it? Udderly BS? xD

Cheers,
Brett

>
> --
> With regards,
> Christian Walde

--
--
oodler@cpan.org
oodler577@sdf-eu.org
SDF-EU Public Access UNIX System - http://sdfeu.org
irc.perl.org #openmp #pdl #native

1 2 3  View All