Coqでリストのreverse関数を書いてください。
- User
Coqでリストのreverse関数を書いてください。
Assistant
Coqでリストのreverse関数を実装します。効率的な実装のため、アキュムレータを使った末尾再帰版を書きます。
coqRequire Import List. Import ListNotations. (* アキュムレータを使った補助関数 *) Fixpoint rev_append {A : Type} (l1 l2 : list A) : list A := match l1 with | [] => l2 | h :: t => rev_append t (h :: l2) end. (* メインのreverse関数 *) Definition reverse {A : Type} (l : list A) : list A := rev_append l []. (* 使用例 *) Compute reverse [1; 2; 3; 4; 5]. (* = [5; 4; 3; 2; 1] *) (* 正しさの証明の例 *) Lemma reverse_nil : forall A, @reverse A [] = []. Proof. intros. unfold reverse. simpl. reflexivity. Qed. Lemma reverse_singleton : forall A (x : A), reverse [x] = [x]. Proof. intros. unfold reverse. simpl. reflexivity. Qed.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.