Hi there,
I’ve just started exploring Rust today, so please bear with me if I get anything wrong. ![]()
I know Rust is all about safety and performance, but I was wondering if it could benefit from introducing a special kind of function (let’s call it a partial function or pfn) where the input domains are restricted through regular expressions at compile time. The compiler could then generate the necessary test conditions for runtime checking.
In mathematics, we have total functions, where all values of the domain are allowed, and partial functions, where only a subset of the domain is allowed. In programming, it seems unwise to allow total functions at the language level because it's dangerous to leave all the restrictions to the coder. We can make mistakes due to our imperfect nature. Delegating this responsibility to the compiler makes sense, as automated code generation would always be consistent. Additionally, allowing a mechanism to describe the outputs of various 'pfn' functions as inputs to other 'pfn' functions could help avoid repetitive checks.
Some might argue that extensive checks could affect performance, but this is just an idea, and I'll leave it to the experts to decide the extent of checks needed.
Also, there’s a big difference between functions in mathematics and programming languages. In mathematics, types and operations are well-defined and there is no runtime, whereas in programming, a function might rely on other services and functions, and the algorithm might not always yield a value due to runtime exceptions. Perhaps Rust already addresses this by making errors and exceptions part of the function type; I’m not sure!
Partial functions could contribute to algorithm proofs and potentially lead to new ideas for secure systems and operating systems.
Regards,
kk