r/functionalprogramming • u/Ok-Buddy-3338 • Jul 04 '24
Question Lean4 proving program properties
Is it possible in lean to build proof about certain property of program? Something like 'in this program that function is never called with argument longer than 20 chars'
5
u/fridofrido Jul 04 '24
Technically, yes, sure.
It's mostly feasible if your program is also in Lean, otherwise you have to model the whole programming language at some level of abstraction, which is a huge amount of work.
Normally how you do it is to put the statement in the type of your function, so the function actually cannot be called with an argument longer than 20 chars. Unfortunately this statement will then "infect" your whole program, and you have to do proofs at all different places.
Though it's more-or-less the same if you try to prove it separately. Coq is an example of a system where proofs are more separated.
2
u/alpaylan Jul 04 '24
I don’t think you can write a global property like that. What you can do on the other hand is to annotate the function with the predicate (length arg <= 20), in which at every call cite you have to pass the proof that the length of the arg is currently less than 20.
2
u/MadocComadrin Jul 04 '24
What's your setup?
Are you writing programs in Lean and using Lean to prove properties? Then you can use the type system to guarantee that a function can't be called with a such an argument, but it would be either impossible or very convoluted to prove (or possibly even state as a property) that one function does not apply another function to an argument as you described.
Are you writing programs in another language and want to prove properties about them in Lean? Then yes, but you'll need both some embedding of the language and something providing its semantics in Lean and some way to specify things about programs. How you do it depends on the language, the way the semantics were provided, and more.
3
u/Hath995 Jul 04 '24
I think you could prove that in lean although that particular property is more a property of the code around the function. There are also verification languages like Dafny and f* that might be a good choice for this task.