Skip to content

Commit 9d27351

Browse files
WhatisRTomelkonian
authored andcommitted
Fix various bugs with the type generation of the deriving mechanism
N-ary types were omitted and some DB indices were a bit off in some scenarios.
1 parent f4492c5 commit 9d27351

File tree

8 files changed

+136
-34
lines changed

8 files changed

+136
-34
lines changed

Class/MonadTC.agda

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ open import Reflection.Syntax
1212

1313
open import Reflection.Debug
1414

15+
open import Class.Show
16+
open import Class.DecEq
1517
open import Class.MonadReader
1618
open import Class.MonadError
1719
open import Class.Functor
@@ -135,13 +137,82 @@ record MonadTC (M : ∀ {f} → Set f → Set f)
135137
where _ error ("Not a record!" ∷ᵈ [])
136138
return $ con c args
137139

140+
isSolvedMeta : Term M Bool
141+
isSolvedMeta m@(meta x args) = do
142+
r@(meta y _) reduce m
143+
where _ return true
144+
return (x ≠ y)
145+
isSolvedMeta _ = error ("Not a meta!" ∷ᵈ [])
146+
147+
hasUnsolvedMetas : Term M Bool
148+
hasUnsolvedMetas' : List (Arg Term) M Bool
149+
hasUnsolvedMetasCl : List Clause M Bool
150+
hasUnsolvedMetasTel : Telescope M Bool
151+
152+
hasUnsolvedMetas (var x args) = hasUnsolvedMetas' args
153+
hasUnsolvedMetas (con c args) = hasUnsolvedMetas' args
154+
hasUnsolvedMetas (def f args) = hasUnsolvedMetas' args
155+
hasUnsolvedMetas (lam v (abs _ t)) = hasUnsolvedMetas t
156+
hasUnsolvedMetas (pat-lam cs args) = do
157+
a hasUnsolvedMetasCl cs
158+
b hasUnsolvedMetas' args
159+
return (a ∨ b)
160+
hasUnsolvedMetas (pi (arg _ a) (abs _ b)) = do
161+
a hasUnsolvedMetas a
162+
b hasUnsolvedMetas b
163+
return (a ∨ b)
164+
hasUnsolvedMetas (sort (set t)) = hasUnsolvedMetas t
165+
hasUnsolvedMetas (sort (prop t)) = hasUnsolvedMetas t
166+
hasUnsolvedMetas (sort unknown) = return true
167+
hasUnsolvedMetas (sort _) = return false
168+
hasUnsolvedMetas (lit l) = return false
169+
hasUnsolvedMetas m@(meta x x₁) = not <$> isSolvedMeta m
170+
hasUnsolvedMetas unknown = return false
171+
172+
hasUnsolvedMetas' [] = return false
173+
hasUnsolvedMetas' ((arg _ x) ∷ l) = do
174+
a hasUnsolvedMetas x
175+
b hasUnsolvedMetas' l
176+
return (a ∨ b)
177+
178+
hasUnsolvedMetasCl [] = return false
179+
hasUnsolvedMetasCl (clause tel _ t ∷ cl) = do
180+
a hasUnsolvedMetas t
181+
b hasUnsolvedMetasTel tel
182+
c hasUnsolvedMetasCl cl
183+
return (a ∨ b ∨ c)
184+
hasUnsolvedMetasCl (absurd-clause tel _ ∷ cl) = do
185+
a hasUnsolvedMetasTel tel
186+
b hasUnsolvedMetasCl cl
187+
return (a ∨ b)
188+
189+
hasUnsolvedMetasTel [] = return false
190+
hasUnsolvedMetasTel ((_ , arg _ x) ∷ tel) = do
191+
a hasUnsolvedMetas x
192+
b hasUnsolvedMetasTel tel
193+
return (a ∨ b)
194+
138195
-- this allows mutual recursion
139196
declareAndDefineFuns : List (Arg Name × Type × List Clause) M ⊤
140197
declareAndDefineFuns ds = do
141198
traverse (λ (n , t , _) declareDef n t) ds
142199
traverse (λ where (arg _ n , _ , cs) defineFun n cs) ds
143200
return _
144201

