doc: autodocs for expr and data

This commit is contained in:
Leni Aniva 2024-10-20 09:52:12 -07:00
parent 1f3784d12c
commit 75221cca0b
Signed by: aniva
GPG Key ID: 4D9B1C8D10EA4C50
6 changed files with 49 additions and 7 deletions

View File

@ -11,3 +11,5 @@ parts:
chapters: chapters:
- file: api-server - file: api-server
- file: api-search - file: api-search
- file: api-expr
- file: api-data

5
docs/api-data.rst Normal file
View File

@ -0,0 +1,5 @@
Data
=============
.. automodule:: pantograph.compiler
:members:

8
docs/api-expr.rst Normal file
View File

@ -0,0 +1,8 @@
Expr
=============
.. automodule:: pantograph.expr
:members:
.. autodata:: pantograph.expr.Expr
.. autodata:: pantograph.expr.Tactic

View File

@ -2,6 +2,10 @@ from dataclasses import dataclass
@dataclass(frozen=True) @dataclass(frozen=True)
class TacticInvocation: class TacticInvocation:
"""
One tactic invocation with the before/after goals extracted from Lean source
code.
"""
before: str before: str
after: str after: str
tactic: str tactic: str

View File

@ -2,11 +2,14 @@
Data structuers for expressions and goals Data structuers for expressions and goals
""" """
from dataclasses import dataclass, field from dataclasses import dataclass, field
from typing import Optional, Union from typing import Optional, TypeAlias
Expr = str Expr: TypeAlias = str
def parse_expr(payload: dict) -> Expr: def parse_expr(payload: dict) -> Expr:
"""
:meta private:
"""
return payload["pp"] return payload["pp"]
@dataclass(frozen=True) @dataclass(frozen=True)
@ -25,6 +28,9 @@ class Variable:
return Variable(t, v, name) return Variable(t, v, name)
def __str__(self): def __str__(self):
"""
:meta public:
"""
result = self.name if self.name else "_" result = self.name if self.name else "_"
result += f" : {self.t}" result += f" : {self.t}"
if self.v: if self.v:
@ -41,6 +47,9 @@ class Goal:
@staticmethod @staticmethod
def sentence(target: Expr): def sentence(target: Expr):
"""
:meta public:
"""
return Goal(variables=[], target=target) return Goal(variables=[], target=target)
@staticmethod @staticmethod
@ -56,6 +65,9 @@ class Goal:
return Goal(variables, target, sibling_dep, name, is_conversion) return Goal(variables, target, sibling_dep, name, is_conversion)
def __str__(self): def __str__(self):
"""
:meta public:
"""
front = "|" if self.is_conversion else "" front = "|" if self.is_conversion else ""
return "\n".join(str(v) for v in self.variables) + \ return "\n".join(str(v) for v in self.variables) + \
f"\n{front} {self.target}" f"\n{front} {self.target}"
@ -74,6 +86,8 @@ class GoalState:
def is_solved(self) -> bool: def is_solved(self) -> bool:
""" """
WARNING: Does not handle dormant goals. WARNING: Does not handle dormant goals.
:meta public:
""" """
return not self.goals return not self.goals
@ -87,6 +101,9 @@ class GoalState:
return GoalState.parse_inner(payload["nextStateId"], payload["goals"], _sentinel) return GoalState.parse_inner(payload["nextStateId"], payload["goals"], _sentinel)
def __str__(self): def __str__(self):
"""
:meta public:
"""
return "\n".join([str(g) for g in self.goals]) return "\n".join([str(g) for g in self.goals])
@dataclass(frozen=True) @dataclass(frozen=True)
@ -102,7 +119,7 @@ class TacticHave:
@dataclass(frozen=True) @dataclass(frozen=True)
class TacticLet: class TacticLet:
""" """
The `have` tactic, equivalent to The `let` tactic, equivalent to
```lean ```lean
let {binder_name} : {branch} := ... let {binder_name} : {branch} := ...
``` ```
@ -126,4 +143,4 @@ class TacticExpr:
""" """
expr: str expr: str
Tactic = Union[str, TacticHave, TacticLet, TacticCalc, TacticExpr] Tactic: TypeAlias = str | TacticHave | TacticLet | TacticCalc | TacticExpr

View File

@ -100,6 +100,8 @@ class Server:
def run(self, cmd, payload): def run(self, cmd, payload):
""" """
Runs a raw JSON command. Preferably use one of the commands below. Runs a raw JSON command. Preferably use one of the commands below.
:meta private:
""" """
assert self.proc assert self.proc
s = json.dumps(payload) s = json.dumps(payload)
@ -123,9 +125,7 @@ class Server:
def gc(self): def gc(self):
""" """
Garbage collect deleted goal states. Garbage collect deleted goal states to free up memory.
Must be called periodically.
""" """
if not self.to_remove_goal_states: if not self.to_remove_goal_states:
return return
@ -145,6 +145,9 @@ class Server:
return parse_expr(result["type"]) return parse_expr(result["type"])
def goal_start(self, expr: Expr) -> GoalState: def goal_start(self, expr: Expr) -> GoalState:
"""
Create a goal state with one root goal, whose target is `expr`
"""
result = self.run('goal.start', {"expr": str(expr)}) result = self.run('goal.start', {"expr": str(expr)})
if "error" in result: if "error" in result:
print(f"Cannot start goal: {expr}") print(f"Cannot start goal: {expr}")
@ -152,6 +155,9 @@ class Server:
return GoalState(state_id=result["stateId"], goals=[Goal.sentence(expr)], _sentinel=self.to_remove_goal_states) return GoalState(state_id=result["stateId"], goals=[Goal.sentence(expr)], _sentinel=self.to_remove_goal_states)
def goal_tactic(self, state: GoalState, goal_id: int, tactic: Tactic) -> GoalState: def goal_tactic(self, state: GoalState, goal_id: int, tactic: Tactic) -> GoalState:
"""
Execute a tactic on `goal_id` of `state`
"""
args = {"stateId": state.state_id, "goalId": goal_id} args = {"stateId": state.state_id, "goalId": goal_id}
if isinstance(tactic, str): if isinstance(tactic, str):
args["tactic"] = tactic args["tactic"] = tactic