Zulip Chat Archive

Stream: lean4

Topic: type constructor constraint


Mikhail Katkov (Oct 28 2025 at 16:33):

Appologies for possible neglegence,

Suppose I have (many) inductive type for Capacity:

inductive Capacity where
| c1
| c2

class CapacityValue (a:Type) where
  capacity_of : Nat

Now I want to have a type that can only be constructed if its parameter is smaller than capacity, something like

inductive WithinCapacity where
| nil
| within_capacity (c:Type) (n:Nat) [CapacityValue c] [n<= CapacityValue.capacity_of c] : ...

Can I dot it? and how if I can?

Kenny Lau (Oct 28 2025 at 16:34):

please use #backticks to format your code

Kenny Lau (Oct 28 2025 at 16:35):

that should be possible, you just won't be able to use [] for it, in Lean [] and () have specialised meanings and are used for different situations

Kenny Lau (Oct 28 2025 at 16:36):

[] is only used for classes

Mikhail Katkov (Oct 28 2025 at 17:36):

So how technically I can impose constraint?

Kenny Lau (Oct 28 2025 at 17:42):

use ()

Mikhail Katkov (Oct 28 2025 at 18:29):

class CapacityValue (a:Type) where
  capacity_of : Nat

  inductive WithinCapacity where
| nil : WithinCapacity
| within_capacity (c:Type) (n:Nat) [CapacityValue c] (n<= CapacityValue.capacity_of c) : WithinCapacity

unexpected token '<='; expected ')'

Aaron Liu (Oct 28 2025 at 18:33):

You need to name the hypothesis

Mikhail Katkov (Oct 28 2025 at 18:35):

Oh! thanks a lot.

For completness, if anyone encounter that:

class CapacityValue (a:Type) where
  capacity_of : Nat

  inductive WithinCapacity where
| nil : WithinCapacity
| within_capacity (c:Type) (n:Nat) [CapacityValue c] ( _ : n<= CapacityValue.capacity_of c) : WithinCapacity

Works!


Last updated: Dec 20 2025 at 21:32 UTC