@@ -60,10 +60,11 @@ elabInstance :: ElabInfo -> SyntaxInfo ->
60 Name -> -- the class
60 Name -> -- the class
61 FC -> -- precise location of class name
61 FC -> -- precise location of class name
62 [PTerm] -> -- class parameters (i.e. instance)
62 [PTerm] -> -- class parameters (i.e. instance)
63+ [(Name, PTerm)] -> -- Extra arguments in scope (e.g. instance in where block)
63 PTerm -> -- full instance type
64 PTerm -> -- full instance type
64 Maybe Name -> -- explicit name
65 Maybe Name -> -- explicit name
65 [PDecl] -> Idris ()
66 [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
67 ist <- getIState
68 ist <- getIState
68 (n, ci) <- case lookupCtxtName n (idris_classes ist) of
69 (n, ci) <- case lookupCtxtName n (idris_classes ist) of
69 [c] -> return c
70 [c] -> return c
@@ -79,7 +80,8 @@ elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
80 let emptyclass = null (class_methods ci)
81 let emptyclass = null (class_methods ci)
81 when (what /= EDefns) $ do
82 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)
83 -- if the instance type matches any of the instances we have already,
85 -- if the instance type matches any of the instances we have already,
84 -- and it's not a named instance, then it's overlapping, so report an error
86 -- and it's not a named instance, then it's overlapping, so report an error
85 case expn of
87 case expn of
@@ -125,7 +127,7 @@ elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
125 all_meths
127 all_meths
126 t' = substMatchesShadow mnamemap pnames t_in in
128 t' = substMatchesShadow mnamemap pnames t_in in
127 (decorate ns iname n,
129 (decorate ns iname n,
128− op, coninsert cs t', t'))
130+ op, coninsert cs pextra t', t'))
129 (class_methods ci)
131 (class_methods ci)
130 logElab 3 (show (mtys, ips))
132 logElab 3 (show (mtys, ips))
131 logElab 5 ("Before defaults: " ++ show ds ++ "\n" ++ show (map fst (class_methods ci)))
133 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
136 mapM_ (warnMissing ds' ns iname) (map fst (class_methods ci))
138 mapM_ (warnMissing ds' ns iname) (map fst (class_methods ci))
137 mapM_ (checkInClass (map fst (class_methods ci))) (concatMap defined ds')
139 mapM_ (checkInClass (map fst (class_methods ci))) (concatMap defined ds')
138 let wbTys = map mkTyDecl mtys
140 let wbTys = map mkTyDecl mtys
139− let wbVals = map (decorateid (decorate ns iname)) ds'
141+ let wbVals_orig = map (decorateid (decorate ns iname)) ds'
143+ let wbVals = map (expandParamsD False ist id pextra (map methName mtys)) wbVals_orig
140 let wb = wbTys ++ wbVals
144 let wb = wbTys ++ wbVals
141 logElab 3 $ "Method types " ++ showSep "\n" (map (show . showDeclImp verbosePPOption . mkTyDecl) mtys)
146 logElab 3 $ "Method types " ++ showSep "\n" (map (show . showDeclImp verbosePPOption . mkTyDecl) mtys)
142 logElab 3 $ "Instance is " ++ show ps ++ " implicits " ++
147 logElab 3 $ "Instance is " ++ show ps ++ " implicits " ++
143 show (concat (nub wparams))
148 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
146 let lhsImps = map (\n -> pimp n (PRef fc [] n) True) headVars
151 let lhsImps = map (\n -> pimp n (PRef fc [] n) True) headVars
148− let lhs = PApp fc (PRef fc [] iname) lhsImps
153+ let lhs = PApp fc (PRef fc [] iname) (lhsImps ++ map (toExp .fst) pextra)
149 let rhs = PApp fc (PRef fc [] (instanceCtorName ci))
154 let rhs = PApp fc (PRef fc [] (instanceCtorName ci))
150 (map (pexp . (mkMethApp lhsImps)) mtys)
155 (map (pexp . (mkMethApp lhsImps)) mtys)
@@ -154,6 +159,7 @@ elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
155 push_estack iname True
160 push_estack iname True
156 logElab 3 ("Method types: " ++ show wbTys)
161 logElab 3 ("Method types: " ++ show wbTys)
162+ logElab 3 ("Method bodies (before params): " ++ show wbVals_orig)
157 logElab 3 ("Method bodies: " ++ show wbVals)
163 logElab 3 ("Method bodies: " ++ show wbVals)
159 let idecls = [PClauses fc totopts iname
165 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
196 Just m -> sNS (SN (sInstanceN n' (map show ps'))) m
202 Just m -> sNS (SN (sInstanceN n' (map show ps'))) m
197 Just nm -> nm
203 Just nm -> nm
199− substInstance ips pnames (PInstance doc argDocs syn _ cs acc opts n nfc ps t expn ds)
205+ substInstance ips pnames (PInstance doc argDocs syn _ cs acc opts n nfc ps pextra 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
206+ = PInstance doc argDocs syn fc cs acc opts n nfc
207+ (map (substMatchesShadow ips pnames) ps)
209+ (substMatchesShadow ips pnames t) expn ds
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 _)
203 = case lookupCtxtName n (idris_classes i) of
212 = case lookupCtxtName n (idris_classes i) of
204 [(n, ci)] -> let iname = (mkiname n (namespace info) ps expn) in
213 [(n, ci)] -> let iname = (mkiname n (namespace info) ps expn) in
205 case lookupTy iname (tt_ctxt i) of
214 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
256 map snd (filter (\(i, _) -> i `elem` dets) a')
265 map snd (filter (\(i, _) -> i `elem` dets) a')
257 keepDets dets t = t
266 keepDets dets t = t
268+ methName (n, _, _, _) = n
269+ toExp n = pexp (PRef fc [] n)
259 mkMethApp ps (n, _, _, ty)
271 mkMethApp ps (n, _, _, ty)
260 = lamBind 0 ty (papp fc (PRef fc [] n)
272 = lamBind 0 ty (papp fc (PRef fc [] n)
261− (ps ++ methArgs 0 ty))
273+ (ps ++ map (toExp . fst) pextra ++ methArgs 0 ty))
263 needed is p = pname p `elem` map pname is
275 needed is p = pname p `elem` map pname is
264 lamBind i (PPi (Constraint _ _) _ _ _ sc) sc'
277 lamBind i (PPi (Constraint _ _) _ _ _ sc) sc'
265 = PLam fc (sMN i "meth") NoFC Placeholder (lamBind (i+1) sc sc')
278 = PLam fc (sMN i "meth") NoFC Placeholder (lamBind (i+1) sc sc')
266 lamBind i (PPi _ n _ ty sc) sc'
279 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
287 _ -> return ps'
300 _ -> return ps'
288 getWParams (_ : ps) = getWParams ps
301 getWParams (_ : ps) = getWParams ps
290− decorate ns iname (UN nm)
291− = NS (SN (WhereN 0 iname (SN (MethodN (UN nm))))) ns
292 decorate ns iname (NS (UN nm) s)
303 decorate ns iname (NS (UN nm) s)
293 = NS (SN (WhereN 0 iname (SN (MethodN (UN nm))))) ns
304 = 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
295 mkTyDecl (n, op, t, _)
308 mkTyDecl (n, op, t, _)
296 = PTy emptyDocstring [] syn fc op n NoFC
309 = 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
300 conbind ((c,ty) : ns) x = PPi constraint c NoFC ty (conbind ns x)
313 conbind ((c,ty) : ns) x = PPi constraint c NoFC ty (conbind ns x)
301 conbind [] x = x
314 conbind [] x = x
316+ extrabind :: [(Name, PTerm)] -> PTerm -> PTerm
317+ extrabind ((c,ty) : ns) x = PPi expl c NoFC ty (extrabind ns x)
303− coninsert :: [(Name, PTerm)] -> PTerm -> PTerm
320+ coninsert :: [(Name, PTerm)] -> [(Name, PTerm)] -> PTerm -> PTerm
304− coninsert cs (PPi p@(Imp _ _ _ _ _) n fc t sc) = PPi p n fc t (coninsert cs sc)
321+ coninsert cs ex (PPi p@(Imp _ _ _ _ _) n fc t sc) = PPi p n fc t (coninsert cs ex sc)
305− coninsert cs sc = conbind cs sc
322+ coninsert cs ex sc = conbind cs (extrabind ex sc)
307 -- Reorder declarations to be in the same order as defined in the
324 -- Reorder declarations to be in the same order as defined in the
308 -- class declaration (important so that we insert default definitions
325 -- class declaration (important so that we insert default definitions