RE: PMCs, setting, and suchlike things [forward from p6-internals]
This came up on perl6-internals, and Dan liked the try suggestion and suggested That I post it here for comments. I'm not subscribed to p6-language, so you'll need to include me in any replies where you want a response from me. = Dave Mitchell [EMAIL PROTECTED] wrote: my Complex $c = 3+4i; my $plain = 1.1; $plain = $c; This might be even more Complex than that - what if Complex can be reduced? Should it? for instance: my Complex $c = 3+4i; my Complex $d = 4i; my $plain = $c / $d; Does $plain (which is actually '3' after reducing) get promoted to Complex, or does the result from the division get demoted? Perhaps there could be a sort of 'try' for conversion that returns the best possible result? for instance: my Complex $c = 3+4i; my Complex $d = unknown qty; my $plain = try_demote( $c / $d ); $plain now ISA Complex if it couldn't demote the result of the math, or it ISA scalar (int or float) if it could. Now if you need to know, then just check: $plain = try_demote( $c / $d ); # the '(or)'s here are alternate forms, not comparison if( $plain.type == Complex (or) $plain.Complex ){ print It promoted!\n; } elsif( $plain.type == Scalar (or) $plain.Scalar ) { print Result was reduced!\n; } Ramblings of a madman, Grant M.
Re: PMCs, setting, and suchlike things [forward from p6-internals]
Wizard [EMAIL PROTECTED] writes: This came up on perl6-internals, and Dan liked the try suggestion and suggested That I post it here for comments. I'm not subscribed to p6-language, so you'll need to include me in any replies where you want a response from me. = Dave Mitchell [EMAIL PROTECTED] wrote: my Complex $c = 3+4i; my $plain = 1.1; $plain = $c; This might be even more Complex than that - what if Complex can be reduced? Should it? for instance: my Complex $c = 3+4i; my Complex $d = 4i; my $plain = $c / $d; Does $plain (which is actually '3' after reducing) get promoted to Complex, or does the result from the division get demoted? In general I'd say that $plain shouldn't change class (I'm not sure it's being demoted; after all, a real number has more constraints on it than a complex number), for the same reason that Rectangle.new(sides = [1, 1]).isa('Square'); should return false. Perhaps there could be a sort of 'try' for conversion that returns the best possible result? for instance: my Complex $c = 3+4i; my Complex $d = unknown qty; my $plain = try_demote( $c / $d ); $plain now ISA Complex if it couldn't demote the result of the math, or it ISA scalar (int or float) if it could. Now if you need to know, then just check: $plain = try_demote( $c / $d ); # the '(or)'s here are alternate forms, not comparison if( $plain.type == Complex (or) $plain.Complex ){ print It promoted!\n; } elsif( $plain.type == Scalar (or) $plain.Scalar ) { print Result was reduced!\n; } Demote is a really bad name I think. $plain = most_appropriate_type($c / $d); might be better, but it's still ugly. -- Piers It is a truth universally acknowledged that a language in possession of a rich syntax must be in need of a rewrite. -- Jane Austen?
Re: PMCs, setting, and suchlike things [forward from p6-internals]
W == Wizard [EMAIL PROTECTED] writes: my Complex $c = 3+4i; my $plain = 1.1; $plain = $c; W This might be even more Complex than that - what if Complex can be W reduced? Should it? for instance: W my Complex $c = 3+4i; W my Complex $d = 4i; W my $plain = $c / $d; W Does $plain (which is actually '3' after reducing) get promoted to W Complex, or does the result from the division get demoted? wouldn't the new value actually be 3/4i + 1? i think you would need $c - 4i to get just a real part out. W Perhaps there could be a sort of 'try' for conversion that returns the best W possible result? for instance: W my Complex $c = 3+4i; W my Complex $d = unknown qty; W my $plain = try_demote( $c / $d ); W $plain now ISA Complex if it couldn't demote the result of the math, or it W ISA scalar (int or float) if it could. Now if you need to know, then just W check: W $plain = try_demote( $c / $d ); W # the '(or)'s here are alternate forms, not comparison W if( $plain.type == Complex (or) $plain.Complex ){ Wprint It promoted!\n; W } or as dan said in internal, if plain is not tagged with any type, it just gets the complex value. that would be handled by the $plain.Complex case as all untyped scalars can take any value. but i think a simpler and faster test of no typing should be in there. another post in internals had psuedo code with that test Buddha Buck [EMAIL PROTECTED] wrote: if (destPMC is specified as typeX) { if (srcPMC ISA typeX) { destPMC - srcPMC } else { destPMC - typeX.convert(srcPMC); } } else { destPMC - srcPMC } i take that first if to mean destPMC has any fixed type. else it just takes on the type of the source. W elsif( $plain.type == Scalar (or) $plain.Scalar ) { Wprint Result was reduced!\n; W } plain scalars should handle any type. maybe that example should be int or float? if you assign a complex value to an int/float it should reduce to the real part of the value and assign that (with int/float conversion as needed). if you wanted the imaginary part only you would have to do: $imag = $complex.imaginary or would this be possible? my $imag is imaginary ; # not complex! $imag = $complex ; that would cause the imaginary method/extraction to be called on $complex and the scalar int/float would be assigned to $imag. $imag could also have an int/float property which would further reduce the value. uri -- Uri Guttman -- [EMAIL PROTECTED] http://www.stemsystems.com -- Stem is an Open Source Network Development Toolkit and Application Suite - - Stem and Perl Development, Systems Architecture, Design and Coding Search or Offer Perl Jobs http://jobs.perl.org
RE: PMCs, setting, and suchlike things [forward from p6-internals]
my Complex $c = 3+4i; my Complex $d = 4i; my $plain = $c / $d; Does $plain (which is actually '3' after reducing) get promoted to Complex, or does the result from the division get demoted? In a related matter, computer languages with Symbolic Mathematics capabilities, like Mapple, let you explicitly demand where do you want the operation to take place. This could be done naturally in perl6 using the colon meta-operator: my $plain = $c - $d : Math::Reals # 3.0 my $plain = $c - $d : Math::Complex # 3.0 + 0i As long it is well documented and consistent, it doesn't really matter which one is the default. Adding this feature is useful in a wider area of applications: 2 ^ 4 : Math::FiniteField(7) # - 1 Or even: sqrt(2 : Math::Integers) # - exception or not-a-number sqrt(2 : Math::FiniteField(7)) # - 3 sqrt(2 : Math::TrueReals) # - sqrt(2) In general, any mathematical operation should be capable of taking optionally the structure where the operations is defined, and a whole universe of fun will open for us mathematicians.. :-) (Just think of factoring polynomials in F[25][X] over its algebraic closure... mmm...) This can be implemented having operations defined as: sub operator:DIV($a, $b : $structure) { if $structure and $strcture.can('DIV') {$structure.DIV($a,$b)} elsif $a.can('DIV') {$a.DIV($a,$b)} elsif $b.can('DIV') {$c.DIV($a,$b)} else {..throw exception..} } On the other hand, it is (it will be) perfectly possible to do it as an user module, so this post it's actually a big OT, and you should better forget it, in case you have been unfortunate enough to read until here... Oh. Damn. -angel
RE: PMCs, setting, and suchlike things [forward from p6-internals]
In a related matter, computer languages with Symbolic Mathematics capabilities, like Mapple, let you explicitly demand where do you want the operation to take place. This could be done naturally in perl6 using the colon meta-operator: my $plain = $c - $d : Math::Complex # 3.0 + 0i sqrt(2 : Math::Integers) # - exception or not-a-number Not a bad idea,. I beleive that the perl6 adjective operator (for functions) will be a semicolon, not a colon. I'm not sure how it is planned to apply it to operators. Its also plausable to use properties: my $plain2 is Math::Complex = $c - $d. That would require overloading the '-' operator based on return type; and could get a bit nasty. Dave.
Re: PMCs, setting, and suchlike things [forward from p6-internals]
: This might be even more Complex than that - what if Complex can be : reduced? Should it? for instance: : : my Complex $c = 3+4i; : my Complex $d = 4i; : my $plain = $c / $d; : : Does $plain (which is actually '3' after reducing) get promoted to Complex, : or does the result from the division get demoted? I wish you guys would quit confusing variables with values. As an unmarked variable, $plain can hold a Complex just as easily as any other type. Now, if you'd said my num $plain = $c / $d; that would be more like the situation you're thinking of. Larry
RE: PMCs, setting, and suchlike things [forward from p6-internals]
my $plain = $c - $d : Math::Complex # 3.0 + 0i sqrt(2 : Math::Integers) # - exception or not-a-number Not a bad idea,. I beleive that the perl6 adjective operator (for functions) will be a semicolon, not a colon. I'm not sure how it is planned to apply it to operators. Its also plausable to use properties: my $plain2 is Math::Complex = $c - $d. That would require overloading the '-' operator based on return type; and could get a bit nasty. The nice thing about being per-operation (or per-function) is that in some cases, the place where you want to define the operation is not alone defined by the operands. Is more like a kind of enviroment where things happen. While most cases can be handled with a rich type system, others do need explicit information about the structure where the operation is defined (although, to be honest, they are probably the lesser). [The example I've got in mind is factoring a polynomal, where the type of the object (Polynomal of real parameters) doesn't determine the place where you factor it (over Q, R, a finite extension, ...? ) ] A second one (probably more important) is that with adverbs you can use both classes (Math::Complex) and instance objects ( Math::FiniteFields::F(8) ) as long as they have the right set of methods. I don't know if you can say : my $plain2 is $structureObject = $c - $d; do you? This allows structures to be user-constructed objects if needed (and only if needed), and thus in theory (probably only in theory) you could even define an algebra of structures. And finally, I find some syntatic pleasure in: print [0,2,3] * [1,3,4] : $myEuclidianSpace; -angel who must have taken the rant pill today...