r/ProgrammingLanguages • u/levodelellis • Feb 08 '26
Languages with strong pre/post conditions?
I had some random thoughts. I have class where if I call setThing(N); another function (process) will return an array with at minimum (or exactly) N elements. Is that expressible in any languages?
I always though D pre/post/invariant was interesting. I know ada and spark has more (I plan to look at spark). Is there any languages that can express process returning Array [n] where n is the parameter of setThing? What languages should I look at if I'm interested in contract programming?
I'll likely keep a list of ideas and try to figure it out when I want to implement another programming language
5
u/Gnaxe Feb 08 '26
Clojure functions can have pretty much arbitrary pre/post assertions. This is built in, but it's evaluated at run time, not statically.
29
u/fl00pz Feb 08 '26
Dafny.
Or any dependently typed language. cc /u/nerdycatgamer
55
u/nerdycatgamer Feb 08 '26
inb4 everyone suggests dependent types
17
u/Syrak Feb 08 '26 edited Feb 08 '26
There are languages where formally verified contracts are a built-in feature: Why3, F-star, Dafny.
There are also many extensions of existing languages, like SPARK for Ada. Frama-C for C, Gospel for OCaml, Liquid Haskell, and to name a couple for Rust: Verus, Verifast, Flux, Aeneas, RefinedRust, Creusot (that I work on, AMA).
9
u/extensional-software Feb 08 '26
Maybe look into Verus for Rust. You can write pre and post conditions and write proofs involving recursive functions and loops. It hands most of the hard work to Z3 and is not based on dependent types.
8
u/ejstembler kit-lang.org Feb 08 '26
After learning about Design-by-contract from Eiffel back in the day, I've made it a habit to use preconditions and postconditions in every lanugage where possible. Sometimes the language has something built-in, sometimes I've had to write on my code for each function.
Theses days I'm working on my own language and I support the concept two different ways: pre/post attributes (decorators) and refinement types. The developer can choose either.
7
u/phischu Effekt Feb 08 '26
Everyone here answering "dependent types" is slightly missing the point of the original question. Yes, with dependent types you can express pre- and post conditions of functions. But the question is about tracking modification of the state of an object, which we did not know how to do.
Until recently, when my colleagues published Deriving Dependently-Typed OOP from First Principles and the polarity language. Here is this example, which you can try on the online playground. There is a follow-up version that is more convenient to use.
We define an interface (
codata)Thing(n: Nat)that is parameterized over the current state ofn. When we callsetThingwe get back a new object with a different paramter. When we callprocesswe get back an array (Vec(n)) of the specified size. The size is written in unary, soS(S(Z))is2. If you would change this number in the signature ofmainthe program would not typecheck anymore, because we create and object with number0, and then set it to2, and the callprocess.11
u/ineffective_topos Feb 08 '26
As foretold this situation is when dependent types are used, as well as weaker forms. The unfortunate consequence is that they typically are either extremely complex or don't have admissable substitution.
The other good alternative is liquid types, which add propositions that assert things like this with extra annotations past the type system. The downside compared to dependent types is that proving it is very opaque, you're hoping the system does it automatically. And maybe you can add some lemmas to make it happen but generally don't have much control.
Either way it's a lot of complexity, and liquid types are not that mature.
10
u/zogrodea Feb 08 '26
Dependent types, as mentioned in the other commends, are interesting.
I should take a look at how static asserts work in C. I would be interested in what kind of things inside C enable that kind of analysis.
Edit: I suddenly remembered the "Eiffel" programming language as one that uses contract-driven programming (I think without dependent types). I don't know much about it though.
1
u/snugar_i Feb 09 '26
What if someone else calls
setThingwith a different number in the meantime? That can happen easily when multithreading, but even in a single thread if there are multiple references to the class3
u/raevnos Feb 08 '26
Racket has strong design-by-contract support.
3
u/WittyStick Feb 08 '26 edited Feb 08 '26
Interesting problem, though I'm not sure it has much practical use it's still worth considering.
The first question is, is
Na constant or a runtime value? IfNis constant this may be easier to deal with. If the value is provided at runtime then we're ultimately going to need a form of dynamic typing or dependant typing.In the dynamic case, we could use something like Kernel, which allows a callee to mutate the caller's environment:
($setThing! N)implicitly captures the callers environment in the first-class symboldenv. It then bindsprocessin this environment to a lambda returning an array ofNelements.$set!will overwrite any existing binding ofprocessin the caller's local environment, and shadow any existing binding ofprocessin the ancestors of the caller's environment.2
u/blcsm Feb 09 '26
C++ is getting something similar with P2900.
Here's also a talk about it.
1
u/Gnaxe Feb 08 '26
You might also be interested in CrossHair. This kind of blurs the line between unit tests and static typing. It attempts to symbolically find a counterexample of your pre/post condition, but it does that by actually calling your function with special mock objects, at test time. You can try examples here.
1
u/gergoerdi Feb 08 '26
Try a taste of B:
https://unsafeperform.io/blog/2010-02-16-the_b_method_for_programmers_(part_1)/
https://unsafeperform.io/blog/2010-02-22-the_b_method_for_programmers_(part_2)/
1
u/Ronin-s_Spirit Feb 08 '26
What another function and how did it get here? I'm faircly certain "contracts" usually validate conditions on every call, not change the outputs of a function (that part sounds like a decorator).
1
u/GunpowderGuy Feb 08 '26
Idris2. You can for example define a function that takes a list ( or some other collection ) and returns another list ( or collection ) that is a permutation of the first, and ordered. Which means the ouput is an ordered version of the input. You proved your function sorts its input
1
u/tal_franji Feb 08 '26
There used to be Eiffle: https://www.eiffel.org/doc/eiffel/ET-Design_by_Contract%28tm%29%2C_Assertions_and_Exceptions
1
u/Smoother-Bytes Feb 09 '26
Spark/ada
1
u/kwan_e tonal-lang Feb 09 '26
You could perhaps specify them separately. That
setThing(N)will always succeed in setting some object to N, and that N is within some legal bounds. Then another to specify thatprocessalways accesses that object for N which is used to allocate the array.Of course, another way to specify pre-conditions and post-conditions is to wrap it up in a type that prevents any other usage.
1
u/bsdooby Feb 09 '26
The D language: https://dlang.org/spec/contracts.html
1
u/sennalen Feb 08 '26
Ada probably
0
u/Fofeu Feb 08 '26
You do not need built-in pre/post-conditions in your language. I even think that this is the wrong way to address the issue. In practice, there is a point when projects grow in size where dependent types just become unsolvable headaches.
Instead, define a (formal) semantics and standardise specification annotations. From there, external tools can take the lead. You could hand everything off to an SMT solver, hop in an interactive theorem prover, etc.
0
u/[deleted] Feb 08 '26
[deleted] — view deleted comment
0
u/[deleted] Feb 08 '26
[deleted] — view deleted comment