|
| 1 | +open import Mockingbird.Forest using (Forest) |
| 2 | +import Mockingbird.Forest.Birds as Birds |
| 3 | + |
| 4 | +-- The Master Forest |
| 5 | +module Mockingbird.Problems.Chapter18 {b ℓ} (forest : Forest {b} {ℓ}) |
| 6 | + ⦃ _ : Birds.HasStarling forest ⦄ |
| 7 | + ⦃ _ : Birds.HasKestrel forest ⦄ where |
| 8 | + |
| 9 | +open import Data.Maybe using (Maybe; nothing; just) |
| 10 | +open import Data.Product using (_×_; _,_; proj₁; ∃-syntax) |
| 11 | +open import Data.Vec using (Vec; []; _∷_; _++_) |
| 12 | +open import Data.Vec.Relation.Unary.Any.Properties using (++⁺ʳ) |
| 13 | +open import Function using (_$_) |
| 14 | +open import Level using (_⊔_) |
| 15 | +open import Mockingbird.Forest.Combination.Vec forest using (⟨_⟩; here; there; [_]; _⟨∙⟩_∣_; _⟨∙⟩_; [#_]) |
| 16 | +open import Mockingbird.Forest.Combination.Vec.Properties forest using (subst′; weaken-++ˡ; weaken-++ʳ; ++-comm) |
| 17 | +open import Relation.Unary using (_∈_) |
| 18 | + |
| 19 | +open Forest forest |
| 20 | +open import Mockingbird.Forest.Birds forest |
| 21 | + |
| 22 | +problem₁ : HasIdentity |
| 23 | +problem₁ = record |
| 24 | + { I = S ∙ K ∙ K |
| 25 | + ; isIdentity = λ x → begin |
| 26 | + S ∙ K ∙ K ∙ x ≈⟨ isStarling K K x ⟩ |
| 27 | + K ∙ x ∙ (K ∙ x) ≈⟨ isKestrel x (K ∙ x) ⟩ |
| 28 | + x ∎ |
| 29 | + } |
| 30 | + |
| 31 | +private instance hasIdentity = problem₁ |
| 32 | + |
| 33 | +problem₂ : HasMockingbird |
| 34 | +problem₂ = record |
| 35 | + { M = S ∙ I ∙ I |
| 36 | + ; isMockingbird = λ x → begin |
| 37 | + S ∙ I ∙ I ∙ x ≈⟨ isStarling I I x ⟩ |
| 38 | + I ∙ x ∙ (I ∙ x) ≈⟨ congʳ $ isIdentity x ⟩ |
| 39 | + x ∙ (I ∙ x) ≈⟨ congˡ $ isIdentity x ⟩ |
| 40 | + x ∙ x ∎ |
| 41 | + } |
| 42 | + |
| 43 | +private instance hasMockingbird = problem₂ |
| 44 | + |
| 45 | +problem₃ : HasThrush |
| 46 | +problem₃ = record |
| 47 | + { T = S ∙ (K ∙ (S ∙ I)) ∙ K |
| 48 | + ; isThrush = λ x y → begin |
| 49 | + S ∙ (K ∙ (S ∙ I)) ∙ K ∙ x ∙ y ≈⟨ congʳ $ isStarling (K ∙ (S ∙ I)) K x ⟩ |
| 50 | + K ∙ (S ∙ I) ∙ x ∙ (K ∙ x) ∙ y ≈⟨ (congʳ $ congʳ $ isKestrel (S ∙ I) x) ⟩ |
| 51 | + S ∙ I ∙ (K ∙ x) ∙ y ≈⟨ isStarling I (K ∙ x) y ⟩ |
| 52 | + I ∙ y ∙ (K ∙ x ∙ y) ≈⟨ congʳ $ isIdentity y ⟩ |
| 53 | + y ∙ (K ∙ x ∙ y) ≈⟨ congˡ $ isKestrel x y ⟩ |
| 54 | + y ∙ x ∎ |
| 55 | + } |
| 56 | + |
| 57 | +-- TODO: Problem 4. |
| 58 | + |
| 59 | +I∈⟨S,K⟩ : I ∈ ⟨ S ∷ K ∷ [] ⟩ |
| 60 | +I∈⟨S,K⟩ = subst′ refl $ [# 0 ] ⟨∙⟩ [# 1 ] ⟨∙⟩ [# 1 ] |
| 61 | + |
| 62 | +-- Try to strengthen a proof of X ∈ ⟨ y ∷ xs ⟩ to X ∈ ⟨ xs ⟩, which can be done |
| 63 | +-- if y does not occur in X. |
| 64 | +strengthen : ∀ {n X y} {xs : Vec Bird n} → X ∈ ⟨ y ∷ xs ⟩ → Maybe (X ∈ ⟨ xs ⟩) |
| 65 | +-- NOTE: it could be the case that y ∈ xs, but checking that requires decidable |
| 66 | +-- equality. |
| 67 | +strengthen [ here X≈y ] = nothing |
| 68 | +strengthen [ there X∈xs ] = just [ X∈xs ] |
| 69 | +strengthen (Y∈⟨y,xs⟩ ⟨∙⟩ Z∈⟨y,xs⟩ ∣ YZ≈X) = do |
| 70 | + Y∈⟨xs⟩ ← strengthen Y∈⟨y,xs⟩ |
| 71 | + Z∈⟨xs⟩ ← strengthen Z∈⟨y,xs⟩ |
| 72 | + just $ Y∈⟨xs⟩ ⟨∙⟩ Z∈⟨xs⟩ ∣ YZ≈X |
| 73 | + where |
| 74 | + open import Data.Maybe.Categorical using (monad) |
| 75 | + open import Category.Monad using (RawMonad) |
| 76 | + open RawMonad (monad {b ⊔ ℓ}) |
| 77 | + |
| 78 | +eliminate : ∀ {n X y} {xs : Vec Bird n} → X ∈ ⟨ y ∷ xs ⟩ → ∃[ X′ ] (X′ ∈ ⟨ S ∷ K ∷ xs ⟩ × X′ ∙ y ≈ X) |
| 79 | +eliminate {X = X} {y} [ here X≈y ] = (I , weaken-++ˡ I∈⟨S,K⟩ , trans (isIdentity y) (sym X≈y)) |
| 80 | +eliminate {X = X} {y} [ there X∈xs ] = (K ∙ X , [# 1 ] ⟨∙⟩ [ ++⁺ʳ (S ∷ K ∷ []) X∈xs ] , isKestrel X y) |
| 81 | +eliminate {X = X} {y} (_⟨∙⟩_∣_ {Y} {Z} Y∈⟨y,xs⟩ [ here Z≈y ] YZ≈X) with strengthen Y∈⟨y,xs⟩ |
| 82 | +... | just Y∈⟨xs⟩ = (Y , weaken-++ʳ (S ∷ K ∷ []) Y∈⟨xs⟩ , trans (congˡ (sym Z≈y)) YZ≈X) |
| 83 | +... | nothing = |
| 84 | + let (Y′ , Y′∈⟨S,K,xs⟩ , Y′y≈Y) = eliminate Y∈⟨y,xs⟩ |
| 85 | + |
| 86 | + SY′Iy≈X : S ∙ Y′ ∙ I ∙ y ≈ X |
| 87 | + SY′Iy≈X = begin |
| 88 | + S ∙ Y′ ∙ I ∙ y ≈⟨ isStarling Y′ I y ⟩ |
| 89 | + Y′ ∙ y ∙ (I ∙ y) ≈⟨ congʳ $ Y′y≈Y ⟩ |
| 90 | + Y ∙ (I ∙ y) ≈⟨ congˡ $ isIdentity y ⟩ |
| 91 | + Y ∙ y ≈˘⟨ congˡ Z≈y ⟩ |
| 92 | + Y ∙ Z ≈⟨ YZ≈X ⟩ |
| 93 | + X ∎ |
| 94 | + in (S ∙ Y′ ∙ I , [# 0 ] ⟨∙⟩ Y′∈⟨S,K,xs⟩ ⟨∙⟩ weaken-++ˡ I∈⟨S,K⟩ , SY′Iy≈X) |
| 95 | +eliminate {X = X} {y} (_⟨∙⟩_∣_ {Y} {Z} Y∈⟨y,xs⟩ Z∈⟨y,xs⟩ YZ≈X) = |
| 96 | + let (Y′ , Y′∈⟨S,K,xs⟩ , Y′y≈Y) = eliminate Y∈⟨y,xs⟩ |
| 97 | + (Z′ , Z′∈⟨S,K,xs⟩ , Z′y≈Z) = eliminate Z∈⟨y,xs⟩ |
| 98 | + |
| 99 | + SY′Z′y≈X : S ∙ Y′ ∙ Z′ ∙ y ≈ X |
| 100 | + SY′Z′y≈X = begin |
| 101 | + S ∙ Y′ ∙ Z′ ∙ y ≈⟨ isStarling Y′ Z′ y ⟩ |
| 102 | + Y′ ∙ y ∙ (Z′ ∙ y) ≈⟨ congʳ Y′y≈Y ⟩ |
| 103 | + Y ∙ (Z′ ∙ y) ≈⟨ congˡ Z′y≈Z ⟩ |
| 104 | + Y ∙ Z ≈⟨ YZ≈X ⟩ |
| 105 | + X ∎ |
| 106 | + in (S ∙ Y′ ∙ Z′ , [# 0 ] ⟨∙⟩ Y′∈⟨S,K,xs⟩ ⟨∙⟩ Z′∈⟨S,K,xs⟩ , SY′Z′y≈X) |
| 107 | + |
| 108 | +module _ (x y : Bird) where |
| 109 | + -- Example: y-eliminating the expression y should give I. |
| 110 | + _ : proj₁ (eliminate {y = y} {xs = x ∷ []} $ [# 0 ]) ≈ I |
| 111 | + _ = refl |
| 112 | + |
| 113 | + -- Example: y-eliminating the expression x should give Kx. |
| 114 | + _ : proj₁ (eliminate {y = y} {xs = x ∷ []} $ [# 1 ]) ≈ K ∙ x |
| 115 | + _ = refl |
| 116 | + |
| 117 | + -- Example: y-eliminating the expression xy should give x (Principle 3). |
| 118 | + _ : proj₁ (eliminate {y = y} {xs = x ∷ []} $ [# 1 ] ⟨∙⟩ [# 0 ]) ≈ x |
| 119 | + _ = refl |
| 120 | + |
| 121 | + -- Example: y-eliminating the expression yx should give SI(Kx). |
| 122 | + _ : proj₁ (eliminate {y = y} {xs = x ∷ []} $ [# 0 ] ⟨∙⟩ [# 1 ]) ≈ S ∙ I ∙ (K ∙ x) |
| 123 | + _ = refl |
| 124 | + |
| 125 | + -- Example: y-eliminating the expression yy should give SII. |
| 126 | + _ : proj₁ (eliminate {y = y} {xs = x ∷ []} $ [# 0 ] ⟨∙⟩ [# 0 ]) ≈ S ∙ I ∙ I |
| 127 | + _ = refl |
| 128 | + |
| 129 | +strengthen-SK : ∀ {n X y} {xs : Vec Bird n} → X ∈ ⟨ S ∷ K ∷ y ∷ xs ⟩ → Maybe (X ∈ ⟨ S ∷ K ∷ xs ⟩) |
| 130 | +strengthen-SK {y = y} {xs} X∈⟨S,K,y,xs⟩ = do |
| 131 | + let X∈⟨y,xs,S,K⟩ = ++-comm (S ∷ K ∷ []) (y ∷ xs) X∈⟨S,K,y,xs⟩ |
| 132 | + X∈⟨xs,S,K⟩ ← strengthen X∈⟨y,xs,S,K⟩ |
| 133 | + let X∈⟨S,K,xs⟩ = ++-comm xs (S ∷ K ∷ []) X∈⟨xs,S,K⟩ |
| 134 | + just X∈⟨S,K,xs⟩ |
| 135 | + where |
| 136 | + open import Data.Maybe.Categorical using (monad) |
| 137 | + open import Category.Monad using (RawMonad) |
| 138 | + open RawMonad (monad {b ⊔ ℓ}) |
| 139 | + |
| 140 | +-- TODO: formulate eliminate or eliminate-SK in terms of the other. |
| 141 | +eliminate-SK : ∀ {n X y} {xs : Vec Bird n} → X ∈ ⟨ S ∷ K ∷ y ∷ xs ⟩ → ∃[ X′ ] (X′ ∈ ⟨ S ∷ K ∷ xs ⟩ × X′ ∙ y ≈ X) |
| 142 | +eliminate-SK {X = X} {y} [ here X≈S ] = (K ∙ S , [# 1 ] ⟨∙⟩ [# 0 ] , trans (isKestrel S y) (sym X≈S)) |
| 143 | +eliminate-SK {X = X} {y} [ there (here X≈K) ] = (K ∙ K , [# 1 ] ⟨∙⟩ [# 1 ] , trans (isKestrel K y) (sym X≈K)) |
| 144 | +eliminate-SK {X = X} {y} [ there (there (here X≈y)) ] = (I , weaken-++ˡ I∈⟨S,K⟩ , trans (isIdentity y) (sym X≈y)) |
| 145 | +eliminate-SK {X = X} {y} [ there (there (there X∈xs)) ] = (K ∙ X , ([# 1 ] ⟨∙⟩ [ (++⁺ʳ (S ∷ K ∷ []) X∈xs) ]) , isKestrel X y) |
| 146 | +eliminate-SK {X = X} {y} (_⟨∙⟩_∣_ {Y} {Z} Y∈⟨S,K,y,xs⟩ [ there (there (here Z≈y)) ] YZ≈X) with strengthen-SK Y∈⟨S,K,y,xs⟩ |
| 147 | +... | just Y∈⟨S,K,xs⟩ = (Y , Y∈⟨S,K,xs⟩ , trans (congˡ (sym Z≈y)) YZ≈X) |
| 148 | +... | nothing = |
| 149 | + let (Y′ , Y′∈⟨S,K,xs⟩ , Y′y≈Y) = eliminate-SK Y∈⟨S,K,y,xs⟩ |
| 150 | + |
| 151 | + SY′Iy≈X : S ∙ Y′ ∙ I ∙ y ≈ X |
| 152 | + SY′Iy≈X = begin |
| 153 | + S ∙ Y′ ∙ I ∙ y ≈⟨ isStarling Y′ I y ⟩ |
| 154 | + Y′ ∙ y ∙ (I ∙ y) ≈⟨ congʳ $ Y′y≈Y ⟩ |
| 155 | + Y ∙ (I ∙ y) ≈⟨ congˡ $ isIdentity y ⟩ |
| 156 | + Y ∙ y ≈˘⟨ congˡ Z≈y ⟩ |
| 157 | + Y ∙ Z ≈⟨ YZ≈X ⟩ |
| 158 | + X ∎ |
| 159 | + in (S ∙ Y′ ∙ I , [# 0 ] ⟨∙⟩ Y′∈⟨S,K,xs⟩ ⟨∙⟩ weaken-++ˡ I∈⟨S,K⟩ , SY′Iy≈X) |
| 160 | +eliminate-SK {X = X} {y} (_⟨∙⟩_∣_ {Y} {Z} Y∈⟨S,K,y,xs⟩ Z∈⟨S,K,y,xs⟩ YZ≈X) = |
| 161 | + let (Y′ , Y′∈⟨S,K,xs⟩ , Y′y≈Y) = eliminate-SK Y∈⟨S,K,y,xs⟩ |
| 162 | + (Z′ , Z′∈⟨S,K,xs⟩ , Z′y≈Z) = eliminate-SK Z∈⟨S,K,y,xs⟩ |
| 163 | + |
| 164 | + SY′Z′y≈X : S ∙ Y′ ∙ Z′ ∙ y ≈ X |
| 165 | + SY′Z′y≈X = begin |
| 166 | + S ∙ Y′ ∙ Z′ ∙ y ≈⟨ isStarling Y′ Z′ y ⟩ |
| 167 | + Y′ ∙ y ∙ (Z′ ∙ y) ≈⟨ congʳ Y′y≈Y ⟩ |
| 168 | + Y ∙ (Z′ ∙ y) ≈⟨ congˡ Z′y≈Z ⟩ |
| 169 | + Y ∙ Z ≈⟨ YZ≈X ⟩ |
| 170 | + X ∎ |
| 171 | + in (S ∙ Y′ ∙ Z′ , [# 0 ] ⟨∙⟩ Y′∈⟨S,K,xs⟩ ⟨∙⟩ Z′∈⟨S,K,xs⟩ , SY′Z′y≈X) |
| 172 | + |
| 173 | +module _ (x y : Bird) where |
| 174 | + -- Example: y-eliminating the expression y should give I. |
| 175 | + _ : proj₁ (eliminate-SK {y = y} {xs = x ∷ []} $ [# 2 ]) ≈ I |
| 176 | + _ = refl |
| 177 | + |
| 178 | + -- Example: y-eliminating the expression x should give Kx. |
| 179 | + _ : proj₁ (eliminate-SK {y = y} {xs = x ∷ []} $ [# 3 ]) ≈ K ∙ x |
| 180 | + _ = refl |
| 181 | + |
| 182 | + -- Example: y-eliminating the expression xy should give x (Principle 3). |
| 183 | + _ : proj₁ (eliminate-SK {y = y} {xs = x ∷ []} $ [# 3 ] ⟨∙⟩ [# 2 ]) ≈ x |
| 184 | + _ = refl |
| 185 | + |
| 186 | + -- Example: y-eliminating the expression yx should give SI(Kx). |
| 187 | + _ : proj₁ (eliminate-SK {y = y} {xs = x ∷ []} $ [# 2 ] ⟨∙⟩ [# 3 ]) ≈ S ∙ I ∙ (K ∙ x) |
| 188 | + _ = refl |
| 189 | + |
| 190 | + -- Example: y-eliminating the expression yy should give SII. |
| 191 | + _ : proj₁ (eliminate-SK {y = y} {xs = x ∷ []} $ [# 2 ] ⟨∙⟩ [# 2 ]) ≈ S ∙ I ∙ I |
| 192 | + _ = refl |
| 193 | + |
| 194 | +infixl 6 _∙ⁿ_ |
| 195 | +_∙ⁿ_ : ∀ {n} → (A : Bird) (xs : Vec Bird n) → Bird |
| 196 | +A ∙ⁿ [] = A |
| 197 | +A ∙ⁿ (x ∷ xs) = A ∙ⁿ xs ∙ x |
| 198 | + |
| 199 | +eliminateAll′ : ∀ {n X} {xs : Vec Bird n} → X ∈ ⟨ S ∷ K ∷ xs ⟩ → ∃[ X′ ] (X′ ∈ ⟨ S ∷ K ∷ [] ⟩ × X′ ∙ⁿ xs ≈ X) |
| 200 | +eliminateAll′ {X = X} {[]} X∈⟨S,K⟩ = (X , X∈⟨S,K⟩ , refl) |
| 201 | +eliminateAll′ {X = X} {x ∷ xs} X∈⟨S,K,x,xs⟩ = |
| 202 | + let (X′ , X′∈⟨S,K,xs⟩ , X′x≈X) = eliminate-SK X∈⟨S,K,x,xs⟩ |
| 203 | + (X″ , X″∈⟨S,K⟩ , X″xs≈X′) = eliminateAll′ X′∈⟨S,K,xs⟩ |
| 204 | + |
| 205 | + X″∙ⁿxs∙x≈X : X″ ∙ⁿ xs ∙ x ≈ X |
| 206 | + X″∙ⁿxs∙x≈X = begin |
| 207 | + X″ ∙ⁿ xs ∙ x ≈⟨ congʳ X″xs≈X′ ⟩ |
| 208 | + X′ ∙ x ≈⟨ X′x≈X ⟩ |
| 209 | + X ∎ |
| 210 | + in (X″ , X″∈⟨S,K⟩ , X″∙ⁿxs∙x≈X) |
| 211 | + |
| 212 | +-- TOOD: can we do this in some way without depending on xs : Vec Bird n? |
| 213 | +eliminateAll : ∀ {n X} {xs : Vec Bird n} → X ∈ ⟨ xs ⟩ → ∃[ X′ ] (X′ ∈ ⟨ S ∷ K ∷ [] ⟩ × X′ ∙ⁿ xs ≈ X) |
| 214 | +eliminateAll X∈⟨xs⟩ = eliminateAll′ $ weaken-++ʳ (S ∷ K ∷ []) X∈⟨xs⟩ |
0 commit comments