Skip to content

Commit f1b33e8

Browse files
committed
use copatterns to define Sheaves; fully eta-expand
1 parent 09f4327 commit f1b33e8

File tree

13 files changed

+135
-114
lines changed

13 files changed

+135
-114
lines changed

src/1Lab/Reflection/Copattern.agda

Lines changed: 60 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,40 @@ make-copattern declare? def-name tm tp = do
2929

3030
-- Agda will insert implicits when defining copatterns even
3131
-- with 'withExpandLast true', so we need to do implicit instantiation
32-
-- by hand. First, we strip off all leading implicits from the field type.
33-
let (implicit-tele , tp) = pi-impl-view field-tp
34-
let nimplicits = length implicit-tele
35-
let clause-tele = tele ++ implicit-tele
36-
37-
-- Construct the pattern portion of the clause, making sure to bind
38-
-- all implicit variables. Note that copattern projections are always visible.
39-
let pat =
40-
tel→pats nimplicits tele ++
41-
arg (set-visibility visible field-info) (proj field-name) ∷
42-
tel→pats 0 implicit-tele
43-
44-
-- Construct the body of the clause, making sure to apply all arguments
45-
-- bound outside the copattern match, and instantiate all implicit arguments.
46-
-- We also need to apply all of the implicit arguments to 'tm'.
32+
-- by hand. There are also cases where it's better to fully
33+
-- eta-expand than not (e.g. the 'helper' we're expanding has a
34+
-- field defined by lazy matching, which does not reduce unless
35+
-- applied, and would cause duplication of the big input term). So
36+
-- we fully eta-expand clauses here.
37+
-- First, we strip off all leading quantifiers from the field
38+
-- type.
39+
let
40+
(field-tele , tp) = pi-view field-tp
41+
nargs = length field-tele
42+
clause-tele = tele ++ field-tele
43+
44+
-- Construct the pattern portion of the clause, making sure to
45+
-- bind all variables. Note that copattern projections are always
46+
-- visible.
47+
let
48+
pat = tel→pats nargs tele ++
49+
arg (set-visibility visible field-info) (proj field-name) ∷
50+
tel→pats 0 field-tele
51+
52+
-- Construct the body of the clause, making sure to apply all
53+
-- arguments bound outside the copattern match, and apply the
54+
-- eta-expanded arguments. We also need to apply all of the
55+
-- implicit arguments to 'tm'.
4756
body
4857
in-context (reverse clause-tele) $
49-
reduce (def field-name (argN (raise nimplicits inst-tm) ∷ tel→args 0 implicit-tele))
58+
reduce (def field-name (raise nargs inst-tm v∷ tel→args 0 field-tele))
5059

5160
-- Construct the final clause.
5261
pure $ clause clause-tele pat body
5362

5463
-- Define a copattern binding, and predeclare its type if required.
5564
case declare? of λ where
56-
true declare (argN def-name) tp
65+
true declare (argN def-name) tp <|> pure tt
5766
false pure tt
5867

5968
-- Construct the final copattern.
@@ -82,6 +91,17 @@ repack-record tm tp = do
8291
-- Builld a pointwise repacking.
8392
pure (tel→lam tele (con ctor args))
8493

94+
-- Helper for the 'define' macros; Unifies the given goal with the type
95+
-- of the given function, if it has been defined. If the function has
96+
-- not been defined, and the first argument is 'false', then an error is
97+
-- raised.
98+
type-for : String Bool Name Term TC ⊤
99+
type-for tac decl? fun goal with decl?
100+
... | true = (unify goal =<< get-type fun) <|> pure tt
101+
... | false = (unify goal =<< get-type fun) <|> typeError
102+
[ "define-" , strErr tac , ": the function " , nameErr fun , " should already have been declared."
103+
]
104+
85105
--------------------------------------------------------------------------------
86106
-- Usage
87107

