Guest User

Untitled

a guest
Oct 16th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. compose = lambda f: lambda g: lambda *a, **k: f(g(*a, **k))
  2.  
  3. class Formula:
  4. def __rshift__(self, other):
  5. return Impl(self, other)
  6.  
  7. class Atom(Formula):
  8. def __init__(self, name):
  9. self.name = name
  10.  
  11. def __repr__(self):
  12. return f"Atom('{name}')"
  13.  
  14. def __str__(self):
  15. return self.name
  16.  
  17. class Impl(Formula):
  18. def __init__(self, prem, conc):
  19. self.prem = prem
  20. self.conc = conc
  21.  
  22. def __repr__(self):
  23. return f"({self.prem} >> {self.conc})"
  24.  
  25. class Proof:
  26. def __init__(self, context, goal):
  27. self.context = context
  28. self.goal = goal
  29. self.proved = False
  30. self.subproofs = []
  31.  
  32. def do_assume(self):
  33. assert("Assumption is possible" and isinstance(self.goal, Impl))
  34. self.context.append(self.goal.prem)
  35. self.goal = self.goal.conc
  36.  
  37. def do_use(self, n):
  38. assert("Context formula in range" and 0 <= n < len(self.context))
  39. f = self.context[n]
  40. premises = []
  41. while f != self.goal:
  42. assert("Formula is usable for goal" and isinstance(f, Impl))
  43. premises.append(f.prem)
  44. f = f.conc
  45. for premise in premises:
  46. self.subproofs.append(Proof(self.context, premise))
  47. self.proved = True
  48.  
  49. def open_problems(self):
  50. if not self.proved:
  51. yield self
  52. for subproof in self.subproofs:
  53. yield from subproof.open_problems()
  54.  
  55. def next_problem(self):
  56. return next(self.open_problems())
  57.  
  58. def assume(self):
  59. self.next_problem().do_assume()
  60.  
  61. def use(self, n):
  62. self.next_problem().do_use(n)
  63.  
  64. @compose('\n'.join)
  65. def __str__(self):
  66. for i, f in enumerate(self.context):
  67. yield f"{i:>2}: {f}"
  68. yield "-" * 40
  69. yield f" {self.goal}"
  70. yield ""
  71.  
  72. @compose('\n'.join)
  73. def status(self):
  74. problems = [str(p) for p in self.open_problems()]
  75. if problems:
  76. yield from reversed(problems)
  77. else:
  78. yield "Done."
  79.  
  80.  
  81. P, Q, R = (Atom(x) for x in "PQR")
  82. p = Proof([], (P >> (Q >> R)) >> (Q >> (P >> R)))
  83. p.assume()
  84. p.assume()
  85. p.assume()
  86. p.use(0)
  87. p.use(2)
  88. p.use(1)
  89.  
  90. print(p.status())
Add Comment
Please, Sign In to add comment