You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Manual/Language/InductiveTypes/Structures.lean
+150-8Lines changed: 150 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -12,6 +12,8 @@ open Verso.Genre Manual
12
12
13
13
open Lean.Parser.Command («inductive» «structure» declValEqns computedField)
14
14
15
+
set_option guard_msgs.diff true
16
+
15
17
#doc (Manual) "Structure Declarations" =>
16
18
%%%
17
19
tag := "structures"
@@ -316,11 +318,21 @@ tag := "structure-inheritance"
316
318
Structures may be declared as extending other structures using the optional {keywordOf Lean.Parser.Command.declaration (parser:=«structure»)}`extends` clause.
317
319
The resulting structuretypehasallofthefieldsofalloftheparentstructuretypes.
Theresultingstructurehasa {deftech}_field resolution order_ that affects the values of fields.
323
+
When possible, this resolution order is the [C3 linearization](https://en.wikipedia.org/wiki/C3_linearization) of the structure's parents.
324
+
Essentially, the field resolution order should be a total ordering of the entire set of parents such that every {keywordOf Lean.Parser.Command.declaration (parser:=«structure»)}`extends` list is in order.
325
+
When there is no C3 linearization, a heuristic is used to find an order nonetheless.
326
+
Every structuretypeisfirstinitsownfieldresolutionorder.
Parent projections are in the child structure's namespace, and their name is the parent structure's name preceded by`to`.
353
424
354
425
::: example"Structure type inheritance with overlapping fields"
355
426
In this example, a {lean}`Textbook` is a {lean}`Book` that is also an {lean}`AcademicWork`:
@@ -518,14 +589,85 @@ structure E extends A, B where
518
589
#guard_msgs in
519
590
#check E.mk
520
591
/--
521
-
error: parent field type mismatch, field 'x' from parent 'A'' has type
592
+
error: field type mismatch, field 'x' from parent 'A'' has type
522
593
Int : Type
523
594
but is expected to have type
524
595
Nat : Type
525
596
-/
526
597
#guard_msgs in
527
598
structureFextends A, A' where
528
599
600
+
```
601
+
602
+
603
+
The {keywordOf Lean.Parser.Command.print}`#print` command displays the most important information about structuretypes,includingthe {tech}[parent projections], all the fields with their default values, the constructor, and the {tech}[field resolution order].
604
+
When working with deep hierarchies that contain inheritance diamonds, this information can be very useful.
529
605
606
+
::: example"{keyword}`#print` and Structure Types"
530
607
608
+
This collection of structuretypesmodelsavarietyofbicycles,bothelectricandnon-electricandbothordinary-sizedandlargefamilybicycles.
609
+
Thefinalstructuretype, {lean}`ElectricFamilyBike`, contains a diamond in its inheritance graph, because both {lean}`FamilyBike` and {lean}`ElectricBike` extend {lean}`Bicycle`.
0 commit comments