@@ -1541,6 +1541,8 @@ elabClass info syn doc fc constraints tn ps ds
15411541 (map (pexp . PRef fc) (map fst ps))
15421542 -- build data declaration
15431543 let mdecls = filter tydecl ds -- method declarations
1544+ let idecls = filter instdecl ds -- default superclass instance declarations
1545+ mapM_ checkDefaultSuperclassInstance idecls
15441546 let mnames = map getMName mdecls
15451547 logLvl 2 $ "Building methods " ++ show mnames
15461548 ims <- mapM (tdecl mnames) mdecls
@@ -1549,7 +1551,7 @@ elabClass info syn doc fc constraints tn ps ds
15491551 let (methods, imethods)
15501552 = unzip (map (\ ( x,y,z) -> (x, y)) ims)
15511553 let defaults = map (\ (x, (y, z)) -> (x,y)) defs
1552− addClass tn (CI cn (map nodoc imethods) defaults (map fst ps) [])
1554+ addClass tn (CI cn (map nodoc imethods) defaults idecls (map fst ps) [])
15531555 -- build instance constructor type
15541556 -- decorate names of functions to ensure they can't be referred
15551557 -- to elsewhere in the class declaration
@@ -1571,7 +1573,6 @@ elabClass info syn doc fc constraints tn ps ds
15711573 mapM_ (elabDecl EAll info) (concat fns)
15721574 -- add the default definitions
15731575 mapM_ (elabDecl EAll info) (concat (map (snd.snd) defs))
1574− i <- getIState
15751576 addIBC (IBCClass tn)
15761577 where
15771578 nodoc (n, (_, o, t)) = (n, (o, t))
@@ -1582,6 +1583,17 @@ elabClass info syn doc fc constraints tn ps ds
15821583 mdec (NS x n) = NS (mdec x) n
15831584 mdec x = x
15841585
1586+ -- TODO: probably should normalise
1587+ checkDefaultSuperclassInstance (PInstance _ fc cs n ps _ _ _)
1588+ = do when (not $ null cs) . tclift
1589+ $ tfail (At fc (Msg $ "Default superclass instances can't have constraints."))
1590+ i <- getIState
1591+ let t = PApp fc (PRef fc n) (map pexp ps)
1592+ let isConstrained = any (== t) constraints
1593+ when (not isConstrained) . tclift
1594+ $ tfail (At fc (Msg $ "Default instances must be for a superclass constraint on the containing class."))
1595+ return ()
1596+
15851597 impbind [] x = x
15861598 impbind ((n, ty): ns) x = PPi impl n ty (impbind ns x)
15871599 conbind (ty : ns) x = PPi constraint (MN 0 "class") ty (conbind ns x)
@@ -1613,6 +1625,8 @@ elabClass info syn doc fc constraints tn ps ds
16131625
16141626 tydecl (PTy _ _ _ _ _ _) = True
16151627 tydecl _ = False
1628+ instdecl (PInstance _ _ _ _ _ _ _ _) = True
1629+ instdecl _ = False
16161630 clause (PClauses _ _ _ _) = True
16171631 clause _ = False
16181632
@@ -1698,15 +1712,12 @@ elabInstance info syn fc cs n ps t expn ds
16981712 [c] -> return c
16991713 _ -> ifail $ show fc ++ ":" ++ show n ++ " is not a type class"
17001714 let constraint = PApp fc (PRef fc n) (map pexp ps)
1701− let iname = case expn of
1702− Nothing -> SN (InstanceN n (map show ps))
1703− -- UN ('@':show n ++ "$" ++ show ps)
1704− Just nm -> nm
1715+ let iname = mkiname n ps expn
17051716 nty <- elabType' True info syn "" fc [] iname t
17061717 -- if the instance type matches any of the instances we have already,
17071718 -- and it's not a named instance, then it's overlapping, so report an error
17081719 case expn of
1709− Nothing -> do mapM_ (checkNotOverlapping i (delab i nty))
1720+ Nothing -> do mapM_ (maybe (return ()) overlapping . findOverlapping i (delab i nty))
17101721 (class_instances ci)
17111722 addInstance intInst n iname
17121723 Just _ -> addInstance intInst n iname
@@ -1720,6 +1731,9 @@ elabInstance info syn fc cs n ps t expn ds
17201731 PApp _ _ args -> getWParams args
17211732 _ -> return []) ps
17221733 let pnames = map pname (concat (nub wparams))
1734+ let superclassInstances = map (substInstance ips pnames) (class_default_superclasses ci)
1735+ undefinedSuperclassInstances <- filterM (fmap not . isOverlapping i) superclassInstances
1736+ mapM_ (elabDecl EAll info) undefinedSuperclassInstances
17231737 let all_meths = map (nsroot . fst) (class_methods ci)
17241738 let mtys = map (\ (n, (op, t)) ->
17251739 let t_in = substMatchesShadow ips pnames t
@@ -1746,7 +1760,7 @@ elabInstance info syn fc cs n ps t expn ds
17461760 let idecls = [PClauses fc [Dictionary] iname
17471761 [PClause fc iname lhs [] rhs wb]]
17481762 iLOG (show idecls)
1749− mapM (elabDecl EAll info) idecls
1763+ mapM_ (elabDecl EAll info) idecls
17501764 addIBC (IBCInstance intInst n iname)
17511765-- -- for each constraint, build a top level function to chase it
17521766-- logLvl 5 $ "Building functions"
@@ -1757,20 +1771,50 @@ elabInstance info syn fc cs n ps t expn ds
17571771 [PConstant (AType (ATInt ITNative))] -> True
17581772 _ -> False
17591773
1760− checkNotOverlapping i t n
1761− | take 2 (show n) == "@@" = return ()
1774+ mkiname n' ps' expn' =
1775+ case expn' of
1776+ Nothing -> SN (InstanceN n' (map show ps'))
1777+ Just nm -> nm
1778+
1779+ substInstance ips pnames (PInstance syn _ cs n ps t expn ds)
1780+ = PInstance syn fc cs n (map (substMatchesShadow ips pnames) ps) (substMatchesShadow ips pnames t) expn ds
1781+
1782+ isOverlapping i (PInstance syn _ _ n ps t expn _)
1783+ = case lookupCtxtName n (idris_classes i) of
1784+ [(n, ci)] -> let iname = (mkiname n ps expn) in
1785+ case lookupTy iname (tt_ctxt i) of
1786+ [] -> elabFindOverlapping i ci iname syn t
1787+ (_:_) -> return True
1788+ _ -> return False -- couldn't find class, just let elabInstance fail later
1789+
1790+ -- TODO: largely based upon elabType' - should try to abstract
1791+ elabFindOverlapping i ci iname syn t
1792+ = do ty' <- addUsingConstraints syn fc t
1793+ ty' <- implicit syn iname ty'
1794+ let ty = addImpl i ty'
1795+ ctxt <- getContext
1796+ ((tyT, _, _), _) <-
1797+ tclift $ elaborate ctxt iname (TType (UVal 0)) []
1798+ (errAt "type of " iname (erun fc (build i info False iname ty)))
1799+ ctxt <- getContext
1800+ (cty, _) <- recheckC fc [] tyT
1801+ let nty = normalise ctxt [] cty
1802+ return $ any (isJust . findOverlapping i (delab i nty)) (class_instances ci)
1803+
1804+ findOverlapping i t n
1805+ | take 2 (show n) == "@@" = Nothing
17621806 | otherwise
17631807 = case lookupTy n (tt_ctxt i) of
17641808 [t'] -> let tret = getRetType t
17651809 tret' = getRetType (delab i t') in
17661810 case matchClause i tret' tret of
1767− Right ms -> overlapping tret tret'
1811+ Right ms -> Just tret'
17681812 Left _ -> case matchClause i tret tret' of
1769− Right ms -> overlapping tret tret'
1770− Left _ -> return ()
1771− _ -> return ()
1772− overlapping t t' = tclift $ tfail (At fc (Msg $
1773− "Overlapping instance: " ++ show t' ++ " already defined"))
1813+ Right ms -> Just tret'
1814+ Left _ -> Nothing
1815+ _ -> Nothing
1816+ overlapping t' = tclift $ tfail (At fc (Msg $
1817+ "Overlapping instance: " ++ show t' ++ " already defined"))
17741818 getRetType (PPi _ _ _ sc) = getRetType sc
17751819 getRetType t = t
17761820