用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/ffsshhttiikk/opencode-agents-skills --skill formal-methods命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
基于 SOC 职业分类
正在显示 SKILL.md
| name | formal-methods |
| description | Formal verification methods |
| license | MIT |
| compatibility | opencode |
| metadata | {"audience":"developers","category":"computer-science"} |
When building high-assurance systems where correctness is critical.
class FormalSpec:
"""Design by contract"""
def requires(self, *conditions):
"""Precondition decorator"""
def decorator(func):
def wrapper(*args, **kwargs):
for i, cond in enumerate(conditions):
if not cond(args[i] if i < len(args) else None):
raise PreconditionViolation(
f"Precondition {cond.__name__} failed"
)
return func(*args, **kwargs)
return wrapper
return decorator
def ensures(self, *conditions):
"""Postcondition decorator"""
def decorator(func):
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
for cond in conditions:
if not cond(result):
raise PostconditionViolation(
f"Postcondition {cond.__name__} failed"
)
return result
return wrapper
return decorator
# Example: Sorted list specification
def is_sorted(lst: list) -> bool:
"""Postcondition: list is sorted"""
for i in range(len(lst) - 1):
if lst[i] > lst[i + 1]:
return False
return True
def non_negative(x: int) -> bool:
"""Precondition: x is non-negative"""
return x >= 0
def sorted_insert(lst: list, x: int) -> list:
"""Insert x into sorted list lst"""
result = lst + [x]
result.sort()
return result
class Invariant:
"""Class and loop invariants"""
@staticmethod
def class_invariant(cls):
"""Decorator for class invariant"""
original_init = cls.__init__
def new_init(self, *args, **kwargs):
original_init(self, *args, **kwargs)
if not cls.invariant(self):
raise InvariantViolation("Class invariant violated")
cls.__init__ = new_init
# Check invariant after each public method
original_methods = [m for m in dir(cls)
if not m.startswith('_')
and callable(getattr(cls, m))]
for method_name in original_methods:
original_method = getattr(cls, method_name)
def make_wrapper(method):
def wrapper(self, *args, **kwargs):
result = method(self, *args, **kwargs)
if not cls.invariant(self):
raise InvariantViolation(
f"Invariant violated after "
)
result
wrapper
(cls, method_name, make_wrapper(original_method))
cls
:
():
.items = []
.capacity = capacity
():
(
(.items) >=
(.items) <= .capacity
)
():
(.items) >= .capacity:
OverflowError()
.items.append(item)
():
.items:
IndexError()
.items.pop()
class HoareTriple:
"""Hoare logic for program verification"""
@staticmethod
def verify(Pre: Callable, program: Callable,
Post: Callable) -> bool:
"""Verify Hoare triple: {P} program {Q}"""
# In practice, use theorem prover
pass
@staticmethod
def assignment(x: str, expr: str, post: str) -> str:
"""Hoare rule for assignment: {Q[x/E]} x := E {Q}"""
return f"{{{post.replace(x, expr)}}}"
@staticmethod
def sequence(stmts: List[str], pre: str, post: str) -> str:
"""Hoare rule for sequence"""
# {P} S1; S2 {R} from {P} S1 {Q}; {Q} S2 {R}
return pre
@staticmethod
def conditional(pre: str, cond: str,
post_then: str, post_else: str) -> str:
"""Hoare rule for if"""
# {P} if B then {Q} else {R} from {P && B} S1 {Q} and {P && !B} S2 {R}
class ModelChecker:
"""Simple model checker"""
def __init__(self):
self.states = set()
self.transitions = {}
self.properties = []
def add_state(self, state: str):
self.states.add(state)
def add_transition(self, from_state: str, to_state: str,
action: str):
if from_state not in self.transitions:
self.transitions[from_state] = []
self.transitions[from_state].append((action, to_state))
def check_reachability(self, start: str, goal: str) -> bool:
"""Check if goal is reachable from start"""
visited = set()
stack = [start]
while stack:
state = stack.pop()
if state == goal:
return True
if state in visited:
continue
visited.add(state)
for _, next_state in .transitions.get(state, []):
stack.append(next_state)
() -> :
visited = ()
stack = [start]
stack:
state = stack.pop()
state visited:
visited.add(state)
state good_states:
_, next_state .transitions.get(state, []):
stack.append(next_state)
() -> :
.check_reachability(start,
bad_states.pop() bad_states )