@@ -296,10 +296,12 @@ elabClass info syn fc constraints tn ps ds
296296 let ddecl = PData syn fc (PDatadecl tn tty cons)
297297 elabDecl info ddecl
298298 -- for each method, build a top level function
299− fns <- mapM (tfun cn constraint (map fst imethods)) imethods
299+ let usyn = syn { using = ps ++ using syn }
300+ fns <- mapM (tfun cn constraint usyn (map fst imethods)) imethods
300301 mapM_ (elabDecl info) (concat fns)
301302 i <- get
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) })
303305 where
304306 pibind [] x = x
305307 pibind ((n, ty): ns) x = PPi expl n ty (pibind ns x)
@@ -314,7 +316,7 @@ elabClass info syn fc constraints tn ps ds
314316 tdecl (PClauses _ _ _) = fail "No default definitions allowed yet"
315317 tdecl _ = fail "Not allowed in a class declaration"
316318
317− tfun cn c all (m, ty)
319+ tfun cn c syn all (m, ty)
318320 = do let ty' = insertConstraint c ty
319321 let mnames = take (length all) $ map (\x -> MN x "meth") [0..]
320322 let capp = PApp fc (PRef fc cn) (map (pexp . PRef fc) mnames)
@@ -355,8 +357,55 @@ elabClass info syn fc constraints tn ps ds
355357 toExp ns e sc = sc
356358
357359elabInstance :: ElabInfo -> SyntaxInfo ->
358− FC -> Name -> PTerm -> [PDecl] -> Idris ()
359−elabInstance info syn fc n t ds = fail "Not implemented"
360+ FC -> [PTerm] -> Name ->
361+ [PTerm] -> PTerm -> [PDecl] -> Idris ()
362+elabInstance info syn fc cs n ps t ds
363+ = do i <- get
364+ ci <- case lookupCtxt n (idris_classes i) of
365+ Just c -> return c
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))
371+ (class_methods ci)
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]
378+ iLOG (show idecl)
379+ elabDecl info idecl
380+ where
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')
384+ lamBind i _ 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)
390+ = methArgs i sc
391+ methArgs i _ = []
392+
393+ papp fc f [] = f
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
401+ dappname t = t
402+ mkTyDecl (n, t) = PTy syn fc n t
403+
404+ conbind (ty : ns) x = PPi constraint (MN 0 "c") ty (conbind ns x)
405+ conbind [] x = x
406+
407+ coninsert cs (PPi p@(Imp _ _) n t sc) = PPi p n t (coninsert cs sc)
408+ coninsert cs sc = conbind cs sc
360409
361410pbinds (Bind n (PVar t) sc) = do attack; patbind n
362411 pbinds sc
@@ -405,8 +454,9 @@ elabDecl' info (PParams f ns ps) = mapM_ (elabDecl' pinfo) ps
405454 inblock = newb }
406455elabDecl' info (PClass s f cs n ps ds) = do iLOG $ "Elaborating class " ++ show n
407456 elabClass info s f cs n ps ds
408−elabDecl' info (PInstance s f n t ds) = do iLOG $ "Elaborating instance " ++ show n
409− elabInstance info s f n t ds
457+elabDecl' info (PInstance s f cs n ps t ds)
458+ = do iLOG $ "Elaborating instance " ++ show n
459+ elabInstance info s f cs n ps t ds
410460
411461
412462-- elabDecl' info (PImport i) = loadModule i