summaryrefslogtreecommitdiff
path: root/Test/dafny0/Trait/TraitPolymorphism.dfy
blob: f53d83462a4de7095b2c5bbde5672b88fe9c61f8 (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
// RUN: %dafny /compile:0 /print:"%t.print" /dprint:"%t.dprint" "%s" > "%t"
// RUN: %diff "%s.expect" "%t"

trait T1
{
   var f: int;
   
   function method Plus (x:int, y:int) : int
     requires x>y;
   {
      x + y
   }
   
   function method Mul (x:int, y:int, z:int) : int
     requires x>y;
   {
     x * y * z
   }
   
   //function method BodyLess1() : int
   
   static method GetPhoneNumber (code:int, n:int) returns (z:int)
   {
     z := code + n;
   }
   
   method TestPhone ()
   {
     var num : int;
     num := GetPhoneNumber (10, 30028);
   }
}

trait T2
{
}

class C1 extends T1
{
    method P2(x:int, y:int) returns (z:int)
      requires x>y;
    {
       z:= Plus(x,y) + Mul (x,y,1);
    }
}



method Good(c: C1) returns (t: T1)
ensures c == t;
{
    t := c;    
}

method Bad1(c: C1) returns (t: T2)
ensures c == t;
{
    t := c;  //error, C1 has not implemented T2
}

method Bad2(c: C1) returns (t: T1)
ensures c == t;
{
    c := t;  //error, can not assign a trait to a class
}