summaryrefslogtreecommitdiff
path: root/Test/dafny0/Trait/TraitExample.dfy
blob: be38bfe5ff9b4cf0661df88f556410aa2b57fd4c (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// RUN: %dafny /compile:3 /print:"%t.print" /dprint:"%t.dprint" "%s" > "%t"
// RUN: %diff "%s.expect" "%t"

trait Automobile {
  ghost var Repr: set<object>
  predicate Valid()
    reads this //, Repr
    ensures Valid() ==> this in Repr
  function method Brand(): string
  var position: int
  method Drive()
    requires Valid()
    modifies this // Repr
    ensures old(position) <= position
}

class Volvo extends Automobile {
  predicate Valid()
    reads this //, Repr
    ensures Valid() ==> this in Repr
  {
    this in Repr
  }
  constructor()
    modifies this
    ensures Valid()
  {
    Repr := {this};
  }
  function method Brand(): string {
    "Volvo"
  }
  method Drive()
//    requires Valid()
    modifies this // Repr
    ensures old(position) <= position
  {
    position := position + 10;
  }
}

class Fiat extends Automobile {
  predicate Valid()
    reads this // , Repr
    ensures Valid() ==> this in Repr
  {
    this in Repr
  }
  constructor()
    modifies this
    ensures Valid()
  {
    Repr := {this};
  }
  function method Brand(): string {
    "Fiat"
  }
  method Drive()
//    requires Valid()
    modifies this // Repr
    ensures old(position) <= position
  {
    position := position + 3;
  }
}

method Main() {
  var auto: Automobile;
  auto := new Volvo();
  WorkIt(auto);
  auto := new Fiat();
  WorkIt(auto);
}

method WorkIt(auto: Automobile)
  requires auto != null && auto.Valid()
  modifies auto // auto.Repr
{
  auto.Drive();
  print auto.Brand(), ": ", auto.position, "\n";
  auto.position := 0;
}