-
-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make Fraction contexts as extensions so you can add fractions to other compatible contexts. #1108
Conversation
PS, because this |
…r compatible contexts. Move original Fraction context to legacyFraction.pl
0d2dea6
to
d089092
Compare
I am seeing an issue with the following MWE:
That works with the develop branch, but gives an error with this pull request. |
OK, I forgot to include the |
That fixes the issue with that example. Thanks. I am seeing another issue when different contexts are used as in the following: DOCUMENT();
loadMacros('PGstandard.pl', 'PGML.pl', 'contextFraction.pl', 'PGcourse.pl');
Context('Fraction');
$fractionAns = Compute('x/7');
Context('Numeric');
$numericAns = Compute("3");
BEGIN_PGML
[`[$fractionAns] =`] [_]{$fractionAns}{10}
[`[$numericAns] = `] [_]{$numericAns}{10}
END_PGML
ENDDOCUMENT(); |
Note that if either answer is removed from that problem it works. Also, if the fraction does not have |
You latest changes fix the issues! Thanks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may not like the perltidy format, but look at much of the formatting of webwork and PG code before we adopted it. It was utterly horrendous. If you set your editor up with it, it also makes it easy to have nicely formatted code with no effort.
Yes, I was still doing some more changes before writing up the details. It turns out that changing the context was what was causing the problem. Although MathObjects contain a pointer to the context that created them, there are places where new objects are created via their I need to update the units PR to accommodate those changes as well.
I don't object to it in principle, but I think some of the internal alignment make things harder to read, and some of the spacing is unnecessary. For example, just because two consecutive lines have equal signs or suffix $x = $context->Package("Formula")->new($context, $x)->eval if !ref($x) && $x =~ m!/!;
$x = $x->eval if @_ == 0 && Value::classMatch($x, 'Fraction'); is nonsense in my opinion. It is much harder to see the second line as being a conditional when the my $class = join('::', (split(/::/, ref($self) || $self))[ 0, 1 ]); That was the source of my commit comment. I frequently end up shaking my head at some of the changes it makes. But I understand the desire to have consistency, and you all can decide on the formatting you want to use. I may still grumble about it at times, however. |
I also don't like some of the formatting at times. In some cases we may be able to improve things by tweaking the settings. At least you can do that, unlike prettier for JavaScript, typescript, etc. I just know that some of the webwork and PG code used to be really untradable with excessively inconsistent indentation in particular. |
The particular alignment issue you mentioned is certainly not the nicest in appearance, I agree. |
I agree with you about the consistency for indenting, line breaks, and continuation lines; that is a big win. It is the internal alignment that I find so unsettling. It seems to me that internal alignment should be used to indicate a meaningful structural connection between two lines, but most of the internal alignment that I see is meaningless, like the one I illustrated above (and I did choose it because of its particularly grievous nature, though most aren't that bad). That has several consequences. First, it makes things harder to read because the extra spacing interferes with reading the individual line, and one is incorrectly lead to believe there is a connection to a previous or following line, and it takes time to recognize that that is not the case. Second, because that is so frequently not the case, it trains you to ignore the alignment, as it is generally is meaningless. That means you have watered down the meaning of alignment, so that when it truly is important, you are less likely to recognize it among the sea of other nonsense alignments. For the most part, I think internal alignment should be relatively rare, and reserved for cases where a connection needs to be emphasized. I'm happy with alignment of My own preference would be to have the authors do the internal alignment that is important (since they are the ones that understand which ones have meaning, whereas perltidy clearly doesn't), and have a style guide that indicates the ones that you expect to make, and request that authors abide by that before merging if they don't. By forcing such alignments, you may be prevention poor coders from making bad decisions, but you are also forcing other bad decisions, and preventing good coders from providing meaningful alignments (and actively undoing the meaningful ones they include). That seems to be to be guaranteeing a mediocre output; certainly not as bad as it was, but not as good as it could be. Again, I realize it is up to the currently active programming community (of which I am not longer a member) to decide these issues, and I will abide by the choices that are made. But I reserve the right to make snarky commit comments now and again. :-) |
I should point out that my recent changes to handle your context-switching example included a minor change to my $symbol = shift || ''; would perform stringification on the first argument in order to do the |
PS, I meant to thank you for the careful testing. Very helpful! |
From #1106:
This turns out to be due to a change included in the new Unfortunately, there is no easy way to adjust the Lines 321 to 325 in 10746b1
There are several approaches to resolving this. One would be to add a check like sub isNumber {
my $n = shift;
return $n->{tree}->isNumber if isFormula($n);
return classMatch($n, 'Real', 'Complex') || $n->{isNumber} || matchNumber($n);
} and add Another would be to look for an sub isNumber {
my $n = shift;
return $n->{tree}->isNumber if isFormula($n);
return $n->isNumber if Value->subclassed($n, "isNumber");
return classMatch($n, 'Real', 'Complex') || matchNumber($n);
} and then add sub isNumber { 1 } to the Finally, and perhaps the best option, is to test the MathObject's type: sub isNumber {
my $n = shift;
return $n->{tree}->isNumber if isFormula($n);
return (isValue($n) && ($n->type eq 'Number' || classMatch($n, 'Real', 'Complex'))) || matchNumber($n);
} The I'm not sure which is the best approach, but I'm leaning toward the third one. Discussion welcome. I was never happy about adding fractions to the
The check for reducing fractions is done in the |
If you are leaning toward the third approach, then go with that. I agree that that approach seems nicest.
This is not critical. I can still use a custom grader to check simplification using the |
OK, I've made that change. I think this is ready to go from my end, unless anyone else has things they need fixed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything seems to be working in my tests. Thanks for your work on this.
This PR modifies the
contextFraction.pl
file to use the context-extension framework, making it possible to add fractions to any context. It still creates the original four fraction contexts, but also providescontext::Fraction::extending()
that can be used to add fractions to other contexts. E.g., you could usecontext::Fraction::extending("Matrix")
to get a context in which fractions can be entered in matrices.This should be backward compatible with the current
contextFraction.pl
from an author's perspective, except for the following:strictMultiplication
andstrictMinus
flags have been removed, as they are no longer needed (the strict handling is done by extending theLimitedNumeric
context, rather than reimplementing the strict behavior).class
was modified to returnINTEGER
,MINUS
andFRACTION
, but now that fractions can be attached to arbitrary contexts, we don't want to modify the class of numbers, since that could affect how the other context operates. Instead these values are stored internally in private data within thetypeRef
objects.It appears that at least one problem (Contrib/PCC/BasicAlgebra/LinearEquationApplications/Ratio20.pg) subclasses the
LimitedFraction
class and uses the originalclass
values, so would need to be modified to work with the new contexts. I have renamed the originalcontextFraction.pl
aslegacyFraction.pl
, for use in cases like this, though you all can decide if that is necessary or not.Finally, I modified the
t/contexts/fraction.t
file to include the needed Parser classes for the newcontextFractions.pl
. Of course, that test file doesn't test most of the functionality, and needs many more tests to fully test the various options.This PR includes the
contextExtensions.pl
file, even though I made a separate PR for it. Because that branch isn't in the openwebwork/pg repository, I can't target this PR to that branch. When that PR is merged, thecontextExtensions.pl
file should disappear from here.