Skip to content

Commit 149d25f

Browse files
WhatisRTomelkonian
authored andcommitted
Add type annotations to the generated case_of_ terms
This improves instance generation time by another ~10% in the existing benchmarks and prevents exponential blowup in constructors with many arguments.
1 parent 2df226a commit 149d25f

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

Tactic/Derive/DecEq.agda

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ open import Relation.Nullary
2020

2121
open import Reflection.Tactic
2222
open import Reflection.QuotedDefinitions
23+
open import Reflection.AST.DeBruijn
2324

2425
open import Class.DecEq.Core
2526
open import Class.Functor
@@ -32,6 +33,11 @@ open import Tactic.Derive (quote DecEq) (quote _≟_)
3233

3334
open ClauseExprM
3435

36+
-- simply typed annotated case_of_, giving better performance than without a type annotation
37+
-- the type annotation prevents elaboration time from doubling on every argument to a constructor
38+
`case_returning_of_ : Term Term Term Term
39+
`case t returning t' of t'' = def (quote case_of_) (hArg? ∷ hArg? ∷ hArg? ∷ hArg t' ∷ vArg t ∷ vArg t'' ∷ [])
40+
3541
private
3642
instance _ = ContextMonad-MonadTC
3743

@@ -65,10 +71,14 @@ private
6571
genBranch : Maybe SinglePattern TC Term
6672
genBranch nothing = return $ `no `λ⦅ [ ("" , vArg?) ] ⦆∅
6773
genBranch (just ([] , _)) = return $ `yes `refl
68-
genBranch (just p@(l@(x ∷ xs) , _)) = do
74+
genBranch (just p@(l@(x ∷ xs) , arg _ pat)) = do
75+
(con n args) return pat
76+
where _ error1 "BUG: genBranch"
6977
typeList traverse inferType (applyUpTo ♯ (length l))
70-
let eqs = L.map eqFromTerm typeList
71-
return $ foldl (λ t eq genCase eq t) genTrueCase eqs
78+
let info = L.zip typeList (downFrom (length l))
79+
let ty = quote Dec ∙⟦ con n (applyDownFrom (vArg ∘ ♯ ∘ (_+ length l)) (length l))
80+
`≡ con n (applyDownFrom (vArg ∘ ♯) (length l)) ⟧
81+
return $ foldl (λ t (eq , k) genCase (weaken k ty) (eqFromTerm eq) t) genTrueCase info
7282
where
7383
k = ℕ.suc (length xs)
7484

@@ -78,8 +88,10 @@ private
7888
-- case (xᵢ ≟ yᵢ) of λ { (false because ...) → no ... ; (true because p) → t }
7989
-- since we always add one variable to the scope of t the uncompared terms
8090
-- are always at index 2k+1 and k
81-
genCase : (Term Term Term) Term Term
82-
genCase _`≟_ t = `case ♯ (2 * k ∸ 1) `≟ ♯ (k ∸ 1) of clauseExprToPatLam (MatchExpr
91+
genCase : Term (Term Term Term) Term Term
92+
genCase goalTy _`≟_ t = `case ♯ (2 * k ∸ 1) `≟ ♯ (k ∸ 1)
93+
returning goalTy
94+
of clauseExprToPatLam (MatchExpr
8395
( (singlePatternFromPattern (vArg (``yes' (` 0))) , inj₂ (just t))
8496
∷ (singlePatternFromPattern (vArg (``no' (` 0))) , inj₂ (just (`no $
8597
-- case ¬p of λ where (ofⁿ ¬p) refl → ¬p refl
@@ -132,6 +144,7 @@ private
132144

133145
unquoteDecl DecEq-R1 = derive-DecEq [ (quote R1 , DecEq-R1) ]
134146
unquoteDecl DecEq-R2 = derive-DecEq [ (quote R2 , DecEq-R2) ]
147+
unquoteDecl DecEq-R20 = derive-DecEq [ (quote R20 , DecEq-R20) ]
135148

136149
unquoteDecl DecEq-M₁ DecEq-M₂ = derive-DecEq $ (quote M₁ , DecEq-M₁) ∷ (quote M₂ , DecEq-M₂) ∷ []
137150

Tactic/Derive/TestTypes.agda

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ record R2 {a} (A : Set a) : Set a where
4242
f5R2 : A
4343
f6R2 : A
4444

45+
record R20 : Set where
46+
field r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13 r14 r15 r16 r17 r18 r19 : Bool
47+
4548
data M₁ : Set
4649
data M₂ : Set
4750
data M₁ where

0 commit comments

Comments
 (0)