@@ -55,15 +55,17 @@ elabInstance :: ElabInfo -> SyntaxInfo ->
55 [(Name, Docstring (Either Err PTerm))] ->
55 [(Name, Docstring (Either Err PTerm))] ->
56 ElabWhat -> -- phase
56 ElabWhat -> -- phase
57 FC -> [(Name, PTerm)] -> -- constraints
57 FC -> [(Name, PTerm)] -> -- constraints
58+ [Name] -> -- parent dictionary names (optionally)
58 Accessibility ->
59 Accessibility ->
60 Name -> -- the class
61 Name -> -- the class
61 FC -> -- precise location of class name
62 FC -> -- precise location of class name
62 [PTerm] -> -- class parameters (i.e. instance)
63 [PTerm] -> -- class parameters (i.e. instance)
64+ [(Name, PTerm)] -> -- Extra arguments in scope (e.g. instance in where block)
63 PTerm -> -- full instance type
65 PTerm -> -- full instance type
64 Maybe Name -> -- explicit name
66 Maybe Name -> -- explicit name
65 [PDecl] -> Idris ()
67 [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
67 ist <- getIState
69 ist <- getIState
68 (n, ci) <- case lookupCtxtName n (idris_classes ist) of
70 (n, ci) <- case lookupCtxtName n (idris_classes ist) of
69 [c] -> return c
71 [c] -> return c
@@ -79,7 +81,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)
82 let emptyclass = null (class_methods ci)
81 when (what /= EDefns) $ do
83 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)
83 -- if the instance type matches any of the instances we have already,
86 -- 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
87 -- and it's not a named instance, then it's overlapping, so report an error
85 case expn of
88 case expn of
@@ -88,6 +91,9 @@ elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
88 addInstance intInst True n iname
91 addInstance intInst True n iname
89 Just _ -> addInstance intInst False n iname
92 Just _ -> addInstance intInst False n iname
90 when (what /= ETypes && (not (null ds && not emptyclass))) $ do
93 when (what /= ETypes && (not (null ds && not emptyclass))) $ do
94+ -- Add the parent implementation names to the privileged set
95+ oldOpen <- addOpenImpl parents
91 let ips = zip (class_params ci) ps
97 let ips = zip (class_params ci) ps
92 let ns = case n of
98 let ns = case n of
93 NS n ns' -> ns'
99 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
105 let superclassInstances = map (substInstance ips pnames) (class_default_superclasses ci)
111 let superclassInstances = map (substInstance ips pnames) (class_default_superclasses ci)
106 undefinedSuperclassInstances <- filterM (fmap not . isOverlapping ist) superclassInstances
112 undefinedSuperclassInstances <- filterM (fmap not . isOverlapping ist) superclassInstances
107 mapM_ (rec_elabDecl info EAll info) undefinedSuperclassInstances
113 mapM_ (rec_elabDecl info EAll info) undefinedSuperclassInstances
116+ -- Bring variables in instance head into scope when building the
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
108 let all_meths = map (nsroot . fst) (class_methods ci)
125 let all_meths = map (nsroot . fst) (class_methods ci)
109 let mtys = map (\ (n, (op, t)) ->
126 let mtys = map (\ (n, (op, t)) ->
110 let t_in = substMatchesShadow ips pnames t
127 let t_in = substMatchesShadow ips pnames t
111− mnamemap = map (\n -> (n, PRef fc [] (decorate ns iname n)))
129+ map (\n -> (n, PApp fc (PRef fc [] (decorate ns iname n))
130+ (map (toImp fc) headVars)))
112 all_meths
131 all_meths
113 t' = substMatchesShadow mnamemap pnames t_in in
132 t' = substMatchesShadow mnamemap pnames t_in in
114 (decorate ns iname n,
133 (decorate ns iname n,
115− op, coninsert cs t', t'))
134+ op, coninsert cs pextra t', t'))
116 (class_methods ci)
135 (class_methods ci)
117 logElab 3 (show (mtys, ips))
136 logElab 3 (show (mtys, ips))
118 logElab 5 ("Before defaults: " ++ show ds ++ "\n" ++ show (map fst (class_methods ci)))
137 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
123 mapM_ (warnMissing ds' ns iname) (map fst (class_methods ci))
142 mapM_ (warnMissing ds' ns iname) (map fst (class_methods ci))
124 mapM_ (checkInClass (map fst (class_methods ci))) (concatMap defined ds')
143 mapM_ (checkInClass (map fst (class_methods ci))) (concatMap defined ds')
125 let wbTys = map mkTyDecl mtys
144 let wbTys = map mkTyDecl mtys
126− let wbVals = map (decorateid (decorate ns iname)) ds'
145+ let wbVals_orig = map (decorateid (decorate ns iname)) ds'
147+ let wbVals = map (expandParamsD False ist id pextra (map methName mtys)) wbVals_orig
127 let wb = wbTys ++ wbVals
148 let wb = wbTys ++ wbVals
128 logElab 3 $ "Method types " ++ showSep "\n" (map (show . showDeclImp verbosePPOption . mkTyDecl) mtys)
150 logElab 3 $ "Method types " ++ showSep "\n" (map (show . showDeclImp verbosePPOption . mkTyDecl) mtys)
129 logElab 3 $ "Instance is " ++ show ps ++ " implicits " ++
151 logElab 3 $ "Instance is " ++ show ps ++ " implicits " ++
130 show (concat (nub wparams))
152 show (concat (nub wparams))
132− -- Bring variables in instance head into scope when building the
154+
134− let headVars = nub $ concatMap getHeadVars ps
136 let lhsImps = map (\n -> pimp n (PRef fc [] n) True) headVars
155 let lhsImps = map (\n -> pimp n (PRef fc [] n) True) headVars
138− let lhs = PApp fc (PRef fc [] iname) lhsImps
157+ let lhs = PApp fc (PRef fc [] iname) (lhsImps ++ map (toExp .fst) pextra)
139 let rhs = PApp fc (PRef fc [] (instanceCtorName ci))
158 let rhs = PApp fc (PRef fc [] (instanceCtorName ci))
140 (map (pexp . (mkMethApp lhsImps)) mtys)
159 (map (pexp . (mkMethApp lhsImps)) mtys)
@@ -144,12 +163,13 @@ elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
145 push_estack iname True
164 push_estack iname True
146 logElab 3 ("Method types: " ++ show wbTys)
165 logElab 3 ("Method types: " ++ show wbTys)
166+ logElab 3 ("Method bodies (before params): " ++ show wbVals_orig)
147 logElab 3 ("Method bodies: " ++ show wbVals)
167 logElab 3 ("Method bodies: " ++ show wbVals)
149 let idecls = [PClauses fc totopts iname
169 let idecls = [PClauses fc totopts iname
150 [PClause fc iname lhs [] rhs []]]
170 [PClause fc iname lhs [] rhs []]]
152− mapM_ (rec_elabDecl info EAll info) (map (impBind headVars) wbTys)
172+ mapM_ (rec_elabDecl info EAll info) (map (impBind headVarTypes) wbTys)
153 mapM_ (rec_elabDecl info EAll info) idecls
173 mapM_ (rec_elabDecl info EAll info) idecls
155 ctxt <- getContext
175 ctxt <- getContext
@@ -169,6 +189,8 @@ elabInstance info syn doc argDocs what fc cs acc opts n nfc ps t expn ds = do
169 mapM_ (rec_elabDecl info EAll info) wbVals'
189 mapM_ (rec_elabDecl info EAll info) wbVals'
171 pop_estack
191 pop_estack
172-- totalityCheckBlock
194-- totalityCheckBlock
174 checkInjectiveArgs fc n (class_determiners ci) (lookupTyExact iname (tt_ctxt ist))
196 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
186 Just m -> sNS (SN (sInstanceN n' (map show ps'))) m
208 Just m -> sNS (SN (sInstanceN n' (map show ps'))) m
187 Just nm -> nm
209 Just nm -> nm
189− substInstance ips pnames (PInstance doc argDocs syn _ cs acc opts n nfc ps t expn ds)
211+ substInstance ips pnames (PInstance doc argDocs syn _ cs parents acc opts n nfc ps pextra 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
212+ = PInstance doc argDocs syn fc cs parents acc opts n nfc
213+ (map (substMatchesShadow ips pnames) ps)
215+ (substMatchesShadow ips pnames t) expn ds
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 _)
193 = case lookupCtxtName n (idris_classes i) of
218 = case lookupCtxtName n (idris_classes i) of
194 [(n, ci)] -> let iname = (mkiname n (namespace info) ps expn) in
219 [(n, ci)] -> let iname = (mkiname n (namespace info) ps expn) in
195 case lookupTy iname (tt_ctxt i) of
220 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
246 map snd (filter (\(i, _) -> i `elem` dets) a')
271 map snd (filter (\(i, _) -> i `elem` dets) a')
247 keepDets dets t = t
272 keepDets dets t = t
274+ methName (n, _, _, _) = n
275+ toExp n = pexp (PRef fc [] n)
249− mkMethApp ps (n, _, _, ty)
277+ mkMethApp ps (n, _, _, ty)
250− = lamBind 0 ty (papp fc (PRef fc [] n)
278+ = lamBind 0 ty (papp fc (PRef fc [] n)
251− (ps ++ methArgs 0 ty))
279+ (ps ++ map (toExp . fst) pextra ++ methArgs 0 ty))
281+ needed is p = pname p `elem` map pname is
252 lamBind i (PPi (Constraint _ _) _ _ _ sc) sc'
283 lamBind i (PPi (Constraint _ _) _ _ _ sc) sc'
253 = PLam fc (sMN i "meth") NoFC Placeholder (lamBind (i+1) sc sc')
284 = PLam fc (sMN i "meth") NoFC Placeholder (lamBind (i+1) sc sc')
254 lamBind i (PPi _ n _ ty sc) sc'
285 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
275 _ -> return ps'
306 _ -> return ps'
276 getWParams (_ : ps) = getWParams ps
307 getWParams (_ : ps) = getWParams ps
278− decorate ns iname (UN nm)
279− = NS (SN (WhereN 0 iname (SN (MethodN (UN nm))))) ns
280 decorate ns iname (NS (UN nm) s)
309 decorate ns iname (NS (UN nm) s)
281 = NS (SN (WhereN 0 iname (SN (MethodN (UN nm))))) ns
310 = 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
283 mkTyDecl (n, op, t, _)
314 mkTyDecl (n, op, t, _)
284 = PTy emptyDocstring [] syn fc op n NoFC
315 = 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
288 conbind ((c,ty) : ns) x = PPi constraint c NoFC ty (conbind ns x)
319 conbind ((c,ty) : ns) x = PPi constraint c NoFC ty (conbind ns x)
289 conbind [] x = x
320 conbind [] x = x
322+ extrabind :: [(Name, PTerm)] -> PTerm -> PTerm
323+ extrabind ((c,ty) : ns) x = PPi expl c NoFC ty (extrabind ns x)
291− coninsert :: [(Name, PTerm)] -> PTerm -> PTerm
326+ coninsert :: [(Name, PTerm)] -> [(Name, PTerm)] -> PTerm -> PTerm
292− coninsert cs (PPi p@(Imp _ _ _ _ _) n fc t sc) = PPi p n fc t (coninsert cs sc)
327+ coninsert cs ex (PPi p@(Imp _ _ _ _ _) n fc t sc) = PPi p n fc t (coninsert cs ex sc)
293− coninsert cs sc = conbind cs sc
328+ coninsert cs ex sc = conbind cs (extrabind ex sc)
295 -- Reorder declarations to be in the same order as defined in the
330 -- Reorder declarations to be in the same order as defined in the
296 -- class declaration (important so that we insert default definitions
331 -- class declaration (important so that we insert default definitions
@@ -346,19 +381,28 @@ getHeadVars (PDPair _ _ _ l t r) = getHeadVars l ++ getHeadVars t ++ getHeadVars
346getHeadVars _ = []
381getHeadVars _ = []
348-- Implicitly bind variables from the instance head in method types
383-- Implicitly bind variables from the instance head in method types
349−impBind :: [Name] -> PDecl -> PDecl
384+impBind :: [(Name, PTerm)] -> PDecl -> PDecl
350impBind vs (PTy d ds syn fc opts n fc' t)
385impBind 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)
353 doImpBind [] ty = ty
389 doImpBind [] ty = ty
390+ doImpBind ((n, argty) : ns) ty
354− doImpBind (n : ns) ty = PPi impl n NoFC Placeholder (doImpBind ns ty)
391+ = PPi impl n NoFC argty (doImpBind ns ty)
356 boundIn (PPi _ n _ _ sc) = n : boundIn sc
393 boundIn (PPi _ n _ _ sc) = n : boundIn sc
357 boundIn _ = []
394 boundIn _ = []
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
402+toImp fc n = pimp n (PRef fc [] n) True
360-- Propagate class parameters to method bodies, if they're not already
404-- 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)
362addParams :: [Name] -> PDecl -> PDecl
406addParams :: [Name] -> PDecl -> PDecl
363addParams ps (PClauses fc opts n cs) = PClauses fc opts n (map addCParams cs)
407addParams ps (PClauses fc opts n cs) = PClauses fc opts n (map addCParams cs)
@@ -369,13 +413,12 @@ addParams ps (PClauses fc opts n cs) = PClauses fc opts n (map addCParams cs)
369 (map (addParams ps) ds)
413 (map (addParams ps) ds)
370 addCParams c = c
414 addCParams c = c
416+ addTmParams ps (PRef fc hls n)
372− addTmParams ps (PRef fc hls n) = PApp fc (PRef fc hls n) (map toImp ps)
417+ = PApp fc (PRef fc hls n) (map (toImp fc) ps)
373 addTmParams ps (PApp fc ap@(PRef fc' hls n) args)
418 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)
375 addTmParams ps tm = tm
420 addTmParams ps tm = tm
377− toImp n = pimp n (PRef fc [] n) True
379 mergePs [] args = args
422 mergePs [] args = args
380 mergePs (p : ps) args
423 mergePs (p : ps) args
381 | isImplicit p,
424 | isImplicit p,