| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Boolean algebra expressions |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
open import Algebra.Lattice using (BooleanAlgebra; isBooleanAlgebraʳ; |
| 10 |
isDistributiveLatticeʳʲᵐ) |
| 11 |
|
| 12 |
module Algebra.Lattice.Properties.BooleanAlgebra.Expression |
| 13 |
{b} (B : BooleanAlgebra b b) |
| 14 |
where |
| 15 |
|
| 16 |
open BooleanAlgebra B |
| 17 |
|
| 18 |
open import Data.Fin.Base using (Fin) |
| 19 |
open import Data.Nat.Base using (ℕ) |
| 20 |
open import Data.Product.Base using (_,_; proj₁; proj₂) |
| 21 |
open import Data.Vec.Base as Vec using (Vec) |
| 22 |
import Data.Vec.Effectful as Vec |
| 23 |
import Function.Identity.Effectful as Identity |
| 24 |
open import Data.Vec.Properties using (lookup-map) |
| 25 |
open import Data.Vec.Relation.Binary.Pointwise.Extensional as PW |
| 26 |
using (Pointwise; ext) |
| 27 |
open import Effect.Applicative as Applicative |
| 28 |
open import Function.Base using (_∘_; _$_; flip) |
| 29 |
open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≗_) |
| 30 |
open import Relation.Binary.PropositionalEquality.Properties |
| 31 |
using (module ≡-Reasoning) |
| 32 |
import Relation.Binary.Reflection as Reflection |
| 33 |
|
| 34 |
-- Expressions made up of variables and the operations of a boolean |
| 35 |
-- algebra. |
| 36 |
|
| 37 |
infixr 7 _and_ |
| 38 |
infixr 6 _or_ |
| 39 |
|
| 40 |
data Expr n : Set b where |
| 41 |
var : (x : Fin n) → Expr n |
| 42 |
_or_ _and_ : (e₁ e₂ : Expr n) → Expr n |
| 43 |
not : (e : Expr n) → Expr n |
| 44 |
top bot : Expr n |
| 45 |
|
| 46 |
-- The semantics of an expression, parametrised by an applicative |
| 47 |
-- functor. |
| 48 |
|
| 49 |
module Semantics |
| 50 |
{F : Set b → Set b} |
| 51 |
(A : RawApplicative F) |
| 52 |
where |
| 53 |
|
| 54 |
open RawApplicative A |
| 55 |
|
| 56 |
⟦_⟧ : ∀ {n} → Expr n → Vec (F Carrier) n → F Carrier |
| 57 |
⟦ var x ⟧ ρ = Vec.lookup ρ x |
| 58 |
⟦ e₁ or e₂ ⟧ ρ = _∨_ <$> ⟦ e₁ ⟧ ρ ⊛ ⟦ e₂ ⟧ ρ |
| 59 |
⟦ e₁ and e₂ ⟧ ρ = _∧_ <$> ⟦ e₁ ⟧ ρ ⊛ ⟦ e₂ ⟧ ρ |
| 60 |
⟦ not e ⟧ ρ = ¬_ <$> ⟦ e ⟧ ρ |
| 61 |
⟦ top ⟧ ρ = pure ⊤ |
| 62 |
⟦ bot ⟧ ρ = pure ⊥ |
| 63 |
|
| 64 |
-- flip Semantics.⟦_⟧ e is natural. |
| 65 |
|
| 66 |
module Naturality |
| 67 |
{F₁ F₂ : Set b → Set b} |
| 68 |
{A₁ : RawApplicative F₁} |
| 69 |
{A₂ : RawApplicative F₂} |
| 70 |
(f : Applicative.Morphism A₁ A₂) |
| 71 |
where |
| 72 |
|
| 73 |
open ≡-Reasoning |
| 74 |
open Applicative.Morphism f |
| 75 |
open Semantics A₁ renaming (⟦_⟧ to ⟦_⟧₁) |
| 76 |
open Semantics A₂ renaming (⟦_⟧ to ⟦_⟧₂) |
| 77 |
open RawApplicative A₁ renaming (pure to pure₁; _<$>_ to _<$>₁_; _⊛_ to _⊛₁_) |
| 78 |
open RawApplicative A₂ renaming (pure to pure₂; _<$>_ to _<$>₂_; _⊛_ to _⊛₂_) |
| 79 |
|
| 80 |
natural : ∀ {n} (e : Expr n) → op ∘ ⟦ e ⟧₁ ≗ ⟦ e ⟧₂ ∘ Vec.map op |
| 81 |
natural (var x) ρ = begin |
| 82 |
op (Vec.lookup ρ x) ≡⟨ ≡.sym $ lookup-map x op ρ ⟩ |
| 83 |
Vec.lookup (Vec.map op ρ) x ∎ |
| 84 |
natural (e₁ or e₂) ρ = begin |
| 85 |
op (_∨_ <$>₁ ⟦ e₁ ⟧₁ ρ ⊛₁ ⟦ e₂ ⟧₁ ρ) ≡⟨ op-⊛ _ _ ⟩ |
| 86 |
op (_∨_ <$>₁ ⟦ e₁ ⟧₁ ρ) ⊛₂ op (⟦ e₂ ⟧₁ ρ) ≡⟨ ≡.cong₂ _⊛₂_ (op-<$> _ _) ≡.refl ⟩ |
| 87 |
_∨_ <$>₂ op (⟦ e₁ ⟧₁ ρ) ⊛₂ op (⟦ e₂ ⟧₁ ρ) ≡⟨ ≡.cong₂ (λ e₁ e₂ → _∨_ <$>₂ e₁ ⊛₂ e₂) (natural e₁ ρ) (natural e₂ ρ) ⟩ |
| 88 |
_∨_ <$>₂ ⟦ e₁ ⟧₂ (Vec.map op ρ) ⊛₂ ⟦ e₂ ⟧₂ (Vec.map op ρ) ∎ |
| 89 |
natural (e₁ and e₂) ρ = begin |
| 90 |
op (_∧_ <$>₁ ⟦ e₁ ⟧₁ ρ ⊛₁ ⟦ e₂ ⟧₁ ρ) ≡⟨ op-⊛ _ _ ⟩ |
| 91 |
op (_∧_ <$>₁ ⟦ e₁ ⟧₁ ρ) ⊛₂ op (⟦ e₂ ⟧₁ ρ) ≡⟨ ≡.cong₂ _⊛₂_ (op-<$> _ _) ≡.refl ⟩ |
| 92 |
_∧_ <$>₂ op (⟦ e₁ ⟧₁ ρ) ⊛₂ op (⟦ e₂ ⟧₁ ρ) ≡⟨ ≡.cong₂ (λ e₁ e₂ → _∧_ <$>₂ e₁ ⊛₂ e₂) (natural e₁ ρ) (natural e₂ ρ) ⟩ |
| 93 |
_∧_ <$>₂ ⟦ e₁ ⟧₂ (Vec.map op ρ) ⊛₂ ⟦ e₂ ⟧₂ (Vec.map op ρ) ∎ |
| 94 |
natural (not e) ρ = begin |
| 95 |
op (¬_ <$>₁ ⟦ e ⟧₁ ρ) ≡⟨ op-<$> _ _ ⟩ |
| 96 |
¬_ <$>₂ op (⟦ e ⟧₁ ρ) ≡⟨ ≡.cong (¬_ <$>₂_) (natural e ρ) ⟩ |
| 97 |
¬_ <$>₂ ⟦ e ⟧₂ (Vec.map op ρ) ∎ |
| 98 |
natural top ρ = begin |
| 99 |
op (pure₁ ⊤) ≡⟨ op-pure _ ⟩ |
| 100 |
pure₂ ⊤ ∎ |
| 101 |
natural bot ρ = begin |
| 102 |
op (pure₁ ⊥) ≡⟨ op-pure _ ⟩ |
| 103 |
pure₂ ⊥ ∎ |
| 104 |
|
| 105 |
-- An example of how naturality can be used: Any boolean algebra can |
| 106 |
-- be lifted, in a pointwise manner, to vectors of carrier elements. |
| 107 |
|
| 108 |
lift : ℕ → BooleanAlgebra b b |
| 109 |
lift n = record |
| 110 |
{ Carrier = Vec Carrier n |
| 111 |
; _≈_ = Pointwise _≈_ |
| 112 |
; _∨_ = zipWith _∨_ |
| 113 |
; _∧_ = zipWith _∧_ |
| 114 |
; ¬_ = map ¬_ |
| 115 |
; ⊤ = pure ⊤ |
| 116 |
; ⊥ = pure ⊥ |
| 117 |
; isBooleanAlgebra = isBooleanAlgebraʳ $ record |
| 118 |
{ isDistributiveLattice = isDistributiveLatticeʳʲᵐ $ record |
| 119 |
{ isLattice = record |
| 120 |
{ isEquivalence = PW.isEquivalence isEquivalence |
| 121 |
; ∨-comm = λ xs ys → ext λ i → |
| 122 |
solve i 2 (λ x y → x or y , y or x) |
| 123 |
(∨-comm _ _) xs ys |
| 124 |
; ∨-assoc = λ xs ys _ → ext λ i → |
| 125 |
solve i 3 |
| 126 |
(λ x y z → (x or y) or z , x or (y or z)) |
| 127 |
(∨-assoc _ _ _) xs ys _ |
| 128 |
; ∨-cong = λ {xs} {ys} {us} {vs} xs≈us ys≈vs → ext λ i → |
| 129 |
solve₁ i 4 (λ x y u v → x or y , u or v) |
| 130 |
xs us ys vs |
| 131 |
(∨-cong (Pointwise.app xs≈us i) |
| 132 |
(Pointwise.app ys≈vs i)) |
| 133 |
; ∧-comm = λ xs ys → ext λ i → |
| 134 |
solve i 2 (λ x y → x and y , y and x) |
| 135 |
(∧-comm _ _) xs ys |
| 136 |
; ∧-assoc = λ xs ys _ → ext λ i → |
| 137 |
solve i 3 |
| 138 |
(λ x y z → (x and y) and z , |
| 139 |
x and (y and z)) |
| 140 |
(∧-assoc _ _ _) xs ys _ |
| 141 |
; ∧-cong = λ {xs} {ys} {us} {vs} xs≈ys us≈vs → ext λ i → |
| 142 |
solve₁ i 4 (λ x y u v → x and y , u and v) |
| 143 |
xs us ys vs |
| 144 |
(∧-cong (Pointwise.app xs≈ys i) |
| 145 |
(Pointwise.app us≈vs i)) |
| 146 |
; absorptive = |
| 147 |
(λ xs ys → ext λ i → |
| 148 |
solve i 2 (λ x y → x or (x and y) , x) (∨-absorbs-∧ _ _) xs ys) , |
| 149 |
(λ xs ys → ext λ i → |
| 150 |
solve i 2 (λ x y → x and (x or y) , x) (∧-absorbs-∨ _ _) xs ys) |
| 151 |
} |
| 152 |
; ∨-distribʳ-∧ = λ xs ys zs → ext λ i → |
| 153 |
solve i 3 |
| 154 |
(λ x y z → (y and z) or x , |
| 155 |
(y or x) and (z or x)) |
| 156 |
(∨-distribʳ-∧ _ _ _) xs ys zs |
| 157 |
} |
| 158 |
; ∨-complementʳ = λ xs → ext λ i → |
| 159 |
solve i 1 (λ x → x or (not x) , top) |
| 160 |
(∨-complementʳ _) xs |
| 161 |
; ∧-complementʳ = λ xs → ext λ i → |
| 162 |
solve i 1 (λ x → x and (not x) , bot) |
| 163 |
(∧-complementʳ _) xs |
| 164 |
; ¬-cong = λ {xs} {ys} xs≈ys → ext λ i → |
| 165 |
solve₁ i 2 (λ x y → not x , not y) xs ys |
| 166 |
(¬-cong (Pointwise.app xs≈ys i)) |
| 167 |
} |
| 168 |
} |
| 169 |
where |
| 170 |
open RawApplicative Vec.applicative |
| 171 |
using (pure; zipWith) renaming (_<$>_ to map) |
| 172 |
|
| 173 |
⟦_⟧Id : ∀ {n} → Expr n → Vec Carrier n → Carrier |
| 174 |
⟦_⟧Id = Semantics.⟦_⟧ Identity.applicative |
| 175 |
|
| 176 |
⟦_⟧Vec : ∀ {m n} → Expr n → Vec (Vec Carrier m) n → Vec Carrier m |
| 177 |
⟦_⟧Vec = Semantics.⟦_⟧ Vec.applicative |
| 178 |
|
| 179 |
open module R {n} (i : Fin n) = |
| 180 |
Reflection setoid var |
| 181 |
(λ e ρ → Vec.lookup (⟦ e ⟧Vec ρ) i) |
| 182 |
(λ e ρ → ⟦ e ⟧Id (Vec.map (flip Vec.lookup i) ρ)) |
| 183 |
(λ e ρ → sym $ reflexive $ |
| 184 |
Naturality.natural (Vec.lookup-morphism i) e ρ) |