def test_sustituir_paren(self): s = Sustitucion() s.push(Var('a'), Fun(Var('b'), Int())) self.assertEqual( s.sustituir(Paren(Var('a'))), Paren(Fun(Var('b'), Int())) )
def test_unify_var(self): a = Var('a') b = Fun(Int(), Bool()) s = Sustitucion() s.push(Var('a'), Fun(Int(), Bool())) self.assertEqual(a.unify(b), s)
def test_unify_fun_var(self): a = Fun(Fun(Var('a'), Int()), Var('a')) b = Var('b') s = Sustitucion() s.push(Var('b'), Fun(Fun(Var('a'), Int()), Var('a'))) self.assertEqual(a.unify(b), s)
def test_assign_variable_int(self): a = Ambiente() a.extend(Variable('x'), Var('a')) s = Sustitucion() s.push(Var('a'), Int()) self.assertEqual(a.assign(Variable('x'), Int()), s)
def test_assign_lambda_fun(self): a = Ambiente() s = Sustitucion() s.push(Var('a'), Bool()) s.push(Var('b'), Int()) self.assertEqual(a.assign(Lambda(Variable('x'), Entero(12)), Fun(Bool(), Int())), s)
def test_unify_paren(self): a = Fun(Paren(Paren(Fun(Var('a'), Int()))), Var('a')) b = Fun(Paren(Var('b')), Paren(Fun(Var('c'), Paren(Int())))) s = Sustitucion() s.push(Var('b'), Fun(Fun(Var('c'), Int()), Int())) s.push(Var('a'), Fun(Var('c'), Int())) self.assertEqual(a.unify(b), s)
def test_unify_simple(self): a = Fun(Var('a'), Var('b')) b = Fun(Bool(), Int()) s = Sustitucion() s.push(Var('a'), Bool()) s.push(Var('b'), Int()) self.assertEqual(a.unify(b), s)
def test_compose_simple(self): s1 = Sustitucion() s1.push(Var('a'), Fun(Var('c'), Int())) s2 = Sustitucion() s2.push(Var('b'), Fun(Var('a'), Int())) s = Sustitucion() s.push(Var('a'), Fun(Var('c'), Int())) s.push(Var('b'), Fun(Var('a'), Int())) self.assertEqual(s1.compose(s2), s)
def test_sustituir_tipo_base(self): s = Sustitucion() s.push(Var('a'), Fun(Var('b'), Int())) self.assertEqual( s.sustituir(Int()), Int() ) self.assertEqual( s.sustituir(Bool()), Bool() )
def test_assign_aplicar_int(self): a = Ambiente() x = Aplicacion(Lambda(Variable('x'), Suma(Variable('x'), Entero(1))), Entero(12)) y = Int() s = Sustitucion() s.push(Var('a'), Int()) s.push(Var('b'), Int()) self.assertEqual(a.assign(x, y), s)
def test_size(self): s = Sustitucion() self.assertEqual(s.size(), 0) s.push(1, 2) self.assertEqual(s.size(), 1) s.pop() self.assertEqual(s.size(), 0)
def unify(self, t): if (isinstance(t, Fun)): s_domain = self.domain.unify(t.domain) s_range = self.range.unify(t.range).compose(s_domain) try: s_self = self.domain.unify(self.range) except: s_self = Sustitucion() try: s_t = t.domain.unify(t.range) except: s_t = Sustitucion() return s_range.compose(s_domain.compose(s_t.compose(s_self.compose(s_range)))) else: return t.unify(self)
def test_compose(self): s1 = Sustitucion() s1.push(Var('a'), Fun(Var('b'), Int())) s1.push(Var('w'), Fun(Var('c'), Fun(Var('b'), Bool()))) s2 = Sustitucion() s2.push(Var('b'), Bool()) s2.push(Var('c'), Int()) s2.push(Var('a'), Bool()) s2.push(Var('w'), Int()) s = s1.compose(s2) self.assertEqual( s.all(), [ (Var('a'), Fun(Bool(), Int())), (Var('w'), Fun(Int(), Fun(Bool(), Bool()))), (Var('b'), Bool()), (Var('c'), Int()), ] )
def test_pop(self): s = Sustitucion() s.push(1, 2) self.assertEqual(s.pop(), (1, 2))
def test_contains(self): s = Sustitucion() self.assertFalse(s.contains(Var('x'))) s.push(Var('a'), 1) self.assertTrue(s.contains(Var('a')))
def test_find(self): s = Sustitucion() s.push(Var('a'), Fun(Var('x'), Var('y'))) self.assertEqual(s.find(Var('a')), Fun(Var('x'), Var('y')))
def test_sustituir_vacio(self): s = Sustitucion() self.assertEqual( s.sustituir(Fun(Var('a'), Var('b'))), Fun(Var('a'), Var('b')) )
def unify(self, t): u = Sustitucion() u.push(self, t) return u
def test_push(self): s = Sustitucion() self.assertEqual(s.size(), 0) s.push(Var('a'), Int()) self.assertEqual(s.size(), 1)