Skip to content

Commit 4571f46

Browse files
authored
reflection.induction: handle erased constructors (#478)
Inspired by cmcmA20/cubical-mini#48. Also adds `work-on-types` so that the macro works at all when `--erasure` is used (see agda/agda#6124).
1 parent 6aaf089 commit 4571f46

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

src/1Lab/Reflection.lagda.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ private module P where
103103
-- "blocking" constraints.
104104
noConstraints : ∀ {a} {A : Type a} → TC A → TC A
105105
106+
-- Run the given computation at the type level, allowing use of erased things.
107+
work-on-types : ∀ {a} {A : Type a} → TC A → TC A
108+
106109
-- Run the given TC action and return the first component. Resets to
107110
-- the old TC state if the second component is 'false', or keep the
108111
-- new TC state if it is 'true'.
@@ -158,6 +161,7 @@ private module P where
158161
{-# BUILTIN AGDATCMASKEXPANDLAST askExpandLast #-}
159162
{-# BUILTIN AGDATCMASKREDUCEDEFS askReduceDefs #-}
160163
{-# BUILTIN AGDATCMNOCONSTRAINTS noConstraints #-}
164+
{-# BUILTIN AGDATCMWORKONTYPES work-on-types #-}
161165
{-# BUILTIN AGDATCMRUNSPECULATIVE run-speculative #-}
162166
{-# BUILTIN AGDATCMGETINSTANCES get-instances #-}
163167
{-# BUILTIN AGDATCMDECLAREDATA declareData #-}

src/1Lab/Reflection/Deriving/Show.agda

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private
105105

106106
-- Create the clause of shows-prec for a constructor.
107107
show-clause : Constructor TC Clause
108-
show-clause (conhead conm _ args _) = do
108+
show-clause (conhead conm _ _ args _) = do
109109
let
110110
-- We'll only show the visible arguments to the constructor.
111111
-- Moreover, since Agda can infer the types in the telescope

src/1Lab/Reflection/Induction.agda

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,15 +235,17 @@ private
235235
-- Otherwise, add m : T to the telescope and replace the corresponding
236236
-- constructor with m henceforth.
237237
method ("P" <>_) <$> render-name c
238+
q get-con-quantity c
239+
let argC = arg (arginfo visible (modality relevant q))
238240
let
239241
ps = raise 1 ps
240242
P = raise 1 P
241243
rs = (c' , var 0 []) ∷ raise 1 rs
242-
extend-context method (argN methodT) (go ps P rs cs) <&>
243-
×-map₁₂ ((method , argN methodT) ∷_) (α ∷_)
244+
extend-context method (argC methodT) (go ps P rs cs) <&>
245+
×-map₁₂ ((method , argC methodT) ∷_) (α ∷_)
244246

245247
make-elim-with : Elim-options Name Name TC ⊤
246-
make-elim-with opts elim D = withNormalisation true do
248+
make-elim-with opts elim D = work-on-types $ withNormalisation true do
247249
DT ← get-type D >>= normalise -- D : (ps : Γ) (is : Ξ) Type _
248250
data-type pars cs get-definition D
249251
where _ typeError [ "not a data type: " , nameErr D ]

src/1Lab/Reflection/Signature.agda

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ get-record-type n = get-definition n >>= λ where
2222
(record-type conm fields) pure (conm , fields)
2323
_ typeError [ "get-record-type: definition " , nameErr n , " is not a record type." ]
2424

25+
-- Look up a constructor's quantity in the signature.
26+
get-con-quantity : Name TC Quantity
27+
get-con-quantity n = get-definition n >>= λ where
28+
(data-cons _ q) pure q
29+
_ typeError [ "get-con-erasure: definition " , nameErr n , " is not a constructor." ]
30+
2531
-- Representation of a data/record constructor.
2632
record Constructor : Type where
2733
constructor conhead
@@ -32,6 +38,9 @@ record Constructor : Type where
3238
-- Name of the data type:
3339
con-data : Name
3440

41+
-- Quantity of the constructor.
42+
con-quantity : Quantity
43+
3544
-- Argument telescope for the constructor, with the datatype's
3645
-- parameters removed.
3746
con-arguments : Telescope
@@ -55,22 +64,23 @@ get-type-constructors n = datatype <|> recordtype where
5564
datatype = do
5665
(npars , cons) get-data-type n
5766
for cons λ qn do
67+
q get-con-quantity qn
5868
(args , ty) pi-view <$> get-type qn
59-
pure (conhead qn n (drop npars args) ty)
69+
pure (conhead qn n q (drop npars args) ty)
6070

6171
recordtype = do
6272
(c , _) get-record-type n
6373
(np , _) pi-view <$> get-type n
6474
(args , ty) pi-view <$> get-type c
65-
pure ((conhead c n (drop (length np) args) ty) ∷ [])
75+
pure ((conhead c n quantity-ω (drop (length np) args) ty) ∷ [])
6676

6777
-- Look up a constructor in the signature.
6878
get-constructor : Name TC Constructor
6979
get-constructor n = get-definition n >>= λ where
70-
(data-cons t _) do
80+
(data-cons t q) do
7181
(npars , cons) get-data-type t
7282
(args , ty) pi-view <$> get-type n
73-
pure (conhead n t (drop npars args) ty)
83+
pure (conhead n t q (drop npars args) ty)
7484
_ typeError [ "get-constructor: " , nameErr n , " is not a data constructor." ]
7585

7686
-- If a term reduces to an application of a record type, return

0 commit comments

Comments
 (0)