summaryrefslogtreecommitdiff
path: root/Test/dafny0/Trait/TraitBasix.dfy
blob: eaf4ba4ba5d7d2a94effc6671fac939edabe5e21 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// RUN: %dafny /compile:0 /print:"%t.print" /dprint:"%t.dprint" "%s" > "%t"
// RUN: %diff "%s.expect" "%t"

module m1
{
  trait I1
  {
    function M1(x:int,y:int) :int
    {
      x*y
    }
  }


  trait I2       //all is fine in this trait
  {
    var x: int;
    
    function method Twice(): int
      reads this;
    {
      x + x
    }
    
    function method F(z: int): int
      reads this;

     
    method Compute(s: bool) returns (t: int, u: int)
      modifies this;
    {
      if s {
        t, u := F(F(15)), Twice();
      } else {
        t := Twice();
        x := F(45);
        u := Twice();
        var p := Customizable(u);
        return t+p, u;
      }
    }
    
    method Customizable(w: int) returns (p: int)
      modifies this;

     
    static method StaticM(a: int) returns (b: int)
    {
      b := a;
    }
    
    static method SS(a: int) returns (b:int)
    {
      b:=a*2;
    }
  }

  method I2Client(j: I2) returns (p: int)     //all is fine in this client method
    requires j != null;
    modifies j;
  {
    j.x := 100;
    var h := j.Twice() + j.F(j.Twice());
    var a, b := j.Compute(h < 33);
    var c, d := j.Compute(33 <= h);
    p := j.Customizable(a + b + c + d);
    p := I2.StaticM(p);
  }

  class I0Child extends I2  //errors, body-less methods/functions in the parent have not implemented here
  {
    function method F(z: int): int
      reads this;
    {
       z  
    }
    var x: int; //error, x has been declared in the parent trait      
  }
  
  class I0Child2 extends I2
  {
    method Customizable(w: int) returns (p: int)
      modifies this;
    {
       w:=w+1;    
    }  

    var c1: I0Child;  
  }
  
  class IXChild extends IX   //error, IX trait is undefined
  {
  
  }
}


trait I0 
{
   var x: int;
   constructor I0(x0: int) // error: constructor is not allowed in a trait
   {
     x:=x0;
   }
}

trait I1
{
  function M1(x:int,y:int) :int
  {
    x*y
  }
}

method TestI1()
{
  var i1 := new I1;   //error: new is not allowed in a trait
}

trait I2       //all is fine in this trait
{
  var x: int;
  
  function method Twice(): int
    reads this;
  {
    x + x
  }
  
  function method F(z: int): int
    reads this;

   
  method Compute(s: bool) returns (t: int, u: int)
    modifies this;
  {
    if s {
      t, u := F(F(15)), Twice();
    } else {
      t := Twice();
      x := F(45);
      u := Twice();
      var p := Customizable(u);
      return t+p, u;
    }
  }
  
  method Customizable(w: int) returns (p: int)
    modifies this;

   
  static method StaticM(a: int) returns (b: int)
  {
    b := a;
  }
  
  static method SS(a: int) returns (b:int)
  {
    b:=a*2;
  }
}

method I2Client(j: I2) returns (p: int)     //all is fine in this client method
  requires j != null;
  modifies j;
{
  j.x := 100;
  var h := j.Twice() + j.F(j.Twice());
  var a, b := j.Compute(h < 33);
  var c, d := j.Compute(33 <= h);
  p := j.Customizable(a + b + c + d);
  p := I2.StaticM(p);
}