@@ -796,7 +796,7 @@ elabClause info opts (cnum, PClause fc fname lhs_in_as withs rhs_in_as wherebloc
796796 isArg' _ = False
797797 isArg _ _ = False
798798
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)
800800 = do let tcgen = Dictionary `elem` opts
801801 ctxt <- getContext
802802 -- 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)
859859 -- rather than a de Bruijn index.
860860 let pdeps = usedNamesIn pvars i (delab i cwvalty)
861861 let (bargs_pre, bargs_post) = split pdeps bargs []
862+
863+ let mpn = case pn_in of
864+ Nothing -> Nothing
865+ Just n -> Just (uniqueName n (map fst bargs))
866+
862867 logLvl 10 ("With type " ++ show (getRetTy cwvaltyN) ++
863868 " depends on " ++ show pdeps ++ " from " ++ show pvars)
864869 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)
867872 -- (ps : Xs) -> (withval : cwvalty) -> (ps' : Xs') -> ret_ty
868873 let wargval = getRetTy cwvalN
869874 let wargtype = getRetTy cwvaltyN
875+ let wargname = sMN windex "warg"
876+
870877 logLvl 5 ("Abstract over " ++ show wargval ++ " in " ++ show wargtype)
871878 let wtype = bindTyArgs (flip (Pi Nothing) (TType (UVar 0))) (bargs_pre ++
872− (sMN 0 "warg", wargtype) :
873− map (abstract (sMN 0 "warg") wargval wargtype) bargs_post)
874− (substTerm wargval (P Bound (sMN 0 "warg") wargtype) ret_ty)
875− logLvl 5 ("New function type " ++ show wtype)
879+ (wargname, wargtype) :
880+ map (abstract wargname wargval wargtype) bargs_post ++
881+ case mpn of
882+ Just pn -> [(pn, mkApp (P Ref eqTy Erased)
883+ [wargtype, wargtype,
884+ P Bound wargname Erased, wargval])]
885+ Nothing -> [])
886+ (substTerm wargval (P Bound wargname wargtype) ret_ty)
887+ logLvl 3 ("New function type " ++ show wtype)
876888 let wname = SN (WithN windex fname)
877889
878890 let imps = getImps wtype -- add to implicits context
@@ -893,18 +905,21 @@ elabClause info opts (_, PWith fc fname lhs_in withs wval_in withblock)
893905 -- in the subdecls, lhs becomes:
894906 -- fname pats | wpat [rest]
895907 -- ==> 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))
897909 withblock
898910 logLvl 3 ("with block " ++ show wb)
899911 -- propagate totality assertion to the new definitions
900912 when (AssertTotal `elem` opts) $ setFlags wname [AssertTotal]
901913 mapM_ (rec_elabDecl info EAll info) wb
902914
903− -- rhs becomes: fname' ps wval
915+ -- rhs becomes: fname' ps_pre wval ps_post Refl
904916 let rhs = PApp fc (PRef fc wname)
905917 (map (pexp . (PRef fc) . fst) bargs_pre ++
906918 pexp wval :
907− (map (pexp . (PRef fc) . fst) bargs_post))
919+ (map (pexp . (PRef fc) . fst) bargs_post) ++
920+ case mpn of
921+ Nothing -> []
922+ Just _ -> [pexp (PRefl fc Placeholder)])
908923 logLvl 5 ("New RHS " ++ showTmImpls rhs)
909924 ctxt <- getContext -- New context with block added
910925 i <- getIState
@@ -927,13 +942,13 @@ elabClause info opts (_, PWith fc fname lhs_in withs wval_in withblock)
927942 getImps (Bind n (Pi _ _ _) t) = pexp Placeholder : getImps t
928943 getImps _ = []
929944
930− mkAuxC wname lhs ns ns' (PClauses fc o n cs)
931− | True = do cs' <- mapM (mkAux wname lhs ns ns') cs
945+ mkAuxC pn wname lhs ns ns' (PClauses fc o n cs)
946+ | True = do cs' <- mapM (mkAux pn wname lhs ns ns') cs
932947 return $ PClauses fc o wname cs'
933948 | 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
935950
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)
937952 = do i <- getIState
938953 let tm = addImplPat i tm_in
939954 logLvl 2 ("Matching " ++ showTmImpls tm ++ " against " ++
@@ -942,33 +957,36 @@ elabClause info opts (_, PWith fc fname lhs_in withs wval_in withblock)
942957 Left (a,b) -> ifail $ show fc ++ ":with clause does not match top level"
943958 Right mvars ->
944959 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
946961 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)
948963 = do i <- getIState
949964 let tm = addImplPat i tm_in
950965 logLvl 2 ("Matching " ++ showTmImpls tm ++ " against " ++
951966 showTmImpls toplhs)
952− withs' <- mapM (mkAuxC wname toplhs ns ns') withs
967+ withs' <- mapM (mkAuxC pn wname toplhs ns ns') withs
953968 case matchClause i toplhs tm of
954969 Left (a,b) -> trace ("matchClause: " ++ show a ++ " =/= " ++ show b) (ifail $ show fc ++ "with clause does not match top level")
955970 Right mvars ->
956− do lhs <- updateLHS n wname mvars ns ns' (fullApp tm) w
957− return $ PWith fc wname lhs ws wval withs'
958− mkAux wname toplhs ns ns' c
971+ do lhs <- updateLHS n pn wname mvars ns ns' (fullApp tm) w
972+ return $ PWith fc wname lhs ws wval pn' withs'
973+ mkAux pn wname toplhs ns ns' c
959974 = ifail $ show fc ++ ":badly formed with clause"
960975
961976 addArg (PApp fc f args) w = PApp fc f (args ++ [pexp w])
962977 addArg (PRef fc f) w = PApp fc (PRef fc f) [pexp w]
963978
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
965980 = let ns = map (keepMvar (map fst mvars) fc') ns_in
966981 ns' = map (keepMvar (map fst mvars) fc') ns_in' in
967982 return $ substMatches mvars $
968983 PApp fc (PRef fc' wname)
969− (map pexp ns ++ pexp w : (map pexp ns'))
970− updateLHS n wname mvars ns_in ns_in' tm w
971− = updateLHS n wname mvars ns_in ns_in' (PApp fc tm []) w
984+ (map pexp ns ++ pexp w : (map pexp ns') ++
985+ case pn of
986+ Nothing -> []
987+ Just pnm -> [pexp (PRef fc pnm)])
988+ updateLHS n pn wname mvars ns_in ns_in' tm w
989+ = updateLHS n pn wname mvars ns_in ns_in' (PApp fc tm []) w
972990
973991 keepMvar mvs fc v | v `elem` mvs = PRef fc v
974992 | otherwise = Placeholder