summaryrefslogtreecommitdiff
path: root/Test/hofs/Classes.dfy
blob: 9d8044db238b4f656f490eba367c5165ed7d5428 (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
// RUN: %dafny "%s" > "%t"
// RUN: %diff "%s.expect" "%t"


class C {
  static function method Static() : bool
  {
    true
  }
}

method K() {
  var f := C.Static;
  var o : object;
  assert o !in f.reads();
  assert f.requires();
  assert f();
}


class T {
  var h : int -> int;
}

function B(t : T) : int -> int
  requires t != null;
  reads t;
{
  t.h
}

function J(t : T) : int
  requires t != null
  reads t
  reads t.h.reads(0)
{
  if t.h.requires(0) then
    B(t)(0)
  else
    B(t)(0)  // error: precondition violation
}

method U(t : T)
  requires t != null;
  modifies t;
{
  t.h := x => x;
  assert J(t) == 0; // ok
}

class MyClass {
  var data: int
	function method F(): int
	  reads this
	{
	  data
	}	
  method M(that: MyClass)
	  requires that != null
	{
	  var fn := that.F;  // "that" is captured into the closure
	  var d := fn();
		assert d == that.data;  // yes
		assert d == this.data;  // error: no reason to believe that this would hold
	}	
}