Logo: Imperative To Coq Model Extractor

Imperative To Coq Model Extractor

Communautéit
ArabelaTso
imperative-to-coq-model-extractor

Extract abstract mathematical models from imperative code (C, C++, Python, Java, etc.) suitable for formal reasoning in Coq. Use when the user asks to model imperative code in Coq, create Coq specifications from imperative programs, extract mathematical models for verification, or translate imperative algorithms to Coq for formal reasoning and proof.

Iwwersiicht

EditeurArabelaTso
RepositorySkills-4-SE
Skill-Nummimperative-to-coq-model-extractor
Stären
258
Forks
25
Matgeliwwert Dateien
1
LizenzApache-2.0
Linken
  • Instruktiounen am Markdown-Format

    Eng SKILL.md-Datei, déi de Modell bei Bedarf luet, soudatt nëmmen Token ufalen, wann eng Ufro tatsächlech dozou passt.

  • Funktionéiert mat all LLM

    KI-Skills si reng Markdown, keen ubidderspezifesche Code. Dofir funktionéiert dëse Skill mat GPT, Claude, Gemini, Grok oder engem lokale Modell.

  • 1 matgeliwwert Dateien

    Skripten, Virlagen a Referenzen, déi de Modell beim Schaffe liese kann. D'Dateie kënnen nëmme gelies ginn a ginn ni ausgeféiert.

  • Open Source

    ArabelaTso huet de Skill op GitHub verëffentlecht. Liest de Quelltext, ier Dir de Skill installéiert.

Installatioun

Installéiert de KI-Skill Imperative To Coq Model Extractor an TypingMind, fir e mat all LLM ze benotzen, oder setzt en an en anere KI-Agent an, deen SKILL.md liest.

1

An TypingMind installéieren

TypingMind installéiert e Skill direkt aus sengem GitHub-Dossier — dobäi gëtt SKILL.md gelies, d'Ressourcedateie gi gebëndelt an d'Resultat gëtt lokal gespäichert.

  1. Maacht d'App op a gitt op Plugins → Skills.
  2. Wielt „Vu GitHub installéieren“.
  3. Pecht d'URL vum Skill-Dossier, déi hei drënner steet, an a bestätegt d'Installatioun.
    https://github.com/ArabelaTso/Skills-4-SE/tree/main/skills/imperative-to-coq-model-extractor
  4. Aktivéiert de Skill an all Chat, an deem Dir e benotze wëllt.
Plugins → Skills → Skill derbäisetzen → Iwwer eng GitHub-URL. Pecht dann d'URL vum Dossier an a klickt op Weider.
2

An engem anere KI-Agent installéieren

All KI-Agent, deen d'Format Agent Skills liest, kann dëse Skill benotzen — kopéiert den Dossier an d'Skill-Verzeechnes vun deem KI-Agent.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/ArabelaTso/Skills-4-SE.git /tmp/Skills-4-SE
mkdir -p .claude/skills
cp -r /tmp/Skills-4-SE/skills/imperative-to-coq-model-extractor .claude/skills/imperative-to-coq-model-extractor
Start Claude Code nom Kopéieren nei, fir datt den neie Skill erkannt gëtt.

An TypingMind benotzen

Aktivéiert Imperative To Coq Model Extractor an engem Chat vun TypingMind, an de Modell iwwerhëlt de Rescht. Den Numm an d'Beschreiwung stinn am System-Prompt. Soubal eng Ufro dozou passt, luet de Modell déi komplett Instruktioune selwer — Dir musst de Skill ni manuell opruffen, an et fale keng Token un, bis en tatsächlech benotzt gëtt.

De Modell luet Imperative To Coq Model Extractor automatesch, soubal eng Ufro dozou passt.

Funktionéiert mat all KI-Modell

KI-Skills si reng Markdown-Instruktiounen amplaz vun ubidderspezifeschem Code. Dofir ass Imperative To Coq Model Extractor net un de Modell gebonnen, fir deen de Skill geschriwwe gouf. Installéiert en eemol an TypingMind a benotzt e mat GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama oder engem lokale Modell, deen Dir selwer bedreift — alles mat Ären eegenen API-Schlësselen.

  • Gëtt nëmme gelueden, wann et néideg ass

    De System-Prompt enthält nëmmen den Numm an d'Beschreiwung. D'Instruktioune gi bei der éischter passender Ufro gelueden, soudatt en onbenotzte Skill näischt kascht.

  • De Modell am lafende Chat wiesselen

    Well de Skill aus Instruktiounen amplaz vu Code besteet, funktionéiert en och no engem Modellwiessel — den nächste Modell liest déi selwecht SKILL.md-Datei.

