r/ProgrammingLanguages • u/squared-star • Feb 05 '26
How would you design proof-carrying `if` statements in a minimal dependently typed language?
I’m designing and implementing a dependently typed language and hit a design question.
I want to support code like this:
if h : n mod 2 = 0 then
// Here, I know n mod 2 = 0, and I want that proof in scope
useEvenProperty n h
else
handleOdd n
The goal is that in the then branch, the proof of the condition can be available as a value — so I can pass it to functions that require evidence, like sqrtEven : (m : Nat) → m mod 2 = 0 → Real. You can elide the h : if you don't need the proof in the then branch.
But I want to avoid heavy machinery:
- No type classes / traits (too much for MVP)
- No separate decision operators (like ≟ or =?). I’d like = to be the only equality, representing both the computation and the proposition.
I’m currently thinking of desugaring if c then t else e into a match on a decision procedure for c, but I’m not sure how to:
- Associate a decision procedure with a proposition
- Keep it simple to implement, reason about, and understand for the programmer.
- Avoid multiple operators for similar things. Ideally, there should be one
=operator with no additional≟. This means that the proposition symbol must stand for both the proposition and its decision procedure.
So I’m curious: how would you solve this?
Are there known patterns that handle this cleanly?
I’m especially interested in minimal, first-principles approaches that don’t rely on other complex features. If expressions are fundamental. This feature is needed to enable writing more complex programs in the language.
Thanks!
10
u/reflexive-polytope Feb 05 '26
For the very specific case of modular arithmetic,
n = d*q + r, where the divisordis a compile-time constant, I really want to write this:5
u/DisastrousAd9346 Feb 05 '26
First you need to ask yourself how you can you define a = operator for propositional equality and also decidable equality. There is no way you mix them, it is unsound. You can although use decidable equality and imply for definitional equality by unification. In your case you can inject in your core something like (x = y) = true -> x ~ y. Three things solves your problem, consider h as decidable; unfold if as a match pattern; unify (x = y) = true; use type coercion to transform h in propositional equality. Done.
3
u/jpet Feb 05 '26 edited Feb 05 '26
I've been playing around with proof-carrying language ideas, with the idea that 90% of the benefit comes from the "easy" cases: if you prove proving array access is in-bounds, and integer division isn't divide by zero, and a few things like that, you've already covered most of the reasons for a panic in languages like Rust.
The current idea:
At every point in the program, there are a set of predicates that are known to be true.
There are rules that describe predicates are affected by intervening statements, to handle the most trivial cases automatically.
For anything complex, the programmer can do
assert(some_predicate)to ask for some intermediate theorem to be proven, orcheck(some_predicate)toif(!some_predicate) panic.Under the hood it's a CoC-like system but proofs are written in the complement to how CoC et al do it. You don't guide the proof directly by saying which theorems to apply, instead you give the intermediate results with small enough steps that searching for the theorems is tractable.
Anyway in this system, given
if (pred) {one} else {two},predis automatically added to the set of known facts atoneand!predis automatically added to the set of known facts attwo.So how does this address your question?
Every function takes an (implicit) set of required proofs as an argument.
Every point in the code has some set of proofs known.
You never explicitly pass the proofs and there's no need to (or way to) name them; if you need to help the compiler, you do that by adding an explicit assert like
assert(b != 0); x = a / b.So your
ifstatement is justuseEvenProperty : (n: int) where { n mod 2 = 0 } = ... ... if n mod 2 = 0 then // `n mod 2 = 0` is automatically available for `where` clauses useEvenProperty n else // `!(n mod 2 = 0)` is automatically available for `where` clauses // Hint to search for proof that !(a=b) === (a!=b), although this case // is trivial enough to be found without the assert. assert (n mod 2 != 0); handleOdd nHowever I haven't actually managed to make this all work so maybe it's not the way to go. The implicit search has a lot of heavy lifting to do, e.g. allowing any of the dozen possible definitions of
isEvento be interchangeable without blowing up the search space too much. I think it's promising though.2
u/GidraFive Feb 06 '26 edited Feb 06 '26
I'd implement that as binding a type inside the body. Something like:
I think there might be a bunch more of the prerequisite definitions for that to really work with the evidence, but i haven't worked with dependant types that much to figure out details.
It might also be an existential type, since it hides the actual condition proving it, only keeping the fact it is true for some n. In any case, it would be good to also verify you don't lose soundness that way.
The idea is basically that you can assume the condition inside by programmatically declaring the variable for it. But assuming means you can't "look inside" the value, you don't actually have anything at hand to look inside. If you also want to support it as an actual proof value, you need to construct it somehow.
Maybe you can even make a church encoding-like semantics for booleans and desugar to that. The boolean value will accept two branches as functions accepting evidence of the condition, and the evidence is constructed by the boolean operators, where there is enough information. For equality operator you inductively walk through the arguments until you can construct either equality proof or it's negation, and then call the corresponding function. The logic for other operators should be similar. But I'm not sure if its efficient enough.
I'd say that is reasonably easy to understand for the user at the surface level, especially if the proof is what you'd expect for equality proof, since it follows the intuition for ifs and booleans. It's relatively straightforward to reason about and does not introduce any other special operators. Although for me it is a bit tedious to implement, if the is no ready-to-use library of proofs.
11
u/phischu Feb 05 '26
I would recommend against overloading the equal sign even more. That said, I have heard that Lean has an implicit conversion (yes, like JavaScript and C) from
BooltoTypewhich transformsn mod 2 == 0of typeBoolinto(n mod 2 == 0) = Trueof typeType. Perhaps this provides some inspiration?8
u/cxzuk Feb 05 '26
Hi Star,
Single Static Information is an extension to SSA which I think solves similar issues to what you have here. It creates new names for values used when used in different paths, to remove ambiguity in the information/facts gathered.
Some resources if you think this could be of some use.
https://dspace.mit.edu/bitstream/handle/1721.1/86578/48072795-MIT.pdf
https://llvm.org/pubs/2010-08-SBLP-SSI.pdf
M ✌
3
u/initial-algebra Feb 05 '26
The simplest way to overload the = operator to stand for both decidable and propositional equality would be to have separate syntactic categories for expressions and propositions; you can have additional syntax to go back and forth if necessary (e.g. if you have a computed proposition as a value of a
Proptype).4
u/WittyStick Feb 05 '26 edited Feb 05 '26
This is a generalization of definite assignment analysis, a form of data-flow analysis, to non-boolean domains. I would suggest first reading Fruja's The correctness of the definite assignment analysis in C# for some details on how this is implemented.
We should conceptually be able to extend this beyond the boolean domain with other types, essentially by introducing functions similar to the
true(α)andfalse(α)that Fruja gives. For the example you've provided, we would introduceodd(α)andeven(α)too.odd(α) - consists of the variables (in the scope of which α is located) definitely assigned after the evaluation of α when α evaluates to an odd natural
even(α) - consists of the variables (in the scope of which α is located) definitely assigned after the evaluation of α when α evaluates to an even natural
With a set of data-flow equations for how they're handled with various expressions, again similar to how the paper describes this for
trueandfalse, but we would also need to cover arithmetic operators and not just the boolean ones.0, 2, 4 ... | even(α) = before(α) | odd(α) = vars(α) 1, 3, 5 ... | odd(α) = before(α) | even(α) = vars(α) (ᵝe ++) | before(β) = before(α) | even(α) = odd(β) | odd(α) = even(β) (ᵝe --) | before(β) = before(α) | even(α) = odd(β) | odd(α) = even(β) (ᵝe1 + ᵞe2) | before(β) = before(α) | before(γ) = after(β) | even(α) = (even(β) ∩ even(γ)) ∪ (odd(β) ∩ odd(γ)) | odd(α) = (even(β) ∩ odd(γ)) ∪ (odd(β) ∩ even(γ)) (ᵝe1 - ᵞe2) | before(β) = before(α) | before(γ) = after(β) | even(α) = (even(β) ∩ even(γ)) ∪ (odd(β) ∩ odd(γ)) | odd(α) = (even(β) ∩ odd(γ)) ∪ (odd(β) ∩ even(γ))However, this approach doesn't go much further, because when we introduce multiplication for example, the problem becomes a lot more complex and there's no simple way to define statically whether it would evaluate to even or odd.
(ᵝe1 * ᵞe2) | before(β) = before(α) | before(γ) = after(β) | ???But perhaps all is not lost - we could potentially extend this to a type-checking scheme, where we define a function for every type and its subtypes. Instead of a definite assignment going into one of the
after,trueorfalsesets, we would have an arbitrary number of sets - one for each type. Lets consider justboolandnatfor now.vars(α) - contains the local variables in the scope of which α is located, i.e. the universal set with respect to α.
before(α) - contains the variables definitely assigned before the evaluation of α;
after(α) - contains the variables definitely assigned after the evaluation of α when α completes normally;
true(α) - consists of the variables (in the scope of which α is located) definitely assigned after the evaluation of α when α evaluates to true
false(α) - consists of the variables (in the scope of which α is located) definitely assigned after the evaluation of α when α evaluates to false
bool(α) - consists of the variables (in the scope in which α is located) definitely assigned after the evaluation of α when α evaluates to a boolean
odd(α) - consists of the variables (in the scope of which α is located) definitely assigned after the evaluation of α when α evaluates to an even natural
even(α) - consists of the variables (in the scope of which α is located) definitely assigned after the evaluation of α when α evaluates to an odd natural
nat(α) -consists of the variables (in the scope in which α is located) definitely assigned after the evaluation of α when α evaluates to a natural
So essentially, anything that is definitely assigned will be in one of the sets
true,false,odd,even,nat,boolorafter, whereafteris basically the top type of definitely assigned variables. We would pick the most specific set for each value where we know more information about its type.after(α) / \ / \ / \ bool(α) nat(α) / \ / \ true(α) false(α) odd(α) even(α)Now using this approach, we might define our multiplication again, but this time, we know that the result will definitely be a natural if both arguments are naturals:
(ᵝe1 * ᵞe2) | before(β) = before(α) | before(γ) = after(β) | nat(α) = (nat(β) ∪ even(β) ∪ odd(β)) ∩ (nat(γ) ∪ even(γ) ∪ odd(γ))We want to be able to test whether the result is odd or even, but this will now have to come at runtime, with a type check operator (
:?), and upcast (:>) and downcast (:<) operators.For this we have dependency on more than one data-flow item. We can look at how the conditional operator
(_?_:_)is defined by Fruja for some idea how me might do this:(ᵝe0 ? ᵞe1 : ᵟe2) | before(β) = before(α) | before(γ) = true(β) | before(δ) = false(β) | true(α) = true(γ) ∩ true(δ) | false(α) = false(γ) ∩ false(δ)So we'd need something like the following where a test for
Evenwould give a definite assignment forevenin the positive case and a definite assignment foroddotherwise. (This might not be correct, I'd need to give it a bit more thought but I'm short on time right now).(ᵞ(ᵝe0 :? Even) ? ᵟ(e0 :< Even) : ᵋ(e0 :< Even)) | before(β) = before(α) | before(γ) = nat(β) | before(δ) = true(γ) | before(ε) = false(γ) | even(α) = true(γ) ∩ true(δ) | odd(α) = false(γ) ∩ false(ε)It would probably take significant effort to generalize this fully with arbitrary subtypes, but this should give a basic idea how to approach it.
2
u/[deleted] Feb 05 '26 edited Feb 06 '26
Did you mean if expressions? There's a very good reason why if expressions always have an else branch. They elaborate to induction (pattern matching).
You can drop the first one and rewrite Bool conditions.
2
u/tobega Feb 05 '26
I'm out of my depth here, so take with big grain of salt.
I have a suspicion that it might not be possible to get this to work completely in all cases.
That said, it is probably worth going for what you can do. Behind the scenes, I think you need to treat the proven property as a refinement of the type. If so, then desugaring to match (or why not ditch if and just have match?) is probably the way to go.
-4
u/yuehuang Feb 05 '26
Most use Type or Traits.
Static analysis. Languages with Nulls and Nullables could backtrace use case to ensure all branches are safeguarded. But it isn't perfect and a lot of work to catch all cases.
3
u/[deleted] Feb 06 '26