Debian Codegraph @

dev/doc/case-repr.md

raw symbols SCIP
1 ## Case representation
2
3 Starting from Coq 8.14, the term representation of pattern-matching uses a
4 so-called *compact form*. Compared to the previous representation, the major
5 difference is that all type and term annotations on lambda and let abstractions
6 that were present in branches and return clause of pattern-matchings were
7 removed. In order to keep the ability to construct the old expanded form out of
8 the new compact form, the case node also makes explicit data that was stealthily
9 present in the expanded return clause, namely universe instances and parameters
10 of the inductive type being eliminated.
11
12 ### ML Representation
13
14 The case node now looks like
15 ```
16 Case of
17 case_info *
18 Instance.t * (* universe instances of the inductive *)
19 constr array * (* parameters of the inductive *)
20 case_return * (* erased return clause *)
21 case_invert * (* SProp inversion data *)
22 constr * (* scrutinee *)
23 case_branch array (* erased branches *)
24 ```
25 where
26 ```
27 type case_branch = Name.t binder_annot array * constr
28 type case_return = Name.t binder_annot array * types
29 ```
30
31 For comparison, pre-8.14 case nodes were defined as follows.
32 ```
33 Case of
34 case_info *
35 constr * (* annotated return clause *)
36 case_invert * (* SProp inversion data *)
37 constr * (* scrutinee *)
38 constr array (* annotated branches *)
39 ```
40
41 ### Typing Rules and Invariants
42
43 Disregarding the `case_info` cache and the SProp inversion, the typing rules for
44 the case node can be given as follows.
45
46 Provided
47 - Γ ⊢ c : Ind@{u} pms Indices
48 - Inductive Ind@{i} Δ : forall Θ, Type := cᵢ : forall Ξᵢ, Ind Δ Aᵢ
49 - Γ, Θ@{i := u}{Δ := pms} ⊢ p : Type
50 - Γ, Ξᵢ@{i := u}{Δ := pms} ⊢ snd brᵢ : p{Θ := Aᵢ{Δ := pms}}
51
52 Then Γ ⊢ Case (_, u, pms, ( _, p), _, c, br) : p{Θ := Indices}
53
54 In particular, this implies that Γ ⊢ pms : Δ@{i := u}. Parameters are stored in
55 the same order as in the application node.
56
57 The u universe instance must be a valid instance for the corresponding
58 inductive type, in particular their length must coincide.
59
60 The `Name.t binder_annot array` appearing both in the return clause and
61 in the branches must satisfy these invariants:
62 - For branches, it must have the same length as the corresponding Ξᵢ context
63 (including let-ins)
64 - For the return clause, it must have the same length as the context
65 Θ, self : Ind@{u} pms Θ (including let-ins). The last variable appears as
66 the term being destructed and corresponds to the variable introduced by the
67 "as" clause of the user-facing syntax.
68 - The relevance annotations must match with the corresponding sort of the
69 variable from the context.
70
71 Note that the annotated variable array is reversed w.r.t. the context,
72 i.e. variables appear left to right as in standard practice.
73
74 Let-bindings can appear in Δ, Θ or Ξᵢ, since they are arbitrary
75 contexts. As a general rule, let bindings appear as binders but not as
76 instances. That is, they MUST appear in the variable array, but they MUST NOT
77 appear in the parameter array.
78
79 Example:
80 ```
81 Inductive foo (X := tt) : forall (Y := X), Type := Foo : forall (Z := X), foo.
82
83 Definition case (x : foo) : unit := match x as x₀ in foo with Foo _ z => z end
84 ```
85 The case node of the `case` function is represented as
86 ```
87 Case (
88 _,
89 Instance.empty,
90 [||],
91 ([|(Y, Relevant); (x₀, Relevant)|], unit), (* let (Y := tt) in fun (x₀ : foo) => unit *)
92 NoInvert,
93 #1,
94 [|
95 ([|(z, Relevant)|], #1) (* let z := tt in z *)
96 |]
97 )
98 ```
99
100 This choice of representation for let-bindings requires access to the
101 environment in some cases, e.g. to compute branch reduction. There is a
102 fast-path for non-let-containing inductive types though, which are the vast
103 majority.
104
105 ### Porting plugins
106
107 The conversion functions from and to the expanded form are:
108 - `[Inductive, EConstr].expand_case` which goes from the compact to the expanded
109 form and cannot fail (assuming the term was well-typed)
110 - `[Inductive, EConstr].contract_case` which goes the other way and will
111 raise anomalies if the expanded forms are not fully eta-expanded.
112
113 As such, it is always painless to convert to the old representation. Converting
114 the other way, you must ensure that all the terms you provide the
115 compatibility function with are fully eta-expanded, **including let-bindings**.
116 This works as expected for the common case with eta-expanded branches but will
117 fail for plugins that generate non-eta-expanded branches.
118
119 Some other useful variants of these functions are:
120 - `Inductive.expand_case_specif`
121 - `EConstr.annotate_case`
122 - `EConstr.expand_branch`