Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 145 lines (107 sloc) 5.255 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 Pattern normalisation.
20 @author Rudy Sicard
21 @author Mathieu Barbin (interfaces, documentation)
22 *)
23
24 (**
25 Onion takes a Onionlang:ONIONLANG parameter which define a simplified interface for the langage
26 Onion define a generic internal representation for pattern
27
28 All you have to do is to write conversion functions from the external langage to the internal representation and vice-versa.
29 See QmlLang (implements ONIONLANG)
30 You can then use Onion(MyOnionLang).normalize to normalize the internal represention and convert back.
31
32 This is already done for Qml. So you can directly use Transform.optimize which do the normalisation process
33
34
35 Current implementation :
36 Take an arbitrary pattern disjunction and transform it to recursive matching (i.e. a decision tree)
37 over disjunction of surfaces patterns, such that backtracking is never necessary.
38
39 A surface patterns can only check immediate property like record shape or constant value.
40
41
42 General scheme
43
44 The functor operates on onion pattern, a internal representation containing :
45 - conjunction (like opa record) of patterns,
46 - disjonction (like or pattern and match) of patterns,
47 - simple patterns (const and any),
48 - and corresponding production (an expression) or an explicit recusion on a binded pattern variable
49
50
51
52
53 To garantee that backtracking is not necessary, all intersections of branches in the disjunction must be empty.
54
55 The algorithm can be summarized as
56 1) make everything complete, instantiating _ and ... whenever possible
57 this separates patterns as complete, unstrict and any with the order complete, unstrict and any
58 i.e. no any pattern or unstrict must be checked before a complete pattern ...
59
60 currently this can duplicate pattern and production,
61 it is planned that production duplication will have no effect in the LLVM (sharing, need low level primitives)
62
63 2) separate patterns as complete, unstrict and any
64
65 3) regroup each pattern depending on the surface pattern
66 a) reprocess each group as a subpattern
67 b) when the group contains conjunction of pattern, nest the processing field by field (current implementation)
68 or do the intersection of each field valid branch set (futur implemention).
69 The nesting can exponentially duplicate pattern and production,
70 but it garantees that no test is done twice (compared to backtracking, the ocaml approach, not planned),
71 production will be shared in LLVM.
72 The intersection approach garantees that no test is done twice and that no duplication occurs
73
74
75 During the process missing case are detected (in this case a failure branch is introduced) and useless branch are removed.
76
77 TODO : Useless pattern (i.e. hidden patterns) can be detected when a branch has been erased
78 Currently a pass is doing this in opa, so it is not urgent
79 It temporaly only work on typed pattern. With untyped pattern completion verification cannot be done.
80 *)
81
82 (** {6 Env} *)
83
84 (**
85 For using the pass, the typer env is stored internally.
86 At the end, you should reset env, for the GC.
87 *)
88 module Env :
89 sig
90 val reset : unit -> unit
91 end
92
93 (** {6 Conversion} *)
94
95 (**
96 A abtract type for representing an complete analysed pattern matching.
97 Contains the analysed version of all patterns, and productions
98 *)
99 type pattern_matching
100
101 (**
102 Given the ty of the matched expression, and the pattern matching,
103 represented by a sorted list associating patterns and production,
104 do the conversion, and produces an internal represention of the
105 pattern matching
106
107 The analyse may fail using [QmlError].
108 *)
109 val conversion :
110 gamma:QmlTypes.gamma ->
111 annotmap:QmlAst.annotmap ->
112 label:Annot.label ->
113 matched_expr:QmlAst.expr ->
114 patterns:(QmlAst.pat * QmlAst.expr) list -> pattern_matching
115
116 (** {6 Normalization} *)
117
118 (**
119 A private type for representing a normalized pattern matching.
120 *)
121 type normalized_pattern_matching
122
123 (**
124 Pattern normalization.
125 The normalization may fail using [QmlError].
126 *)
127 val normalize : pattern_matching -> normalized_pattern_matching
128
129 (** {6 Get back to qml} *)
130
131 (**
132 Currently, the normalized code is not used.
133 We use this function only for checking and debuging the pass.
134 The normalized code may be used according to some heuristic.
135 (e.g. if the size of the code does not explose)
136 *)
137 val generation :
138 normalized_pattern_matching ->
139 QmlAst.annotmap * QmlAst.expr
140
141 (** {6 Errors} *)
142
143 val has_public_exceptions : unit -> bool
144 val flush_exceptions_fmt : Format.formatter -> unit -> unit
Something went wrong with that request. Please try again.