Module docstring
{"# SizeOf ","Declare SizeOf instances and theorems for types declared before SizeOf.
From now on, the inductive compiler will automatically generate SizeOf instances and theorems.
"}
{"# SizeOf ","Declare SizeOf instances and theorems for types declared before SizeOf.
From now on, the inductive compiler will automatically generate SizeOf instances and theorems.
"}
SizeOf
structure
(α : Sort u)
/--
`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.
-/
class SizeOf (α : Sort u) where
/-- The "size" of an element, a natural number which decreases on fields of
each inductive type. -/
sizeOf : α → Nat
default.sizeOf
definition
(α : Sort u) : α → Nat
/--
Every type `α` has a default `SizeOf` instance that just returns `0`
for every element of `α`.
-/
protected def default.sizeOf (α : Sort u) : α → Nat
| _ => 0
instSizeOfDefault
instance
(α : Sort u) : SizeOf α
/--
Every type `α` has a low priority default `SizeOf` instance that just returns `0`
for every element of `α`.
-/
instance (priority := low) instSizeOfDefault (α : Sort u) : SizeOf α where
sizeOf := default.sizeOf α
sizeOf_default
theorem
(n : α) : sizeOf n = 0
@[simp] theorem sizeOf_default (n : α) : sizeOf n = 0 := rfl
instSizeOfNat
instance
: SizeOf Nat
sizeOf_nat
theorem
(n : Nat) : sizeOf n = n
@[simp] theorem sizeOf_nat (n : Nat) : sizeOf n = n := rfl
instSizeOfForallUnit
instance
[SizeOf α] : SizeOf (Unit → α)
sizeOf_thunk
theorem
[SizeOf α] (f : Unit → α) : sizeOf f = sizeOf (f ())
Unit.sizeOf
theorem
(u : Unit) : sizeOf u = 1
@[simp] theorem Unit.sizeOf (u : Unit) : sizeOf u = 1 := rfl
Bool.sizeOf_eq_one
theorem
(b : Bool) : sizeOf b = 1
@[simp] theorem Bool.sizeOf_eq_one (b : Bool) : sizeOf b = 1 := by cases b <;> rfl
Lean.Name.sizeOf
definition
: Name → Nat
Lean.instSizeOfName
instance
: SizeOf Name
Lean.Name.anonymous.sizeOf_spec
theorem
: sizeOf anonymous = 1
@[simp] theorem Name.anonymous.sizeOf_spec : sizeOf anonymous = 1 :=
rfl
Lean.Name.str.sizeOf_spec
theorem
(p : Name) (s : String) : sizeOf (str p s) = 1 + sizeOf p + sizeOf s
Lean.Name.num.sizeOf_spec
theorem
(p : Name) (n : Nat) : sizeOf (num p n) = 1 + sizeOf p + sizeOf n