I want to define a function app_1
which converts an n
-ary function f : X ^^ n --> Y
into a new function f' : (Z -> X) ^^ n --> Y
, provided that there is a z : Z
to apply once to all of its arguments. For instance,
Example ex1 : app_1 plus 2 S pred = 4.
trivial. Qed.
app_1 plus 2
is able to take the unary functions S
and pred
as arguments because it applies both of them to 2
first, and then apply the results to plus
.
Here's my attempted definition of app_1
:
Fixpoint app_1 {X Y Z : Type} {n : nat}
(f : X ^^ n --> Y) (z : Z) :
(Z -> X) ^^ n --> Y :=
match n return ((Z -> X) ^^ n --> Y) with
| O => f
| S n' => fun g => app_1 (f (g z)) z
end.
Which doesn't work, because Coq doesn't like the clause | O => f
, and says:
The term "f" has type "X ^^ n --> Y" while it is expected to have type "(Z -> X) ^^ 0 --> Y".
But if n = O
, X ^^ n --> Y = X ^^ 0 --> Y = Y = (Z -> X) ^^ 0 --> Y
, so it's not a type mismatch. The problem is that Coq can't use the contextual information n = O
to figure out the equivalence.
Some preliminary searches show that this is a well-known problem, but it's a little confusing how to apply those discussions to this case. For instance, an almost identical question on SO is solved using return
, which is what I (sort of) tried above as well, to no avail.
Is there a way to make this pattern match work?