You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The new names are certainly better.
I believe we should try to realign the tensors with Mathlib; it seems there has been some work on it.
I am going through the current tensor layer against Mathlib. The main difference is that Physlib's tensor indices are Fin n, while Mathlib's PiTensorProduct API works over general index types.
Was there a specific reason to use Fin n rather than a general finite index type, for example the index notation or computing with concrete colours?
@pariandrea Yes - it was essentially to make sure index notation worked. Basically so if someone wrote down a specific tensor, e.g. T | a b then it was clear what the indexing type would be. If there is a way around this though, I think this would be great, as I think a lot of the pain of working with tensors comes from having to deal with casting the indexing types.
4. @pariandrea Yes - it was essentially to make sure index notation worked. Basically so if someone wrote down a specific tensor, e.g. T | a b then it was clear what the indexing type would be. If there is a way around this though, I think this would be great, as I think a lot of the pain of working with tensors comes from having to deal with casting the indexing types.
Yes, absolutely, I'm happy to look into it. In the meantime, #1943, the last PR of the tensor series, lets us raise and lower indices for SUGRA. After that I'll stop building on top of the current layer and go back to the basics.
On crossToSlotInv, I'd rather remove it as part of that rework than now. It is the ordinary contraction plus a cast that tells Lean the index list it gets back is the original one, so it only exists because of the casting you mention. Removing it today would just move that cast into the lemmas that use it. Once the indexing stops producing these casts, it can go and nothing needs to replace it.
@pariandrea I wonder if defining a weaker form of equality, equality up-to-reindexing, would useful. I think this might actually be the only type of equality which matters
@jstoobysmith thanks, both points are close to what I have been looking at.
The direction I am exploring is to move away from numbered slots altogether and index tensors by general finite types, built directly on Mathlib's PiTensorProduct. My impression is that Vector n would still number the slots, so the casts would come back in a different form.
The two main ideas are these. The first is that a colour carries its dual: it records its own label and the label of its dual (and of its conjugate, where there is one), so the dual of the dual is the colour itself by definition and needs no cast.
The second is contraction as linear maps: a tensor with some dual slots and some ordinary slots is read as a Mathlib linear map between tensors, and contracting two such tensors along their shared slots is composing the maps. Associativity of contraction is then Mathlib's associativity of composition, and the construction is built almost entirely from Mathlib's tensor product API rather than our own.
On equality up to reindexing. I think the equality should say which relabelling it uses, not only that one exists. Otherwise swapping the two slots of a tensor would count as equality, so every tensor would equal its transpose and antisymmetry could not be stated.
I am testing this locally and the results are encouraging.
I can push this on a fork of mathlib on my own github for you to check it out.
I am still not ready for a PR however feedback is always welcome.
What is the best way to collaborate on some code which is still in test phase before the PR ?
One of the advantages of using something like Vector n apparently is that it has a normal form, which can make it easier for Lean to compare two sides.(Not sure if it is much help though)
@jstoobysmith I have opened a draft PR: #2014. It adds a new tensor library in Physlib/Mathematics/Tensors next to the existing one, so the two can be compared.
The main ideas are three. Tensors are indexed by any type, directly on Mathlib's PiTensorProduct, so a product lives on the sum of the two index types and a contraction of any set of pairs is one operation. Nothing is renumbered along the way, so I did not try Vector n.
Today the dual of a color comes from τ, and τ (τ c) = c holds only up to a proof, which is where many of the casts come from. In the PR a color is a kind, such as the Lorentz vector index, and a variance, up or down. The dual keeps the kind and flips the variance, so c.dual.dual = c holds by rfl and a statement that dualizes twice needs no cast. The basis labels depend only on the kind, so an index and its dual are labelled by the same μ, as in v^μ and α_μ. Saying that two bases are dual is then just contr (e_μ ⊗ e^ν) = if μ = ν then 1 else 0, with no matching of labels and no dual-basis class.
The index notation keeps its syntax, {T | μ ν ⊗ U | ν ρ}ᵀ. Each product chain becomes one contraction of all its repeated names at once, rather than one contraction per pair with a renumbering in between, and the free indices come out numbered in the order they are written.
What we obtain is the core of the existing generic layer in about a quarter of the code. Lines with casts or relabelings go from 563 to 17, lines with Fin arithmetic from 721 to 2, and set_option from 40 to 0. The components of a contraction are one formula for any bases.
If the direction looks right, I would split it into a stack of smaller PRs. What is not ported yet is listed in the PR.
@jstoobysmith The notation is still indexed by Fin n, as before. What changes is the operations underneath which are on general index types. The product is a tensor on Fin 2 ⊕ Fin 2, and one Tensor.contr contracts every repeated name and lands directly on Fin 2 with a literal list of colours.
@jstoobysmith
I've updated #2014 with the Pauli matrices as a test of the new machinery, and I'm quite excited about the result. They're in Bispinors/Pauli/Tensor.lean, next to the existing Relations.lean so you can compare.
If you like the direction, I'd go on and split it into PRs and port the rest of the library.
@pariandrea Ok, I think your Pauli matrix results look at lot better then the existing approach. I'm not sure about the notation ↓β though as one might not actually want to assign one color to be 'up' or 'down' (if this is what this means?).
@jstoobysmith Yes, that's what it means. A tensor is defined with each index up or down, and ↓β lowers β with the metric while ↑β raises it. To contract two indices they need opposite variance, so if both are upper you lower one of them with ↓. The only conventions are the index placement in the definition of the Pauli matrices, and which index of the metric is contracted when lowering. Lowering contracts with the first index of the metric, v_ν = v^μ g_μν, and for spinors ψ_β = ψ^α ε_αβ. This is fixed once in toDual and is the same as upstream, where toDualMapAtIndex contracts with slot 0 of the metric. Is this the convention you want?
In the PR the Pauli matrices are defined once with upper indices, as pauliTensor, and lowered with ↓ where needed. Upstream defines each covariant and contravariant form by name:
σ^^^ is toTensor pauliMatrix, here pauliTensor | μ α β
σ_^^ is pauliCo, here pauliTensor | ↓μ α β
σ^__ is pauliContrDown, here pauliTensor | μ ↓α ↓β
σ___ is pauliCoDown, here pauliTensor | ↓μ ↓α ↓β
I find this ad hoc and hard to scale, since every combination of indices would need its own name.
The arrows do ask each color to be up or down. If you'd rather avoid that, the alternative I see is a single marker like τ(β) that flips the variance whichever way it points. Which would you prefer?
@jstoobysmith It is an error, ↑μ needs a visibly lower index. I'd keep both. τ(μ) flips the variance whichever way it points, so it works when a color is not assigned up or down. The arrows say which way the index goes and catch a wrong placement, which reads closer to the physics when the variance is known. Both keep the upstream lowering convention v_ν = v^μ g_μν (Is this the one we want ?). The Pauli matrices stay defined once as pauliTensor, and any other index placement is written with τ or an arrow, so we avoid naming every combination of indices.
Activity
jstoobysmith commentedon Oct 27, 2026
As a start, I think some of the contraction results could be renamed.
TensorSpecies.Tensor.contrTTensorSpecies.Tensor.contrTensorSpecies.Tensor.Pure.contrPTensorSpecies.Tensor.Pure.contrTensorSpecies.Tensor.Pure.contrPCoeffTensorSpecies.Tensor.Pure.contrCoeffTensorSpecies.Tensor.Pure.contrPMultilinearTensorSpecies.Tensor.Pure.contrMultilinearTensorSpecies.Tensor.crossToEndTensorSpecies.Tensor.contrProdTensorSpecies.Tensor.crossToSlotTensorSpecies.Tensor.contrAtIndexTensorSpecies.Tensor.crossToSlotEquivTensorSpecies.Tensor.contrAtIndexEquivTensorSpecies.Tensor.crossToSlotInv@pariandrea What do you think about this?
pariandrea commentedon Oct 28, 2026
The new names are certainly better.
I believe we should try to realign the tensors with Mathlib; it seems there has been some work on it.
I am going through the current tensor layer against Mathlib. The main difference is that Physlib's tensor indices are Fin n, while Mathlib's PiTensorProduct API works over general index types.
Was there a specific reason to use Fin n rather than a general finite index type, for example the index notation or computing with concrete colours?
jstoobysmith commentedon Oct 28, 2026
@pariandrea Yes - it was essentially to make sure index notation worked. Basically so if someone wrote down a specific tensor, e.g.
T | a bthen it was clear what the indexing type would be. If there is a way around this though, I think this would be great, as I think a lot of the pain of working with tensors comes from having to deal with casting the indexing types.jstoobysmith commentedon Oct 28, 2026
Want me to open a PR with the above changes in name?
pariandrea commentedon Oct 28, 2026
Yes that would be great!
pariandrea commentedon Oct 28, 2026
Yes, absolutely, I'm happy to look into it. In the meantime, #1943, the last PR of the tensor series, lets us raise and lower indices for SUGRA. After that I'll stop building on top of the current layer and go back to the basics.
On crossToSlotInv, I'd rather remove it as part of that rework than now. It is the ordinary contraction plus a cast that tells Lean the index list it gets back is the original one, so it only exists because of the casting you mention. Removing it today would just move that cast into the lemmas that use it. Once the indexing stops producing these casts, it can go and nothing needs to replace it.
jstoobysmith commentedon Oct 29, 2026
@pariandrea I wonder if defining a weaker form of equality, equality up-to-reindexing, would useful. I think this might actually be the only type of equality which matters
jstoobysmith commentedon Oct 30, 2026
I think we should explore here whether we are better of using
Vector nto index the tensors compared to maps fromFin n -> C.pariandrea commentedon Oct 30, 2026
@jstoobysmith thanks, both points are close to what I have been looking at.
The direction I am exploring is to move away from numbered slots altogether and index tensors by general finite types, built directly on Mathlib's PiTensorProduct. My impression is that Vector n would still number the slots, so the casts would come back in a different form.
The two main ideas are these. The first is that a colour carries its dual: it records its own label and the label of its dual (and of its conjugate, where there is one), so the dual of the dual is the colour itself by definition and needs no cast.
The second is contraction as linear maps: a tensor with some dual slots and some ordinary slots is read as a Mathlib linear map between tensors, and contracting two such tensors along their shared slots is composing the maps. Associativity of contraction is then Mathlib's associativity of composition, and the construction is built almost entirely from Mathlib's tensor product API rather than our own.
On equality up to reindexing. I think the equality should say which relabelling it uses, not only that one exists. Otherwise swapping the two slots of a tensor would count as equality, so every tensor would equal its transpose and antisymmetry could not be stated.
I am testing this locally and the results are encouraging.
I can push this on a fork of mathlib on my own github for you to check it out.
I am still not ready for a PR however feedback is always welcome.
What is the best way to collaborate on some code which is still in test phase before the PR ?
jstoobysmith commentedon Oct 30, 2026
How would this interact with index notation?
Best way to collaborate is to make a draft PR.
jstoobysmith commentedon Oct 30, 2026
One of the advantages of using something like
Vector napparently is that it has a normal form, which can make it easier for Lean to compare two sides.(Not sure if it is much help though)pariandrea commentedon Nov 2, 2026
@jstoobysmith I have opened a draft PR: #2014. It adds a new tensor library in
Physlib/Mathematics/Tensorsnext to the existing one, so the two can be compared.The main ideas are three. Tensors are indexed by any type, directly on Mathlib's
PiTensorProduct, so a product lives on the sum of the two index types and a contraction of any set of pairs is one operation. Nothing is renumbered along the way, so I did not tryVector n.Today the dual of a color comes from
τ, andτ (τ c) = cholds only up to a proof, which is where many of the casts come from. In the PR a color is a kind, such as the Lorentz vector index, and a variance, up or down. The dual keeps the kind and flips the variance, soc.dual.dual = cholds byrfland a statement that dualizes twice needs no cast. The basis labels depend only on the kind, so an index and its dual are labelled by the sameμ, as inv^μandα_μ. Saying that two bases are dual is then justcontr (e_μ ⊗ e^ν) = if μ = ν then 1 else 0, with no matching of labels and no dual-basis class.The index notation keeps its syntax,
{T | μ ν ⊗ U | ν ρ}ᵀ. Each product chain becomes one contraction of all its repeated names at once, rather than one contraction per pair with a renumbering in between, and the free indices come out numbered in the order they are written.What we obtain is the core of the existing generic layer in about a quarter of the code. Lines with casts or relabelings go from 563 to 17, lines with
Finarithmetic from 721 to 2, andset_optionfrom 40 to 0. The components of a contraction are one formula for any bases.If the direction looks right, I would split it into a stack of smaller PRs. What is not ported yet is listed in the PR.
jstoobysmith commentedon Nov 2, 2026
@pariandrea What is the type of something like
{T | μ ν ⊗ U | ν ρ}ᵀ?pariandrea commentedon Nov 2, 2026
@jstoobysmith The notation is still indexed by Fin n, as before. What changes is the operations underneath which are on general index types. The product is a tensor on Fin 2 ⊕ Fin 2, and one Tensor.contr contracts every repeated name and lands directly on Fin 2 with a literal list of colours.
jstoobysmith commentedon Nov 2, 2026
Maybe we could add some examples involving the Pauli matrices, to see how the proofs differ in specific cases?
jstoobysmith commentedon Nov 2, 2026
@pariandrea pinging for the above.
pariandrea commentedon Nov 7, 2026
@jstoobysmith
I've updated #2014 with the Pauli matrices as a test of the new machinery, and I'm quite excited about the result. They're in
Bispinors/Pauli/Tensor.lean, next to the existingRelations.leanso you can compare.If you like the direction, I'd go on and split it into PRs and port the rest of the library.
jstoobysmith commentedon Nov 8, 2026
@pariandrea Ok, I think your Pauli matrix results look at lot better then the existing approach. I'm not sure about the notation
↓βthough as one might not actually want to assign one color to be 'up' or 'down' (if this is what this means?).pariandrea commentedon Nov 8, 2026
@jstoobysmith Yes, that's what it means. A tensor is defined with each index up or down, and
↓βlowers β with the metric while↑βraises it. To contract two indices they need opposite variance, so if both are upper you lower one of them with↓. The only conventions are the index placement in the definition of the Pauli matrices, and which index of the metric is contracted when lowering. Lowering contracts with the first index of the metric,v_ν = v^μ g_μν, and for spinorsψ_β = ψ^α ε_αβ. This is fixed once intoDualand is the same as upstream, wheretoDualMapAtIndexcontracts with slot0of the metric. Is this the convention you want?In the PR the Pauli matrices are defined once with upper indices, as
pauliTensor, and lowered with↓where needed. Upstream defines each covariant and contravariant form by name:σ^^^istoTensor pauliMatrix, herepauliTensor | μ α βσ_^^ispauliCo, herepauliTensor | ↓μ α βσ^__ispauliContrDown, herepauliTensor | μ ↓α ↓βσ___ispauliCoDown, herepauliTensor | ↓μ ↓α ↓βI find this ad hoc and hard to scale, since every combination of indices would need its own name.
The arrows do ask each color to be up or down. If you'd rather avoid that, the alternative I see is a single marker like
τ(β)that flips the variance whichever way it points. Which would you prefer?jstoobysmith commentedon Nov 9, 2026
I think I prefer
τ(β)which is what we currently have.In the current set up what would e.g.
do? Is it equivalent to
or would it call an error?
pariandrea commentedon Nov 9, 2026
@jstoobysmith It is an error, ↑μ needs a visibly lower index. I'd keep both. τ(μ) flips the variance whichever way it points, so it works when a color is not assigned up or down. The arrows say which way the index goes and catch a wrong placement, which reads closer to the physics when the variance is known. Both keep the upstream lowering convention v_ν = v^μ g_μν (Is this the one we want ?). The Pauli matrices stay defined once as pauliTensor, and any other index placement is written with τ or an arrow, so we avoid naming every combination of indices.
jstoobysmith commentedon Nov 10, 2026
v_ν = g_νμ v^μ would probably be more standard.
pariandrea commentedon Nov 10, 2026
Yes I agree