Coqでリストのreverse関数を書いてください。2回reverseすると元に戻る性質を証明してください。
- User
Coqでリストのreverse関数を書いてください。2回reverseすると元に戻る性質を証明してください。
Assistant
Coqでリストのreverse関数とその性質の証明を書きます。
Require Import List. Import ListNotations. (* reverse関数の定義 *) Fixpoint rev {A : Type} (l : list A) : list A := match l with | [] => [] | h :: t => rev t ++ [h] end. (* 補助定理: rev (l1 ++ l2) = rev l2 ++ rev l1 *) Lemma rev_app_distr : forall (A : Type) (l1 l2 : list A), rev (l1 ++ l2) = rev l2 ++ rev l1. Proof. intros A l1 l2. induction l1 as [| h t IH]. - simpl. rewrite app_nil_r. reflexivity. - simpl. rewrite IH. rewrite app_assoc. reflexivity. Qed. (* 補助定理: rev [a] = [a] *) Lemma rev_singleton : forall (A : Type) (a : A), rev [a] = [a]. Proof. intros A a. simpl. reflexivity. Qed. (* 主定理: 2回reverseすると元に戻る *) Theorem rev_involutive : forall (A : Type) (l : list A), rev (rev l) = l. Proof. intros A l. induction l as [| h t IH]. - simpl. reflexivity. - simpl. rewrite rev_app_distr. simpl. rewrite IH. 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.