Library hydras.solutions_exercises.MorePRExamples

Require Import primRec Arith ArithRing List.
Import extEqualNat.

Solution to an exercise


Definition double n := n × 2.

Lemma doubleIsPR : isPR 1 double.


Fixpoint exp n p :=
  match p with
    0 ⇒ 1
  | S mexp n m × n
  end.


Definition exp_alt := fun a bnat_rec (fun _nat)
                                     1
                                     (fun _ yy × a)
                                     b.

Remark exp_alt_ok : extEqual 2 exp_alt exp.

#[local] Instance exp_alt_PR : isPR 2 exp_alt.

#[export] Instance expIsPR : isPR 2 exp.


Fixpoint tower2 n :=
  match n with
    0 ⇒ 1
  | S pexp 2 (tower2 p)
  end.


Definition tower2_alt h : nat := nat_rec (fun nnat)
                                1
                                (fun _ yexp 2 y)
                                h.

Remark tower2_alt_ok : extEqual 1 tower2_alt tower2.

#[local] Instance tower2_alt_PR : isPR 1 tower2_alt.

#[export] Instance tower2IsPR : isPR 1 tower2.

Fixpoint fact n :=
  match n with 0 ⇒ 1
          | S pn × fact p
  end.

Definition fact_alt
  : nat nat :=
  fun anat_rec _ 1 (fun x yS x × y) a.

Remark fact_alt_ok : extEqual 1 fact_alt fact.

#[local] Instance fact_altIsPR : isPR 1 fact_alt.

#[export] Instance factIsPR : isPR 1 fact.