Zulip Chat Archive

Stream: lean4

Topic: Finite sum notation in lean4:


Shreyas Srinivas (Mar 23 2023 at 15:11):

In the repository referenced here: https://leanprover.zulipchat.com/#narrow/stream/252551-graph-theory/topic/max-flow.20min-cut.20help
they try to prove the max flow min cut theorem in graphs. In this the total flow into a set of vertices as follows:

def mk_in {V : Type* } [inst : fintype V]
  (f : V -> V -> ) (s : finset V) :  :=  x in finset.univ \ s,  y in s, f x y

I tried to replicate this in lean4:

noncomputable def flow_in {V : Type u} [Fintype V] [DecidableEq V]
  (flow : V  V  )
  (vSet : Finset V) :  :=
    ∑' s in (Finset.univ \ vSet),  ∑' t in vSet, flow s t

Lean 4 complains that in is an error and says expected ','

Shreyas Srinivas (Mar 23 2023 at 15:14):

I suspect I can do it by explicitly using the fold functions for Finsets and tsum, but is there a neat way to use the sum notation?

Eric Wieser (Mar 23 2023 at 15:18):

Why did you use ∑' instead?

Shreyas Srinivas (Mar 23 2023 at 15:20):

I followed the docs (docs4#tsum)

Shreyas Srinivas (Mar 23 2023 at 15:20):

Also it was recommended here: https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/.E2.9C.94.20Beginner.20question.20on.20infinite.20sums

Shreyas Srinivas (Mar 23 2023 at 15:21):

Tried first but got expected token and then searched zulip to find this thread.

Reid Barton (Mar 23 2023 at 15:25):

tsum is intended for infinite sums

Reid Barton (Mar 23 2023 at 15:26):

And there's no in in its notation.

Reid Barton (Mar 23 2023 at 15:26):

But anyways, you want

Shreyas Srinivas (Mar 23 2023 at 15:34):

noncomputable def flow_out {V : Type} [Fintype V] [DecidableEq V]
  (flow : V  V  )
  (vSet : Finset V)
  :  :=
     s in vSet, ( t in (Finset.univ \ vSet), flow s t)

This throws the error expected token

Shreyas Srinivas (Mar 23 2023 at 15:34):

Error underlining

Reid Barton (Mar 23 2023 at 15:34):

Do you have open BigOperators?

Shreyas Srinivas (Mar 23 2023 at 15:34):

ah no

Shreyas Srinivas (Mar 23 2023 at 15:36):

Works. Thanks :)

Yaël Dillies (Mar 23 2023 at 15:50):

Finset.univ \ vSet is vSetᶜ, by the way

Shreyas Srinivas (Mar 23 2023 at 15:56):

it works (although it gets a bit hard to read). Do we have a way to put a bar on top (like latex's \overline{}?)

Yaël Dillies (Mar 23 2023 at 15:57):

Don't think so! That sounds like WYSIWYG witchcraft.

Shreyas Srinivas (Mar 23 2023 at 16:28):

I hope I can use this thread for further questions about notation that I am trying out in this file: When defining a subset of a finset, the set brackets/builder notation constructs a set. At first I hoped there would be a simple way to translate this into a Finset. It seems more convoluted though since Finset consists of a multiset and a nodup predicate and so on. What's the quickest (notationally neatest) way to do this?:

Simple example:

def inNeighbours {V : Type u} [Fintype V] (G : Digraph V) (v : V) : Finset V := {w  Fintype.elems | G.isEdge w v}

Shreyas Srinivas (Mar 23 2023 at 16:28):

This is an error because the RHS is a set. But I am creating a subset of Fintype.elems which is a Finset.

Ruben Van de Velde (Mar 23 2023 at 16:33):

Do we not have nice syntax for Finset.filter?

Kyle Miller (Mar 23 2023 at 16:40):

No, generally you use Finset.filter directly.

Also, @Shreyas Srinivas, one thing to know about the Finset API is that you almost never need to worry about the fact it's a multiset with a nodup predicate. There should be enough functions to construct whatever Finset you need.

Kyle Miller (Mar 23 2023 at 16:41):

If you want to use set notation, you can try using docs4#Set.toFinset, which works if you have enough Fintype/Decidable instances lying around.

Kyle Miller (Mar 23 2023 at 16:42):

It can look like this

def inNeighbours {V : Type u} [Fintype V] (G : Digraph V) (v : V) : Finset V :=
  {w | G.isEdge w v}.toFinset

though you might need to write it as Set.toFinset {w | G.isEdge w v}. I expect you'll need a [DecidableRel G.isEdge] argument.

Note that you shouldn't use docs4#Fintype.elems -- the API expects that you use docs4#Finset.univ (i.e., lemmas are about the latter, not the former).

Shreyas Srinivas (Mar 23 2023 at 16:49):

Ah okay got it. Thanks :)

Additionally @Sebastian Ullrich that macOS issue occurred again.

Shreyas Srinivas (Mar 23 2023 at 17:06):

Kyle Miller said:

It can look like this

def inNeighbours {V : Type u} [Fintype V] (G : Digraph V) (v : V) : Finset V :=
  {w | G.isEdge w v}.toFinset

though you might need to write it as Set.toFinset {w | G.isEdge w v}. I expect you'll need a [DecidableRel G.isEdge] argument.

Note that you shouldn't use docs4#Fintype.elems -- the API expects that you use docs4#Finset.univ (i.e., lemmas are about the latter, not the former).

Since I am not using this for computations yet, I marked the defs noncomputable and then lean stopped asking for a DecidableRel instance


Last updated: Dec 20 2023 at 11:08 UTC