def point(name: str) -> NamedOpetope.OCMT: """ The :math:`\\textbf{OptSet${}^!_m$}` :math:`\\texttt{point}` rule. Introduces a :math:`0`-variable with name ``x``. """ t = NamedOpetope.Typing(NamedOpetope.Term(NamedOpetope.Variable(name, 0)), NamedOpetope.Type([NamedOpetope.Term()])) return NamedOpetope.OCMT(NamedOpetope.EquationalTheory(), NamedOpetope.Context() + t)
def test___eq__(self): self.assertEqual(self.tw, self.tw) self.assertEqual(self.tx, self.tx) self.assertEqual(self.ty, self.ty) self.assertEqual(self.tz, self.tz) self.assertNotEqual(self.tx, self.tz) self.assertNotEqual(self.tw, NamedOpetope.Term(self.w)) self.assertNotEqual(NamedOpetope.Term(NamedOpetope.Variable("x", 0)), NamedOpetope.Term(NamedOpetope.Variable("x", 1)))
def degen(ocmt: NamedOpetope.OCMT, name: str) -> NamedOpetope.Sequent: """ The :math:`\\textbf{OptSet${}^!_m$}` :math:`\\texttt{degen}` rule. Introduces the degenerate pasting diagram on a given variable. """ var = ocmt.context[name] type = ocmt.context.typeOf(var) t = NamedOpetope.Typing( NamedOpetope.Term(var, True), NamedOpetope.Type([NamedOpetope.Term(var)] + type.terms)) return NamedOpetope.Sequent(deepcopy(ocmt.theory), deepcopy(ocmt.context), t)
def test_source(self): with self.assertRaises(DerivationError): self.ctx5.source(NamedOpetope.Variable("A", 3), -1) with self.assertRaises(DerivationError): self.ctx5.source(NamedOpetope.Variable("A", 3), 5) self.assertEqual(self.ctx5.source(NamedOpetope.Variable("A", 3), 0), NamedOpetope.Term(NamedOpetope.Variable("A", 3))) self.assertEqual(self.ctx5.source(NamedOpetope.Variable("A", 3), 1), NamedOpetope.Term(NamedOpetope.Variable("α", 2))) self.assertEqual(self.ctx5.source(NamedOpetope.Variable("A", 3), 2), NamedOpetope.Term(NamedOpetope.Variable("f", 1))) self.assertEqual(self.ctx5.source(NamedOpetope.Variable("A", 3), 3), NamedOpetope.Term(NamedOpetope.Variable("a", 0))) self.assertEqual(self.ctx5.source(NamedOpetope.Variable("A", 3), 4), NamedOpetope.Term())
def test___add__(self): with self.assertRaises(DerivationError): self.ctx5 + NamedOpetope.Typing(self.term1, self.typing1) with self.assertRaises(DerivationError): self.ctx5 + NamedOpetope.Typing(self.term2, self.typing2) with self.assertRaises(DerivationError): self.ctx5 + NamedOpetope.Typing(self.term3, self.typing3) with self.assertRaises(DerivationError): self.ctx5 + NamedOpetope.Typing(self.term4, self.typing4) term = NamedOpetope.Term(NamedOpetope.Variable("x", 2)) term[NamedOpetope.Variable("a", 1)] = NamedOpetope.Term( NamedOpetope.Variable("y", 2)) typing = NamedOpetope.Typing(term, self.typing3) with self.assertRaises(DerivationError): self.ctx5 + typing
def repres(seq: NamedOpetope.Sequent) -> NamedOpetope.OCMT: """ The :math:`\\textbf{OptSet${}^!$}` :math:`\\texttt{repr}` rule. """ if not seq.typing.term.isVariable(): raise DerivationError( "repr rule", "Opt! sequent expected to type a variable, typing {term}", term=repr(seq.typing.term)) res = NamedOpetope.OCMT(deepcopy(seq.theory), deepcopy(seq.context)) # new context for typing in seq.context: v = typing.term.variable if v is None: raise RuntimeError("[repres rule] The premiss context types an " "invalid / null term. In valid proof trees, " "this should not happen") for i in range(1, v.dimension + 1): res.context += NamedOpetope.Typing( NamedOpetope.Term(res.target(v, i)), NamedOpetope.Type(typing.type.terms[i:])) # new theory for tup in seq.context.graftTuples(): b, a = tup res.theory += (res.target(a), b) for a in res.context.variables(): if a.dimension >= 2 and not res.source(a).degenerate: s = res.source(a).variable if s is None: raise RuntimeError( "[repres rule] The premiss context types " "the variable {a} of dimension {dim}, " "whose first source is invalid / null. In " "valid proof trees, this should not happen".format( a=str(a), dim=a.dimension)) res.theory += (res.target(a, 2), res.target(s)) for a in seq.context.variables(): for k in range(0, a.dimension - 1): if res.source(res.target(a, k)).degenerate: c = res.source(res.target(a, k)).variable if c is None: raise RuntimeError("[repres rule] The premiss context " "types the variable {var} of dimension " "{dim}, whose first source is invalid " "/ null. In valid proof trees, this " "should not happen".format( var=str(res.target(a, k)), dim=res.target(a, k).dimension)) res.theory += (res.target(a, k + 2), c) # identification of targets tmp = deepcopy(res.theory) for cls in tmp.classes: elems = list(cls) dim = elems[0].dimension for i in range(1, len(cls)): for k in range(1, dim + 1): res.theory += (res.target(elems[0], k), res.target(elems[i], k)) return res
def test_degen(self): s = NamedOpetope.point("x") s = NamedOpetope.degen(s) self.assertEqual( s.typing.term, NamedOpetope.Term(NamedOpetope.Variable("x", 0), True)) self.assertEqual(len(s.context), 1) with self.assertRaises(DerivationError): NamedOpetope.degen(s)
def pd(ocmt: NamedOpetope.OCMT, name: str) -> NamedOpetope.Sequent: """ The :math:`\\textbf{OptSet${}^!_m$}` :math:`\\texttt{pd}` rule. Introduces the trivial non degenerate pasting diagram on a given variable. """ var = ocmt.context[name] t = NamedOpetope.Typing(NamedOpetope.Term(var), ocmt.context.typeOf(var)) return NamedOpetope.Sequent(deepcopy(ocmt.theory), deepcopy(ocmt.context), t)
def setUp(self): self.w = NamedOpetope.Variable("w", 2) self.x = NamedOpetope.Variable("x", 2) self.y = NamedOpetope.Variable("y", 2) self.z = NamedOpetope.Variable("z", 2) self.a = NamedOpetope.Variable("a", 1) self.b = NamedOpetope.Variable("b", 1) self.c = NamedOpetope.Variable("c", 1) self.d = NamedOpetope.Variable("d", 1) self.e = NamedOpetope.Variable("e", 1) # t = w (a <- x, b <- y (c <- z, d <- _e_)) self.tw = NamedOpetope.Term(self.w) self.tx = NamedOpetope.Term(self.x) self.ty = NamedOpetope.Term(self.y) self.tz = NamedOpetope.Term(self.z) self.ty[self.c] = self.tz self.ty[self.d] = NamedOpetope.Term(self.e, True) self.tw[self.a] = self.tx self.tw[self.b] = self.ty
def setUp(self): self.a = NamedOpetope.Variable("a", 0) self.f = NamedOpetope.Variable("f", 1) self.alpha = NamedOpetope.Variable("α", 2) self.t0 = NamedOpetope.Type([NamedOpetope.Term()]) self.t1 = NamedOpetope.Type( [NamedOpetope.Term(self.a), NamedOpetope.Term()]) self.t2 = NamedOpetope.Type([ NamedOpetope.Term(self.f), NamedOpetope.Term(self.a), NamedOpetope.Term() ]) self.t3 = NamedOpetope.Type([ NamedOpetope.Term(self.alpha), NamedOpetope.Term(self.f), NamedOpetope.Term(self.a), NamedOpetope.Term() ])
def test_pd(self): s = NamedOpetopicSetM.point("x") with self.assertRaises(DerivationError): NamedOpetopicSetM.pd(s, "y") s = NamedOpetopicSetM.pd(s, "x") self.assertTrue(isinstance(s, NamedOpetope.Sequent)) self.assertEqual(len(s.context), 1) self.assertEqual(len(s.theory.classes), 0) self.assertEqual( s.typing.term, NamedOpetope.Term( NamedOpetope.Variable("x", 0), False))
def test_graft(self): """ :todo: Test degenerate grafting """ self.assertEqual( NamedOpetope.Term(self.g), self.sequent.graft(NamedOpetope.Term(self.g), self.c1, NamedOpetope.Term(self.f))) self.assertEqual(self.fgh1, self.fgh2) self.assertEqual(len(self.fgh1.keys()), 1) self.assertTrue( self.sequent.theory.equal(list(self.fgh1.keys())[0], self.c1)) t = list(self.fgh1.values())[0] self.assertTrue(self.sequent.theory.equal(t.variable, self.g)) self.assertEqual(len(t.keys()), 1) self.assertTrue(self.sequent.theory.equal(list(t.keys())[0], self.b1)) self.assertTrue( self.sequent.theory.equal(list(t.values())[0].variable, self.f)) with self.assertRaises(DerivationError): self.sequent.graft(self.fg, self.b1, NamedOpetope.Term(self.f))
def shift(seq: NamedOpetope.Sequent, name: str) -> NamedOpetope.OCMT: """ The :math:`\\textbf{OptSet${}^!_m$}` :math:`\\texttt{shift}` rule. Takes a sequent ``seq`` typing a term ``t`` and introduces a new variable ``x`` having ``t`` as :math:`1`-source. """ n = seq.typing.term.dimension var = NamedOpetope.Variable(name, n + 1) if var in seq.context: raise DerivationError( "shift rule", "NamedOpetope.Variable {var} already typed in context", var=name) typing = NamedOpetope.Typing( NamedOpetope.Term(var), NamedOpetope.Type([seq.typing.term] + seq.typing.type.terms)) res = NamedOpetope.OCMT(deepcopy(seq.theory), deepcopy(seq.context) + typing) # targets of new variable for i in range(1, n + 2): res.context += NamedOpetope.Typing( NamedOpetope.Term(res.target(var, i)), NamedOpetope.Type(typing.type.terms[i:])) # additional theory termVar = seq.typing.term.variable if termVar is None: raise RuntimeError( "[shift rule] Premiss sequent types an invalid term. In valid " "proof trees, this should not happen") if seq.typing.term.degenerate: for i in range(n): res.theory += (res.target(var, i + 2), res.target(termVar, i)) elif n >= 1: seq.theory += (res.target(var, 2), res.target(termVar)) for gt in seq.typing.term.graftTuples(): seq.theory += (res.target(gt[1]), gt[0]) return res
def test___init__(self): with self.assertRaises(DerivationError): NamedOpetope.Type([ NamedOpetope.Term(NamedOpetope.Variable("α", 3)), NamedOpetope.Term(self.f), NamedOpetope.Term(self.a), NamedOpetope.Term() ]) with self.assertRaises(DerivationError): NamedOpetope.Type([ NamedOpetope.Term(self.alpha), NamedOpetope.Term(self.f), NamedOpetope.Term(NamedOpetope.Variable("a", 1)), NamedOpetope.Term() ]) with self.assertRaises(DerivationError): NamedOpetope.Type([])
def setUp(self): self.term1 = NamedOpetope.Term(NamedOpetope.Variable("a", 0)) self.term2 = NamedOpetope.Term(NamedOpetope.Variable("f", 1)) self.term3 = NamedOpetope.Term(NamedOpetope.Variable("α", 2)) self.term4 = NamedOpetope.Term(NamedOpetope.Variable("A", 3)) self.typing1 = NamedOpetope.Type([NamedOpetope.Term()]) self.typing2 = NamedOpetope.Type([self.term1, NamedOpetope.Term()]) self.typing3 = NamedOpetope.Type( [self.term2, self.term1, NamedOpetope.Term()]) self.typing4 = NamedOpetope.Type( [self.term3, self.term2, self.term1, NamedOpetope.Term()]) self.ctx1 = NamedOpetope.Context() self.ctx2 = self.ctx1 + NamedOpetope.Typing(self.term1, self.typing1) self.ctx3 = self.ctx2 + NamedOpetope.Typing(self.term2, self.typing2) self.ctx4 = self.ctx3 + NamedOpetope.Typing(self.term3, self.typing3) self.ctx5 = self.ctx4 + NamedOpetope.Typing(self.term4, self.typing4)
def test_equal(self): self.assertTrue(self.sequent.equal(self.fgh1, self.fgh2)) self.assertTrue( self.sequent.equal(NamedOpetope.Term(self.h), NamedOpetope.Term(self.i))) self.assertTrue( self.sequent.equal( self.sequent.graft(NamedOpetope.Term(self.i), self.c1, self.fg), self.fgh1)) self.assertTrue( self.sequent.equal( self.sequent.graft(NamedOpetope.Term(self.i), self.c1, self.fg), self.fgh2)) self.assertFalse(self.sequent.equal(self.gh, NamedOpetope.Term(self.h))) self.assertFalse(self.sequent.equal(self.gh, NamedOpetope.Term(self.g))) self.assertFalse(self.sequent.equal(self.gh, self.fg))
def test___init__(self): NamedOpetope.Typing(NamedOpetope.Term(NamedOpetope.Variable("a", 0)), NamedOpetope.Type([NamedOpetope.Term()])) NamedOpetope.Typing( NamedOpetope.Term(NamedOpetope.Variable("f", 1)), NamedOpetope.Type([ NamedOpetope.Term(NamedOpetope.Variable("a", 0)), NamedOpetope.Term() ])) with self.assertRaises(DerivationError): NamedOpetope.Typing( NamedOpetope.Term(NamedOpetope.Variable("a", 1)), NamedOpetope.Type([NamedOpetope.Term()])) with self.assertRaises(DerivationError): NamedOpetope.Typing( NamedOpetope.Term(NamedOpetope.Variable("f", 2)), NamedOpetope.Type([ NamedOpetope.Term(NamedOpetope.Variable("a", 0)), NamedOpetope.Term() ])) with self.assertRaises(DerivationError): NamedOpetope.Typing( NamedOpetope.Term(NamedOpetope.Variable("f", 0)), NamedOpetope.Type([ NamedOpetope.Term(NamedOpetope.Variable("a", 0)), NamedOpetope.Term() ]))
def test_isIn(self): self.assertTrue(self.th1.isIn(self.a0, NamedOpetope.Term(self.a0))) self.assertFalse(self.th1.isIn(self.b0, NamedOpetope.Term(self.a0))) self.assertFalse(self.th1.isIn(self.c0, NamedOpetope.Term(self.a0))) self.assertFalse(self.th1.isIn(self.d0, NamedOpetope.Term(self.a0))) self.assertFalse(self.th1.isIn(self.e0, NamedOpetope.Term(self.a0))) self.assertFalse(self.th1.isIn(self.a1, NamedOpetope.Term(self.a0))) self.assertTrue(self.th2.isIn(self.a0, NamedOpetope.Term(self.a0))) self.assertTrue(self.th2.isIn(self.b0, NamedOpetope.Term(self.a0))) self.assertFalse(self.th2.isIn(self.c0, NamedOpetope.Term(self.a0))) self.assertFalse(self.th2.isIn(self.d0, NamedOpetope.Term(self.a0))) self.assertFalse(self.th2.isIn(self.e0, NamedOpetope.Term(self.a0))) self.assertFalse(self.th2.isIn(self.a1, NamedOpetope.Term(self.a0))) self.assertTrue(self.th3.isIn(self.a0, NamedOpetope.Term(self.a0))) self.assertTrue(self.th3.isIn(self.b0, NamedOpetope.Term(self.a0))) self.assertFalse(self.th3.isIn(self.c0, NamedOpetope.Term(self.a0))) self.assertFalse(self.th3.isIn(self.d0, NamedOpetope.Term(self.a0))) self.assertFalse(self.th3.isIn(self.e0, NamedOpetope.Term(self.a0))) self.assertFalse(self.th3.isIn(self.a1, NamedOpetope.Term(self.a0))) self.assertTrue(self.th4.isIn(self.a0, NamedOpetope.Term(self.a0))) self.assertTrue(self.th4.isIn(self.b0, NamedOpetope.Term(self.a0))) self.assertFalse(self.th4.isIn(self.c0, NamedOpetope.Term(self.a0))) self.assertFalse(self.th4.isIn(self.d0, NamedOpetope.Term(self.a0))) self.assertFalse(self.th4.isIn(self.e0, NamedOpetope.Term(self.a0))) self.assertFalse(self.th4.isIn(self.a1, NamedOpetope.Term(self.a0))) self.assertTrue(self.th5.isIn(self.a0, NamedOpetope.Term(self.a0))) self.assertTrue(self.th5.isIn(self.b0, NamedOpetope.Term(self.a0))) self.assertFalse(self.th5.isIn(self.c0, NamedOpetope.Term(self.a0))) self.assertFalse(self.th5.isIn(self.d0, NamedOpetope.Term(self.a0))) self.assertFalse(self.th5.isIn(self.e0, NamedOpetope.Term(self.a0))) self.assertFalse(self.th5.isIn(self.a1, NamedOpetope.Term(self.a0))) self.assertTrue(self.th6.isIn(self.a0, NamedOpetope.Term(self.a0))) self.assertTrue(self.th6.isIn(self.b0, NamedOpetope.Term(self.a0))) self.assertTrue(self.th6.isIn(self.c0, NamedOpetope.Term(self.a0))) self.assertTrue(self.th6.isIn(self.d0, NamedOpetope.Term(self.a0))) self.assertTrue(self.th6.isIn(self.e0, NamedOpetope.Term(self.a0))) self.assertFalse(self.th6.isIn(self.a1, NamedOpetope.Term(self.a0)))
def setUp(self): self.a1 = NamedOpetope.Variable("a1", 0) self.b1 = NamedOpetope.Variable("b1", 0) self.c1 = NamedOpetope.Variable("c1", 0) self.a2 = NamedOpetope.Variable("a2", 0) self.b2 = NamedOpetope.Variable("b2", 0) self.c2 = NamedOpetope.Variable("c2", 0) self.f = NamedOpetope.Variable("f", 1) self.g = NamedOpetope.Variable("g", 1) self.h = NamedOpetope.Variable("h", 1) self.i = NamedOpetope.Variable("h", 1) ctx = NamedOpetope.Context() + \ NamedOpetope.Typing( NamedOpetope.Term(self.a1), NamedOpetope.Type([NamedOpetope.Term()])) + \ NamedOpetope.Typing( NamedOpetope.Term(self.b1), NamedOpetope.Type([NamedOpetope.Term()])) + \ NamedOpetope.Typing( NamedOpetope.Term(self.c1), NamedOpetope.Type([NamedOpetope.Term()])) + \ NamedOpetope.Typing( NamedOpetope.Term(self.f), NamedOpetope.Type( [NamedOpetope.Term(self.a2), NamedOpetope.Term()])) + \ NamedOpetope.Typing( NamedOpetope.Term(self.g), NamedOpetope.Type( [NamedOpetope.Term(self.b2), NamedOpetope.Term()])) + \ NamedOpetope.Typing( NamedOpetope.Term(self.h), NamedOpetope.Type( [NamedOpetope.Term(self.c2), NamedOpetope.Term()])) eqth = NamedOpetope.EquationalTheory() + \ (self.b1, self.b2) + \ (self.c1, self.c2) + \ (self.h, self.i) self.sequent = NamedOpetope.Sequent(eqth, ctx, None) self.fg = self.sequent.graft(NamedOpetope.Term(self.g), self.b2, NamedOpetope.Term(self.f)) self.gh = self.sequent.graft(NamedOpetope.Term(self.h), self.c2, NamedOpetope.Term(self.g)) self.fgh1 = self.sequent.graft(self.gh, self.b2, NamedOpetope.Term(self.f)) self.fgh2 = self.sequent.graft(NamedOpetope.Term(self.h), self.c2, self.fg)
def test_dim(self): self.assertEqual(self.tw.dimension, 2) self.assertEqual(self.tx.dimension, 2) self.assertEqual(self.ty.dimension, 2) self.assertEqual(self.tz.dimension, 2) self.assertEqual(NamedOpetope.Term(self.e, True).dimension, 2)
def test_point(self): s = NamedOpetope.point("x") self.assertEqual(s.typing.term, NamedOpetope.Term(NamedOpetope.Variable("x", 0))) self.assertEqual(len(s.context), 1)
def test_substitute(self): res = self.sequent.substitute(self.fg, self.gh, self.g) self.assertIs(res[1], None) self.assertEqual(res[0], self.fgh1) res = self.sequent.substitute(self.gh, self.fg, self.g) self.assertEqual(res[0], self.fgh1) self.assertIs(res[1], None) res = self.sequent.substitute(NamedOpetope.Term(self.f), NamedOpetope.Term(self.f), self.f) self.assertEqual(res[0], NamedOpetope.Term(self.f)) self.assertIs(res[1], None) res = self.sequent.substitute(NamedOpetope.Term(self.f), NamedOpetope.Term(self.fg), self.f) self.assertEqual(res[0], NamedOpetope.Term(self.fg)) self.assertIs(res[1], None) res = self.sequent.substitute(NamedOpetope.Term(self.f), NamedOpetope.Term(self.fg), self.g) self.assertEqual(res[0], NamedOpetope.Term(self.f)) self.assertIs(res[1], None) res = self.sequent.substitute(NamedOpetope.Term(self.f), NamedOpetope.Term(self.fgh1), self.f) self.assertEqual(res[0], NamedOpetope.Term(self.fgh1)) self.assertIs(res[1], None) res = self.sequent.substitute(self.fgh1, NamedOpetope.Term(self.c1, True), self.g) self.assertEqual( res[0], self.sequent.graft(NamedOpetope.Term(self.h), self.c2, NamedOpetope.Term(self.f))) res = self.sequent.substitute(self.fgh1, NamedOpetope.Term(self.b1, True), self.f) self.assertTrue(self.sequent.equal(res[0], self.gh))