百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

Mach7

> 编程语言
开源

用于 C++ 的函数式编程风格模式匹配库

1.3K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

用于 C++ 的函数式编程风格模式匹配库

Mach7: Pattern Matching for C++

by Yuriy Solodkyy, Gabriel Dos Reis, Bjarne Stroustrup

Abstract

Pattern matching is an abstraction mechanism that can greatly simplify source code. Commonly, pattern matching is built into a language to provide better syntax, faster code, correctness guarantees and improved diagnostics. Mach7 is a library solution to pattern matching in C++ that maintains many of these features. All the patterns in Mach7 are user-definable, can be stored in variables, passed among functions, and allow the use of open class hierarchies.

Mach7 by Example

Fibonacci numbers demonstrates the use of patterns with built-in types in Mach7:

cpp
// Fibonacci numbers
int fib(int n)
{
    var m;

    Match(n)
    {
      Case(1)     return 1;
      Case(2)     return 1;
      Case(2*m)   return sqr(fib(m+1)) - sqr(fib(m-1));
      Case(2*m+1) return sqr(fib(m+1)) + sqr(fib(m));
    }
    EndMatch
}

Lambda calculator demonstrates use of pattern matching to decompose objects and nested patterns:

…

Next example demonstrates that the library can deal efficiently and in a type-safe manner with non-polymorphic classes like boost::variant as well.

cpp
void print(const boost::variant& v)
{
    var d; var f; var n;

    Match(v)
    {
      Case(C(d)) cout  (f)) cout  ] [ filemask*.cpp ... ]
    build [ syntax | timing | cmp | doc | clean | test | check ]

