简体   繁体   中英

How to define exp for Church Numerals in coq?

I am currently learning coq thanks to the Software Fondation's ebook . I successfully wrote the addition as follow:

Definition cnat := forall X : Type, (X -> X) -> X -> X.
Definition plus (n m : cnat) : cnat :=
  fun (X : Type) (f : X -> X) (x : X) => n X f (m X f x).

But I'm stuck with exp because of the following error:

Definition exp (n m : cnat) : cnat :=
  m cnat (mult n) n.
(*
In environment
n : cnat
m : cnat
The term "cnat" has type "Type@{cnat.u0+1}"
while it is expected to have type "Type@{cnat.u0}"
(universe inconsistency). 
*)

Since they wrote:

If you hit a "Universe inconsistency" error, try iterating over a different type. Iterating over cnat itself is usually problematic. I tried using the definition of cnat:

Definition exp (n m : cnat) : cnat :=
  m (forall X : Type, (X -> X) -> X) (mult n) n.

But then I have:

In environment
n : cnat
m : cnat
The term "mult n" has type "cnat -> cnat"
while it is expected to have type
 "(forall X : Type, (X -> X) -> X) -> forall X : Type, (X -> X) -> X"
(universe inconsistency).

I do not ask for a solution, but I'd really like to understand these errors. Thanks for your lights !

Just post the solution for others who got stuck on this problem.

Definition exp (n m : cnat) : cnat := 
  fun X => (m (X->X)) (n X).

The key is to understand m (X->X) . The figure below might be helpful.

An example of 2 ^ 2

2 ^ 2 的示例

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM