feat: Add tactic object

This commit is contained in:
Leni Aniva 2024-04-22 13:11:28 -07:00
parent 8f64920789
commit 83fcec5d60
Signed by: aniva
GPG Key ID: 4D9B1C8D10EA4C50
2 changed files with 19 additions and 6 deletions

View File

@ -2,7 +2,7 @@
Data structuers for expressions and goals Data structuers for expressions and goals
""" """
from dataclasses import dataclass from dataclasses import dataclass
from typing import Optional, Self from typing import Optional, Self, Union
Expr = str Expr = str
@ -60,3 +60,12 @@ class GoalState:
@property @property
def is_solved(self) -> bool: def is_solved(self) -> bool:
return not self.goals return not self.goals
@dataclass(frozen=True)
class TacticNormal:
payload: str
@dataclass(frozen=True)
class TacticHave:
branch: str
Tactic = Union[TacticNormal, TacticHave]

View File

@ -3,7 +3,7 @@ Class which manages a Pantograph instance. All calls to the kernel uses this
interface. interface.
""" """
import json, pexpect, pathlib, unittest import json, pexpect, pathlib, unittest
from pantograph.expr import Variable, Goal, GoalState from pantograph.expr import Variable, Goal, GoalState, Tactic, TacticNormal
def _get_proc_cwd(): def _get_proc_cwd():
return pathlib.Path(__file__).parent return pathlib.Path(__file__).parent
@ -65,9 +65,13 @@ class Server:
raise ServerError(result["desc"]) raise ServerError(result["desc"])
return GoalState(state_id = result["stateId"], goals = [Goal.sentence(expr)]) return GoalState(state_id = result["stateId"], goals = [Goal.sentence(expr)])
def goal_tactic(self, state: GoalState, goalId: int, tactic: str) -> GoalState: def goal_tactic(self, state: GoalState, goalId: int, tactic: Tactic) -> GoalState:
result = self.run('goal.tactic', { args = { "stateId": state.state_id, "goalId": goalId }
"stateId": state.state_id, "goalId": goalId, "tactic": tactic}) if isinstance(tactic, TacticNormal):
args["tactic"] = tactic.payload
else:
raise Exception(f"Invalid tactic type: {tactic}")
result = self.run('goal.tactic', args)
if "error" in result: if "error" in result:
raise ServerError(result["desc"]) raise ServerError(result["desc"])
if "tacticErrors" in result: if "tacticErrors" in result:
@ -95,7 +99,7 @@ class TestServer(unittest.TestCase):
server = Server() server = Server()
state0 = server.goal_start("forall (p q: Prop), Or p q -> Or q p") state0 = server.goal_start("forall (p q: Prop), Or p q -> Or q p")
self.assertEqual(state0.state_id, 0) self.assertEqual(state0.state_id, 0)
state1 = server.goal_tactic(state0, goalId=0, tactic="intro a") state1 = server.goal_tactic(state0, goalId=0, tactic=TacticNormal("intro a"))
self.assertEqual(state1.state_id, 1) self.assertEqual(state1.state_id, 1)
self.assertEqual(state1.goals, [Goal( self.assertEqual(state1.goals, [Goal(
variables=[Variable(name="a", t="Prop")], variables=[Variable(name="a", t="Prop")],