# Understand the reduction behind apply

**URL:** <https://discourse.rocq-prover.org/t/understand-the-reduction-behind-apply/1848>\
**Category:** Using Rocq\
**Created:** [December 6, 2022, 3:49pm UTC](https://discourse.rocq-prover.org/t/understand-the-reduction-behind-apply/1848 "2022-12-06T15:49:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZWY](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.rocq-prover.org/zwy/32/335_2.png) [@ZWY](https://discourse.rocq-prover.org/u/ZWY)\
**Post date:** [December 6, 2022, 3:49pm UTC](https://discourse.rocq-prover.org/t/understand-the-reduction-behind-apply/1848/1 "2022-12-06T15:49:43Z")

</div>

I am working on Software Foundation, the volume of Verified Functional Algorithm. I encounter a state `Permutation [] ([] ++ [])`.  
Although `[]` is not equal to `[] ++ []`, I can finish the proof by `apply Permutation_refl.` It seems that `apply` perform some reduction before matching `Permutation_refl` with the current goal.

From the Coq manual, [Tactics — Coq 8.16.1 documentation](https://coq.inria.fr/refman/proof-engine/tactics.html#coq:tacn.apply), I find no information about the reduction behind `apply`.

Where could I know the mechanism of the reduction of `apply` beside the manual? The question can also be extended to other tactics that perform operations not mentioned in the Coq manual

---

<div class="post-metadata">

**Author:** ![LessnessR](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.rocq-prover.org/lessnessr/32/783_2.png) [@LessnessR](https://discourse.rocq-prover.org/u/LessnessR)\
**Post date:** [December 6, 2022, 11:56pm UTC](https://discourse.rocq-prover.org/t/understand-the-reduction-behind-apply/1848/2 "2022-12-06T23:56:30Z")

</div>

```auto
Require Import List.
Eval compute in (@nil nat ++ @nil nat).

```

As one can see, the expression computes to `nil`.  
In other words, `[] ++ []` is equal to `[]`.

---

<div class="post-metadata">

**Author:** ![gasche](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.rocq-prover.org/gasche/32/16_2.png) [@gasche](https://discourse.rocq-prover.org/u/gasche)\
**Post date:** [December 7, 2022, 9:16am UTC](https://discourse.rocq-prover.org/t/understand-the-reduction-behind-apply/1848/3 "2022-12-07T09:16:21Z")

</div>

In general, a function applied to known arguments is “equal” to its result. (Technically we talk of “definitional equality”, equality by definition of the function. Definitionally equal terms are indistinguishable for Coq.) So `[] ++ []` is equal to `[]`, also `2 + 3` equal to `5`, etc. Troubles start when some of the arguments are “unknown” (neutral terms built from variables/parameters): `x + 0` and `0 + x` are not known to be equal-by-definition by Coq – one of them will be equal to `x`, depending on the order in which the function `(+)` inspects its arguments.

---

<div class="post-metadata">

**Author:** ![MevenBertrand](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.rocq-prover.org/mevenbertrand/32/555_2.png) [@MevenBertrand](https://discourse.rocq-prover.org/u/MevenBertrand)\
**Post date:** [December 7, 2022, 11:11am UTC](https://discourse.rocq-prover.org/t/understand-the-reduction-behind-apply/1848/4 "2022-12-07T11:11:39Z")

</div>

The documentation mentions that “[t]he tactic [`apply`](https://coq.inria.fr/refman/proof-engine/tactics.html#coq:tacn.apply) relies on first-order unification with dependent types”. While this note is quite cryptic, it actually corresponds to the “mechanism of the reduction of `apply`”. Indeed, Coq’s unification algorithm in particular knows how to evaluate expressions before attempting to unify them.

In your precise case, what happens is that `Permutation_refl` has type `Permutation ?l ?l` for some unknown `?l` (technically, an [existential variable](https://coq.inria.fr/refman/language/extensions/evars.html)), and gets unified with your goal `Permutation [] ([]++[])`. Coq thus unifies `?l` with `[]` as the first arguments to `Permutation`, and then gets to compare `?l` (which has been solved to `[]`) and `[] ++ []`. It evaluates the second one, obtaining `[]` as LessnessR showed, which solves this second problem by reflexivity.

As far as I can tell, unification is not described in details in the manual. The closest I can think of is [this paper](https://www.cambridge.org/core/journals/journal-of-functional-programming/article/comprehensible-guide-to-a-new-unifier-for-cic-including-universe-polymorphism-and-overloading/19A095CA0645F89A772B7E2B7B3D92B2): although it does not describe the algorithm currently used in Coq, it is close enough to it that you should get a good idea of what happens there.
