Zulip Chat Archive

Stream: general

Topic: run tactic for x in list


Johan Commelin (Oct 12 2020 at 12:32):

I have the following tactic state:

abcd: 
hd0: 0 < d
hdc: d  c
hcb: c  b
hba: b  a
h1: a + b + c + d = 1
 (a + 2 * b + 3 * c + 4 * d) * a ^ a * b ^ b * c ^ c * d ^ d < 1

I want to lift a, b, c, and d to nnreal. How do I do that? I feel like there could be a useful tactic similar to repeat and friends.

Johan Commelin (Oct 12 2020 at 12:33):

I tried do [`a, `b, `c, `d].mmap (λ x, `[lift x to nnreal, swap, linarith]) but this is entirely cargo-cult tactic hacking.

Johan Commelin (Oct 12 2020 at 12:34):

It would be nice if we could write something like

for x in [`a, `b, `c, `d], { lift %%x to nnreal, swap, linarith }

modulo syntax errors

Anne Baanen (Oct 12 2020 at 12:35):

Do antiquotations work in tactic quotations?

do [`a, `b, `c, `d].mmap (λ x, `[lift %%x to nnreal, swap, linarith])

Johan Commelin (Oct 12 2020 at 12:38):

The do also seems to kick me out of interactive mode

Johan Commelin (Oct 12 2020 at 12:39):

If I end that line with a , then I can't do ordinary tactic proving after it.

Johan Commelin (Oct 12 2020 at 12:39):

Also, the do-line gives the error

don't know how to synthesize placeholder
context:
a b c d : ,
hd0 : 0 < d,
hdc : d  c,
hcb : c  b,
hba : b  a,
h1 : a + b + c + d = 1
 Type

Mario Carneiro (Oct 12 2020 at 12:40):

you have to put the entire do block in parentheses, or else the comma will continue the do block instead of the tactic block

Mario Carneiro (Oct 12 2020 at 12:41):

actually I think you can just leave off the do in anne's version

Mario Carneiro (Oct 12 2020 at 12:42):

#mwe

Johan Commelin (Oct 12 2020 at 12:42):

([`a, `b, `c, `d].mmap (λ x, `[lift %%x to nnreal, swap, linarith])),

gives the error

Failed to find a lift from name to nnreal. Provide an instance of
  can_lift name nnreal

Johan Commelin (Oct 12 2020 at 12:42):

So I must have some of the backticks and %% wrong

Johan Commelin (Oct 12 2020 at 12:43):

MWE:

import data.real.nnreal

example (a b c d : nnreal)
  (hba : b  a)
  (hcb : c  b)
  (hd0 : 0 < d)
  (hdc : d  c)
  (h1 : a + b + c + d = 1) :
  a + 2 * b + 3 * c + 4 * d  a + 3 * b + 3 * c + 3 * d :=
begin
  -- lift `a,b,c,d` to `nnreal` in a one liner...
end

Anne Baanen (Oct 12 2020 at 12:46):

I don't have Lean in front of me. Does this help?

([`(a), `(b), `(c), `(d)].mmap (λ x, `[lift %%x to nnreal, swap, linarith])),

Mario Carneiro (Oct 12 2020 at 12:46):

wait, can't lift nnreal to nnreal?

Mario Carneiro (Oct 12 2020 at 12:46):

your mwe was wrong

example (a b c d : )
  (hba : b  a)
  (hcb : c  b)
  (hd0 : 0 < d)
  (hdc : d  c)
  (h1 : a + b + c + d = 1) :
  a + 2 * b + 3 * c + 4 * d  a + 3 * b + 3 * c + 3 * d :=
begin
  ([```(a),```(b),```(c),```(d)].mmap $ λ x,
    `[lift %%x to nnreal, swap, linarith]),
  -- lift `a,b,c,d` to `nnreal` in a one liner...
end

Anne Baanen (Oct 12 2020 at 12:47):

Ah, triple-backquote.

Johan Commelin (Oct 12 2020 at 12:50):

Ooh, I had tried ``` , but without the ().

Anne Baanen (Oct 12 2020 at 12:52):

My reasoning to use `(a) was that `a gives a name and the error complained about name.

Mario Carneiro (Oct 12 2020 at 12:52):

See also https://leanprover-community.github.io/extras/tactic_writing.html#marios-backtick-cheat-sheet

Johan Commelin (Oct 12 2020 at 13:09):

I feel like this syntax isn't optimized for one-off hacks like this

Johan Commelin (Oct 12 2020 at 13:10):

As in, we probably wouldn't want this one-liner ending up in mathlib

Mario Carneiro (Oct 12 2020 at 13:10):

eh, it's fine

Johan Commelin (Oct 12 2020 at 13:10):

I wouldn't mind having something with less sigils

Mario Carneiro (Oct 12 2020 at 13:10):

it used to be single backtick but the assignments got swapped around

Mario Carneiro (Oct 12 2020 at 13:11):

because the expr version seemed more useful

Mario Carneiro (Oct 12 2020 at 13:11):

I'm sure lean 4 is doing something else

Mario Carneiro (Oct 12 2020 at 13:15):

alternatively:

  (do lc  tactic.local_context, lc.mmap' $ λ x,
    `[try {lift %%x to nnreal, swap, linarith}]),

Johan Commelin (Oct 12 2020 at 13:23):

That certainly has fewer backticks :thumbs_up:


Last updated: Dec 20 2023 at 11:08 UTC