Skill-Instruktiounen

Dëst ass den Inhalt vu SKILL.md, deen de Modell luet. Liest e virun der Installatioun — e Skill besteet aus Instruktiounen, déi Äre Modell befollegt.

Imperative to Coq Model Extractor

Overview

Extract abstract mathematical models from imperative code that can be used for formal reasoning and verification in Coq. This skill transforms imperative programs into functional Coq definitions with explicit state modeling, enabling formal proofs about program behavior.

Extraction Workflow

Step 1: Analyze Imperative Code

Understand the program structure and semantics:

  1. Identify components:

    • Functions and their signatures
    • Data structures and types
    • State and mutable variables
    • Control flow (loops, conditionals)
    • Side effects and I/O
  2. Understand semantics:

    • What does the program compute?
    • What state does it maintain?
    • What are the invariants?
    • What are preconditions and postconditions?
  3. Note modeling challenges:

    • Mutable state
    • Imperative loops
    • Pointers and memory
    • Side effects

Step 2: Design Coq Model Structure

Plan the Coq representation:

  1. Choose appropriate types:

    • Z for arbitrary precision integers
    • nat for natural numbers
    • bool for booleans
    • list for sequences
    • Record for structured state
    • Inductive types for enumerations
  2. Model state explicitly:

    • Pure functions: Direct translation
    • Mutable state: State transformation functions
    • Side effects: Explicit state passing
  3. Plan control flow translation:

    • Conditionals → Pattern matching or if-then-else
    • Loops → Recursive functions (Fixpoint)
    • Early returns → Conditional expressions

Step 3: Extract Core Model

Translate imperative constructs to Coq:

Pattern: Pure function

c
// Imperative
int add(int a, int b) {
    return a + b;
}
coq
(* Coq Model *)
Definition add (a b : Z) : Z := a + b.

Pattern: Function with state

c
// Imperative
int counter = 0;
void increment() {
    counter++;
}
coq
(* Coq Model *)
Definition counter_state : Type := Z.
Definition increment (c : counter_state) : counter_state := c + 1.

Pattern: Loop → Recursion

c
// Imperative
int sum(int n) {
    int total = 0;
    for (int i = 0; i < n; i++) {
        total += i;
    }
    return total;
}
coq
(* Coq Model *)
Fixpoint sum_aux (n i total : nat) : nat :=
  match n with
  | 0 => total
  | S n' =>
      if i <? n then
        sum_aux n (S i) (total + i)
      else
        total
  end.

Definition sum (n : nat) : nat := sum_aux n 0 0.

Step 4: Add Specifications

Enhance the model with formal specifications:

  1. Function specifications:

    coq
    Definition abs (n : Z) : Z :=
      if n <? 0 then -n else n.
    
    Lemma abs_nonneg : forall n : Z, abs n >= 0.
    Proof.
      intros n. unfold abs.
      destruct (n <? 0) eqn:E.
      - apply Z.ltb_lt in E. lia.
      - apply Z.ltb_ge in E. lia.
    Qed.
  2. Loop invariants:

    coq
    (* Invariant: sum = sum of first i elements *)
    Lemma sum_invariant : forall n i total,
      i <= n ->
      sum_aux n i total = total + (sum of 0..i-1).
    Proof.
      (* Proof by induction *)
    Admitted.
  3. Correctness properties:

    coq
    Lemma sum_correct : forall n,
      sum n = n * (n - 1) / 2.
    Proof.
      (* Proof *)
    Admitted.

Step 5: Verify and Test

Ensure the model is correct:

  1. Type check:

    bash
    coqc model.v
  2. Test with examples:

    coq
    Compute add 2 3.        (* Should output 5 *)
    Compute sum 5.          (* Should output 10 *)
  3. Compare semantics:

    • Run original imperative program
    • Evaluate Coq model
    • Verify outputs match

Step 6: Refine and Document

Improve the extracted model:

  1. Simplify definitions:

    • Use standard library functions
    • Remove redundant constructs
    • Factor out common patterns
  2. Add documentation:

    coq
    (* Computes the absolute value of an integer *)
    Definition abs (n : Z) : Z := ...
  3. Organize proofs:

    • Group related lemmas
    • Add proof sketches
    • Document proof strategies

Common Extraction Patterns

For detailed patterns, see extraction_patterns.md.

Quick Reference

ImperativeCoq Model
int x = 10Definition x : Z := 10
x = x + 1x + 1 (functional update)
if (x > 0)if x >? 0 then ... else ...
for (i=0; i<n; i++)Fixpoint loop (i : nat) ...
while (cond)Fixpoint loop ... if cond then ...
struct S { int x; }Record S := { x : Z }
int arr[5]list Z or Definition arr := [...]
return xx (last expression)

