33
u/JAMIEISSLEEPWOKEN 2d ago
……you want a hashmap…?
14
u/swagnation77 2d ago
lmao yeah if I did a hashmap the solution would've been better, but the proofs would've taken me 10x as long. (I spent two days on a single algorithm)
6
u/NooneAtAll3 1d ago
am I reading Lean problem statement correctly? when you return some(), you are not guaranteeing that it's the returned indexes that sum to the target, only that some such indexes exist?
3
u/swagnation77 1d ago
Damn good catch, such a dumb mistake on my end. I will update it to match the correct specification, thanks!
2
u/swagnation77 1d ago
Yeah so
some _ => (∃ p : Fin n × Fin n, v[p.1] + v[p.2] = target ∧ p.1 ≠ p.2)becomes:some p => (v[p.1] + v[p.2] = target ∧ p.1 ≠ p.2)and matching on pairs in the
somecase returns a different constructor:
in the base case:
⟨some p, hsol⟩ => ⟨some (⟨0, h0⟩, p.2), hsol⟩
In the inductive case:
⟨some p, hsol⟩ => ⟨some (⟨i + 1, hip⟩, p.2), hsol⟩which actually simplified my proof because I don't need to turn an already valid pair into a proof that a valid pair exists, I just have to propagate it.
2
u/NooneAtAll3 1d ago
by the way, have you tried experimenting with cpp-based syntax?
lean supports many styles, you don't have to only use python-esk whitespace-based nesting
1
u/swagnation77 1d ago
Never tried it but seems interesting. I'm still learning Lean so I have just been following convention. I don't really like Lean's default syntax so thanks for the tip!

98
u/MistakeIndividual690 2d ago
I don’t know what Lean is but what for the love of god is this…
TwoSum is like eight lines in Python
But this is clearly the perfect post for this sub