@@ -94,8 +114,10 @@ If you wish to give the binding a type annotation, you can also use
94114
> copat : Your-type
95115
> unquoteDecl copat = declare-copattern copat thing-to-be-expanded
96116
97-
All features of non-recursive records are supported, including instance
98-
fields and fields with implicit arguments.
117+
Note that, in this case, the thing-to-be-expanded must have exactly the
118+
same type as the binding `copat`. All features of non-recursive records
119+
are supported, including instance fields and fields with implicit
120+
arguments.
99121
100122
These macros also allow you to lift functions 'A → some-record-type'
101123
into copattern definitions. Note that Agda will generate meta for
@@ -109,10 +131,13 @@ declare-copattern {A = A} nm x = do
109131
`A quoteTC A
110132
make-copattern true nm `x `A
111133

112-
define-copattern : {ℓ} {A : Type ℓ} Name A TC ⊤
113-
define-copattern {A = A} nm x = do
114-
`x quoteTC x
134+
define-copattern
135+
: {ℓ} (nm : Name)
136+
{@(tactic (type-for "copattern" true nm)) A : Type ℓ}
137+
A TC ⊤
138+
define-copattern nm {A = A} x = do
115139
`A quoteTC A
140+
`x define-abbrev nm "value" `A =<< quoteTC x
116141
make-copattern false nm `x `A
117142

118143
{-
@@ -121,32 +146,19 @@ they cannot be quoted into any `Type ℓ`. With this in mind,
121146
we also provide a pair of macros that work over `Typeω` instead.
122147
-}
123148

124-
-- Helper for the 'define' macros; Unifies the given goal with the type
125-
-- of the given function, if it has been defined. If the function has
126-
-- not been defined, and the first argument is 'false', then an error is
127-
-- raised.
128-
type-for : Bool Name Term TC ⊤
129-
type-for decl? fun goal with decl?
130-
... | true = (unify goal =<< get-type fun) <|> pure tt
131-
... | false = (unify goal =<< get-type fun) <|> typeError
132-
[ "define-copattern-levels: the function " , nameErr fun , " should already have been declared."
133-
]
134-
135-
declare-copattern-levels
136-
: (nm : Name) {@(tactic (type-for true nm)) U : Typeω}
137-
U TC ⊤
138-
declare-copattern-levels nm A = do
149+
declare-copatternω : {U : Typeω} Name U TC ⊤
150+
declare-copatternω nm A = do
139151
`A quoteωTC A
140152
-- Cannot quote things in type Typeω, but we can infer their type.
141153
`U infer-type `A
142154
make-copattern true nm `A `U
143155

144-
define-copattern-levels
145-
: (nm : Name) {@(tactic (type-for false nm)) U : Typeω}
156+
define-copatternω
157+
: (nm : Name) {@(tactic (type-for "copatternω" false nm)) U : Typeω}
146158
U TC ⊤
147-
define-copattern-levels nm A = do
148-
`A quoteωTC A
159+
define-copatternω nm A = do
149160
`U get-type nm
161+
`A define-abbrev nm "value" `U =<< quoteωTC A
150162
make-copattern false nm `A `U
151163

