Spec# languagedormant
Microsoft Research's design-by-contract extension of C#: non-null reference types, method pre- and postconditions, checked exceptions, and object invariants checked by a static verifier that translates to Boogie.
Overview
Spec# is Microsoft Research's extension of C# that puts API contracts into the language itself: non-null types, method pre- and postconditions, object invariants, checked exceptions. It exists to test whether a verifier can check specifications written in the programmer's own language, and that work became Boogie.
Code
string aString = "Hello"; // non-null by default (/nn)
string? maybe = null; // possibly-null: may hold null
int x = 9;
bool inRange = 0 <= x && x < 100; // conjunction of comparisons
bool big = inRange ==> x > 5; // lower precedence than &&
int size = maybe != null ? maybe.Length : 0; // guarded, so the deref is legal
bool three = exists{int i in (0: x); i == 3}; // half-open integer range
int[] a = new int[] { 4, 8, 15, 16 };
int second = a[1]; // indexing owes a bounds proof
bool sorted = forall{int i in (1: a.Length); a[i-1] <= a[i]};
bool has16 = exists{int i in (0: a.Length); a[i] == 16};
int lo = min{a[0], a[1], a[2]}; // min/max take a set of values
int hi = max{lo, a[3], 100};
int ISqrt(int x)
requires 0 <= x;
ensures result*result <= x && x < (result+1)*(result+1);
{
int r = 0;
while ((r+1)*(r+1) <= x)
invariant r*r <= x; // a loop invariant, not an object one
{ r++; }
return r;
}
void ReadToken(ArrayList a)
throws EndOfFileException
ensures a.Count == old(a.Count); // holds on the throwing path too
using Microsoft.Contracts;
class Band {
int gigs;
[Rep] Guitar gt; // gt is a component of this aggregate
invariant 7 <= gt.Level; // re-established per class frame
public void Play() {
expose (this) { // mutable from here…
gigs++;
gt.Strum();
} // …and the invariant holds again
}
}
/nn makes string non-null, so string? must be tested before a member is
reachable. forall/exists are the quantifiers: they range over a half-open
interval, and min/max aggregate a set — neither loops. The clauses are C#
expressions, not a separate language. The idiom slot is the payoff: [Rep]ownership plus expose lets
an invariant mention a sub-object and still be checked one class frame at a
time. Examples follow the tutorial.Distinctive traits
- Contracts are syntax:
requires,ensures,invariant, andmodifiessit between the signature and the body, written in C# itself. - Non-null types split references in two:
stringcannot be null,string?may be, and only the first is dereferenceable without a guard or cast. - An object invariant holds for every valid object;
exposeopens a scoped region in which a multi-field update may break it. [Rep]marks a component of an aggregate, and ownership lets an invariant mention a sub-object while still checking each frame modularly.- The static verifier is sound but not complete: Boogie plus a solver may reject correct methods until specifications are strengthened.
- A checked exception is part of the contract:
throwsnames it, and anensuresafter it constrains what holds when it is raised.
Status
Research project, not a product: the last CodePlex release was 1.0.21125 in 2009, licensed for non-commercial use only, and Code Contracts carried the contract idea into .NET 4. A community fork moved the compiler and libraries to .NET Framework 4.x.
Sources & references
- Official sitemicrosoft.com
- The Spec# Programming System — archived project page announcing the preliminary release, May 2005web.archive.org
- Using the Spec# Language, Methodology, and Tools to Write Bug-Free Programs (Leino & Müller)microsoft.com
- The Spec# Programming System: An Overview (Barnett, Leino & Schulte, CASSIS 2004)microsoft.com
- Specification and Verification: The Spec# Experience (Barnett et al., CACM 2011)microsoft.com
- Verification of Object-Oriented Programs with Invariants (Barnett et al., JOT 2004)jot.fm
- Spec# on CodePlex — archived project page, December 2009web.archive.org
- Spec# sources ported to .NET Framework 4.x — community continuation of the last CodePlex releasegithub.com
- Automatic memory management (.NET fundamentals, Microsoft Learn)learn.microsoft.com