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: Fri, 15 Oct 2021 21:59:44 -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-qv1-f44.google.com
- Ironport-hdrordr: A9a23:1bioM68TA5fqcOLVm9luk+D7I+orL9Y04lQ7vn2ZOiYlD/Bw8Pre4MjztCWE8gr5PUtLpTnuAtjnfZqxz+8Q3WBVB8bYYOCEghrREGgB1/qE/9SIIUSXnY5gPOVbAs1D4bXLbGSS5vyKgzVQfexQouVvvJrY/Ns2DE0AceipUcxdBstCZTpz23cZeDV7
- Ironport-phdr: A9a23:bz6GJhD3hEEl+TjFvzvrUyQUt0MY04WdBeb1wqQuh78GSKm/5ZOqZBWZua81yg6QFtiEo9t/yMPu+5j6XmIB5ZvT+FsjS7drEyE/tMMNggY7C9SEA0CoZNTjbig9AdgQHAQ9pyLzPkdaAtvxaEPPqXOu8zESBg//NQ1oLejpB4Lelcu62/6u95HJZwhEmTWxbLNwIR6rsQjfq84ajJd4JK0s0BXJuHxIe+pXxWNsO12emgv369mz8pB+7Sleouot+MFcX6r0eaQ4VqFYAy89M28p/s3rtALMQhWJ63ABT2gZiBtIAwzC7BHnQpf8tzbxu+Rh1CWGO8D9ULY5Uimg4ah2Uh/lkDoJOSA38G/XicJ+gqxUrx2jqBNjzIDZe52VOflkc6/BYd8XS2hMU8BMXCJBGIO8aI4PAvIfMOZctY79okUBrR2iBQa0Hu3vyyNIimbo0K0+yeshDBzJ0xIkH9kTt3nbsM31NKYOUe+pyKnH1yjDYO5I1jf584XIfRUhruuNXbJ0a8be1U4vFwbcg1iWtIfqMC+b2P4XvGiH8+pvS/ivi2g/pg1voDWj2NoghInNi4wb1F3K+ip3zYg6K9C5RkN2b96pHYdMui2GM4Z4TN4vTmBotSomyLALtpq2ciYOxZk6xhPSbeGMfYaP4hLmTumRIDF4iWpqeLK+mxay8VWgxfbmWsao11ZKqyxImcTPuHAVzxHf9NSLR/9n8kqi2TuDzR7f5vxALEwumqfWJIYtzqA/m5YJsEnPAzX6lFv5gaOKbEkp+fSk5uL6abv8vJCcLZV7igTmP6QuhMO/BeM4PxALX2eB+OS80KTv/VTnQLlXl/E2nKbUvZ/AKcQUoa65BABV0oI95BqlEzim19EYkWEGLFJDZh2Hk5DkN0/SLP38F/uygFShnC11y/zbOrDtGJrAI3fbnLfkZ7l96kpcyAQpzdBY4pJZEq0OIOryWk/tqNPYCgU2MxGpzOn5DNVwzYweWWeVDa+YNKPeq0OH5uUqI+WUfo8apC79K+Q55/7plXI2hVgdfbCw0ZQLbHC4A+9pLl6CYXvsh9cBCX0FshA/TOzskl2CUCRca2y8X6ImtXkHD9etCp6GTYSwipSA2j26F9tYfDNoEFeJRFzs9oCJXes7UCOOZ+RlmyEIWLzpH4Q51A2lsAbnx7djBuXR8ywc85nk0Y4mtKXoiRgu+GksXIym2GaXQjQs9ovtbzAz1aF750d6zwXbuUCXq/NdHN1XofhOV1VjXXY95+lzCtS3QxyYO9nVFBCpRdKpBTx3RdU0kYdmXg==
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>
- [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+.