-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sessions.agda
203 lines (162 loc) · 8.23 KB
/
Sessions.agda
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
module Sessions where
{-
This file defines the session calculus: first-order
pi-calculus with recursion, sessions, and branching.
-}
open import Basics
open import Size
open import Data.Nat
open import Context
open import Data.Vec
open import Data.String
open import Data.Bool
open import Data.Fin using (Fin; zero; suc)
mutual
{- Value types -}
data VType : Set where
unit : VType
nat : VType
sess : forall {i : Size} -> SType {i} -> VType
-- in the future, could do a polyadic form with a Context of VType, rather that just one
def : forall {i : Size} -> VType -> Context (SType {i}) -> VType
{- (Sized) session types. The size is used to prove termination
of dual -}
data SType : {i : Size} -> Set where
[_]!∙_ : forall {i : Size} -> VType -> SType {i} -> SType {↑ i}
[_]?∙_ : forall {i : Size} -> VType -> SType {i} -> SType {↑ i}
⊕_ : forall {n : ℕ} {i : Size} (vs : Vec (Pair String (SType {i} )) n) -> SType {↑ i}
&_ : forall {n : ℕ} {i : Size} (vs : Vec (Pair String (SType {i} )) n) -> SType {↑ i}
*_ : forall {i : Size} -> SType {i} -> SType {↑ i}
end : forall {i : Size} -> SType {↑ i}
-- Process types
data PType : Set where
val : VType -> PType
proc : PType
-- Session duality
dual : forall {i : Size} -> SType {i} → SType {i}
dual ([ V ]!∙ P) = [ V ]?∙ (dual P)
dual ([ V ]?∙ P) = [ V ]!∙ (dual P)
dual (⊕_ vs) = &_ (Data.Vec.map (\x -> ( pi1 x , dual (pi2 x))) vs)
dual (&_ vs) = ⊕_ (Data.Vec.map (\x -> ( pi1 x , dual (pi2 x))) vs)
dual (* P) = * (dual P)
dual end = end
-- Construct a session environment of size n, where each channel has type 'end'
allEnd : forall {n : ℕ} -> Context (SType)
allEnd {n = zero} = Em
allEnd {n = suc n} = (allEnd {n = n}) , end
mutual
{- Vector or derivations, each sharing the same value typing environment -}
data DerivVec : (Γ : Context VType) (Σ : Context (SType)) (n : ℕ) (vs : Vec (SType) n) (T : PType) -> Set where
[] : forall {Γ Σ T} -> DerivVec Γ Σ zero [] T
Cons : forall {Γ : Context VType} {Σ : Context (SType {∞} )} {n : ℕ} {ss : Vec (SType {∞} ) n} {T} {k} -> (x : Γ * (Σ , k) |- proc) (xs : DerivVec Γ Σ n ss T) -> DerivVec Γ Σ (suc n) (k ∷ ss) T
{- Well-typed session terms -}
data _*_|-_ : (Γ : Context VType) -> (Σ : Context (SType)) -> (t : PType) -> Set where
-- Value receive
_?[-]∙_ : forall {Γ Σ S t}
(k : S <: Σ)
(P : ( Γ , t ) * Σ |- proc)
-> ---------------------------------------------
Γ * ((Σ \\ k) , ([ t ]?∙ S)) |- proc
-- Channel receive
_[_]∙_ : forall {Γ Σ S T}
(k : T <: Σ)
(x : S <: (Σ \\ k))
(P : Γ * Σ |- proc)
-> ---------------------------------------------
Γ * (((Σ \\ k) \\ x) , ([ sess S ]?∙ T)) |- proc
-- Value send
_!<_>∙_ : forall {Γ Σ1 Σ2 S t}
(k : Either (S <: Σ1) (S <: Σ2))
(V : Γ * Σ2 |- val t)
(P : Γ * Σ1 |- proc)
-> -------------------------------------------------------------
Γ * (Case (\k -> (Σ1 \\ k) +++ Σ2)
(\k -> Σ1 +++ (Σ2 \\ k)) k) , ([ t ]!∙ S) |- proc
-- Channel send
_<->∙_ : forall {Γ Σ S T} (k : T <: Σ)
(p : Γ * Σ |- proc)
-> ----------------------------------------------
Γ * (((Σ \\ k) , [ sess S ]!∙ T) , S) |- proc
-- Branch
_▷[_] : forall {Γ Σ n} {Si : Vec (Pair String SType) n}
(k : (& Si) <: Σ)
(pi : DerivVec Γ (Σ \\ k) n (Data.Vec.map pi2 Si) proc)
-> -----------------------------------------------
Γ * Σ |- proc
-- Select
_◁_∙_ : forall {Γ Σ S n} {Si : Vec (Pair String SType) n}
(k : (pi2 S) <: Σ) (mem : S ∈ Si)
(p : Γ * Σ |- proc)
-> -----------------------------------------------
Γ * ((Σ \\ k) , ⊕ Si) |- proc
-- Select - by value (label determined dynamically by a value)
_◁[_]∙_ : forall {Γ Σ n} {S : SType} {Si : Vec (Pair String SType) n}
(k : (S) <: Σ) (v : Γ * Σ |- val nat)
(p : Γ * Σ |- proc)
-> ------------------------------------------
Γ * ((Σ \\ k) , ⊕ Si) |- proc
-- End the process
nil : forall {Γ n} -> --------------------------
Γ * (allEnd {n}) |- proc
-- Value variable
var : forall {Γ Σ t} (x : t <: Γ)
-> ----------------
Γ * Σ |- val t
-- Parallel compose
par : forall {Γ Σ1 Σ2} (p : Γ * Σ1 |- proc)
(q : Γ * Σ2 |- proc)
-> -----------------------
Γ * (Σ1 +++ Σ2) |- proc
-- Session restriction
restrict : forall {Γ Σ s sbar t} (p : Γ * Σ |- t)
(x : s <: Σ)
(xbar : sbar <: (Σ \\ x))
{prf : (dual s) ≡ sbar}
-> --------------------------
Γ * (Σ \\ x) \\ xbar |- t
-- Def
def : forall {Γ Σ t ss} (p : ((Γ , def t ss) , t) * (Σ +++ ss) |- proc)
(q : (Γ , def t ss) * Σ |- proc)
-> ------------------------------------
Γ * Σ |- proc
-- Dvar
dvar : forall {Γ n t vt ss} (e : Γ * allEnd {n} |- val vt)
-> ----------------------------------------------
(Γ , def t ss) * (ss +++ allEnd {n}) |- proc
-- Subtyping on selections
subtype : forall {Γ Σ t n m}
{Si : Vec (Pair String SType) n}
{Ti : Vec (Pair String SType) m}
(k : ⊕ Si <: Σ)
(p : Γ * Σ |- t)
-> -----------------------------------
Γ * (Σ \\ k) , ⊕ (Si Data.Vec.++ Ti) |- t
-- Value constants and operations
unit : forall {Γ Σ} -> --------------------------
Γ * Σ |- val unit
nzero : forall {Γ Σ} -> -----------------------
Γ * Σ |- val nat
nsucc : forall {Γ Σ} (p : Γ * Σ |- val nat)
-> ------------------------
Γ * Σ |- val nat
{- Structural transformations on session terms -}
postulate
weaken : forall {Γ Σ wS pt} -> (e : Γ * Σ |- pt) -> Γ * (Σ , wS) |- pt
weakenG : forall {Γ Σ wT pt} -> (e : Γ * Σ |- pt) -> (Γ , wT) * Σ |- pt
exchg : forall {Γ Σ S T pt} -> (e : Γ * ((Σ , S), T) |- pt) -> Γ * ((Σ , T) , S) |- pt
exchgE : forall {Γ Σ R S T pt} -> (e : Γ * (((Σ , S), T), R) |- pt) -> Γ * (((Σ , T) , S), R) |- pt
exchgEF : forall {Γ Σ R S T pt} -> (e : Γ * (((Σ , S), T), R) |- pt) -> Γ * (((Σ , R) , T), S) |- pt
weakenE : forall {Γ Σ S T pt} -> (e : Γ * (Σ , S) |- pt) -> Γ * ((Σ , T), S) |- pt
{- Example embedding of [ABS] -}
abs : forall {Γ σ τ}
-> (m : (Γ , σ) * (Em , [ τ ]!∙ end) |- proc)
-> (Γ * (Em , [ sess ([ σ ]!∙ [ sess ([ τ ]!∙ end) ]!∙ end) ]!∙ end) |- proc)
abs {Γ} {σ} {τ} m =
let S = [ τ ]!∙ end
T = [ sess ([ τ ]!∙ end) ]?∙ end
e0 = _[_]∙_ here here (weaken {wS = end} m)
e1 = _?[-]∙_ here e0
e1' = weaken e1
e2 = _<->∙_ here e1'
e = restrict e2 (there (there here)) here {refl}
in e