"""Notation for terms and proofs: reading strings and printing (lecture 3). This is not a part of the kernel, but what the kernel checks depends on this module: if the parser misreads a theorem, the kernel checks a different statement from the one we had in mind. Grammar (Boolean algebra in Huntington's notation, the complement as n(x), as in the papers on the Robbins problem): sum := product ('+' product)* x + y + z = (x + y) + z product := atom ('*' atom)* atom := variable | 0 | 1 | n(sum) | (sum) """ import re from kernel import Refl, Symm, Trans, CongrArg, Inst _TOKEN = re.compile(r"\s*(?:(\d+)|([A-Za-z_][A-Za-z0-9_]*)|(.))") def _tokens(text): result = [] for m in _TOKEN.finditer(text): if m.group(0).strip(): result.append(m.group(1) or m.group(2) or m.group(3)) return result def term(text): """'x + n(x)' -> ('+', 'x', ('n', 'x'))""" toks = _tokens(text) pos = 0 def peek(): return toks[pos] if pos < len(toks) else None def take(expected=None): nonlocal pos z = peek() if z is None or (expected and z != expected): raise SyntaxError(f"in {text!r}: expected " f"{expected or 'an expression'}, got {z!r}") pos += 1 return z def sum_term(): t = product_term() while peek() == "+": take() t = ("+", t, product_term()) return t def product_term(): t = atom() while peek() == "*": take() t = ("*", t, atom()) return t def atom(): z = take() if z == "(": t = sum_term() take(")") return t if z == "n" and peek() == "(": take("(") t = sum_term() take(")") return ("n", t) if z in ("0", "1") or (z.isidentifier() and z != "n"): return z raise SyntaxError(f"in {text!r}: unexpected character {z!r}") t = sum_term() if peek() is not None: raise SyntaxError(f"in {text!r}: unexpected {peek()!r} at the end") return t def equation(text): """'x + x = x' -> (('+', 'x', 'x'), 'x')""" left, right = text.split("=") return term(left), term(right) def show(t, prec=0): """Printing with the fewest parentheses (prec: 0, 1 or 2).""" if isinstance(t, str): return t f = t[0] if f == "n": return f"n({show(t[1])})" if f == "+": s = f"{show(t[1], 0)} + {show(t[2], 1)}" return f"({s})" if prec >= 1 else s if f == "*": s = f"{show(t[1], 1)} * {show(t[2], 2)}" return f"({s})" if prec >= 2 else s return f"{f}({', '.join(show(a) for a in t[1:])})" def show_equation(r): return f"{show(r[0])} = {show(r[1])}" def show_lean(t, prec=0): """A term in Lean notation: the complement is a function, so n x.""" if isinstance(t, str): return t f = t[0] if f == "n": s = f"n {show_lean(t[1], 3)}" return f"({s})" if prec >= 3 else s if f == "+": s = f"{show_lean(t[1], 0)} + {show_lean(t[2], 1)}" return f"({s})" if prec >= 1 else s if f == "*": s = f"{show_lean(t[1], 1)} * {show_lean(t[2], 2)}" return f"({s})" if prec >= 2 else s raise ValueError(t) def lean(d): """A proof in Lean notation (Eq.refl, Eq.symm, Eq.trans, congrArg).""" def arg(t): return show_lean(t, 3) match d: case Refl(t): return f"Eq.refl {arg(t)}" case Symm(p): return f"Eq.symm ({lean(p)})" case Trans(p, q): return f"Eq.trans ({lean(p)}) ({lean(q)})" case CongrArg(z, C, p): return f"congrArg (fun {z} => {show_lean(C)}) ({lean(p)})" case Inst(name, args): return " ".join([name] + [arg(a) for a in args]) raise ValueError(d) def size(d): """The number of nodes of a proof tree (applications of rules).""" match d: case Refl(_) | Inst(_, _): return 1 case Symm(p) | CongrArg(_, _, p): return 1 + size(p) case Trans(p, q): return 1 + size(p) + size(q) raise ValueError(d)