coq-club AT inria.fr
Subject: The Coq mailing list
List archive
- From: Clément Pit-Claudel <cpitclaudel AT gmail.com>
- To: coq-club AT inria.fr
- Subject: Re: [Coq-Club] Rewriting Fails for a Dependent Type Function
- Date: Sat, 16 Oct 2021 08:41:33 -0400
- Authentication-results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=cpitclaudel AT gmail.com; spf=Pass smtp.mailfrom=cpitclaudel AT gmail.com; spf=None smtp.helo=postmaster AT mail-qt1-f179.google.com
- Ironport-hdrordr: A9a23:0nAYDKzp4vIlQ0ArKJ5EKrPwD71zdoMgy1knxilNoG9uA7GlfqeV7YgmPH7P+UwssRQb8+xoV5PwJ080maQFhbX5eI3SJzUO21HYVL2Kj7GSpAEIcheWnoU86U4jSdkaNDSZNzlHZK3BkWqF+rgbsbu6GeyT9J7jJrRWIT2CqZsM0+60MGmm+4RNKjV7OQ==
- Ironport-phdr: A9a23:NrOUxx9kYy/u5P9uWYO8ngc9DxPPW53KNwIYoqAql6hJOvz6uci4ZAqOvL4w0xfgZsby07p8ke3YsqTtCyQrwK2qlzQ8SqFKTAIPks4MngYtU4avAEz/K+P2PWRhRJwRHFBq8GumPkdLBc3we0PdomGo7T4VBx7zKRd5Kv76F4LMk8i7zeS/94DcbwhIhje2fK9/IgixoQjNrMcdnJFsKrw2yhvHo3tIf/pZyGZ1Ll+NnBjy+9m98od7/ytfp/wu+chAUb7nf6sjVrxXEC4mM2Eu68L1sxTIUBaC6WEdUmUSlRpIHhTF4RTnVZr/rif2quxw0zScMMbrT747RC6i4r92RhH2hicJNz03/m/ZhcN/jq1UvB2vqgdjw4PXeoyZKOZyc63fcN4cWGFPXtxRVytEAo6kcYYPC/AGPeNGoIn7u1sBtQGwBQiwBO/21DJIgmX53bAn3Os/FQHNwQstEM4WsHTVsNX6KKMSXvqozKbV0zrDa+hb2Tj46IfScxAhpeuAUq53ccrU0EQiER7OgVqMp4L/JTyVyvgNvHaB7+pmTe+jlWwqpx9srjavwsoihJfEiI0axF3K+it0wYg4KNO4RUNnfdOoDJ9duS+HOoZ3Qs4uX2NltSk+x7MIvZO2YSkHxZI6zBDRbPyHdpKH4hPlVOuJLjd4hW5leLKihxmp60Sv1ur8Vsyy3V1XrSRFisHBum4R2xHX8MSKSftw8l281TuOywzf8PxILEIwmKbDNZIszaQ8m5sOvUjZACP7l1n6gLWVe0k4/OWj9v7pba/8ppCGMo95kgH+Pboqmsy4Gek4NxIBX2mf+eilzb3j4VD1TKxEjvErkKTUtIrWJcscpq6+DA9V1pgs5wyjADeh1dQUhXgHLFRbdxKbl4XlJU3CLfTiAfq8g1mgiipnyvHaMrH7H5nALHzOnK/kfbln6k5czAQzzcpY55JRErwOPOrzWk7ttNPECh82KRG0zPv5B9V5zY4eVmePDbWYMKPWq1OH+uUvI+yUaI8PpDn9M+Ql5+LpjXIhhVAderCp0YILZ3C8A/RpOF6UYWHsg9cECWcFpBAyTO3siF2YUD5cfWy+X6wm5mJzNIXzBoDaA4upnbap3SGhH5QQaHoVJEqLFCLDcwSBVvEQXxqTPopKljUZWbWlA9sqzRCyvwvz1rZqKsLb/yQZsdTo090jtL6brg076TEhV5fV6GqKVWwhxgvgphcz1aF75FVhkxKNiPcpxfNfEtNX6rVCVQJobfY0KsR1DtnzXkTKedLbED5Orf2pBDgwSpQ6xNpcOi5A
As a side note: in this particular case, picking a simpler definition of
np_total helps a lot:
From Coq Require Import NArith Strings.Byte Utf8 Lia.
Open Scope N_scope.
Definition np_total (np : N): (np <? 256 = true) -> byte.
Proof.
intros H.
pose proof of_N_None_iff np.
destruct (of_N np) as [b | ].
- exact b.
- exfalso;
abstract (apply N.ltb_lt in H;
intuition lia).
Defined.
Lemma np_true : forall np (H : np <? 256 = true) x,
of_N np = Some x -> np_total np H = x.
Proof.
unfold np_total; intros * Heq.
generalize (of_N_None_iff np).
rewrite Heq; intros.
reflexivity.
Qed.
On 10/15/21 11:03 PM, mukesh tiwari wrote:
> Thanks Clémen! Very helpful. Finally, I get the idea.
>
> Best,
> Mukesh
>
> On Sat, Oct 16, 2021 at 1:00 PM Clément Pit-Claudel <cpitclaudel AT gmail.com
> <mailto:cpitclaudel AT gmail.com>> wrote:
>
> The error message is actually pretty decent in this case:
>
> The term "np_total_subproof0" of type
> "∀ np : N,
> (np <? 256) = true → ∀ mp : N, np = mp → (mp <? 256) = false →
> byte"
> cannot be applied to the terms
> "np" : "N"
> "H1" : "b0 = true"
> "mp" : "N"
> "Hmp" : "np = mp"
> "Hmf" : "(mp <? 256) = false"
> The 2nd term has type "b0 = true" which should be coercible to
> "(np <? 256) = true".
>
> What this means is that your np_total_subproof0 wants an np of type N
> and a proof that n <? 256 is true. When you try to rewrite (same problem
> if you try to destruct (n <? 256), btw), you abstract over the value of (n
> <? 256), turning it into an arbitrary boolean b0; as a result, the argument
> that gets passed to np_total_subproof0 has the wrong type.
>
> You can see the same issue with `set` and `clearbody`: as long as you
> can't do `set (np <? 256) as b0; clearbody b0`, the rewrite won't work.
> Same with `pattern (np <? 256)`.
>
> The usual trick for these issues is to generalize things to have
> less-dependent types. Here's the completed proof, concretely:
>
> Require Import NArith Byte Utf8 Lia.
> Open Scope N_scope.
>
> Lemma np_true : forall np (H : np <? 256 = true) x, of_N np = Some
> x -> np_total np H = x.
> Proof.
> intros. unfold np_total.
> (* Following the error messages: *)
> generalize (np_total_subproof0 np H) as sp0.
> generalize (@eq_refl bool (np <? 256)).
> (* But don't generalize too much *)
> set (np <? 256) as b in *; unfold b at 1.
> Fail Fail clearbody b. (* Phew *)
> rewrite H; subst b. (* Phew *)
>
> (* Now the actual proof: *)
> intros.
> Fail rewrite H0. (* Same problem *)
> generalize (np_total_subproof np np eq_refl e) as sp.
> rewrite H0.
> intros.
> reflexivity.
> Qed.
>
> Clément.
>
>
>
> On 10/15/21 8:37 PM, mukesh tiwari wrote:
> >
> > Why does the "rewrite H" fail in np_true? I am trying to write a
> (dependent) type function, np_total, that takes a binary natural number N,
> a proof that it's less than 256, and returns a byte. It's fairly
> straightforward but the problem happens when I am trying to prove the
> correctness lemma, np_true. Based on my understanding, so far, of Coq, I
> need to generalise the terms and therefore this (enriched/generalised)
> return type: *match (np <? 256) as b return ∀ mp, np = mp -> (mp <? 256)
> = b -> _ with *but the problem is no matter how I generalise (I have tried
> a few), I am not able to prove the lemma np_true. I understand that I can
> write it as a Sigma type, which is fairly straightforward [1], but I am
> interested in knowing how to solve this problem.
> >
> > **
> >
> > Lemma np_true : forall np (H : np <? 256 = true) x, of_N np = Some x
> -> np_total np H = x.
> > Proof.
> > intros. unfold np_total.
> > (* this rewrite fails *)
> > Fail rewrite H.
> >
> > Definition np_total (np : N): (np <? 256 = true) -> byte.
> > Proof.
> > intros H.
> > refine(match (np <? 256) as b return ∀ mp, np = mp ->
> > (mp <? 256) = b -> _ with
> > | true => fun mp Hmp Hmpt =>
> > match of_N mp as npt return _ = npt -> _ with
> > | Some x => fun _ => x
> > | None => fun Hf => _
> > end eq_refl
> > | false => fun mp Hmp Hmf => _
> > end np eq_refl eq_refl).
> > abstract(
> > apply of_N_None_iff in Hf;
> > apply N.ltb_lt in Hmpt; nia).
> > abstract (subst; congruence).
> > Defined.
> >
> > Best,
> > Mukesh
> >
> >
> > [1]
> https://gist.github.com/mukeshtiwari/99538fd3ce0715155797528bdeb3d3a9#file-sig-v-L1-L17
>
> <https://gist.github.com/mukeshtiwari/99538fd3ce0715155797528bdeb3d3a9#file-sig-v-L1-L17>
>
> <https://gist.github.com/mukeshtiwari/99538fd3ce0715155797528bdeb3d3a9#file-sig-v-L1-L17
>
> <https://gist.github.com/mukeshtiwari/99538fd3ce0715155797528bdeb3d3a9#file-sig-v-L1-L17>>
>
- [Coq-Club] Rewriting Fails for a Dependent Type Function, mukesh tiwari, 10/16/2021
- Re: [Coq-Club] Rewriting Fails for a Dependent Type Function, Clément Pit-Claudel, 10/16/2021
- Re: [Coq-Club] Rewriting Fails for a Dependent Type Function, mukesh tiwari, 10/16/2021
- Re: [Coq-Club] Rewriting Fails for a Dependent Type Function, Clément Pit-Claudel, 10/16/2021
- Re: [Coq-Club] Rewriting Fails for a Dependent Type Function, mukesh tiwari, 10/16/2021
- Re: [Coq-Club] Rewriting Fails for a Dependent Type Function, Clément Pit-Claudel, 10/16/2021
Archive powered by MHonArc 2.6.19+.