coq-club AT inria.fr
Subject: The Coq mailing list
List archive
- From: mukesh tiwari <mukeshtiwari.iiitm AT gmail.com>
- To: coq-club AT inria.fr
- Subject: [Coq-Club] Termination for three argument addition function on natural numbers
- Date: Tue, 21 Sep 2021 16:09:36 +1000
- Authentication-results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=mukeshtiwari.iiitm AT gmail.com; spf=Pass smtp.mailfrom=mukeshtiwari.iiitm AT gmail.com; spf=None smtp.helo=postmaster AT mail-ed1-f44.google.com
- Ironport-hdrordr: A9a23:fp8gnq+EVvO+2UQ35Uxuk+DVI+orL9Y04lQ7vn2ZKCYlFfBw8vrFoB11726WtN98YhEdcLO7WZVoI0msl6KdiLN5VdyftWLdyQ6Vxe9ZnO/fKv7bdxEWNNQx6U6tScdD4RTLY2RHsQ==
- Ironport-phdr: A9a23:dfJdGhMPKlpQE19JZJ0l6nbnDRdPi9zP1u491JMrhvp0f7i5+Ny6ZQqDv60r3QaCANqTwskHotKei7rnV20E7MTJm1E5W7sIaSU4j94LlRcrGs+PBB6zBvfraysnAJYKDwc9rDm0PkdPBcnxeUDZrGGs4j4OABX/Mhd+KvjoFoLIgMm7yfy+94fXbglVizawYbF/JwiqoAvMscUbnZFsIbsrxBvTpXtIdeVWxWd2Kl+Wgh3x+MS+8oN9/ipJo/4u+NJOXqv8f6QjULxXFy8mPHwv5M3qrhbMUw2C7WYBX2oMkxpIBw/F7AzmXpr0ryD3uPZx1DWcMMbrS70/RDas4LpxSBLwhygJNzA3/mLKhMJukK1WuwiuqwBlzoPOfI2ZKPhzc6XAdt0aX2pBWcNRWjREAoOhbIsAEfEBPeZFpInhplsPowGxBROtBOjyzTJIg2L23a4k3OQ6EQHGwA0gHtwUv3TOrdX5LqYSUeG0zKnUyDXOdPxW2TLn54jJdhAtu+2DXbV1ccfIz0QkCgzKgEmKp4P/IzOVyvoCs3Kd7+d4Se6ihGAqpx1wrzSx28ohhZTFi58Rx17L6Cl0wog7KNK4RUN6YtOoDZ9duiKaOoZ1TM0sTWNltig+x7AYt5C2eC4Hw4kkyR7Hc/GLbZSE7xb5WOuSITp0nmxpdK68ihqo/kWtxevxXdSu3llQtCpKiNzMu2gN1xPN7siHTeNw/kK71jaO0wDf8/9LLl0pmabCJZ4szbE9moAcsUTEGS/2l0H2g7GMeko4/eio7vzrYrTgppCCK495kh/yPrgql8ClAuk1MhICU3aH9em/zrHu8kL0TK1PjvIsk6nZtJ7aJd4cpq68GwJV15sj6xWhADe90NQYgXgHLVxfdxKdjojlIVDOIPH5DfejhlShizhrx/XcMb3gBpXBNGTMkLDkfbpl8U5T1BIzzcxD55JTErwOPPXzWlbouNPECh85Lhe7zv38CNR904MeQXiADrWYMKPUq1+I5/ggL/OCZI8P637BLK0u4Oerhnskk3cce7Oo1N0ZciOWBPNjdkCEYnf3gp8dEHgDpAt2GOn3i1CZUSJSeH+oXuQ95zAnDaqpCI7CQsamh7nXj3TzJYFfem0TUgPEKnzvbYjRA5/khwqdJ8ZglnoPUr3zEufJMDmhsQb7z/xsKe+GokXwVLrm3dlxouDfzFQ8qWAyAMOa3GWACWpzmzFQLwI=
Why does the Coq termination checker complain that it cannot guess the decreasing argument for the three_add function? How can I convince Coq that it terminates, without using the well founded argument (see add_three). (I tried few variants of add_three but it did not work)
Fail Fixpoint three_add (n m p : nat) : nat :=
match n, m with
| 0, 0 => p
| S n', 0 => S (three_add n' 0 p)
| 0, S m' => S (three_add 0 m' p)
| S n', S m' => S (S (three_add n' m' 0))
end.
The command has indeed failed with message: Cannot guess decreasing argument of fix.
More context: someone, in the Zulip Coq channel, has asked about an addition function on natural numbers, albeit with three arguments, so I wrote three_add but the termination checker did not like it. Therefore, I ended up writing one, which I recently learnt from Dominique, but it seems overkill for a simple function like three_add. Moreover, it relies on the Coq's addition function (+ in Acc lt (n + m)), so I would not call it a (perfect) solution, so my question is: is it possible to define three_add without any additional machinery?
Definition add_three (n m p : nat) : nat.
refine((fix add_three n m p (H: Acc lt (n + m)) {struct H} :=
match n as n' return n = n' -> _ with
| 0 =>
match m as m' return m = m' -> _ with
| 0 => fun Hm Hn => p
| S m' => fun Hm Hn => S (add_three 0 m' p _)
end eq_refl
| S n' =>
match m as m' return m = m' -> _ with
| 0 => fun Hm Hn => S (add_three n' 0 p _)
| S m' => fun Hm Hn => S (S (add_three n' m' p _))
end eq_refl
end eq_refl) n m p _).
subst. apply Acc_inv with (1 := H).
abstract lia.
subst. apply Acc_inv with (1 := H).
abstract lia.
subst. apply Acc_inv with (1 := H).
abstract lia.
apply lt_wf.
Defined.
Best,
Mukesh
Fail Fixpoint three_add (n m p : nat) : nat :=
match n, m with
| 0, 0 => p
| S n', 0 => S (three_add n' 0 p)
| 0, S m' => S (three_add 0 m' p)
| S n', S m' => S (S (three_add n' m' 0))
end.
The command has indeed failed with message: Cannot guess decreasing argument of fix.
More context: someone, in the Zulip Coq channel, has asked about an addition function on natural numbers, albeit with three arguments, so I wrote three_add but the termination checker did not like it. Therefore, I ended up writing one, which I recently learnt from Dominique, but it seems overkill for a simple function like three_add. Moreover, it relies on the Coq's addition function (+ in Acc lt (n + m)), so I would not call it a (perfect) solution, so my question is: is it possible to define three_add without any additional machinery?
Definition add_three (n m p : nat) : nat.
refine((fix add_three n m p (H: Acc lt (n + m)) {struct H} :=
match n as n' return n = n' -> _ with
| 0 =>
match m as m' return m = m' -> _ with
| 0 => fun Hm Hn => p
| S m' => fun Hm Hn => S (add_three 0 m' p _)
end eq_refl
| S n' =>
match m as m' return m = m' -> _ with
| 0 => fun Hm Hn => S (add_three n' 0 p _)
| S m' => fun Hm Hn => S (S (add_three n' m' p _))
end eq_refl
end eq_refl) n m p _).
subst. apply Acc_inv with (1 := H).
abstract lia.
subst. apply Acc_inv with (1 := H).
abstract lia.
subst. apply Acc_inv with (1 := H).
abstract lia.
apply lt_wf.
Defined.
Best,
Mukesh
- [Coq-Club] Termination for three argument addition function on natural numbers, mukesh tiwari, 09/21/2021
- Re: [Coq-Club] Termination for three argument addition function on natural numbers, Robbert Krebbers, 09/21/2021
- Re: [Coq-Club] Termination for three argument addition function on natural numbers, mukesh tiwari, 09/21/2021
- Re: [Coq-Club] Termination for three argument addition function on natural numbers, Dominique Larchey-Wendling, 09/21/2021
- Re: [Coq-Club] Termination for three argument addition function on natural numbers, mukesh tiwari, 09/22/2021
- Re: [Coq-Club] Termination for three argument addition function on natural numbers, Wilayat Khan, 09/22/2021
- Re: [Coq-Club] Termination for three argument addition function on natural numbers, mukesh tiwari, 09/22/2021
- Re: [Coq-Club] Termination for three argument addition function on natural numbers, Wilayat Khan, 09/22/2021
- Re: [Coq-Club] Termination for three argument addition function on natural numbers, mukesh tiwari, 09/22/2021
- Re: [Coq-Club] Termination for three argument addition function on natural numbers, Robbert Krebbers, 09/21/2021
Archive powered by MHonArc 2.6.19+.