本文へスキップ
Skillpedia
一般的なプログラミング48k · #4965

invariant-guard

できること

このツールは、より信頼性の高いコードを書くのに役立ちます。実際のコードを書く前に、ルール(不変条件)、処理がどのように終了するか、そして特殊なケースを記述させます。

使う場面

  • ✓バグが潜んでいる可能性のあるアルゴリズムを書くとき。
  • ✓繰り返し処理(ループ)や再帰呼び出しを扱うとき。
  • ✓あらゆる状況で正しく動作するかを確認するためにコードをレビューするとき。

スキルのリンク

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/invariant-guard/SKILL.md

コピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。

使い方

いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。

貼り付け用メッセージ

このスキルを使って手伝ってください:https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/invariant-guard/SKILL.md このリンクのSKILL.mdファイルを読んで、私の作業でその指示に従ってください。
こういうスキルを、AIが自分で見つけてくれたら?無料のSkillpedia MCPをインストール。1行で、作業の前に15,000+のスキルから探してくれます。1分でインストール →

AIへの頼み方の例

“リストの中から最大の要素を見つける関数を書いてください。ただし、その前にループの不変条件、終了の根拠、エッジケースを定義してください。”

毎週5つの新しいスキル。1分で読めます。

仕組み

このツールは、ループや再帰などのコードの各ステップの前、途中、後に成立していなければならない条件を、形式的に明記させます。これにより、見落とされがちな一般的なバグを防ぐことができます。

技術的な内容(元のSKILL.md)

作者による元のテキスト(英語)をGitHubからそのまま表示しています。

invariant-guard — Correctness-First Coding

The model knows what a loop invariant is. It knows recursion needs a base case. It knows about empty lists, integer overflow, and the difference between < and ≤. It just does not write these down before producing code, so it ships subtle correctness bugs that tests do not catch.

invariant-guard fixes the behavior. State the invariants. State the base case. State the termination argument. State the edge cases. Then write the code — and verify that the code maintains what you stated.

Violating the letter of these rules is violating the spirit of the skill. "I know this algorithm" is the exact rationalization that ships off-by-one and missing-postcondition bugs.

When to Use This Skill

Use invariant-guard when writing or reviewing algorithms where the obvious implementation is subtly wrong:

  • Postcondition stronger than the loop's natural invariant: Boyer–Moore majority, Floyd's cycle detection, leftmost vs any binary search, QuickSelect partition.
  • In-place mutation with read+write pointers: dedup-in-place, partition, rotate.
  • Recursion with multiple parameters or accumulator state.
  • Off-by-one suspects with duplicates, empty inputs, boundary values.
  • Iterative refinements that must terminate: fixed-point, Newton, EM.
  • Any function where you catch yourself thinking "I know this algorithm" — the trap is usually in the contract, not the loop body.

Pairs with lemmaly (picks the algorithm) and mathguard (picks the math). Load invariant-guard after the algorithm has been chosen and before the loop body is written.

The Iron Law

NO LOOP OR RECURSION WITHOUT A WRITTEN INVARIANT AND TERMINATION ARGUMENT

If you cannot write the invariant in one sentence, you have not designed the loop. Write code anyway and you are coding by guess — and the bug will be in the case you did not enumerate.

Non-negotiable rules

  1. Every loop gets a one-line invariant. Before writing any loop, state in one sentence what is true at the top of every iteration. Examples:

    • "At loop top: result contains the sum of a[0..i)."
    • "At loop top: lo ≤ target_position ≤ hi."
    • "At loop top: seen contains every element processed so far; dups contains every element that appeared at least twice."

    If you cannot write the invariant in one sentence, you have not designed the loop yet.

  2. Every loop gets a one-line termination argument. Name the quantity that strictly decreases (or strictly increases toward a bound) on every iteration. Examples:

    • "hi − lo strictly decreases each iteration."
    • "i increases by 1 and is bounded above by n."
    • "stack.length strictly decreases each pop; nothing pushes inside this branch."

    No termination argument, no loop.

  3. Every recursion gets an explicit base case and a measure. Before writing a recursive function, state:

    • The base case(s) — the smallest inputs that return without recursing.
    • The measure — a non-negative integer that strictly decreases on every recursive call (e.g. len(xs), hi − lo, depth, n).
    • The combination — how the recursive results combine into the answer.

    No base case + measure, no recursion. (Mutual recursion: state the measure across the cycle.)

  4. List edge cases before writing, not after. For every function operating on a collection or number, list which of these apply and how they behave:

    • Empty input ([], "", null, undefined, None).
    • Singleton ([x]).
    • All-equal elements.
    • Already-sorted / reverse-sorted input.
    • Duplicates (when uniqueness is assumed).
    • Negative numbers, zero, exactly the boundary value.
    • Integer overflow / underflow at the type max/min.
    • NaN, ±Infinity, -0, denormals (for floats).
    • Off-by-one boundaries: index 0, index n−1, index n, length 0, length 1.
    • Concurrent modification while iterating.

    The cases that apply must each have a one-phrase expected behavior written down.

  5. Make illegal states unreachable, not just unhandled. Prefer encoding constraints in types and structure so the wrong state cannot be constructed:

    • Sum type over boolean flag soup (Loading | Loaded(data) | Error(msg) not {loading, data, error}).
    • Newtype for IDs that must not be swapped (UserId vs OrderId).
    • Non-empty list type when the function requires at least one element.

…

よくある質問

「ループの不変条件」とは何ですか?

ループの各繰り返しの開始時に、常に成り立っている命題のことです。例えば「変数Xは、これまでに処理した要素の合計を常に保持している」などがそれにあたります。

「終了の根拠」とは何ですか?

ループや再帰関数がどのように終わるかの説明です。通常は、各ステップで特定の量が減少または増加し、ある限界に向かっていくことを示します。

なぜ最初に「エッジケース」を挙げるのですか?

多くのバグは、空のリスト、要素が一つだけのリスト、重複する値などの普通でない状況で発生するからです。事前にこれらを考えておくと、エラーを防げます。

詳細

作者による元の説明

Correctness-first: forces writing the function contract, loop invariant, termination argument, and edge cases BEFORE code. Catches Boyer-Moore, leftmost binary search, QuickSelect traps.

repo: sickn33/agentic-awesome-skills

人気

GitHubのスター
48k
ランキング
#4965
ライセンス
MIT
更新日
2026/10/15

このスキルは役に立ちますか?

0

ログイン して投票しましょう。

コメント

  • …

このリポジトリの他のスキル

似ているスキル