202+
declareAndDefineFunsDebug : List (Arg Name × Type × List Clause) M ⊤
203+
declareAndDefineFunsDebug ds = do
204+
traverse (λ (n , t , _) declareDef n t) ds
205+
traverse (λ where (arg _ n , _ , _) do
206+
b getType n >>= hasUnsolvedMetas
207+
if b then error [ strErr $ show n ] else return tt) ds
208+
traverse (λ where (arg _ n , _ , cs) defineFun n cs) ds
209+
traverse (λ where (arg _ n , _ , _) do
210+
d@(function cs) getDefinition n
211+
where _ error [ strErr "Weird bug" ]
212+
b hasUnsolvedMetasCl cs
213+
if b then error [ strErr $ show d ] else return tt) ds
214+
return _
215+
145216
declareAndDefineFun : Arg Name Type List Clause M ⊤
146217
declareAndDefineFun n ty cls = declareAndDefineFuns [ (n , ty , cls) ]
147218

Reflection/Debug.agda

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ instance
3333
IsErrorPart-Name : IsErrorPart Name
3434
IsErrorPart-Name .toErrorPart = ErrorPart.nameErr
3535

36+
IsErrorPart-Pattern : IsErrorPart Pattern
37+
IsErrorPart-Pattern .toErrorPart = ErrorPart.pattErr
38+
3639
IsErrorPart-Clause : IsErrorPart Clause
3740
IsErrorPart-Clause .toErrorPart c = ErrorPart.termErr (pat-lam [ c ] [])
3841

Reflection/Utils.agda

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,9 @@ updateField fs rexp fn fexp =
251251
else
252252
clause [] [ vArg (proj f) ] (f ∙⟦ rexp ⟧)
253253
) []
254+
255+
toTelescope : List (Abs (Arg Type)) Telescope
256+
toTelescope = map (λ where (abs n x) (n , x))
257+
258+
fromTelescope : Telescope List (Abs (Arg Type))
259+
fromTelescope = map (λ where (n , x) (abs n x))

Reflection/Utils/TCI.agda

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ viewAndReduceTy ty = helper ty =<< (length ∘ proj₁ ∘ viewTy) <$> normalise
224224
getType' : Name M TypeView
225225
getType' n = viewAndReduceTy =<< getType n
226226

227+
getTypeDB : M TypeView
228+
getTypeDB n = viewAndReduceTy =<< inferType (♯ n)
229+
227230
getDataDef : Name M DataDef
228231
getDataDef n = inDebugPath "getDataDef" do
229232
debugLog ("Find details for datatype: " ∷ᵈ n ∷ᵈ [])
@@ -268,11 +271,11 @@ isSort t = do
268271
where _ return false
269272
return true
270273

271-
isNArySort : Term M Bool
272-
isNArySort n t = do
274+
isNArySort : Term M (Bool × ℕ)
275+
isNArySort t = do
273276
(tel , ty) viewAndReduceTy t
274277
b isSort ty
275-
return (⌊ (length tel) ≟ n ⌋ ∧ b)
278+
return (b , length tel)
276279

277280
isDefT : Name Term M Bool
278281
isDefT n t = do
@@ -293,8 +296,13 @@ withSafeReset x = runAndReset $ do
293296
debugLog ("In term: " ∷ᵈ t ∷ᵈ [])
294297
error1 "Unsolved metavariables remaining in withSafeReset!"
295298

296-
-- apply a list of terms to a name, picking the correct constructor & visibility
299+
-- apply a list of terms to a name, picking the correct constructor of the term datatype & visibility
297300
applyWithVisibility : Name List Term M Term
298301
applyWithVisibility n l = do
299302
(argTys , _) getType' n
300303
nameConstr n (zipWith (λ where (abs _ (arg i _)) arg i) argTys l)
304+
305+
applyWithVisibilityDB : List Term M Term
306+
applyWithVisibilityDB n l = do
307+
(argTys , _) getTypeDB n
308+
return $ var n (zipWith (λ where (abs _ (arg i _)) arg i) argTys l)

Tactic/Assumption.agda

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,6 @@ open import Reflection.Utils.TCI
2121
instance
2222
_ = Functor-M
2323

24-
solve : ⦃ TCOptions ⦄ Term Tactic
25-
solve t = initTac $ runSpeculative $ do
26-
inj₁ goal reader TCEnv.goal
27-
where _ error1 "solve: Goal is not a term!"
28-
metas findMetas <$> checkType t goal
29-
if null metas
30-
then (_, true) <$> unify goal t
31-
else (_, false) <$> error1 "Unsolved metavariables remaining!"
32-
3324
assumption' : ITactic
3425
assumption' = inDebugPath "assumption" do
3526
c getContext

