LLMs and Specs
I was trying to get a specification for a program at one point, to see if it was doing what I wanted. My LLM, Lyin’ Brian, created one for me.
The background was that I had asked an LLM for a link to math book with the algorithm of, for example, a lower hull. I asked it to write it out in English, used that for the prompt.
It found me https://cp-algorithms.com/geometry/convex-hull.html, and once I had it and checked it, I had Claude convert it to Go, and then I debugged it.
Once I had that, I asked Claude for the specification, and got:
Logical Specification for Lower Hull
Here's a formal specification of what the lower hull algorithm should accomplish:
Given: A finite set of points P = {p₁, p₂, ..., pₙ} in 2D space
Output: An ordered sequence of points L = [l₁, l₂, ..., lₖ] representing the lower hull
Postconditions (what must be true after execution):
Subset property: L ⊆ P (all points in the lower hull are from the original set)
Ordering property: l₁ has the minimum x-coordinate (leftmost), and lₖ has the maximum x-coordinate (rightmost). For all i < j, x(lᵢ) ≤ x(lⱼ) (x-coordinates are monotonically non-decreasing)
Convexity property: For all consecutive triples (lᵢ, lᵢ₊₁, lᵢ₊₂) in L, the orientation must be counterclockwise (left turn). Formally: (lᵢ₊₁ - lᵢ) × (lᵢ₊₂ - lᵢ₊₁) > 0
Minimality property: L is minimal — no point from P can be removed from L while still maintaining properties 2 and 3. (This ensures you have the actual hull, not just some counterclockwise path)
Completeness property: All points on the lower convex hull boundary are included. Formally: for any point p ∈ P that lies on the lower boundary of the convex hull, p ∈ L
Preconditions (assumptions):
P contains at least 2 points
Points are distinct (no duplicates)
The meaning of Lower Hull is understood by all
This successfully translated the program into a spec I could read. Not easily, though! It’s been a while since I did symbolic logic, back at Sun.
So I worked it through with a pencil, paper and a trivial set of data, and that was really what I was asking for. It also told me that I needed to add code to check that the points really were distinct (no duplicates).
Necessary and sufficient? I’m going to think more about that.
#ai #artificialIntelligence #mathematics #programming #specifications