| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- An example of how to use `Wrap` to help term inference. |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module README.Data.Wrap where |
| 10 |
|
| 11 |
open import Data.Wrap |
| 12 |
|
| 13 |
open import Algebra |
| 14 |
open import Data.Nat |
| 15 |
open import Data.Nat.Properties |
| 16 |
open import Data.Product.Base using (_,_; ∃; ∃₂; _×_) |
| 17 |
open import Level using (Level) |
| 18 |
|
| 19 |
private |
| 20 |
variable |
| 21 |
c ℓ : Level |
| 22 |
A : Set c |
| 23 |
m n : ℕ |
| 24 |
|
| 25 |
------------------------------------------------------------------------ |
| 26 |
-- `Wrap` for remembering instances |
| 27 |
------------------------------------------------------------------------ |
| 28 |
|
| 29 |
module Instances where |
| 30 |
|
| 31 |
-- `Monoid.Carrier` gets the carrier set from a monoid, and thus has |
| 32 |
-- type `Monoid c ℓ → Set c`. |
| 33 |
-- Using `Wrap`, we can convert `Monoid.Carrier` into an equivalent |
| 34 |
-- “wrapped” version: `MonoidEl`. |
| 35 |
MonoidEl : Monoid c ℓ → Set c |
| 36 |
MonoidEl = Wrap Monoid.Carrier |
| 37 |
|
| 38 |
-- We can turn any monoid into the equivalent monoid where the elements |
| 39 |
-- and equations have been wrapped. |
| 40 |
-- The translation mainly consists of wrapping and unwrapping everything |
| 41 |
-- via the `Wrap` constructor, `[_]`. |
| 42 |
-- Notice that the equality field is wrapping the binary relation |
| 43 |
-- `_≈_ : (x y : Carrier) → Set ℓ`, giving an example of how `Wrap` works |
| 44 |
-- for arbitrary n-ary relations. |
| 45 |
Wrap-monoid : Monoid c ℓ → Monoid c ℓ |
| 46 |
Wrap-monoid M = record |
| 47 |
{ Carrier = MonoidEl M |
| 48 |
; _≈_ = λ ([ x ]) ([ y ]) → Wrap _≈_ x y |
| 49 |
; _∙_ = λ ([ x ]) ([ y ]) → [ x ∙ y ] |
| 50 |
; ε = [ ε ] |
| 51 |
; isMonoid = record |
| 52 |
{ isSemigroup = record |
| 53 |
{ isMagma = record |
| 54 |
{ isEquivalence = record |
| 55 |
{ refl = [ refl ] |
| 56 |
; sym = λ ([ xy ]) → [ sym xy ] |
| 57 |
; trans = λ ([ xy ]) ([ yz ]) → [ trans xy yz ] |
| 58 |
} |
| 59 |
; ∙-cong = λ ([ xx ]) ([ yy ]) → [ ∙-cong xx yy ] |
| 60 |
} |
| 61 |
; assoc = λ ([ x ]) ([ y ]) ([ z ]) → [ assoc x y z ] |
| 62 |
} |
| 63 |
; identity = (λ ([ x ]) → [ identityˡ x ]) |
| 64 |
, (λ ([ x ]) → [ identityʳ x ]) |
| 65 |
} |
| 66 |
} |
| 67 |
where open Monoid M |
| 68 |
|
| 69 |
-- Usually, we would only open one monoid at a time. |
| 70 |
-- If we were to open two monoids `M` and `N` simultaneously, Agda would |
| 71 |
-- get confused whenever it came across, for example, `_∙_`, not knowing |
| 72 |
-- whether it came from `M` or `N`. |
| 73 |
-- This is true whether or not `M` and `N` can be disambiguated by some |
| 74 |
-- other means (such as by their `Carrier`s). |
| 75 |
|
| 76 |
-- However, with wrapped monoids, we are going to remember the monoid |
| 77 |
-- while checking any monoid expressions, so we can afford to have just |
| 78 |
-- one, polymorphic, version of `_∙_` visible globally. |
| 79 |
open module Wrap-monoid {c ℓ} {M : Monoid c ℓ} = Monoid (Wrap-monoid M) |
| 80 |
|
| 81 |
-- Now we can test out this construct on some existing monoids. |
| 82 |
|
| 83 |
open import Data.Nat.Properties |
| 84 |
|
| 85 |
-- Notice that, while the following two definitions appear to be defined |
| 86 |
-- by the same expression, their types are genuinely different. |
| 87 |
-- Whereas `Carrier +-0-monoid = ℕ = Carrier *-1-monoid`, `MonoidEl M` |
| 88 |
-- does not compute, and thus |
| 89 |
-- `MonoidEl +-0-monoid ≠ MonoidEl *-1-monoid` definitionally. |
| 90 |
-- This lets us use the respective monoids when checking the respective |
| 91 |
-- definitions. |
| 92 |
|
| 93 |
test-+ : MonoidEl +-0-monoid |
| 94 |
test-+ = ([ 3 ] ∙ ε) ∙ [ 2 ] |
| 95 |
|
| 96 |
test-* : MonoidEl *-1-monoid |
| 97 |
test-* = ([ 3 ] ∙ ε) ∙ [ 2 ] |
| 98 |
|
| 99 |
-- The reader is invited to normalise these two definitions |
| 100 |
-- (`C-c C-n`, then type in the name). |
| 101 |
-- `test-+` is interpreted using (ℕ, +, 0), and thus computes to `[ 5 ]`. |
| 102 |
-- Meanwhile, `test-*` is interpreted using (ℕ, *, 1), and thus computes |
| 103 |
-- to `[ 6 ]`. |
| 104 |
|
| 105 |
------------------------------------------------------------------------ |
| 106 |
-- `Wrap` for dealing with functions spoiling unification |
| 107 |
------------------------------------------------------------------------ |
| 108 |
|
| 109 |
module Unification where |
| 110 |
|
| 111 |
open import Relation.Binary.PropositionalEquality |
| 112 |
|
| 113 |
module Naïve where |
| 114 |
|
| 115 |
-- We want to work with factorisations of natural numbers in a |
| 116 |
-- “proof-relevant” style. We could draw out `Factor m n o` as |
| 117 |
-- m |
| 118 |
-- /*\ |
| 119 |
-- n o. |
| 120 |
|
| 121 |
Factor : (m n o : ℕ) → Set |
| 122 |
Factor m n o = m ≡ n * o |
| 123 |
|
| 124 |
-- We can prove a basic lemma about `Factor`: the following tree rotation |
| 125 |
-- can be done, due to associativity of `_*_`. |
| 126 |
-- m m |
| 127 |
-- /*\ /*\ |
| 128 |
-- no p ----> n op |
| 129 |
-- /*\ /*\ |
| 130 |
-- n o o p |
| 131 |
|
| 132 |
assoc-→ : ∀ {m n o p} → |
| 133 |
(∃ λ no → Factor m no p × Factor no n o) → |
| 134 |
(∃ λ op → Factor m n op × Factor op o p) |
| 135 |
assoc-→ {m} {n} {o} {p} (._ , refl , refl) = _ , *-assoc n o p , refl |
| 136 |
|
| 137 |
-- We must give at least some arguments to `*-assoc`, as Agda is unable to |
| 138 |
-- unify `? * ? * ?` with `n * o * p`, as `_*_` is a function and not |
| 139 |
-- necessarily injective (and indeed not injective when one of its |
| 140 |
-- arguments is 0). |
| 141 |
|
| 142 |
-- We now want to use this lemma in a more complex proof: |
| 143 |
-- m m |
| 144 |
-- /*\ /*\ |
| 145 |
-- nop q n opq |
| 146 |
-- /*\ ----> /*\ |
| 147 |
-- no p o pq |
| 148 |
-- /*\ /*\ |
| 149 |
-- n o p q |
| 150 |
|
| 151 |
test : ∀ {m n o p q} → |
| 152 |
(∃₂ λ no nop → Factor m nop q × Factor nop no p × Factor no n o) → |
| 153 |
(∃₂ λ pq opq → Factor m n opq × Factor opq o pq × Factor pq p q) |
| 154 |
test {n = n} (no , nop , fm , fnop , fno) = |
| 155 |
let _ , fm , fpq = assoc-→ {n = no} (_ , fm , fnop) in |
| 156 |
let _ , fm , fopq = assoc-→ {n = n} (_ , fm , fno) in |
| 157 |
_ , _ , fm , fopq , fpq |
| 158 |
|
| 159 |
-- This works okay, but where we have written `{n = no}` and similar, we |
| 160 |
-- are being forced to deal with details we don't really care about. Agda |
| 161 |
-- should be able to fill in the vertices given part of a tree, but can't |
| 162 |
-- due to similar reasons as before: `Factor ? ? ?` doesn't unify against |
| 163 |
-- `Factor m no p`, because both instances of `Factor` compute and we're |
| 164 |
-- left trying to unify `? * ?` against `no * p`. |
| 165 |
|
| 166 |
module Wrapped where |
| 167 |
|
| 168 |
-- We can use `Wrap` to stop the computation of `Factor`. |
| 169 |
|
| 170 |
Factor : (m n o : ℕ) → Set |
| 171 |
Factor = Wrap λ m n o → m ≡ n * o |
| 172 |
|
| 173 |
-- Because `assoc-→` needs access to the implementation of `Factor`, the |
| 174 |
-- proof is exactly as before except for using `[_]` to wrap and unwrap. |
| 175 |
|
| 176 |
assoc-→ : ∀ {m n o p} → |
| 177 |
(∃ λ no → Factor m no p × Factor no n o) → |
| 178 |
(∃ λ op → Factor m n op × Factor op o p) |
| 179 |
assoc-→ {m} {n} {o} {p} (._ , [ refl ] , [ refl ]) = |
| 180 |
_ , [ *-assoc n o p ] , [ refl ] |
| 181 |
|
| 182 |
-- The difference is that now we have our basic lemma, the complex proof |
| 183 |
-- can work purely in terms of `Factor` trees. In particular, |
| 184 |
-- `Factor ? ? ?` now does unify with `Factor m no p`, so we don't have to |
| 185 |
-- give `no` explicitly again. |
| 186 |
|
| 187 |
test : ∀ {m n o p q} → |
| 188 |
(∃₂ λ no nop → Factor m nop q × Factor nop no p × Factor no n o) → |
| 189 |
(∃₂ λ pq opq → Factor m n opq × Factor opq o pq × Factor pq p q) |
| 190 |
test (_ , _ , fm , fnop , fno) = |
| 191 |
let _ , fm , fpq = assoc-→ (_ , fm , fnop) in |
| 192 |
let _ , fm , fopq = assoc-→ (_ , fm , fno) in |
| 193 |
_ , _ , fm , fopq , fpq |