Tactic/ClauseBuilder.agda

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ open SortingAlgorithm ≤-decTotalOrder (mergeSort ≤-decTotalOrder) public
1818
open import Reflection.Utils
1919
open import Reflection.Utils.TCI
2020

21+
open import Class.Show
2122
open import Class.DecEq
2223
open import Class.Functor
2324
open import Class.MonadError.Instances
@@ -34,6 +35,8 @@ private variable
3435
a b : Level
3536
A : Set a
3637

38+
open import Data.String as S using (parens)
39+
3740
record ClauseBuilder (M : Set Set) : Set₁ where
3841
field
3942
Base : Set Set
@@ -241,7 +244,7 @@ module _ {M : ∀ {a} → Set a → Set a} ⦃ _ : Monad M ⦄ ⦃ me : MonadErr
241244

242245
-- if the goal is of type (a : A) → B, return the type of the branch of pattern p and new context
243246
specializeType : SinglePattern Type M (Type × Telescope)
244-
specializeType p@(t , arg i p') goalTy = markDontFail "specializeType" $ inDebugPath "specializeType" $ runAndReset do
247+
specializeType p@(t , arg i p') goalTy = inDebugPath "specializeType" $ runAndReset do
245248
debugLog ("Goal type to specialize: " ∷ᵈ goalTy ∷ᵈ [])
246249
cls@((Clause.clause tel _ _) ∷ _) return $ clauseExprToClauses $ MatchExpr $
247250
(p , inj₂ (just unknown)) ∷
@@ -261,7 +264,7 @@ module _ {M : ∀ {a} → Set a → Set a} ⦃ _ : Monad M ⦄ ⦃ me : MonadErr
261264
return (goalTy' , newCtx)
262265

263266
ContextMonad-MonadTC : ContextMonad M
264-
ContextMonad-MonadTC .introPatternM pat x = do
267+
ContextMonad-MonadTC .introPatternM pat@(_ , arg _ p) x = do
265268
goalTy goalTy
266269
(newGoalTy , newContext) specializeType pat goalTy
267270
extendContext' newContext (runWithGoalTy newGoalTy x)

Tactic/Derive.agda

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,39 +34,56 @@ open import Relation.Nullary.Decidable
3434
open import Tactic.ClauseBuilder
3535

3636
open import Class.DecEq
37-
open import Class.Monad
38-
open import Class.Traversable
3937
open import Class.Functor
38+
open import Class.Monad
4039
open import Class.MonadReader.Instances
4140
open import Class.MonadTC.Instances
41+
open import Class.Show
42+
open import Class.Traversable
4243

4344
instance
4445
_ = ContextMonad-MonadTC
4546
_ = Functor-M
47+
_ = Show-List
4648

4749
open ClauseExprM
4850

51+
-- generate the type of the `className dName` instance
4952
genClassType : Name Maybe Name TC Type
5053
genClassType arity dName wName = do
5154
params getParamsAndIndices dName
52-
adjParams adjustParams $ take (length params ∸ arity) params
55+
let params' = L.map (λ where (abs x y) abs x (hide y)) $ take (length params ∸ arity) params
56+
sorts collectRelevantSorts params'
57+
debugLog1 ("Generate required instances at indices: " S.++ show (L.map proj₁ sorts))
58+
let adjustedDBs = L.zipWith (λ (i , tel) a (i + a , tel)) sorts (upTo (length sorts))
59+
adjustedDBs' extendContext' (toTelescope params) $ genSortInstanceWithCtx adjustedDBs
60+
let adjParams = params' ++ adjustedDBs'
5361
debugLog1 "AdjustedParams: "
54-
logTelescope (L.map ((λ where (abs s x) just s , x) ∘ proj₁) adjParams)
55-
ty applyWithVisibility dName (L.map ♯ (trueIndices adjParams))
56-
return $ modifyClassType wName (L.map proj₁ adjParams , ty)
62+
logTelescope (L.map ((λ where (abs s x) just s , x)) adjParams)
63+
ty applyWithVisibility dName (L.map (♯ ∘ (_+ length sorts)) (downFrom (length params)))
64+
return $ modifyClassType wName (adjParams , ty)
5765
where
58-
adjustParams : List (Abs (Arg Type)) TC (List ((Abs (Arg Type)) × Bool))
59-
adjustParams [] = return []
60-
adjustParams (abs x (arg _ t) ∷ l) = do
61-
a (if_then [ (abs "_" (iArg (className ∙⟦ ♯ 0 ⟧)) , false) ] else []) <$> isNArySort arity t
62-
ps extendContext (x , hArg t) (adjustParams l)
63-
let ps' = flip L.map ps λ where
64-
(abs s (arg i t) , b) (abs s (arg i (mapVars (_+ (if b then length a else 0)) t)) , b)
65-
return (((abs x (hArg t) , true) ∷ a) ++ ps')
66-
67-
trueIndices : {A : Set} List (A × Bool) List ℕ
68-
trueIndices [] = []
69-
trueIndices (x ∷ l) = if proj₂ x then length l ∷ trueIndices l else trueIndices l
66+
-- returns list of DB indices (at the end) and telescope of argument types
67+
collectRelevantSorts : List (Abs (Arg Type)) TC (List (ℕ × ℕ))
68+
collectRelevantSorts [] = return []
69+
collectRelevantSorts (abs x (arg _ t) ∷ l) = do
70+
rec extendContext (x , hArg t) $ collectRelevantSorts l
71+
(b , k) isNArySort t
72+
return (if b then (length l , k) ∷ rec else rec)
73+
74+
genSortInstance : TC Term
75+
genSortInstance k 0 i = do
76+
res applyWithVisibilityDB (i + k) (L.map ♯ $ downFrom k)
77+
return $ className ∙⟦ res ⟧
78+
genSortInstance k (suc a) i = do
79+
res extendContext ("" , hArg unknown) $ genSortInstance k a i
80+
return $ pi (hArg unknown) $ abs "_" res
81+
82+
genSortInstanceWithCtx : List (ℕ × ℕ) TC (List (Abs (Arg Term)))
83+
genSortInstanceWithCtx [] = return []
84+
genSortInstanceWithCtx ((i , k) ∷ xs) = do
85+
x' (abs "_" ∘ iArg) <$> (genSortInstance k k i)
86+
(x' ∷_) <$> (extendContext ("", hArg unknown) $ genSortInstanceWithCtx xs)
7087

7188
modifyClassType : Maybe Name TypeView Type
7289
modifyClassType nothing (tel , ty) = tyView (tel , className ∙⟦ ty ⟧)
@@ -115,7 +132,7 @@ module _ (arity : ℕ) (genCe : (Name → Maybe Name) → List SinglePattern →
115132

116133
derive-Class : ⦃ TCOptions ⦄ List (Name × Name) UnquoteDecl
117134
derive-Class l = initUnquoteWithGoal (className ∙) $
118-
declareAndDefineFuns =<< concat <$> traverse ⦃ Functor-List ⦄ helper l
135+
declareAndDefineFuns =<< runAndReset (concat <$> traverse ⦃ Functor-List ⦄ helper l)
119136
where
120137
helper : Name × Name TC (List (Arg Name × Type × List Clause))
121138
helper (a , b) = do hs genMutualHelpers a ; deriveMulti (a , b , hs)

Tactic/Derive/Show.agda

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,15 @@ private
7070
open import Tactic.Derive.TestTypes
7171
import Data.Nat.Show
7272
open import Tactic.Defaults
73+
open import Data.List.Relation.Binary.Pointwise.Base
7374

7475
unquoteDecl Show-Bool Show-ℤ Show-List Show-Maybe Show-ℕ Show-Sign Show-⊤ = derive-Show ((quote Bool , Show-Bool) ∷ (quote ℤ , Show-ℤ) ∷ (quote List , Show-List) ∷ (quote Maybe , Show-Maybe) ∷ (quote ℕ , Show-ℕ) ∷ (quote Sign , Show-Sign) ∷ (quote ⊤ , Show-⊤) ∷ [])
7576

7677
unquoteDecl Show-Fin = derive-Show [ (quote Fin , Show-Fin) ]
7778
unquoteDecl Show-Vec = derive-Show [ (quote Vec , Show-Vec) ]
7879

80+
unquoteDecl Show-Pointwise = derive-Show [ (quote Pointwise , Show-Pointwise) ]
81+
7982
unquoteDecl Show-These = derive-Show [ (quote These , Show-These) ]
8083
unquoteDecl Show-⊎ = derive-Show [ (quote _⊎_ , Show-⊎) ]
8184

0 commit comments

Comments
 (0)