| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Concepts from rewriting theory |
| 5 |
-- Definitions are based on "Term Rewriting Systems" by J.W. Klop |
| 6 |
------------------------------------------------------------------------ |
| 7 |
|
| 8 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 9 |
|
| 10 |
module Relation.Binary.Rewriting where |
| 11 |
|
| 12 |
open import Agda.Builtin.Equality using (_≡_ ; refl) |
| 13 |
open import Data.Product.Base using (_×_ ; ∃ ; -,_; _,_ ; proj₁ ; proj₂) |
| 14 |
open import Function.Base using (flip) |
| 15 |
open import Induction.WellFounded using (WellFounded; Acc; acc) |
| 16 |
open import Relation.Binary.Core using (REL; Rel) |
| 17 |
open import Relation.Binary.Construct.Closure.Equivalence using (EqClosure) |
| 18 |
open import Relation.Binary.Construct.Closure.Equivalence.Properties |
| 19 |
using (a—↠b&a—↠c⇒b↔c) |
| 20 |
open import Relation.Binary.Construct.Closure.ReflexiveTransitive |
| 21 |
using (Star; _◅_; ε; _◅◅_) |
| 22 |
open import Relation.Binary.Construct.Closure.Symmetric using (fwd; bwd) |
| 23 |
open import Relation.Binary.Construct.Closure.Transitive |
| 24 |
using (Plus; [_]; _∼⁺⟨_⟩_) |
| 25 |
open import Relation.Nullary.Negation.Core using (¬_; contradiction) |
| 26 |
|
| 27 |
-- The following definitions are taken from Klop [5] |
| 28 |
module _ {a ℓ} {A : Set a} (_⟶_ : Rel A ℓ) where |
| 29 |
|
| 30 |
private |
| 31 |
_⟵_ = flip _⟶_ |
| 32 |
_—↠_ = Star _⟶_ |
| 33 |
_↔_ = EqClosure _⟶_ |
| 34 |
|
| 35 |
IsNormalForm : A → Set _ |
| 36 |
IsNormalForm a = ¬ ∃ λ b → (a ⟶ b) |
| 37 |
|
| 38 |
HasNormalForm : A → Set _ |
| 39 |
HasNormalForm b = ∃ λ a → IsNormalForm a × (b —↠ a) |
| 40 |
|
| 41 |
NormalForm : Set _ |
| 42 |
NormalForm = ∀ {a b} → IsNormalForm a → b ↔ a → b —↠ a |
| 43 |
|
| 44 |
WeaklyNormalizing : Set _ |
| 45 |
WeaklyNormalizing = ∀ a → HasNormalForm a |
| 46 |
|
| 47 |
StronglyNormalizing : Set _ |
| 48 |
StronglyNormalizing = WellFounded _⟵_ |
| 49 |
|
| 50 |
UniqueNormalForm : Set _ |
| 51 |
UniqueNormalForm = ∀ {a b} → IsNormalForm a → IsNormalForm b → a ↔ b → a ≡ b |
| 52 |
|
| 53 |
Confluent : Set _ |
| 54 |
Confluent = ∀ {A B C} → A —↠ B → A —↠ C → ∃ λ D → (B —↠ D) × (C —↠ D) |
| 55 |
|
| 56 |
WeaklyConfluent : Set _ |
| 57 |
WeaklyConfluent = ∀ {A B C} → A ⟶ B → A ⟶ C → ∃ λ D → (B —↠ D) × (C —↠ D) |
| 58 |
|
| 59 |
|
| 60 |
Deterministic : ∀ {a b ℓ₁ ℓ₂} → {A : Set a} → {B : Set b} → Rel B ℓ₁ → REL A B ℓ₂ → Set _ |
| 61 |
Deterministic _≈_ _—→_ = ∀ {x y z} → x —→ y → x —→ z → y ≈ z |
| 62 |
|
| 63 |
module _ {a ℓ} {A : Set a} {_⟶_ : Rel A ℓ} where |
| 64 |
|
| 65 |
private |
| 66 |
_—↠_ = Star _⟶_ |
| 67 |
_↔_ = EqClosure _⟶_ |
| 68 |
_⟶₊_ = Plus _⟶_ |
| 69 |
|
| 70 |
det⇒conf : Deterministic _≡_ _⟶_ → Confluent _⟶_ |
| 71 |
det⇒conf det ε rs₂ = -, rs₂ , ε |
| 72 |
det⇒conf det rs₁ ε = -, ε , rs₁ |
| 73 |
det⇒conf det (r₁ ◅ rs₁) (r₂ ◅ rs₂) |
| 74 |
rewrite det r₁ r₂ = det⇒conf det rs₁ rs₂ |
| 75 |
|
| 76 |
conf⇒wcr : Confluent _⟶_ → WeaklyConfluent _⟶_ |
| 77 |
conf⇒wcr c fst snd = c (fst ◅ ε) (snd ◅ ε) |
| 78 |
|
| 79 |
conf⇒nf : Confluent _⟶_ → NormalForm _⟶_ |
| 80 |
conf⇒nf c aIsNF ε = ε |
| 81 |
conf⇒nf c aIsNF (fwd x ◅ rest) = x ◅ conf⇒nf c aIsNF rest |
| 82 |
conf⇒nf c aIsNF (bwd y ◅ rest) with c (y ◅ ε) (conf⇒nf c aIsNF rest) |
| 83 |
... | _ , _ , x ◅ _ = contradiction (_ , x) aIsNF |
| 84 |
... | _ , left , ε = left |
| 85 |
|
| 86 |
conf⇒unf : Confluent _⟶_ → UniqueNormalForm _⟶_ |
| 87 |
conf⇒unf _ _ _ ε = refl |
| 88 |
conf⇒unf _ aIsNF _ (fwd x ◅ _) = contradiction (_ , x) aIsNF |
| 89 |
conf⇒unf c aIsNF bIsNF (bwd y ◅ r) with c (y ◅ ε) (conf⇒nf c bIsNF r) |
| 90 |
... | _ , ε , x ◅ _ = contradiction (_ , x) bIsNF |
| 91 |
... | _ , x ◅ _ , _ = contradiction (_ , x) aIsNF |
| 92 |
... | _ , ε , ε = refl |
| 93 |
|
| 94 |
un&wn⇒cr : UniqueNormalForm _⟶_ → WeaklyNormalizing _⟶_ → Confluent _⟶_ |
| 95 |
un&wn⇒cr un wn {a} {b} {c} aToB aToC with wn b | wn c |
| 96 |
... | (d , (d-nf , bToD)) | (e , (e-nf , cToE)) |
| 97 |
with un d-nf e-nf (a—↠b&a—↠c⇒b↔c (aToB ◅◅ bToD) (aToC ◅◅ cToE)) |
| 98 |
... | refl = d , bToD , cToE |
| 99 |
|
| 100 |
-- Newman's lemma |
| 101 |
sn&wcr⇒cr : StronglyNormalizing _⟶₊_ → WeaklyConfluent _⟶_ → Confluent _⟶_ |
| 102 |
sn&wcr⇒cr sn wcr = helper (sn _) where |
| 103 |
|
| 104 |
starToPlus : ∀ {a b c} → (a ⟶ b) → b —↠ c → a ⟶₊ c |
| 105 |
starToPlus aToB ε = [ aToB ] |
| 106 |
starToPlus {a} aToB (e ◅ bToC) = a ∼⁺⟨ [ aToB ] ⟩ (starToPlus e bToC) |
| 107 |
|
| 108 |
helper : ∀ {a b c} → (acc : Acc (flip _⟶₊_) a) → |
| 109 |
a —↠ b → a —↠ c → ∃ λ d → (b —↠ d) × (c —↠ d) |
| 110 |
helper _ ε snd = -, snd , ε |
| 111 |
helper _ fst ε = -, ε , fst |
| 112 |
helper (acc g) (toJ ◅ fst) (toK ◅ snd) = result where |
| 113 |
|
| 114 |
wcrProof = wcr toJ toK |
| 115 |
innerPoint = proj₁ wcrProof |
| 116 |
jToInner = proj₁ (proj₂ wcrProof) |
| 117 |
kToInner = proj₂ (proj₂ wcrProof) |
| 118 |
|
| 119 |
lhs = helper (g [ toJ ]) fst jToInner |
| 120 |
rhs = helper (g [ toK ]) snd kToInner |
| 121 |
|
| 122 |
fromAB = proj₁ (proj₂ lhs) |
| 123 |
fromInnerB = proj₂ (proj₂ lhs) |
| 124 |
|
| 125 |
fromAC = proj₁ (proj₂ rhs) |
| 126 |
fromInnerC = proj₂ (proj₂ rhs) |
| 127 |
|
| 128 |
aToInner : _ ⟶₊ innerPoint |
| 129 |
aToInner = starToPlus toJ jToInner |
| 130 |
|
| 131 |
finalRecursion = helper (g aToInner) fromInnerB fromInnerC |
| 132 |
|
| 133 |
bMidToDest = proj₁ (proj₂ finalRecursion) |
| 134 |
cMidToDest = proj₂ (proj₂ finalRecursion) |
| 135 |
|
| 136 |
result : ∃ λ d → (_ —↠ d) × (_ —↠ d) |
| 137 |
result = _ , fromAB ◅◅ bMidToDest , fromAC ◅◅ cMidToDest |