summaryrefslogtreecommitdiff
path: root/Jennisys/examples/DList.jen
blob: 47c74dc1442791d24152d2860994c62069bd4f94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
interface DList[T] {
  var list: seq[T]

  constructor Empty()
    ensures list = []

  constructor Singleton(t: T)
    ensures list = [t]

  constructor Double(p: T, q: T)
    ensures list = [p q]
}

datamodel DList[T] {
  var root: DNode[T]

  frame
    root

  invariant
    root = null ==> |list| = 0
    root != null ==> list = root.list
}

interface DNode[T] {
  var list: seq[T]

  invariant
    |list| > 0

  constructor Init(t: T)
    ensures list = [t]

  constructor Double(p: T, q: T)
    ensures list = [p q]

  method List() returns (ret: seq[T])
    ensures ret = list

  method Size() returns (ret: int)
    ensures ret = |list|

  method Get(idx: int) returns (ret: T)
    requires idx >= 0 && idx < |list|
    ensures ret = list[idx]

  method Find(n: T) returns (ret: bool)
    ensures ret = (n in list)
}

datamodel DNode[T] {
  var data: T
  var next: DNode[T]
  var prev: DNode[T]
  
  frame 
    next

  invariant
    next = null ==> list = [data]
    next != null ==> (list = [data] + next.list  &&  next.prev = this)
	prev != null ==> prev.next = this
}