Zulip Chat Archive

Stream: new members

Topic: List of elements all satisfying predicate


Eric Wieser (Jun 27 2020 at 12:56):

How do I declare a type where each element satisfies a predicate? list.all seems to almost be what I want, but it works in terms of bool instead of Prop.

Jalex Stark (Jun 27 2020 at 12:58):

i don't understand your question

Jalex Stark (Jun 27 2020 at 12:58):

does Pred mean Prop?

Jalex Stark (Jun 27 2020 at 12:59):

are you asking how to coerce a bool to a Prop?

Alistair Tucker (Jun 27 2020 at 13:00):

Subtype?

Jalex Stark (Jun 27 2020 at 13:01):

def x : Prop := ff
#print x
#check x

Eric Wieser (Jun 27 2020 at 13:02):

No, I'm asking how to convert an a -> Prop to an a -> bool

Reid Barton (Jun 27 2020 at 13:06):

See the statement of all_iff_forall_prop for example.

Jalex Stark (Jun 27 2020 at 13:07):

why do you want to work with bool?

Reid Barton (Jun 27 2020 at 13:07):

Jalex, see the original question.

Jalex Stark (Jun 27 2020 at 13:07):

(oh, probably by original question you mean this thread, where maybe eric is computing with stuff)

Jalex Stark (Jun 27 2020 at 13:08):

my objection is that the main reason to work with bool is computing things, but coming from Prop loses computational interpretation?

Eric Wieser (Jun 27 2020 at 13:15):

I don't want to work with bool, but the closest thing I found to what I want is list.all, which does

Eric Wieser (Jun 27 2020 at 13:16):

It's looking like the answer is I should subtype the _elements_ of the list

Eric Wieser (Jun 27 2020 at 13:16):

Which I somehow missed

Eric Wieser (Jun 27 2020 at 13:18):

So as the simplest version of my question, how would I state the proposition all elements of a list of Nat are > 2

Jalex Stark (Jun 27 2020 at 13:20):

example (l : list ) : Prop := list.all l (λ x, 2 < x)

Jalex Stark (Jun 27 2020 at 13:21):

you were in fact asking how to coerce a bool into a Prop

Jalex Stark (Jun 27 2020 at 13:21):

typeclass inference knows about a coercion from bool to Prop, so if you just ask Lean to interpret a bool as a Prop, it will apply that coercion

Alistair Tucker (Jun 27 2020 at 13:23):

Wouldn't you want to construct a term of type ∀ n : ℕ, n ∈ l → 2 < n?

Reid Barton (Jun 27 2020 at 13:23):

Eric Wieser said:

How do I declare a type where each element satisfies a predicate? list.all seems to almost be what I want, but it works in terms of bool instead of Prop.

(Jalex, I'm talking about this one!)

Jalex Stark (Jun 27 2020 at 13:26):

Reid Barton said:

Eric Wieser said:

How do I declare a type where each element satisfies a predicate? list.all seems to almost be what I want, but it works in terms of bool instead of Prop.

(Jalex, I'm talking about this one!)

okay, I think I understand now. Eric said they wanted to be able to turn their a \to Prop into a \to bool, but actually they're just as happy going the other way, and the other way is the right way

Kenny Lau (Jun 27 2020 at 13:32):

def they_all_satisfy_predicate (α : Sort*) (P : α  Prop) (L : list α) : Prop :=
 x  L, P x

Kenny Lau (Jun 27 2020 at 13:32):

@Eric Wieser is this what you want?

Alistair Tucker (Jun 27 2020 at 14:01):

Ignoring the excursion into bool land, you are looking at one of two options. You could construct a list from a subtype with a proof attached to each element i.e. list {n : ℕ // 2 < n}. Or make a list l and then prove this fact about it ∀ n : ℕ, n ∈ l → 2 < n. Which is better might depend on your particular use case, but I think I see more of the latter style in mathlib.

Mario Carneiro (Jun 27 2020 at 14:01):

I recommend Kenny's idiom

Mario Carneiro (Jun 27 2020 at 14:02):

there are simp lemmas for it and decidability

Johan Commelin (Jun 27 2020 at 14:02):

Shouldn't list.all be renamed to list.ball?

Mario Carneiro (Jun 27 2020 at 14:02):

perhaps, but it's not really competing with anything

Johan Commelin (Jun 27 2020 at 14:03):

Well, it would free up the name for a Prop-valued version

Mario Carneiro (Jun 27 2020 at 14:03):

I don't want a prop valued version to exist

Mario Carneiro (Jun 27 2020 at 14:03):

because that would mean two idioms

Mario Carneiro (Jun 27 2020 at 14:03):

∀ x ∈ L, P x already exists

Eric Wieser (Aug 14 2021 at 10:06):

It turns out we do have the definition I was looking for, it's docs#list_all which satisfies list_all p l ↔ ∀ x ∈ l, p x. Should we rename that to list.all or list.forall? It's a bit silly having list.all be about bool and list_all being about Prop.

Eric Wieser (Aug 14 2021 at 10:07):

Mario Carneiro said:

I don't want a prop valued version to exist

This is a strange claim to make given you are responsible for it existing!


Last updated: Dec 20 2023 at 11:08 UTC