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: Kevin Hamlen <hamlen AT utdallas.edu>
  • To: <coq-club AT inria.fr>
  • Subject: Re: [Coq-Club] autorewrite neglects sub-patterns?
  • Date: Fri, 8 Jun 2018 01:08:17 -0500
  • Authentication-results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=hamlen AT utdallas.edu; spf=Pass smtp.mailfrom=hamlen AT utdallas.edu; spf=None smtp.helo=postmaster AT esa1.hc50-71.r1.ces.cisco.com
  • Ironport-phdr: 9a23:RvyohhUlMRJGRaxeeNeEzaNogKHV8LGtZVwlr6E/grcLSJyIuqrYYx2Bt8tkgFKBZ4jH8fUM07OQ7/i9HzRYqb+681k6OKRWUBEEjchE1ycBO+WiTXPBEfjxciYhF95DXlI2t1uyMExSBdqsLwaK+i764jEdAAjwOhRoLerpBIHSk9631+ev8JHPfglEnjWwba9yIRmssQndqtQdjJd/JKo21hbHuGZDdf5MxWNvK1KTnhL86dm18ZV+7SleuO8v+tBZX6nicKs2UbJXDDI9M2Ao/8LrrgXMTRGO5nQHTGoblAdDDhXf4xH7WpfxtTb6tvZ41SKHM8D6Uaw4VDK/5KptVRTmijoINyQh/W/KlMJwgqJVrhGvqRNxzIHbYp2aNPtlc6zHYd8XX3BMUtpfWiFDBI63cosBD/AGPeZdt4TzokUBrQW/BQmqGejhyCVHhnry3aIk1eQqDAbL3Aw6ENILtHTbstP1ObwPUeCp0abH1zHDb/dM1jfm8oTHbA0uoeyVUL92bMHfx04vFwbfgVWRr4zoJzKV1uIXs2ia9eVsT+yvi3QhpgpsoTav3t8hh4fJi44P11zJ9Dl1zJw0KNGmUkJ3fN2pHINNuy2GLYd6X8AvT39ytCs41rEKo4O3cSYWxJg/2hLTdfiKf5KV7h7+SOqdOyl0iX17dL6lmhq//kutx+vhXceuyllKtDBKktzUu3ANyRPT7s+HR+Nj8Ue6wzmDyhrf6uFYLk00iabXMpwszqMqmpoJrEjPBDP5l1vtjKOMakok+/Oo5PjgYrn8upCcMIp0hhn/MqQohMO/Hfw1PwYOUmSB5Oix1aHv8Vf4TblUlPE6j6rUvIjfJcsBp665BwFV0pwk6xa6Fzqm0M4XnXwDLF1bfhKKlJXpO03TL//iCfe/h06jkCxxy//YI7LhH43BLmLfn7f5YbZ990lcxRIvwtBY/pJYE60OIPbuWkDqr9HYFR84Mwmsw+n9Etl914UeWXiOAqCDKq/Sv0WItaoTJLzYb4gM/T35NvIN5vj0jHZ/l0VLLoez2p5CS32mH/IuA0ycZXfqyoMBGGIOswwWUfDmgVmGWCUVanqvCfFvrgonAZ6rWN+QDrumh6aMiX/iT89mI1teA1XJKk/GMoCNWvMCciWXe5MzjyAIXLylQpRn2B2z5lajl+hXa9HM8yhdjqrNkcBv7rSPxwwu+TVwC82GlWyBUjMsxz5ad3oNxKl65HdF5BKD3Kx/2KUKGMdavrVJWVpicMGFifYjTdv/QUTKf8yCTxCtRdD0WTw=

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.

Regards,
Kevin



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.

https://coq.inria.fr/refman/addendum/generalized-rewriting.html#strategies-for-rewriting

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