DisableFunctions
Disabling Functions and Operators in Student Answers: PG Code Snippet
This code snippet shows the essential PG code to Disabling Functions in Student Answers. Note that these are insertions, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.
In general, this can be done in a number of ways: there are a number of available limited contexts that limit the types of answers that students may enter, or, alternately, it is possible to specifically indicate within the context what functions or operations are not allowed. In the following, we first consider the available contexts, and, below that, show how to specifically disallow some functions.
PG problem file  Explanation 

loadMacros("contextLimitedPolynomial.pl"); 
In the initialization section of the file, we need to load the Context that we're using. This will be 
Context("LimitedPolynomial"); # to require that only one monomial of each # degree be included in the polynomial, we # would also set that flag in the Context: # Context()>flags>set(singlePowers=>1); $ans = Compute("x^2 + 3x + 2"); 
There are no additions required in the tagging and description or initialization sections of the problem file. In the problem setup section, we specify the
There are a number of limited contexts like
It is possible to restrict what constants may be raised to powers by modifying the current context (such as Numeric, Complex, Vector, etc., as there is no LimitedPowers context) by issuing the following commands after setting the context. There are several available restrictions, and only one of the last three may be selected:

BEGIN_TEXT Expand the polynomial: $BR \((x+1)(x+2) = \) \{ ans_rule(25) \} END_TEXT 
No changes are required in the text section of the problem file. 
ANS( $ans>cmp() ); 
And in the answer and solution section of the file, we check the answer as usual. 
Alternately, we may find that we need to disable specific functions or operations in student answers. This can be done manually within an existing Context, as shown in the following snippet.
PG problem file  Explanation 

Context("Numeric"); ## to disable specific operations in ## student answers, use the undefine ## method for the operations: Context()>operators>undefine("^","**"); ## we can similarly disable specific ## functions with Context()>functions>undefine("sin","cos","tan","sqrt"); $ans = Compute("1/2"); # To disallow absolute value, disable abs(), # sqrt and exponentiation (for sqrt(x^2) and (x^4)^(1/4)), and # the parentheses  , and give consistent # error messages Context()>functions>disable("abs","sqrt"); Context()>operators>undefine("^","**"); Context()>parens>remove(""); Context()>{error}{convert} = sub { my $message = shift; $message =~ s/Unexpected character '~~'/Absolute value is not allowed/; return $message; }; 
No changes are necessary in the documentation and tagging section of the problem, or in the initialization section. In the problem setup section, we declare a context (here, we've chosen the
We can disable specific operations in the Context: in general, predefined operations are
After disabling the operation, they can be reenabled with
To disable specific functions in the Context, we similarly
In addition, classes of functions can be disabled with
Alternatively, we could use the following syntax. Parser::Context::Functions::Disable('All'); 
BEGIN_TEXT Find the numerical value: \( sin^2(\pi/4) = \) \{ ans_rule(15) \} END_TEXT 
We don't need to make any changes to the text portion of the problem file. 
ANS( $ans>cmp() ); 
Nor in the answer and solution section. 
 POD documentation: Look for contextSomeContext.pl
 PG macro: Look for contextSomeContext.pl