Skip to content

Commit

Permalink
Add replicate to listz
Browse files Browse the repository at this point in the history
  • Loading branch information
tchajed committed Jul 12, 2024
1 parent 78d9529 commit c258e7c
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/Helpers/ListZ.v
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,49 @@ Proof.
rewrite firstn_skipn //.
Qed.

Definition replicate n x :=
list.replicate (Z.to_nat n) x.

Lemma replicate_length' n x :
length (replicate n x) = n `max` 0.
Proof.
rewrite /replicate /length.
rewrite list.replicate_length.
lia.
Qed.

Lemma replicate_length n x :
0 ≤ n →
length (replicate n x) = n.
Proof.
intros H.
rewrite replicate_length'; lia.
Qed.

Lemma lookup_replicate i n x :
0 ≤ i < n →
replicate n x !!! i = x.
Proof.
intros.
pose proof (replicate_length' n x).
list_simpl.
rewrite /replicate.
apply list.lookup_replicate_1 in Hget.
intuition congruence.
Qed.

(* (perhaps common) special case of replicating the inhabitant or "zero value"
of a type *)
Lemma lookup_replicate_0 i n x :
x = inhabitant →
replicate n x !!! i = x.
Proof.
intros.
pose proof (replicate_length' n x).
destruct (decide (0 ≤ i < n)); list_solve.
rewrite lookup_replicate //; lia.
Qed.

End list.

Lemma fmap_length {A B} `{!Inhabited A, !Inhabited B}
Expand Down

0 comments on commit c258e7c

Please sign in to comment.