@@ -60,10 +60,11 @@ elabInstance :: ElabInfo -> SyntaxInfo ->
6060 Name -> -- the class
6161 FC -> -- precise location of class name
6262 [PTerm] -> -- class parameters (i.e. instance)
63+ [(Name, PTerm)] -> -- Extra arguments in scope (e.g. instance in where block)
6364 PTerm -> -- full instance type
6465 Maybe Name -> -- explicit name
6566 [PDecl] -> Idris ()
66−elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
67+elabInstance info syn doc argDocs what fc cs acc opts n nfc ps pextra t expn ds = do
6768 ist <- getIState
6869 (n, ci) <- case lookupCtxtName n (idris_classes ist) of
6970 [c] -> return c
@@ -79,7 +80,8 @@ elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
7980
8081 let emptyclass = null (class_methods ci)
8182 when (what /= EDefns) $ do
82− nty <- elabType' True info syn doc argDocs fc totopts iname NoFC t
83+ nty <- elabType' True info syn doc argDocs fc totopts iname NoFC
84+ (piBindp expl_param pextra t)
8385 -- if the instance type matches any of the instances we have already,
8486 -- and it's not a named instance, then it's overlapping, so report an error
8587 case expn of
@@ -125,7 +127,7 @@ elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
125127 all_meths
126128 t' = substMatchesShadow mnamemap pnames t_in in
127129 (decorate ns iname n,
128− op, coninsert cs t', t'))
130+ op, coninsert cs pextra t', t'))
129131 (class_methods ci)
130132 logElab 3 (show (mtys, ips))
131133 logElab 5 ("Before defaults: " ++ show ds ++ "\n" ++ show (map fst (class_methods ci)))
@@ -136,8 +138,11 @@ elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
136138 mapM_ (warnMissing ds' ns iname) (map fst (class_methods ci))
137139 mapM_ (checkInClass (map fst (class_methods ci))) (concatMap defined ds')
138140 let wbTys = map mkTyDecl mtys
139− let wbVals = map (decorateid (decorate ns iname)) ds'
141+ let wbVals_orig = map (decorateid (decorate ns iname)) ds'
142+ ist <- getIState
143+ let wbVals = map (expandParamsD False ist id pextra (map methName mtys)) wbVals_orig
140144 let wb = wbTys ++ wbVals
145+
141146 logElab 3 $ "Method types " ++ showSep "\n" (map (show . showDeclImp verbosePPOption . mkTyDecl) mtys)
142147 logElab 3 $ "Instance is " ++ show ps ++ " implicits " ++
143148 show (concat (nub wparams))
@@ -145,7 +150,7 @@ elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
145150
146151 let lhsImps = map (\n -> pimp n (PRef fc [] n) True) headVars
147152
148− let lhs = PApp fc (PRef fc [] iname) lhsImps
153+ let lhs = PApp fc (PRef fc [] iname) (lhsImps ++ map (toExp .fst) pextra)
149154 let rhs = PApp fc (PRef fc [] (instanceCtorName ci))
150155 (map (pexp . (mkMethApp lhsImps)) mtys)
151156
@@ -154,6 +159,7 @@ elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
154159
155160 push_estack iname True
156161 logElab 3 ("Method types: " ++ show wbTys)
162+ logElab 3 ("Method bodies (before params): " ++ show wbVals_orig)
157163 logElab 3 ("Method bodies: " ++ show wbVals)
158164
159165 let idecls = [PClauses fc totopts iname
@@ -196,10 +202,13 @@ elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
196202 Just m -> sNS (SN (sInstanceN n' (map show ps'))) m
197203 Just nm -> nm
198204
199− substInstance ips pnames (PInstance doc argDocs syn _ cs acc opts n nfc ps t expn ds)
200− = PInstance doc argDocs syn fc cs acc opts n nfc (map (substMatchesShadow ips pnames) ps) (substMatchesShadow ips pnames t) expn ds
205+ substInstance ips pnames (PInstance doc argDocs syn _ cs acc opts n nfc ps pextra t expn ds)
206+ = PInstance doc argDocs syn fc cs acc opts n nfc
207+ (map (substMatchesShadow ips pnames) ps)
208+ pextra
209+ (substMatchesShadow ips pnames t) expn ds
201210
202− isOverlapping i (PInstance doc argDocs syn _ _ _ _ n nfc ps t expn _)
211+ isOverlapping i (PInstance doc argDocs syn _ _ _ _ n nfc ps pextra t expn _)
203212 = case lookupCtxtName n (idris_classes i) of
204213 [(n, ci)] -> let iname = (mkiname n (namespace info) ps expn) in
205214 case lookupTy iname (tt_ctxt i) of
@@ -256,11 +265,15 @@ elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
256265 map snd (filter (\(i, _) -> i `elem` dets) a')
257266 keepDets dets t = t
258267
268+ methName (n, _, _, _) = n
269+ toExp n = pexp (PRef fc [] n)
270+
259271 mkMethApp ps (n, _, _, ty)
260272 = lamBind 0 ty (papp fc (PRef fc [] n)
261− (ps ++ methArgs 0 ty))
273+ (ps ++ map (toExp . fst) pextra ++ methArgs 0 ty))
262274 where
263275 needed is p = pname p `elem` map pname is
276+
264277 lamBind i (PPi (Constraint _ _) _ _ _ sc) sc'
265278 = PLam fc (sMN i "meth") NoFC Placeholder (lamBind (i+1) sc sc')
266279 lamBind i (PPi _ n _ ty sc) sc'
@@ -287,10 +300,10 @@ elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
287300 _ -> return ps'
288301 getWParams (_ : ps) = getWParams ps
289302
290− decorate ns iname (UN nm)
291− = NS (SN (WhereN 0 iname (SN (MethodN (UN nm))))) ns
292303 decorate ns iname (NS (UN nm) s)
293304 = NS (SN (WhereN 0 iname (SN (MethodN (UN nm))))) ns
305+ decorate ns iname nm
306+ = NS (SN (WhereN 0 iname (SN (MethodN nm)))) ns
294307
295308 mkTyDecl (n, op, t, _)
296309 = PTy emptyDocstring [] syn fc op n NoFC
@@ -300,9 +313,13 @@ elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
300313 conbind ((c,ty) : ns) x = PPi constraint c NoFC ty (conbind ns x)
301314 conbind [] x = x
302315
303− coninsert :: [(Name, PTerm)] -> PTerm -> PTerm
304− coninsert cs (PPi p@(Imp _ _ _ _ _) n fc t sc) = PPi p n fc t (coninsert cs sc)
305− coninsert cs sc = conbind cs sc
316+ extrabind :: [(Name, PTerm)] -> PTerm -> PTerm
317+ extrabind ((c,ty) : ns) x = PPi expl c NoFC ty (extrabind ns x)
318+ extrabind [] x = x
319+
320+ coninsert :: [(Name, PTerm)] -> [(Name, PTerm)] -> PTerm -> PTerm
321+ coninsert cs ex (PPi p@(Imp _ _ _ _ _) n fc t sc) = PPi p n fc t (coninsert cs ex sc)
322+ coninsert cs ex sc = conbind cs (extrabind ex sc)
306323
307324 -- Reorder declarations to be in the same order as defined in the
308325 -- class declaration (important so that we insert default definitions