Documentation

Mathlib.Data.Num.Basic

Binary representation of integers using inductive types #

Note: Unlike in Coq, where this representation is preferred because of the reliance on kernel reduction, in Lean this representation is discouraged in favor of the "Peano" natural numbers Nat, and the purpose of this collection of theorems is to show the equivalence of the different approaches.

inductive PosNum :

The type of positive binary numbers.

13 = 1101(base 2) = bit1 (bit0 (bit1 one))

Instances For
    def instDecidableEqPosNum.decEq (xโœ xโœยน : PosNum) :
    Decidable (xโœ = xโœยน)
    Equations
      Instances For
        inductive Num :

        The type of nonnegative binary numbers, using PosNum.

        13 = 1101(base 2) = pos (bit1 (bit0 (bit1 one)))

        Instances For
          def instDecidableEqNum.decEq (xโœ xโœยน : Num) :
          Decidable (xโœ = xโœยน)
          Equations
            Instances For
              inductive ZNum :

              Representation of integers using trichotomy around zero.

              13 = 1101(base 2) = pos (bit1 (bit0 (bit1 one))) -13 = -1101(base 2) = neg (bit1 (bit0 (bit1 one)))

              Instances For
                def instDecidableEqZNum.decEq (xโœ xโœยน : ZNum) :
                Decidable (xโœ = xโœยน)
                Equations
                  Instances For
                    def PosNum.bit (b : Bool) :
                    PosNum โ†’ PosNum

                    bit b n appends the bit b to the end of n, where bit tt x = x1 and bit ff x = x0.

                    Equations
                      Instances For

                        The successor of a PosNum.

                        Equations
                          Instances For
                            def PosNum.isOne :
                            PosNum โ†’ Bool

                            Returns a Boolean for whether the PosNum is one.

                            Equations
                              Instances For
                                def PosNum.add :
                                PosNum โ†’ PosNum โ†’ PosNum

                                Addition of two PosNums.

                                Equations
                                  Instances For
                                    def PosNum.pred' :
                                    PosNum โ†’ Num

                                    The predecessor of a PosNum as a Num.

                                    Equations
                                      Instances For

                                        The predecessor of a PosNum as a PosNum. This means that pred 1 = 1.

                                        Equations
                                          Instances For

                                            The number of bits of a PosNum, as a PosNum.

                                            Equations
                                              Instances For

                                                The number of bits of a PosNum, as a Nat.

                                                Equations
                                                  Instances For
                                                    def PosNum.mul (a : PosNum) :
                                                    PosNum โ†’ PosNum

                                                    Multiplication of two PosNums.

                                                    Equations
                                                      Instances For

                                                        ofNatSucc n is the PosNum corresponding to n + 1.

                                                        Equations
                                                          Instances For

                                                            ofNat n is the PosNum corresponding to n, except for ofNat 0 = 1.

                                                            Equations
                                                              Instances For
                                                                @[instance 100]
                                                                Equations
                                                                  def PosNum.cmp :
                                                                  PosNum โ†’ PosNum โ†’ Ordering

                                                                  Ordering of PosNums.

                                                                  Equations
                                                                    Instances For
                                                                      def castPosNum {ฮฑ : Type u_1} [One ฮฑ] [Add ฮฑ] :
                                                                      PosNum โ†’ ฮฑ

                                                                      castPosNum casts a PosNum into any type which has 1 and +.

                                                                      Equations
                                                                        Instances For
                                                                          def castNum {ฮฑ : Type u_1} [One ฮฑ] [Add ฮฑ] [Zero ฮฑ] :
                                                                          Num โ†’ ฮฑ

                                                                          castNum casts a Num into any type which has 0, 1 and +.

                                                                          Equations
                                                                            Instances For
                                                                              @[instance 900]
                                                                              instance posNumCoe {ฮฑ : Type u_1} [One ฮฑ] [Add ฮฑ] :
                                                                              Equations
                                                                                @[instance 900]
                                                                                instance numNatCoe {ฮฑ : Type u_1} [One ฮฑ] [Add ฮฑ] [Zero ฮฑ] :
                                                                                CoeHTCT Num ฮฑ
                                                                                Equations
                                                                                  def Num.succ' :
                                                                                  Num โ†’ PosNum

                                                                                  The successor of a Num as a PosNum.

                                                                                  Equations
                                                                                    Instances For
                                                                                      def Num.succ (n : Num) :

                                                                                      The successor of a Num as a Num.

                                                                                      Equations
                                                                                        Instances For
                                                                                          def Num.add :
                                                                                          Num โ†’ Num โ†’ Num

                                                                                          Addition of two Nums.

                                                                                          Equations
                                                                                            Instances For
                                                                                              def Num.bit0 :
                                                                                              Num โ†’ Num

                                                                                              bit0 n appends a 0 to the end of n, where bit0 n = n0.

                                                                                              Equations
                                                                                                Instances For
                                                                                                  def Num.bit1 :
                                                                                                  Num โ†’ Num

                                                                                                  bit1 n appends a 1 to the end of n, where bit1 n = n1.

                                                                                                  Equations
                                                                                                    Instances For
                                                                                                      def Num.bit (b : Bool) :
                                                                                                      Num โ†’ Num

                                                                                                      bit b n appends the bit b to the end of n, where bit tt x = x1 and bit ff x = x0.

                                                                                                      Equations
                                                                                                        Instances For
                                                                                                          def Num.size :
                                                                                                          Num โ†’ Num

                                                                                                          The number of bits required to represent a Num, as a Num. size 0 is defined to be 0.

                                                                                                          Equations
                                                                                                            Instances For
                                                                                                              def Num.natSize :
                                                                                                              Num โ†’ โ„•

                                                                                                              The number of bits required to represent a Num, as a Nat. size 0 is defined to be 0.

                                                                                                              Equations
                                                                                                                Instances For
                                                                                                                  def Num.mul :
                                                                                                                  Num โ†’ Num โ†’ Num

                                                                                                                  Multiplication of two Nums.

                                                                                                                  Equations
                                                                                                                    Instances For
                                                                                                                      def Num.cmp :
                                                                                                                      Num โ†’ Num โ†’ Ordering

                                                                                                                      Ordering of Nums.

                                                                                                                      Equations
                                                                                                                        Instances For
                                                                                                                          def Num.toZNum :
                                                                                                                          Num โ†’ ZNum

                                                                                                                          Converts a Num to a ZNum.

                                                                                                                          Equations
                                                                                                                            Instances For
                                                                                                                              def Num.toZNumNeg :
                                                                                                                              Num โ†’ ZNum

                                                                                                                              Converts x : Num to -x : ZNum.

                                                                                                                              Equations
                                                                                                                                Instances For
                                                                                                                                  def Num.ofNat' :
                                                                                                                                  โ„• โ†’ Num

                                                                                                                                  Converts a Nat to a Num.

                                                                                                                                  Equations
                                                                                                                                    Instances For
                                                                                                                                      def ZNum.zNeg :
                                                                                                                                      ZNum โ†’ ZNum

                                                                                                                                      The negation of a ZNum.

                                                                                                                                      Equations
                                                                                                                                        Instances For
                                                                                                                                          def ZNum.abs :
                                                                                                                                          ZNum โ†’ Num

                                                                                                                                          The absolute value of a ZNum as a Num.

                                                                                                                                          Equations
                                                                                                                                            Instances For
                                                                                                                                              def ZNum.succ :
                                                                                                                                              ZNum โ†’ ZNum

                                                                                                                                              The successor of a ZNum.

                                                                                                                                              Equations
                                                                                                                                                Instances For
                                                                                                                                                  def ZNum.pred :
                                                                                                                                                  ZNum โ†’ ZNum

                                                                                                                                                  The predecessor of a ZNum.

                                                                                                                                                  Equations
                                                                                                                                                    Instances For
                                                                                                                                                      def ZNum.bit0 :
                                                                                                                                                      ZNum โ†’ ZNum

                                                                                                                                                      bit0 n appends a 0 to the end of n, where bit0 n = n0.

                                                                                                                                                      Equations
                                                                                                                                                        Instances For
                                                                                                                                                          def ZNum.bit1 :
                                                                                                                                                          ZNum โ†’ ZNum

                                                                                                                                                          bit1 x appends a 1 to the end of x, mapping x to 2 * x + 1.

                                                                                                                                                          Equations
                                                                                                                                                            Instances For
                                                                                                                                                              def ZNum.bitm1 :
                                                                                                                                                              ZNum โ†’ ZNum

                                                                                                                                                              bitm1 x appends a 1 to the end of x, mapping x to 2 * x - 1.

                                                                                                                                                              Equations
                                                                                                                                                                Instances For

                                                                                                                                                                  Converts an Int to a ZNum.

                                                                                                                                                                  Equations
                                                                                                                                                                    Instances For
                                                                                                                                                                      def PosNum.sub' :
                                                                                                                                                                      PosNum โ†’ PosNum โ†’ ZNum

                                                                                                                                                                      Subtraction of two PosNums, producing a ZNum.

                                                                                                                                                                      Equations
                                                                                                                                                                        Instances For

                                                                                                                                                                          Converts a ZNum to Option PosNum, where it is some if the ZNum was positive and none otherwise.

                                                                                                                                                                          Equations
                                                                                                                                                                            Instances For

                                                                                                                                                                              Converts a ZNum to a PosNum, mapping all out of range values to 1.

                                                                                                                                                                              Equations
                                                                                                                                                                                Instances For
                                                                                                                                                                                  def PosNum.sub (a b : PosNum) :

                                                                                                                                                                                  Subtraction of PosNums, where if a < b, then a - b = 1.

                                                                                                                                                                                  Equations
                                                                                                                                                                                    Instances For
                                                                                                                                                                                      def Num.ppred :
                                                                                                                                                                                      Num โ†’ Option Num

                                                                                                                                                                                      The predecessor of a Num as an Option Num, where ppred 0 = none

                                                                                                                                                                                      Equations
                                                                                                                                                                                        Instances For
                                                                                                                                                                                          def Num.pred :
                                                                                                                                                                                          Num โ†’ Num

                                                                                                                                                                                          The predecessor of a Num as a Num, where pred 0 = 0.

                                                                                                                                                                                          Equations
                                                                                                                                                                                            Instances For
                                                                                                                                                                                              def Num.div2 :
                                                                                                                                                                                              Num โ†’ Num

                                                                                                                                                                                              Divides a Num by 2

                                                                                                                                                                                              Equations
                                                                                                                                                                                                Instances For
                                                                                                                                                                                                  def Num.ofZNum' :
                                                                                                                                                                                                  ZNum โ†’ Option Num

                                                                                                                                                                                                  Converts a ZNum to an Option Num, where ofZNum' p = none if p < 0.

                                                                                                                                                                                                  Equations
                                                                                                                                                                                                    Instances For
                                                                                                                                                                                                      def Num.ofZNum :
                                                                                                                                                                                                      ZNum โ†’ Num

                                                                                                                                                                                                      Converts a ZNum to an Option Num, where ofZNum p = 0 if p < 0.

                                                                                                                                                                                                      Equations
                                                                                                                                                                                                        Instances For
                                                                                                                                                                                                          def Num.sub' :
                                                                                                                                                                                                          Num โ†’ Num โ†’ ZNum

                                                                                                                                                                                                          Subtraction of two Nums, producing a ZNum.

                                                                                                                                                                                                          Equations
                                                                                                                                                                                                            Instances For
                                                                                                                                                                                                              def Num.psub (a b : Num) :

                                                                                                                                                                                                              Subtraction of two Nums, producing an Option Num.

                                                                                                                                                                                                              Equations
                                                                                                                                                                                                                Instances For
                                                                                                                                                                                                                  def Num.sub (a b : Num) :

                                                                                                                                                                                                                  Subtraction of two Nums, where if a < b, a - b = 0.

                                                                                                                                                                                                                  Equations
                                                                                                                                                                                                                    Instances For
                                                                                                                                                                                                                      def ZNum.add :
                                                                                                                                                                                                                      ZNum โ†’ ZNum โ†’ ZNum

                                                                                                                                                                                                                      Addition of ZNums.

                                                                                                                                                                                                                      Equations
                                                                                                                                                                                                                        Instances For
                                                                                                                                                                                                                          def ZNum.mul :
                                                                                                                                                                                                                          ZNum โ†’ ZNum โ†’ ZNum

                                                                                                                                                                                                                          Multiplication of ZNums.

                                                                                                                                                                                                                          Equations
                                                                                                                                                                                                                            Instances For
                                                                                                                                                                                                                              def ZNum.cmp :
                                                                                                                                                                                                                              ZNum โ†’ ZNum โ†’ Ordering

                                                                                                                                                                                                                              Ordering on ZNums.

                                                                                                                                                                                                                              Equations
                                                                                                                                                                                                                                Instances For
                                                                                                                                                                                                                                  def PosNum.divModAux (d : PosNum) (q r : Num) :

                                                                                                                                                                                                                                  Auxiliary definition for PosNum.divMod.

                                                                                                                                                                                                                                  Equations
                                                                                                                                                                                                                                    Instances For
                                                                                                                                                                                                                                      def PosNum.divMod (d : PosNum) :

                                                                                                                                                                                                                                      divMod x y = (y / x, y % x).

                                                                                                                                                                                                                                      Equations
                                                                                                                                                                                                                                        Instances For
                                                                                                                                                                                                                                          def PosNum.div' (n d : PosNum) :

                                                                                                                                                                                                                                          Division of PosNum

                                                                                                                                                                                                                                          Equations
                                                                                                                                                                                                                                            Instances For
                                                                                                                                                                                                                                              def PosNum.mod' (n d : PosNum) :

                                                                                                                                                                                                                                              Modulus of PosNums.

                                                                                                                                                                                                                                              Equations
                                                                                                                                                                                                                                                Instances For
                                                                                                                                                                                                                                                  def Num.div :
                                                                                                                                                                                                                                                  Num โ†’ Num โ†’ Num

                                                                                                                                                                                                                                                  Division of Nums, where x / 0 = 0.

                                                                                                                                                                                                                                                  Equations
                                                                                                                                                                                                                                                    Instances For
                                                                                                                                                                                                                                                      def Num.mod :
                                                                                                                                                                                                                                                      Num โ†’ Num โ†’ Num

                                                                                                                                                                                                                                                      Modulus of Nums.

                                                                                                                                                                                                                                                      Equations
                                                                                                                                                                                                                                                        Instances For
                                                                                                                                                                                                                                                          def Num.gcdAux :
                                                                                                                                                                                                                                                          โ„• โ†’ Num โ†’ Num โ†’ Num

                                                                                                                                                                                                                                                          Auxiliary definition for Num.gcd.

                                                                                                                                                                                                                                                          Equations
                                                                                                                                                                                                                                                            Instances For
                                                                                                                                                                                                                                                              def Num.gcd (a b : Num) :

                                                                                                                                                                                                                                                              Greatest Common Divisor (GCD) of two Nums.

                                                                                                                                                                                                                                                              Equations
                                                                                                                                                                                                                                                                Instances For
                                                                                                                                                                                                                                                                  def ZNum.div :
                                                                                                                                                                                                                                                                  ZNum โ†’ ZNum โ†’ ZNum

                                                                                                                                                                                                                                                                  Division of ZNum, where x / 0 = 0.

                                                                                                                                                                                                                                                                  Equations
                                                                                                                                                                                                                                                                    Instances For
                                                                                                                                                                                                                                                                      def ZNum.mod :
                                                                                                                                                                                                                                                                      ZNum โ†’ ZNum โ†’ ZNum

                                                                                                                                                                                                                                                                      Modulus of ZNums.

                                                                                                                                                                                                                                                                      Equations
                                                                                                                                                                                                                                                                        Instances For
                                                                                                                                                                                                                                                                          def ZNum.gcd (a b : ZNum) :

                                                                                                                                                                                                                                                                          Greatest Common Divisor (GCD) of two ZNums.

                                                                                                                                                                                                                                                                          Equations
                                                                                                                                                                                                                                                                            Instances For
                                                                                                                                                                                                                                                                              def castZNum {ฮฑ : Type u_1} [Zero ฮฑ] [One ฮฑ] [Add ฮฑ] [Neg ฮฑ] :
                                                                                                                                                                                                                                                                              ZNum โ†’ ฮฑ

                                                                                                                                                                                                                                                                              castZNum casts a ZNum into any type which has 0, 1, + and neg

                                                                                                                                                                                                                                                                              Equations
                                                                                                                                                                                                                                                                                Instances For
                                                                                                                                                                                                                                                                                  @[instance 900]
                                                                                                                                                                                                                                                                                  instance znumCoe {ฮฑ : Type u_1} [Zero ฮฑ] [One ฮฑ] [Add ฮฑ] [Neg ฮฑ] :
                                                                                                                                                                                                                                                                                  CoeHTCT ZNum ฮฑ
                                                                                                                                                                                                                                                                                  Equations