17. 帰納的関数の定義 / プログラム抽出 Coq < Print plus. // 予め定義されている plus = fix plus (nm : nat) : nat := match n with | 0 => m | S p => S (p + m) end : nat -> nat -> nat Coq < Extraction “plus.ml” plus. // OCamlプログラムとして抽出 % cat plus.ml type nat = | O | S of nat (** val plus : nat -> nat -> nat **)// 普通のOcamlの関数 let rec plus nm = match n with | O -> m | S p -> S (plus pm) 17
18. 帰納的関数の性質の証明(1) Coq < Theorem t : forallnm : nat, n + m = m + n. // これを証明する。 1 subgoal ============================ forallnm : nat, n + m = m + n t < intros. // forallの除去。初手は大抵intros. 1 subgoal n : nat m : nat ============================ n + m = m + n t < induction n. // nについて帰納法。0と(S n)とに場合分け 2 subgoals m : nat ============================ 0 + m = m + 0 // n=0 の場合 subgoal 2 is: S n + m = m + S n t < simpl; trivial. // 0 + m = m + 0 を簡単化すると自明な式に。 18
19. 帰納的関数の性質の証明(2) 1 subgoal n : nat m : nat IHn : n + m = m + n // 帰納法の前提 ============================ S n + m = m + S n t < rewrite plus_Sn_m. 1 subgoal n : nat m : nat IHn : n + m = m + n ============================ S (n + m) = m + S n t < rewrite <- plus_n_Sm. 1 subgoal n : nat m : nat IHn : n + m = m + n ============================ S (n + m) = S (m + n) t < apply eq_S. 1 subgoal n : nat m : nat IHn : n + m = m + n ============================ n + m = m + n t < exact IHn. Proof completed. t < Qed. 19 Coq < Check plus_Sn_m. plus_Sn_m : forallnm : nat, S n + m = S (n + m) Coq < Check eq_S. eq_S : forallxy : nat, x = y -> S x = S y
38. 練習問題 Coq本より Theorem ex561 : forall A B C : Prop, A/B/) -> (A/)/. Theorem ex562 : forall A B C D : Prop, (A->B)/C->D)// -> B/. Theorem ex563 : forall A : Prop, ~(A/A). Theorem ex564 : forall A B C : Prop, A(BC) -> (AB)C. Theorem ex565 : forall A : Prop, ~~(A~A). Theorem ex566 : forall A B : Prop, (AB)/A -> B. Theorem ex567 : forallA:Set, forall P Q:A->Prop, (forallx, P x)(forally, Q y)->forallx, P xQ x. パズルゲーム感覚で解けます。 というか端から色々試すアドヴェンチャーゲーム? 適用出来ないtacticはエラーメッセージが出るだけ。 23
42. Hoare論理の公理 {Q[e/x]}x:=e{Q} {P}S1{R}, {R}S2{Q} ------------------------ {P}S1;S2{Q} {P∧B}S1{Q}, {P∧¬B}S2{Q} ----------------------------- {P} if B then S1 else S2 {Q} {P∧B}S{P} ----------------------------- {P} while B do S end {P∧¬B} PP1, {P1}S{Q1}, Q1Q ------------------------------ {P}S{Q} 27