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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright 2017 The libsesstype Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::rc::Rc;
use super::global::Type as G;
use super::local::Type as S;
use super::Role;

/// Converts a global::Type into a local::Type.
///
/// This implements the projection G↾q defined in Definition A.1.
///
pub fn project(global_type: &Box<G>, role: &Rc<Role>) -> Option<Box<S>> {
    match **global_type {
        G::Interact {
            ref p,
            ref q,
            ref g,
        } => {
            if Rc::ptr_eq(&role, &p) {
                let mut sel = S::select(q);
                for (m_i, g_i) in g {
                    let projected_s = project(&g_i, role);
                    match projected_s {
                        Some(projected_s) => {
                            sel = S::add_message(sel, m_i.clone(), projected_s);
                            ()
                        }
                        None => (),
                    }
                }
                if sel.len() > 0 { Some(sel) } else { None }
            } else if Rc::ptr_eq(&role, &q) {
                let mut br = S::branch(p);
                for (m_i, g_i) in g {
                    let projected_s = project(&g_i, role);
                    match projected_s {
                        Some(projected_s) => {
                            br = S::add_message(br, m_i.clone(), projected_s);
                            ()
                        }
                        None => (),
                    }
                }
                if br.len() > 0 { Some(br) } else { None }
            } else {
                // p != role != q
                match g.len() {
                    1 => {
                        let item = g.iter().next();
                        match item {
                            Some(ref item) => project(item.1, role),
                            None => None,
                        }
                    }
                    _ => {
                        let mut iter = g.iter();
                        let first_item = iter.next();
                        match first_item {
                            Some(ref first_item) => {
                                let mut merged = project(first_item.1, role);
                                for (_, g_i) in iter {
                                    let projected_s = project(&g_i, role);
                                    match projected_s {
                                        Some(ref projected_s) => {
                                            match merged {
                                                Some(merged_) => {
                                                    merged = merge(&merged_, projected_s);
                                                    ()
                                                }
                                                None => (),
                                            }
                                            ()
                                        }
                                        None => (),
                                    }
                                }
                                match merged {
                                    Some(s) => if s.len() > 0 { Some(s) } else { None }
                                    None => None,
                                }
                            }
                            None => None,
                        }
                    }
                }
            }
        }
        G::Recur { ref t, ref g } => {
            let projected_s = project(g, role);
            match projected_s {
                Some(ref projected_s) => Some(S::recur(t, projected_s.clone())),
                None => None,
            }
        }
        G::TypeVar { ref t } => Some(S::typevar(t)),
        G::End => Some(S::end()),
    }
}

