@@ -20,6 +20,7 @@ open import Relation.Nullary
2020
2121open import Reflection.Tactic
2222open import Reflection.QuotedDefinitions
23+ open import Reflection.AST.DeBruijn
2324
2425open import Class.DecEq.Core
2526open import Class.Functor
@@ -32,6 +33,11 @@ open import Tactic.Derive (quote DecEq) (quote _≟_)
3233
3334open 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+
3541private
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
0 commit comments