Examples

Example 1: Simple Algorithm

Imperative Input (C):

c
int factorial(int n) {
    int result = 1;
    while (n > 1) {
        result *= n;
        n--;
    }
    return result;
}

Coq Model:

coq
Fixpoint factorial (n : nat) : nat :=
  match n with
  | 0 => 1
  | S n' => n * factorial n'
  end.

Lemma factorial_positive : forall n,
  factorial n >= 1.
Proof.
  induction n.
  - simpl. lia.
  - simpl. lia.
Qed.

Example 2: Array Processing

Imperative Input (Python):

python
def find_max(arr):
    max_val = arr[0]
    for i in range(1, len(arr)):
        if arr[i] > max_val:
            max_val = arr[i]
    return max_val

Coq Model:

coq
Require Import List.
Import ListNotations.

Fixpoint find_max (l : list Z) (default : Z) : Z :=
  match l with
  | [] => default
  | [x] => x
  | x :: xs => Z.max x (find_max xs default)
  end.

Lemma find_max_in_list : forall l default,
  l <> [] ->
  In (find_max l default) l.
Proof.
  (* Proof by induction *)
Admitted.

Lemma find_max_is_max : forall l default x,
  In x l ->
  x <= find_max l default.
Proof.
  (* Proof by induction *)
Admitted.

Example 3: Stateful Program

Imperative Input (Java):

java
class Counter {
    private int count = 0;

    public void increment() {
        count++;
    }

    public int getCount() {
        return count;
    }
}

Coq Model:

coq
Record CounterState : Type := mkCounter {
  count : nat
}.

Definition init_counter : CounterState := {|
  count := 0
|}.

Definition increment (s : CounterState) : CounterState := {|
  count := S s.(count)
|}.

Definition getCount (s : CounterState) : nat :=
  s.(count).

(* Specification *)
Lemma increment_increases : forall s,
  getCount (increment s) = S (getCount s).
Proof.
  intros s. unfold increment, getCount. simpl. reflexivity.
Qed.

Example 4: Binary Search

Imperative Input (C++):

cpp
int binary_search(int arr[], int n, int target) {
    int left = 0, right = n - 1;
    while (left <= right) {
        int mid = (left + right) / 2;
        if (arr[mid] == target)
            return mid;
        else if (arr[mid] < target)
            left = mid + 1;
        else
            right = mid - 1;
    }
    return -1;
}

Coq Model:

coq
Require Import List.
Import ListNotations.

Fixpoint binary_search_aux (l : list Z) (target : Z)
                            (left right : nat) : option nat :=
  match right - left with
  | 0 =>
      match nth_error l left with
      | Some v => if v =? target then Some left else None
      | None => None
      end
  | S _ =>
      if left <=? right then
        let mid := (left + right) / 2 in
        match nth_error l mid with
        | Some v =>
            if v =? target then Some mid
            else if v <? target then
              binary_search_aux l target (S mid) right
            else
              binary_search_aux l target left (mid - 1)
        | None => None
        end
      else None
  end.

Definition binary_search (l : list Z) (target : Z) : option nat :=
  binary_search_aux l target 0 (length l - 1).

(* Specification: if result is Some i, then l[i] = target *)
Lemma binary_search_correct : forall l target i,
  binary_search l target = Some i ->
  nth_error l i = Some target.
Proof.
  (* Proof *)
Admitted.

Best Practices

  1. Model state explicitly: Make all state transformations visible in function signatures
  2. Use appropriate types: Choose nat for non-negative values, Z for integers
  3. Preserve semantics: Ensure the Coq model computes the same results as the original
  4. Add specifications: Document expected behavior with lemmas and theorems
  5. Test incrementally: Verify small pieces before combining
  6. Use standard library: Leverage existing Coq definitions and tactics
  7. Document assumptions: Make implicit assumptions explicit in the model
  8. Handle edge cases: Explicitly model boundary conditions

Key Differences: Imperative vs Coq Model

  1. Mutability: Imperative code mutates variables; Coq models use functional updates
  2. Loops: Imperative loops become recursive functions in Coq
  3. State: Imperative state is implicit; Coq models make state explicit
  4. Side effects: Imperative side effects become explicit state transformations
  5. Types: Imperative types may be implicit; Coq requires explicit types
  6. Verification: Coq models enable formal proofs about program behavior

Limitations and Considerations

  1. Abstraction level: Models abstract away low-level details (memory layout, performance)
  2. Undefined behavior: Imperative undefined behavior must be handled explicitly
  3. Concurrency: Multi-threaded code requires more sophisticated modeling
  4. I/O: Input/output operations need special treatment in pure models
  5. Pointers: Pointer arithmetic requires explicit memory modeling
  6. Complexity: Some imperative patterns are complex to model functionally

