@@ -296,10 +296,12 @@ elabClass info syn fc constraints tn ps ds
296 let ddecl = PData syn fc (PDatadecl tn tty cons)
296 let ddecl = PData syn fc (PDatadecl tn tty cons)
297 elabDecl info ddecl
297 elabDecl info ddecl
298 -- for each method, build a top level function
298 -- for each method, build a top level function
299+ let usyn = syn { using = ps ++ using syn }
299− fns <- mapM (tfun cn constraint (map fst imethods)) imethods
300+ fns <- mapM (tfun cn constraint usyn (map fst imethods)) imethods
300 mapM_ (elabDecl info) (concat fns)
301 mapM_ (elabDecl info) (concat fns)
302− put (i { idris_classes = addDef tn (map fst methods) (idris_classes i) })
303+ put (i { idris_classes = addDef tn (CI cn imethods (map fst ps))
304+ (idris_classes i) })
304 pibind [] x = x
306 pibind [] x = x
305 pibind ((n, ty): ns) x = PPi expl n ty (pibind ns x)
307 pibind ((n, ty): ns) x = PPi expl n ty (pibind ns x)
@@ -314,7 +316,7 @@ elabClass info syn fc constraints tn ps ds
314 tdecl (PClauses _ _ _) = fail "No default definitions allowed yet"
316 tdecl (PClauses _ _ _) = fail "No default definitions allowed yet"
315 tdecl _ = fail "Not allowed in a class declaration"
317 tdecl _ = fail "Not allowed in a class declaration"
317− tfun cn c all (m, ty)
319+ tfun cn c syn all (m, ty)
318 = do let ty' = insertConstraint c ty
320 = do let ty' = insertConstraint c ty
319 let mnames = take (length all) $ map (\x -> MN x "meth") [0..]
321 let mnames = take (length all) $ map (\x -> MN x "meth") [0..]
320 let capp = PApp fc (PRef fc cn) (map (pexp . PRef fc) mnames)
322 let capp = PApp fc (PRef fc cn) (map (pexp . PRef fc) mnames)
@@ -355,8 +357,55 @@ elabClass info syn fc constraints tn ps ds
355 toExp ns e sc = sc
357 toExp ns e sc = sc
357elabInstance :: ElabInfo -> SyntaxInfo ->
359elabInstance :: ElabInfo -> SyntaxInfo ->
360+ FC -> [PTerm] -> Name ->
358− FC -> Name -> PTerm -> [PDecl] -> Idris ()
361+ [PTerm] -> PTerm -> [PDecl] -> Idris ()
359−elabInstance info syn fc n t ds = fail "Not implemented"
362+elabInstance info syn fc cs n ps t ds
364+ ci <- case lookupCtxt n (idris_classes i) of
366+ Nothing -> fail $ show n ++ " is not a type class"
367+ let iname = UN ['@':show n ++ "$" ++ show ps]
368+ elabType info syn fc iname t
369+ let ips = zip (class_params ci) ps
370+ let mtys = map (\ (n, t) -> (decorate n, coninsert cs $ substMatches ips t))
372+ logLvl 3 (show (mtys, ips))
373+ let wb = map mkTyDecl mtys ++ map decorateid ds
374+ let lhs = PRef fc iname
375+ let rhs = PApp fc (PRef fc (instanceName ci))
376+ (map (pexp . mkMethApp) mtys)
377+ let idecl = PClauses fc iname [PClause iname lhs [] rhs wb]
381+ mkMethApp (n, ty) = lamBind 0 ty (papp fc (PRef fc n) (methArgs 0 ty))
382+ lamBind i (PPi (Constraint _ _) _ _ sc) sc' = lamBind i sc sc'
383+ lamBind i (PPi _ n ty sc) sc' = PLam (MN i "meth") Placeholder (lamBind (i+1) sc sc')
385+ methArgs i (PPi (Imp _ _) n ty sc)
386+ = PImp 0 False n (PRef fc (MN i "meth")) : methArgs (i+1) sc
387+ methArgs i (PPi (Exp _ _) n ty sc)
388+ = PExp 0 False (PRef fc (MN i "meth")) : methArgs (i+1) sc
389+ methArgs i (PPi (Constraint _ _) n ty sc)
394+ papp fc f as = PApp fc f as
395+ decorate (UN (n:ns)) = UN (('!':n) : ns)
396+ decorateid (PTy s f n t) = PTy s f (decorate n) t
397+ decorateid (PClauses f n cs) = PClauses f (decorate n) (map dc cs)
398+ where dc (PClause n t as w ds) = PClause (decorate n) (dappname t) as w ds
399+ dc (PWith n t as w ds) = PWith (decorate n) (dappname t) as w ds
400+ dappname (PApp fc (PRef fc' n) as) = PApp fc (PRef fc' (decorate n)) as
402+ mkTyDecl (n, t) = PTy syn fc n t
404+ conbind (ty : ns) x = PPi constraint (MN 0 "c") ty (conbind ns x)
407+ coninsert cs (PPi p@(Imp _ _) n t sc) = PPi p n t (coninsert cs sc)
408+ coninsert cs sc = conbind cs sc
361pbinds (Bind n (PVar t) sc) = do attack; patbind n
410pbinds (Bind n (PVar t) sc) = do attack; patbind n
362 pbinds sc
411 pbinds sc
@@ -405,8 +454,9 @@ elabDecl' info (PParams f ns ps) = mapM_ (elabDecl' pinfo) ps
405 inblock = newb }
454 inblock = newb }
406elabDecl' info (PClass s f cs n ps ds) = do iLOG $ "Elaborating class " ++ show n
455elabDecl' info (PClass s f cs n ps ds) = do iLOG $ "Elaborating class " ++ show n
407 elabClass info s f cs n ps ds
456 elabClass info s f cs n ps ds
457+elabDecl' info (PInstance s f cs n ps t ds)
408−elabDecl' info (PInstance s f n t ds) = do iLOG $ "Elaborating instance " ++ show n
458+ = do iLOG $ "Elaborating instance " ++ show n
409− elabInstance info s f n t ds
459+ elabInstance info s f cs n ps t ds
412-- elabDecl' info (PImport i) = loadModule i
462-- elabDecl' info (PImport i) = loadModule i