@@ -1542,6 +1542,8 @@ elabClass info syn doc fc constraints tn ps ds
1542 (map (pexp . PRef fc) (map fst ps))
1542 (map (pexp . PRef fc) (map fst ps))
1543 -- build data declaration
1543 -- build data declaration
1544 let mdecls = filter tydecl ds -- method declarations
1544 let mdecls = filter tydecl ds -- method declarations
1545+ let idecls = filter instdecl ds -- default superclass instance declarations
1546+ mapM_ checkDefaultSuperclassInstance idecls
1545 let mnames = map getMName mdecls
1547 let mnames = map getMName mdecls
1546 logLvl 2 $ "Building methods " ++ show mnames
1548 logLvl 2 $ "Building methods " ++ show mnames
1547 ims <- mapM (tdecl mnames) mdecls
1549 ims <- mapM (tdecl mnames) mdecls
@@ -1550,7 +1552,7 @@ elabClass info syn doc fc constraints tn ps ds
1550 let (methods, imethods)
1552 let (methods, imethods)
1551 = unzip (map (\ ( x,y,z) -> (x, y)) ims)
1553 = unzip (map (\ ( x,y,z) -> (x, y)) ims)
1552 let defaults = map (\ (x, (y, z)) -> (x,y)) defs
1554 let defaults = map (\ (x, (y, z)) -> (x,y)) defs
1553− addClass tn (CI cn (map nodoc imethods) defaults (map fst ps) [])
1555+ addClass tn (CI cn (map nodoc imethods) defaults idecls (map fst ps) [])
1554 -- build instance constructor type
1556 -- build instance constructor type
1555 -- decorate names of functions to ensure they can't be referred
1557 -- decorate names of functions to ensure they can't be referred
1556 -- to elsewhere in the class declaration
1558 -- to elsewhere in the class declaration
@@ -1572,7 +1574,6 @@ elabClass info syn doc fc constraints tn ps ds
1572 mapM_ (elabDecl EAll info) (concat fns)
1574 mapM_ (elabDecl EAll info) (concat fns)
1573 -- add the default definitions
1575 -- add the default definitions
1574 mapM_ (elabDecl EAll info) (concat (map (snd.snd) defs))
1576 mapM_ (elabDecl EAll info) (concat (map (snd.snd) defs))
1576 addIBC (IBCClass tn)
1577 addIBC (IBCClass tn)
1578 nodoc (n, (_, o, t)) = (n, (o, t))
1579 nodoc (n, (_, o, t)) = (n, (o, t))
@@ -1583,6 +1584,17 @@ elabClass info syn doc fc constraints tn ps ds
1583 mdec (NS x n) = NS (mdec x) n
1584 mdec (NS x n) = NS (mdec x) n
1584 mdec x = x
1585 mdec x = x
1587+ -- TODO: probably should normalise
1588+ checkDefaultSuperclassInstance (PInstance _ fc cs n ps _ _ _)
1589+ = do when (not $ null cs) . tclift
1590+ $ tfail (At fc (Msg $ "Default superclass instances can't have constraints."))
1592+ let t = PApp fc (PRef fc n) (map pexp ps)
1593+ let isConstrained = any (== t) constraints
1594+ when (not isConstrained) . tclift
1595+ $ tfail (At fc (Msg $ "Default instances must be for a superclass constraint on the containing class."))
1586 impbind [] x = x
1598 impbind [] x = x
1587 impbind ((n, ty): ns) x = PPi impl n ty (impbind ns x)
1599 impbind ((n, ty): ns) x = PPi impl n ty (impbind ns x)
1588 conbind (ty : ns) x = PPi constraint (MN 0 "class") ty (conbind ns x)
1600 conbind (ty : ns) x = PPi constraint (MN 0 "class") ty (conbind ns x)
@@ -1614,6 +1626,8 @@ elabClass info syn doc fc constraints tn ps ds
1615 tydecl (PTy _ _ _ _ _ _) = True
1627 tydecl (PTy _ _ _ _ _ _) = True
1616 tydecl _ = False
1628 tydecl _ = False
1629+ instdecl (PInstance _ _ _ _ _ _ _ _) = True
1617 clause (PClauses _ _ _ _) = True
1631 clause (PClauses _ _ _ _) = True
1618 clause _ = False
1632 clause _ = False
@@ -1699,15 +1713,12 @@ elabInstance info syn fc cs n ps t expn ds
1699 [c] -> return c
1713 [c] -> return c
1700 _ -> ifail $ show fc ++ ":" ++ show n ++ " is not a type class"
1714 _ -> ifail $ show fc ++ ":" ++ show n ++ " is not a type class"
1701 let constraint = PApp fc (PRef fc n) (map pexp ps)
1715 let constraint = PApp fc (PRef fc n) (map pexp ps)
1702− let iname = case expn of
1716+ let iname = mkiname n ps expn
1703− Nothing -> SN (InstanceN n (map show ps))
1704− -- UN ('@':show n ++ "$" ++ show ps)
1706 nty <- elabType' True info syn "" fc [] iname t
1717 nty <- elabType' True info syn "" fc [] iname t
1707 -- if the instance type matches any of the instances we have already,
1718 -- if the instance type matches any of the instances we have already,
1708 -- and it's not a named instance, then it's overlapping, so report an error
1719 -- and it's not a named instance, then it's overlapping, so report an error
1709 case expn of
1720 case expn of
1710− Nothing -> do mapM_ (checkNotOverlapping i (delab i nty))
1721+ Nothing -> do mapM_ (maybe (return ()) overlapping . findOverlapping i (delab i nty))
1711 (class_instances ci)
1722 (class_instances ci)
1712 addInstance intInst n iname
1723 addInstance intInst n iname
1713 Just _ -> addInstance intInst n iname
1724 Just _ -> addInstance intInst n iname
@@ -1721,6 +1732,9 @@ elabInstance info syn fc cs n ps t expn ds
1721 PApp _ _ args -> getWParams args
1732 PApp _ _ args -> getWParams args
1722 _ -> return []) ps
1733 _ -> return []) ps
1723 let pnames = map pname (concat (nub wparams))
1734 let pnames = map pname (concat (nub wparams))
1735+ let superclassInstances = map (substInstance ips pnames) (class_default_superclasses ci)
1736+ undefinedSuperclassInstances <- filterM (fmap not . isOverlapping i) superclassInstances
1737+ mapM_ (elabDecl EAll info) undefinedSuperclassInstances
1724 let all_meths = map (nsroot . fst) (class_methods ci)
1738 let all_meths = map (nsroot . fst) (class_methods ci)
1725 let mtys = map (\ (n, (op, t)) ->
1739 let mtys = map (\ (n, (op, t)) ->
1726 let t_in = substMatchesShadow ips pnames t
1740 let t_in = substMatchesShadow ips pnames t
@@ -1747,7 +1761,7 @@ elabInstance info syn fc cs n ps t expn ds
1747 let idecls = [PClauses fc [Dictionary] iname
1761 let idecls = [PClauses fc [Dictionary] iname
1748 [PClause fc iname lhs [] rhs wb]]
1762 [PClause fc iname lhs [] rhs wb]]
1749 iLOG (show idecls)
1763 iLOG (show idecls)
1750− mapM (elabDecl EAll info) idecls
1764+ mapM_ (elabDecl EAll info) idecls
1751 addIBC (IBCInstance intInst n iname)
1765 addIBC (IBCInstance intInst n iname)
1752-- -- for each constraint, build a top level function to chase it
1766-- -- for each constraint, build a top level function to chase it
1753-- logLvl 5 $ "Building functions"
1767-- logLvl 5 $ "Building functions"
@@ -1758,20 +1772,50 @@ elabInstance info syn fc cs n ps t expn ds
1758 [PConstant (AType (ATInt ITNative))] -> True
1772 [PConstant (AType (ATInt ITNative))] -> True
1759 _ -> False
1773 _ -> False
1761− checkNotOverlapping i t n
1775+ mkiname n' ps' expn' =
1777+ Nothing -> SN (InstanceN n' (map show ps'))
1780+ substInstance ips pnames (PInstance syn _ cs n ps t expn ds)
1781+ = PInstance syn fc cs n (map (substMatchesShadow ips pnames) ps) (substMatchesShadow ips pnames t) expn ds
1783+ isOverlapping i (PInstance syn _ _ n ps t expn _)
1784+ = case lookupCtxtName n (idris_classes i) of
1785+ [(n, ci)] -> let iname = (mkiname n ps expn) in
1786+ case lookupTy iname (tt_ctxt i) of
1787+ [] -> elabFindOverlapping i ci iname syn t
1788+ (_:_) -> return True
1789+ _ -> return False -- couldn't find class, just let elabInstance fail later
1791+ -- TODO: largely based upon elabType' - should try to abstract
1792+ elabFindOverlapping i ci iname syn t
1793+ = do ty' <- addUsingConstraints syn fc t
1794+ ty' <- implicit syn iname ty'
1795+ let ty = addImpl i ty'
1797+ ((tyT, _, _), _) <-
1798+ tclift $ elaborate ctxt iname (TType (UVal 0)) []
1799+ (errAt "type of " iname (erun fc (build i info False iname ty)))
1801+ (cty, _) <- recheckC fc [] tyT
1802+ let nty = normalise ctxt [] cty
1803+ return $ any (isJust . findOverlapping i (delab i nty)) (class_instances ci)
1805+ findOverlapping i t n
1762− | take 2 (show n) == "@@" = return ()
1806+ | take 2 (show n) == "@@" = Nothing
1763 | otherwise
1807 | otherwise
1764 = case lookupTy n (tt_ctxt i) of
1808 = case lookupTy n (tt_ctxt i) of
1765 [t'] -> let tret = getRetType t
1809 [t'] -> let tret = getRetType t
1766 tret' = getRetType (delab i t') in
1810 tret' = getRetType (delab i t') in
1767 case matchClause i tret' tret of
1811 case matchClause i tret' tret of
1768− Right ms -> overlapping tret tret'
1812+ Right ms -> Just tret'
1769 Left _ -> case matchClause i tret tret' of
1813 Left _ -> case matchClause i tret tret' of
1770− Right ms -> overlapping tret tret'
1814+ Right ms -> Just tret'
1771− Left _ -> return ()
1815+ Left _ -> Nothing
1772− _ -> return ()
1816+ _ -> Nothing
1773− overlapping t t' = tclift $ tfail (At fc (Msg $
1817+ overlapping t' = tclift $ tfail (At fc (Msg $
1774− "Overlapping instance: " ++ show t' ++ " already defined"))
1818+ "Overlapping instance: " ++ show t' ++ " already defined"))
1775 getRetType (PPi _ _ _ sc) = getRetType sc
1819 getRetType (PPi _ _ _ sc) = getRetType sc
1776 getRetType t = t
1820 getRetType t = t