Zulip Chat Archive
Stream: mathlib4
Topic: RFC: Boolean Semiring
Rudy Peterson (Oct 29 2025 at 21:33):
Hello,
In my research it would be helpful to have a boolean Semiring, where addition is or instead of xor.
Would it make sense to add this, or would such a new Add instance conflict with the existing Boolean Ring instances, where addition is xor?
Thanks!
Aaron Liu (Oct 29 2025 at 21:41):
sounds similar to docs#IdemSemiring
Kenny Lau (Oct 29 2025 at 21:43):
if you want a different instance, you should probably use a type synonym, i.e. do something like
def Bool' := Bool
and then construct the instance on Bool'
Aaron Liu (Oct 29 2025 at 21:43):
I think they're talking about docs#BooleanRing
Aaron Liu (Oct 29 2025 at 21:43):
not putting a different semiring instance on Bool
Yaël Dillies (Oct 29 2025 at 21:49):
Hey, can you clarify what exactly is a boolean semiring? Is it a specific (family of) ring(s) or is it any semiring satisfying some axioms? If so, do those axioms happen to be captured by docs#IdemSemiring ?
Yaël Dillies (Oct 29 2025 at 21:54):
nLab claims there are at least five different definitions of "boolean semirings" in the literature
Rudy Peterson (Oct 29 2025 at 22:05):
Thanks for all of the replies!
Basically, I need a Semiring instance for Bool where Add is or ||. I don't mean a type or family of ring(s).
For my general work, I believe that docs#IdemSemiring is too restrictive, since I also care about Semirings in general, not just a Semiring instance for Bool.
I am worked on weighted formal language theory, where automata and regular expressions also produce a weight, and the type of the weights need to satisfy the Semiring axioms.
Currently, I am working on showing that one may construct an unweighted/standard NFA given a weighted NFA with boolean weights, and for this case I believe I need the Add operation to be boolean or.
Rudy Peterson (Oct 29 2025 at 22:07):
Perhaps for this general construction (weighted to unweighted NFA) I do not need the boolean semiring specifically, but I believe it would be the most elegant
Rudy Peterson (Oct 29 2025 at 22:09):
I performed a similar construction to produce an unweighted/normal docs#DFA from a boolean weighted DFA, and this case the Semiring addition operator did not matter since for weighted DFAs only semiring multiplication matters (i think, or at least in this case)
Kenny Lau (Oct 29 2025 at 22:10):
see my answer
Rudy Peterson (Oct 29 2025 at 22:11):
Kenny Lau said:
if you want a different instance, you should probably use a type synonym, i.e. do something like
def Bool' := Bool
and then construct the instance onBool'
is this the standard way to get around something like this in Mathlib or Lean when there are instances with different implementations for the same typeclass and data type?
Kenny Lau (Oct 29 2025 at 22:12):
yes
Edward van de Meent (Oct 29 2025 at 22:13):
(see for examples docs#Additive and docs#Opposite)
Rudy Peterson (Oct 29 2025 at 22:15):
Is there a way to suppress instances from imports and then define my own version of the instance? Or maybethe boolean ring file is not that pervasively imported across Matlib, and for my work I could just avoid importing modules that export that?
Aaron Liu (Oct 29 2025 at 22:16):
You could disable the instances locally
Aaron Liu (Oct 29 2025 at 22:18):
in Counterexamples.CharPZeroNeCharZero there's a semiring addmonoidwithone that's sort of like what you want (the addition and multiplication line up) that they use WithZero Unit
Aaron Liu (Oct 29 2025 at 22:19):
oh it actually has a semiring instance
import Mathlib
#synth Semiring (WithZero Unit)
Damiano Testa (Oct 29 2025 at 22:22):
Depending on which commit of mathlib you use, WithZero Unit may be Fin 2.
Rudy Peterson (Oct 29 2025 at 22:24):
Aaron Liu said:
You could disable the instances locally
do you mean like this? It doesn't get rid of the prior instance entirely...?
section boolean
attribute [-instance] instAddBool_mathlib
-- CommRing.toNonUnitalCommRing.toNonUnitalNonAssocCommRing.toNonUnitalNonAssocCommSemiring.toDistrib.toAdd
#synth Add Bool
instance myAddBool : Add Bool where add := Bool.or
-- myAddBool
#synth Add Bool
...
end boolean
Aaron Liu (Oct 29 2025 at 22:27):
yeah you really need to disable a lot of instances
Rudy Peterson (Oct 29 2025 at 22:29):
I guess for my use-case it would be easier for me to define my own local Semiring Bool instance...
Aaron Liu (Oct 29 2025 at 22:30):
just use WithZero Unit instead it will save you a lot of trouble
Rudy Peterson (Oct 29 2025 at 22:31):
Would a Semiring Bool instance (with Add as or) be good for Mathlib in general
Aaron Liu said:
just use
WithZero Unitinstead it will save you a lot of trouble
what does this do?
Aaron Liu (Oct 29 2025 at 22:31):
Rudy Peterson said:
Would a Semiring Bool instance (with Add as or) be good for Mathlib in general
No it would not since Bool already has a different semiring instance
Aaron Liu (Oct 29 2025 at 22:32):
Rudy Peterson said:
Aaron Liu said:
just use
WithZero Unitinstead it will save you a lot of troublewhat does this do?
Gives you a way to refer to the semiring you want without fighting typeclass
Last updated: Dec 20 2025 at 21:32 UTC