/// Merges two local session types.
///
/// This implements the merge operator Π on session types defined in Definition A.1.
///
fn merge(l: &Box<S>, r: &Box<S>) -> Option<Box<S>> {
    let (ref box_l, ref box_r) = (&*l, &*r);
    match (box_l.as_ref(), box_r.as_ref()) {
        (&S::Branch { ref p, ref s },
         &S::Branch {
             p: ref p_r,
             s: ref s_r,
         }) => {
            if Rc::ptr_eq(p, p_r) {
                let mut br = S::branch(p);
                for (m_i, s_i) in s {
                    if s_r.contains_key(m_i) {
                        // Intersect case
                        let s_j = s_r.get(m_i);
                        match s_j {
                            Some(s_j) => {
                                let merged_br = merge(s_i, s_j);
                                match merged_br {
                                    Some(merged_br) => {
                                        br = S::add_message(br, m_i.clone(), merged_br);
                                        ()
                                    }
                                    None => (),
                                }
                            }
                            None => (),
                        }
                    } else {
                        // Only in s case
                        br = S::add_message(br, m_i.clone(), s_i.clone())
                    }
                }
                for (m_j, s_j) in s_r {
                    if !s.contains_key(m_j) {
                        // Only in s_r case
                        br = S::add_message(br, m_j.clone(), s_j.clone())
                    }
                }
                Some(br)
            } else {
                None
            }
        }
        (&S::Select { ref p, ref s },
         &S::Select {
             p: ref p_r,
             s: ref s_r,
         }) => {
            if Rc::ptr_eq(p, p_r) {
                let mut sel = S::select(p);
                for (m_i, s_i) in s {
                    if s_r.contains_key(m_i) {
                        // Note: s_i must be equal to s_r.entry(m_i)
                        sel = S::add_message(sel, m_i.clone(), s_i.clone())
                    }
                }
                Some(sel)
            } else {
                None
            }
        }
        (&S::Recur { ref t, ref s },
         &S::Recur {
             t: ref t_r,
             s: ref s_r,
         }) => {
            if t == t_r {
                let s_merged = merge(s, s_r);
                match s_merged {
                    Some(s_) => Some(S::recur(t, s_)),
                    None => None,
                }
            } else {
                None
            }
        }
        (&S::TypeVar { ref t }, &S::TypeVar { t: ref t_r }) => {
            if t == t_r { Some(S::typevar(t)) } else { None }
        }
        (&S::End, &S::End) => Some(S::end()),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use std::rc::Rc;
    use global;
    use local;
    use Message;
    use Role;

    #[test]
    fn test_projection() {
        let sndr = Role::new("alice");
        let rcvr = Role::new("bob");
        let msg1 = Message::new("label1");
        let msg2 = Message::new("label2");

        let p1 = global::Type::interaction(&sndr, &rcvr);
        let p1_1 = global::Type::add_message(p1, msg1, global::Type::typevar("T"));
        let p1_2 = global::Type::add_message(p1_1, msg2, global::Type::end());
        let p0 = global::Type::recur("T", p1_2);

        let local_alice = super::project(&p0, &sndr);
        let local_bob = super::project(&p0, &rcvr);

        match local_alice {
            Some(ref t) => {
                match **t {
                    local::Type::Recur { ref t, ref s } => {
                        assert_eq!(t, "T");
                        match **s {
                            local::Type::Select { ref p, ref s } => {
                                assert!(Rc::ptr_eq(p, &rcvr));
                                for (m_i, s_i) in s {
                                    match m_i.label().as_str() {
                                        "label1" => {
                                            match **s_i {
                                                local::Type::TypeVar { ref t } => {
                                                    assert_eq!(t, "T")
                                                }
                                                _ => assert!(false),
                                            }
                                        }
                                        "label2" => {
                                            match **s_i {
                                                local::Type::End => assert!(true),
                                                _ => assert!(false),
                                            }
                                        }
                                        _ => assert!(false),
                                    }
                                }
                            }
                            _ => assert!(false),
                        }
                    }
                    _ => assert!(false),
                }
            }
            None => assert!(false),
        };

        match local_bob {
            Some(ref t) => {
                match **t {
                    local::Type::Recur { ref t, ref s } => {
                        assert_eq!(t, "T");
                        match **s {
                            local::Type::Branch { ref p, ref s } => {
                                assert!(Rc::ptr_eq(p, &sndr));
                                for (m_i, s_i) in s {
                                    match m_i.label().as_str() {
                                        "label1" => {
                                            match **s_i {
                                                local::Type::TypeVar { ref t } => {
                                                    assert_eq!(t, "T")
                                                }
                                                _ => assert!(false),
                                            }
                                        }
                                        "label2" => {
                                            match **s_i {
                                                local::Type::End => assert!(true),
                                                _ => assert!(false),
                                            }
                                        }
                                        _ => assert!(false),
                                    }
                                }
                            }
                            _ => assert!(false),
                        }
                    }
                    _ => assert!(false),
                }
            }
            None => assert!(false),
        }
    }

    #[test]
    fn test_project_mergefail() {
        let p = Role::new("P");
        let q = Role::new("Q");
        let r = Role::new("R");
        let a = Message::new("a");
        let b = Message::new("b");
        let a2_1 = Message::new("a2");
        let a2_2 = Message::new("a2");
        let a3 = Message::new("a3");

        let p3_1 =
            global::Type::add_message(global::Type::interaction(&r, &p), a3, global::Type::end());
        let p2_1 = global::Type::add_message(global::Type::interaction(&q, &r), a2_1, p3_1);
        let p2_2 =
            global::Type::add_message(global::Type::interaction(&q, &r), a2_2, global::Type::end());
        let p1 = global::Type::interaction(&p, &q);
        let p1_1 = global::Type::add_message(p1, a, p2_1);
        let p1_2 = global::Type::add_message(p1_1, b, p2_2);

        let local_r = super::project(&p1_2, &r);
        // This should return None because merge is not possible.
        match local_r {
            Some(_) => assert!(false),
            None    => (),
        }
    }
}