|
1 |
| - |
2 |
| -Require Import SetoidClass. |
3 |
| - |
4 |
| -Section Countable. |
5 |
| - |
6 |
| - Context (A : Type) {A_Setoid : Setoid A}. |
7 |
| - Class Countable := { |
8 |
| - cnt_count :> A -> nat; |
9 |
| - cnt_choose :> nat -> A; |
10 |
| - cnt_inverse1 : forall a, a == cnt_choose (cnt_count a); |
11 |
| - cnt_inverse2 : forall n, n = cnt_count (cnt_choose n) |
12 |
| - }. |
13 |
| - Coercion cnt_count : Countable >-> Funclass. |
14 |
| -End Countable. |
15 |
| - |
16 |
| - |
17 |
| -(* |
18 |
| -Program Instance nat_setoid : Setoid nat. |
19 |
| -
|
20 |
| -Program Instance nat_count : Countable nat. |
21 |
| -*) |
22 |
| -Section NatFMap. |
23 |
| - Set Implicit Arguments. |
24 |
| - Context (Codom : Type). |
25 |
| - Record NatFMap := { |
26 |
| - nfm :> nat -> option Codom; |
27 |
| - nfm_bound : nat; |
28 |
| - nfm_bounded : forall a, a > nfm_bound -> nfm a = None |
29 |
| - }. |
30 |
| - |
31 |
| -Require Import Max. |
32 |
| - |
33 |
| - |
34 |
| - Program Definition nfm_set (n : nat) (v : Codom) (m : NatFMap) : NatFMap := |
35 |
| - {| nfm x := if Peano_dec.eq_nat_dec x n then Some v else nfm m x; |
36 |
| - nfm_bound := max n (nfm_bound m) |}. |
37 |
| - Obligation 1. |
38 |
| - remember (Peano_dec.eq_nat_dec a n) as s. |
39 |
| - destruct s; subst. |
40 |
| - contradict H. |
41 |
| - generalize (le_max_l n (nfm_bound m)); intro. |
42 |
| - auto with *. |
43 |
| - |
44 |
| - apply nfm_bounded. |
45 |
| - generalize (le_max_r n (nfm_bound m)); intro. |
46 |
| - unfold gt in *. |
47 |
| - unfold lt in *. |
48 |
| - generalize (Le.le_n_S _ _ H0); intro. |
49 |
| - rewrite H1. |
50 |
| - trivial. |
51 |
| - Qed. |
52 |
| - Definition nfm_fresh (m : NatFMap) := S (nfm_bound m). |
53 |
| - Hint Resolve nfm_bounded. |
54 |
| - Lemma nfm_fresh_is_fresh (m : NatFMap) : nfm m (nfm_fresh m) = None. |
55 |
| - auto. |
56 |
| - Qed. |
57 |
| - Unset Implicit Arguments. |
58 |
| -End NatFMap. |
59 |
| - |
60 |
| - |
61 |
| -Definition CFMap A `{Countable A} codom := NatFMap codom. |
62 |
| - |
63 |
| -Section CountableFiniteMaps. |
64 |
| - |
65 |
| - Context (A : Type) {A_Setoid : Setoid A} {A_Countable : Countable A}. |
66 |
| - Variable R : Type. |
67 |
| - Set Implicit Arguments. |
68 |
| - Definition cfm : CFMap A R -> A -> option R := |
69 |
| - fun m a => m (A_Countable a). |
70 |
| - Definition cfm_set (a : A) (v : R) (m : CFMap A R) : CFMap A R := |
71 |
| - nfm_set (A_Countable a) v m. |
72 |
| - Definition cfm_fresh (m : CFMap A R) : A := |
73 |
| - (cnt_choose _ (nfm_fresh m)). |
74 |
| - Lemma cfm_fresh_is_fresh (m : CFMap A R) : cfm m (cfm_fresh m) = None. |
75 |
| - cbv [cfm_fresh cfm]. |
76 |
| - rewrite <- cnt_inverse2. |
77 |
| - apply nfm_fresh_is_fresh. |
78 |
| - Qed. |
79 |
| - |
80 |
| -End CountableFiniteMaps. |
| 1 | + |
| 2 | +Require Import SetoidClass. |
| 3 | + |
| 4 | +Section Countable. |
| 5 | + |
| 6 | + Context (A : Type) {A_Setoid : Setoid A}. |
| 7 | + Class Countable := { |
| 8 | + cnt_count :> A -> nat; |
| 9 | + cnt_choose :> nat -> A; |
| 10 | + cnt_inverse1 : forall a, a == cnt_choose (cnt_count a); |
| 11 | + cnt_inverse2 : forall n, n = cnt_count (cnt_choose n) |
| 12 | + }. |
| 13 | + Coercion cnt_count : Countable >-> Funclass. |
| 14 | +End Countable. |
| 15 | + |
| 16 | + |
| 17 | +(* |
| 18 | +Program Instance nat_setoid : Setoid nat. |
| 19 | +
|
| 20 | +Program Instance nat_count : Countable nat. |
| 21 | +*) |
| 22 | +Section NatFMap. |
| 23 | + Set Implicit Arguments. |
| 24 | + Context (Codom : Type). |
| 25 | + Record NatFMap := { |
| 26 | + nfm :> nat -> option Codom; |
| 27 | + nfm_bound : nat; |
| 28 | + nfm_bounded : forall a, a > nfm_bound -> nfm a = None |
| 29 | + }. |
| 30 | + |
| 31 | +Require Import Max. |
| 32 | + |
| 33 | + |
| 34 | + Program Definition nfm_set (n : nat) (v : Codom) (m : NatFMap) : NatFMap := |
| 35 | + {| nfm x := if Peano_dec.eq_nat_dec x n then Some v else nfm m x; |
| 36 | + nfm_bound := max n (nfm_bound m) |}. |
| 37 | + Obligation 1. |
| 38 | + remember (Peano_dec.eq_nat_dec a n) as s. |
| 39 | + destruct s; subst. |
| 40 | + contradict H. |
| 41 | + generalize (le_max_l n (nfm_bound m)); intro. |
| 42 | + auto with *. |
| 43 | + |
| 44 | + apply nfm_bounded. |
| 45 | + generalize (le_max_r n (nfm_bound m)); intro. |
| 46 | + unfold gt in *. |
| 47 | + unfold lt in *. |
| 48 | + generalize (Le.le_n_S _ _ H0); intro. |
| 49 | + rewrite H1. |
| 50 | + trivial. |
| 51 | + Qed. |
| 52 | + Definition nfm_fresh (m : NatFMap) := S (nfm_bound m). |
| 53 | + Hint Resolve nfm_bounded. |
| 54 | + Lemma nfm_fresh_is_fresh (m : NatFMap) : nfm m (nfm_fresh m) = None. |
| 55 | + auto. |
| 56 | + Qed. |
| 57 | + Unset Implicit Arguments. |
| 58 | +End NatFMap. |
| 59 | + |
| 60 | + |
| 61 | +Definition CFMap A `{Countable A} codom := NatFMap codom. |
| 62 | + |
| 63 | +Section CountableFiniteMaps. |
| 64 | + |
| 65 | + Context (A : Type) {A_Setoid : Setoid A} {A_Countable : Countable A}. |
| 66 | + Variable R : Type. |
| 67 | + Set Implicit Arguments. |
| 68 | + Definition cfm : CFMap A R -> A -> option R := |
| 69 | + fun m a => m (A_Countable a). |
| 70 | + Definition cfm_set (a : A) (v : R) (m : CFMap A R) : CFMap A R := |
| 71 | + nfm_set (A_Countable a) v m. |
| 72 | + Definition cfm_fresh (m : CFMap A R) : A := |
| 73 | + (cnt_choose _ (nfm_fresh m)). |
| 74 | + Lemma cfm_fresh_is_fresh (m : CFMap A R) : cfm m (cfm_fresh m) = None. |
| 75 | + cbv [cfm_fresh cfm]. |
| 76 | + rewrite <- cnt_inverse2. |
| 77 | + apply nfm_fresh_is_fresh. |
| 78 | + Qed. |
| 79 | + Inductive cfm_def_at (m : CFMap A R) : A -> Prop := |
| 80 | + | cfm_def_witness : forall a v, Some v = cfm m a -> cfm_def_at m a. |
| 81 | + Definition cfm_make_def_at (m : CFMap A R) (a : A) : option (cfm_def_at m a) := |
| 82 | + match (cfm m a) as o return (o = cfm m a -> option (cfm_def_at m a)) with |
| 83 | + | Some r => fun H : Some r = cfm m a => Some (cfm_def_witness m a H) |
| 84 | + | None => fun _ => None |
| 85 | + end eq_refl. |
| 86 | + Fixpoint cfm_dom_fold_to |
| 87 | + (m : CFMap A R) (T : Type) (f : T -> forall a, cfm_def_at m a -> T) (o : T) (n : nat) : T := |
| 88 | + match n with |
| 89 | + | O => o |
| 90 | + | S n' => let t' := (cfm_dom_fold_to f o n') in |
| 91 | + match (cfm_make_def_at m (cnt_choose A n)) with |
| 92 | + | Some da => f t' (cnt_choose A n) da |
| 93 | + | _ => t' end |
| 94 | + end. |
| 95 | + |
| 96 | + Definition cfm_dom_fold (m : CFMap A R) (T : Type) |
| 97 | + (f : T -> forall a, cfm_def_at m a -> T) (o : T) : T := |
| 98 | + cfm_dom_fold_to f o (nfm_bound m). |
| 99 | + Definition cfm_def_get (m : CFMap A R) (a : A) (P : cfm_def_at m a) : R. |
| 100 | + remember (cfm m a). |
| 101 | + destruct (o). |
| 102 | + exact r. |
| 103 | + contradict P. |
| 104 | + intro. |
| 105 | + destruct H. |
| 106 | + rewrite <- Heqo in H. |
| 107 | + inversion H. |
| 108 | + Defined. |
| 109 | + |
| 110 | +End CountableFiniteMaps. |
| 111 | + |
| 112 | + |
0 commit comments