战术自动化:简单的决策程序

时间:2017-11-05 21:26:04

标签: coq coq-tactic

我试图自动决定ASCII字符是否为空白的决策程序。这是我现在拥有的。

Require Import Ascii String.

Scheme Equality for ascii.

Definition IsWhitespace (c : ascii) := (c = "009"%char) \/ (c = "032"%char).

Definition isWhitespace (c : ascii) : {IsWhitespace c} + {not (IsWhitespace c)}.
Proof.
  unfold IsWhitespace.
  pose proof (ascii_eq_dec c "009"%char) as [H1|H1];
  pose proof (ascii_eq_dec c "032"%char) as [H2|H2];
  auto.
  right. intros [H3|H3]; auto.
Admitted.

什么是使证明更简洁的好方法?

3 个答案:

答案 0 :(得分:5)

通常,使证明更加自动化需要编写比您开始时更多的代码,以便您可以处理更多案例。采用这种方法,我改编了fiat-crypto的一些样板:

<div class="my-container">

 <blockquote class="twitter-tweet" data-lang="en">
  <p lang="und" dir="ltr">Fan. <a href="asdasd">asdasdasdas</a></p>&mdash; #DateChallenge (@Oloni) <a href="https://twitter.com/Oloni/status/927333497538252800?ref_src=twsrc%5Etfw">November 6, 2017</a></blockquote>

 <blockquote class="twitter-tweet" data-lang="en">
  <p lang="en" dir="ltr"><a href="https://twitter.com/Oloni?ref_src=twsrc%5Etfw">@Oloni</a> I didn&#39;t know what to say after he said yes  <a href="asdasd">pic.twitter.com/aRcbEIaF06</a></p>&mdash; keshef mir (@keshefmir) <a href="https://twitter.com/keshefmir/status/923566544637984768?ref_src=twsrc%5Etfw">October 26, 2017</a></blockquote>


 <blockquote class="twitter-tweet" data-lang="en">
  <p lang="en" dir="ltr">There’s a severe weather warning out for Sydney and the NSW south coast today. <a href="https://twitter.com/hashtag/9News?src=hash&amp;ref_src=twsrc%5Etfw">#9News</a> <a href="asdasd">pic.twitter.com/J43t8Z4wJ2</a></p>&mdash; Nine News
  Sydney (@9NewsSyd) <a href="https://twitter.com/9NewsSyd/status/927250318454169601?ref_src=twsrc%5Etfw">November 5, 2017</a></blockquote>

</div>

使用此样板,证明变为

Require Import Coq.Strings.Ascii Coq.Strings.String.

Class Decidable (P : Prop) := dec : {P} + {~P}.
Arguments dec _ {_}.
Notation DecidableRel R := (forall x y, Decidable (R x y)).

Global Instance dec_or {A B} {HA : Decidable A} {HB : Decidable B} : Decidable (A \/ B).
Proof. hnf in *; tauto. Defined.
Global Instance dec_eq_ascii : DecidableRel (@eq ascii) := ascii_dec.

与证据一样短。 (请注意,Definition IsWhitespace (c : ascii) := (c = "009"%char) \/ (c = "032"%char). Definition isWhitespace (c : ascii) : Decidable (IsWhitespace c) := _. := _相同,. Proof. exact _. Defined.本身与. Proof. typeclasses eauto. Defined.相同。)

请注意,这与ejgallego的证明非常相似,因为tautointuition fail相同。

另请注意the original boilerplate in fiat-crypto比使用hnf in *; tauto更长,但也更强大,并处理几十种不同类型的可判定命题。

答案 1 :(得分:4)

证明几乎是最简洁的!至多你可以做的是调用更强大的策略,例如intuition

Definition isWhitespace (c : ascii) : {IsWhitespace c} + {not (IsWhitespace c)}.
Proof.
now unfold IsWhitespace;
    case (ascii_eq_dec c "009"%char);
    case (ascii_eq_dec c " "%char); intuition.

答案 2 :(得分:4)

遵循杰森的回答精神,我们当然可以使用一些处理可判定平等的图书馆来达到你的结果:

这会将ascii声明为具有可判定等式的类型:

From Coq Require Import Ascii String ssreflect ssrfun ssrbool.
From mathcomp Require Import eqtype ssrnat.

Lemma ascii_NK : cancel N_of_ascii ascii_of_N.
Proof. exact: ascii_N_embedding. Qed.

Definition ascii_eqMixin := CanEqMixin ascii_NK.
Canonical ascii_eqType := EqType _ ascii_eqMixin.

在这种风格中,通常你说你的属性是可判定的命题,所以没有什么可以证明的:

Definition IsWhitespaceb (c : ascii) := [|| c == "009"%char | c == " "%char].

但是如果你愿意,你当然可以恢复非计算性的:

Definition IsWhitespace (c : ascii) := (c = "009"%char) \/ (c = "032"%char).

Lemma whitespaceP c : reflect (IsWhitespace c) (IsWhitespaceb c).
Proof. exact: pred2P. Qed.

当然可以使用更多自动化。