-
Notifications
You must be signed in to change notification settings - Fork 0
/
overload_override.pl
executable file
·220 lines (175 loc) · 5.35 KB
/
overload_override.pl
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env perl
use strict;
use warnings;
use v5.18;
package UnitRole;
use Moo::Role;
use Scalar::Util;
sub unit {
my ($self, $unit) = @_;
state $_unit; # table to provide inside-out attribute
if( defined $unit ) {
$_unit->{ Scalar::Util::refaddr $self } = $unit;
}
$_unit->{ Scalar::Util::refaddr $self } // '';
}
sub is_unit_dimensionless {
my ($self) = @_;
$self->unit eq '';
}
package PDL::UnitRole;
use Moo::Role;
use Data::Perl qw(number);
use Scalar::Util;
around qw(sumover cumusumover avg sum) => sub {
my $orig = shift;
my ($self) = @_;
my $ret = $orig->(@_);
if( !ref $ret and Scalar::Util::looks_like_number($ret) ) {
$ret = number($ret);
}
OOverload->mangle($ret, $self->unit); # propagates
};
package Unit;
use Carp;
sub op_unit_mult {
my ($x, $y, $o) = @_;
my @prod = ( ($x->unit)x!!( not $x->is_unit_dimensionless ),
($y->unit)x!!( not $y->is_unit_dimensionless ) );
my $unit_prod = join "*", @prod;
}
sub op_unit_div {
my ($x, $y, $o) = @_;
my @prod = ( ($x->unit)x!!( not $x->is_unit_dimensionless ),
($y->unit)x!!( not $y->is_unit_dimensionless ) );
my $unit_prod = join "/", @prod;
}
sub op_unit_add {
my ($x, $y, $o) = @_;
confess "Units not compatible: @{[$x->unit]} != @{[$y->unit]}" unless $x->unit eq $y->unit;
$x->unit;
}
sub op_unit_sub {
&op_unit_add;
}
package OOverload;
our $OVERLOADED;
use Scalar::Util;
use Package::Stash;
sub mangle {
my ($class, $object, $unit) = @_;
my @roles;
push @roles, 'UnitRole';
push @roles, 'PDL::UnitRole' if $object->isa('PDL');
my $role_class = Moo::Role->create_class_with_roles(ref $object, @roles);
#unless( $object->can('unit') ) { # it already has UnitRole
unless( exists $OVERLOADED->{ ref $object } ) { # it already has UnitRole
Moo::Role->apply_roles_to_object( $object, @roles );;
}
$object->unit($unit);
my $package_of_overload = ref $object;
unless( exists $OVERLOADED->{ $package_of_overload } ) {
my %new_ops;
my %op_table = ( # these should be better
'+' => \&Unit::op_unit_add,
'-' => \&Unit::op_unit_sub,
'*' => \&Unit::op_unit_mult,
'/' => \&Unit::op_unit_div,
);
for my $op (keys %op_table) {
my $op_func = $op_table{$op};
my $op_overload_orig = overload::Method($object, $op);
$new_ops{$op} = sub {
my ($x, $y, $o) = @_;
my $obj_res = $op_overload_orig->(@_);
my $unit_res = $op_func->(@_);
OOverload->mangle($obj_res, $unit_res);
};
}
my $overload_orig_str = overload::Method($object, '""');
if( $object->isa('PDL') ) {
# hack
$overload_orig_str = \&PDL::Core::string;
#} elsif( $object->isa('Data::Perl::Number') ) {
#$overload_orig_str = sub {
#overload::StrVal( ${ $_[0] } );
#};
} elsif( not defined $overload_orig_str ) {
# last resort
# does it numify?
my $scalar_fallback = sub {
my ($x) = @_;
if( Scalar::Util::reftype($x) =~ /^(SCALAR|REF)$/ ) {
overload::StrVal($$x);
} else {
&overload::StrVal;
}
};
$overload_orig_str = overload::Method($object, '0+') // $scalar_fallback;
}
my $overload_new_str = sub {
my ($x, $o) = @_;
my $obj_str = $overload_orig_str->(@_);
"$obj_str~@{[$x->unit]}";
};
{
# warning!
#print "--$package_of_overload--\n";#DEBUG
eval qq|
package $package_of_overload;
use overload \%new_ops,
'""' => \$overload_new_str;
|;
}
$OVERLOADED->{ $package_of_overload } = 1;
}
overload::Overloaded($object);
$object;
}
package main;
use Test::Most;
use PDL;
use Data::Perl qw(number);
use Number::Fraction;
# FUTURE
use Math::FixedPoint;
use Data::Float;
use Data::Integer;
use Scalar::Number;
# compile once to enable SvAMAGIC on new objects from each package
# RT #112708: Overload in runtime <https://rt.perl.org/Ticket/Display.html?id=112708>
# so either run these ahead of time or use v5.18
#
# see also:
# <https://metacpan.org/source/MooseX::Role::WithOverloading::Meta::Role::Application>
# <https://metacpan.org/source/MooseX::Role::WithOverloading::Meta::Role::Application::FixOverloadedRefs>
# <https://metacpan.org/source/ETHER/MooseX-Role-WithOverloading-0.13/WithOverloading.xs>
#OOverload->mangle( pdl(0), 'm' );
#OOverload->mangle( number(0), 'm' );
my $p = pdl q[4 2];
my $q = pdl q[2 3];
OOverload->mangle( $p, 'm' );
OOverload->mangle( $q, 's' );
is( "$p", "[4 2]~m", '$p is a PDL in metres');
is( "$q", "[2 3]~s", '$q is a PDL in seconds');
my $y = OOverload->mangle( pdl(q[2 2]) , 'm*s' );
is( "$y", "[2 2]~m*s", '$y is a PDL in m*s');
my $z = $p * $q;
is( "$z", '[8 6]~m*s', q|multiplying units of 'm' and 's' gives 'm*s'|);
diag "$p * $q = $z";
my $add_z_y = $z + $y;
is( "$add_z_y", '[10 8]~m*s', q|Two 'm*s' units can be added|);
diag "$z + $y = $add_z_y";
my $div_p_q = $p / $q;
is( "$div_p_q", '[2 0.66666667]~m/s', q|Dividing units of 'm' by 's' gives 'm/s'|);
diag "$p / $q = $div_p_q";
is( "@{[$p->sumover]}", "6~m", 'Summing keeps the same units');
is( "@{[$p->cumusumover]}", "[4 6]~m", 'Cumulative summing keeps the same units');
#say "--\nData::Perl::Number --- bug territory";
is( "@{[$p->avg]}", "3~m", "Average keeps the same units");
throws_ok { my $g = $z + $p; 1 } qr/Units not compatible/, q|Can not add 'm*s' to 'm'|;
my $half = OOverload->mangle(Number::Fraction->new(1, 2) , 'cup');
my $three = OOverload->mangle(Number::Fraction->new(3), '');
my $three_halves = $half * $three;
is( "$three_halves", '3/2~cup', 'multiplying fractions times dimensionless value');
done_testing;