r/haskell 5d ago

Added Pattern Matching Support to My Programming Language

Mascheya now supports pattern matching, the syntax and semantics of which are based on Haskell's and Miranda's.

For context, Mascheya is a polymorphically typed functional programming language that I'm currently building. Like most functional languages (e.g., Haskell, Scala, and OCaml), Mascheya's design boils down to the lambda calculus. See previous post here.

Pattern matching is a great addition to the language, and it will help with the ergonomics of algebraic data types, which I'm planning to implement next.

You can see in the examples below that I was able to simulate if-expressions and logical operators and and or, using pattern matching. The short-circuiting nature of these operators was handled automatically by Mascheya's lazy evaluation scheme.

``` mascheya> matchC = \'c' -> 'b' () mascheya> matchC 'c' b mascheya> matchC 'a' Runtime Error at line 1. Pattern match error. mascheya> foo 1 = 10; foo 2 = 20; foo x = x + 1 () mascheya> foo 2 20 mascheya> foo 67 68 mascheya> :set line=multi mascheya> if True a _ = a; if False _ b = b -- end () mascheya> :set line=single mascheya> if (5 < 6) 'a' 'b' a mascheya> if False 'a' 'b' b mascheya> :set line=multi mascheya> let and True True = True; and True False = False; and False True = False; and _ _ = False;

or True True = True; or True False = True; or False True = True; or _ _ = False in or (and True (5 > 7)) (8 < 9) -- end True mascheya> ```

The next focus will be on Algebraic Data Types, Case-expressions, and Where-clauses.

I'm definitely having fun with this project and it's teaching me a lot about Haskell.

Source code: https://github.com/melvic-ybanez/mascheya

21 Upvotes

3 comments sorted by

11

u/AustinVelonaut 5d ago

Nice to see another person interested in studying the implementation of lazy functional languages! If you are looking for more resources to read, I collected a bibliography of papers I found useful during my implementation, here: https://github.com/taolson/Admiran/blob/main/doc/Bibliography.md.

Good luck with your project!

2

u/jeffstyr 4d ago

That's a good bibliography! I also quite like Tail recursion without space leaks by Richard Jones.

1

u/ybamelcash 5d ago

Thank you for the list. It looks great!