55 lines
1002 B
Coq
55 lines
1002 B
Coq
Inductive m22 : Type :=
|
|
| M22: nat -> nat -> nat -> nat -> m22.
|
|
|
|
Definition m22Mult (n m:m22): m22 :=
|
|
match n, m with
|
|
| M22 a b c d, M22 p q r s =>
|
|
M22 (a*p+b*r) (a*q+b*s) (c*p+d*r) (c*q+d*s)
|
|
end.
|
|
|
|
Definition M: m22 := (M22 1 1 1 0).
|
|
|
|
Compute m22Mult (M22 5 3 3 2) M.
|
|
(* = M22 8 5 5 3 : m22 *)
|
|
|
|
Fixpoint m22Pow (acc m:m22) (n:nat): m22 :=
|
|
match n with
|
|
| 0 => acc
|
|
| S n' => m22Pow (m22Mult acc m) m n'
|
|
end.
|
|
Compute m22Pow M M 1.
|
|
|
|
Fixpoint fibo_aux (acc:nat*nat) (n:nat): nat :=
|
|
match n with
|
|
| 0 => snd acc
|
|
| S n' =>
|
|
let '(a,b) := acc in
|
|
fibo_aux (a+b, a) n'
|
|
end.
|
|
Definition fibo (n:nat) : nat := fibo_aux (1,0) n.
|
|
|
|
Definition projc12 (m:m22): nat :=
|
|
match m with
|
|
| M22 _ _ c _ => c
|
|
end.
|
|
|
|
Compute fibo 7.
|
|
Compute projc12 (m22Pow M M 7).
|
|
|
|
Theorem foo: forall n:nat,
|
|
fibo (S n) = projc12 (m22Pow M M n).
|
|
Proof.
|
|
intros n.
|
|
induction n.
|
|
- unfold fibo.
|
|
now simpl.
|
|
- unfold fibo.
|
|
simpl.
|
|
unfold M.
|
|
simpl.
|
|
|
|
Compute fibo 5.
|
|
Compute fibo 8.
|
|
|
|
|