Commit Graph

140 Commits (a282c170f914da8b01bdcba52b7518a3ed7b8bce)

Author SHA1 Message Date
Gabriel Scherer 3527653363 matching: add a comment suggested by Florian Angeletti's review 2020-05-01 21:58:28 +02:00
Gabriel Scherer 50fdc06fcd [minor] matching.ml: tune ~scopes handling 2020-05-01 21:56:27 +02:00
Gabriel Scherer ac1243cbd1 matching: use heads in the make_*_matching specialization calls 2020-05-01 21:56:27 +02:00
Gabriel Scherer a26e509c57 matching: finally, merge matcher and Context.ctx_matcher 2020-05-01 21:56:15 +02:00
Gabriel Scherer 387955e189 matching: refine the types in Context.ctx_matcher 2020-05-01 21:56:15 +02:00
Gabriel Scherer 0f5a1c4d1e matching: consolidate all matcher_ functions in a single matcher_head
This commit is delicate and needs a careful review.

The `matcher_of_pattern` function is a temporary measure to reduce the
invasiveness of the patch, and make it easier to review.

(Note for reviewers: in the previous version the Record case had
a funny handling of Any, but it is in fact equivalent to just adding
omegas as we now do in all cases.)

There are two obvious directions for improvement:

- Get rid of matcher_of_pattern and pass a head directly to the
  various make_matching_* functions.

- Try to factorize this code with ctx_matcher which, it is now
  obvious, does essentially the same thing.