Resources

Matgeliwwert Dateien

De Modell liest dës Dateie bei Bedarf, wärend de Skill gelueden ass. Si ginn als liesbar Dateie bereetgestallt a ginn ni ausgeféiert.

Heefeg gestallte Froen

Wat mécht de KI-Skill Imperative To Coq Model Extractor?

Extract abstract mathematical models from imperative code (C, C++, Python, Java, etc.) suitable for formal reasoning in Coq. Use when the user asks to model imperative code in Coq, create Coq specifications from imperative programs, extract mathematical models for verification, or translate imperative algorithms to Coq for formal reasoning and proof.

Firwat Imperative To Coq Model Extractor op TypingMind benotzen?

Well Dir en eemol installéiert a mat all Modell benotze kënnt. Imperative To Coq Model Extractor besteet aus einfachem Markdown amplaz vun ubidderspezifeschem Code. Dee selwechte Skill funktionéiert also mat GPT-5, Claude, Gemini, Grok oder engem lokale Modell – an Dir kënnt de Modell matzen am Chat wiesselen, ouni datt de Skill ophält ze funktionéieren. TypingMind benotzt Är eegen API-Schlësselen, sou datt Dir d'Ubidder direkt bezuelt, amplaz en Abonnement pro Benotzerplaz ze bezuelen. Är Skills a Chats bleiwen an Ärem eegene Späicher.

Wéi installéieren ech Imperative To Coq Model Extractor an TypingMind?

Maacht an TypingMind Plugins → Skills → Vu GitHub installéieren op a setzt https://github.com/ArabelaTso/Skills-4-SE/tree/main/skills/imperative-to-coq-model-extractor an. TypingMind liest d'Datei SKILL.md, bëndelt déi zougehéiereg Dateien an installéiert dat Ganzt als Skill, deen Dir pro Chat aktivéiere kënnt.

Wéi eng KI-Modeller kënnen Imperative To Coq Model Extractor benotzen?

All Modell, deen Dir an TypingMind verbënnt. KI-Skills sinn einfach Markdown-Instruktiounen an net ubidderspezifesche Code. Dofir kënne GPT, Claude, Gemini, Grok a lokal Modeller dëse Skill lueden, wann eng Ufro dozou passt.

Wéi vill KI-Modeller kann ech mat Imperative To Coq Model Extractor benotzen?

Esou vill wéi Dir wëllt. Soulaang e Modell Skills ënnerstëtzt, kënnt Dir Imperative To Coq Model Extractor domat benotzen – zum Beispill mat GPT, Claude, Gemini, Grok, DeepSeek, Mistral oder Llama – alles op TypingMind mat Ären eegenen API-Schlësselen.

Ass de KI-Skill Imperative To Coq Model Extractor gratis?

Jo. ArabelaTso huet de Skill ënner der Lizenz Apache-2.0 op GitHub publizéiert. Dir bezuelt nëmmen Ären eegene KI-Ubidder fir d'Token, déi Dir benotzt.

Wat si KI-Skills?

E KI-Skill ass e weiderverwendbare Pak vun Instruktiounen, deen engem KI-Modell bäibréngt, wéi en eng bestëmmt Aufgab erfëllt. E follegt dem oppenen Agent Skills-Format: eng Datei SKILL.md mat engem Numm an enger Beschreiwung, plus all Scripten, Virlagen oder Referenzdateien, déi de Modell eventuell brauch. De Modell liest d'Instruktiounen eréischt, wann Är Ufro zum Skill passt. En installéierte Skill kascht also näischt, bis e benotzt gëtt.

Wéi ënnerscheede sech KI-Skills vu Plugins oder MCP-Serveren?

E Plugin oder MCP-Server gëtt engem Modell nei Tools, déi en opruffe kann – Code, deen iergendwou leeft an e Resultat zeréckgëtt. E KI-Skill gëtt dem Modell dogéint Wëssen an eng Virgoensweis: wéi en eng Aufgab ugeet, wéi eng Schrëtt e maache soll a wéi eng gutt Ausgab ausgesäit. Skills bestinn aus einfachem Markdown, brauche kee Server, keen API-Schlëssel a kee Lafzäitëmfeld a funktionéiere mat all Modell.

Alleguer weisen

Elo Ären eegene KI-Aarbechtsberäich ariichten

Abonnéiert Iech op eisen Newsletter, fir iwwer nei Funktiounen an zukünfteg Gewënnspiller informéiert ze ginn 👇