|
| 1 | +{-| |
| 2 | +This module shows that artifacts of choice calculus expressions have a fixed number of children. |
| 3 | +Afterwards, we introduce some more usable lemmas on top og this insight. |
| 4 | +-} |
| 5 | +open import Vatras.Framework.Definitions using (𝔽; 𝔸; atoms) |
| 6 | +module Vatras.Lang.2CC.FixedArtifactLength (Dimension : 𝔽) (A : 𝔸) where |
| 7 | + |
| 8 | +open import Data.Bool using (true; false) |
| 9 | +open import Data.Empty using (⊥-elim) |
| 10 | +open import Data.List as List using (List; []; _∷_; _++_) |
| 11 | +import Data.List.Properties as List |
| 12 | +open import Data.List.Relation.Ternary.Interleaving.Propositional using (Interleaving; []; consˡ; consʳ) |
| 13 | +open import Data.List.Relation.Unary.All as All using (All; []; _∷_) |
| 14 | +import Data.List.Relation.Unary.All.Properties |
| 15 | +open import Data.List.Relation.Unary.AllPairs as AllPairs using (AllPairs; []; _∷_) |
| 16 | +open import Data.Nat as ℕ using (ℕ; suc; _+_; _∸_; _*_; _≤_; z≤n; s≤s; _≥_) |
| 17 | +import Data.Nat.Properties as ℕ |
| 18 | +open import Data.Product using (_×_; _,_; proj₂; ∃-syntax) |
| 19 | +open import Function using (_∘_; const) |
| 20 | +open import Relation.Binary.PropositionalEquality as Eq using (refl; _≡_; _≢_) |
| 21 | +open import Size using (Size; ∞) |
| 22 | + |
| 23 | +import Vatras.Util.List as List |
| 24 | +open import Vatras.Data.EqIndexedSet using (_∈_; _⊆_) |
| 25 | +open import Vatras.Framework.Variants using (Rose; children-equality) |
| 26 | +open import Vatras.Lang.2CC Dimension using (2CC; _⟨_,_⟩; _-<_>-; ⟦_⟧) |
| 27 | +open import Vatras.Lang.2CC.ReflectsVariantSize using (reflectsVariantSize) |
| 28 | +open import Vatras.Succinctness.Sizes using (sizeRose; size2CC) |
| 29 | + |
| 30 | +_≉_ : Rose ∞ A → Rose ∞ A → Set |
| 31 | +(a₁ Rose.-< cs₁ >-) ≉ (a₂ Rose.-< cs₂ >-) = List.length cs₁ ≢ List.length cs₂ |
| 32 | + |
| 33 | +{-| |
| 34 | +The key insight of this module: |
| 35 | +Given a choice calculus expression with an artifact at the root, |
| 36 | +all expressed variants must have the same number of children. |
| 37 | +-} |
| 38 | +fixedChildCount : ∀ {i} |
| 39 | + → {a₁ : atoms A} {cs₁ : List (Rose ∞ A)} |
| 40 | + → {a₂ : atoms A} {cs₂ : List (2CC i A)} |
| 41 | + → (a₁ Rose.-< cs₁ >-) ∈ ⟦ a₂ -< cs₂ >- ⟧ |
| 42 | + → List.length cs₁ ≡ List.length cs₂ |
| 43 | +fixedChildCount {cs₁ = cs₁} {cs₂ = cs₂} (c , v≡e) = |
| 44 | + List.length cs₁ |
| 45 | + ≡⟨ Eq.cong List.length (children-equality v≡e) ⟩ |
| 46 | + List.length (List.map (λ e → ⟦ e ⟧ c) cs₂) |
| 47 | + ≡⟨ List.length-map (λ e → ⟦ e ⟧ c) cs₂ ⟩ |
| 48 | + List.length cs₂ |
| 49 | + ∎ |
| 50 | + where |
| 51 | + open Eq.≡-Reasoning |
| 52 | + |
| 53 | +{-| |
| 54 | +We can partition a list of variants |
| 55 | +on whether we can choose the left or right alternative of a choice |
| 56 | +in order to configure each variant. |
| 57 | +-} |
| 58 | +partition : ∀ {i : Size} |
| 59 | + → (D : Dimension) (c₁ c₂ : 2CC i A) |
| 60 | + → (vs : List (Rose ∞ A)) |
| 61 | + → AllPairs _≉_ vs |
| 62 | + → All (_∈ ⟦ D 2CC.⟨ c₁ , c₂ ⟩ ⟧) vs |
| 63 | + → ∃[ vs₁ ] ∃[ vs₂ ] |
| 64 | + Interleaving vs₁ vs₂ vs |
| 65 | + × All (_∈ ⟦ c₁ ⟧) vs₁ |
| 66 | + × All (_∈ ⟦ c₂ ⟧) vs₂ |
| 67 | +partition D c₁ c₂ [] unique-vs vs⊆e = [] , [] , [] , [] , [] |
| 68 | +partition D c₁ c₂ (v ∷ vs) (v∉vs ∷ unique-vs) ((c , v≡e) ∷ vs⊆e) |
| 69 | + with partition D c₁ c₂ vs unique-vs vs⊆e |
| 70 | +... | vs₁ , vs₂ , partition , vs₁⊆e , vs₂⊆e |
| 71 | + with c D |
| 72 | +... | true = v ∷ vs₁ , vs₂ , consˡ partition , (c , v≡e) ∷ vs₁⊆e , vs₂⊆e |
| 73 | +... | false = vs₁ , v ∷ vs₂ , consʳ partition , vs₁⊆e , (c , v≡e) ∷ vs₂⊆e |
| 74 | + |
| 75 | +{-| |
| 76 | +Gives a lower bound on the size of a choice calculus expression |
| 77 | +given that it expresses a number of variants with pairwise different child count. |
| 78 | +-} |
| 79 | +sum≤size2CC : ∀ {i : Size} |
| 80 | + → (e : 2CC i A) |
| 81 | + → (vs : List (Rose ∞ A)) |
| 82 | + → AllPairs (_≉_) vs |
| 83 | + → All (_∈ ⟦ e ⟧) vs |
| 84 | + → List.sum (List.map sizeRose vs) ≤ size2CC e |
| 85 | +sum≤size2CC (a -< cs >-) [] unique-vs vs⊆e = z≤n |
| 86 | +sum≤size2CC (a -< cs >-) (v ∷ []) unique-vs (v∈e ∷ []) = |
| 87 | + begin |
| 88 | + List.sum (List.map sizeRose (v ∷ [])) |
| 89 | + ≡⟨⟩ |
| 90 | + sizeRose v + 0 |
| 91 | + ≡⟨ ℕ.+-identityʳ (sizeRose v) ⟩ |
| 92 | + sizeRose v |
| 93 | + ≤⟨ reflectsVariantSize v (a -< cs >-) v∈e ⟩ |
| 94 | + size2CC (a -< cs >-) |
| 95 | + ∎ |
| 96 | + where |
| 97 | + open ℕ.≤-Reasoning |
| 98 | +sum≤size2CC (a -< cs >-) ((a₁ Rose.-< cs₁ >-) ∷ (a₂ Rose.-< cs₂ >-) ∷ vs) ((v₁≢v₂ ∷ v₁∉vs) ∷ unique-vs) (v₁∈e ∷ v₂∈e ∷ vs⊆e) = |
| 99 | + ⊥-elim (v₁≢v₂ (Eq.trans (fixedChildCount v₁∈e) (Eq.sym (fixedChildCount v₂∈e)))) |
| 100 | +sum≤size2CC (D ⟨ c₁ , c₂ ⟩) vs unique-vs vs⊆e with partition D c₁ c₂ vs unique-vs vs⊆e |
| 101 | +... | vs₁ , vs₂ , partition , vs₁⊆c₁ , vs₂⊆c₂ = |
| 102 | + begin |
| 103 | + List.sum (List.map sizeRose vs) |
| 104 | + ≡⟨ List.sum-Interleaving (List.map-Interleaving partition) ⟨ |
| 105 | + List.sum (List.map sizeRose vs₁) + List.sum (List.map sizeRose vs₂) |
| 106 | + ≤⟨ ℕ.+-mono-≤ (sum≤size2CC c₁ vs₁ (List.AllPairs-resp-⊆ (List.Interleaving⇒Sublistˡ partition) unique-vs) vs₁⊆c₁) (sum≤size2CC c₂ vs₂ (List.AllPairs-resp-⊆ (List.Interleaving⇒Sublistʳ partition) unique-vs) vs₂⊆c₂) ⟩ |
| 107 | + size2CC c₁ + size2CC c₂ |
| 108 | + <⟨ ℕ.n<1+n (size2CC c₁ + size2CC c₂) ⟩ |
| 109 | + size2CC (D ⟨ c₁ , c₂ ⟩) |
| 110 | + ∎ |
| 111 | + where |
| 112 | + open ℕ.≤-Reasoning |
| 113 | + |
| 114 | +{-| |
| 115 | +Gives a lower bound on the size of a choice calculus expression |
| 116 | +given that it expresses a number of variants with pairwise different child count. |
| 117 | +In contrast to `sum≤size2CC`, this lemma is a simplified special case |
| 118 | +which makes use of a lower bound on the variant size. |
| 119 | +-} |
| 120 | +different-children-counts : |
| 121 | + ∀ {i : Size} |
| 122 | + → (n : ℕ) |
| 123 | + → (e : 2CC i A) |
| 124 | + → (vs : List (Rose ∞ A)) |
| 125 | + → All (_∈ ⟦ e ⟧) vs |
| 126 | + → All (λ v → sizeRose v ≥ n) vs |
| 127 | + → AllPairs _≉_ vs |
| 128 | + → size2CC e ≥ List.length vs * n |
| 129 | +different-children-counts n e vs vs⊆e vs≥n unique-vs = |
| 130 | + begin |
| 131 | + List.length vs * n |
| 132 | + ≡⟨ List.sum-replicate (List.length vs) n ⟨ |
| 133 | + List.sum (List.replicate (List.length vs) n) |
| 134 | + ≡⟨ Eq.cong List.sum (List.map-const n vs) ⟨ |
| 135 | + List.sum (List.map (const n) vs) |
| 136 | + ≤⟨ List.sum-map-≤-with∈ vs (λ v v∈vs → All.lookup vs≥n v∈vs) ⟩ |
| 137 | + List.sum (List.map sizeRose vs) |
| 138 | + ≤⟨ sum≤size2CC e vs unique-vs (vs⊆e) ⟩ |
| 139 | + size2CC e |
| 140 | + ∎ |
| 141 | + where |
| 142 | + open ℕ.≤-Reasoning |
0 commit comments