152164
{-
@@ -206,7 +218,11 @@ private module Test where
206218
zero-unused-param = record { actual = 0 }
207219

208220
one-unused-param : {n} Unused n
209-
unquoteDef one-unused-param = define-copattern one-unused-param zero-unused-param
221+
unquoteDef one-unused-param = declare-copattern one-unused-param zero-unused-param
222+
-- This is a type error:
223+
-- unquoteDef one-unused-param = define-copattern one-unused-param zero-unused-param
224+
-- because the 'define' macro propagates the type of the thing being
225+
-- defined inwards.
210226

211227
-- Functions into records that are universe polymorphic
212228
neat : {ℓ} {A : Type ℓ} A Record A
@@ -217,11 +233,11 @@ private module Test where
217233
-- Implicit insertion is correct for the define- macro, since the type
218234
-- of the function is given.
219235
cool : {ℓ} {A : Type ℓ} A Record A
220-
unquoteDef cool = define-copattern-levels cool neat
236+
unquoteDef cool = define-copatternω cool neat
221237

222238
-- Eta-expanders
223239
expander : {m n : Nat} Unused m Unused n
224240
unquoteDef expander = define-eta-expansion expander
225241

226242
-- Raises a type error: the function should have a declaration.
227-
-- unquoteDecl uncool = define-copattern-levels uncool neat
243+
-- unquoteDecl uncool = define-copatternω uncool neat

src/1Lab/Reflection/Signature.agda

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,18 @@ helper-function def-nm suf ty cls = do
211211
_ define-function nm cls
212212
pure nm
213213

214+
-- Given a well-typed `val : ty`, return a definitionally-equal atomic
215+
-- term equal to `val`, potentially by lifting it into the signature.
216+
-- See 'helper-function' for the naming scheme.
217+
define-abbrev : Name String Term Term TC Term
218+
define-abbrev def-nm suf ty val with is-atomic-tree? val
219+
... | true = pure val
220+
... | false = do
221+
let (tel , _) = pi-impl-view ty
222+
nm helper-function def-nm suf ty
223+
[ clause tel (tel→pats 0 tel) (apply-tm* val (tel→args 0 tel)) ]
224+
pure (def₀ nm)
225+
214226
private
215227
make-args : Nat List (Arg Nat) List (Arg Term)
216228
make-args n xs = reverse $ map (λ (arg ai i) arg ai (var (n - i - 1) [])) xs

src/1Lab/Reflection/Subst.agda

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ subst-tm ρ (agda-sort s) with s
142142
subst-tel ρ [] = []
143143
subst-tel ρ ((x , arg ai t) ∷ xs) = (x , arg ai (subst-tm ρ t)) ∷ subst-tel (liftS 1 ρ) xs
144144

145-
subst-clause σ (clause tel ps t) = clause (subst-tel σ tel) ps (subst-tm (wkS (length tel) σ) t)
145+
subst-clause σ (clause tel ps t) = clause (subst-tel σ tel) ps (subst-tm (liftS (length tel) σ) t)
146146
subst-clause σ (absurd-clause tel ps) = absurd-clause (subst-tel σ tel) ps
147147

148148
_<#>_ : Term Arg Term Term

src/Cat/Abelian/Images.lagda.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ images : ∀ {A B} (f : Hom A B) → Image C f
5353
images f = im where
5454
the-img : ↓Obj (const! (cut f)) Forget-full-subcat
5555
the-img .x = tt
56-
the-img .y .object = cut (Ker.kernel (Coker.coeq f))
57-
the-img .y .witness {c} = kernels-are-subobjects C ∅ _ (Ker.has-is-kernel _)
56+
the-img .y .fst = cut (Ker.kernel (Coker.coeq f))
57+
the-img .y .snd {c} = kernels-are-subobjects C ∅ _ (Ker.has-is-kernel _)
5858
```
5959
6060
Break $f$ down as an epi $p : A \epi \ker (\coker f)$ followed by a mono
@@ -128,7 +128,7 @@ a (unique) map $\coker (\ker f) \to X$ s.t. the triangle above commutes!
128128
where abstract
129129
path : other .map .map ∘ 0m ≡ other .map .map ∘ kernel f .Kernel.kernel
130130
path =
131-
other .y .witness _ _ $ sym $
131+
other .y .snd _ _ $ sym $
132132
pulll (other .map .commutes)
133133
·· Ker.equal f
134134
·· ∅.zero-∘r _
@@ -154,14 +154,14 @@ is the image of $f$.
154154
(coker-ker≃ker-coker f .is-invertible.invr))) refl
155155
∙ pullr (Coker.factors _) ∙ other .map .commutes)
156156
(sym (decompose f .snd ∙ assoc _ _ _))
157-
factor .sq = /-Hom-path $ sym $ other .y .witness _ _ $
157+
factor .sq = /-Hom-path $ sym $ other .y .snd _ _ $
158158
pulll (factor .β .commutes)
159159
∙ the-img .map .commutes
160160
·· sym (other .map .commutes)
161-
·· ap (other .y .object .map ∘_) (intror refl)
161+
·· ap (other .y .fst .map ∘_) (intror refl)
162162
163163
unique : ∀ x → factor ≡ x
164-
unique x = ↓Hom-path _ _ refl $ /-Hom-path $ other .y .witness _ _ $
164+
unique x = ↓Hom-path _ _ refl $ /-Hom-path $ other .y .snd _ _ $
165165
sym (x .β .commutes ∙ sym (factor .β .commutes))
166166
```
167167
</details>

src/Cat/Diagram/Equaliser/RegularMono.lagda.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ is an [[equaliser]] of some pair of arrows $g, h : b \to c$.
4343
{c} : Ob
4444
arr₁ arr₂ : Hom b c
4545
has-is-eq : is-equaliser C arr₁ arr₂ f
46-
46+
4747
open is-equaliser has-is-eq public
4848
```
4949

@@ -53,7 +53,7 @@ are in fact monomorphisms:
5353
```agda
5454
is-regular-mono→is-mono : is-monic f
5555
is-regular-mono→is-mono = is-equaliser→is-monic _ has-is-eq
56-
56+
5757
open is-regular-mono using (is-regular-mono→is-mono) public
5858
```
5959

@@ -126,7 +126,7 @@ $\phi \circ i_2 = \rm{arr_2}$.
126126
```agda
127127
phi : Hom f⊔f.coapex reg.c
128128
phi = f⊔f.universal reg.equal
129-
129+
130130
open is-effective-mono
131131
mon : is-effective-mono C f
132132
mon .cokernel = f⊔f.coapex
@@ -201,20 +201,20 @@ the code below demonstrates.
201201
→ M-image C (is-regular-mono C , is-regular-mono→is-mono) f
202202
is-effective-mono→image {f = f} mon = im where
203203
module eff = is-effective-mono mon
204-
204+
205205
itself : ↓Obj _ _
206206
itself .x = tt
207-
itself .y = restrict (cut f) eff.is-effective-mono→is-regular-mono
207+
itself .y = cut f , eff.is-effective-mono→is-regular-mono
208208
itself .map = record { map = id ; commutes = idr _ }
209-
209+
210210
im : Initial _
211211
im .bot = itself
212212
im .has⊥ other = contr hom unique where
213213
hom : ↓Hom _ _ itself other
214214
hom .α = tt
215215
hom .β = other .map
216216
hom .sq = trivial!
217-
217+
218218
unique : ∀ x → hom ≡ x
219219
unique x = ↓Hom-path _ _ refl
220220
(ext (intror refl ∙ ap map (x .sq) ∙ elimr refl))

src/Cat/Diagram/Image.lagda.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,18 @@ is the image object, and $m$ is the inclusion map:
124124

125125
```agda
126126
Im : Ob
127-
Im = im .bot .y .object .domain
127+
Im = im .bot .y .fst .domain
128128
129129
Im→codomain : Hom Im b
130-
Im→codomain = im .bot .y .object .map
130+
Im→codomain = im .bot .y .fst .map
131131
```
132132

133133
Furthermore, this map is both an inclusion (since $M$ is a class of
134134
monomorphisms), and an $M$-inclusion at that:
135135

136136
```agda
137137
Im→codomain-is-M : M .fst Im→codomain
138-
Im→codomain-is-M = im .bot .y .witness
138+
Im→codomain-is-M = im .bot .y .snd
139139
140140
Im→codomain-is-monic : is-monic Im→codomain
141141
Im→codomain-is-monic = M .snd Im→codomain-is-M
@@ -175,7 +175,7 @@ through $k$:
175175
universal m M i p = im .has⊥ obj .centre .β .map where
176176
obj : ↓Obj _ _
177177
obj .x = tt
178-
obj .y = restrict (cut m) M
178+
obj .y = cut m , M
179179
obj .map = record { map = i ; commutes = p }
180180
181181
universal-factors

src/Cat/Functor/Algebra.lagda.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ forms a full subcategory of $\cC$.
603603
private module Free-algebras = Cat.Reasoning Free-algebras
604604
module Free-alg (α : Free-algebras.Ob) where
605605
-- Rexport stuff in a more user-friendly format
606-
open Free-object (α .witness) public
606+
open Free-object (α .snd) public
607607
608608
ob : Ob
609609
ob = free .fst

0 commit comments

Comments
 (0)