Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About the pattern matching algorithm in OCaml

I am writing a compiler for a functional language I designed with OCaml. I want my little language to have the feature of pattern matching, however, I got stuck in coming up with an algorithm to implement it. It seems really complicated as I dig into the problem. I can't find much useful information about the corresponding algorithm with google. I will be appreciated if someone can give me some hint or point me to the resources. Or are there any tricks to take advantage of OCaml's power in pattern matching to solve this problem so that I don't need to implement it? Thanks!

like image 607
hooray9 Avatar asked Nov 27 '13 02:11

hooray9


People also ask

What is pattern matching in OCaml?

Pattern matching comes up in several places in OCaml: as a powerful control structure combining a multi-armed conditional, unification, data destructuring and variable binding; as a shortcut way of defining functions by case analysis; and as a way of handling exceptions.

What is pattern matching algorithm explain with example?

Pattern matching algorithms are the algorithms that are used to figure out whether a specific string pattern occurs in a string text. Two of the most widely used pattern matching algorithms are the Naive Algorithm for pattern matching and the pattern matching algorithm using finite automata.

How does pattern matching work?

Pattern Matching works by "reading" through text strings to match patterns that are defined using Pattern Matching Expressions, also known as Regular Expressions. Pattern Matching can be used in Identification as well as in Pre-Classification Processing, Page Processing, or Storage Processing.

Can you pattern match strings OCaml?

There is a way to match strings as a list of characters, using a function from SML (which you can write in OCaml) called 'explode' and 'implode' which --respectively -- take a string to a char list and vice versa.


1 Answers

There's a few good papers on compiling pattern matching by some of the people behind OCaml. In particular see Compiling Pattern Matching to Good Decision Trees and Optimizing Pattern Matching. It might also be useful to go over this stackoverflow post.

like image 118
gsg Avatar answered Sep 21 '22 14:09

gsg