// Implement Valid() and make small adjustments to the constructor and to the specs so // that LinkedListNode and the test method are verified. // Dafny infers `decreases Repr` for recursive functions with `reads Repr`! // Do *not* override any of the default `decreases` clauses. class LinkedListNode { var data: T var next: LinkedListNode? ghost var Repr: set // the representation set ghost predicate Valid() reads this, Repr ensures Valid() ==> this in Repr { true } constructor (data: T, next: LinkedListNode?) ensures Valid() { this.data := data; this.next := next; } function Length(): nat reads Repr requires Valid() { if next == null then 1 else 1 + next.Length() } function Get(i: nat): T reads Repr requires Valid() { if i == 0 then data else next.Get(i - 1) } } method LinkedListNodeTest1() { var one := new LinkedListNode(1, null); var two := new LinkedListNode(2, one); var i := 0; while i < two.Length() { var elt := two.Get(i); i := i + 1; } }