Init.SizeOf

# SizeOf #

class SizeOf (α : Sort u) :
Sort (max 1 u)
• sizeOf : αNat

The "size" of an element, a natural number which decreases on fields of each inductive type.

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.

Instances

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) :
αNat

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

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

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

Equations
Instances For
noncomputable instance Lean.instSizeOfName :
@[simp]
@[simp]
theorem Lean.Name.str.sizeOf_spec (p : Lake.Name) (s : String) :
sizeOf () = 1 + +
@[simp]
theorem Lean.Name.num.sizeOf_spec (p : Lake.Name) (n : Nat) :
sizeOf () = 1 + +