@@ -34,39 +34,56 @@ open import Relation.Nullary.Decidable
3434open import Tactic.ClauseBuilder
3535
3636open import Class.DecEq
37- open import Class.Monad
38- open import Class.Traversable
3937open import Class.Functor
38+ open import Class.Monad
4039open import Class.MonadReader.Instances
4140open import Class.MonadTC.Instances
41+ open import Class.Show
42+ open import Class.Traversable
4243
4344instance
4445 _ = ContextMonad-MonadTC
4546 _ = Functor-M
47+ _ = Show-List
4648
4749open ClauseExprM
4850
51+ -- generate the type of the `className dName` instance
4952genClassType : ℕ → Name → Maybe Name → TC Type
5053genClassType 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)
0 commit comments