@@ -136,11 +136,11 @@ elabType' norm info syn doc argDocs fc opts n ty' = {- let ty' = piBind (params
136 (cty, ty, inacc) <- buildType info syn fc opts n ty'
136 (cty, ty, inacc) <- buildType info syn fc opts n ty'
138 addStatics n cty ty
138 addStatics n cty ty
139− logLvl 2 $ "Rechecked to " ++ show cty
140 let nty = cty -- normalise ctxt [] cty
139 let nty = cty -- normalise ctxt [] cty
141 -- if the return type is something coinductive, freeze the definition
140 -- if the return type is something coinductive, freeze the definition
142 ctxt <- getContext
141 ctxt <- getContext
143 let nty' = normalise ctxt [] nty
142 let nty' = normalise ctxt [] nty
143+ logLvl 2 $ "Rechecked to " ++ show nty'
145 -- Add normalised type to internals
145 -- Add normalised type to internals
146 i <- getIState
146 i <- getIState
@@ -863,19 +863,31 @@ elabRecord info syn doc fc tyn ty opts cdoc cn cty_in
863 _ -> ifail "Something went inexplicably wrong"
863 _ -> ifail "Something went inexplicably wrong"
864 cimp <- case lookupCtxt cn (idris_implicits i) of
864 cimp <- case lookupCtxt cn (idris_implicits i) of
865 [imps] -> return imps
865 [imps] -> return imps
866+ ppos <- case lookupCtxt tyn (idris_datatypes i) of
867+ [ti] -> return $ param_pos ti
866− let ptys = getProjs [] (renameBs cimp cty)
868+ let cty_imp = renameBs cimp cty
869+ let ptys = getProjs [] cty_imp
867 let ptys_u = getProjs [] cty
870 let ptys_u = getProjs [] cty
868− let recty = getRecTy cty
871+ let recty = getRecTy cty_imp
872+ let recty_u = getRecTy cty
874+ let paramNames = getPNames recty ppos
870 -- rename indices when we generate the getter/setter types, so
876 -- rename indices when we generate the getter/setter types, so
871 -- that they don't clash with the names of the projections
877 -- that they don't clash with the names of the projections
872 -- we're generating
878 -- we're generating
873− let index_names_in = getRecNameMap "_in" recty
879+ let index_names_in = getRecNameMap "_in" ppos recty
874 let recty_in = substMatches index_names_in recty
880 let recty_in = substMatches index_names_in recty
876− logLvl 1 $ show (recty, ptys)
882+ logLvl 3 $ show (recty, recty_u, ppos, paramNames, ptys)
883+ -- Substitute indices with projection functions, and parameters with
884+ -- the updated parameter name
877− let substs = map (\ (n, _) -> (n, PApp fc (PRef fc n)
885+ let substs = map (\ (n, _) ->
886+ if n `elem` paramNames
887+ then (n, PRef fc (mkp n))
888+ else (n, PApp fc (PRef fc n)
878− [pexp (PRef fc rec)])) ptys
889+ [pexp (PRef fc rec)]))
880 -- Generate projection functions
892 -- Generate projection functions
881 proj_decls <- mapM (mkProj recty_in substs cimp) (zip ptys [0..])
893 proj_decls <- mapM (mkProj recty_in substs cimp) (zip ptys [0..])
@@ -884,7 +896,7 @@ elabRecord info syn doc fc tyn ty opts cdoc cn cty_in
884 let implBinds = getImplB id cty'
896 let implBinds = getImplB id cty'
886 -- Generate update functions
898 -- Generate update functions
887− update_decls <- mapM (mkUpdate recty index_names_in extraImpls
899+ update_decls <- mapM (mkUpdate recty_u index_names_in extraImpls
888 (getFieldNames cty')
900 (getFieldNames cty')
889 implBinds (length nonImp)) (zip nonImp [0..])
901 implBinds (length nonImp)) (zip nonImp [0..])
890 mapM_ (elabDecl EAll info) (concat proj_decls)
902 mapM_ (elabDecl EAll info) (concat proj_decls)
@@ -896,6 +908,14 @@ elabRecord info syn doc fc tyn ty opts cdoc cn cty_in
896 isNonImp (PExp _ _ _ _, a) = Just a
908 isNonImp (PExp _ _ _ _, a) = Just a
897 isNonImp _ = Nothing
909 isNonImp _ = Nothing
911+ getPNames (PApp _ _ as) ppos = getpn as ppos
914+ getpn as (i:is) | length as > i,
915+ PRef _ n <- getTm (as!!i) = n : getpn as is
916+ | otherwise = getpn as is
899 tryElabDecl info (fn, ty, val)
919 tryElabDecl info (fn, ty, val)
900 = do i <- getIState
920 = do i <- getIState
901 idrisCatch (do elabDecl' EAll info ty
921 idrisCatch (do elabDecl' EAll info ty
@@ -932,15 +952,21 @@ elabRecord info syn doc fc tyn ty opts cdoc cn cty_in
932 getRecTy (PPi _ n ty s) = getRecTy s
952 getRecTy (PPi _ n ty s) = getRecTy s
933 getRecTy t = t
953 getRecTy t = t
955+ -- make sure we pick a consistent name for parameters; any name will do
935− getRecNameMap x (PApp fc t args) = mapMaybe (toMN . getTm) args
957+ getRecNameMap x ppos (PApp fc t args)
958+ = mapMaybe toMN (zip [0..] (map getTm args))
961+ | i `elem` ppos = Just (n, PRef fc (mkp n))
937− toMN (PRef fc n) = Just (n, PRef fc (sMN 0 (show n ++ x)))
962+ | otherwise = Just (n, PRef fc (sMN 0 (show n ++ x)))
938 toMN _ = Nothing
963 toMN _ = Nothing
939− getRecNameMap x _ = []
964+ getRecNameMap x _ _ = []
941 rec = sMN 0 "rec"
966 rec = sMN 0 "rec"
968+ -- only UNs propagate properly as parameters (bit of a hack then...)
943− mkp (UN n) = sMN 0 ("p_" ++ str n)
969+ mkp (UN n) = sUN ("_p_" ++ str n)
944 mkp (MN i n) = sMN i ("p_" ++ str n)
970 mkp (MN i n) = sMN i ("p_" ++ str n)
945 mkp (NS n s) = NS (mkp n) s
971 mkp (NS n s) = NS (mkp n) s
@@ -953,18 +979,19 @@ elabRecord info syn doc fc tyn ty opts cdoc cn cty_in
953 mkType (NS n s) = NS (mkType n) s
979 mkType (NS n s) = NS (mkType n) s
955 mkProj recty substs cimp ((pn_in, pty), pos)
981 mkProj recty substs cimp ((pn_in, pty), pos)
956− = do let pn = expandNS syn pn_in
982+ = do let pn = expandNS syn pn_in -- projection name
983+ -- use pn_in in the indices, consistently, to avoid clash
957 let pfnTy = PTy emptyDocstring [] defaultSyntax fc [] pn
984 let pfnTy = PTy emptyDocstring [] defaultSyntax fc [] pn
958 (PPi expl rec recty
985 (PPi expl rec recty
959 (substMatches substs pty))
986 (substMatches substs pty))
960 let pls = repeat Placeholder
987 let pls = repeat Placeholder
961 let before = pos
988 let before = pos
962 let after = length substs - (pos + 1)
989 let after = length substs - (pos + 1)
963− let args = take before pls ++ PRef fc (mkp pn) : take after pls
990+ let args = take before pls ++ PRef fc (mkp pn_in) : take after pls
964 let iargs = map implicitise (zip cimp args)
991 let iargs = map implicitise (zip cimp args)
965 let lhs = PApp fc (PRef fc pn)
992 let lhs = PApp fc (PRef fc pn)
966 [pexp (PApp fc (PRef fc cn) iargs)]
993 [pexp (PApp fc (PRef fc cn) iargs)]
967− let rhs = PRef fc (mkp pn)
994+ let rhs = PRef fc (mkp pn_in)
968 let pclause = PClause fc pn lhs [] rhs []
995 let pclause = PClause fc pn lhs [] rhs []
969 return [pfnTy, PClauses fc [] pn [pclause]]
996 return [pfnTy, PClauses fc [] pn [pclause]]
@@ -1425,7 +1452,8 @@ checkPossible info fc tcgen fname lhs_in
1425 | otherwise = False -- name is different, unrecoverable
1452 | otherwise = False -- name is different, unrecoverable
1427getFixedInType i env (PExp _ _ _ _ : is) (Bind n (Pi t) sc)
1454getFixedInType i env (PExp _ _ _ _ : is) (Bind n (Pi t) sc)
1455+ = nub $ getFixedInType i env [] t ++
1428− = getFixedInType i (n : env) is (instantiate (P Bound n t) sc)
1456+ getFixedInType i (n : env) is (instantiate (P Bound n t) sc)
1429getFixedInType i env (_ : is) (Bind n (Pi t) sc)
1457getFixedInType i env (_ : is) (Bind n (Pi t) sc)
1430 = getFixedInType i (n : env) is (instantiate (P Bound n t) sc)
1458 = getFixedInType i (n : env) is (instantiate (P Bound n t) sc)
1431getFixedInType i env is tm@(App f a)
1459getFixedInType i env is tm@(App f a)
@@ -1472,8 +1500,8 @@ paramNames args env (p : ps)
1472 _ -> paramNames args env ps
1500 _ -> paramNames args env ps
1473 | otherwise = paramNames args env ps
1501 | otherwise = paramNames args env ps
1475−propagateParams :: [Name] -> Type -> PTerm -> PTerm
1503+propagateParams :: IState -> [Name] -> Type -> PTerm -> PTerm
1476−propagateParams ps t tm@(PApp _ (PRef fc n) args)
1504+propagateParams i ps t tm@(PApp _ (PRef fc n) args)
1477 = PApp fc (PRef fc n) (addP t args)
1505 = PApp fc (PRef fc n) (addP t args)
1478 where addP (Bind n _ sc) (t : ts)
1506 where addP (Bind n _ sc) (t : ts)
1479 | Placeholder <- getTm t,
1507 | Placeholder <- getTm t,
@@ -1482,9 +1510,15 @@ propagateParams ps t tm@(PApp _ (PRef fc n) args)
1482 = t { getTm = PRef fc n } : addP sc ts
1510 = t { getTm = PRef fc n } : addP sc ts
1483 addP (Bind n _ sc) (t : ts) = t : addP sc ts
1511 addP (Bind n _ sc) (t : ts) = t : addP sc ts
1484 addP _ ts = ts
1512 addP _ ts = ts
1485−propagateParams ps t (PRef fc n)
1513+propagateParams i ps t (PRef fc n)
1514+ = case lookupCtxt n (idris_implicits i) of
1515+ [is] -> let ps' = filter (isImplicit is) ps in
1486− = PApp fc (PRef fc n) (map (\x -> pimp x (PRef fc x) True) ps)
1516+ PApp fc (PRef fc n) (map (\x -> pimp x (PRef fc x) True) ps')
1518+ where isImplicit [] n = False
1519+ isImplicit (PImp _ _ _ x _ : is) n | x == n = True
1520+ isImplicit (_ : is) n = isImplicit is n
1487−propagateParams ps t x = x
1521+propagateParams i ps t x = x
1489-- Return the elaborated LHS/RHS, and the original LHS with implicits added
1523-- Return the elaborated LHS/RHS, and the original LHS with implicits added
1490elabClause :: ElabInfo -> FnOpts -> (Int, PClause) ->
1524elabClause :: ElabInfo -> FnOpts -> (Int, PClause) ->
@@ -1516,7 +1550,7 @@ elabClause info opts (cnum, PClause fc fname lhs_in withs rhs_in whereblock)
1517 let params = getParamsInType i [] fn_is fn_ty
1551 let params = getParamsInType i [] fn_is fn_ty
1518 let lhs = mkLHSapp $ stripUnmatchable i $
1552 let lhs = mkLHSapp $ stripUnmatchable i $
1519− propagateParams params fn_ty (addImplPat i (stripLinear i lhs_in))
1553+ propagateParams i params fn_ty (addImplPat i (stripLinear i lhs_in))
1520 logLvl 5 ("LHS: " ++ show fc ++ " " ++ showTmImpls lhs)
1554 logLvl 5 ("LHS: " ++ show fc ++ " " ++ showTmImpls lhs)
1521 logLvl 4 ("Fixed parameters: " ++ show params ++ " from " ++ show lhs_in ++
1555 logLvl 4 ("Fixed parameters: " ++ show params ++ " from " ++ show lhs_in ++
1522 "\n" ++ show (fn_ty, fn_is))
1556 "\n" ++ show (fn_ty, fn_is))
@@ -1711,7 +1745,7 @@ elabClause info opts (_, PWith fc fname lhs_in withs wval_in withblock)
1711 [t] -> t
1745 [t] -> t
1713 let params = getParamsInType i [] fn_is fn_ty
1747 let params = getParamsInType i [] fn_is fn_ty
1714− let lhs = propagateParams params fn_ty (addImplPat i (stripLinear i lhs_in))
1748+ let lhs = propagateParams i params fn_ty (addImplPat i (stripLinear i lhs_in))
1715 logLvl 2 ("LHS: " ++ show lhs)
1749 logLvl 2 ("LHS: " ++ show lhs)
1716 ((lhs', dlhs, []), _) <-
1750 ((lhs', dlhs, []), _) <-
1717 tclift $ elaborate ctxt (sMN 0 "patLHS") infP []
1751 tclift $ elaborate ctxt (sMN 0 "patLHS") infP []
@@ -1719,11 +1753,11 @@ elabClause info opts (_, PWith fc fname lhs_in withs wval_in withblock)
1719 (erun fc (buildTC i info ELHS opts fname (infTerm lhs))) )
1753 (erun fc (buildTC i info ELHS opts fname (infTerm lhs))) )
1720 let lhs_tm = orderPats (getInferTerm lhs')
1754 let lhs_tm = orderPats (getInferTerm lhs')
1721 let lhs_ty = getInferType lhs'
1755 let lhs_ty = getInferType lhs'
1722− let ret_ty = getRetTy lhs_ty
1756+ let ret_ty = getRetTy (explicitNames (normalise ctxt [] lhs_ty))
1723 logLvl 3 (show lhs_tm)
1757 logLvl 3 (show lhs_tm)
1724 (clhs, clhsty) <- recheckC fc [] lhs_tm
1758 (clhs, clhsty) <- recheckC fc [] lhs_tm
1725 logLvl 5 ("Checked " ++ show clhs)
1759 logLvl 5 ("Checked " ++ show clhs)
1726− let bargs = getPBtys lhs_tm
1760+ let bargs = getPBtys (explicitNames (normalise ctxt [] lhs_tm))
1727 let wval = addImplBound i (map fst bargs) wval_in
1761 let wval = addImplBound i (map fst bargs) wval_in
1728 logLvl 5 ("Checking " ++ showTmImpls wval)
1762 logLvl 5 ("Checking " ++ showTmImpls wval)
1729 -- Elaborate wval in this context
1763 -- Elaborate wval in this context
@@ -1744,8 +1778,8 @@ elabClause info opts (_, PWith fc fname lhs_in withs wval_in withblock)
1744 mapM_ (elabCaseBlock info opts) is
1778 mapM_ (elabCaseBlock info opts) is
1745 logLvl 5 ("Checked wval " ++ show wval')
1779 logLvl 5 ("Checked wval " ++ show wval')
1746 (cwval, cwvalty) <- recheckC fc [] (getInferTerm wval')
1780 (cwval, cwvalty) <- recheckC fc [] (getInferTerm wval')
1747− let cwvaltyN = explicitNames cwvalty
1781+ let cwvaltyN = explicitNames (normalise ctxt [] cwvalty)
1748− let cwvalN = explicitNames cwval
1782+ let cwvalN = explicitNames (normalise ctxt [] cwval)
1749 logLvl 5 ("With type " ++ show cwvalty ++ "\nRet type " ++ show ret_ty)
1783 logLvl 5 ("With type " ++ show cwvalty ++ "\nRet type " ++ show ret_ty)
1750 let pvars = map fst (getPBtys cwvalty)
1784 let pvars = map fst (getPBtys cwvalty)
1751 -- we need the unelaborated term to get the names it depends on
1785 -- we need the unelaborated term to get the names it depends on
@@ -1760,12 +1794,12 @@ elabClause info opts (_, PWith fc fname lhs_in withs wval_in withblock)
1760 -- (ps : Xs) -> (withval : cwvalty) -> (ps' : Xs') -> ret_ty
1794 -- (ps : Xs) -> (withval : cwvalty) -> (ps' : Xs') -> ret_ty
1761 let wargval = getRetTy cwvalN
1795 let wargval = getRetTy cwvalN
1762 let wargtype = getRetTy cwvaltyN
1796 let wargtype = getRetTy cwvaltyN
1763− logLvl 5 ("Abstract over " ++ show wargval)
1797+ logLvl 5 ("Abstract over " ++ show wargval ++ " in " ++ show wargtype)
1764 let wtype = bindTyArgs Pi (bargs_pre ++
1798 let wtype = bindTyArgs Pi (bargs_pre ++
1765 (sMN 0 "warg", wargtype) :
1799 (sMN 0 "warg", wargtype) :
1766 map (abstract (sMN 0 "warg") wargval wargtype) bargs_post)
1800 map (abstract (sMN 0 "warg") wargval wargtype) bargs_post)
1767 (substTerm wargval (P Bound (sMN 0 "warg") wargtype) ret_ty)
1801 (substTerm wargval (P Bound (sMN 0 "warg") wargtype) ret_ty)
1768− logLvl 3 ("New function type " ++ show wtype)
1802+ logLvl 5 ("New function type " ++ show wtype)
1769 let wname = sMN windex (show fname)
1803 let wname = sMN windex (show fname)
1771 let imps = getImps wtype -- add to implicits context
1805 let imps = getImps wtype -- add to implicits context
@@ -1824,7 +1858,7 @@ elabClause info opts (_, PWith fc fname lhs_in withs wval_in withblock)
1824 logLvl 2 ("Matching " ++ showTmImpls tm ++ " against " ++
1858 logLvl 2 ("Matching " ++ showTmImpls tm ++ " against " ++
1825 showTmImpls toplhs)
1859 showTmImpls toplhs)
1826 case matchClause i toplhs tm of
1860 case matchClause i toplhs tm of
1827− Left (a,b) -> trace ("matchClause: " ++ show a ++ " =/= " ++ show b) (ifail $ show fc ++ ":with clause does not match top level")
1861+ Left (a,b) -> ifail $ show fc ++ ":with clause does not match top level"
1828 Right mvars ->
1862 Right mvars ->
1829 do logLvl 3 ("Match vars : " ++ show mvars)
1863 do logLvl 3 ("Match vars : " ++ show mvars)
1830 lhs <- updateLHS n wname mvars ns ns' (fullApp tm) w
1864 lhs <- updateLHS n wname mvars ns ns' (fullApp tm) w