@@ -796,7 +796,7 @@ elabClause info opts (cnum, PClause fc fname lhs_in_as withs rhs_in_as wherebloc
796 isArg' _ = False
796 isArg' _ = False
797 isArg _ _ = False
797 isArg _ _ = False
799−elabClause info opts (_, PWith fc fname lhs_in withs wval_in withblock)
799+elabClause info opts (_, PWith fc fname lhs_in withs wval_in pn_in withblock)
800 = do let tcgen = Dictionary `elem` opts
800 = do let tcgen = Dictionary `elem` opts
801 ctxt <- getContext
801 ctxt <- getContext
802 -- Build the LHS as an "Infer", and pull out its type and
802 -- Build the LHS as an "Infer", and pull out its type and
@@ -859,6 +859,11 @@ elabClause info opts (_, PWith fc fname lhs_in withs wval_in withblock)
859 -- rather than a de Bruijn index.
859 -- rather than a de Bruijn index.
860 let pdeps = usedNamesIn pvars i (delab i cwvalty)
860 let pdeps = usedNamesIn pvars i (delab i cwvalty)
861 let (bargs_pre, bargs_post) = split pdeps bargs []
861 let (bargs_pre, bargs_post) = split pdeps bargs []
863+ let mpn = case pn_in of
865+ Just n -> Just (uniqueName n (map fst bargs))
862 logLvl 10 ("With type " ++ show (getRetTy cwvaltyN) ++
867 logLvl 10 ("With type " ++ show (getRetTy cwvaltyN) ++
863 " depends on " ++ show pdeps ++ " from " ++ show pvars)
868 " depends on " ++ show pdeps ++ " from " ++ show pvars)
864 logLvl 10 ("Pre " ++ show bargs_pre ++ "\nPost " ++ show bargs_post)
869 logLvl 10 ("Pre " ++ show bargs_pre ++ "\nPost " ++ show bargs_post)
@@ -867,12 +872,19 @@ elabClause info opts (_, PWith fc fname lhs_in withs wval_in withblock)
867 -- (ps : Xs) -> (withval : cwvalty) -> (ps' : Xs') -> ret_ty
872 -- (ps : Xs) -> (withval : cwvalty) -> (ps' : Xs') -> ret_ty
868 let wargval = getRetTy cwvalN
873 let wargval = getRetTy cwvalN
869 let wargtype = getRetTy cwvaltyN
874 let wargtype = getRetTy cwvaltyN
875+ let wargname = sMN windex "warg"
870 logLvl 5 ("Abstract over " ++ show wargval ++ " in " ++ show wargtype)
877 logLvl 5 ("Abstract over " ++ show wargval ++ " in " ++ show wargtype)
871 let wtype = bindTyArgs (flip (Pi Nothing) (TType (UVar 0))) (bargs_pre ++
878 let wtype = bindTyArgs (flip (Pi Nothing) (TType (UVar 0))) (bargs_pre ++
872− (sMN 0 "warg", wargtype) :
879+ (wargname, wargtype) :
873− map (abstract (sMN 0 "warg") wargval wargtype) bargs_post)
880+ map (abstract wargname wargval wargtype) bargs_post ++
882+ Just pn -> [(pn, mkApp (P Ref eqTy Erased)
883+ [wargtype, wargtype,
884+ P Bound wargname Erased, wargval])]
874− (substTerm wargval (P Bound (sMN 0 "warg") wargtype) ret_ty)
886+ (substTerm wargval (P Bound wargname wargtype) ret_ty)
875− logLvl 5 ("New function type " ++ show wtype)
887+ logLvl 3 ("New function type " ++ show wtype)
876 let wname = SN (WithN windex fname)
888 let wname = SN (WithN windex fname)
878 let imps = getImps wtype -- add to implicits context
890 let imps = getImps wtype -- add to implicits context
@@ -893,18 +905,21 @@ elabClause info opts (_, PWith fc fname lhs_in withs wval_in withblock)
893 -- in the subdecls, lhs becomes:
905 -- in the subdecls, lhs becomes:
894 -- fname pats | wpat [rest]
906 -- fname pats | wpat [rest]
895 -- ==> fname' ps wpat [rest], match pats against toplevel for ps
907 -- ==> fname' ps wpat [rest], match pats against toplevel for ps
896− wb <- mapM (mkAuxC wname lhs (map fst bargs_pre) (map fst bargs_post))
908+ wb <- mapM (mkAuxC mpn wname lhs (map fst bargs_pre) (map fst bargs_post))
897 withblock
909 withblock
898 logLvl 3 ("with block " ++ show wb)
910 logLvl 3 ("with block " ++ show wb)
899 -- propagate totality assertion to the new definitions
911 -- propagate totality assertion to the new definitions
900 when (AssertTotal `elem` opts) $ setFlags wname [AssertTotal]
912 when (AssertTotal `elem` opts) $ setFlags wname [AssertTotal]
901 mapM_ (rec_elabDecl info EAll info) wb
913 mapM_ (rec_elabDecl info EAll info) wb
903− -- rhs becomes: fname' ps wval
915+ -- rhs becomes: fname' ps_pre wval ps_post Refl
904 let rhs = PApp fc (PRef fc wname)
916 let rhs = PApp fc (PRef fc wname)
905 (map (pexp . (PRef fc) . fst) bargs_pre ++
917 (map (pexp . (PRef fc) . fst) bargs_pre ++
906 pexp wval :
918 pexp wval :
907− (map (pexp . (PRef fc) . fst) bargs_post))
919+ (map (pexp . (PRef fc) . fst) bargs_post) ++
922+ Just _ -> [pexp (PRefl fc Placeholder)])
908 logLvl 5 ("New RHS " ++ showTmImpls rhs)
923 logLvl 5 ("New RHS " ++ showTmImpls rhs)
909 ctxt <- getContext -- New context with block added
924 ctxt <- getContext -- New context with block added
910 i <- getIState
925 i <- getIState
@@ -927,13 +942,13 @@ elabClause info opts (_, PWith fc fname lhs_in withs wval_in withblock)
927 getImps (Bind n (Pi _ _ _) t) = pexp Placeholder : getImps t
942 getImps (Bind n (Pi _ _ _) t) = pexp Placeholder : getImps t
928 getImps _ = []
943 getImps _ = []
930− mkAuxC wname lhs ns ns' (PClauses fc o n cs)
945+ mkAuxC pn wname lhs ns ns' (PClauses fc o n cs)
931− | True = do cs' <- mapM (mkAux wname lhs ns ns') cs
946+ | True = do cs' <- mapM (mkAux pn wname lhs ns ns') cs
932 return $ PClauses fc o wname cs'
947 return $ PClauses fc o wname cs'
933 | otherwise = ifail $ show fc ++ "with clause uses wrong function name " ++ show n
948 | otherwise = ifail $ show fc ++ "with clause uses wrong function name " ++ show n
934− mkAuxC wname lhs ns ns' d = return $ d
949+ mkAuxC pn wname lhs ns ns' d = return $ d
936− mkAux wname toplhs ns ns' (PClause fc n tm_in (w:ws) rhs wheres)
951+ mkAux pn wname toplhs ns ns' (PClause fc n tm_in (w:ws) rhs wheres)
937 = do i <- getIState
952 = do i <- getIState
938 let tm = addImplPat i tm_in
953 let tm = addImplPat i tm_in
939 logLvl 2 ("Matching " ++ showTmImpls tm ++ " against " ++
954 logLvl 2 ("Matching " ++ showTmImpls tm ++ " against " ++
@@ -942,33 +957,36 @@ elabClause info opts (_, PWith fc fname lhs_in withs wval_in withblock)
942 Left (a,b) -> ifail $ show fc ++ ":with clause does not match top level"
957 Left (a,b) -> ifail $ show fc ++ ":with clause does not match top level"
943 Right mvars ->
958 Right mvars ->
944 do logLvl 3 ("Match vars : " ++ show mvars)
959 do logLvl 3 ("Match vars : " ++ show mvars)
945− lhs <- updateLHS n wname mvars ns ns' (fullApp tm) w
960+ lhs <- updateLHS n pn wname mvars ns ns' (fullApp tm) w
946 return $ PClause fc wname lhs ws rhs wheres
961 return $ PClause fc wname lhs ws rhs wheres
947− mkAux wname toplhs ns ns' (PWith fc n tm_in (w:ws) wval withs)
962+ mkAux pn wname toplhs ns ns' (PWith fc n tm_in (w:ws) wval pn' withs)
948 = do i <- getIState
963 = do i <- getIState
949 let tm = addImplPat i tm_in
964 let tm = addImplPat i tm_in
950 logLvl 2 ("Matching " ++ showTmImpls tm ++ " against " ++
965 logLvl 2 ("Matching " ++ showTmImpls tm ++ " against " ++
951 showTmImpls toplhs)
966 showTmImpls toplhs)
952− withs' <- mapM (mkAuxC wname toplhs ns ns') withs
967+ withs' <- mapM (mkAuxC pn wname toplhs ns ns') withs
953 case matchClause i toplhs tm of
968 case matchClause i toplhs tm of
954 Left (a,b) -> trace ("matchClause: " ++ show a ++ " =/= " ++ show b) (ifail $ show fc ++ "with clause does not match top level")
969 Left (a,b) -> trace ("matchClause: " ++ show a ++ " =/= " ++ show b) (ifail $ show fc ++ "with clause does not match top level")
955 Right mvars ->
970 Right mvars ->
956− do lhs <- updateLHS n wname mvars ns ns' (fullApp tm) w
971+ do lhs <- updateLHS n pn wname mvars ns ns' (fullApp tm) w
957− return $ PWith fc wname lhs ws wval withs'
972+ return $ PWith fc wname lhs ws wval pn' withs'
958− mkAux wname toplhs ns ns' c
973+ mkAux pn wname toplhs ns ns' c
959 = ifail $ show fc ++ ":badly formed with clause"
974 = ifail $ show fc ++ ":badly formed with clause"
961 addArg (PApp fc f args) w = PApp fc f (args ++ [pexp w])
976 addArg (PApp fc f args) w = PApp fc f (args ++ [pexp w])
962 addArg (PRef fc f) w = PApp fc (PRef fc f) [pexp w]
977 addArg (PRef fc f) w = PApp fc (PRef fc f) [pexp w]
964− updateLHS n wname mvars ns_in ns_in' (PApp fc (PRef fc' n') args) w
979+ updateLHS n pn wname mvars ns_in ns_in' (PApp fc (PRef fc' n') args) w
965 = let ns = map (keepMvar (map fst mvars) fc') ns_in
980 = let ns = map (keepMvar (map fst mvars) fc') ns_in
966 ns' = map (keepMvar (map fst mvars) fc') ns_in' in
981 ns' = map (keepMvar (map fst mvars) fc') ns_in' in
967 return $ substMatches mvars $
982 return $ substMatches mvars $
968 PApp fc (PRef fc' wname)
983 PApp fc (PRef fc' wname)
969− (map pexp ns ++ pexp w : (map pexp ns'))
984+ (map pexp ns ++ pexp w : (map pexp ns') ++
987+ Just pnm -> [pexp (PRef fc pnm)])
970− updateLHS n wname mvars ns_in ns_in' tm w
988+ updateLHS n pn wname mvars ns_in ns_in' tm w
971− = updateLHS n wname mvars ns_in ns_in' (PApp fc tm []) w
989+ = updateLHS n pn wname mvars ns_in ns_in' (PApp fc tm []) w
973 keepMvar mvs fc v | v `elem` mvs = PRef fc v
991 keepMvar mvs fc v | v `elem` mvs = PRef fc v
974 | otherwise = Placeholder
992 | otherwise = Placeholder