Build Status codecov License Docs Join the chat at https://gitter.im/MLStyle-jl/community

What is MLStyle.jl?

MLStyle.jl is a Julia package that provides multiple productivity tools from ML (Meta Language) like pattern matching which is statically generated and extensible, ADTs/GADTs (Algebraic Data Type, Generalized Algebraic Data Type) and Active Patterns.

Think of MLStyle.jl as a package bringing advanced functional programming idioms to Julia.


Those used to functional programming may feel limited when they don't have pattern matching and ADTs, and of course I'm one of them.

However, I don't want to make a trade-off here by using some available alternatives that miss features or are not well-optimized. Just like why those greedy people created Julia, I'm also so greedy that I want to integrate all those useful features into one language, and make all of them convenient, efficient and extensible.

On the other side, in recent years I was addicted to extending Python with metaprogramming and even internal mechanisms. Although I made something interesting like pattern-matching, goto, ADTs, constexpr, macros, etc., most of these implementations are also disgustingly evil. Fortunately, in Julia, all of them could be achieved straightforwardly without any black magic, at last, some of these ideas come into existence with MLStyle.jl.

Finally, we have such a library that provides extensible pattern matching for such an efficient language.

Why use MLStyle.jl

Installation, Documentations and Tutorials

Rich features are provided by MLStyle.jl and you can check the documentation to get started.

For installation, open the package manager mode in the Julia REPL and add MLStyle.

For more examples or tutorials, see this project which will be frequently updated to present some interesting uses of MLStyle.jl.


Rock Paper Scissors

Here's a trivial example of MLStyle.jl in action:

using MLStyle
@data Shape begin # Define an algebraic data type Shape

# Determine who wins a game of rock paper scissors with pattern matching
play(a::Shape, b::Shape) = @match (a,b) begin
    (Paper(), Rock())     => "Paper Wins!";
    (Rock(), Scissors())  => "Rock Wins!";
    (Scissors(), Paper()) => "Scissors Wins!";
    (a, b)                => a == b ? "Tie!" : play(b, a)

Homoiconic pattern matching for Julia ASTs

Here's a less trivial use of MLStyle.jl for deconstructing and pattern matching Julia code.

rmlines = @λ begin
    e :: Expr           -> Expr(e.head, filter(x -> x !== nothing, map(rmlines, e.args))...)
      :: LineNumberNode -> nothing
    a                   -> a
expr = quote
    struct S{T}
        a :: Int
        b :: T
end |> rmlines

@match expr begin
        struct $name{$tvar}
            $f1 :: $t1
            $f2 :: $t2
    end =>
        struct $name{$tvar}
            $f1 :: $t1
            $f2 :: $t2
    end |> rmlines == expr

Generalized Algebraic Data Types

@use GADT

@data public Exp{T} begin
    Sym{A}    :: Symbol                        => Exp{A}
    Val{A}    :: A                             => Exp{A}
    App{A, B, A_} :: (Exp{Fun{A, B}}, Exp{A_}) => Exp{B}
    Lam{A, B} :: (Symbol, Exp{B})              => Exp{Fun{A, B}}
    If{A}     :: (Exp{Bool}, Exp{A}, Exp{A})   => Exp{A}

A simple interpreter implemented via GADTs could be found at test/untyped_lam.jl.

Active Patterns

Currently, MLStyle does not have fully featured active patterns, but the subset of parametric active patterns that are implemented are very powerful.

@active Re{r :: Regex}(x) begin
    match(r, x)

@match "123" begin
    Re{r"\d+"}(x) => x
    _ => @error ""
end # RegexMatch("123")



Recent benchmarks have been run, showing that MLStyle.jl can be extremely fast for complicated pattern matching, but due to its advanced machinery has noticeable overhead in some very simple cases such as straightforwardly destructuring shallow tuples, arrays and datatypes without recursive invocations.

All benchmark scripts are provided in the directory Matrix-Benchmark.

To run these cross-implementation benchmarks, some extra dependencies should be installed:

After installing dependencies, you can directly benchmark them with julia matrix_benchmark.jl hw-tuple hw-array match macrotools match-datatype in the root directory.

The benchmarks presented here are made by Julia v1.1 on Fedora 28. For reports made on Win10, check stats/windows/ directory.

Contributing to MLStyle

Thanks to all individuals referred in Acknowledgements!

Feel free to ask questions about usage, development or extensions about MLStyle at Gitter Room.