@@ -55,15 +55,17 @@ elabInstance :: ElabInfo -> SyntaxInfo ->
5555 [(Name, Docstring (Either Err PTerm))] ->
5656 ElabWhat -> -- phase
5757 FC -> [(Name, PTerm)] -> -- constraints
58+ [Name] -> -- parent dictionary names (optionally)
5859 Accessibility ->
5960 FnOpts ->
6061 Name -> -- the class
6162 FC -> -- precise location of class name
6263 [PTerm] -> -- class parameters (i.e. instance)
64+ [(Name, PTerm)] -> -- Extra arguments in scope (e.g. instance in where block)
6365 PTerm -> -- full instance type
6466 Maybe Name -> -- explicit name
6567 [PDecl] -> Idris ()
66−elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
68+elabInstance info syn doc argDocs what fc cs parents acc opts n nfc ps pextra t expn ds = do
6769 ist <- getIState
6870 (n, ci) <- case lookupCtxtName n (idris_classes ist) of
6971 [c] -> return c
@@ -79,7 +81,8 @@ elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
7981
8082 let emptyclass = null (class_methods ci)
8183 when (what /= EDefns) $ do
82− nty <- elabType' True info syn doc argDocs fc totopts iname NoFC t
84+ nty <- elabType' True info syn doc argDocs fc totopts iname NoFC
85+ (piBindp expl_param pextra t)
8386 -- if the instance type matches any of the instances we have already,
8487 -- and it's not a named instance, then it's overlapping, so report an error
8588 case expn of
@@ -88,6 +91,9 @@ elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
8891 addInstance intInst True n iname
8992 Just _ -> addInstance intInst False n iname
9093 when (what /= ETypes && (not (null ds && not emptyclass))) $ do
94+ -- Add the parent implementation names to the privileged set
95+ oldOpen <- addOpenImpl parents
96+
9197 let ips = zip (class_params ci) ps
9298 let ns = case n of
9399 NS n ns' -> ns'
@@ -105,14 +111,27 @@ elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
105111 let superclassInstances = map (substInstance ips pnames) (class_default_superclasses ci)
106112 undefinedSuperclassInstances <- filterM (fmap not . isOverlapping ist) superclassInstances
107113 mapM_ (rec_elabDecl info EAll info) undefinedSuperclassInstances
114+
115+ ist <- getIState
116+ -- Bring variables in instance head into scope when building the
117+ -- dictionary
118+ let headVars = nub $ concatMap getHeadVars ps
119+ let (headVarTypes, ty)
120+ = case lookupTyExact iname (tt_ctxt ist) of
121+ Just ty -> (map (\n -> (n, getTypeIn ist n ty)) headVars, ty)
122+ _ -> (zip headVars (repeat Placeholder), Erased)
123+ logElab 3 $ "Head var types " ++ show headVarTypes ++ " from " ++ show ty
124+
108125 let all_meths = map (nsroot . fst) (class_methods ci)
109126 let mtys = map (\ (n, (op, t)) ->
110127 let t_in = substMatchesShadow ips pnames t
111− mnamemap = map (\n -> (n, PRef fc [] (decorate ns iname n)))
128+ mnamemap =
129+ map (\n -> (n, PApp fc (PRef fc [] (decorate ns iname n))
130+ (map (toImp fc) headVars)))
112131 all_meths
113132 t' = substMatchesShadow mnamemap pnames t_in in
114133 (decorate ns iname n,
115− op, coninsert cs t', t'))
134+ op, coninsert cs pextra t', t'))
116135 (class_methods ci)
117136 logElab 3 (show (mtys, ips))
118137 logElab 5 ("Before defaults: " ++ show ds ++ "\n" ++ show (map fst (class_methods ci)))
@@ -123,19 +142,19 @@ elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
123142 mapM_ (warnMissing ds' ns iname) (map fst (class_methods ci))
124143 mapM_ (checkInClass (map fst (class_methods ci))) (concatMap defined ds')
125144 let wbTys = map mkTyDecl mtys
126− let wbVals = map (decorateid (decorate ns iname)) ds'
145+ let wbVals_orig = map (decorateid (decorate ns iname)) ds'
146+ ist <- getIState
147+ let wbVals = map (expandParamsD False ist id pextra (map methName mtys)) wbVals_orig
127148 let wb = wbTys ++ wbVals
149+
128150 logElab 3 $ "Method types " ++ showSep "\n" (map (show . showDeclImp verbosePPOption . mkTyDecl) mtys)
129151 logElab 3 $ "Instance is " ++ show ps ++ " implicits " ++
130152 show (concat (nub wparams))
131153
132− -- Bring variables in instance head into scope when building the
133− -- dictionary
134− let headVars = nub $ concatMap getHeadVars ps
135−
154+
136155 let lhsImps = map (\n -> pimp n (PRef fc [] n) True) headVars
137156
138− let lhs = PApp fc (PRef fc [] iname) lhsImps
157+ let lhs = PApp fc (PRef fc [] iname) (lhsImps ++ map (toExp .fst) pextra)
139158 let rhs = PApp fc (PRef fc [] (instanceCtorName ci))
140159 (map (pexp . (mkMethApp lhsImps)) mtys)
141160
@@ -144,12 +163,13 @@ elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
144163
145164 push_estack iname True
146165 logElab 3 ("Method types: " ++ show wbTys)
166+ logElab 3 ("Method bodies (before params): " ++ show wbVals_orig)
147167 logElab 3 ("Method bodies: " ++ show wbVals)
148168
149169 let idecls = [PClauses fc totopts iname
150170 [PClause fc iname lhs [] rhs []]]
151171
152− mapM_ (rec_elabDecl info EAll info) (map (impBind headVars) wbTys)
172+ mapM_ (rec_elabDecl info EAll info) (map (impBind headVarTypes) wbTys)
153173 mapM_ (rec_elabDecl info EAll info) idecls
154174
155175 ctxt <- getContext
@@ -169,6 +189,8 @@ elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
169189 mapM_ (rec_elabDecl info EAll info) wbVals'
170190
171191 pop_estack
192+
193+ setOpenImpl oldOpen
172194-- totalityCheckBlock
173195
174196 checkInjectiveArgs fc n (class_determiners ci) (lookupTyExact iname (tt_ctxt ist))
@@ -186,10 +208,13 @@ elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
186208 Just m -> sNS (SN (sInstanceN n' (map show ps'))) m
187209 Just nm -> nm
188210
189− substInstance ips pnames (PInstance doc argDocs syn _ cs acc opts n nfc ps t expn ds)
190− = PInstance doc argDocs syn fc cs acc opts n nfc (map (substMatchesShadow ips pnames) ps) (substMatchesShadow ips pnames t) expn ds
211+ substInstance ips pnames (PInstance doc argDocs syn _ cs parents acc opts n nfc ps pextra t expn ds)
212+ = PInstance doc argDocs syn fc cs parents acc opts n nfc
213+ (map (substMatchesShadow ips pnames) ps)
214+ pextra
215+ (substMatchesShadow ips pnames t) expn ds
191216
192− isOverlapping i (PInstance doc argDocs syn _ _ _ _ n nfc ps t expn _)
217+ isOverlapping i (PInstance doc argDocs syn _ _ _ _ _ n nfc ps pextra t expn _)
193218 = case lookupCtxtName n (idris_classes i) of
194219 [(n, ci)] -> let iname = (mkiname n (namespace info) ps expn) in
195220 case lookupTy iname (tt_ctxt i) of
@@ -246,9 +271,15 @@ elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
246271 map snd (filter (\(i, _) -> i `elem` dets) a')
247272 keepDets dets t = t
248273
249− mkMethApp ps (n, _, _, ty)
250− = lamBind 0 ty (papp fc (PRef fc [] n)
251− (ps ++ methArgs 0 ty))
274+ methName (n, _, _, _) = n
275+ toExp n = pexp (PRef fc [] n)
276+
277+ mkMethApp ps (n, _, _, ty)
278+ = lamBind 0 ty (papp fc (PRef fc [] n)
279+ (ps ++ map (toExp . fst) pextra ++ methArgs 0 ty))
280+ where
281+ needed is p = pname p `elem` map pname is
282+
252283 lamBind i (PPi (Constraint _ _) _ _ _ sc) sc'
253284 = PLam fc (sMN i "meth") NoFC Placeholder (lamBind (i+1) sc sc')
254285 lamBind i (PPi _ n _ ty sc) sc'
@@ -275,10 +306,10 @@ elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
275306 _ -> return ps'
276307 getWParams (_ : ps) = getWParams ps
277308
278− decorate ns iname (UN nm)
279− = NS (SN (WhereN 0 iname (SN (MethodN (UN nm))))) ns
280309 decorate ns iname (NS (UN nm) s)
281310 = NS (SN (WhereN 0 iname (SN (MethodN (UN nm))))) ns
311+ decorate ns iname nm
312+ = NS (SN (WhereN 0 iname (SN (MethodN nm)))) ns
282313
283314 mkTyDecl (n, op, t, _)
284315 = PTy emptyDocstring [] syn fc op n NoFC
@@ -288,9 +319,13 @@ elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
288319 conbind ((c,ty) : ns) x = PPi constraint c NoFC ty (conbind ns x)
289320 conbind [] x = x
290321
291− coninsert :: [(Name, PTerm)] -> PTerm -> PTerm
292− coninsert cs (PPi p@(Imp _ _ _ _ _) n fc t sc) = PPi p n fc t (coninsert cs sc)
293− coninsert cs sc = conbind cs sc
322+ extrabind :: [(Name, PTerm)] -> PTerm -> PTerm
323+ extrabind ((c,ty) : ns) x = PPi expl c NoFC ty (extrabind ns x)
324+ extrabind [] x = x
325+
326+ coninsert :: [(Name, PTerm)] -> [(Name, PTerm)] -> PTerm -> PTerm
327+ coninsert cs ex (PPi p@(Imp _ _ _ _ _) n fc t sc) = PPi p n fc t (coninsert cs ex sc)
328+ coninsert cs ex sc = conbind cs (extrabind ex sc)
294329
295330 -- Reorder declarations to be in the same order as defined in the
296331 -- class declaration (important so that we insert default definitions
@@ -346,19 +381,28 @@ getHeadVars (PDPair _ _ _ l t r) = getHeadVars l ++ getHeadVars t ++ getHeadVars
346381getHeadVars _ = []
347382
348383-- Implicitly bind variables from the instance head in method types
349−impBind :: [Name] -> PDecl -> PDecl
384+impBind :: [(Name, PTerm)] -> PDecl -> PDecl
350385impBind vs (PTy d ds syn fc opts n fc' t)
351− = PTy d ds syn fc opts n fc' (doImpBind (vs \\ boundIn t) t)
386+ = PTy d ds syn fc opts n fc'
387+ (doImpBind (filter (\(n, ty) -> n `notElem` boundIn t) vs) t)
352388 where
353389 doImpBind [] ty = ty
354− doImpBind (n : ns) ty = PPi impl n NoFC Placeholder (doImpBind ns ty)
390+ doImpBind ((n, argty) : ns) ty
391+ = PPi impl n NoFC argty (doImpBind ns ty)
355392
356393 boundIn (PPi _ n _ _ sc) = n : boundIn sc
357394 boundIn _ = []
358395
396+getTypeIn :: IState -> Name -> Type -> PTerm
397+getTypeIn ist n (Bind x b sc)
398+ | n == x = delab ist (binderTy b)
399+ | otherwise = getTypeIn ist n (substV (P Ref x Erased) sc)
400+getTypeIn ist n tm = Placeholder
401+
402+toImp fc n = pimp n (PRef fc [] n) True
359403
360404-- Propagate class parameters to method bodies, if they're not already
361−-- there
405+-- there, and they are needed (i.e. appear in method's type)
362406addParams :: [Name] -> PDecl -> PDecl
363407addParams ps (PClauses fc opts n cs) = PClauses fc opts n (map addCParams cs)
364408 where
@@ -369,13 +413,12 @@ addParams ps (PClauses fc opts n cs) = PClauses fc opts n (map addCParams cs)
369413 (map (addParams ps) ds)
370414 addCParams c = c
371415
372− addTmParams ps (PRef fc hls n) = PApp fc (PRef fc hls n) (map toImp ps)
416+ addTmParams ps (PRef fc hls n)
417+ = PApp fc (PRef fc hls n) (map (toImp fc) ps)
373418 addTmParams ps (PApp fc ap@(PRef fc' hls n) args)
374− = PApp fc ap (mergePs (map toImp ps) args)
419+ = PApp fc ap (mergePs (map (toImp fc) ps) args)
375420 addTmParams ps tm = tm
376421
377− toImp n = pimp n (PRef fc [] n) True
378−
379422 mergePs [] args = args
380423 mergePs (p : ps) args
381424 | isImplicit p,