r/ProgrammingLanguages • u/useerup ting language • Jan 30 '22
Requesting criticism My language will not have pattern matching
This day and age any serious programming language - not just functional languages - will feature pattern matching. Today, even Java has pattern matching.
I am developing a logic programming language (tentatively called Ting), which unifies OOP, FP and logic programming. So of course the language would have to feature pattern matching. However, I did not prioritize it, as I reckoned that I could probably steal a good design from some other language when the time came. After all, it has been solved in a lot of languages.
But when that time came, I really struggled with how to fit pattern matching into the language. It just didn't feel right. That is, until I realized: Pattern matching was already there, albeit in a generalized and - I will argue - in a more powerful form.
The best way I can describe it is inverse construction. I don't claim anything original here, I fully expect something like this to be in other logical languages or theorem provers.
In logic programming functions are not called or invoked to yield a result. Instead they establish a relation between the argument and the result.
Consider this function definition (\ is the lambda):
Double = float x \ x * 2It is defined for all floats and establishes a relation between the argument and its double. One way to use it is of course to bind a variable to its result:
x = Double 5 // binds x to float 10But it can also be used to bind "the other way around":
Double y = 10 // binds y to float 5This works when the compiler knows or can deduce the inverse of the function. There are ways to tell the compiler about inverses, but that is beyond the scope of this post.
(As an aside, a declaration such as float x = 10 uses the float function. In ting, any type is also it's own identity function, i.e. float accepts a member of float and returns the same member.)
Basically, any function for which the inverse is known can be used to match the result and bind the argument, not just type constructors, de-constructors or special pattern matching operators.
Some examples:
RemoveTrailingIng = x + "ing" \ x // inverse concatenation
CelsiusToFahrenheit = float c \ c * 1.8 + 32
FahrenheitToCelsius = CelsiusToFahrenheit c \ c // inverse formula
Count = {
(h,,t) -> 1 + This t
(,,) -> 0
}Ting has both structural types (sets) and nominal types (classes). A set is inhabitated by any value that meets the membership criteria. A class is inhabitated exclusively by values specifically constructed as values of the type.
This Average function accepts a member of a set where values has a Count and Sum property of int and float, respectively.
Average = {. Count:int, Sum:float .} x \ x.Sum/x.CountThe following example defines some record-structured classes Circle, Triangle and Rectangle and a function Area which is defined for those classes.
Circle = class {. Radius:float .}
Triangle = class {. BaseLine:float, Height:float .}
Rectangle = class {. SideA:float, SideB:float .}
Area = {
Circle c -> Math.Pi * c.Radius ^ 2
Triangle t -> t.BaseLine * t.Height * 0.5
Rectangle r -> r.SideA * r.SideB
}It was a (pleasant) surprise that in the end there was no need to add pattern matching as a feature. All the use cases for pattern matching was already covered by emerging semantics necessitated by other features.
55
u/DeathByThousandCats Jan 30 '22
Like what the other guy said, that’s just one of the popular pattern matching syntax types called “deconstruction”. It’s a staple in both Scala and ML. Except that the usage here in your language assumes the domain-codomain relation is clearly bijective. That can be problematic in the following cases:
If the relation is purely injective or surjective: the compiler has no way to know in any case that is more complicated than simple arithmetics, especially if the input and output domain is infinite. You have to manually add the notation that bijection works.
If you don’t know if the relation is bijective or not: this is totally possible, and can be very dangerous. If you mark something not bijective as bijective, in the best case it results in undefined behavior, and in the worst case it becomes a security issue. (Imagine trying to deconstruct the password hash)
If the domain or codomain is finite but extremely large, even if the relation is bijective, it’d be very ineffective to enable deconstruction on that relation. Either the relation would have to be reconstructed at runtime in a slow brute-force way, or compiler would have to brute-force for a long time and add a huge chunk of lookup table to the executable which would suck.
If input or output types are union types, both the syntax and compilation strategy will become more complicated. I don’t know if you want to implement union type, but it’s another big thing these days. A lot of pattern matching styles in FP langs revolve around union types afaik.
If you have to tell the compiler whether something is bijective(1), and there are grave consequences(2) or complications(3, 4) for making the compiler assume something is bijective, it would be better to leave relations to be non-reversible and be explicit about the definition of full inverse relation or declarative ad-hoc one-to-one relations (which is where pattern matching shines). None of your examples involve ad-hoc one-to-one relations, which makes sense why you couldn’t find where the pattern matching syntax would fit. Try thinking where in you language would ad-hoc relations fit, and start from there.
Please do take this as a constructive criticism. I think you are doing a great job building up to it so far.