Exemplo n.º 1
0
 def test_desugar_expression_if_easy(self):
     a = ['if', ['flip'], '0', '1']
     syntax = macro_system.expand(a)
     b = [[
         'biplex', ['flip'], ['make_csp', ['quote', []], ['quote', '0']],
         ['make_csp', ['quote', []], ['quote', '1']]
     ]]
     self.assertEqual(syntax.desugared(), b)
     self.assertEqual(syntax.resugar_index([0, 3, 2, 1]), [3])
Exemplo n.º 2
0
 def expand(self, exp):
     exp = self._canonicalize(exp)
     expanded = []
     for i, s in enumerate(exp):
         try:
             expanded.append(expand(s))
         except VentureException as e:
             e.data['expression_index'].insert(0, i)
             raise
     return ListSyntax(expanded)
Exemplo n.º 3
0
 def test_desugar_expression_do(self):
     a = ['do', ['+', 'a', 'b'], ['*', 'c', 'd']]
     syntax = macro_system.expand(a)
     b = [
         'bind_', ['+', 'a', 'b'],
         ['make_csp', ['quote', []], ['quote', ['*', 'c', 'd']]]
     ]
     self.assertEqual(syntax.desugared(), b)
     self.assertEqual(syntax.resugar_index([1, 1]), [1, 1])
     self.assertEqual(syntax.resugar_index([2, 2, 1, 1]), [2, 1])
Exemplo n.º 4
0
 def test_desugar_expression_and(self):
     a = ['and', 'a', 'b']
     syntax = macro_system.expand(a)
     b = [[
         'biplex', 'a', ['make_csp', ['quote', []], ['quote', 'b']],
         ['make_csp', ['quote', []], ['quote', v.boolean(False)]]
     ]]
     self.assertEqual(syntax.desugared(), b)
     self.assertEqual(syntax.resugar_index([0, 1]), [1])
     self.assertEqual(syntax.resugar_index([0, 2, 2, 1]), [2])
Exemplo n.º 5
0
 def expand(self, exp):
     verify(self.pattern, exp, self.pattern[0])
     try:
         bindings = bind(self.pattern, exp)
         subbed = sub(bindings, self.template)
         expanded = expand(subbed)
         return SubstitutionSyntax(expanded, self)
     except VentureException as e:
         e.data['expression_index'] = self.resugar(
             e.data['expression_index'])
         raise
Exemplo n.º 6
0
 def test_desugar_expression_let_4(self):
     a = ['let', [['a', '1'], ['b', '2']], ['+', 'a', 'b']]
     syntax = macro_system.expand(a)
     b = [[
         'make_csp', ['quote', ['a']],
         [
             'quote',
             [['make_csp', ['quote', ['b']], ['quote', ['+', 'a', 'b']]],
              '2']
         ]
     ], '1']
     self.assertEqual(syntax.desugared(), b)
     self.assertEqual(syntax.resugar_index([0, 1, 1, 0]), [1, 0, 0])
     self.assertEqual(syntax.resugar_index([1]), [1, 0, 1])
     self.assertEqual(syntax.resugar_index([0, 2, 1, 0, 1, 1, 0]),
                      [1, 1, 0])
     self.assertEqual(syntax.resugar_index([0, 2, 1, 1]), [1, 1, 1])
     self.assertEqual(syntax.resugar_index([0, 2, 1, 0, 2, 1]), [2])
Exemplo n.º 7
0
 def test_desugar_expression_lambda(self):
     a = ['lambda', ['x'], ['+', 'x', 'x']]
     syntax = macro_system.expand(a)
     b = ['make_csp', ['quote', ['x']], ['quote', ['+', 'x', 'x']]]
     self.assertEqual(syntax.desugared(), b)
     self.assertEqual(syntax.resugar_index([2, 1, 2]), [2, 2])
Exemplo n.º 8
0
 def test_desugar_expression_list(self):
     a = [['+', '1', ['*', '2', '3']]]
     syntax = macro_system.expand(a)
     b = [['+', '1', ['*', '2', '3']]]
     self.assertEqual(syntax.desugared(), b)
     self.assertEqual(syntax.resugar_index([0, 2, 0]), [0, 2, 0])
Exemplo n.º 9
0
 def _call_core_sivm_instruction(self, instruction):
     desugared_instruction = copy.copy(instruction)
     instruction_type = instruction['instruction']
     predicted_did = None
     # desugar the expression
     if instruction_type in [
             'assume',
             'define',
             'evaluate',
             'infer',
             'labeled_assume',
             'labeled_observe',
             'labeled_predict',
             'observe',
             'predict',
             'predict_all',
     ]:
         exp = utils.validate_arg(instruction,
                                  'expression',
                                  utils.validate_expression,
                                  wrap_exception=False)
         syntax = macro_system.expand(exp)
         desugared_instruction['expression'] = syntax.desugared()
         # for error handling
         predicted_did = self._record_running_instruction(
             instruction, (exp, syntax))
     if instruction_type == 'forget':
         forgotten_did = instruction['directive_id']
     elif instruction_type == 'labeled_forget':
         label = utils.validate_arg(instruction, 'label',
                                    utils.validate_symbol)
         forgotten_did = self.core_sivm.engine.get_directive_id(label)
     else:
         forgotten_did = None
     try:
         response = self.core_sivm.execute_instruction(
             desugared_instruction)
     except VentureException as e:
         if self._do_not_annotate:
             raise
         import sys
         info = sys.exc_info()
         try:
             e = self._annotate(e, instruction)
         except Exception:
             print "Trying to annotate an exception at SIVM level led to:"
             import traceback
             print traceback.format_exc()
             raise e, None, info[2]
         finally:
             if instruction_type in [
                     'define', 'assume', 'observe', 'predict',
                     'predict_all', 'evaluate', 'infer'
             ]:
                 # After annotation completes, clear the syntax
                 # dictionary, because the instruction was
                 # (presumably!) not recorded in the underlying
                 # engine (so e.g. future list_directives commands
                 # should not list it)
                 if predicted_did in self.syntax_dict:
                     del self.syntax_dict[predicted_did]
         raise e, None, info[2]
     self._register_executed_instruction(instruction, predicted_did,
                                         forgotten_did, response)
     return response