Another, less immediate area of attack would be to consider
a presentation of Pattern_head.t where the Any case can be statically
ruled out -- maybe the description could have two levels, one
isomorphic to option (Any or not?) and one for non-any heads.
2020-05-01 21:56:15 +02:00
Gabriel Scherer 97caf289b5 matching: specialize_matrix uses non-empty rows 2020-05-01 21:56:15 +02:00
Gabriel Scherer 67c56e6ec8 matching: matcher_* take Simple.pattern arguments 2020-05-01 21:56:15 +02:00
Stephen Dolan 2986beaa78 Replace Location.t with Lambda.scoped_location in Lambda code
This commit threads scopes through translation from Typedtree to
Lambda, extending the scopes when entering functions, modules,
classes and methods.
2020-04-27 12:58:53 +01:00
Stephen Dolan a5292808d2 Introduce the Lambda.scoped_location type 2020-04-27 12:51:46 +01:00
Gabriel Scherer 4943a373f3
Merge pull request #9464 from gasche/rematch-exceptionless-matcher
pattern-matching refactoring: simplify `Default_env.specialize_matrix` by avoiding exceptions
2020-04-23 12:18:13 +02:00
Thomas Refis 9f49a71e90
Add forgotten substitution when compiling anonymous modules (#9477)
Fixes #9375
2020-04-23 10:55:40 +02:00
Gabriel Scherer 6e153b1e71 matching: remove the OrPat exception by handling Tpat_or on the caller side
(This commit is more tricky than the previous ones in the patchset
and requires a careful review.)

This refactoring clarifies and simplifies the specialize_matrix logic
by getting rid of the OrPat exception used in a higher-order
way (or sometimes not used in certain matchers, when it is possible to
"push" the or-pattern down in the pattern). Instead it uses an
arity-based criterion to implement the or-pattern-related logic once
in the specializer, instead of having to implement it in each
matcher. As a result, the compiler improves a bit as it will push
or-patterns down during specialization in valid situations that were
not implemented before -- probably because they are not terribly
important in practice: all constant and arity-1 constructs benefit
from optimized or-pattern handling, in particular the following are
new:
- lazy patterns
- non-constant polymorphic variants
- size-one records and arrays
2020-04-22 21:32:38 +02:00
Gabriel Scherer a2e6746f64 matching: refactor recursive specialization functions for clarity
Several functions in the pattern-matching compiler do recursive
"specialization" through a filter_rec helper written in tail-call
style with a 'rem' parameter containing the matrix rows yet to be
processed as input. Typical returns of the function are

  foo :: filter_rec rem

to add a new output, and

  filter_rec (foo :: rem)

to add a new input to be processed (usually by partial decomposition
of the current input row).

Some places would contain the declaration (let rem = filter_rec rem)
to factorize outputs of the first kind, but this gives a programming
style that is very confusing as `rem` may now represent either an
input or an output of the filter.

Using better types (as will be done farther away in the
pattern-matching refactoring) avoids this problem completely:
specialization then has different input and output types (typically,
from general to half-simple patterns), so incorrectly mixing inputs
and outputs is impossible. Yay typing.
2020-04-22 09:58:04 +02:00
Gabriel Scherer 537e395cc0 matching: pass explicit arity to Default_environment.specialize 2020-04-18 11:08:12 +02:00
Leo White 59fac074fe
Merge pull request #9349 from lpw25/inline-hint
Add [@inlined hint] attribute
2020-04-17 08:27:58 +01:00
Leo White f46abe1916 Add [@inlined hint] attribute 2020-04-16 15:58:03 +01:00
Gabriel Scherer 6ceba91930 matching: minor code factorization of compile_* functions 2020-04-15 17:33:27 +02:00
Gabriel Scherer b2a2c94211 matching: factorize compile_match and compile_match_nonempty 2020-04-15 17:32:42 +02:00
Gabriel Scherer d67a091fb0 matching: inline split_and_precompile* to clarify compile_* 2020-04-15 17:30:59 +02:00
Gabriel Scherer 2d9aafce62 matching: what_is_cases returns a head 2020-04-14 09:58:34 +02:00
Gabriel Scherer d05f86e13b [minor] matching: rename `group_var` into `simple_omega_like`
(Report from Florian Angeletti)
2020-04-07 06:41:20 +02:00
Thomas Refis acd44f90af [REVIEW REQUIRED] matching: simplify can_group
review of can_group factorization by @gasche:

> I reviewed the change to `can_group` and believe it is correct.
> (I knew it would be nicer as a binary operation!)
>
> The different treatments of Lazy and Tuple/Record does look a bit odd,
> but I believe that it is actually the right thing to write.
>
> In the Tuple or Record case, the idea is that we can group Any heads
> with the Tuple/Record case and just explode all of them (including the
> Any cases) according to the tuple/record arity.
>
> In the Lazy case, the corresponding choice would be to add Any values
> to the Lazy group, and force all the elements of the group. This is
> not so nice, because intuitively we shouldn't force Lazy values unless
> we have to.
>
> One may expect that in general the argument of the pattern will be
> forced anyway, as long as there is at least one Lazy pattern above in
> the matrix, so it doesn't really matter whether we include the Any
> patterns in the forced group or not. I would argue that (1) even if
> that was true, it would be semantically dubious to handle Any that way
> (see a more precise criterion below), (2) I am not actually sure that
> this is true, what if the first group gets found unreachable by
> splits in following columns?
>
>     # type void = | ;;
>     # match (lazy (print_endline "foo"), (None : void option)) with
>       | lazy (), Some _ -> .
>       | _, None -> false;;
>     - : bool = false
>
> This gives the following decision tree for whether Any is included in
> the group:
>
> - Can the absence of Any be used to generate nice/efficient tests for
>   the heads of the group? In that case, don't include Any.
>   (Not included: all the Constant cases, Array (discriminate on size),
>    String, etc.)
>
> - Is Any always exactly equivalent to a more-defined head for values
>   of this type? In that case, do include Any, otherwise do not.
>   (Included: Variant, Record)
>   (Not included: Lazy)
2020-04-07 06:41:20 +02:00
Greta Yorsh 824ce35492
Replace caml_int_compare and caml_float_compare with primitives (#2324) 2020-03-26 10:58:10 +01:00
Thomas Refis 3196a70671
matching: use polymorphic variants to classify general/half_simple/simple patterns (#9361)
Also: these types are now instances of Typedtree.pattern_data.

Co-authored-by: Gabriel Scherer <gabriel.scherer@gmail.com>
2020-03-20 14:12:37 +01:00
Stephen Dolan 07d0192b22 Generate the same backtraces for method send in bytecode and native 2020-03-16 18:15:09 +00:00
Stephen Dolan cc7b687a02 Generate the same locations for raises in bytecode and native code 2020-03-16 18:15:09 +00:00
Nicolás Ojeda Bär 57d329e07b
Deprecate -annot (#2141)
* Move driver code from Cmt2annot to Read_cmt
* Move cmt2annot.ml into typing/
* make depend
* Use standard error handling
* Move specific logic to read_cmt
* Do not pass full cmt record as argument
* Better locations
* Emit .annot files produced from cmt data
* Remove direct calls to Stypes
* Deprecate -annot
* Changes
* make depend
* Adapt doc
* make -C tools depend
2020-03-13 12:59:34 +01:00
Thomas Refis 7fd5dd9fdc
matching: ctx_matcher with heads (#9359)
Co-authored-by: Gabriel Scherer <gabriel.scherer@gmail.com>
2020-03-11 13:58:56 +01:00
Thomas Refis abeaef92fb
Merge pull request #9322 from trefis/rematch-structured-rows
[matching.ml cleanup] structured rows
2020-03-10 17:02:14 +01:00
Gabriel Scherer 9ba926c774 matching: deduplicate the debug information printing 2020-03-10 14:15:46 +01:00
Gabriel Scherer 3388d0abcb matching: deduplicate the matrix pretty-printing functions 2020-03-10 14:15:43 +01:00
Thomas Refis f3e6fc709b matching: add an assert false in divide variant
This triggers if the first column contains something other than a
variant (this is similar to the "get_key"s for other heads).
2020-03-10 12:12:43 +01:00
Thomas Refis 3d339ea6ea matching: remove dedicated type, use labelled arguments 2020-03-10 12:12:39 +01:00
Florian Angeletti 5a22a88dcd matching: more precise argo argument 2020-03-10 12:12:36 +01:00
Thomas Refis 8790988aa4 matching: start using Pattern_head 2020-03-10 12:08:39 +01:00
Thomas Refis 4b41481d2c matching: Add intermediary types to keep track of the compilation state of pms 2020-03-10 12:08:39 +01:00
Thomas Refis d52dd5c33e Add a unique id to every signature item 2020-03-05 13:34:12 +01:00
Thomas Refis bfc2b13f42 matching: move things around 2020-02-26 16:01:12 +01:00
Thomas Refis c591c17d52 matching: half_compiled_row: (head, patl, act) => - 12 assert false 2020-02-26 16:01:10 +01:00
Thomas Refis 488a588169 matching: formatting noise
There were some patches on the file since our last series of patch that
didn't respect the formatting.
So we're calling ocamlformat again (the same version as before,
something < 0.10) so everything rebases cleanly.
2020-02-20 13:11:33 +01:00
Gabriel Scherer 8938886721 -dno-locations: hide source locations (and debug events) from IR dumps
This PR was tested with also the -dsel, -dlinear output (also fixed to
not-print locations), but the output is architecture-dependent so this
part of the test was removed.
2020-01-09 15:25:16 +01:00
Jacques-Henri Jourdan 52661f14a4 Make ocamlc preserve events after primitives even if they are at tail position. 2020-01-09 00:16:42 +01:00
Stephen Dolan 291cbaf1d1 Make tuple matching optimisation apply to Lswitch and Lstringswitch. 2019-12-19 14:29:34 +00:00
Florian Angeletti 345cfb6a98 manual: make labels mandatory 2019-12-05 17:31:04 +01:00
Fourchaux 1f9474c63c Fixing typos (#9162) 2019-12-04 19:38:02 +00:00
Drup 9c8b63f4ce Annotated Asttypes.constant's string with content location. 2019-11-13 16:08:40 +01:00
Gabriel Scherer 92bfafc1ac
Merge pull request #8805 from stedolan/statmemprof-comballoc-native
Keep information about allocation sizes, for statmemprof, and use during GC.
2019-11-06 13:44:14 +01:00
Gabriel Scherer dec6513105 Enforce a structure invariant in [Tpat_value p] patterns
pat_attributes and pat_extra nodes should be on the inner value
pattern, rather than on the outer computation pattern, so that a user
looking for a specific value-pattern constructor with a specific
attribute does not need to consider the Tpat_value case specifically.

(Thanks to Alain Frisch for this suggestion.)
2019-10-31 13:29:16 +01:00
Gabriel Scherer 312253ce82 split patterns into "value patterns" and "computation patterns"
Value patterns match on a value (the result of computation), while
computation patterns handle the effects (hint hint) of
a computation. The only forms of computation patterns in OCaml today
are value patterns and exception patterns (exception p).

The sub-pattern `p` of the `lazy p` construction should be
a computation pattern, rather than a value pattern. This pull-request
does not make this change.

Most of the changes in this PR are boilerplate -- it really is a lot
of work now to add a new syntactic category to the typed-tree
syntax. This boilerplate is fairly automatic and should be easy to
review.

There is a subtle part to the patch, though: the implementation of the
pattern type-checking. It now has to reconstruct the value/computation
distinction (absent from the parse-tree), and return values from two
different types. Instead of splitting the type-checker in several
functions (which risked code duplications), I choose to use a GADT to
have the same [type_pat] function return two different types depending
on the caller. This is the least invasive way to adapt this part of
the codebase, whose inherent complexity is so large (unfortunately)
that adding a GADT to the mix barely makes a difference.
2019-10-31 13:29:16 +01:00