Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 148 lines (122 sloc) 4.53 kB
fccc685 Initial open-source release
MLstate authored
1 (*
2 Copyright © 2011 MLstate
3
4 This file is part of OPA.
5
6 OPA is free software: you can redistribute it and/or modify it under the
7 terms of the GNU Affero General Public License, version 3, as published by
8 the Free Software Foundation.
9
10 OPA is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
13 more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with OPA. If not, see <http://www.gnu.org/licenses/>.
17 *)
18 (**
19 Simple pattern analysis for optimizing javascript pattern matching generation.
20 @author Mathieu Barbin
21 @author Francois Pessaux
22 *)
23
24 (**
25 This module defines private AST in order to have a pattern classification for
26 computing optimization in the javascript back-ends.
27
28 The module contains the definition of these AST, and the analysis and transformation
29 function for projecting QML patterns into the correct kind of patterns, depending on
30 the structure of the pattern and the type of the matched expression.
31 *)
32
d3757ec [fix] typos: various corrections
Arthur Milchior authored
33 (** {6 Flags for pattern termination} *)
fccc685 Initial open-source release
MLstate authored
34 (**
35 Matching sums and column
36 [`closed] means that all cases are present,
37 [`open_] means that the pattern has an open variable.
38 *)
39 type flag = [`closed | `open_]
40 type colvar = flag
41 type rowvar = flag
42
43 val pp_flag : Format.formatter -> flag -> unit
44
45 (** {6 Dedicated Algebra for Pattern manipulation} *)
46
47 type label = Annot.label
48
49 (**
50 A private AST for trivial patterns.
51 *)
52 module T :
53 sig
54 type pat =
55 | Const of label * QmlAst.const_expr
56 | Var of label * Ident.t
57 | Any of label
58 | As of label * pat * Ident.t
59
60 val pp : Format.formatter -> pat -> unit
61 end
62
63 (**
64 A private AST for representing sum patterns.
65 In [qmljsimp] we do not have tags for representing the full structure of the record,
66 so we do not to differenciate row variable.
67
68 The only thing we should take care of, is the [colvar] when we match some fields.
69 If the [colvar] is open, we should then perform an extra check of size.
70 In this case (colvar = `open_), we assert than there were not any open [row_var]
71 in the structure of the pattern (typer inconsistency).
72
73 If the [colvar] is closed, we should not complete the 3-dots with S_PatAny for optimization,
74 that means that the simple fact that the presence of matched field is suffisant for validating
75 the pattern.
76
d3757ec [fix] typos: various corrections
Arthur Milchior authored
77 Invariant: in a list of [pat] of the same level, the [col_var] is homogeneous.
fccc685 Initial open-source release
MLstate authored
78 For optimization of representation, and size computation, the fields are stored into
79 an array rather than a list.
80 *)
81 type pat =
82 | Fields of label * (string * pat) array * rowvar * colvar
83 (**
84 Gathering all fields, and a final info for taging strict pattern.
85 [`open] pattern are those with a Some rowvar, e.g. {[{ a ; ... }]}
86 Invariant: the list is sorted by fields. (lexicographic order)
87 *)
88
89 | Const of label * QmlAst.const_expr
90 (**
91 For sub-patterns only
92 *)
93
94 | Var of label * Ident.t
95 | Any of label
96
97 | As of label * pat * Ident.t
98 (**
99 For extra bindings efficient compilation.
100 *)
101
102
103 val pp : Format.formatter -> pat -> unit
104
105 (** {6 Projection} *)
106
107 type ('pat, 'right_hand) matching = ('pat * 'right_hand) list
108
109 (**
110 The projection of patterns depends on all patterns present in a given
111 pattern matching. For projecting correctly all pattern of a pattern matching,
112 we should analysis all of them.
d3757ec [fix] typos: various corrections
Arthur Milchior authored
113 Invariant: in the [pat] list, the [col_var] is homogeneous
fccc685 Initial open-source release
MLstate authored
114 *)
115 type 'right_hand t =
116 | Trivial of (T.pat, 'right_hand) matching
117 | Pat of (pat, 'right_hand) matching
118
119 (**
120 The type is given because sometimes, the result of structural analysis do not
121 correspond to the inferred type of the pattern, because of e.g. coercion added
122 in the code.
123 e.g.:
124 {[
125 f(e) =
126 match e with
127 | { foo } ->
128 | _ ->
129 ]}
130 and
131 {[
132 type foo = { foo } / { bar }
133 f(e : foo) =
134 match e with
135 | { foo } ->
136 | _ ->
137 ]}
138 In the first case the pattern is open, because the function is polymorphic in column.
139 In the second case, the pattern is strict.
140 If you look just at the structure of patterns, you cannot differenciate the 2 forms.
141 *)
142 val analysis :
143 gamma:QmlTypes.gamma ->
144 annotmap:QmlAst.annotmap ->
145 ty:QmlAst.ty ->
146 (QmlAst.pat * 'right_hand) list ->
147 'right_hand t
Something went wrong with that request. Please try again.