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 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 sum(ocmt1: NamedOpetope.OCMT, ocmt2: NamedOpetope.OCMT) -> NamedOpetope.OCMT: """ The :math:`\\textbf{OptSet${}^!$}` :math:`\\texttt{sum}` rule. """ if len(ocmt1.context.variables() & ocmt2.context.variables()) != 0: raise DerivationError( "sum rule", "The two premiss OCTM are expected to have disjoint contexts, " "but intersection types the following variables {inter}", inter=ocmt1.context.variables() & ocmt2.context.variables()) return NamedOpetope.OCMT(ocmt1.theory | ocmt2.theory, ocmt1.context | ocmt2.context)
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 zero() -> NamedOpetope.OCMT: """ The :math:`\\textbf{OptSet${}^!$}` :math:`\\texttt{zero}` rule. """ return NamedOpetope.OCMT(NamedOpetope.EquationalTheory(), NamedOpetope.Context())