SizeOf #

class SizeOf (α : Sort u) :
Sort (max1u)
  • The "size" of an element, a natural number which decreases on fields of each inductive type.

    sizeOf : αNat

SizeOf is a typeclass automatically derived for every inductive type, which equips the type with a "size" function to Nat. The default instance defines each constructor to be 1 plus the sum of the sizes of all the constructor fields.

This is used for proofs by well-founded induction, since every field of the constructor has a smaller size than the constructor itself, and in many cases this will suffice to do the proof that a recursive function is only called on smaller values. If the default proof strategy fails, it is recommended to supply a custom size measure using the termination_by argument on the function definition.


    Declare SizeOf instances and theorems for types declared before SizeOf. From now on, the inductive compiler will automatically generate SizeOf instances and theorems.

    def default.sizeOf (α : Sort u) :

    Every type α has a default SizeOf instance that just returns 0 for every element of α.

    instance instSizeOf (α : Sort u) :
    theorem sizeOf_default {α : Sort u_1} (n : α) :
    sizeOf n = 0
    theorem sizeOf_nat (n : Nat) :
    sizeOf n = n
    instance instSizeOfForAllUnit {α : Sort u_1} [inst : SizeOf α] :
    SizeOf (Unitα)
    • instSizeOfForAllUnit = { sizeOf := fun f => sizeOf (f ()) }
    theorem sizeOf_thunk {α : Sort u_1} [inst : SizeOf α] (f : Unitα) :
    theorem Unit.sizeOf (u : Unit) :
    sizeOf u = 1
    theorem Bool.sizeOf_eq_one (b : Bool) :
    sizeOf b = 1
    noncomputable def Lean.Name.sizeOf :

    We manually define the Lean.Name instance because we use an opaque function for computing the hashcode field.

    noncomputable instance Lean.instSizeOfName :