@@ -1541,6 +1541,8 @@ elabClass info syn doc fc constraints tn ps ds
1541 (map (pexp . PRef fc) (map fst ps))
1541 (map (pexp . PRef fc) (map fst ps))
1542 -- build data declaration
1542 -- build data declaration
1543 let mdecls = filter tydecl ds -- method declarations
1543 let mdecls = filter tydecl ds -- method declarations
1544+ let idecls = filter instdecl ds -- default superclass instance declarations
1545+ mapM_ checkDefaultSuperclassInstance idecls
1544 let mnames = map getMName mdecls
1546 let mnames = map getMName mdecls
1545 logLvl 2 $ "Building methods " ++ show mnames
1547 logLvl 2 $ "Building methods " ++ show mnames
1546 ims <- mapM (tdecl mnames) mdecls
1548 ims <- mapM (tdecl mnames) mdecls
@@ -1549,7 +1551,7 @@ elabClass info syn doc fc constraints tn ps ds
1549 let (methods, imethods)
1551 let (methods, imethods)
1550 = unzip (map (\ ( x,y,z) -> (x, y)) ims)
1552 = unzip (map (\ ( x,y,z) -> (x, y)) ims)
1551 let defaults = map (\ (x, (y, z)) -> (x,y)) defs
1553 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) [])
1553 -- build instance constructor type
1555 -- build instance constructor type
1554 -- decorate names of functions to ensure they can't be referred
1556 -- decorate names of functions to ensure they can't be referred
1555 -- to elsewhere in the class declaration
1557 -- to elsewhere in the class declaration
@@ -1571,7 +1573,6 @@ elabClass info syn doc fc constraints tn ps ds
1571 mapM_ (elabDecl EAll info) (concat fns)
1573 mapM_ (elabDecl EAll info) (concat fns)
1572 -- add the default definitions
1574 -- add the default definitions
1573 mapM_ (elabDecl EAll info) (concat (map (snd.snd) defs))
1575 mapM_ (elabDecl EAll info) (concat (map (snd.snd) defs))
1575 addIBC (IBCClass tn)
1576 addIBC (IBCClass tn)
1577 nodoc (n, (_, o, t)) = (n, (o, t))
1578 nodoc (n, (_, o, t)) = (n, (o, t))
@@ -1582,6 +1583,17 @@ elabClass info syn doc fc constraints tn ps ds
1582 mdec (NS x n) = NS (mdec x) n
1583 mdec (NS x n) = NS (mdec x) n
1583 mdec x = x
1584 mdec x = x
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."))
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."))
1585 impbind [] x = x
1597 impbind [] x = x
1586 impbind ((n, ty): ns) x = PPi impl n ty (impbind ns x)
1598 impbind ((n, ty): ns) x = PPi impl n ty (impbind ns x)
1587 conbind (ty : ns) x = PPi constraint (MN 0 "class") ty (conbind ns x)
1599 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
1614 tydecl (PTy _ _ _ _ _ _) = True
1626 tydecl (PTy _ _ _ _ _ _) = True
1615 tydecl _ = False
1627 tydecl _ = False
1628+ instdecl (PInstance _ _ _ _ _ _ _ _) = True
1616 clause (PClauses _ _ _ _) = True
1630 clause (PClauses _ _ _ _) = True
1617 clause _ = False
1631 clause _ = False
@@ -1698,15 +1712,12 @@ elabInstance info syn fc cs n ps t expn ds
1698 [c] -> return c
1712 [c] -> return c
1699 _ -> ifail $ show fc ++ ":" ++ show n ++ " is not a type class"
1713 _ -> ifail $ show fc ++ ":" ++ show n ++ " is not a type class"
1700 let constraint = PApp fc (PRef fc n) (map pexp ps)
1714 let constraint = PApp fc (PRef fc n) (map pexp ps)
1701− let iname = case expn of
1715+ let iname = mkiname n ps expn
1702− Nothing -> SN (InstanceN n (map show ps))
1703− -- UN ('@':show n ++ "$" ++ show ps)
1705 nty <- elabType' True info syn "" fc [] iname t
1716 nty <- elabType' True info syn "" fc [] iname t
1706 -- if the instance type matches any of the instances we have already,
1717 -- if the instance type matches any of the instances we have already,
1707 -- and it's not a named instance, then it's overlapping, so report an error
1718 -- and it's not a named instance, then it's overlapping, so report an error
1708 case expn of
1719 case expn of
1709− Nothing -> do mapM_ (checkNotOverlapping i (delab i nty))
1720+ Nothing -> do mapM_ (maybe (return ()) overlapping . findOverlapping i (delab i nty))
1710 (class_instances ci)
1721 (class_instances ci)
1711 addInstance intInst n iname
1722 addInstance intInst n iname
1712 Just _ -> addInstance intInst n iname
1723 Just _ -> addInstance intInst n iname
@@ -1720,6 +1731,9 @@ elabInstance info syn fc cs n ps t expn ds
1720 PApp _ _ args -> getWParams args
1731 PApp _ _ args -> getWParams args
1721 _ -> return []) ps
1732 _ -> return []) ps
1722 let pnames = map pname (concat (nub wparams))
1733 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
1723 let all_meths = map (nsroot . fst) (class_methods ci)
1737 let all_meths = map (nsroot . fst) (class_methods ci)
1724 let mtys = map (\ (n, (op, t)) ->
1738 let mtys = map (\ (n, (op, t)) ->
1725 let t_in = substMatchesShadow ips pnames t
1739 let t_in = substMatchesShadow ips pnames t
@@ -1746,7 +1760,7 @@ elabInstance info syn fc cs n ps t expn ds
1746 let idecls = [PClauses fc [Dictionary] iname
1760 let idecls = [PClauses fc [Dictionary] iname
1747 [PClause fc iname lhs [] rhs wb]]
1761 [PClause fc iname lhs [] rhs wb]]
1748 iLOG (show idecls)
1762 iLOG (show idecls)
1749− mapM (elabDecl EAll info) idecls
1763+ mapM_ (elabDecl EAll info) idecls
1750 addIBC (IBCInstance intInst n iname)
1764 addIBC (IBCInstance intInst n iname)
1751-- -- for each constraint, build a top level function to chase it
1765-- -- for each constraint, build a top level function to chase it
1752-- logLvl 5 $ "Building functions"
1766-- logLvl 5 $ "Building functions"
@@ -1757,20 +1771,50 @@ elabInstance info syn fc cs n ps t expn ds
1757 [PConstant (AType (ATInt ITNative))] -> True
1771 [PConstant (AType (ATInt ITNative))] -> True
1758 _ -> False
1772 _ -> False
1760− checkNotOverlapping i t n
1774+ mkiname n' ps' expn' =
1776+ Nothing -> SN (InstanceN n' (map show ps'))
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
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
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'
1796+ ((tyT, _, _), _) <-
1797+ tclift $ elaborate ctxt iname (TType (UVal 0)) []
1798+ (errAt "type of " iname (erun fc (build i info False iname ty)))
1800+ (cty, _) <- recheckC fc [] tyT
1801+ let nty = normalise ctxt [] cty
1802+ return $ any (isJust . findOverlapping i (delab i nty)) (class_instances ci)
1804+ findOverlapping i t n
1761− | take 2 (show n) == "@@" = return ()
1805+ | take 2 (show n) == "@@" = Nothing
1762 | otherwise
1806 | otherwise
1763 = case lookupTy n (tt_ctxt i) of
1807 = case lookupTy n (tt_ctxt i) of
1764 [t'] -> let tret = getRetType t
1808 [t'] -> let tret = getRetType t
1765 tret' = getRetType (delab i t') in
1809 tret' = getRetType (delab i t') in
1766 case matchClause i tret' tret of
1810 case matchClause i tret' tret of
1767− Right ms -> overlapping tret tret'
1811+ Right ms -> Just tret'
1768 Left _ -> case matchClause i tret tret' of
1812 Left _ -> case matchClause i tret tret' of
1769− Right ms -> overlapping tret tret'
1813+ Right ms -> Just tret'
1770− Left _ -> return ()
1814+ Left _ -> Nothing
1771− _ -> return ()
1815+ _ -> Nothing
1772− overlapping t t' = tclift $ tfail (At fc (Msg $
1816+ overlapping t' = tclift $ tfail (At fc (Msg $
1773− "Overlapping instance: " ++ show t' ++ " already defined"))
1817+ "Overlapping instance: " ++ show t' ++ " already defined"))
1774 getRetType (PPi _ _ _ sc) = getRetType sc
1818 getRetType (PPi _ _ _ sc) = getRetType sc
1775 getRetType t = t
1819 getRetType t = t