summaryrefslogtreecommitdiff
path: root/Chalice/tests/examples/CopyLessMessagePassing.chalice
blob: 3a9b80e08d4d0eee70f34504097372978935160d (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
// program inspired by "Proving Copyless Message Passing" (Villard, Lozes and Calcagno, APLAS 2009)

// msg tag indicates what the type of the message
// channel is freed by Getter when it completes

// todo: accept ack message before sending the next one (requires sending negative credit!)

channel C(msg: bool, n: Node) where n!= null && acc(n.next) && acc(n.mu) && (msg ==> credit(this, 1)) && (!msg ==> acc(this.mu, 50));

class Node {
  var next: Node;
  
  function length(): int
    requires this.list;
  {
    unfolding this.list in 1 + (next == null ? 0 : next.length())
  }
  
  predicate list {
    acc(next) && acc(mu) && (next != null ==> next.list)
  }
}

class Program {
  method Putter(e: C, x0: Node) 
    requires e!= null && acc(e.mu, 50) && (x0 != null ==> x0.list) && (x0 != null ==> credit(e, - 1));
  {
    var x: Node := x0;
    var t: Node;
    
    while(x != null) 
      invariant (x != null ==> x.list) && (x!=null ==> acc(e.mu, 50)) && (x != null ==> credit(e, - 1));
    {
      unfold x.list;
      t := x.next;
      if(t != null) {
        send e(true, x);
      } else {
        send e(false, x);
      }
      x := t;
    }
  }  
  
  method Getter(f: C) 
    requires f!= null && credit(f, 1) && acc(f.mu, 50) && waitlevel << f.mu;
  {
    var x: Node := null;
    var msg: bool := true;
    while(msg)
      invariant acc(f.mu, 50) && waitlevel << f.mu && (msg ==> credit(f, 1)) && (!msg ==> acc(f.mu, 50));
    {
      receive msg, x := f;
      if(msg) {
        free x;
      }
    }
    free f; // close the channel
  } 

  method Main(x: Node)
    requires x != null;
    requires x.list;
  {
    var e := new C;
    fork Putter(e, x);
    fork Getter(e);
  }  
}