Skip to Content.
Sympa Menu

coq-club - Re: [Coq-Club] autorewrite neglects sub-patterns?

coq-club AT inria.fr

Subject: The Coq mailing list

List archive

Re: [Coq-Club] autorewrite neglects sub-patterns?


Chronological Thread 
  • From: Matthieu Sozeau <mattam AT mattam.org>
  • To: coq-club AT inria.fr
  • Subject: Re: [Coq-Club] autorewrite neglects sub-patterns?
  • Date: Tue, 12 Jun 2018 10:16:56 +0200
  • Authentication-results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=mattam AT mattam.org; spf=Pass smtp.mailfrom=matthieu.sozeau AT gmail.com; spf=None smtp.helo=postmaster AT mail-wm0-f54.google.com
  • Ironport-phdr: 9a23:AvSBTxbukxH0mNzhaKpzMHH/LSx+4OfEezUN459isYplN5qZoM+5bnLW6fgltlLVR4KTs6sC17KL9fi4EUU7or+5+EgYd5JNUxJXwe43pCcHRPC/NEvgMfTxZDY7FskRHHVs/nW8LFQHUJ2mPw6arXK99yMdFQviPgRpOOv1BpTSj8Oq3Oyu5pHfeQpFiCa9bL9oMBm6sRjau9ULj4dlNqs/0AbCrGFSe+RRy2NoJFaTkAj568yt4pNt8Dletuw4+cJYXqr0Y6o3TbpDDDQ7KG81/9HktQPCTQSU+HQRVHgdnwdSDAjE6BH6WYrxsjf/u+Fg1iSWIdH6QLYpUjmk8qxlSgLniD0fOjE2/mHYiMx+gqxYrhy8uRJw35XZb5uJOPdkZK7RYc8WSGhHU81MVyJBGIS8b44XAuQGJ+lYs5X9p1sPrRSgGAmnGf7hyjhJh3Dox6I6zvkqHAbD3AM6A9IOrG7brNDuOacXS++10LXIwi/Gb/9M3jf98ofIfwknrPqRU7xwds/RxlMuFwPDlliQqIrlPymU1usXqWSb4fBgWOSygGAkswF8ujqiy8g2hoXUmI4YykrI+CZnzIovJdC1R0h2asO+HpRKrSGVLY52T9siQ252vCY6zaULuZuhcygLzJQr3hDea/mbf4SR7BLuVOmcLS13hHJif7K/iBKy/la6xuLgUcm01U5GritDktbSqnAAzx7e58edRvdg4Eus2SyD2gPN5u1eIU05mrLXK5s7zb4xkpoTv17DHijzmEjuia+ZbFkk+vSo6+TmfLrmu4WQN5RzigD/LKshgNG/DP83MggLRWeb+OC82Kf/8k3+RbVGluc2nbXBsJDGOcQboba0DBNS0oY68hqwEzOm0MkDknQcN1JEeBeHj5DzNF3UIfD4C+2/g1W2nztxyfDGJO6pPpKYJX/a1bzlYLxV6khGyQN1w8oMyYhTD+QkKe7vWk78qZTjCQ00OhH8l+PuFMl006sbUH6TC6rfN7nd5wzbrtkzKvWBMddG8A32LOIosqa33C0J3GQFdKzs5qM5LXWxH/BoOUKcOCO+jdIIEGNMtQ07HrWz1A+yFAVLbnP3ZJoSoykhAdv8X4LKWpykhfqGxijpRsQLNFADMUiFFDLTT6vBW/oIb3jPcMpokzhBSr35DoF8iVehswj1z7chJe3RqHUV


Le ven. 8 juin 2018 à 08:08, Kevin Hamlen <hamlen AT utdallas.edu> a écrit :
Dear Matthieu,

Thank you for the pointer!  I didn't know about "rewrite_strat".  It indeed appears to be very powerful.

But it also seems to be strangely slow, and I'm not sure I understand why.  Running "rewrite_strat (topdown (hints mydb))" on a large term takes much longer to complete than my Ltac, which just context-matches on all the patterns of hints in the database and tries all the matching lemmas on each.  Here's a minimal example:


Require Import NArith.
Open Scope N.

Hint Rewrite N.mod_small using assumption : mydb.

Definition large_term (m:N) (x:N): N.
  do 300 refine (_ mod m). exact x.
Defined.

Ltac my_rewrite :=
  repeat match goal with [ |- context [ ?x mod ?y ] ] => rewrite (N.mod_small x y) by assumption end.

Goal forall x y m, x < y -> large_term m (x mod y) = large_term m x.
  intros. unfold large_term.
  assert_succeeds (time my_rewrite). (* takes about 10s on my machine *)
  time rewrite_strat (topdown (hints mydb)).  (* takes about 50s on my machine *)
  reflexivity.
Qed.

Here I get a 5x performance hit using rewrite_strat with topdown vs. my manual approach, and with a larger hint database I get as much as 1000x slow-down.  So something obviously isn't scaling well.  But isn't my Ltac essentially doing exactly what topdown does?  Perhaps I don't yet understand how rewrite strategies work.

Hi Kevin,

  It might be the unification strategy used by rewrite_strat which differs and takes much longer to fail than the syntactic matching you perform here. You might want to guard the rewrite with a `pattern` strategy that does syntactic matching as your Ltac code does. Also, contrary to rewrite, rewrite_strat does not treat the equality type specially and uses generalized rewriting to produce the proof of the toplevel equality, whereby a proof-search is performed which might slow things down.

Cheers,
— Matthieu





On 6/7/2018 1:27 PM, Matthieu Sozeau wrote:
Dear Kevin,

  [autorewrite] is indeed a bit crude, namely it only does [repeat rewrite].
You might prefer more control using the [rewrite_strat] tactic which allows
you to control the traversal strategy and will try to apply the rewrite rule to 
subterms as you instruct it. In your example:

  now rewrite_strat (topdown (hints mydb)).
or 
  now rewrite_strat (bottomup (hints mydb)).

complete your proof. Both strategies are fixpoints: 
- for topdown, if the argument tactic succeeds (here (hints mydb) which
  succeeds when it can use one of the rewrite hints), it calls itself recursively 
  and if it fails it recursively calls itself on subterms or stops making no 
  progress if there are no subterms
- for bottomup, it goes to the leaves of the term first.


Best,
-- Matthieu

On Thu, Jun 7, 2018 at 8:02 PM Kevin Hamlen <hamlen AT utdallas.edu> wrote:
Dear Coq users,

I'm finding that "autorewrite" fails to consider rewriting opportunities
within patterns that match a hint but fail it.  Is this behavior
intentional?  Here's an example:

Require Import NArith.
Open Scope N.

Hint Rewrite N.mod_small using assumption : mydb.

Goal forall x y z, x < y -> (x mod y) mod z = x mod z.
   intros.
   Fail progress (autorewrite with mydb).
   (* This unfortunately fails to progress even though it could
successfully rewrite "x mod y" to "x".  It fails because it first tries
rewriting "(x mod y) mod z" to "x mod y" and is unsuccessful, and then
never looks for rewrite opportunities within the matched pattern. *)

   (* If I manually strip the "_ mod z" part, it finds the pattern and
rewrites it: *)
   apply (f_equal (fun n => n mod z)).
   progress (autorewrite with mydb).
   reflexivity.
Qed.

Is there any work-around?  This behavior unfortunately makes autorewrite
useless for most of my patterns, forcing me to resort to much slower Ltacs.

Cheers,
Kevin






Archive powered by MHonArc 2.6.18.

Top of Page