######  Commands supported so far:

    build [ pgo | repro | tmp |  |  ] [ filemask*.cpp ... ] - build given C++ files
    build        - Build all examples using the most recent MS Visual C++ compiler installed
    build unit   - Build all unit tests
    build syntax - Build all supported library options combination for syntax variations
    build timing - Build all supported library options combination for timing variations
    build cmp    - Build all executables for comparison with other languages
    build doc    - Build Mach7 documentation
    build clean  - Clean all built examples
    build test   - Run all built examples
    build check  - Run those examples for which there are correct_output/*.out files and
                   check that output is the same

######  Modifiers:

           pgo   - Perform Profile-Guided Optimization on produced executables
           repro - In case of error, create and compile a pre-processed repro
           tmp   - Keep temporaries
            - Use a specific version of Visual Studio to compile the source
                   code.  can be one of the following:
                    - 2019 - Visual C++ 16.0
                    - 2017 - Visual C++ 15.0
                    - 2015 - Visual C++ 14.0
                    - 2013 - Visual C++ 12.0
                    - 2012 - Visual C++ 11.0
                    - 2010 - Visual C++ 10.0
                    - 2008 - Visual C++  9.0
                    - 2005 - Visual C++  8.0
                    - 2003 - Visual C++  7.1
                      0000 - Do not use any VS to set up the environment, I will set it up by myself
           - Target architecture. Can be one of the following: x86, x64, arm

Talks
-------------

 * "[Accept No Visitors](http://bit.ly/AcceptNoVisitorsVideo)". [CppCon 2014](http://cppcon.org/). September 12, 2014. Bellevue, WA. [[slides](http://bit.ly/AcceptNoVisitors), [video](http://bit.ly/AcceptNoVisitorsVideo)]
 * "[Mach7: The Design and Evolution of a Pattern Matching Library for C++](http://bit.ly/Mach7CppNowVideo)". [C++ Now 2014](http://cppnow.org). May 14, 2014. Aspen, CO. [[slides](http://bit.ly/Mach7CppNow), [video](http://bit.ly/Mach7CppNowVideo)]

Publications
------------

 * Y.Solodkyy, G.Dos Reis, B.Stroustrup. "[Open Pattern Matching for C++: Extended Abstract](http://bit.ly/OPM13EA)" In Proceedings of the 2013 companion publication for conference on Systems, programming, & applications: software for humanity (SPLASH '13). ACM, New York, NY, USA, pp. 97-98. [pdf](http://bit.ly/OPM13EA), [slides](http://bit.ly/OPM13Slides), [notes](http://bit.ly/OPM13Notes), [poster](http://bit.ly/OPM13Poster), [project](http://bit.ly/Mach7)
 * Y.Solodkyy, G.Dos Reis, B.Stroustrup. "[Open Pattern Matching for C++](http://bit.ly/OPM-GPCE13)" In Proceedings of the 12th international conference on Generative programming: concepts & experiences (GPCE '13). ACM, New York, NY, USA, pp. 33-42. [pdf](http://bit.ly/OPM-GPCE13), [slides](http://bit.ly/OPM13Slides), [notes](http://bit.ly/OPM13Notes), [poster](http://bit.ly/OPM13Poster), [project](http://bit.ly/Mach7)
 * Y.Solodkyy. "[Simplifying the Analysis of C++ Programs](http://bit.ly/PhdThesis)" Ph.D. Thesis. Texas A&M University. August 2013. [pdf](http://bit.ly/SolodkyyThesis), [slides](http://bit.ly/SolodkyyThesisSlides)
 * Y.Solodkyy, G.Dos Reis, B.Stroustrup. "[Open and Efficient Type Switch for C++](http://bit.ly/TS-OOPSLA12)" In Proceedings of the ACM international conference on Object Oriented Programming Systems Languages and Applications (OOPSLA '12). ACM, New York, NY, USA, pp. 963-982. [pdf](http://bit.ly/TS-OOPSLA12), [slides](http://bit.ly/TS12Slides), [notes](http://bit.ly/TS12Notes), [poster](http://bit.ly/OPM13Poster), [extras](http://bit.ly/TS12Extras), [project](http://bit.ly/Mach7)]

Others about Mach7
------------------
 * 2016-02-27: [Andrzej's C++ blog](https://akrzemi1.wordpress.com/?s=Mach7): [Another polymorphism](https://akrzemi1.wordpress.com/2016/02/27/another-polymorphism/)

Projects using Mach7
--------------------
 * [Yodl](https://github.com/forflo/yodl): a VHDL frontend for Yosys
 * [Arrow](https://github.com/arrow-lang/arrow): Arrow is a fast (as or faster than C) general-purpose programming language. It does not employ a garbage collector and has minimal runtime overhead.

License
-------

Mach7 is licensed under the [BSD License](LICENSE).

Support
-------

If you have any question about Mach7 or have trouble using it, the best way to get answers is to post an
[issue](https://github.com/solodon4/Mach7/issues) and label it as
[Question](https://github.com/solodon4/Mach7/issues?q=label%3Aquestion). This will contribute to our
[poor man's FAQ](https://github.com/solodon4/Mach7/issues?q=label%3Aquestion) and hopefully help others
with a similar question. I get notifications about new issues and usually respond within the
same day. If you prefer not to discuss your question on GitHub, feel free to send me a
[private email](mailto:[email protected]) (note there is a
[+](https://gmail.googleblog.com/2008/03/2-hidden-ways-to-get-more-from-your.html) in the email address).

Call for Help
-------------

We are looking for contributors to the project. If you are a student taking a
programming languages class or any other class that would require you to write
a small compiler or interpreter, we would love you try Mach7 for the job. We
promise to help with any issues you might have with the library.

Known bugs and limitations
--------------------------

Right now, there are several experimental headers that one would need to include to enable one or the other syntax
to work. This is a work in progress, so before you start working with a particular syntax, check examples with that
syntax and make note of which of headers they include. We will clear this eventually leaving only one header, but at
the moment it is a mess, and the most intuitive match.hpp is probably not the header you want as it represents older
experiments. The most recent experimentation and the header you are probably looking for is
[mach7/type_switchN-patterns-xtl.hpp](https://github.com/solodon4/Mach7/blob/master/code/mach7/type_switchN-patterns-xtl.hpp).

The library is not yet suitable for multi-threaded environment. Lock-free version of vtbl-map is in the works.

Please refrain from using solution or project files checked in here. They are not in sync with most recent changes
to directory structure and are difficult to maintain. They will ultimately be replaced with a less verbose system
(likely CMake), and in the meantime please use build.bat to build tests on Windows.

For the most up-to-date list of known issues see [Mach7 Issues](https://github.com/solodon4/Mach7/issues).

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C++adtalgebraic-data-typespatternpattern-matching

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言