95 lines
2.4 KiB
Coq
95 lines
2.4 KiB
Coq
Require Import Streams.
|
|
Require Import Lia.
|
|
|
|
Inductive exactly {A:Type} (p:A -> Prop)
|
|
: Stream A -> nat -> Prop :=
|
|
| ExactDone: forall s:Stream A, exactly p s 0
|
|
| ExactMore: forall (n:nat) (s:Stream A),
|
|
p (hd s) -> exactly p (tl s) n -> exactly p s (S n).
|
|
|
|
Inductive atMost {A:Type} (p:A -> Prop)
|
|
: Stream A -> nat -> Prop :=
|
|
| MostDone: forall (n:nat) (s:Stream A),
|
|
atMost p s n
|
|
| MostMore: forall (n:nat) (s:Stream A),
|
|
p (hd s) -> atMost p (tl s) n -> atMost p s (S n).
|
|
|
|
CoInductive atLeast {A:Type} (p:A -> Prop)
|
|
: Stream A -> nat -> nat -> Prop :=
|
|
| LeastDone: forall (N n:nat) (s:Stream A),
|
|
n>=N -> atLeast p s N n
|
|
| LeastDoneMore: forall (N n:nat) (s:Stream A),
|
|
n>=N -> p (hd s) -> atLeast p (tl s) N (S n) -> atLeast p s N n
|
|
| LeastMore: forall (N n:nat) (s:Stream A),
|
|
n<=N -> p (hd s) -> atLeast p (tl s) N (S n) -> atLeast p s N n.
|
|
|
|
Inductive range {A:Type} (p:A->Prop) (low high:nat)
|
|
: Stream A -> nat -> Prop :=
|
|
| RangeDone: forall (s:Stream A) (n:nat),
|
|
n>=low /\ n<=high
|
|
-> range p low high s n
|
|
| RangeMore: forall (s:Stream A) (n:nat),
|
|
p (hd s)
|
|
-> range p low high (tl s) (S n)
|
|
-> range p low high s n.
|
|
|
|
Definition egstr1 := const 3.
|
|
|
|
Goal
|
|
range (fun x=>x=2) 2 5
|
|
(Cons 2 (Cons 2 (Cons 2 (Cons 2 (Cons 2 egstr1))))) 0.
|
|
Proof.
|
|
Check RangeMore.
|
|
eapply RangeMore; simpl; try lia.
|
|
eapply RangeMore; simpl; try lia.
|
|
(* eapply RangeDone; simpl; try lia. *)
|
|
eapply RangeMore; simpl; try lia.
|
|
eapply RangeMore; simpl; try lia.
|
|
eapply RangeMore; simpl; try lia.
|
|
eapply RangeDone; simpl; try lia.
|
|
(* eapply RangeMore; simpl; try lia. ✗ *)
|
|
Qed.
|
|
|
|
Goal
|
|
atLeast (fun x=>x=3) (const 3) 0 0.
|
|
Proof.
|
|
apply LeastDone; simpl; lia.
|
|
Qed.
|
|
|
|
Goal
|
|
atLeast (fun x=>x=2) (const 3) 0 0.
|
|
Proof.
|
|
apply LeastDone; simpl; lia.
|
|
Qed.
|
|
|
|
Goal
|
|
atLeast (fun x=>x=2)
|
|
(Cons 2 (Cons 2 (Cons 2 (Cons 2 (Cons 2 egstr1)))))
|
|
2 0.
|
|
Proof.
|
|
constructor 3; simpl; try lia.
|
|
apply LeastMore; simpl; try lia.
|
|
apply LeastMore; simpl; try lia.
|
|
apply LeastDoneMore; simpl; try lia.
|
|
apply LeastDoneMore; simpl; try lia.
|
|
apply LeastDone; simpl; lia.
|
|
Qed.
|
|
|
|
Goal
|
|
atMost (fun x=>x=3) egstr1 2.
|
|
Proof.
|
|
(* Could do a [left] here and be over with it *)
|
|
right; simpl; try lia.
|
|
right; simpl; try lia.
|
|
Fail (right; simpl; try lia).
|
|
left.
|
|
Qed.
|
|
|
|
Goal
|
|
exactly (fun x=>x=3) egstr1 2.
|
|
Proof.
|
|
right; simpl; try lia.
|
|
right; simpl; try lia.
|
|
left.
|
|
Qed.
|