r/Compilers • u/ApprehensivePipe1584 • 1d ago
Patterns
I've been making a programming language for a while now, and just came up with this idea for a feature i'm bout to implement. Patterns.
pattern P {
N;
T(int, int);
S {
int x;
int y;
};
}
struct MS {
int x;
}
class C {
C() {}
int y;
}
MS fits P(self) {
N if (self->x == 0);
T(self->x, -self->x) if (self->x < 10);
S {
x: self->x;
y: 0;
} if (self->x < 20);
default N;
}
C fits P(self) {
default N;
}
int main() {
C c = C();
c.y = 0;
MS ms = {19};
match (c) {
P.N => `qout("Worked");
default => `qout("Failed");
}
match (ms) {
P.S {x, y} => `qout("Worked");
default => `qout("Failed");
}
return 0;
}
Basically, making user types have the ability to have custom behavior + extraction in match stmts, beyond just enums & integer types
1
u/Axman6 1d ago
So, algebra is data types which Haskell has had for more than thirty years? it wasn’t the first but has been the influence for their inclusion in many other languages like Rust, Scala. For even more fun, look at pattern synonyms, which are patterns which can abstract applying functions when creating or matching them.
4
u/ApprehensivePipe1584 1d ago
Oh yeah, its very similar to pattern synonyms in Haskell. Didn't even think of that. I don't think it's the exact same purpose, but definitely similar.
2
u/AustinVelonaut 1d ago
It's not just ADT or pattern synonyms, though. It looks to me more like a form of typeclasses (ad-hoc polymorphism) combined with patterns, where a pattern can have multiple independent concrete instances defined by user data structures.
3
u/Competitive_Ideal866 1d ago
Sounds like active patterns aka view patterns?
2
u/AustinVelonaut 1d ago
Yeah, F# active patterns come closer to describing what the OP proposes, except additionally the constructors are grouped into a typeclass-like
patternwhich can have multiple ad-hocfitsinstances.
1
1
3
u/AustinVelonaut 1d ago
Looks interesting! Can you show a practical use case for them that makes pattern matching code simpler / more generic?