85 lines
1.8 KiB
Coq
85 lines
1.8 KiB
Coq
Require Import Vector Bvector.
|
|
Import BvectorNotations.
|
|
Import VectorNotations.
|
|
|
|
Check [true;false;false]%Bvector.
|
|
Example foo := [true]%Bvector.
|
|
|
|
Check cons nat 3.
|
|
Fixpoint cust_nth {A:Type} {n:nat}
|
|
(v:Vector.t A n) (i:nat) : option A :=
|
|
if (Nat.eqb (n-1) i) then
|
|
match v with
|
|
| Vector.nil _ => None
|
|
| Vector.cons _ x n' vs => Some x
|
|
end
|
|
else
|
|
match v with
|
|
| Vector.nil _ => None
|
|
| Vector.cons _ _ _ vs => cust_nth vs i
|
|
end.
|
|
Compute cust_nth [true; false; true]%Bvector 0.
|
|
Compute cust_nth [true; false; true]%Bvector 1.
|
|
Compute cust_nth [true; false; true]%Bvector 2.
|
|
|
|
Compute cust_nth [3; 2; 1]%vector 0.
|
|
Compute cust_nth [3; 2; 1]%vector 1.
|
|
Compute cust_nth [3; 2; 1]%vector 2.
|
|
|
|
(* bool/bit to nat *)
|
|
Definition b2n (b:bool) : nat :=
|
|
match b with
|
|
| true => 1
|
|
| false => 0
|
|
end.
|
|
|
|
(* bool vector to nat *)
|
|
Fixpoint bvec2n {n:nat} (v:Vector.t bool n) : nat :=
|
|
match v with
|
|
| Vector.nil _ => 0
|
|
| Vector.cons _ x n' vs =>
|
|
match x with
|
|
| true => (Nat.pow 2 n') + (bvec2n vs)
|
|
| false => (bvec2n vs)
|
|
end
|
|
end.
|
|
Compute bvec2n [true; false; true]%Bvector.
|
|
|
|
(* bool vector to nat *)
|
|
Fixpoint bvec2n {n:nat} (v:Vector.t bool n) : nat :=
|
|
match v with
|
|
| Vector.nil _ => 0
|
|
| Vector.cons _ x n' vs =>
|
|
match x with
|
|
| true => (Nat.pow 2 n') + (bvec2n vs)
|
|
| false => (bvec2n vs)
|
|
end
|
|
end.
|
|
|
|
(* nat to bool/bit *)
|
|
Definition n2b (n:nat) : bool :=
|
|
match n with
|
|
| O => false
|
|
| _ => true
|
|
end.
|
|
|
|
Definition modulob (a b:nat): bool:=
|
|
match (Nat.modulo a b) with
|
|
| O => true
|
|
| _ => false
|
|
end.
|
|
|
|
(* nat to bool vector *)
|
|
Fixpoint n2bvec (n z:nat) : Bvector z :=
|
|
match z with
|
|
| O => []%vector
|
|
| S z' =>
|
|
let e2n := Nat.pow 2 z' in
|
|
if Nat.leb e2n n then
|
|
Vector.cons _ true z' (n2bvec (n - e2n) z')
|
|
else
|
|
Vector.cons _ false z' (n2bvec n z')
|
|
end.
|
|
Compute n2bvec 6 3.
|
|
|