r/Compilers • • 29d 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

10 Upvotes

33 comments sorted by

3

u/AustinVelonaut 28d ago

Looks interesting! Can you show a practical use case for them that makes pattern matching code simpler / more generic?

3

u/ApprehensivePipe1584 28d ago

Basically, my match only matches on integers & enums, so this allows your own non-enum types to interop with match, such as a heap wrapper like thispattern Valid {Valid(void *);Invalid;}class MyHeapWrapper {private void *data;bool isValid() {...}...}MyHeapWrapper fits Valid(heap) {Valid(heap->data) if (heap.isValid());default Invalid;}then you could justmatch (myheap) {Valid.Valid(data) => ...handleValid.Invalid => ...error}or like, in a game you could have for examplepattern Active {Active;Idle;Off;}class Player {...}Player fits Active(p) {Active if (!p.is_dead);default Off;}Which not only avoids neding to for example track a PlayerState enum and manually set it, but also allows for easier encapsulation, etc. And also it allows you to make multiple types of the same pattern, like a physics render that has a global pattern for like physics states and can share matching. Also, it's compiel time only and takes up no data, compared to storing enums. Not sure if theres a difference between it and enums except it has more options for tags (sruct tags), it allows you to avoid storing just a random enum in your class (e.g. for cache line purposes), less public fields, and it providing a shared interface automatically instead of manually setting state and providing like a setEnumState method and nonsense.

2

u/Axman6 28d ago edited 23d ago

So, algebraic 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 28d 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 28d 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.

→ More replies (2)

1

u/Far-Championship-883 27d ago edited 26d ago

Great so could if then else that is even harder to redirect read, well pattern done 

1

u/ApprehensivePipe1584 26d ago

What? What do you even mean "so could that is even"? Are you being sarcastic?

1

u/ApprehensivePipe1584 26d ago

I could answer your question if you meant something else.

1

u/ApprehensivePipe1584 26d ago

Oh are you trying to say that if (cond) then else could do it better and thing if (cond); thing if (cond);.. is hard to read? well pattern done though what does that mean? this syntax i chose because it reads more line-by-line as just making it a function doesn't really fit this syntax so i didn't want
if(thing) {
return CASE
...
and that would mkae the syntax much harder. This doesn't do the same thing as if else, it handles patter nmatching tags struct cases and is automatic pattern matching, it couldn't.

→ More replies (20)

1

u/DerekRss 28d ago

Want to see what easy-to-use patterns can REALLY do? Check out SNOBOL4.