def validate(a, b): """ Validate that a => b. """ parser = ParserWrapper(Parser) # Parse both expressions a = parser.run([a]) b = parser.run([b]) if a.equals(b): return VALIDATE_NOPROGRESS # Evaluate a and b, counting the number of steps # Optimization: if b is encountered while evaluating a, return parser.set_root_node(a) A = a a_steps = 0 for i in xrange(MAXIMUM_REWRITE_STEPS): obj = parser.rewrite() if not obj: break # If b is some reduction of a, it will be detected here if obj.equals(b): return VALIDATE_SUCCESS A = obj a_steps += 1 if not A: return VALIDATE_ERROR parser.set_root_node(b) B, b_steps = parser.rewrite_and_count_all() if not B: return VALIDATE_ERROR # Evaluations must be equal if not A.equals(B): return VALIDATE_FAILURE # If evaluation of b took more staps than evaluation of a, the step from a # to b was probably useless or even bad if b_steps >= a_steps: return VALIDATE_NOPROGRESS # Evaluations match and b is evaluated quicker than a => success return VALIDATE_SUCCESS
def post(self): try: last_line = get_last_line(self) if last_line: parser = ParserWrapper(Parser) response = parser.run([last_line]) if response: response = parser.rewrite(include_step=True, check_implicit=True) if response: hint, step = response self.write({'step': str(step), 'hint': str(hint)}) return self.write({'hint': 'No further reduction is possible.'}) except Exception as e: self.write(format_exception(e))