coq-club AT inria.fr
Subject: The Coq mailing list
List archive
Re: [Coq-Club] Termination for three argument addition function on natural numbers
Chronological Thread
- From: mukesh tiwari <mukeshtiwari.iiitm AT gmail.com>
- To: coq-club AT inria.fr
- Subject: Re: [Coq-Club] Termination for three argument addition function on natural numbers
- Date: Tue, 21 Sep 2021 16:56:44 +1000
- Authentication-results: mail3-smtp-sop.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-f47.google.com
- Ironport-hdrordr: A9a23:Ak3IAqs+Jpit5YlWMy25IPDg7skDkdV00zEX/kB9WHVpm6uj5r+TdZUgtCMc7Qx6NR1O9+xoV5PwP080maQFmrX5fo3SOjUOwVHYVL2KjrGSsgEIeReOldK1vJ0IG8ND4bvLfCVHZK3BkXWF+rgbsaO6GaqT9J7j5kYodgIvQKl85QYRMHf+LmRGADJEQbA8DfOnl616jgvlQndSSsigHHEKU6ziirTw9K7OUFo9A1ob8wGFjzms6LT5eiLouys2Ynd0xfMe6mDOkQrh6qLLiYDY9iPh
- Ironport-phdr: A9a23:z8rACRBEMDQmi6LF5hdRUyQUiEMY04WdBeb1wqQuh78GSKm/5ZOqZBWZua81yg6QFtiEo9t/yMPu+5j6XmIB5ZvT+FsjS7drEyE/tMMNggY7C9SEA0CoZNTjbig9AdgQHAQ9pyLzPkdaAtvxaEPPqXOu8zESBg//NQ1oLejpB4Lelcu62/6u95HJZwhEmTWxbLNwIR6rsQjfq84ajJd4JK0s0BXJuHxIe+pXxWNsO12emgv369mz8pB+7Sleouot+MFcX6r0eaQ4VqFYAy89M28p/s3rtALMQhWJ63ABT2gZiBtIAwzC7BHnQpf8tzbxu+Rh1CWGO8D9ULY5Uimg4ah2Uh/lkCgINzA7/2/XhMJ+j79Vrgy9qBFk2YHYfJuYOeBicq/Bf94XQ3dKUMZLVyxGB4Oxd44PD/cCPelGtIn9p0YFrQe/BQiiHuzv0D5IjWLx0K0/zuQhEh/J3BcgH9ISsXTVos/6NL0TUeyvzanIzDTDb+9T2Trm54jIdwouofCIXb5qbcXRzkwvGhrDg16NpoPrIymb2f4Rs2iH8eVgT+SvhnY5pg9+vzShydshhpTVio8Xy13J9id0zokpKNO4RkN2bsOoHYZfui2EOIV4TMwsTmJntSok1LELuoC2cDUXxZkmwRPUdv+Jc5CQ7x79SOqcJS10iXFldb6lmRq+7UutxvfzW8S21ltBszBLncPWtn8X0hze8siHReV5/kemwTuP0hrc6uBAIUwti6XUNoMtzqc+lpccs0nPBCD2mELxjK+ZckUr5PKk5PjgYrXjvpOcNol0hR/iMqk2hMCzHeA1PhINUmWb4+iwyqHv8E7jTLhFjPA6iqzZv4rbJcQfqK65GQhV0oM75ha6Ejem08oXnWIHLFJZeRKGgZLmO0vPIPziDPe/glWskCtux/3dMb3hB4/CLnnHkLv7Ybl97EtcxBIpzd9D/5JUFq0BIPXrV0Dts9zYFwY1PBCww+b6E9pwzZgeWGKKAq+BKqzeq16I5uQ1I+mNfoAZojj9K+J2r8Lp2HQ+gBoWebSj9ZoRcnGxWPp8cGuDZn+5h8oCHHwK9hY/U+XwiRXWVCNQam2yQ6Mj7ys6ToOnDJvGbo+oib2Fmiy8G8sFNSh9FlmQHCKwJM2/UPAWZXfKSieEujMBXLmlDYQm0EP23OcV47ViJ+vQvCYfsMC6vDCUz+jalBV3+DAtSsrBjCeCSGZ7mm5OTDgzjvgXnA==
Thanks Robbert.
-Mukesh
On Tue, Sep 21, 2021 at 4:18 PM Robbert Krebbers <mail AT robbertkrebbers.nl> wrote:
Coq's termination checker requires a single argument to become smaller
in each recursive call. In your example that is not the case: the first
and third recursive call reduce n, while the second reduces m.
Such functions can often be written using a nested fixpoint:
Fixpoint three_add (n : nat) : nat -> nat -> nat :=
fix go m p :=
match n, m with
| 0, 0 => p
| S n', 0 => S (three_add n' 0 p)
| 0, S m' => S (go m' p)
| S n', S m' => S (S (three_add n' m' 0))
end.
In this case, the nested fixpoint makes sure that when m reduces, n has
to remain unchanged. Hence, this guarantees termination.
On 21/09/2021 08.09, mukesh tiwari wrote:
> 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
- [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+.