Inductive Types #
The main type construction in Lean is the inductive type. We will see how to define inductive types and how to define functions by recursion on these types.
We have already seen one special case of inductive types: structures. In ShortAnswer, we saw another special case, namely enumerated types. We will now see a third special case, namely non-recursive inductive types. Finally, we will see the general case, namely recursive inductive types.
The Answer type is a non-recursive inductive type, with constructors a short answer or a short answer with reason.
- just (ans : ShortAnswer) : Answer
- because (ans : ShortAnswer) (reason : String) : Answer
Instances For
An example of a term in the Answer type.
Equations
- Answer.eg₁ = Answer.because ShortAnswer.yes "I said so"
Instances For
The short answer to an answer.
Equations
- (Answer.just ans).short = ans
- (Answer.because ans reason).short = ans
Instances For
The dot notation.
A LongAnswer is either a ShortAnswer or a LongAnswer with a reason.
- just (ans : ShortAnswer) : LongAnswer
- because (ans : LongAnswer) (reason : String) : LongAnswer
Instances For
An example of a term in the LongAnswer type.
Equations
- LongAnswer.eg₂ = (LongAnswer.just ShortAnswer.yes).because "I said so"
Instances For
An example of a term in the LongAnswer type.
Equations
- LongAnswer.eg₃ = ((LongAnswer.just ShortAnswer.yes).because "I said so").because "Did you not hear me?"
Instances For
The short answer to a long answer. Lean has convenient notation for pattern matching
Equations
- (LongAnswer.just ans).short = ans
- (ans.because reason).short = ans.short
Instances For
The short answer to a long answer. Expanded definition
Equations
- (LongAnswer.just ans).short' = ans
- (ans.because reason).short' = ans.short'
Instances For
An example of a term in the LongAnswer type.
Equations
- LongAnswer.eg₄ = ShortAnswer.yes.just.justify "I said so"
Instances For
An example of a term in the LongAnswer type.
Equations
- LongAnswer.eg₅ = (ShortAnswer.yes.just.justify "I said so").justify "Did you not hear me?"
Instances For
An example of a term in the LongAnswer type.
Equations
- LongAnswer.eg₆ = ((ShortAnswer.yes.just.justify "I said so").justify "Did you not hear me?").justify "Please listen next time."
Instances For
There are no terms of this type.
- justified (and : EmptyAnswer) (reason : String) : EmptyAnswer
Instances For
A function from EmptyAnswer to α for any type α. Such a function can only exist if EmptyAnswer has no terms.
Equations
- EmptyAnswer.mapToType α (prev.justified reason) = EmptyAnswer.mapToType α prev
Instances For
A function from EmptyAnswer to α for any type α. Such a function can only exist if EmptyAnswer has no terms.
Equations
- EmptyAnswer.mapToType' α a = EmptyAnswer.rec (fun (prev : EmptyAnswer) (j : String) (ih : α) => ih) a