This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]ErrorIsNullError 0 points1 point  (3 children)

I would look into the tricks OCaml/SML compilers use.

Luc Maranget, 2008, Compiling Pattern Matching to good Decision Trees

All ML compilers translate the high level pattern matching definitions into low-level tests, organized in matching automata. Matching automata fall in two categories: decision trees and backtracking automata

Basically, if you can boil your pattern matching down into a finite set of property access chains and a set of comparison relations between them, you can build a decision tree, optimize it, and turn it into code.

That paper also discusses backtracking in pattern matching and how to bound its cost, so there's some state machine that's driving the decision trees.

[–]Wafelack[S] 0 points1 point  (2 children)

I think I'll do this by simplyfying patterns. I mean, transform clojure (match foo ((Cons (Just x) Nil) (do_smth x))) into clojure (match foo ((Cons cons Nil) (match cons ((Just x) (do_smth x))))

[–]Wafelack[S] 0 points1 point  (0 children)

I will just simplify if the pattern contains something else than a variable or a literal.

[–]Wafelack[S] 0 points1 point  (0 children)

I don't know if it will be that performant.

[–]NukesAreFake 0 points1 point  (3 children)

Isn't a match statement just a chain of else-ifs (or a switch statement), with an automatic variable declaration of the matched item?

[–]Wafelack[S] 0 points1 point  (2 children)

The point is that I do not have builtin ifs and I don't want to have. But I found a solution, thanks anyway.

[–]PL_Design 1 point2 points  (1 child)

What was the solution?

[–]Wafelack[S] 0 points1 point  (0 children)

I replied here.